use serde::{Deserialize, Serialize};
use crate::context::Context;
use crate::exec;
use crate::host::{Arch, Host, HostOs};
pub use isothrum_runner::Provider;
mod found;
pub(crate) mod helper;
mod hyperv;
pub(crate) mod kvm;
mod needs;
pub mod ready;
mod reasons;
mod unavailable;
mod vbox;
pub(crate) mod virtiofsd;
#[cfg(test)]
use reasons::hypervisor_too_old;
use reasons::{plugin_too_old, unavailable_reason};
use found::{Found, hypervisor_found, hypervisor_version};
pub use helper::{Helper, helpers};
pub use kvm::{groups_pending, kvm_group, login_groups, uses_kvm};
pub use needs::{Need, UtilityState, needs, vmware_utility};
pub use unavailable::Unavailable;
pub use virtiofsd::{needs_virtiofsd, virtiofsd};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Check {
Tool(&'static str, &'static [&'static str]),
Qemu,
UtmApp,
HypervFeature,
Remote,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct ProviderRow {
pub oses: &'static [HostOs],
pub windows_on_arm: bool,
pub mac_cpu: Option<Arch>,
pub linux_cpu: Option<Arch>,
pub vendor: Option<crate::install::VendorPage>,
pub shares_folders: &'static [HostOs],
pub plugin_minimum: Option<&'static str>,
pub hypervisor_minimum: Option<(&'static str, &'static str)>,
check: Check,
}
const HYPERV_PAGE: crate::install::VendorPage = crate::install::VendorPage {
name: "Microsoft",
url: "https://learn.microsoft.com/virtualization/hyper-v-on-windows/quick-start/enable-hyper-v",
};
const PARALLELS_PAGE: crate::install::VendorPage = crate::install::VendorPage {
name: "Parallels",
url: "https://www.parallels.com/products/desktop/",
};
impl ProviderRow {
#[must_use]
pub fn of(provider: Provider) -> ProviderRow {
const EVERYWHERE: ProviderRow = ProviderRow {
oses: &crate::host::ALL_OSES,
windows_on_arm: true,
mac_cpu: None,
linux_cpu: None,
vendor: None,
shares_folders: &crate::host::ALL_OSES,
plugin_minimum: None,
hypervisor_minimum: None,
check: Check::Remote,
};
const fn found_by(check: Check) -> ProviderRow {
ProviderRow { check, ..EVERYWHERE }
}
match provider {
Provider::Virtualbox => ProviderRow {
windows_on_arm: false,
mac_cpu: Some(Arch::Amd64),
linux_cpu: Some(Arch::Amd64),
..found_by(Check::Tool("VBoxManage", &["--version"]))
},
Provider::VmwareDesktop => ProviderRow {
windows_on_arm: false,
linux_cpu: Some(Arch::Amd64),
vendor: Some(crate::install::BROADCOM_DOWNLOADS),
..found_by(Check::Tool("vmrun", &[]))
},
Provider::Hyperv => ProviderRow {
oses: &[HostOs::Windows],
vendor: Some(HYPERV_PAGE),
shares_folders: &[],
..found_by(Check::HypervFeature)
},
Provider::Parallels => ProviderRow {
oses: &[HostOs::Macos],
vendor: Some(PARALLELS_PAGE),
..found_by(Check::Tool("prlctl", &["--version"]))
},
Provider::Utm => ProviderRow {
oses: &[HostOs::Macos],
mac_cpu: Some(Arch::Arm64),
plugin_minimum: Some("0.2.0"),
hypervisor_minimum: Some(("UTM", "4.6.5")),
..found_by(Check::UtmApp)
},
Provider::Libvirt => ProviderRow {
oses: &[HostOs::Linux],
..found_by(Check::Tool("virsh", &["--version"]))
},
Provider::Qemu => ProviderRow {
oses: &[HostOs::Macos, HostOs::Linux],
shares_folders: &[HostOs::Linux],
..found_by(Check::Qemu)
},
Provider::VmwareEsxi => ProviderRow {
shares_folders: &[],
..EVERYWHERE
},
}
}
#[must_use]
pub fn remote(&self) -> bool {
self.check == Check::Remote
}
}
#[must_use]
pub fn download_page(provider: Provider, os: HostOs) -> Option<crate::install::VendorPage> {
match (provider, os) {
(Provider::VmwareDesktop, HostOs::Macos) => Some(crate::install::VMWARE_FUSION_PAGE),
(Provider::VmwareDesktop, _) => Some(crate::install::VMWARE_WORKSTATION_PAGE),
_ => ProviderRow::of(provider).vendor,
}
}
#[must_use]
pub fn applicable(provider: Provider, host: &Host) -> bool {
let row = ProviderRow::of(provider);
row.oses.contains(&host.os) && (row.windows_on_arm || !host.is_windows_on_arm())
}
#[must_use]
pub fn offered(provider: Provider, host: &Host, installed: bool) -> bool {
let row = ProviderRow::of(provider);
let cpu = match host.os {
HostOs::Macos => row.mac_cpu,
HostOs::Linux => row.linux_cpu,
HostOs::Windows => None,
};
applicable(provider, host) && (installed || cpu.is_none_or(|cpu| cpu == host.arch))
}
#[must_use]
pub fn needs_vmware_utility(provider: Provider) -> bool {
provider == Provider::VmwareDesktop
}
#[must_use]
pub fn hypervisor_probe(provider: Provider, arch: Arch) -> Option<(&'static str, &'static [&'static str])> {
match ProviderRow::of(provider).check {
Check::Tool(program, args) => Some((program, args)),
Check::Qemu => Some((crate::host::qemu_system(arch), &["--version"])),
Check::UtmApp | Check::HypervFeature | Check::Remote => None,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(any(test, feature = "ts-rs"), derive(ts_rs::TS))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum AccountLacks {
HypervAdmin,
NewLogin,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(any(test, feature = "ts-rs"), derive(ts_rs::TS))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ProviderStatus {
pub provider: Provider,
pub remote: bool,
pub available: bool,
pub hypervisor: Option<bool>,
pub plugin: Option<String>,
pub plugin_installed: bool,
pub utility: Option<UtilityState>,
pub needs: Vec<Need>,
pub reason: Option<Unavailable>,
#[cfg_attr(any(test, feature = "ts-rs"), ts(optional = nullable))]
pub account_lacks: Option<AccountLacks>,
}
pub async fn detect(cx: &Context, vagrant_installed: bool) -> Vec<ProviderStatus> {
let mut asked = Asked::of(cx, vagrant_installed).await;
let mut statuses = Vec::new();
for provider in Provider::ALL {
statuses.extend(detect_one(cx, provider, &mut asked).await);
}
statuses
}
pub async fn status(cx: &Context, provider: Provider) -> Option<ProviderStatus> {
let vagrant_installed = cx.read_ok("vagrant", &["--version"]).await.is_ok();
let mut asked = Asked::of(cx, vagrant_installed).await;
detect_one(cx, provider, &mut asked).await
}
struct Asked {
vagrant_installed: bool,
plugins: Result<Vec<(String, Option<String>)>, String>,
pending_groups: Option<Vec<String>>,
vmware_plugin_alone: Option<bool>,
}
impl Asked {
async fn of(cx: &Context, vagrant_installed: bool) -> Asked {
let plugins = if vagrant_installed {
cx.read_ok("vagrant", &["plugin", "list", "--machine-readable"])
.await
.map(|out| isothrum_runner::vagrant_plugins(&out))
} else {
Ok(Vec::new())
};
Asked {
vagrant_installed,
plugins,
pending_groups: None,
vmware_plugin_alone: None,
}
}
async fn vmware_plugin_without_utility(&mut self, cx: &Context) -> bool {
if !self.vagrant_installed {
return false;
}
if self.vmware_plugin_alone.is_none() {
let plugin = Provider::VmwareDesktop.plugin().and_then(|p| self.plugin(p)).is_some();
self.vmware_plugin_alone = Some(plugin && vmware_utility(&cx.host).await == UtilityState::Missing);
}
self.vmware_plugin_alone == Some(true)
}
async fn pending_groups(&mut self, cx: &Context) -> &[String] {
if self.pending_groups.is_none() {
self.pending_groups = Some(kvm::groups_pending(cx).await);
}
self.pending_groups.as_deref().unwrap_or_default()
}
fn plugin(&self, plugin: &str) -> Option<&(String, Option<String>)> {
let listed = self.plugins.as_deref().unwrap_or_default();
listed.iter().find(|(name, _)| name == plugin)
}
}
async fn detect_one(cx: &Context, provider: Provider, asked: &mut Asked) -> Option<ProviderStatus> {
if !applicable(provider, &cx.host) {
return None;
}
let found = hypervisor_found(cx, provider).await;
if !offered(provider, &cx.host, found.installed == Some(true)) {
return None;
}
let findings = findings(cx, provider, found, asked).await;
Some(status_from(provider, asked.vagrant_installed, &findings))
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum Account {
HypervAdmin,
NewLogin(String),
}
#[derive(Debug, Clone, Default)]
struct Findings {
held: Option<Unavailable>,
unresponsive: bool,
hypervisor: Option<bool>,
helper_missing: Option<Helper>,
program: Option<&'static str>,
hypervisor_version: Option<String>,
plugin: Option<&'static str>,
plugin_version: Option<String>,
plugin_installed: bool,
plugins_unknown: Option<String>,
utility: Option<UtilityState>,
account: Option<Account>,
vmware_plugin_alone: bool,
platform: Option<Unavailable>,
driver: Option<Unavailable>,
}
async fn findings(cx: &Context, provider: Provider, found: Found, asked: &mut Asked) -> Findings {
let host = &cx.host;
let helper_missing = if found.installed == Some(true) {
helper::missing(cx, provider).await
} else {
None
};
let hypervisor = if helper_missing.is_some() { Some(false) } else { found.installed };
let ready = hypervisor == Some(true);
let virtualbox = provider == Provider::Virtualbox;
let plugin = provider.plugin();
let listed = plugin.and_then(|p| asked.plugin(p));
let plugin_version = listed.and_then(|(_, version)| version.clone());
Findings {
held: (virtualbox && kvm::holds_virtualization(host)).then(|| {
if kvm::shares_from_next_load(host) {
Unavailable::KvmSharesAfterRestart
} else {
Unavailable::KvmHoldsVirtualization
}
}),
unresponsive: asked.vagrant_installed && virtualbox && ready && vbox::wedged(cx).await,
hypervisor,
helper_missing,
program: hypervisor_probe(provider, host.arch).map(|(program, _)| program),
hypervisor_version: if ready { hypervisor_version(cx, provider).await } else { None },
plugin,
plugin_installed: plugin.is_none() || (asked.vagrant_installed && listed.is_some() && plugin_too_old(provider, plugin_version.as_deref()).is_none()),
plugin_version,
plugins_unknown: asked.plugins.as_ref().err().cloned(),
utility: utility_of(provider, host).await,
account: if ready { account_lacks(cx, provider, asked).await } else { None },
vmware_plugin_alone: asked.vmware_plugin_without_utility(cx).await,
platform: if ready { platform_problem(cx, provider).await } else { None },
driver: if virtualbox && ready {
vbox::driver_problem(cx, &found.said).await
} else {
None
},
}
}
async fn utility_of(provider: Provider, host: &Host) -> Option<UtilityState> {
if needs_vmware_utility(provider) {
Some(vmware_utility(host).await)
} else {
None
}
}
async fn account_lacks(cx: &Context, provider: Provider, asked: &mut Asked) -> Option<Account> {
if provider == Provider::Hyperv && hyperv::needs_admin(cx).await {
return Some(Account::HypervAdmin);
}
let joined = login_groups(provider, &cx.host);
if joined.is_empty() {
return None;
}
let pending = asked.pending_groups(cx).await;
joined
.iter()
.any(|g| pending.contains(g))
.then(|| Account::NewLogin(kvm::new_login_group(provider, &cx.host)))
}
async fn platform_problem(cx: &Context, provider: Provider) -> Option<Unavailable> {
if uses_kvm(provider, cx.host.os) {
kvm::kvm_problem(&cx.host)
} else if provider == Provider::Hyperv {
hyperv::hypervisor_stopped(cx).await
} else {
None
}
}
fn status_from(provider: Provider, vagrant_installed: bool, findings: &Findings) -> ProviderStatus {
let needs = needs(vagrant_installed, findings.plugin_installed, findings.utility);
let reason = unavailable_reason(provider, &needs, findings);
ProviderStatus {
provider,
remote: ProviderRow::of(provider).remote(),
available: reason.is_none(),
hypervisor: findings.hypervisor,
plugin: findings.plugin.map(str::to_owned),
plugin_installed: findings.plugin_installed,
utility: findings.utility,
needs,
reason,
account_lacks: findings.account.as_ref().map(|account| match account {
Account::HypervAdmin => AccountLacks::HypervAdmin,
Account::NewLogin(_) => AccountLacks::NewLogin,
}),
}
}
#[cfg(test)]
mod tests;