Skip to content

Commit cd62f96

Browse files
Merge pull request NVIDIA#19 from markatcanonical/fix/check-cpu-virt
Check for virtualization support in CPU
2 parents 7e7c97f + 677789a commit cd62f96

5 files changed

Lines changed: 136 additions & 7 deletions

File tree

‎crates/openshell-cli/src/run.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2138,7 +2138,11 @@ pub async fn sandbox_create(
21382138
eprintln!();
21392139
std::process::exit(1);
21402140
}
2141-
return Err(status).into_diagnostic();
2141+
if msg.is_empty() {
2142+
return Err(status).into_diagnostic();
2143+
} else {
2144+
return Err(miette::miette!("{msg}"));
2145+
}
21422146
}
21432147
};
21442148
let sandbox = response

‎crates/openshell-core/src/kvm.rs‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
/// Provide an architecture-specific error message when `/dev/kvm` is inaccessible.
5+
///
6+
/// If hardware virtualization is supported but KVM is missing, this function returns a fallback
7+
/// suggesting the user load the KVM module or connect the snap interface.
8+
pub fn diagnose_missing_kvm() -> String {
9+
let arch = std::env::consts::ARCH;
10+
let cpuinfo = std::fs::read_to_string("/proc/cpuinfo").unwrap_or_default();
11+
diagnose_missing_kvm_with_cpuinfo(arch, &cpuinfo)
12+
}
13+
14+
/// Internal testable implementation.
15+
pub(crate) fn diagnose_missing_kvm_with_cpuinfo(arch: &str, cpuinfo: &str) -> String {
16+
match arch {
17+
"x86_64" | "x86" => {
18+
if !cpuinfo.contains("vmx") && !cpuinfo.contains("svm") {
19+
return "Your machine does not have the 'vmx' (Intel) or 'svm' (AMD) CPU flag. If you are running in a VM, you have not enabled nested virtualization.".to_string();
20+
}
21+
}
22+
"aarch64" => {
23+
// ARM64 relies on the CPU booting in EL2. There isn't always a simple flag in cpuinfo.
24+
return "Your machine does not support hardware virtualization. Ensure your CPU booted in EL2 (Hypervisor mode) and that virtualization is enabled in your firmware.".to_string();
25+
}
26+
"riscv64" => {
27+
// Look for the 'h' extension in the ISA string
28+
let has_h_ext = cpuinfo.lines()
29+
.filter(|line| line.starts_with("isa"))
30+
.any(|line| line.contains("_h") || line.contains(" h "));
31+
32+
if !has_h_ext {
33+
return "Your machine does not support hardware virtualization. Missing the 'h' (Hypervisor) extension in the RISC-V ISA string.".to_string();
34+
}
35+
}
36+
"s390x" => {
37+
if !cpuinfo.contains("sie") {
38+
return "Your machine does not support hardware virtualization. Missing the 'sie' (System z Virtualization) feature.".to_string();
39+
}
40+
}
41+
"powerpc" | "ppc64" | "ppc64le" => {
42+
// PowerPC KVM requires PowerNV or specific LPAR configurations
43+
// We check for the PowerNV platform in /proc/cpuinfo.
44+
let is_powernv = cpuinfo.lines().any(|line| line.starts_with("platform") && line.contains("PowerNV"));
45+
if !is_powernv {
46+
return "Your machine does not support hardware virtualization. Your platform may not provide KVM hypervisor capabilities (requires PowerNV or a compatible bare-metal environment).".to_string();
47+
}
48+
}
49+
_ => {
50+
return format!("Hardware virtualization requirements are not explicitly checked for architecture '{}'.", arch);
51+
}
52+
}
53+
54+
// Fallback if CPU capabilities exist but /dev/kvm is still missing
55+
"Hardware virtualization is supported, but /dev/kvm is missing. Ensure the KVM kernel module is loaded ('modprobe kvm') or the snap interface is connected:\n\n sudo snap connect openshell:kvm".to_string()
56+
}
57+
58+
#[cfg(test)]
59+
mod tests {
60+
use super::*;
61+
62+
#[test]
63+
fn test_diagnose_missing_kvm_x86_64_missing() {
64+
let cpuinfo = "flags: fpu vme de pse tsc";
65+
let err = diagnose_missing_kvm_with_cpuinfo("x86_64", cpuinfo);
66+
assert!(err.contains("vmx"));
67+
}
68+
69+
#[test]
70+
fn test_diagnose_missing_kvm_x86_64_present() {
71+
let cpuinfo = "flags: fpu vme de pse tsc vmx svm";
72+
let err = diagnose_missing_kvm_with_cpuinfo("x86_64", cpuinfo);
73+
assert!(err.contains("Hardware virtualization is supported"));
74+
}
75+
76+
#[test]
77+
fn test_diagnose_missing_kvm_powerpc_missing() {
78+
let cpuinfo = "platform: PowerVM";
79+
let err = diagnose_missing_kvm_with_cpuinfo("powerpc", cpuinfo);
80+
assert!(err.contains("requires PowerNV"));
81+
}
82+
83+
#[test]
84+
fn test_diagnose_missing_kvm_powerpc_present() {
85+
let cpuinfo = "platform: PowerNV";
86+
let err = diagnose_missing_kvm_with_cpuinfo("powerpc", cpuinfo);
87+
assert!(err.contains("Hardware virtualization is supported"));
88+
}
89+
90+
#[test]
91+
fn test_diagnose_missing_kvm_riscv64_present() {
92+
let cpuinfo = "isa: rv64imafdc_h_zicbom";
93+
let err = diagnose_missing_kvm_with_cpuinfo("riscv64", cpuinfo);
94+
assert!(err.contains("Hardware virtualization is supported"));
95+
}
96+
97+
#[test]
98+
fn test_diagnose_missing_kvm_riscv64_missing() {
99+
let cpuinfo = "isa: rv64imafdc_zicbom";
100+
let err = diagnose_missing_kvm_with_cpuinfo("riscv64", cpuinfo);
101+
assert!(err.contains("Missing the 'h' (Hypervisor) extension"));
102+
}
103+
104+
#[test]
105+
fn test_diagnose_missing_kvm_s390x_present() {
106+
let cpuinfo = "features : sie";
107+
let err = diagnose_missing_kvm_with_cpuinfo("s390x", cpuinfo);
108+
assert!(err.contains("Hardware virtualization is supported"));
109+
}
110+
111+
#[test]
112+
fn test_diagnose_missing_kvm_s390x_missing() {
113+
let cpuinfo = "features : lpar";
114+
let err = diagnose_missing_kvm_with_cpuinfo("s390x", cpuinfo);
115+
assert!(err.contains("Missing the 'sie'"));
116+
}
117+
}

