linux-kernel-cmdline 0.1.0

A parser for the Linux kernel command line (/proc/cmdline), supporting both byte-level and UTF-8 parsing with proper quote handling and dash/underscore equivalence for parameter keys.
Documentation

linux-kernel-cmdline

A parser for the Linux kernel command line (/proc/cmdline), supporting both byte-level and UTF-8 parsing with proper quote handling and dash/underscore equivalence for parameter keys.

Usage

Add the dependency to your Cargo.toml:

[dependencies]
linux-kernel-cmdline = "0.1"

Parsing the current system's kernel command line

use linux_kernel_cmdline::utf8::Cmdline;

let cmdline = Cmdline::from_proc()?;

if let Some(value) = cmdline.value_of("root") {
    println!("root={value}");
}

for param in cmdline.iter() {
    println!("{param}");
}

Parsing a command line from a string

use linux_kernel_cmdline::utf8::Cmdline;

let cmdline = Cmdline::from("console=ttyS0,115200 root=/dev/sda1 rd.break");

// Find a specific parameter
if let Some(param) = cmdline.find("console") {
    assert_eq!(param.value(), Some("ttyS0,115200"));
}

// Dashes and underscores in keys are treated as equivalent
let cmdline = Cmdline::from("net.ifnames=0 rd.auto-lvm=1");
assert_eq!(cmdline.value_of("rd.auto_lvm"), Some("1"));

Working with non-UTF-8 data

For environments where the kernel command line may contain arbitrary bytes, use the bytes module:

use linux_kernel_cmdline::bytes::Cmdline;

let cmdline = Cmdline::from_proc()?;

if let Some(value) = cmdline.value_of("root") {
    println!("root={}", String::from_utf8_lossy(value));
}

License

Licensed under either of

at your option.