vm-virtio: blk: Expect an identifier upon device creation

This identifier is chosen from the DeviceManager so that it will manage
all identifiers across the VM, which will ensure uniqueness.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2020-04-27 11:21:15 +02:00 committed by Rob Bradford
parent ff9c8b847f
commit be946caf4b
2 changed files with 16 additions and 11 deletions

View File

@ -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<T: DiskFile> {
id: String,
kill_evt: Option<EventFd>,
disk_image: Arc<Mutex<T>>,
disk_path: PathBuf,
@ -916,6 +917,7 @@ impl<T: DiskFile> Block<T> {
///
/// 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<T: DiskFile> Block<T> {
}
Ok(Block {
id,
kill_evt: None,
disk_image: Arc::new(Mutex::new(disk_image)),
disk_path,
@ -1163,19 +1166,18 @@ impl<T: 'static + DiskFile + Send> VirtioDevice for Block<T> {
}
virtio_pausable!(Block, T: 'static + DiskFile + Send);
const BLOCK_SNAPSHOT_ID: &str = "virtio-block";
impl<T: 'static + DiskFile + Send> Snapshottable for Block<T> {
fn id(&self) -> String {
BLOCK_SNAPSHOT_ID.to_string()
self.id.clone()
}
fn snapshot(&self) -> std::result::Result<Snapshot, MigratableError> {
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<T: 'static + DiskFile + Send> Snapshottable for Block<T> {
}
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) => {

View File

@ -1181,9 +1181,13 @@ impl DeviceManager {
&mut self,
disk_cfg: &mut DiskConfig,
) -> DeviceManagerResult<(VirtioDeviceArc, bool, Option<String>)> {
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