Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use krun_add_disk and increase SHM window size #15

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 164 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "krunkit"
version = "0.1.2"
version = "0.1.3"
authors = ["Tyler Fanelli <[email protected]>"]
edition = "2021"
description = "CLI tool to start VMs with libkrun"
Expand All @@ -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"
5 changes: 3 additions & 2 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,7 +64,8 @@ impl TryFrom<Args> 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"));
}

Expand Down
35 changes: 16 additions & 19 deletions src/virtio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ 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;
fn krun_set_net_mac(ctx_id: u32, c_mac: *const u8) -> i32;
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 {
Expand Down Expand Up @@ -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(())
Expand Down
Loading