Skip to content

Commit

Permalink
feat(vm): check if virtualization is enabled (#2375)
Browse files Browse the repository at this point in the history
* feat(vm): check if virtualization is enabled
  • Loading branch information
kmd-fl authored Sep 16, 2024
1 parent bb7e639 commit e95704b
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions nox/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ fn main() -> eyre::Result<()> {

let resolved_config = config.clone().resolve()?;

if resolved_config.node_config.vm.is_some() {
check_virtualization()?;
}

let acquire_strategy = if resolved_config.dev_mode_config.enable {
AcquireStrategy::RoundRobin
} else {
Expand Down Expand Up @@ -284,3 +288,38 @@ fn vm_config(config: &ResolvedConfig) -> VmConfig {
config.node_config.avm_config.hard_limit_enabled,
)
}

// 1. Runs a `kvm_ok` cli utility and checks that virtualization is enabled.
// We check virtualization by looking for a string `KVM acceleration can be used`.
// If not, return an error.
// 2. If `kvm_ok` isn't available, check `/dev/kvm`.
// If it's not available, return a warning.
fn check_virtualization() -> eyre::Result<()> {
let kvm_ok = std::process::Command::new("kvm-ok")
.output()
.map(|output| String::from_utf8_lossy(&output.stdout).trim().to_string());

match kvm_ok {
Ok(output) => {
if output.contains("KVM acceleration can be used") {
log::info!("Virtualization is enabled");
Ok(())
} else {
Err(eyre::eyre!(
"Virtualization is not enabled. kvm-ok output:\n {}",
output
))
}
}
Err(err) => {
log::warn!("Failed to run kvm-ok: {err}");
log::warn!("Doing a simple virtualization check...");
if !std::path::Path::new("/dev/kvm").exists() {
log::warn!("Virtualization is not enabled: /dev/kvm is not available");
} else {
log::warn!("Virtualization is enabled: /dev/kvm is available");
}
Ok(())
}
}
}

0 comments on commit e95704b

Please sign in to comment.