Skip to content

Commit

Permalink
units: split out BindsTo= line to a generated dropin
Browse files Browse the repository at this point in the history
In subsequent commits, systemd-zram-setup@.service will be used
for non-swap purposes, so the BindsTo= line needs to become optional.

Unit descriptions are changed to use titlecase. This is the general
recommended systemd style, because those strings are used in messages
like "Starting ..." and "Stopping ...", and a non-capitalized string
looks strange.
  • Loading branch information
keszybz committed Jan 12, 2021
1 parent dec8b20 commit 4ddc273
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 19 deletions.
56 changes: 43 additions & 13 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ pub fn run_generator(devices: &[Device], output_directory: &Path, fake_mode: boo
Ok(())
}

fn write_contents(output_directory: &Path, filename: &str, contents: &str) -> Result<()> {
let path = output_directory.join(filename);
make_parent(&path)?;

let contents = format!(
"\
# Automatically generated by {exe_name}
{contents}",
exe_name = std::env::current_exe().unwrap().display(),
contents = contents
);

fs::write(&path, contents).with_context(|| format!("Failed to write {:?}", path))
}

fn handle_device(output_directory: &Path, device: &Device) -> Result<u64> {
let swap_name = format!("dev-{}.swap", device.name);
info!(
Expand All @@ -99,14 +115,30 @@ fn handle_device(output_directory: &Path, device: &Device) -> Result<u64> {
device.disksize / 1024 / 1024
);

let swap_path = output_directory.join(&swap_name);
/* systemd-zram-setup@.service.
* We use the packaged unit, and only need to provide a small drop-in. */

let contents = format!(
write_contents(
output_directory,
&format!(
"systemd-zram-setup@{}.service.d/bindsto-swap.conf",
device.name
),
"\
# Automatically generated by {exe_name}
[Unit]
BindsTo=dev-%i.swap
",
)?;

/* dev-zramX.swap */

write_contents(
output_directory,
&swap_name,
&format!(
"\
[Unit]
Description=Compressed swap on /dev/{zram_device}
Description=Compressed Swap on /dev/{zram_device}
Documentation=man:zram-generator(8) man:zram-generator.conf(5)
Requires=systemd-zram-setup@{zram_device}.service
After=systemd-zram-setup@{zram_device}.service
Expand All @@ -115,20 +147,18 @@ After=systemd-zram-setup@{zram_device}.service
What=/dev/{zram_device}
Priority=100
",
exe_name = std::env::current_exe().unwrap().display(),
zram_device = device.name
);
fs::write(&swap_path, contents).with_context(|| {
format!(
"Failed to write a swap service into {}",
swap_path.display()
)
})?;
zram_device = device.name
),
)?;

/* enablement symlink */

let symlink_path = output_directory.join("swap.target.wants").join(&swap_name);
let target_path = format!("../{}", swap_name);
make_symlink(&target_path, &symlink_path)?;

/* Return the device number */

device.name[4..]
.parse()
.with_context(|| format!("zram device \"{}\" number", device.name))
Expand Down
2 changes: 1 addition & 1 deletion tests/01-basic/run.expected/units/dev-zram0.swap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Automatically generated by zram-generator

[Unit]
Description=Compressed swap on /dev/zram0
Description=Compressed Swap on /dev/zram0
Documentation=man:zram-generator(8) man:zram-generator.conf(5)
Requires=systemd-zram-setup@zram0.service
After=systemd-zram-setup@zram0.service
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Automatically generated by zram-generator

[Unit]
BindsTo=dev-%i.swap
2 changes: 1 addition & 1 deletion tests/02-zstd/run.expected/units/dev-zram0.swap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Automatically generated by zram-generator

[Unit]
Description=Compressed swap on /dev/zram0
Description=Compressed Swap on /dev/zram0
Documentation=man:zram-generator(8) man:zram-generator.conf(5)
Requires=systemd-zram-setup@zram0.service
After=systemd-zram-setup@zram0.service
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Automatically generated by zram-generator

[Unit]
BindsTo=dev-%i.swap
2 changes: 1 addition & 1 deletion tests/04-dropins/run.expected/units/dev-zram0.swap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Automatically generated by zram-generator

[Unit]
Description=Compressed swap on /dev/zram0
Description=Compressed Swap on /dev/zram0
Documentation=man:zram-generator(8) man:zram-generator.conf(5)
Requires=systemd-zram-setup@zram0.service
After=systemd-zram-setup@zram0.service
Expand Down
2 changes: 1 addition & 1 deletion tests/04-dropins/run.expected/units/dev-zram2.swap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Automatically generated by zram-generator

[Unit]
Description=Compressed swap on /dev/zram2
Description=Compressed Swap on /dev/zram2
Documentation=man:zram-generator(8) man:zram-generator.conf(5)
Requires=systemd-zram-setup@zram2.service
After=systemd-zram-setup@zram2.service
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Automatically generated by zram-generator

[Unit]
BindsTo=dev-%i.swap
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Automatically generated by zram-generator

[Unit]
BindsTo=dev-%i.swap
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Automatically generated by zram-generator
[Unit]
BindsTo=dev-%i.swap
2 changes: 1 addition & 1 deletion tests/06-kernel-enabled/run.expected/units/dev-zram0.swap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Automatically generated by zram-generator

[Unit]
Description=Compressed swap on /dev/zram0
Description=Compressed Swap on /dev/zram0
Documentation=man:zram-generator(8) man:zram-generator.conf(5)
Requires=systemd-zram-setup@zram0.service
After=systemd-zram-setup@zram0.service
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Automatically generated by zram-generator

[Unit]
BindsTo=dev-%i.swap
1 change: 0 additions & 1 deletion units/systemd-zram-setup@.service.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
Description=Create swap on /dev/%i
Documentation=man:zram-generator(8) man:zram-generator.conf(5)
After=dev-%i.device
BindsTo=dev-%i.swap
DefaultDependencies=false

[Service]
Expand Down

0 comments on commit 4ddc273

Please sign in to comment.