zinit 0.3.8

Process supervisor with dependency management
Documentation
//! Kernel command line parameter parsing

use std::fs;

const CMDLINE_PATH: &str = "/proc/cmdline";
const DEFAULT_MOS_SIZE_GB: u32 = 4;

/// Get the data partition size from kernel parameters
///
/// Parses `mossize=X` from /proc/cmdline where X is size in GB.
/// Returns the default (4GB) if the parameter is not found or invalid.
pub fn get_data_partition_size_gb() -> u32 {
    parse_cmdline_param("mossize").unwrap_or(DEFAULT_MOS_SIZE_GB)
}

/// Parse a numeric parameter from the kernel command line
fn parse_cmdline_param(name: &str) -> Option<u32> {
    let cmdline = fs::read_to_string(CMDLINE_PATH).ok()?;
    parse_param_from_string(&cmdline, name)
}

/// Parse a parameter value from a command line string
fn parse_param_from_string(cmdline: &str, name: &str) -> Option<u32> {
    let prefix = format!("{}=", name);

    for part in cmdline.split_whitespace() {
        if let Some(value) = part.strip_prefix(&prefix)
            && let Ok(num) = value.parse::<u32>()
        {
            return Some(num);
        }
    }

    None
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_mossize() {
        assert_eq!(
            parse_param_from_string("quiet splash mossize=8", "mossize"),
            Some(8)
        );
    }

    #[test]
    fn test_parse_mossize_at_start() {
        assert_eq!(
            parse_param_from_string("mossize=16 quiet", "mossize"),
            Some(16)
        );
    }

    #[test]
    fn test_parse_mossize_missing() {
        assert_eq!(parse_param_from_string("quiet splash", "mossize"), None);
    }

    #[test]
    fn test_parse_mossize_invalid() {
        assert_eq!(parse_param_from_string("mossize=invalid", "mossize"), None);
    }
}