From 2c4c316ccaa7b0abf6362dbb6507268c0d7be999 Mon Sep 17 00:00:00 2001 From: Sergio Lopez Date: Tue, 1 Oct 2024 10:08:02 +0200 Subject: [PATCH 1/3] 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 --- src/virtio.rs | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/virtio.rs b/src/virtio.rs index ae3c3d7..ec18f69 100644 --- a/src/virtio.rs +++ b/src/virtio.rs @@ -14,8 +14,12 @@ use mac_address::MacAddress; #[link(name = "krun-efi")] extern "C" { - fn krun_set_root_disk(ctx_id: u32, c_disk_path: *const c_char) -> i32; - fn krun_set_data_disk(ctx_id: u32, c_disk_path: *const c_char) -> i32; + fn krun_add_disk( + ctx_id: u32, + c_block_id: *const c_char, + c_disk_path: *const c_char, + read_only: bool, + ) -> i32; fn krun_add_vsock_port(ctx_id: u32, port: u32, c_filepath: *const c_char) -> i32; fn krun_add_virtiofs(ctx_id: u32, c_tag: *const c_char, c_path: *const c_char) -> i32; fn krun_set_gvproxy_path(ctx_id: u32, c_path: *const c_char) -> i32; @@ -23,9 +27,6 @@ extern "C" { fn krun_set_console_output(ctx_id: u32, c_filepath: *const c_char) -> i32; } -static mut ROOT_BLK_SET: bool = false; -static mut DATA_BLK_SET: bool = false; - /// Each virito device configures itself with krun differently. This is used by each virtio device /// to set their respective configurations with libkrun. pub trait KrunContextSet { @@ -117,22 +118,18 @@ impl FromStr for BlkConfig { /// Set the virtio-blk device to be the krun VM's root disk. impl KrunContextSet for BlkConfig { unsafe fn krun_ctx_set(&self, id: u32) -> Result<(), anyhow::Error> { + let basename = match self.path.file_name() { + Some(osstr) => osstr.to_str().unwrap_or("disk"), + None => "disk", + }; + let block_id_cstr = CString::new(basename).context("can't convert basename to cstring")?; let path_cstr = path_to_cstring(&self.path)?; - if !ROOT_BLK_SET { - if krun_set_root_disk(id, path_cstr.as_ptr()) < 0 { - return Err(anyhow!("unable to set virtio-blk root disk")); - } - - ROOT_BLK_SET = true; - } else if !DATA_BLK_SET { - if krun_set_data_disk(id, path_cstr.as_ptr()) < 0 { - return Err(anyhow!("unable to set virtio-blk data disk")); - } - - DATA_BLK_SET = true; - } else { - return Err(anyhow!("krun root and data disk already set")); + if krun_add_disk(id, block_id_cstr.as_ptr(), path_cstr.as_ptr(), false) < 0 { + return Err(anyhow!(format!( + "unable to set virtio-blk disk for {}", + self.path.display() + ))); } Ok(()) From bfe73154193401d25786dc60fc135dcae2a695cd Mon Sep 17 00:00:00 2001 From: Sergio Lopez Date: Tue, 1 Oct 2024 15:11:24 +0200 Subject: [PATCH 2/3] context: set SHM window to be as large as the host's RAM On version 1.9.5 libkrun introduced an API to set the size of the SHM window for virtio-gpu. Use it to set it to be as large as the host's RAM. This should give us enough room for running large models, accounting for potential device memory fragmentation. Signed-off-by: Sergio Lopez --- Cargo.toml | 1 + src/context.rs | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 90240cc..b5189cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,4 @@ build = "build.rs" anyhow = "1.0.79" clap = { version = "4.5.0", features = ["derive"] } mac_address = "1.1.5" +sysinfo = "0.31.4" diff --git a/src/context.rs b/src/context.rs index c866539..ea8ac9b 100644 --- a/src/context.rs +++ b/src/context.rs @@ -15,7 +15,7 @@ use anyhow::{anyhow, Context}; #[link(name = "krun-efi")] extern "C" { fn krun_create_ctx() -> i32; - fn krun_set_gpu_options(ctx_id: u32, virgl_flags: u32) -> i32; + fn krun_set_gpu_options2(ctx_id: u32, virgl_flags: u32, shm_size: u64) -> i32; fn krun_set_vm_config(ctx_id: u32, num_vcpus: u8, ram_mib: u32) -> i32; fn krun_set_smbios_oem_strings(ctx_id: u32, oem_strings: *const *const c_char) -> i32; fn krun_start_enter(ctx_id: u32) -> i32; @@ -64,7 +64,8 @@ impl TryFrom for KrunContext { // Temporarily enable GPU by default let virgl_flags = VIRGLRENDERER_VENUS | VIRGLRENDERER_NO_VIRGL; - if unsafe { krun_set_gpu_options(id, virgl_flags) } < 0 { + let sys = sysinfo::System::new_all(); + if unsafe { krun_set_gpu_options2(id, virgl_flags, sys.total_memory()) } < 0 { return Err(anyhow!("unable to set krun vCPU/RAM configuration")); } From 95d79b5dd919748f25148836a7288a574a14a673 Mon Sep 17 00:00:00 2001 From: Sergio Lopez Date: Tue, 1 Oct 2024 15:13:58 +0200 Subject: [PATCH 3/3] Bump version to 0.1.3 Signed-off-by: Sergio Lopez --- Cargo.lock | 181 ++++++++++++++++++++++++++++++++++++++++++++++++----- Cargo.toml | 2 +- 2 files changed, 165 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 13af43c..8a06980 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -129,6 +129,43 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "crossbeam-deque" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" + +[[package]] +name = "either" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" + [[package]] name = "heck" version = "0.4.1" @@ -137,11 +174,12 @@ checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" [[package]] name = "krunkit" -version = "0.1.1" +version = "0.1.3" dependencies = [ "anyhow", "clap", "mac_address", + "sysinfo", ] [[package]] @@ -160,6 +198,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + [[package]] name = "memoffset" version = "0.6.5" @@ -182,6 +226,15 @@ dependencies = [ "memoffset", ] +[[package]] +name = "ntapi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4" +dependencies = [ + "winapi", +] + [[package]] name = "proc-macro2" version = "1.0.78" @@ -200,6 +253,26 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rayon" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "strsim" version = "0.11.0" @@ -217,6 +290,20 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "sysinfo" +version = "0.31.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "355dbe4f8799b304b05e1b0f05fc59b2a18d36645cf169607da45bde2f69a1be" +dependencies = [ + "core-foundation-sys", + "libc", + "memchr", + "ntapi", + "rayon", + "windows", +] + [[package]] name = "unicode-ident" version = "1.0.12" @@ -251,6 +338,59 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12342cb4d8e3b046f3d80effd474a7a02447231330ef77d71daa6fbc40681143" +dependencies = [ + "windows-core", + "windows-targets", +] + +[[package]] +name = "windows-core" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2ed2439a290666cd67ecce2b0ffaad89c2a56b976b736e6ece670297897832d" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-result", + "windows-targets", +] + +[[package]] +name = "windows-implement" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9107ddc059d5b6fbfbffdfa7a7fe3e22a226def0b2608f72e9d552763d3e1ad7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29bee4b38ea3cde66011baa44dba677c432a78593e202392d1e9070cf2a7fca7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-result" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-sys" version = "0.52.0" @@ -262,13 +402,14 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", + "windows_i686_gnullvm", "windows_i686_msvc", "windows_x86_64_gnu", "windows_x86_64_gnullvm", @@ -277,42 +418,48 @@ dependencies = [ [[package]] name = "windows_aarch64_gnullvm" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml index b5189cc..d221cdc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "krunkit" -version = "0.1.2" +version = "0.1.3" authors = ["Tyler Fanelli "] edition = "2021" description = "CLI tool to start VMs with libkrun"