heim_virt/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! Virtualization system detection.
//!
//! This module is enabled with the `virt` feature flag (enabled by default).
//!
//! At the moment not all declared virtualization systems are detected,
//! therefore this crate should be used very carefully.
//!
//! See the [issues list](https://github.com/heim-rs/heim/issues?q=is%3Aissue+is%3Aopen+label%3AA-virt)
//! for a not supported currently systems.

#![doc(html_root_url = "https://docs.rs/heim-virt/0.0.11")]
#![deny(
    unused,
    unused_imports,
    unused_features,
    bare_trait_objects,
    future_incompatible,
    missing_debug_implementations,
    missing_docs,
    nonstandard_style,
    dead_code,
    deprecated
)]
#![warn(
    trivial_casts,
    trivial_numeric_casts,
    unused_extern_crates,
    unused_import_braces,
    unused_results
)]

use heim_common::prelude::*;

mod sys;

/// Virtualization systems (both VMs and containers)
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, Hash)]
pub enum Virtualization {
    // VMs
    /// Kernel Virtual Machine (https://www.linux-kvm.org)
    Kvm,

    /// QEMU (https://www.qemu.org/)
    Qemu,

    /// Bochs IA-32 emulator (http://bochs.sourceforge.net/)
    Bochs,

    /// Xen project (https://xenproject.org/)
    Xen,

    /// User-Mode Linux (http://user-mode-linux.sourceforge.net/)
    Uml,

    /// VMware (https://www.vmware.com)
    Vmware,

    /// Oracle virtualization (https://www.oracle.com/virtualization/)
    Oracle,

    /// Microsoft Hyper-V (http://www.microsoft.com/hyper-v)
    HyperV,

    /// FreeBSD bhyve (https://wiki.freebsd.org/bhyve)
    Bhyve,
    //    Zvm,
    /// Parallels (https://www.parallels.com/)
    Parallels,

    /// QNX hypervisor (https://blackberry.qnx.com/en/products/hypervisor/index)
    Qnx,

    /// ACRN hypervisor (https://projectacrn.org/)
    Acrn,

    // Containers
    /// `systemd-nspawn` container manager (https://www.freedesktop.org/wiki/Software/systemd/)
    SystemdNspawn,

    /// `lxc-libvirt` (https://libvirt.org/drvlxc.html)
    LxcLibvirt,

    /// Linux Containers (https://linuxcontainers.org/lxc)
    Lxc,

    /// OpenVz (https://openvz.org/)
    OpenVz,

    /// Docker (https://www.docker.com/)
    Docker,

    /// Podman (https://podman.io/)
    Podman,

    /// CoreOS rkt (https://coreos.com/rkt/)
    Rkt,

    /// Microsoft WSL (https://docs.microsoft.com/en-us/windows/wsl/about)
    Wsl,

    /// Unknown virtualization system.
    ///
    /// Usually means that there are symptoms of being running in some virtualization system,
    /// but it can't be determined specifically.
    Unknown,

    #[doc(hidden)]
    __Nonexhaustive,
}

impl Virtualization {
    /// Returns `true` if it is a Virtual Machine virtualization.
    pub fn is_vm(&self) -> bool {
        match self {
            Virtualization::Kvm => true,
            Virtualization::Qemu => true,
            Virtualization::Bochs => true,
            Virtualization::Xen => true,
            Virtualization::Uml => true,
            Virtualization::Vmware => true,
            Virtualization::Oracle => true,
            Virtualization::HyperV => true,
            Virtualization::Bhyve => true,
            Virtualization::Qnx => true,
            Virtualization::Acrn => true,
            _ => false,
        }
    }

    /// Returns `true` if it is a container virtualization.
    pub fn is_container(&self) -> bool {
        match self {
            Virtualization::SystemdNspawn => true,
            Virtualization::LxcLibvirt => true,
            Virtualization::Lxc => true,
            Virtualization::OpenVz => true,
            Virtualization::Docker => true,
            Virtualization::Podman => true,
            Virtualization::Rkt => true,
            Virtualization::Wsl => true,
            _ => false,
        }
    }

    /// Returns string identifying this virtualization system.
    pub fn as_str(&self) -> &str {
        match self {
            Virtualization::Kvm => "kvm",
            Virtualization::Qemu => "qemu",
            Virtualization::Bochs => "bochs",
            Virtualization::Xen => "xen",
            Virtualization::Uml => "uml",
            Virtualization::Vmware => "vmware",
            Virtualization::Oracle => "oracle",
            Virtualization::HyperV => "hyperv",
            Virtualization::Bhyve => "bhyve",
            Virtualization::Qnx => "qnx",
            Virtualization::Acrn => "acrn",
            Virtualization::SystemdNspawn => "systemd-nspawn",
            Virtualization::LxcLibvirt => "lxc-libvirt",
            Virtualization::Lxc => "lxc",
            Virtualization::OpenVz => "openvz",
            Virtualization::Docker => "docker",
            Virtualization::Podman => "podman",
            Virtualization::Rkt => "rkt",
            Virtualization::Wsl => "wsl",
            Virtualization::Unknown => "unknown",
            _ => unreachable!(),
        }
    }
}

/// Returns future which tries to determine if the running program is running
/// in some [Virtualization] system.
///
/// ## Compatibility
///
/// At the moment this function works only for Linux (partially)
/// and always returns `None` for macOS and Windows.
pub fn detect() -> impl Future<Output = Option<Virtualization>> {
    self::sys::detect()
}