1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <stdbool.h>
5 
6 #include "errno-list.h"
7 #include "macro.h"
8 
9 typedef enum Virtualization {
10         VIRTUALIZATION_NONE = 0,
11 
12         VIRTUALIZATION_VM_FIRST,
13         VIRTUALIZATION_KVM = VIRTUALIZATION_VM_FIRST,
14         VIRTUALIZATION_AMAZON,
15         VIRTUALIZATION_QEMU,
16         VIRTUALIZATION_BOCHS,
17         VIRTUALIZATION_XEN,
18         VIRTUALIZATION_UML,
19         VIRTUALIZATION_VMWARE,
20         VIRTUALIZATION_ORACLE,
21         VIRTUALIZATION_MICROSOFT,
22         VIRTUALIZATION_ZVM,
23         VIRTUALIZATION_PARALLELS,
24         VIRTUALIZATION_BHYVE,
25         VIRTUALIZATION_QNX,
26         VIRTUALIZATION_ACRN,
27         VIRTUALIZATION_POWERVM,
28         VIRTUALIZATION_VM_OTHER,
29         VIRTUALIZATION_VM_LAST = VIRTUALIZATION_VM_OTHER,
30 
31         VIRTUALIZATION_CONTAINER_FIRST,
32         VIRTUALIZATION_SYSTEMD_NSPAWN = VIRTUALIZATION_CONTAINER_FIRST,
33         VIRTUALIZATION_LXC_LIBVIRT,
34         VIRTUALIZATION_LXC,
35         VIRTUALIZATION_OPENVZ,
36         VIRTUALIZATION_DOCKER,
37         VIRTUALIZATION_PODMAN,
38         VIRTUALIZATION_RKT,
39         VIRTUALIZATION_WSL,
40         VIRTUALIZATION_PROOT,
41         VIRTUALIZATION_POUCH,
42         VIRTUALIZATION_CONTAINER_OTHER,
43         VIRTUALIZATION_CONTAINER_LAST = VIRTUALIZATION_CONTAINER_OTHER,
44 
45         _VIRTUALIZATION_MAX,
46         _VIRTUALIZATION_INVALID = -EINVAL,
47         _VIRTUALIZATION_ERRNO_MAX = -ERRNO_MAX, /* ensure full range of errno fits into this enum */
48 } Virtualization;
49 
VIRTUALIZATION_IS_VM(Virtualization x)50 static inline bool VIRTUALIZATION_IS_VM(Virtualization x) {
51         return x >= VIRTUALIZATION_VM_FIRST && x <= VIRTUALIZATION_VM_LAST;
52 }
53 
VIRTUALIZATION_IS_CONTAINER(Virtualization x)54 static inline bool VIRTUALIZATION_IS_CONTAINER(Virtualization x) {
55         return x >= VIRTUALIZATION_CONTAINER_FIRST && x <= VIRTUALIZATION_CONTAINER_LAST;
56 }
57 
58 Virtualization detect_vm(void);
59 Virtualization detect_container(void);
60 Virtualization detect_virtualization(void);
61 
62 int running_in_userns(void);
63 int running_in_chroot(void);
64 
65 const char *virtualization_to_string(Virtualization v) _const_;
66 Virtualization virtualization_from_string(const char *s) _pure_;
67 bool has_cpu_with_flag(const char *flag);
68