From e95704b82f436b6d66979accc7f12510060111ce Mon Sep 17 00:00:00 2001 From: Maria Kuklina <101095419+kmd-fl@users.noreply.github.com> Date: Mon, 16 Sep 2024 19:25:57 +0200 Subject: [PATCH] feat(vm): check if virtualization is enabled (#2375) * feat(vm): check if virtualization is enabled --- nox/src/main.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/nox/src/main.rs b/nox/src/main.rs index 295dee0ae7..a8db578651 100644 --- a/nox/src/main.rs +++ b/nox/src/main.rs @@ -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 { @@ -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(()) + } + } +}