Skip to content

Commit

Permalink
Merge pull request kata-containers#10420 from pmores/add-support-for-…
Browse files Browse the repository at this point in the history
…virtio-scsi

runtime-rs: support virtio-scsi device in qemu-rs
  • Loading branch information
fidencio authored Oct 22, 2024
2 parents 91b874f + 8cdd968 commit 4c34cfb
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/runtime-rs/crates/hypervisor/src/qemu/cmdline_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{kernel_param::KernelParams, Address, HypervisorConfig};

use anyhow::{anyhow, Context, Result};
use async_trait::async_trait;
use kata_types::config::hypervisor::VIRTIO_SCSI;
use std::collections::HashMap;
use std::fmt::Display;
use std::fs::{read_to_string, File};
Expand Down Expand Up @@ -1513,6 +1514,66 @@ impl ToQemuParams for QmpSocket {
}
}

#[derive(Debug)]
struct DeviceVirtioScsi {
bus_type: VirtioBusType,
id: String,
disable_modern: bool,
iothread: String,
}

impl DeviceVirtioScsi {
fn new(id: &str, disable_modern: bool, bus_type: VirtioBusType) -> Self {
DeviceVirtioScsi {
bus_type,
id: id.to_owned(),
disable_modern,
iothread: "".to_owned(),
}
}

fn set_iothread(&mut self, iothread: &str) {
self.iothread = iothread.to_owned();
}
}

#[async_trait]
impl ToQemuParams for DeviceVirtioScsi {
async fn qemu_params(&self) -> Result<Vec<String>> {
let mut params = Vec::new();
params.push(format!("virtio-scsi-{}", self.bus_type));
params.push(format!("id={}", self.id));
if self.disable_modern {
params.push("disable-modern=true".to_owned());
}
if !self.iothread.is_empty() {
params.push(format!("iothread={}", self.iothread));
}
Ok(vec!["-device".to_owned(), params.join(",")])
}
}

#[derive(Debug)]
struct ObjectIoThread {
id: String,
}

impl ObjectIoThread {
fn new(id: &str) -> Self {
ObjectIoThread { id: id.to_owned() }
}
}

#[async_trait]
impl ToQemuParams for ObjectIoThread {
async fn qemu_params(&self) -> Result<Vec<String>> {
let mut params = Vec::new();
params.push("iothread".to_owned());
params.push(format!("id={}", self.id));
Ok(vec!["-object".to_owned(), params.join(",")])
}
}

fn is_running_in_vm() -> Result<bool> {
let res = read_to_string("/proc/cpuinfo")?
.lines()
Expand Down Expand Up @@ -1596,6 +1657,10 @@ impl<'a> QemuCmdLine<'a> {
qemu_cmd_line.add_bridges(config.device_info.default_bridges);
}

if config.blockdev_info.block_device_driver == VIRTIO_SCSI {
qemu_cmd_line.add_scsi_controller();
}

Ok(qemu_cmd_line)
}

Expand Down Expand Up @@ -1637,6 +1702,18 @@ impl<'a> QemuCmdLine<'a> {
}
}

fn add_scsi_controller(&mut self) {
let mut virtio_scsi =
DeviceVirtioScsi::new("scsi0", should_disable_modern(), bus_type(self.config));
if self.config.enable_iothreads {
let iothread_id = "scsi-io-thread";
let iothread = ObjectIoThread::new(iothread_id);
virtio_scsi.set_iothread(iothread_id);
self.devices.push(Box::new(iothread));
}
self.devices.push(Box::new(virtio_scsi));
}

pub fn add_virtiofs_share(
&mut self,
virtiofsd_socket_path: &str,
Expand Down

0 comments on commit 4c34cfb

Please sign in to comment.