|
| 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 | +} |
0 commit comments