From a54d95966b18a854028ce50f37897b83a22a10be Mon Sep 17 00:00:00 2001 From: Alex Lyn Date: Thu, 9 Oct 2025 15:52:33 +0800 Subject: [PATCH 1/2] runtime-rs: Support virtio-scsi for initdata within non-TEE This commit introduces support for selecting `virtio-scsi` as the block device driver for QEMU during initial setup. The primary goal is to resolve a conflict in non-TEE environments: 1. The global block device configuration defaults to `virtio-scsi`. 2. The `initdata` device driver was previously designed and hardcoded to `virtio-blk-pci`. 3. This conflict prevented unified block device usage. By allowing `virtio-scsi` to be configured at cold boot, the `initdata` device can now correctly adhere to the global setting, eliminating the need for a hardcoded driver and ensuring consistent block device configuration across all supported devices (excluding rootfs). Signed-off-by: Alex Lyn --- .../hypervisor/src/qemu/cmdline_generator.rs | 72 +++++++++++++++++-- .../crates/hypervisor/src/qemu/inner.rs | 3 +- 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/src/runtime-rs/crates/hypervisor/src/qemu/cmdline_generator.rs b/src/runtime-rs/crates/hypervisor/src/qemu/cmdline_generator.rs index 1ff80da7ed..c97127119b 100644 --- a/src/runtime-rs/crates/hypervisor/src/qemu/cmdline_generator.rs +++ b/src/runtime-rs/crates/hypervisor/src/qemu/cmdline_generator.rs @@ -979,6 +979,54 @@ impl ToQemuParams for BlockBackend { } } +#[derive(Debug)] +struct DeviceScsiHd { + device: String, + + bus: String, + + drive: String, + + devno: Option, +} + +impl DeviceScsiHd { + fn new(id: &str, bus: &str, devno: Option) -> DeviceScsiHd { + DeviceScsiHd { + device: "scsi-hd".to_owned(), + bus: bus.to_owned(), + drive: id.to_owned(), + devno, + } + } + + #[allow(dead_code)] + fn set_scsi_bus(&mut self, bus: &str) -> &mut Self { + self.bus = bus.to_owned(); + self + } + + #[allow(dead_code)] + fn set_scsi_drive(&mut self, drive: &str) -> &mut Self { + self.drive = drive.to_owned(); + self + } +} + +#[async_trait] +impl ToQemuParams for DeviceScsiHd { + async fn qemu_params(&self) -> Result> { + let mut params = Vec::new(); + params.push(self.device.clone()); + params.push(format!("drive=image-{}", self.drive)); + params.push(format!("bus={}", self.bus)); + if let Some(devno) = &self.devno { + params.push(format!("devno={}", devno)); + } + Ok(vec!["-device".to_owned(), params.join(",")]) + } +} + #[derive(Debug)] struct DeviceVirtioBlk { bus_type: VirtioBusType, @@ -2361,15 +2409,27 @@ impl<'a> QemuCmdLine<'a> { Ok(()) } - pub fn add_block_device(&mut self, device_id: &str, path: &str, is_direct: bool) -> Result<()> { + pub fn add_block_device( + &mut self, + device_id: &str, + path: &str, + is_direct: bool, + is_scsi: bool, + ) -> Result<()> { self.devices .push(Box::new(BlockBackend::new(device_id, path, is_direct))); let devno = get_devno_ccw(&mut self.ccw_subchannel, device_id); - self.devices.push(Box::new(DeviceVirtioBlk::new( - device_id, - bus_type(self.config), - devno, - ))); + if is_scsi { + self.devices + .push(Box::new(DeviceScsiHd::new(device_id, "scsi0.0", devno))); + } else { + self.devices.push(Box::new(DeviceVirtioBlk::new( + device_id, + bus_type(self.config), + devno, + ))); + } + Ok(()) } diff --git a/src/runtime-rs/crates/hypervisor/src/qemu/inner.rs b/src/runtime-rs/crates/hypervisor/src/qemu/inner.rs index 7d68ae868d..a7615a5d88 100644 --- a/src/runtime-rs/crates/hypervisor/src/qemu/inner.rs +++ b/src/runtime-rs/crates/hypervisor/src/qemu/inner.rs @@ -132,13 +132,14 @@ impl QemuInner { &block_dev.config.path_on_host, block_dev.config.is_readonly, )?, - "ccw" | "blk" => cmdline.add_block_device( + "ccw" | "blk" | "scsi" => cmdline.add_block_device( block_dev.device_id.as_str(), &block_dev.config.path_on_host, block_dev .config .is_direct .unwrap_or(self.config.blockdev_info.block_device_cache_direct), + block_dev.config.driver_option.as_str() == "scsi", )?, unsupported => { info!(sl!(), "unsupported block device driver: {}", unsupported) From 4c386b51d9be6cc087ce5ba03978ee41c2725e1a Mon Sep 17 00:00:00 2001 From: Alex Lyn Date: Fri, 10 Oct 2025 11:31:04 +0800 Subject: [PATCH 2/2] runtime-rs: Add support for handling virtio-scsi devices As virtio-scsi has been set the default block device driver, the runtime also need to correctly handle the virtio-scsi info, specially the SCSI address required within kata-agent handling logic. And getting and assigning the scsi_addr to kata agent device id will be enough. This commit just do such work. Signed-off-by: Alex Lyn --- src/runtime-rs/crates/resource/src/manager_inner.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/runtime-rs/crates/resource/src/manager_inner.rs b/src/runtime-rs/crates/resource/src/manager_inner.rs index 155c2a8b7f..3b908d4e7b 100644 --- a/src/runtime-rs/crates/resource/src/manager_inner.rs +++ b/src/runtime-rs/crates/resource/src/manager_inner.rs @@ -434,10 +434,11 @@ impl ResourceManagerInner { // create block device for kata agent, // if driver is virtio-blk-pci, the id will be pci address. if let DeviceType::Block(device) = device_info { - // The following would work for drivers virtio-blk-pci and mmio. - // Once scsi support is added, need to handle scsi identifiers. + // The following would work for drivers virtio-blk-pci and virtio-mmio and virtio-scsi. let id = if let Some(pci_path) = device.config.pci_path { pci_path.to_string() + } else if let Some(scsi_address) = device.config.scsi_addr { + scsi_address } else { device.config.virt_path.clone() };