Skip to content

Commit

Permalink
add as_string() to the Cmdline crate
Browse files Browse the repository at this point in the history
In some cases, having a String representation of the Linux command line
can be useful. This is the case of vmm-reference which otherwise would require a
conversion dance between string types.

Signed-off-by: Alvise Rigo <[email protected]>
  • Loading branch information
arigo-vos committed Nov 14, 2023
1 parent 35cb0d0 commit 431d52c
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/cmdline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,33 @@ impl Cmdline {
}
}

/// Returns a String representation of the command line
///
/// # Examples
///
/// ```rust
/// # use linux_loader::cmdline::*;
/// let mut cl = Cmdline::new(20).unwrap();
/// cl.insert_str("foo").unwrap();
/// cl.insert_init_args("bar").unwrap();
/// assert_eq!(cl.as_string().unwrap(), "foo -- bar");
/// ```
pub fn as_string(&self) -> Result<String> {
if self.boot_args.is_empty() && self.init_args.is_empty() {
Ok("".to_string())
} else if self.boot_args.is_empty() {
Err(Error::NoBootArgsInserted)
} else if self.init_args.is_empty() {
Ok(self.boot_args.to_string())
} else {
Ok(format!(
"{}{}{}",
self.boot_args, INIT_ARGS_SEPARATOR, self.init_args
)
.to_owned())
}
}

/// Adds a virtio MMIO device to the kernel command line.
///
/// Multiple devices can be specified, with multiple `virtio_mmio.device=` options. This
Expand Down

0 comments on commit 431d52c

Please sign in to comment.