1use std::sync::OnceLock;
25
26#[derive(Clone, Debug, Default, PartialEq, Eq)]
29#[non_exhaustive]
30pub struct Virtualization {
31 pub hypervisor: Option<Hypervisor>,
33 pub container: Option<Container>,
35 pub wsl: Option<u8>,
37 pub kubernetes: bool,
40 pub evidence: Vec<Evidence>,
42}
43
44impl Virtualization {
45 pub(crate) fn fired(&mut self, check: &'static str, strength: Strength) {
47 self.evidence.push(Evidence { check, strength });
48 }
49}
50
51#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
53#[non_exhaustive]
54pub enum Hypervisor {
55 Kvm,
57 Qemu,
59 Vmware,
61 HyperV,
63 Xen,
65 VirtualBox,
67 Parallels,
69 Bhyve,
71 Acrn,
73 Qnx,
75 AppleVirtualization,
77 AmazonNitro,
79 GoogleCompute,
81 Bochs,
83 Other,
85}
86
87#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
89#[non_exhaustive]
90pub enum Container {
91 Docker,
93 Podman,
95 Containerd,
97 Lxc,
99 SystemdNspawn,
101 OpenVz,
103 Other,
105}
106
107#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
109#[non_exhaustive]
110pub struct Evidence {
111 pub check: &'static str,
113 pub strength: Strength,
115}
116
117#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
119#[non_exhaustive]
120pub enum Strength {
121 Strong,
123 Weak,
125}
126
127#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
130#[non_exhaustive]
131pub enum Cloud {
132 Aws,
134 Gcp,
136 Azure,
138 Oracle,
140 Alibaba,
142 Hetzner,
144 DigitalOcean,
146 Linode,
148 Vultr,
150 Scaleway,
152 Ovh,
154 Other,
156}
157
158#[must_use]
160pub fn detect() -> &'static Virtualization {
161 static FACTS: OnceLock<Virtualization> = OnceLock::new();
162 FACTS.get_or_init(crate::sys::virtualization)
163}
164
165#[must_use]
169pub fn is_vm() -> bool {
170 detect().hypervisor.is_some()
171}
172
173#[must_use]
175pub fn is_container() -> bool {
176 detect().container.is_some()
177}
178
179#[must_use]
181pub fn is_wsl() -> bool {
182 detect().wsl.is_some()
183}
184
185#[must_use]
187pub fn is_kubernetes() -> bool {
188 detect().kubernetes
189}
190
191#[must_use]
193pub fn hypervisor() -> Option<Hypervisor> {
194 detect().hypervisor
195}
196
197#[must_use]
199pub fn container() -> Option<Container> {
200 detect().container
201}
202
203#[must_use]
206pub fn cloud() -> Option<Cloud> {
207 static FACTS: OnceLock<Option<Cloud>> = OnceLock::new();
208 *FACTS.get_or_init(crate::sys::cloud)
209}
210
211#[cfg_attr(target_vendor = "apple", allow(dead_code))]
213#[derive(Clone, Copy, Debug, Default)]
214pub(crate) struct Dmi<'a> {
215 pub(crate) sys_vendor: &'a str,
216 pub(crate) product: &'a str,
217 pub(crate) product_version: &'a str,
218 pub(crate) board_vendor: &'a str,
219 pub(crate) bios_vendor: &'a str,
220 pub(crate) asset_tag: &'a str,
221}
222
223impl Dmi<'_> {
224 #[cfg_attr(target_vendor = "apple", allow(dead_code))]
225 fn any(&self, needle: &str) -> bool {
226 [
227 self.sys_vendor,
228 self.product,
229 self.product_version,
230 self.board_vendor,
231 self.bios_vendor,
232 ]
233 .iter()
234 .any(|s| contains_ignore_case(s, needle))
235 }
236}
237
238#[cfg_attr(target_vendor = "apple", allow(dead_code))]
239fn contains_ignore_case(haystack: &str, needle: &str) -> bool {
240 haystack
241 .as_bytes()
242 .windows(needle.len())
243 .any(|w| w.eq_ignore_ascii_case(needle.as_bytes()))
244}
245
246pub(crate) fn hypervisor_from_signature(signature: &[u8; 12]) -> Hypervisor {
248 match signature {
249 b"KVMKVMKVM\0\0\0" | b"Linux KVM Hv" => Hypervisor::Kvm,
250 b"TCGTCGTCGTCG" => Hypervisor::Qemu,
251 b"VMwareVMware" => Hypervisor::Vmware,
252 b"Microsoft Hv" => Hypervisor::HyperV,
253 b"XenVMMXenVMM" => Hypervisor::Xen,
254 b"bhyve bhyve " => Hypervisor::Bhyve,
255 b"QNXQVMBSQG\0\0" => Hypervisor::Qnx,
256 b"ACRNACRNACRN" => Hypervisor::Acrn,
257 b"Apple VZ\0\0\0\0" | b"VirtualApple" => Hypervisor::AppleVirtualization,
258 b"VBoxVBoxVBox" => Hypervisor::VirtualBox,
259 b" prl hyperv " | b" lrpepyh vr " => Hypervisor::Parallels,
260 _ => Hypervisor::Other,
261 }
262}
263
264#[cfg_attr(target_vendor = "apple", allow(dead_code))]
267pub(crate) fn hypervisor_from_dmi(dmi: &Dmi<'_>) -> Option<Hypervisor> {
268 let table: &[(&str, Hypervisor)] = &[
269 ("Amazon EC2", Hypervisor::AmazonNitro),
270 ("Google Compute Engine", Hypervisor::GoogleCompute),
271 ("Alibaba Cloud ECS", Hypervisor::Kvm),
272 ("KubeVirt", Hypervisor::Kvm),
273 ("OpenStack", Hypervisor::Kvm),
274 ("KVM", Hypervisor::Kvm),
275 ("QEMU", Hypervisor::Qemu),
276 ("VMware", Hypervisor::Vmware),
277 ("innotek GmbH", Hypervisor::VirtualBox),
278 ("VirtualBox", Hypervisor::VirtualBox),
279 ("Xen", Hypervisor::Xen),
280 ("Bochs", Hypervisor::Bochs),
281 ("Parallels", Hypervisor::Parallels),
282 ("BHYVE", Hypervisor::Bhyve),
283 ("Apple Virtualization", Hypervisor::AppleVirtualization),
284 ];
285 if let Some((_, h)) = table.iter().find(|(needle, _)| dmi.any(needle)) {
286 return Some(*h);
287 }
288 let microsoft = contains_ignore_case(dmi.sys_vendor, "Microsoft Corporation");
291 (microsoft && contains_ignore_case(dmi.product, "Virtual Machine"))
292 .then_some(Hypervisor::HyperV)
293}
294
295#[cfg_attr(target_vendor = "apple", allow(dead_code))]
297pub(crate) fn cloud_from_dmi(dmi: &Dmi<'_>, hypervisor: Option<Hypervisor>) -> Option<Cloud> {
298 let table: &[(&str, Cloud)] = &[
299 ("Amazon EC2", Cloud::Aws),
300 ("Amazon", Cloud::Aws),
301 ("Google Compute Engine", Cloud::Gcp),
302 ("Google", Cloud::Gcp),
303 ("Alibaba Cloud", Cloud::Alibaba),
304 ("Hetzner", Cloud::Hetzner),
305 ("DigitalOcean", Cloud::DigitalOcean),
306 ("Linode", Cloud::Linode),
307 ("Akamai", Cloud::Linode),
308 ("Vultr", Cloud::Vultr),
309 ("Scaleway", Cloud::Scaleway),
310 ("OVHcloud", Cloud::Ovh),
311 ];
312 if let Some((_, c)) = table.iter().find(|(needle, _)| dmi.any(needle)) {
313 return Some(*c);
314 }
315 if dmi.asset_tag == "7783-7084-3265-9085-8269-3286-77" {
317 return Some(Cloud::Azure);
318 }
319 if dmi.asset_tag == "OracleCloud.com" {
320 return Some(Cloud::Oracle);
321 }
322 match hypervisor {
323 Some(Hypervisor::AmazonNitro) => Some(Cloud::Aws),
324 Some(Hypervisor::GoogleCompute) => Some(Cloud::Gcp),
325 _ => None,
326 }
327}
328
329#[cfg(test)]
330mod tests {
331 use super::*;
332
333 #[test]
334 fn test_virt_signatures() {
335 assert_eq!(
336 hypervisor_from_signature(b"KVMKVMKVM\0\0\0"),
337 Hypervisor::Kvm
338 );
339 assert_eq!(hypervisor_from_signature(b"Linux KVM Hv"), Hypervisor::Kvm);
340 assert_eq!(
341 hypervisor_from_signature(b"Microsoft Hv"),
342 Hypervisor::HyperV
343 );
344 assert_eq!(
345 hypervisor_from_signature(b"VMwareVMware"),
346 Hypervisor::Vmware
347 );
348 assert_eq!(
349 hypervisor_from_signature(b"something new"[..12].try_into().unwrap()),
350 Hypervisor::Other
351 );
352 }
353
354 #[test]
355 fn test_virt_dmi_strings() {
356 let dmi = |product, sys_vendor| Dmi {
357 product,
358 sys_vendor,
359 ..Dmi::default()
360 };
361 assert_eq!(
362 hypervisor_from_dmi(&dmi("Standard PC (Q35 + ICH9, 2009)", "QEMU")),
363 Some(Hypervisor::Qemu)
364 );
365 assert_eq!(
366 hypervisor_from_dmi(&dmi("VirtualBox", "innotek GmbH")),
367 Some(Hypervisor::VirtualBox)
368 );
369 assert_eq!(
370 hypervisor_from_dmi(&dmi("m5.large", "Amazon EC2")),
371 Some(Hypervisor::AmazonNitro)
372 );
373 assert_eq!(
374 hypervisor_from_dmi(&dmi("Virtual Machine", "Microsoft Corporation")),
375 Some(Hypervisor::HyperV)
376 );
377 assert_eq!(
379 hypervisor_from_dmi(&dmi("Surface Laptop 5", "Microsoft Corporation")),
380 None
381 );
382 assert_eq!(hypervisor_from_dmi(&dmi("ThinkPad X1", "LENOVO")), None);
383 }
384
385 #[test]
386 fn test_virt_clouds() {
387 let aws = Dmi {
388 sys_vendor: "Amazon EC2",
389 ..Dmi::default()
390 };
391 assert_eq!(cloud_from_dmi(&aws, None), Some(Cloud::Aws));
392 let azure = Dmi {
393 sys_vendor: "Microsoft Corporation",
394 product: "Virtual Machine",
395 asset_tag: "7783-7084-3265-9085-8269-3286-77",
396 ..Dmi::default()
397 };
398 assert_eq!(
399 cloud_from_dmi(&azure, Some(Hypervisor::HyperV)),
400 Some(Cloud::Azure)
401 );
402 let hyperv = Dmi {
403 asset_tag: "",
404 ..azure
405 };
406 assert_eq!(cloud_from_dmi(&hyperv, Some(Hypervisor::HyperV)), None);
407 assert_eq!(cloud_from_dmi(&Dmi::default(), None), None);
408 }
409
410 #[test]
411 fn test_virt_live_is_consistent() {
412 let v = detect();
413 assert!(std::ptr::eq(v, detect()));
414 assert_eq!(is_vm(), v.hypervisor.is_some());
415 assert_eq!(is_container(), v.container.is_some());
416 if let Some(w) = v.wsl {
417 assert!(matches!(w, 1 | 2));
418 }
419 for e in &v.evidence {
421 assert!(!e.check.is_empty());
422 }
423 }
424}