Skip to content

Commit

Permalink
Add options= config key, defaulting to "discard"
Browse files Browse the repository at this point in the history
Supersedes #95
  • Loading branch information
nabijaczleweli committed Oct 13, 2021
1 parent 771df17 commit 31d8579
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 12 deletions.
6 changes: 6 additions & 0 deletions man/zram-generator.conf.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ Devices with the final size of *0* will be discarded.

Also see systemd-makefs(8).

* `options`=

Sets mount or swapon options. Availability depends on `fs-type`.

Defaults to *discard*.

## ENVIRONMENT VARIABLES

Setting `ZRAM_GENERATOR_ROOT` during parsing will cause */proc/meminfo* to be read from *$ZRAM_GENERATOR_ROOT/proc/meminfo* instead,
Expand Down
8 changes: 8 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::{anyhow, Context, Result};
use ini::Ini;
use liboverdrop::FragmentScanner;
use log::{info, warn};
use std::borrow::Cow;
use std::cmp;
use std::collections::{BTreeMap, HashMap};
use std::fmt;
Expand All @@ -25,6 +26,7 @@ pub struct Device {
pub mount_point: Option<PathBuf>, // when set, a mount unit will be created
pub fs_type: Option<String>, // useful mostly for mounts, None is the same
// as "swap" when mount_point is not set
pub options: Cow<'static, str>,
}

impl Device {
Expand All @@ -39,6 +41,7 @@ impl Device {
swap_priority: 100,
mount_point: None,
fs_type: None,
options: "discard".into(),
}
}

Expand Down Expand Up @@ -107,6 +110,7 @@ impl fmt::Display for Device {
Some(alg) => f.write_str(alg)?,
None => f.write_str("<default>")?,
}
write!(f, " options={}", self.options)?;
Ok(())
}
}
Expand Down Expand Up @@ -295,6 +299,10 @@ fn parse_line(dev: &mut Device, key: &str, value: &str) -> Result<()> {
dev.fs_type = Some(value.to_string());
}

"options" => {
dev.options = value.to_string().into();
}

_ => {
warn!("Unknown key {}, ignoring.", key);
}
Expand Down
6 changes: 5 additions & 1 deletion src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,11 @@ After=systemd-zram-setup@{zram_device}.service
[Swap]
What=/dev/{zram_device}
Priority={swap_priority}
Options={options}
",
zram_device = device.name,
swap_priority = device.swap_priority
swap_priority = device.swap_priority,
options = device.options.replace('%', "%%"),
),
)?;

Expand Down Expand Up @@ -285,9 +287,11 @@ After=systemd-zram-setup@{zram_device}.service
[Mount]
What=/dev/{zram_device}
Where={mount_point}
Options={options}
",
zram_device = device.name,
mount_point = device.mount_point.as_ref().unwrap().to_str().unwrap(),
options = device.options.replace('%', "%%"),
),
)?;

Expand Down
1 change: 1 addition & 0 deletions tests/01-basic/run.expected/units/dev-zram0.swap
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ After=systemd-zram-setup@zram0.service
[Swap]
What=/dev/zram0
Priority=100
Options=discard
1 change: 1 addition & 0 deletions tests/02-zstd/run.expected/units/dev-zram0.swap
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ After=systemd-zram-setup@zram0.service
[Swap]
What=/dev/zram0
Priority=100
Options=discard
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[zram2]
zram-fraction=0.8
swap-priority=200
options=
1 change: 1 addition & 0 deletions tests/04-dropins/run.expected/units/dev-zram0.swap
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ After=systemd-zram-setup@zram0.service
[Swap]
What=/dev/zram0
Priority=100
Options=discard
1 change: 1 addition & 0 deletions tests/04-dropins/run.expected/units/dev-zram2.swap
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ After=systemd-zram-setup@zram2.service
[Swap]
What=/dev/zram2
Priority=200
Options=
1 change: 1 addition & 0 deletions tests/06-kernel-enabled/run.expected/units/dev-zram0.swap
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ After=systemd-zram-setup@zram0.service
[Swap]
What=/dev/zram0
Priority=100
Options=discard
5 changes: 5 additions & 0 deletions tests/07-mount-point/etc/systemd/zram-generator.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[zram11]
mount-point = /var/compressed
fs-type = ext4

[zram12]
mount-point = /var/folded
fs-type = ext4
options = discard,casefold
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Automatically generated by "zram-generator"

