Skip to content

context: Check if nested virt is supported before enabling #43

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

Merged
merged 2 commits into from
May 2, 2025
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
140 changes: 134 additions & 6 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ anyhow = "1.0.79"
clap = { version = "4.5.0", features = ["derive"] }
mac_address = "1.1.5"
sysinfo = "0.31.4"
log = "0.4.0"
env_logger = "0.11.8"
24 changes: 22 additions & 2 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern "C" {
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_set_nested_virt(ctx_id: u32, enabled: bool) -> i32;
fn krun_check_nested_virt() -> i32;
fn krun_start_enter(ctx_id: u32) -> i32;
}

Expand All @@ -40,6 +41,17 @@ impl TryFrom<Args> for KrunContext {
// Start by setting up the desired log level for libkrun.
unsafe { krun_set_log_level(args.krun_log_level) };

let log_level = match args.krun_log_level {
0 => "off",
1 => "error",
2 => "warn",
3 => "info",
4 => "debug",
_ => "trace",
};
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or(log_level))
.init();

// Create a new context in libkrun. Store identifier to later use to configure VM
// resources and devices.
let id = unsafe { krun_create_ctx() };
Expand Down Expand Up @@ -89,8 +101,16 @@ impl TryFrom<Args> for KrunContext {

set_smbios_oem_strings(id, &args.oem_strings)?;

if args.nested && unsafe { krun_set_nested_virt(id, args.nested) } < 0 {
return Err(anyhow!("unable to set krun nested virtualization"));
if args.nested {
match unsafe { krun_check_nested_virt() } {
1 => {
if unsafe { krun_set_nested_virt(id, args.nested) } < 0 {
return Err(anyhow!("krun nested virtualization reported as supported, yet failed to enable"));
}
}
0 => log::debug!("nested virtualization is not supported on this host. -n,--nested argument ignored"),
_ => return Err(anyhow!("unable to check nested virtualization is supported on this host")),
}
}

Ok(Self { id, args })
Expand Down
Loading