From d9ce29117e9e513279a8e198df8f93ba5d916a48 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Mon, 8 Jul 2019 15:48:39 -0700 Subject: [PATCH] vmm: Flag --disk should be optional Now that cloud-hypervisor VMM supports virtio-pmem, it can directly boot a VM from an image exposed as a persistent memory block device. That's why there is no need to force the --disk option as being mandatory. Fixes #90 Signed-off-by: Sebastien Boeuf --- src/main.rs | 5 +--- vmm/src/config.rs | 14 +++++---- vmm/src/vm.rs | 72 +++++++++++++++++++++++++---------------------- 3 files changed, 48 insertions(+), 43 deletions(-) diff --git a/src/main.rs b/src/main.rs index a63ad0574..906dbbbea 100755 --- a/src/main.rs +++ b/src/main.rs @@ -100,10 +100,7 @@ fn main() { let cmdline = cmd_arguments.value_of("cmdline"); - let disks: Vec<&str> = cmd_arguments - .values_of("disk") - .expect("Missing argument: disk. Provide at least one") - .collect(); + let disks: Option> = cmd_arguments.values_of("disk").map(|x| x.collect()); let net: Option> = cmd_arguments.values_of("net").map(|x| x.collect()); diff --git a/vmm/src/config.rs b/vmm/src/config.rs index c395672fc..dc86cfe15 100644 --- a/vmm/src/config.rs +++ b/vmm/src/config.rs @@ -58,7 +58,7 @@ pub struct VmParams<'a> { pub memory: &'a str, pub kernel: &'a str, pub cmdline: Option<&'a str>, - pub disks: Vec<&'a str>, + pub disks: Option>, pub net: Option>, pub rng: &'a str, pub fs: Option>, @@ -343,7 +343,7 @@ pub struct VmConfig<'a> { pub memory: MemoryConfig<'a>, pub kernel: KernelConfig<'a>, pub cmdline: CmdlineConfig, - pub disks: Vec>, + pub disks: Option>>, pub net: Option>>, pub rng: RngConfig<'a>, pub fs: Option>>, @@ -352,9 +352,13 @@ pub struct VmConfig<'a> { impl<'a> VmConfig<'a> { pub fn parse(vm_params: VmParams<'a>) -> Result { - let mut disks: Vec = Vec::new(); - for disk in vm_params.disks.iter() { - disks.push(DiskConfig::parse(disk)?); + let mut disks: Option> = None; + if let Some(disk_list) = &vm_params.disks { + let mut disk_config_list = Vec::new(); + for item in disk_list.iter() { + disk_config_list.push(DiskConfig::parse(item)?); + } + disks = Some(disk_config_list); } let mut net: Option> = None; diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index a7c0a17a4..e919061df 100755 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -528,42 +528,46 @@ impl DeviceManager { let pci_root = PciRoot::new(None); let mut pci = PciConfigIo::new(pci_root); - for disk_cfg in &vm_cfg.disks { - // Open block device path - let raw_img: File = OpenOptions::new() - .read(true) - .write(true) - .open(disk_cfg.path) - .map_err(DeviceManagerError::Disk)?; + // Add virtio-blk if required + if let Some(disk_list_cfg) = &vm_cfg.disks { + for disk_cfg in disk_list_cfg.iter() { + // Open block device path + let raw_img: File = OpenOptions::new() + .read(true) + .write(true) + .open(disk_cfg.path) + .map_err(DeviceManagerError::Disk)?; - // Add virtio-blk - let image_type = - qcow::detect_image_type(&raw_img).map_err(DeviceManagerError::DetectImageType)?; - let block = match image_type { - ImageType::Raw => { - let raw_img = vm_virtio::RawFile::new(raw_img); - let dev = vm_virtio::Block::new(raw_img, disk_cfg.path.to_path_buf(), false) - .map_err(DeviceManagerError::CreateVirtioBlock)?; - Box::new(dev) as Box - } - ImageType::Qcow2 => { - let qcow_img = - QcowFile::from(raw_img).map_err(DeviceManagerError::QcowDeviceCreate)?; - let dev = vm_virtio::Block::new(qcow_img, disk_cfg.path.to_path_buf(), false) - .map_err(DeviceManagerError::CreateVirtioBlock)?; - Box::new(dev) as Box - } - }; + let image_type = qcow::detect_image_type(&raw_img) + .map_err(DeviceManagerError::DetectImageType)?; + let block = match image_type { + ImageType::Raw => { + let raw_img = vm_virtio::RawFile::new(raw_img); + let dev = + vm_virtio::Block::new(raw_img, disk_cfg.path.to_path_buf(), false) + .map_err(DeviceManagerError::CreateVirtioBlock)?; + Box::new(dev) as Box + } + ImageType::Qcow2 => { + let qcow_img = QcowFile::from(raw_img) + .map_err(DeviceManagerError::QcowDeviceCreate)?; + let dev = + vm_virtio::Block::new(qcow_img, disk_cfg.path.to_path_buf(), false) + .map_err(DeviceManagerError::CreateVirtioBlock)?; + Box::new(dev) as Box + } + }; - DeviceManager::add_virtio_pci_device( - block, - memory.clone(), - allocator, - vm_fd, - &mut pci, - &mut mmio_bus, - &interrupt_info, - )?; + DeviceManager::add_virtio_pci_device( + block, + memory.clone(), + allocator, + vm_fd, + &mut pci, + &mut mmio_bus, + &interrupt_info, + )?; + } } // Add virtio-net if required