use std::path::Path;
use std::time::Duration;
use isothrum_runner::{ProcessRunner, version_found};
use crate::exec;
pub use isoloom_manifest::Provider;
fn all() -> Vec<Provider> {
isothrum_runner::every_name::<Provider>()
.iter()
.filter_map(|name| isothrum_runner::from_wire_name(name))
.collect()
}
fn applicable(provider: Provider) -> bool {
applicable_on(provider, std::env::consts::OS, std::env::consts::ARCH)
}
fn applicable_on(provider: Provider, os: &str, arch: &str) -> bool {
match provider {
Provider::Virtualbox | Provider::VmwareDesktop if os == "windows" && arch == "aarch64" => false,
Provider::Hyperv => os == "windows",
Provider::Parallels | Provider::Utm => os == "macos",
Provider::Libvirt => os == "linux",
_ => true,
}
}
fn plugin_minimum(provider: Provider) -> Option<&'static str> {
match provider {
Provider::Utm => Some("0.2.0"),
_ => None,
}
}
fn hypervisor_minimum(provider: Provider) -> Option<(&'static str, &'static str)> {
match provider {
Provider::Utm => Some(("UTM", "4.6.5")),
_ => None,
}
}
async fn hypervisor_version(provider: Provider, runner: &dyn ProcessRunner) -> Option<String> {
if provider != Provider::Utm {
return None;
}
let plist = format!("{UTM_APP}/Contents/Info.plist");
let out = exec::read(runner, "defaults", &["read", &plist, "CFBundleShortVersionString"]).await.ok()?;
let version = out.trim().to_owned();
(!version.is_empty()).then_some(version)
}
fn probe(provider: Provider) -> Option<(&'static str, &'static [&'static str])> {
match provider {
Provider::Virtualbox => Some(("VBoxManage", &["--version"])),
Provider::VmwareDesktop => Some(("vmrun", &[])),
Provider::Parallels => Some(("prlctl", &["--version"])),
Provider::Libvirt => Some(("virsh", &["--version"])),
Provider::Qemu => Some((qemu_binary(), &["--version"])),
_ => None,
}
}
async fn hypervisor_installed(provider: Provider, runner: &dyn ProcessRunner) -> Option<bool> {
match provider {
Provider::Utm => Some(Path::new(UTM_APP).exists()),
Provider::Hyperv => Some(hyperv_enabled()),
_ => {
let (program, args) = probe(provider)?;
Some(tool_present(runner, program, args).await)
}
}
}
const UTM_APP: &str = "/Applications/UTM.app";
fn hyperv_enabled() -> bool {
let root = std::env::var_os("SystemRoot").map_or_else(|| std::path::PathBuf::from("C:\\Windows"), std::path::PathBuf::from);
root.join("System32").join("vmms.exe").is_file()
}
const KVM: &str = "/dev/kvm";
fn kvm_problem(dev: &Path) -> Option<String> {
if !dev.exists() {
return Some("KVM isn't available on this machine (no /dev/kvm): turn on virtualization in the firmware, or use another hypervisor".to_owned());
}
match std::fs::OpenOptions::new().read(true).write(true).open(dev) {
Ok(_) => None,
Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => {
Some("you can't use /dev/kvm yet: add yourself to the kvm and libvirt groups, then sign out and back in".to_owned())
}
Err(e) => Some(format!("/dev/kvm can't be opened: {e}")),
}
}
fn qemu_binary() -> &'static str {
qemu_binary_on(std::env::consts::ARCH)
}
fn qemu_binary_on(arch: &str) -> &'static str {
if arch == "aarch64" { "qemu-system-aarch64" } else { "qemu-system-x86_64" }
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ProviderStatus {
pub provider: Provider,
pub reason: Option<String>,
}
fn plugin_too_old(provider: Provider, installed: Option<&str>) -> Option<(&str, &'static str)> {
let (version, min) = installed.zip(plugin_minimum(provider))?;
older(version, min).then_some((version, min))
}
fn hypervisor_too_old(provider: Provider, installed: Option<&str>) -> Option<(&'static str, &str, &'static str)> {
let (version, (name, min)) = installed.zip(hypervisor_minimum(provider))?;
older(version, min).then_some((name, version, min))
}
fn older(installed: &str, min: &str) -> bool {
version_found(installed).zip(version_found(min)).is_some_and(|(found, min)| found < min)
}
async fn tool_present(runner: &dyn ProcessRunner, program: &str, args: &[&str]) -> bool {
exec::read(runner, program, args).await.is_ok()
}
async fn installed_plugins(runner: &dyn ProcessRunner) -> Result<Vec<(String, Option<String>)>, String> {
exec::read_ok(runner, "vagrant", &["plugin", "list", "--machine-readable"])
.await
.map(|out| isothrum_runner::vagrant_plugins(&out))
}
const NO_VAGRANT: &str = "Vagrant is not installed";
const VBOX_PROBE: Duration = Duration::from_secs(8);
const HYPERV_NEEDS_ADMIN: &str = "Vagrant drives Hyper-V only for an administrator or a member of the Hyper-V Administrators group: \
add this account to that group and sign out of Windows and back in, or run elevated";
pub async fn detect(runner: &dyn ProcessRunner, vagrant_installed: bool) -> Vec<ProviderStatus> {
if !vagrant_installed {
return all()
.into_iter()
.filter(|p| applicable(*p))
.map(|provider| ProviderStatus {
provider,
reason: Some(NO_VAGRANT.to_owned()),
})
.collect();
}
let plugins = installed_plugins(runner).await;
let (plugins, plugins_unknown) = match plugins {
Ok(plugins) => (plugins, None),
Err(why) => (Vec::new(), Some(why)),
};
let mut statuses = Vec::new();
for provider in all() {
if !applicable(provider) {
continue;
}
let hypervisor = hypervisor_installed(provider, runner).await;
let hypervisor_version = if hypervisor == Some(true) {
hypervisor_version(provider, runner).await
} else {
None
};
let plugin = provider.plugin();
let installed = plugin.and_then(|needed| plugins.iter().find(|(i, _)| i == needed));
let plugin_installed = plugin.is_none() || installed.is_some();
let plugin_version = installed.and_then(|(_, v)| v.as_deref());
let unresponsive = provider == Provider::Virtualbox && hypervisor == Some(true) && vbox_wedged(runner).await;
let needs_admin = needs_admin(provider, hypervisor, runner).await;
let platform_problem = if provider == Provider::Libvirt { kvm_problem(Path::new(KVM)) } else { None };
let platform_problem = platform_problem.as_deref();
let detection = Detection {
hypervisor,
hypervisor_version: hypervisor_version.as_deref(),
unresponsive,
plugin,
plugin_installed,
plugin_version,
plugins_unknown: plugins_unknown.as_deref(),
needs_admin,
platform_problem,
};
let reason = unavailable_reason(provider, detection);
statuses.push(ProviderStatus { provider, reason });
}
statuses
}
async fn needs_admin(provider: Provider, hypervisor: Option<bool>, runner: &dyn ProcessRunner) -> bool {
provider == Provider::Hyperv && hypervisor == Some(true) && isothrum_runner::may_manage_hyperv(runner).await == Some(false)
}
async fn vbox_wedged(runner: &dyn ProcessRunner) -> bool {
let probe = exec::read(runner, "VBoxManage", &["list", "runningvms"]);
tokio::time::timeout(VBOX_PROBE, probe).await.is_err()
}
#[derive(Clone, Copy)]
struct Detection<'a> {
hypervisor: Option<bool>,
hypervisor_version: Option<&'a str>,
unresponsive: bool,
plugin: Option<&'a str>,
plugin_installed: bool,
plugin_version: Option<&'a str>,
plugins_unknown: Option<&'a str>,
needs_admin: bool,
platform_problem: Option<&'a str>,
}
fn unavailable_reason(provider: Provider, detection: Detection<'_>) -> Option<String> {
let Detection {
hypervisor,
hypervisor_version,
unresponsive,
plugin,
plugin_installed,
plugin_version,
plugins_unknown,
needs_admin,
platform_problem,
} = detection;
if unresponsive {
Some("VirtualBox isn't responding (its background service is stuck): quit VirtualBox and any VM windows, or restart the computer".to_owned())
} else if hypervisor == Some(false) && provider == Provider::Hyperv {
Some("Hyper-V isn't turned on (Windows Features > Hyper-V), or this edition of Windows doesn't have it".to_owned())
} else if needs_admin {
Some(HYPERV_NEEDS_ADMIN.to_owned())
} else if hypervisor == Some(false) {
Some(probe(provider).map_or_else(|| "not installed".to_owned(), |(program, _)| format!("`{program}` was not found")))
} else if let Some(why) = platform_problem {
Some(why.to_owned())
} else if let Some((name, version, min)) = hypervisor_too_old(provider, hypervisor_version) {
Some(format!("{name} {version} is too old: {min} or later is needed. Update {name}"))
} else if let Some(why) = plugins_unknown.filter(|_| plugin.is_some()) {
Some(format!("Vagrant couldn't say which plugins it has (`vagrant plugin list`: {why})"))
} else if !plugin_installed {
plugin.map(|p| format!("Vagrant plugin `{p}` is not installed"))
} else if let Some((version, min)) = plugin_too_old(provider, plugin_version) {
plugin.map(|p| format!("Vagrant plugin `{p}` {version} is too old: {min} or later is needed. Install it again to update it"))
} else {
None
}
}
#[cfg(test)]
mod tests {
use isothrum_runner::fake::{FakeProcesses, Reply};
use super::*;
fn isoloom_providers() -> Vec<String> {
let mut names = isothrum_runner::every_name::<Provider>();
names.sort();
names
}
#[test]
fn every_provider_isoloom_names_is_listed() {
let mut listed: Vec<String> = all().iter().map(isothrum_runner::wire_name).collect();
listed.sort();
assert_eq!(listed, isoloom_providers());
}
fn denied_device(dir: &Path) -> String {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let device = dir.join("denied");
std::fs::write(&device, "").unwrap();
std::fs::set_permissions(&device, std::fs::Permissions::from_mode(0o400)).unwrap();
if let Some(said) = kvm_problem(&device) {
assert!(said.starts_with("you can't use /dev/kvm yet"), "{said}");
return said;
}
}
let _ = dir;
"you can't use /dev/kvm yet: add yourself to the kvm and libvirt groups, then sign out and back in".to_owned()
}
#[test]
fn a_provider_applies_where_its_hypervisor_runs() {
let platforms = [
("linux", "x86_64"),
("linux", "aarch64"),
("macos", "aarch64"),
("macos", "x86_64"),
("windows", "x86_64"),
("windows", "aarch64"),
];
let table = [
(Provider::Virtualbox, [true, true, true, true, true, false]),
(Provider::VmwareDesktop, [true, true, true, true, true, false]),
(Provider::Hyperv, [false, false, false, false, true, true]),
(Provider::Parallels, [false, false, true, true, false, false]),
(Provider::Utm, [false, false, true, true, false, false]),
(Provider::Libvirt, [true, true, false, false, false, false]),
(Provider::Qemu, [true; 6]),
(Provider::VmwareEsxi, [true; 6]),
];
assert_eq!(table.len(), all().len(), "every provider is in the table");
for (provider, expected) in table {
for ((os, arch), applies) in platforms.iter().zip(expected) {
assert_eq!(applicable_on(provider, os, arch), applies, "{provider:?} on {os} {arch}");
}
}
assert_eq!(qemu_binary_on("aarch64"), "qemu-system-aarch64");
assert_eq!(qemu_binary_on("x86_64"), "qemu-system-x86_64");
}
#[test]
fn a_providers_id_is_the_one_vagrant_and_the_manifest_use() {
let ids: Vec<String> = all().iter().map(isothrum_runner::wire_name).collect();
assert_eq!(
ids,
["virtualbox", "vmware_desktop", "hyperv", "parallels", "libvirt", "qemu", "utm", "vmware_esxi"]
);
}
#[test]
fn utm_and_its_plugin_are_held_to_a_version() {
let detection = |hv: Option<&'static str>, pl: Option<&'static str>, installed: bool| Detection {
hypervisor: Some(true),
hypervisor_version: hv,
unresponsive: false,
plugin: Some("vagrant_utm"),
plugin_installed: installed,
plugin_version: pl,
plugins_unknown: None,
needs_admin: false,
platform_problem: None,
};
assert_eq!(plugin_too_old(Provider::Utm, Some("0.1.6")), Some(("0.1.6", "0.2.0")));
assert_eq!(plugin_too_old(Provider::Utm, Some("0.2.0-beta")), Some(("0.2.0-beta", "0.2.0")));
assert_eq!(plugin_too_old(Provider::Utm, Some("unknown")), None, "no version to judge");
assert_eq!(
unavailable_reason(Provider::Utm, detection(Some("4.7.5"), Some("0.1.6"), true)).as_deref(),
Some("Vagrant plugin `vagrant_utm` 0.1.6 is too old: 0.2.0 or later is needed. Install it again to update it")
);
assert_eq!(
unavailable_reason(Provider::Utm, detection(Some("4.5.1"), Some("0.1.6"), true)).as_deref(),
Some("UTM 4.5.1 is too old: 4.6.5 or later is needed. Update UTM")
);
assert_eq!(unavailable_reason(Provider::Utm, detection(Some("4.7.5"), Some("0.2.0"), true)), None);
assert_eq!(unavailable_reason(Provider::Utm, detection(None, None, true)), None);
assert_eq!(plugin_too_old(Provider::Utm, None), None);
assert_eq!(plugin_too_old(Provider::Qemu, Some("0.1.0")), None);
assert_eq!(hypervisor_too_old(Provider::Virtualbox, Some("6.0")), None);
}
#[tokio::test]
async fn detect_reports_a_ready_qemu_and_an_unavailable_one_by_its_plugin() {
let with = FakeProcesses::rules(&[
("qemu-system", Reply::ok("QEMU 10.0")),
(
"vagrant plugin list --machine-readable",
Reply::ok("1,,plugin-name,vagrant-qemu\n1,vagrant-qemu,plugin-version,0.6.3%!(VAGRANT_COMMA) global"),
),
]);
let qemu = detect(&with, true).await.into_iter().find(|s| s.provider == Provider::Qemu).unwrap();
assert!(qemu.reason.is_none(), "{qemu:?}");
let without = FakeProcesses::rules(&[("qemu-system", Reply::ok("QEMU 10.0")), ("vagrant plugin list", Reply::ok(""))]);
let no_plugin = detect(&without, true).await.into_iter().find(|s| s.provider == Provider::Qemu).unwrap();
assert_eq!(no_plugin.reason.as_deref(), Some("Vagrant plugin `vagrant-qemu` is not installed"),);
}
#[tokio::test]
async fn a_vagrant_that_cant_list_its_plugins_is_one_reason_not_each_plugin_missing() {
let broken = FakeProcesses::rules(&[
("qemu-system", Reply::ok("QEMU 10.0")),
("VBoxManage", Reply::ok("7.1.4")),
("vagrant plugin list", Reply::fail(1, "Vagrant failed to initialize at a very early stage")),
]);
let statuses = detect(&broken, true).await;
let qemu = statuses.iter().find(|s| s.provider == Provider::Qemu).unwrap();
assert_eq!(
qemu.reason.as_deref(),
Some("Vagrant couldn't say which plugins it has (`vagrant plugin list`: Vagrant failed to initialize at a very early stage (exited with code 1))")
);
let virtualbox = statuses.iter().find(|s| s.provider == Provider::Virtualbox);
assert!(virtualbox.is_none_or(|s| s.reason.is_none()), "{virtualbox:?}");
}
#[test]
fn a_hypervisor_that_is_off_or_not_installed_says_which() {
let absent = Detection {
hypervisor: Some(false),
hypervisor_version: None,
unresponsive: false,
plugin: None,
plugin_installed: true,
plugin_version: None,
plugins_unknown: None,
needs_admin: false,
platform_problem: None,
};
assert_eq!(
unavailable_reason(Provider::Hyperv, absent).as_deref(),
Some("Hyper-V isn't turned on (Windows Features > Hyper-V), or this edition of Windows doesn't have it")
);
assert_eq!(unavailable_reason(Provider::Virtualbox, absent).as_deref(), Some("`VBoxManage` was not found"));
}
#[test]
fn libvirt_without_kvm_or_its_leave_says_which() {
let dir = tempfile::tempdir().unwrap();
let missing = kvm_problem(&dir.path().join("kvm")).unwrap();
assert!(missing.starts_with("KVM isn't available on this machine (no /dev/kvm)"), "{missing}");
let other = kvm_problem(dir.path()).unwrap();
assert!(other.starts_with("/dev/kvm can't be opened: "), "{other}");
let device = dir.path().join("device");
std::fs::write(&device, "").unwrap();
assert_eq!(kvm_problem(&device), None);
let denied = denied_device(dir.path());
let (missing, denied) = (missing.as_str(), denied.as_str());
let libvirt = |hypervisor, platform_problem| Detection {
hypervisor: Some(hypervisor),
hypervisor_version: None,
unresponsive: false,
plugin: Some("vagrant-libvirt"),
plugin_installed: true,
plugin_version: None,
plugins_unknown: None,
needs_admin: false,
platform_problem,
};
assert_eq!(unavailable_reason(Provider::Libvirt, libvirt(true, Some(missing))).as_deref(), Some(missing));
assert_eq!(unavailable_reason(Provider::Libvirt, libvirt(true, Some(denied))).as_deref(), Some(denied));
assert_eq!(
unavailable_reason(Provider::Libvirt, libvirt(false, Some(missing))).as_deref(),
Some("`virsh` was not found")
);
assert_eq!(unavailable_reason(Provider::Libvirt, libvirt(true, None)), None);
}
#[tokio::test]
async fn a_virtualbox_that_isnt_installed_is_detected_as_such() {
let tools = FakeProcesses::rules(&[("VBoxManage", Reply::missing())]);
let statuses = detect(&tools, true).await;
let virtualbox = statuses.iter().find(|s| s.provider == Provider::Virtualbox);
assert!(
virtualbox.is_none_or(|s| s.reason.as_deref() == Some("`VBoxManage` was not found")),
"{virtualbox:?}"
);
}
#[tokio::test]
async fn without_vagrant_every_provider_says_so_and_nothing_is_probed() {
let tools = FakeProcesses::rules(&[]);
let statuses = detect(&tools, false).await;
assert_eq!(statuses.len(), all().iter().filter(|p| applicable(**p)).count());
assert!(statuses.iter().all(|s| s.reason.as_deref() == Some(NO_VAGRANT)), "{statuses:?}");
assert_eq!(tools.calls(), [], "no probe runs without Vagrant");
}
struct Stuck;
impl ProcessRunner for Stuck {
fn run<'a>(
&'a self,
_spec: &'a isothrum_runner::ProcessSpec,
_on_line: isothrum_runner::OnLine<'a>,
) -> isothrum_runner::BoxFuture<'a, Result<isothrum_runner::Exit, isothrum_runner::ProcessError>> {
Box::pin(async {
tokio::time::sleep(Duration::from_secs(3600)).await;
Ok(isothrum_runner::Exit::new(Some(0)))
})
}
}
#[tokio::test(start_paused = true)]
async fn a_virtualbox_whose_service_never_answers_is_wedged() {
assert!(vbox_wedged(&Stuck).await);
let answering = FakeProcesses::rules(&[("VBoxManage list runningvms", Reply::ok(""))]);
assert!(!vbox_wedged(&answering).await);
}
#[tokio::test]
async fn only_utm_is_asked_its_version() {
let tools = FakeProcesses::rules(&[("defaults read", Reply::ok("4.7.5\n"))]);
assert_eq!(hypervisor_version(Provider::Utm, &tools).await.as_deref(), Some("4.7.5"));
assert_eq!(hypervisor_version(Provider::Qemu, &tools).await, None);
let silent = FakeProcesses::rules(&[("defaults read", Reply::ok(""))]);
assert_eq!(hypervisor_version(Provider::Utm, &silent).await, None, "no version said, none read");
let call = tools.calls().into_iter().next().unwrap();
assert_eq!(call.args, ["read", "/Applications/UTM.app/Contents/Info.plist", "CFBundleShortVersionString"]);
}
#[tokio::test]
async fn an_installed_hyperv_needs_admin_only_when_whoami_says_the_account_may_not_manage_it() {
let groups = |sid: &str| format!("\"Label\",\"Label\",\"{sid}\",");
let not_allowed = FakeProcesses::rules(&[("whoami /groups", Reply::ok(&groups("S-1-16-8192")))]);
let allowed = FakeProcesses::rules(&[("whoami /groups", Reply::ok(&groups("S-1-5-32-578")))]);
let unknown = FakeProcesses::rules(&[("whoami", Reply::missing())]);
assert!(needs_admin(Provider::Hyperv, Some(true), ¬_allowed).await);
assert!(!needs_admin(Provider::Hyperv, Some(true), &allowed).await);
assert!(!needs_admin(Provider::Hyperv, Some(true), &unknown).await, "no answer adds no reason");
assert!(!needs_admin(Provider::Hyperv, Some(false), ¬_allowed).await, "not installed says so instead");
assert!(!needs_admin(Provider::Virtualbox, Some(true), ¬_allowed).await);
assert_eq!(not_allowed.calls().len(), 1, "whoami is asked for an installed Hyper-V only");
}
#[test]
fn hyperv_needs_admin_folds_into_its_reason() {
let hyperv = |needs_admin| Detection {
hypervisor: Some(true),
hypervisor_version: None,
unresponsive: false,
plugin: None,
plugin_installed: true,
plugin_version: None,
plugins_unknown: None,
needs_admin,
platform_problem: None,
};
assert_eq!(unavailable_reason(Provider::Hyperv, hyperv(true)).as_deref(), Some(HYPERV_NEEDS_ADMIN));
assert_eq!(unavailable_reason(Provider::Hyperv, hyperv(false)), None);
}
}