‎crates/openshell-core/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub mod error;
1414
pub mod forward;
1515
pub mod image;
1616
pub mod inference;
17+
pub mod kvm;
1718
pub mod net;
1819
pub mod paths;
1920
pub mod proto;

‎crates/openshell-driver-vm/src/driver.rs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -709,9 +709,8 @@ impl ComputeDriver for VmDriver {
709709
fn validate_vm_sandbox(sandbox: &Sandbox) -> Result<(), Status> {
710710
if let Err(e) = std::fs::OpenOptions::new().read(true).write(true).open("/dev/kvm") {
711711
if e.kind() == std::io::ErrorKind::PermissionDenied || e.kind() == std::io::ErrorKind::NotFound {
712-
return Err(Status::failed_precondition(
713-
"gateway does not have KVM permissions. To grant these permissions:\n\n sudo snap connect openshell:kvm"
714-
));
712+
let diagnostic = openshell_core::kvm::diagnose_missing_kvm();
713+
return Err(Status::failed_precondition(diagnostic));
715714
}
716715
return Err(Status::failed_precondition(format!(
717716
"failed to access /dev/kvm: {e}"

‎crates/openshell-vm/src/lib.rs‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,9 +1199,17 @@ fn check_kvm_access() -> Result<(), VmError> {
11991199
use std::fs::OpenOptions;
12001200
match OpenOptions::new().read(true).open("/dev/kvm") {
12011201
Ok(_) => Ok(()),
1202-
Err(e) => Err(VmError::KvmAccess {
1203-
reason: e.to_string(),
1204-
}),
1202+
Err(e) => {
1203+
if e.kind() == std::io::ErrorKind::NotFound || e.kind() == std::io::ErrorKind::PermissionDenied {
1204+
Err(VmError::KvmAccess {
1205+
reason: openshell_core::kvm::diagnose_missing_kvm(),
1206+
})
1207+
} else {
1208+
Err(VmError::KvmAccess {
1209+
reason: format!("failed to access /dev/kvm: {e}"),
1210+
})
1211+
}
1212+
}
12051213
}
12061214
}
12071215

0 commit comments

Comments
 (0)