mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2025-02-22 03:12:27 +00:00
vmm: Simplify adding devices to config
To handle that devices are stored in an Option<Vec<T>> and reduce duplicated code use generic function to add the devices to the the struct. Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
parent
f237789753
commit
151668b260
@ -1244,6 +1244,14 @@ impl Vm {
|
||||
Err(Error::ResizeZone)
|
||||
}
|
||||
|
||||
fn add_to_config<T>(devices: &mut Option<Vec<T>>, 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<PciDeviceInfo> {
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user