From b14fd37db9b8560770bc6dbc6bcfb4242cc96e05 Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Thu, 19 Sep 2019 09:52:55 +0200 Subject: [PATCH] vmm: Make --kernel optional The kernel path was the only mandatory command line option. With the addition of the --api-socket option, we can run without a kernel path and get it later through the API. Since we can end up with VM configurations that are no longer valid by default, we need to provide a validation check for it. For now, if the kernel path is not defined, the VM configuration is invalid. Signed-off-by: Samuel Ortiz --- src/main.rs | 26 +++++++++++++------------- vmm/src/config.rs | 27 ++++++++++++++++----------- vmm/src/vm.rs | 3 ++- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7720ba19f..d57a1b80a 100755 --- a/src/main.rs +++ b/src/main.rs @@ -255,9 +255,7 @@ fn main() { let rng = cmd_arguments.value_of("rng").unwrap(); let serial = cmd_arguments.value_of("serial").unwrap(); - let kernel = cmd_arguments - .value_of("kernel") - .expect("Missing argument: kernel"); + let kernel = cmd_arguments.value_of("kernel"); let cmdline = cmd_arguments.value_of("cmdline"); let disks: Option> = cmd_arguments.values_of("disk").map(|x| x.collect()); @@ -332,7 +330,7 @@ fn main() { api_socket_path, u8::from(&vm_config.cpus), vm_config.memory.size >> 20, - vm_config.kernel.path, + vm_config.kernel, vm_config.cmdline.args.as_str(), vm_config.disks, ); @@ -349,15 +347,17 @@ fn main() { } }; - // Create and start the VM based off the VM config we just built. - let sender = api_request_sender.clone(); - vmm::vm_create( - api_evt.try_clone().unwrap(), - api_request_sender, - Arc::new(vm_config), - ) - .expect("Could not create the VM"); - vmm::vm_start(api_evt.try_clone().unwrap(), sender).expect("Could not start the VM"); + if cmd_arguments.is_present("vm-config") && vm_config.valid() { + // Create and start the VM based off the VM config we just built. + let sender = api_request_sender.clone(); + vmm::vm_create( + api_evt.try_clone().unwrap(), + api_request_sender, + Arc::new(vm_config), + ) + .expect("Could not create the VM"); + vmm::vm_start(api_evt.try_clone().unwrap(), sender).expect("Could not start the VM"); + } match vmm_thread.join() { Ok(res) => match res { diff --git a/vmm/src/config.rs b/vmm/src/config.rs index 0281053f6..496b1803b 100644 --- a/vmm/src/config.rs +++ b/vmm/src/config.rs @@ -75,13 +75,15 @@ pub enum Error<'a> { ParseVsockCidParam(std::num::ParseIntError), /// Failed parsing vsock socket path parameter. ParseVsockSockParam, + /// Missing kernel configuration + ValidateMissingKernelConfig, } pub type Result<'a, T> = result::Result>; pub struct VmParams<'a> { pub cpus: &'a str, pub memory: &'a str, - pub kernel: &'a str, + pub kernel: Option<&'a str>, pub cmdline: Option<&'a str>, pub disks: Option>, pub net: Option>, @@ -177,14 +179,6 @@ pub struct KernelConfig { pub path: PathBuf, } -impl KernelConfig { - pub fn parse(kernel: &str) -> Result { - Ok(KernelConfig { - path: PathBuf::from(kernel), - }) - } -} - #[derive(Clone)] pub struct CmdlineConfig { pub args: Cmdline, @@ -617,7 +611,7 @@ impl VhostUserBlkConfig { pub struct VmConfig { pub cpus: CpusConfig, pub memory: MemoryConfig, - pub kernel: KernelConfig, + pub kernel: Option, pub cmdline: CmdlineConfig, pub disks: Option>, pub net: Option>, @@ -633,6 +627,10 @@ pub struct VmConfig { } impl VmConfig { + pub fn valid(&self) -> bool { + self.kernel.is_some() + } + pub fn parse(vm_params: VmParams) -> Result { let mut disks: Option> = None; if let Some(disk_list) = &vm_params.disks { @@ -712,10 +710,17 @@ impl VmConfig { vhost_user_blk = Some(vhost_user_blk_config_list); } + let mut kernel: Option = None; + if let Some(k) = vm_params.kernel { + kernel = Some(KernelConfig { + path: PathBuf::from(k), + }); + } + Ok(VmConfig { cpus: CpusConfig::parse(vm_params.cpus)?, memory: MemoryConfig::parse(vm_params.memory)?, - kernel: KernelConfig::parse(vm_params.kernel)?, + kernel, cmdline: CmdlineConfig::parse(vm_params.cmdline)?, disks, net, diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index b49be3fe4..f975396cc 100755 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -463,7 +463,8 @@ fn get_host_cpu_phys_bits() -> u8 { impl Vm { pub fn new(config: Arc, exit_evt: EventFd, reset_evt: EventFd) -> Result { let kvm = Kvm::new().map_err(Error::KvmNew)?; - let kernel = File::open(&config.kernel.path).map_err(Error::KernelFile)?; + let kernel = + File::open(&config.kernel.as_ref().unwrap().path).map_err(Error::KernelFile)?; let fd = kvm.create_vm().map_err(Error::VmCreate)?; let fd = Arc::new(fd); let creation_ts = std::time::Instant::now();