Skip to content

Commit

Permalink
mount-options: Add Mount Options Support
Browse files Browse the repository at this point in the history
When using zram for file system, we need to enable `discard` mount
option for issue discard/TRIM commands to the underlying zram block
device when blocks are freed. Else deleted files are not zero-ed, so
zram does not remove the compressed pages.

Fixes #89
  • Loading branch information
hswong3i committed Jul 26, 2021
1 parent f42315c commit 064ab12
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ pub struct Device {
pub disksize: u64,

pub swap_priority: i32,
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 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 mount_options: Option<String>, // when set, mount options will be applied
}

impl Device {
Expand All @@ -39,6 +40,7 @@ impl Device {
swap_priority: 100,
mount_point: None,
fs_type: None,
mount_options: None,
}
}

Expand Down Expand Up @@ -295,6 +297,10 @@ fn parse_line(dev: &mut Device, key: &str, value: &str) -> Result<()> {
dev.fs_type = Some(value.to_string());
}

"mount-options" => {
dev.mount_options = Some(value.to_string());
}

_ => {
warn!("Unknown key {}, ignoring.", key);
}
Expand Down
2 changes: 2 additions & 0 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,11 @@ After=systemd-zram-setup@{zram_device}.service
[Mount]
What=/dev/{zram_device}
Where={mount_point:?}
Options={mount_options:?}
",
zram_device = device.name,
mount_point = device.mount_point.as_ref().unwrap(),
mount_options = device.mount_options.as_ref().unwrap_or(&String::new()),
),
)?;

Expand Down
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=""
4 changes: 4 additions & 0 deletions tests/09-mount-options/etc/systemd/zram-generator.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[zram11]
mount-point = /var/compressed
fs-type = ext4
mount-options = discard
3 changes: 3 additions & 0 deletions tests/09-mount-options/proc/meminfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MemTotal: 801322 kB
MemFree: 611992 kB
MemAvailable: 139764 kB
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Automatically generated by "zram-generator"

[Unit]
BindsTo=var-compressed.mount
12 changes: 12 additions & 0 deletions tests/09-mount-options/run.expected/units/var-compressed.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/zram11
Documentation=man:zram-generator(8) man:zram-generator.conf(5)
Requires=systemd-zram-setup@zram11.service
After=systemd-zram-setup@zram11.service

[Mount]
What=/dev/zram11
Where="/var/compressed"
Options="discard"
5 changes: 5 additions & 0 deletions tests/test_cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,8 @@ fn test_07_mount_point() {
fn test_08_plain_device() {
test_generation("08-plain-device").unwrap();
}

#[test]
fn test_09_mount_options() {
test_generation("09-mount-options").unwrap();
}

0 comments on commit 064ab12

Please sign in to comment.