diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index cd353fdef..636458473 100644 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -1244,6 +1244,14 @@ impl Vm { Err(Error::ResizeZone) } + fn add_to_config(devices: &mut Option>, device: T) { + if let Some(devices) = devices { + devices.push(device); + } else { + *devices = Some(vec![device]); + } + } + pub fn add_device(&mut self, mut _device_cfg: DeviceConfig) -> Result { let pci_device_info = self .device_manager @@ -1256,11 +1264,7 @@ impl Vm { // ensure the device would be created in case of a reboot. { let mut config = self.config.lock().unwrap(); - if let Some(devices) = config.devices.as_mut() { - devices.push(_device_cfg); - } else { - config.devices = Some(vec![_device_cfg]); - } + Self::add_to_config(&mut config.devices, _device_cfg); } self.device_manager @@ -1330,11 +1334,7 @@ impl Vm { // ensure the device would be created in case of a reboot. { let mut config = self.config.lock().unwrap(); - if let Some(disks) = config.disks.as_mut() { - disks.push(_disk_cfg); - } else { - config.disks = Some(vec![_disk_cfg]); - } + Self::add_to_config(&mut config.disks, _disk_cfg); } self.device_manager @@ -1358,11 +1358,7 @@ impl Vm { // ensure the device would be created in case of a reboot. { let mut config = self.config.lock().unwrap(); - if let Some(fs_config) = config.fs.as_mut() { - fs_config.push(_fs_cfg); - } else { - config.fs = Some(vec![_fs_cfg]); - } + Self::add_to_config(&mut config.fs, _fs_cfg); } self.device_manager @@ -1386,11 +1382,7 @@ impl Vm { // ensure the device would be created in case of a reboot. { let mut config = self.config.lock().unwrap(); - if let Some(pmem) = config.pmem.as_mut() { - pmem.push(_pmem_cfg); - } else { - config.pmem = Some(vec![_pmem_cfg]); - } + Self::add_to_config(&mut config.pmem, _pmem_cfg); } self.device_manager @@ -1414,11 +1406,7 @@ impl Vm { // ensure the device would be created in case of a reboot. { let mut config = self.config.lock().unwrap(); - if let Some(net) = config.net.as_mut() { - net.push(_net_cfg); - } else { - config.net = Some(vec![_net_cfg]); - } + Self::add_to_config(&mut config.net, _net_cfg); } self.device_manager