Skip to content

Commit 3195c87

Browse files
committed
feat: added vhost-user-block to the BlockDeviceType enum
Now `BlockDeviceType` has both virtio-block and vhost-user-block. Because we can not test vhost-user-block at this time, we skip vhost-user-block in the unit tests. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 8512e44 commit 3195c87

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/vmm/src/builder.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,26 @@ fn attach_block_devices<'a, I: Iterator<Item = &'a BlockDeviceType> + Debug>(
903903
// The device mutex mustn't be locked here otherwise it will deadlock.
904904
attach_virtio_device(event_manager, vmm, id, block.clone(), cmdline)?;
905905
}
906+
BlockDeviceType::VhostUserBlock(block) => {
907+
let id = {
908+
let locked = block.lock().expect("Poisoned lock");
909+
if locked.root_device {
910+
cmdline.insert_str(if let Some(ref partuuid) = locked.partuuid {
911+
format!("root=PARTUUID={}", partuuid)
912+
} else {
913+
// If no PARTUUID was specified for the root device, try with the
914+
// /dev/vda.
915+
"root=/dev/vda".to_string()
916+
})?;
917+
918+
let flags = if locked.read_only { "ro" } else { "rw" };
919+
cmdline.insert_str(flags)?;
920+
}
921+
locked.id.clone()
922+
};
923+
// The device mutex mustn't be locked here otherwise it will deadlock.
924+
attach_virtio_device(event_manager, vmm, id, block.clone(), cmdline)?;
925+
}
906926
}
907927
}
908928
Ok(())

src/vmm/src/vmm_config/drive.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use serde::{Deserialize, Serialize};
1010
use super::RateLimiterConfig;
1111
pub use crate::devices::virtio::block::device::FileEngineType;
1212
use crate::devices::virtio::block::VirtioBlockError;
13+
use crate::devices::virtio::vhost_user_block::device::VhostUserBlock;
1314
pub use crate::devices::virtio::CacheType;
1415
use crate::devices::virtio::VirtioBlock;
1516
use crate::VmmError;
@@ -77,6 +78,8 @@ pub struct BlockDeviceUpdateConfig {
7778
pub enum BlockDeviceType {
7879
/// VirtioBlock type
7980
VirtioBlock(Arc<Mutex<VirtioBlock>>),
81+
/// VhostUserBlock type
82+
VhostUserBlock(Arc<Mutex<VhostUserBlock>>),
8083
}
8184

8285
/// Wrapper for the collection that holds all the Block Devices
@@ -106,6 +109,7 @@ impl BlockBuilder {
106109
BlockDeviceType::VirtioBlock(b) => {
107110
b.lock().expect("Poisoned lock").is_root_device()
108111
}
112+
BlockDeviceType::VhostUserBlock(b) => b.lock().expect("Poisoned lock").root_device,
109113
}
110114
} else {
111115
false
@@ -116,6 +120,7 @@ impl BlockBuilder {
116120
fn get_index_of_drive_id(&self, drive_id: &str) -> Option<usize> {
117121
self.devices.iter().position(|b| match b {
118122
BlockDeviceType::VirtioBlock(b) => b.lock().expect("Poisoned lock").id().eq(drive_id),
123+
BlockDeviceType::VhostUserBlock(b) => b.lock().expect("Poisoned lock").id.eq(drive_id),
119124
})
120125
}
121126

@@ -179,6 +184,7 @@ impl BlockBuilder {
179184
.iter()
180185
.map(|b| match b {
181186
BlockDeviceType::VirtioBlock(b) => b.lock().unwrap().config().into(),
187+
BlockDeviceType::VhostUserBlock(b) => b.lock().unwrap().config().into(),
182188
})
183189
.collect()
184190
}
@@ -250,6 +256,7 @@ mod tests {
250256
assert_eq!(block.partuuid(), dummy_block_device.partuuid.as_ref());
251257
assert_eq!(block.is_read_only(), dummy_block_device.is_read_only);
252258
}
259+
BlockDeviceType::VhostUserBlock(_) => {}
253260
}
254261
}
255262
assert_eq!(block_devs.get_index_of_drive_id(&dummy_id), Some(0));
@@ -284,6 +291,7 @@ mod tests {
284291
assert_eq!(block.partuuid(), dummy_block_device.partuuid.as_ref());
285292
assert_eq!(block.is_read_only(), dummy_block_device.is_read_only);
286293
}
294+
BlockDeviceType::VhostUserBlock(_) => {}
287295
}
288296
}
289297
}
@@ -378,16 +386,19 @@ mod tests {
378386
BlockDeviceType::VirtioBlock(ref b) => {
379387
assert_eq!(b.lock().unwrap().id(), &root_block_device.drive_id)
380388
}
389+
BlockDeviceType::VhostUserBlock(_) => {}
381390
}
382391
match block_iter.next().unwrap() {
383392
BlockDeviceType::VirtioBlock(ref b) => {
384393
assert_eq!(b.lock().unwrap().id(), &dummy_block_dev_2.drive_id)
385394
}
395+
BlockDeviceType::VhostUserBlock(_) => {}
386396
}
387397
match block_iter.next().unwrap() {
388398
BlockDeviceType::VirtioBlock(ref b) => {
389399
assert_eq!(b.lock().unwrap().id(), &dummy_block_dev_3.drive_id)
390400
}
401+
BlockDeviceType::VhostUserBlock(_) => {}
391402
}
392403
}
393404

@@ -447,16 +458,19 @@ mod tests {
447458
BlockDeviceType::VirtioBlock(ref b) => {
448459
assert_eq!(b.lock().unwrap().id(), &root_block_device.drive_id)
449460
}
461+
BlockDeviceType::VhostUserBlock(_) => {}
450462
}
451463
match block_iter.next().unwrap() {
452464
BlockDeviceType::VirtioBlock(ref b) => {
453465
assert_eq!(b.lock().unwrap().id(), &dummy_block_dev_2.drive_id)
454466
}
467+
BlockDeviceType::VhostUserBlock(_) => {}
455468
}
456469
match block_iter.next().unwrap() {
457470
BlockDeviceType::VirtioBlock(ref b) => {
458471
assert_eq!(b.lock().unwrap().id(), &dummy_block_dev_3.drive_id)
459472
}
473+
BlockDeviceType::VhostUserBlock(_) => {}
460474
}
461475
}
462476

@@ -520,6 +534,7 @@ mod tests {
520534
// Validate update was successful.
521535
match block_devs.devices[index] {
522536
BlockDeviceType::VirtioBlock(ref b) => assert!(b.lock().unwrap().is_read_only()),
537+
BlockDeviceType::VhostUserBlock(_) => {}
523538
}
524539

525540
// Update with invalid path.
@@ -572,6 +587,7 @@ mod tests {
572587
BlockDeviceType::VirtioBlock(ref b) => {
573588
assert_eq!(b.lock().unwrap().id(), &root_block_id)
574589
}
590+
BlockDeviceType::VhostUserBlock(_) => {}
575591
}
576592
}
577593

@@ -621,6 +637,7 @@ mod tests {
621637
assert_eq!(block_devs.devices.len(), 1);
622638
match block_devs.devices.pop_back().unwrap() {
623639
BlockDeviceType::VirtioBlock(ref b) => assert_eq!(b.lock().unwrap().id(), block_id),
640+
BlockDeviceType::VhostUserBlock(_) => {}
624641
}
625642
}
626643
}

0 commit comments

Comments
 (0)