Skip to content

Commit 153aab4

Browse files
slptylerfanelli
authored andcommitted
virtio: use krun_add_disk for virtio-blk disks
Use the new krun_add_disk API, introduced in libkrun 1.9.5, to be able to add an arbitrary number of virtio-blk devices. Signed-off-by: Sergio Lopez <slp@redhat.com>
1 parent 29f5ada commit 153aab4

1 file changed

Lines changed: 16 additions & 19 deletions

File tree

src/virtio.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@ use mac_address::MacAddress;
1414

1515
#[link(name = "krun-efi")]
1616
extern "C" {
17-
fn krun_set_root_disk(ctx_id: u32, c_disk_path: *const c_char) -> i32;
18-
fn krun_set_data_disk(ctx_id: u32, c_disk_path: *const c_char) -> i32;
17+
fn krun_add_disk(
18+
ctx_id: u32,
19+
c_block_id: *const c_char,
20+
c_disk_path: *const c_char,
21+
read_only: bool,
22+
) -> i32;
1923
fn krun_add_vsock_port(ctx_id: u32, port: u32, c_filepath: *const c_char) -> i32;
2024
fn krun_add_virtiofs(ctx_id: u32, c_tag: *const c_char, c_path: *const c_char) -> i32;
2125
fn krun_set_gvproxy_path(ctx_id: u32, c_path: *const c_char) -> i32;
2226
fn krun_set_net_mac(ctx_id: u32, c_mac: *const u8) -> i32;
2327
fn krun_set_console_output(ctx_id: u32, c_filepath: *const c_char) -> i32;
2428
}
2529

26-
static mut ROOT_BLK_SET: bool = false;
27-
static mut DATA_BLK_SET: bool = false;
28-
2930
/// Each virito device configures itself with krun differently. This is used by each virtio device
3031
/// to set their respective configurations with libkrun.
3132
pub trait KrunContextSet {
@@ -117,22 +118,18 @@ impl FromStr for BlkConfig {
117118
/// Set the virtio-blk device to be the krun VM's root disk.
118119
impl KrunContextSet for BlkConfig {
119120
unsafe fn krun_ctx_set(&self, id: u32) -> Result<(), anyhow::Error> {
121+
let basename = match self.path.file_name() {
122+
Some(osstr) => osstr.to_str().unwrap_or("disk"),
123+
None => "disk",
124+
};
125+
let block_id_cstr = CString::new(basename).context("can't convert basename to cstring")?;
120126
let path_cstr = path_to_cstring(&self.path)?;
121127

122-
if !ROOT_BLK_SET {
123-
if krun_set_root_disk(id, path_cstr.as_ptr()) < 0 {
124-
return Err(anyhow!("unable to set virtio-blk root disk"));
125-
}
126-
127-
ROOT_BLK_SET = true;
128-
} else if !DATA_BLK_SET {
129-
if krun_set_data_disk(id, path_cstr.as_ptr()) < 0 {
130-
return Err(anyhow!("unable to set virtio-blk data disk"));
131-
}
132-
133-
DATA_BLK_SET = true;
134-
} else {
135-
return Err(anyhow!("krun root and data disk already set"));
128+
if krun_add_disk(id, block_id_cstr.as_ptr(), path_cstr.as_ptr(), false) < 0 {
129+
return Err(anyhow!(format!(
130+
"unable to set virtio-blk disk for {}",
131+
self.path.display()
132+
)));
136133
}
137134

138135
Ok(())

0 commit comments

Comments
 (0)