From 112418d92845bf549d200f16c082743bc2375805 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Mon, 6 May 2019 13:24:57 -0700 Subject: [PATCH] main: Add kernel command line support In order to let the user choose which kernel parameters to append, the kernel boot parameters can be now specified from the command line. Signed-off-by: Sebastien Boeuf --- src/main.rs | 13 ++++++++++++- vmm/src/vm.rs | 32 +++++++++----------------------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/main.rs b/src/main.rs index 92f966bbe..64380d766 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,12 @@ fn main() { .help("Path to kernel image (vmlinux)") .takes_value(true), ) + .arg( + Arg::with_name("cmdline") + .long("cmdline") + .help("Kernel command line") + .takes_value(true), + ) .arg( Arg::with_name("disk") .long("disk") @@ -51,6 +57,11 @@ fn main() { .expect("Missing argument: kernel"); let kernel_path = kernel_arg.as_path(); + let cmdline = cmd_arguments + .value_of("cmdline") + .map(std::string::ToString::to_string) + .expect("Missing argument: cmdline"); + let disk_arg = cmd_arguments .value_of("disk") .map(PathBuf::from) @@ -70,7 +81,7 @@ fn main() { println!("VM [{} vCPUS {} MB of memory]", vcpus, memory); println!("Booting {:?}...", kernel_path); - let vm_config = VmConfig::new(kernel_path, disk_path, vcpus, memory).unwrap(); + let vm_config = VmConfig::new(kernel_path, disk_path, cmdline, vcpus, memory).unwrap(); vmm::boot_kernel(vm_config).unwrap(); } diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index 54ae20889..78b904b4f 100755 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -40,8 +40,6 @@ use vmm_sys_util::EventFd; const VCPU_RTSIG_OFFSET: i32 = 0; pub const DEFAULT_VCPUS: u8 = 1; pub const DEFAULT_MEMORY: GuestUsize = 512; -const DEFAULT_CMDLINE: &str = "console=ttyS0 reboot=k panic=1 nomodules \ - i8042.noaux i8042.nomux i8042.nopnp i8042.dumbkbd"; const CMDLINE_OFFSET: GuestAddress = GuestAddress(0x20000); const X86_64_IRQ_BASE: u32 = 5; @@ -207,7 +205,7 @@ impl Vcpu { pub struct VmConfig<'a> { kernel_path: &'a Path, disk_path: &'a Path, - cmdline: Option, + cmdline: cmdline::Cmdline, cmdline_addr: GuestAddress, memory_size: GuestUsize, @@ -218,36 +216,24 @@ impl<'a> VmConfig<'a> { pub fn new( kernel_path: &'a Path, disk_path: &'a Path, + cmdline_str: String, vcpus: u8, memory_size: GuestUsize, ) -> Result { + let mut cmdline = cmdline::Cmdline::new(arch::CMDLINE_MAX_SIZE); + cmdline.insert_str(cmdline_str).unwrap(); + Ok(VmConfig { kernel_path, disk_path, + cmdline, + cmdline_addr: CMDLINE_OFFSET, memory_size, vcpu_count: vcpus, - ..Default::default() }) } } -impl<'a> Default for VmConfig<'a> { - fn default() -> Self { - let line = String::from(DEFAULT_CMDLINE); - let mut cmdline = cmdline::Cmdline::new(arch::CMDLINE_MAX_SIZE); - cmdline.insert_str(line).unwrap(); - - VmConfig { - kernel_path: Path::new(""), - disk_path: Path::new(""), - cmdline: Some(cmdline), - cmdline_addr: CMDLINE_OFFSET, - memory_size: DEFAULT_MEMORY, - vcpu_count: DEFAULT_VCPUS, - } - } -} - struct DeviceManager { io_bus: devices::Bus, mmio_bus: devices::Bus, @@ -518,8 +504,8 @@ impl<'a> Vm<'a> { } pub fn load_kernel(&mut self) -> Result { - let cmdline = self.config.cmdline.clone().ok_or(Error::CmdLine)?; - let cmdline_cstring = CString::new(cmdline).map_err(|_| Error::CmdLine)?; + let cmdline_cstring = + CString::new(self.config.cmdline.clone()).map_err(|_| Error::CmdLine)?; let entry_addr = linux_loader::loader::Elf::load( &self.memory, None,