use std::sync::OnceLock;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Virtualization {
pub hypervisor: Option<Hypervisor>,
pub container: Option<Container>,
pub wsl: Option<u8>,
pub kubernetes: bool,
pub evidence: Vec<Evidence>,
}
impl Virtualization {
pub(crate) fn fired(&mut self, check: &'static str, strength: Strength) {
self.evidence.push(Evidence { check, strength });
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum Hypervisor {
Kvm,
Qemu,
Vmware,
HyperV,
Xen,
VirtualBox,
Parallels,
Bhyve,
Acrn,
Qnx,
AppleVirtualization,
AmazonNitro,
GoogleCompute,
Bochs,
Other,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum Container {
Docker,
Podman,
Containerd,
Lxc,
SystemdNspawn,
OpenVz,
Other,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub struct Evidence {
pub check: &'static str,
pub strength: Strength,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum Strength {
Strong,
Weak,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum Cloud {
Aws,
Gcp,
Azure,
Oracle,
Alibaba,
Hetzner,
DigitalOcean,
Linode,
Vultr,
Scaleway,
Ovh,
Other,
}
#[must_use]
pub fn detect() -> &'static Virtualization {
static FACTS: OnceLock<Virtualization> = OnceLock::new();
FACTS.get_or_init(crate::sys::virtualization)
}
#[must_use]
pub fn is_vm() -> bool {
detect().hypervisor.is_some()
}
#[must_use]
pub fn is_container() -> bool {
detect().container.is_some()
}
#[must_use]
pub fn is_wsl() -> bool {
detect().wsl.is_some()
}
#[must_use]
pub fn is_kubernetes() -> bool {
detect().kubernetes
}
#[must_use]
pub fn hypervisor() -> Option<Hypervisor> {
detect().hypervisor
}
#[must_use]
pub fn container() -> Option<Container> {
detect().container
}
#[must_use]
pub fn cloud() -> Option<Cloud> {
static FACTS: OnceLock<Option<Cloud>> = OnceLock::new();
*FACTS.get_or_init(crate::sys::cloud)
}
#[cfg_attr(target_vendor = "apple", allow(dead_code))]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct Dmi<'a> {
pub(crate) sys_vendor: &'a str,
pub(crate) product: &'a str,
pub(crate) product_version: &'a str,
pub(crate) board_vendor: &'a str,
pub(crate) bios_vendor: &'a str,
pub(crate) asset_tag: &'a str,
}
impl Dmi<'_> {
#[cfg_attr(target_vendor = "apple", allow(dead_code))]
fn any(&self, needle: &str) -> bool {
[
self.sys_vendor,
self.product,
self.product_version,
self.board_vendor,
self.bios_vendor,
]
.iter()
.any(|s| contains_ignore_case(s, needle))
}
}
#[cfg_attr(target_vendor = "apple", allow(dead_code))]
fn contains_ignore_case(haystack: &str, needle: &str) -> bool {
haystack
.as_bytes()
.windows(needle.len())
.any(|w| w.eq_ignore_ascii_case(needle.as_bytes()))
}
pub(crate) fn hypervisor_from_signature(signature: &[u8; 12]) -> Hypervisor {
match signature {
b"KVMKVMKVM\0\0\0" | b"Linux KVM Hv" => Hypervisor::Kvm,
b"TCGTCGTCGTCG" => Hypervisor::Qemu,
b"VMwareVMware" => Hypervisor::Vmware,
b"Microsoft Hv" => Hypervisor::HyperV,
b"XenVMMXenVMM" => Hypervisor::Xen,
b"bhyve bhyve " => Hypervisor::Bhyve,
b"QNXQVMBSQG\0\0" => Hypervisor::Qnx,
b"ACRNACRNACRN" => Hypervisor::Acrn,
b"Apple VZ\0\0\0\0" | b"VirtualApple" => Hypervisor::AppleVirtualization,
b"VBoxVBoxVBox" => Hypervisor::VirtualBox,
b" prl hyperv " | b" lrpepyh vr " => Hypervisor::Parallels,
_ => Hypervisor::Other,
}
}
#[cfg_attr(target_vendor = "apple", allow(dead_code))]
pub(crate) fn hypervisor_from_dmi(dmi: &Dmi<'_>) -> Option<Hypervisor> {
let table: &[(&str, Hypervisor)] = &[
("Amazon EC2", Hypervisor::AmazonNitro),
("Google Compute Engine", Hypervisor::GoogleCompute),
("Alibaba Cloud ECS", Hypervisor::Kvm),
("KubeVirt", Hypervisor::Kvm),
("OpenStack", Hypervisor::Kvm),
("KVM", Hypervisor::Kvm),
("QEMU", Hypervisor::Qemu),
("VMware", Hypervisor::Vmware),
("innotek GmbH", Hypervisor::VirtualBox),
("VirtualBox", Hypervisor::VirtualBox),
("Xen", Hypervisor::Xen),
("Bochs", Hypervisor::Bochs),
("Parallels", Hypervisor::Parallels),
("BHYVE", Hypervisor::Bhyve),
("Apple Virtualization", Hypervisor::AppleVirtualization),
];
if let Some((_, h)) = table.iter().find(|(needle, _)| dmi.any(needle)) {
return Some(*h);
}
let microsoft = contains_ignore_case(dmi.sys_vendor, "Microsoft Corporation");
(microsoft && contains_ignore_case(dmi.product, "Virtual Machine"))
.then_some(Hypervisor::HyperV)
}
#[cfg_attr(target_vendor = "apple", allow(dead_code))]
pub(crate) fn cloud_from_dmi(dmi: &Dmi<'_>, hypervisor: Option<Hypervisor>) -> Option<Cloud> {
let table: &[(&str, Cloud)] = &[
("Amazon EC2", Cloud::Aws),
("Amazon", Cloud::Aws),
("Google Compute Engine", Cloud::Gcp),
("Google", Cloud::Gcp),
("Alibaba Cloud", Cloud::Alibaba),
("Hetzner", Cloud::Hetzner),
("DigitalOcean", Cloud::DigitalOcean),
("Linode", Cloud::Linode),
("Akamai", Cloud::Linode),
("Vultr", Cloud::Vultr),
("Scaleway", Cloud::Scaleway),
("OVHcloud", Cloud::Ovh),
];
if let Some((_, c)) = table.iter().find(|(needle, _)| dmi.any(needle)) {
return Some(*c);
}
if dmi.asset_tag == "7783-7084-3265-9085-8269-3286-77" {
return Some(Cloud::Azure);
}
if dmi.asset_tag == "OracleCloud.com" {
return Some(Cloud::Oracle);
}
match hypervisor {
Some(Hypervisor::AmazonNitro) => Some(Cloud::Aws),
Some(Hypervisor::GoogleCompute) => Some(Cloud::Gcp),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_virt_signatures() {
assert_eq!(
hypervisor_from_signature(b"KVMKVMKVM\0\0\0"),
Hypervisor::Kvm
);
assert_eq!(hypervisor_from_signature(b"Linux KVM Hv"), Hypervisor::Kvm);
assert_eq!(
hypervisor_from_signature(b"Microsoft Hv"),
Hypervisor::HyperV
);
assert_eq!(
hypervisor_from_signature(b"VMwareVMware"),
Hypervisor::Vmware
);
assert_eq!(
hypervisor_from_signature(b"something new"[..12].try_into().unwrap()),
Hypervisor::Other
);
}
#[test]
fn test_virt_dmi_strings() {
let dmi = |product, sys_vendor| Dmi {
product,
sys_vendor,
..Dmi::default()
};
assert_eq!(
hypervisor_from_dmi(&dmi("Standard PC (Q35 + ICH9, 2009)", "QEMU")),
Some(Hypervisor::Qemu)
);
assert_eq!(
hypervisor_from_dmi(&dmi("VirtualBox", "innotek GmbH")),
Some(Hypervisor::VirtualBox)
);
assert_eq!(
hypervisor_from_dmi(&dmi("m5.large", "Amazon EC2")),
Some(Hypervisor::AmazonNitro)
);
assert_eq!(
hypervisor_from_dmi(&dmi("Virtual Machine", "Microsoft Corporation")),
Some(Hypervisor::HyperV)
);
assert_eq!(
hypervisor_from_dmi(&dmi("Surface Laptop 5", "Microsoft Corporation")),
None
);
assert_eq!(hypervisor_from_dmi(&dmi("ThinkPad X1", "LENOVO")), None);
}
#[test]
fn test_virt_clouds() {
let aws = Dmi {
sys_vendor: "Amazon EC2",
..Dmi::default()
};
assert_eq!(cloud_from_dmi(&aws, None), Some(Cloud::Aws));
let azure = Dmi {
sys_vendor: "Microsoft Corporation",
product: "Virtual Machine",
asset_tag: "7783-7084-3265-9085-8269-3286-77",
..Dmi::default()
};
assert_eq!(
cloud_from_dmi(&azure, Some(Hypervisor::HyperV)),
Some(Cloud::Azure)
);
let hyperv = Dmi {
asset_tag: "",
..azure
};
assert_eq!(cloud_from_dmi(&hyperv, Some(Hypervisor::HyperV)), None);
assert_eq!(cloud_from_dmi(&Dmi::default(), None), None);
}
#[test]
fn test_virt_live_is_consistent() {
let v = detect();
assert!(std::ptr::eq(v, detect()));
assert_eq!(is_vm(), v.hypervisor.is_some());
assert_eq!(is_container(), v.container.is_some());
if let Some(w) = v.wsl {
assert!(matches!(w, 1 | 2));
}
for e in &v.evidence {
assert!(!e.check.is_empty());
}
}
}