[Unit]
BindsTo=var-folded.mount
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ After=systemd-zram-setup@zram11.service
[Mount]
What=/dev/zram11
Where=/var/compressed
Options=discard
12 changes: 12 additions & 0 deletions tests/07-mount-point/run.expected/units/var-folded.mount
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Automatically generated by "/home/zbyszek/src/zram-generator/target/debug/zram-generator"

[Unit]
Description=Compressed Storage on /dev/zram12
Documentation=man:zram-generator(8) man:zram-generator.conf(5)
Requires=systemd-zram-setup@zram12.service
After=systemd-zram-setup@zram12.service

[Mount]
What=/dev/zram12
Where=/var/folded
Options=discard,casefold
42 changes: 31 additions & 11 deletions tests/test_cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ fn test_generation(name: &str) -> Result<Vec<config::Device>> {
assert!(d.is_swap());
assert_eq!(d.host_memory_limit_mb, None);
assert_eq!(d.zram_fraction, 0.5);
assert_eq!(d.options, "discard");
}

"02-zstd" => {
Expand All @@ -59,6 +60,7 @@ fn test_generation(name: &str) -> Result<Vec<config::Device>> {
assert_eq!(d.host_memory_limit_mb.unwrap(), 2050);
assert_eq!(d.zram_fraction, 0.75);
assert_eq!(d.compression_algorithm.as_ref().unwrap(), "zstd");
assert_eq!(d.options, "discard");
}

"03-too-much-memory" => {
Expand All @@ -75,10 +77,12 @@ fn test_generation(name: &str) -> Result<Vec<config::Device>> {
"zram0" => {
assert_eq!(d.host_memory_limit_mb.unwrap(), 1235);
assert_eq!(d.zram_fraction, 0.5);
assert_eq!(d.options, "discard");
}
"zram2" => {
assert!(d.host_memory_limit_mb.is_none());
assert_eq!(d.zram_fraction, 0.8);
assert_eq!(d.options, "");
}
_ => panic!("Unexpected device {}", d),
}
Expand All @@ -95,20 +99,35 @@ fn test_generation(name: &str) -> Result<Vec<config::Device>> {
assert!(d.is_swap());
assert_eq!(d.host_memory_limit_mb, None);
assert_eq!(d.zram_fraction, 0.5);
assert_eq!(d.options, "discard");
}

"07-mount-point" => {
assert_eq!(devices.len(), 1);
let d = devices.iter().next().unwrap();
assert!(!d.is_swap());
assert_eq!(d.host_memory_limit_mb, None);
assert_eq!(d.zram_fraction, 0.5);
assert_eq!(
d.mount_point.as_ref().unwrap(),
Path::new("/var/compressed")
);
assert_eq!(d.fs_type.as_ref().unwrap(), "ext4");
assert_eq!(d.effective_fs_type(), "ext4");
assert_eq!(devices.len(), 2);
for d in &devices {
assert!(!d.is_swap());
assert_eq!(d.host_memory_limit_mb, None);
assert_eq!(d.zram_fraction, 0.5);
assert_eq!(d.fs_type.as_ref().unwrap(), "ext4");
assert_eq!(d.effective_fs_type(), "ext4");
match &d.name[..] {
"zram11" => {
assert_eq!(
d.mount_point.as_ref().unwrap(),
Path::new("/var/compressed")
);
assert_eq!(d.options, "discard");
}
"zram12" => {
assert_eq!(
d.mount_point.as_ref().unwrap(),
Path::new("/var/folded")
);
assert_eq!(d.options, "discard,casefold");
}
_ => panic!("Unexpected device {}", d),
}
}
}

"08-plain-device" => {
Expand All @@ -120,6 +139,7 @@ fn test_generation(name: &str) -> Result<Vec<config::Device>> {
assert!(d.mount_point.is_none());
assert_eq!(d.fs_type.as_ref().unwrap(), "ext2");
assert_eq!(d.effective_fs_type(), "ext2");
assert_eq!(d.options, "discard");
}

_ => (),
Expand Down
5 changes: 5 additions & 0 deletions zram-generator.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ max-zram-size = 512
# or leave unspecified to keep the kernel default.
compression-algorithm = lzo-rle

# By default, filesystems and swap areas are trimmed on-the-go
# by setting "discard".
# Setting this to the empty string clears the option.
options =

[zram1]
# This section describes the settings for /dev/zram1.
#
Expand Down

0 comments on commit 31d8579

Please sign in to comment.