Skip to content

Commit

Permalink
Disable generator when inst.zram is in /proc/cmdline
Browse files Browse the repository at this point in the history
Closes #42.
  • Loading branch information
keszybz committed Jul 9, 2020
1 parent 7b6c6e1 commit 1196558
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
3 changes: 3 additions & 0 deletions man/zram-generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ The generator will be invoked by systemd early at boot. The generator will then:

The generator does nothing if run inside a container (as determined by *systemd-detect-virt(8) --container*).

For compatibility with Anaconda (the OS installer used by Fedora, RHEL, CentOS and other Linux distributions),
the generator will also do nothing when `inst.zram` is present on the kernel command line.

Setting the `ZRAM_GENERATOR_ROOT` environment variable makes the generator run in test mode, in which case containerisation is ignored and step `3` is skipped.<br />
For the ramifications of `ZRAM_GENERATOR_ROOT` on config handling, see zram-generator.conf(5).

Expand Down
52 changes: 52 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,27 @@ fn get_total_memory_kb(root: &Path) -> Result<u64> {
_get_total_memory_kb(&path)
}

fn _kernel_has_option(path: &Path, word: &str) -> Result<bool> {
let text = fs::read_to_string(path)?;

// The last argument wins, so check all words in turn.
let mut val = false;
for w in text.split_whitespace() {
if w == word || w == word.to_owned() + "=1" || w == word.to_owned() + "=on" {
val = true;
} else if w == word.to_owned() + "=0" || w == word.to_owned() + "=off" {
val = false;
}
}

return Ok(val);
}

pub fn kernel_has_option(root: &Path, word: &str) -> Result<bool> {
let path = root.join("proc/cmdline");
_kernel_has_option(&path, word)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -290,4 +311,35 @@ MemTotal:: 8013220 kB
file.flush().unwrap();
_get_total_memory_kb(file.path()).unwrap();
}

#[test]
fn test_kernel_has_option() {
let mut file = tempfile::NamedTempFile::new().unwrap();

file.write(
b"\
foo=1 foo=0 foo=on foo=off foo
",
)
.unwrap();
file.flush().unwrap();
let foo = _kernel_has_option(file.path(), "foo").unwrap();
assert_eq!(foo, true);
}

#[test]
fn test_kernel_has_no_option() {
let mut file = tempfile::NamedTempFile::new().unwrap();

file.write(
b"\
foo=1
foo=0
",
)
.unwrap();
file.flush().unwrap();
let foo = _kernel_has_option(file.path(), "foo").unwrap();
assert_eq!(foo, false);
}
}
10 changes: 10 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod setup;

use anyhow::Result;
use clap::{crate_description, crate_name, crate_version, App, Arg};
use log::{info, warn};
use std::borrow::Cow;
use std::env;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -67,6 +68,15 @@ fn main() -> Result<()> {

match get_opts() {
Opts::GenerateUnits(target) => {
match config::kernel_has_option(root, "inst.zram") {
Ok(true) => {
info!("Disabled by inst.zram option in /proc/cmdline, exiting.");
return Ok(());
}
Ok(false) => {}
Err(_) => warn!("Failed to parse /proc/cmdline, ignoring.")
}

let devices = config::read_all_devices(&root)?;
let output_directory = PathBuf::from(target);
generator::run_generator(&devices, &output_directory, have_env_var)
Expand Down

0 comments on commit 1196558

Please sign in to comment.