diff --git a/docs/getting-started.md b/docs/getting-started.md index 09c9e242a..110e50ff8 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -129,7 +129,15 @@ count after restore = 1 ## Troubleshooting -If you get `Error: NoHypervisorFound`, check that your hypervisor device exists and is accessible: +If you get `Error: NoHypervisorFound`, check that your hypervisor device exists and is accessible. + +The `hyperlight-check` binary runs these checks for you and prints how to fix anything it finds: + +```sh +cargo run -p hyperlight-host --bin hyperlight-check +``` + +It exits with a non-zero status when no usable hypervisor is found. **Linux:** diff --git a/src/hyperlight_host/src/bin/hyperlight-check.rs b/src/hyperlight_host/src/bin/hyperlight-check.rs new file mode 100644 index 000000000..c0a50c065 --- /dev/null +++ b/src/hyperlight_host/src/bin/hyperlight-check.rs @@ -0,0 +1,98 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2025 The Hyperlight Authors. + +//! Check that this machine can run Hyperlight sandboxes. +//! +//! Prints the detected platform, whether Hyperlight can find a usable +//! hypervisor, and what to do when it cannot. + +use std::process::ExitCode; + +const GETTING_STARTED: &str = + "https://github.com/hyperlight-dev/hyperlight/blob/main/docs/getting-started.md"; + +fn main() -> ExitCode { + println!("hyperlight-check"); + println!( + "platform: {} {}", + std::env::consts::OS, + std::env::consts::ARCH + ); + + let available = platform_check(); + println!( + "hypervisor: {}", + if available { + "available" + } else { + "unavailable" + } + ); + + if !available { + println!(); + println!("See {GETTING_STARTED} for setup instructions."); + return ExitCode::FAILURE; + } + ExitCode::SUCCESS +} + +#[cfg(target_os = "linux")] +fn platform_check() -> bool { + let available = hyperlight_host::is_hypervisor_present(); + check_device("/dev/kvm", "kvm"); + check_device("/dev/mshv", "mshv"); + available +} + +#[cfg(target_os = "linux")] +fn check_device(path: &str, group: &str) { + let device = std::path::Path::new(path); + if !device.exists() { + println!("{path}: not present"); + return; + } + match std::fs::OpenOptions::new() + .read(true) + .write(true) + .open(device) + { + Ok(_) => println!("{path}: accessible"), + Err(err) => { + println!("{path}: not accessible ({err})"); + println!(" add your user to the {group} group, then log in again:"); + println!(" sudo usermod -aG {group} $USER"); + } + } +} + +#[cfg(target_os = "windows")] +fn platform_check() -> bool { + let available = hyperlight_host::is_hypervisor_present(); + if !available { + println!("Windows Hypervisor Platform is not available."); + println!("Enable Hyper-V and Windows Hypervisor Platform, then reboot."); + println!("Current feature state (Admin PowerShell):"); + println!( + " Get-WindowsOptionalFeature -Online | Where-Object {{$_.FeatureName -match 'Hyper-V|HypervisorPlatform|VirtualMachinePlatform'}}" + ); + } + available +} + +#[cfg(target_os = "macos")] +fn platform_check() -> bool { + let available = hyperlight_host::is_hypervisor_present(); + if !available { + println!("Hypervisor.framework is not available."); + println!("On Apple silicon this requires a recent macOS."); + println!("Nested virtualization is not available inside most VMs."); + } + available +} + +#[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))] +fn platform_check() -> bool { + println!("Hyperlight is not supported on this platform."); + false +}