vmm: device_manager: Refactor make_virtio_block_devices

Split it into a method that creates a single device which is called by
the multiple device version so this can be used when dynamically adding
a device.

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2020-03-13 09:38:42 +00:00
parent 66da29d8dd
commit 2be703ca92

View File

@ -1054,12 +1054,10 @@ impl DeviceManager {
Ok(sock) Ok(sock)
} }
fn make_virtio_block_devices(&mut self) -> DeviceManagerResult<Vec<(VirtioDeviceArc, bool)>> { fn make_virtio_block_device(
let mut devices = Vec::new(); &mut self,
disk_cfg: &DiskConfig,
let block_devices = self.config.lock().unwrap().disks.clone(); ) -> DeviceManagerResult<(VirtioDeviceArc, bool)> {
if let Some(disk_list_cfg) = &block_devices {
for disk_cfg in disk_list_cfg.iter() {
if disk_cfg.vhost_user { if disk_cfg.vhost_user {
let sock = if let Some(sock) = disk_cfg.vhost_socket.clone() { let sock = if let Some(sock) = disk_cfg.vhost_socket.clone() {
sock sock
@ -1076,14 +1074,13 @@ impl DeviceManager {
.map_err(DeviceManagerError::CreateVhostUserBlk)?, .map_err(DeviceManagerError::CreateVhostUserBlk)?,
)); ));
devices.push((
Arc::clone(&vhost_user_block_device)
as Arc<Mutex<dyn vm_virtio::VirtioDevice>>,
false,
));
self.migratable_devices self.migratable_devices
.push(Arc::clone(&vhost_user_block_device) as Arc<Mutex<dyn Migratable>>); .push(Arc::clone(&vhost_user_block_device) as Arc<Mutex<dyn Migratable>>);
Ok((
Arc::clone(&vhost_user_block_device) as Arc<Mutex<dyn vm_virtio::VirtioDevice>>,
false,
))
} else { } else {
let mut options = OpenOptions::new(); let mut options = OpenOptions::new();
options.read(true); options.read(true);
@ -1094,10 +1091,11 @@ impl DeviceManager {
// Open block device path // Open block device path
let image: File = options let image: File = options
.open( .open(
&disk_cfg disk_cfg
.path .path
.as_ref() .as_ref()
.ok_or(DeviceManagerError::NoDiskPath)?, .ok_or(DeviceManagerError::NoDiskPath)?
.clone(),
) )
.map_err(DeviceManagerError::Disk)?; .map_err(DeviceManagerError::Disk)?;
@ -1123,16 +1121,17 @@ impl DeviceManager {
let block = Arc::new(Mutex::new(dev)); let block = Arc::new(Mutex::new(dev));
devices.push((
Arc::clone(&block) as Arc<Mutex<dyn vm_virtio::VirtioDevice>>,
disk_cfg.iommu,
));
self.migratable_devices self.migratable_devices
.push(Arc::clone(&block) as Arc<Mutex<dyn Migratable>>); .push(Arc::clone(&block) as Arc<Mutex<dyn Migratable>>);
Ok((
Arc::clone(&block) as Arc<Mutex<dyn vm_virtio::VirtioDevice>>,
disk_cfg.iommu,
))
} }
ImageType::Qcow2 => { ImageType::Qcow2 => {
let qcow_img = QcowFile::from(raw_img) let qcow_img =
.map_err(DeviceManagerError::QcowDeviceCreate)?; QcowFile::from(raw_img).map_err(DeviceManagerError::QcowDeviceCreate)?;
let dev = vm_virtio::Block::new( let dev = vm_virtio::Block::new(
qcow_img, qcow_img,
disk_cfg disk_cfg
@ -1149,18 +1148,28 @@ impl DeviceManager {
let block = Arc::new(Mutex::new(dev)); let block = Arc::new(Mutex::new(dev));
devices.push((
Arc::clone(&block) as Arc<Mutex<dyn vm_virtio::VirtioDevice>>,
disk_cfg.iommu,
));
self.migratable_devices self.migratable_devices
.push(Arc::clone(&block) as Arc<Mutex<dyn Migratable>>); .push(Arc::clone(&block) as Arc<Mutex<dyn Migratable>>);
Ok((
Arc::clone(&block) as Arc<Mutex<dyn vm_virtio::VirtioDevice>>,
disk_cfg.iommu,
))
} }
};
} }
} }
} }
fn make_virtio_block_devices(&mut self) -> DeviceManagerResult<Vec<(VirtioDeviceArc, bool)>> {
let mut devices = Vec::new();
let block_devices = self.config.lock().unwrap().disks.clone();
if let Some(disk_list_cfg) = &block_devices {
for disk_cfg in disk_list_cfg.iter() {
devices.push(self.make_virtio_block_device(disk_cfg)?);
}
}
Ok(devices) Ok(devices)
} }