diff --git a/vm-virtio/src/block.rs b/vm-virtio/src/block.rs index 60562311d..93615d069 100755 --- a/vm-virtio/src/block.rs +++ b/vm-virtio/src/block.rs @@ -887,6 +887,7 @@ unsafe impl ByteValued for VirtioBlockConfig {} /// Virtio device for exposing block level read/write operations on a host file. pub struct Block { + id: String, kill_evt: Option, disk_image: Arc>, disk_path: PathBuf, @@ -916,6 +917,7 @@ impl Block { /// /// The given file must be seekable and sizable. pub fn new( + id: String, mut disk_image: T, disk_path: PathBuf, is_disk_read_only: bool, @@ -954,6 +956,7 @@ impl Block { } Ok(Block { + id, kill_evt: None, disk_image: Arc::new(Mutex::new(disk_image)), disk_path, @@ -1163,19 +1166,18 @@ impl VirtioDevice for Block { } virtio_pausable!(Block, T: 'static + DiskFile + Send); -const BLOCK_SNAPSHOT_ID: &str = "virtio-block"; impl Snapshottable for Block { fn id(&self) -> String { - BLOCK_SNAPSHOT_ID.to_string() + self.id.clone() } fn snapshot(&self) -> std::result::Result { let snapshot = serde_json::to_vec(&self.state()).map_err(|e| MigratableError::Snapshot(e.into()))?; - let mut block_snapshot = Snapshot::new(BLOCK_SNAPSHOT_ID); + let mut block_snapshot = Snapshot::new(self.id.as_str()); block_snapshot.add_data_section(SnapshotDataSection { - id: format!("{}-section", BLOCK_SNAPSHOT_ID), + id: format!("{}-section", self.id), snapshot, }); @@ -1183,10 +1185,7 @@ impl Snapshottable for Block { } fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { - if let Some(block_section) = snapshot - .snapshot_data - .get(&format!("{}-section", BLOCK_SNAPSHOT_ID)) - { + if let Some(block_section) = snapshot.snapshot_data.get(&format!("{}-section", self.id)) { let block_state = match serde_json::from_slice(&block_section.snapshot) { Ok(state) => state, Err(error) => { diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 4fe9e6b80..46f7db424 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -1181,9 +1181,13 @@ impl DeviceManager { &mut self, disk_cfg: &mut DiskConfig, ) -> DeviceManagerResult<(VirtioDeviceArc, bool, Option)> { - if disk_cfg.id.is_none() { - disk_cfg.id = Some(self.next_device_name(DISK_DEVICE_NAME_PREFIX)?); - } + let id = if let Some(id) = &disk_cfg.id { + id.clone() + } else { + let id = self.next_device_name(DISK_DEVICE_NAME_PREFIX)?; + disk_cfg.id = Some(id.clone()); + id + }; if disk_cfg.vhost_user { let sock = if let Some(sock) = disk_cfg.vhost_socket.clone() { @@ -1235,6 +1239,7 @@ impl DeviceManager { match image_type { ImageType::Raw => { let dev = vm_virtio::Block::new( + id, raw_img, disk_cfg .path @@ -1262,6 +1267,7 @@ impl DeviceManager { let qcow_img = QcowFile::from(raw_img).map_err(DeviceManagerError::QcowDeviceCreate)?; let dev = vm_virtio::Block::new( + id, qcow_img, disk_cfg .path