1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #if defined(__i386__) || defined(__x86_64__)
12 #include "alloc-util.h"
13 #include "dirent-util.h"
18 #include "process-util.h"
19 #include "stat-util.h"
20 #include "string-table.h"
21 #include "string-util.h"
24 static int detect_vm_cpuid(void) {
26 /* CPUID is an x86 specific interface. */
27 #if defined(__i386__) || defined(__x86_64__)
32 } cpuid_vendor_table
[] = {
33 { "XenVMMXenVMM", VIRTUALIZATION_XEN
},
34 { "KVMKVMKVM", VIRTUALIZATION_KVM
},
35 { "TCGTCGTCGTCG", VIRTUALIZATION_QEMU
},
36 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
37 { "VMwareVMware", VIRTUALIZATION_VMWARE
},
38 /* https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfs */
39 { "Microsoft Hv", VIRTUALIZATION_MICROSOFT
},
40 /* https://wiki.freebsd.org/bhyve */
41 { "bhyve bhyve ", VIRTUALIZATION_BHYVE
},
42 { "QNXQVMBSQG", VIRTUALIZATION_QNX
},
43 /* https://projectacrn.org */
44 { "ACRNACRNACRN", VIRTUALIZATION_ACRN
},
47 uint32_t eax
, ebx
, ecx
, edx
;
50 /* http://lwn.net/Articles/301888/ */
52 /* First detect whether there is a hypervisor */
53 if (__get_cpuid(1, &eax
, &ebx
, &ecx
, &edx
) == 0)
54 return VIRTUALIZATION_NONE
;
56 hypervisor
= ecx
& 0x80000000U
;
65 /* There is a hypervisor, see what it is */
66 __cpuid(0x40000000U
, eax
, ebx
, ecx
, edx
);
72 log_debug("Virtualization found, CPUID=%s", sig
.text
);
74 for (j
= 0; j
< ELEMENTSOF(cpuid_vendor_table
); j
++)
75 if (streq(sig
.text
, cpuid_vendor_table
[j
].cpuid
))
76 return cpuid_vendor_table
[j
].id
;
78 return VIRTUALIZATION_VM_OTHER
;
81 log_debug("No virtualization found in CPUID");
83 return VIRTUALIZATION_NONE
;
86 static int detect_vm_device_tree(void) {
87 #if defined(__arm__) || defined(__aarch64__) || defined(__powerpc__) || defined(__powerpc64__)
88 _cleanup_free_
char *hvtype
= NULL
;
91 r
= read_one_line_file("/proc/device-tree/hypervisor/compatible", &hvtype
);
93 _cleanup_closedir_
DIR *dir
= NULL
;
96 dir
= opendir("/proc/device-tree");
98 if (errno
== ENOENT
) {
99 log_debug_errno(errno
, "/proc/device-tree: %m");
100 return VIRTUALIZATION_NONE
;
105 FOREACH_DIRENT(dent
, dir
, return -errno
)
106 if (strstr(dent
->d_name
, "fw-cfg")) {
107 log_debug("Virtualization QEMU: \"fw-cfg\" present in /proc/device-tree/%s", dent
->d_name
);
108 return VIRTUALIZATION_QEMU
;
111 log_debug("No virtualization found in /proc/device-tree/*");
112 return VIRTUALIZATION_NONE
;
116 log_debug("Virtualization %s found in /proc/device-tree/hypervisor/compatible", hvtype
);
117 if (streq(hvtype
, "linux,kvm"))
118 return VIRTUALIZATION_KVM
;
119 else if (strstr(hvtype
, "xen"))
120 return VIRTUALIZATION_XEN
;
122 return VIRTUALIZATION_VM_OTHER
;
124 log_debug("This platform does not support /proc/device-tree");
125 return VIRTUALIZATION_NONE
;
129 static int detect_vm_dmi(void) {
130 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
132 static const char *const dmi_vendors
[] = {
133 "/sys/class/dmi/id/product_name", /* Test this before sys_vendor to detect KVM over QEMU */
134 "/sys/class/dmi/id/sys_vendor",
135 "/sys/class/dmi/id/board_vendor",
136 "/sys/class/dmi/id/bios_vendor"
139 static const struct {
142 } dmi_vendor_table
[] = {
143 { "KVM", VIRTUALIZATION_KVM
},
144 { "QEMU", VIRTUALIZATION_QEMU
},
145 { "VMware", VIRTUALIZATION_VMWARE
}, /* https://kb.vmware.com/s/article/1009458 */
146 { "VMW", VIRTUALIZATION_VMWARE
},
147 { "innotek GmbH", VIRTUALIZATION_ORACLE
},
148 { "Oracle Corporation", VIRTUALIZATION_ORACLE
},
149 { "Xen", VIRTUALIZATION_XEN
},
150 { "Bochs", VIRTUALIZATION_BOCHS
},
151 { "Parallels", VIRTUALIZATION_PARALLELS
},
152 /* https://wiki.freebsd.org/bhyve */
153 { "BHYVE", VIRTUALIZATION_BHYVE
},
158 for (i
= 0; i
< ELEMENTSOF(dmi_vendors
); i
++) {
159 _cleanup_free_
char *s
= NULL
;
162 r
= read_one_line_file(dmi_vendors
[i
], &s
);
170 for (j
= 0; j
< ELEMENTSOF(dmi_vendor_table
); j
++)
171 if (startswith(s
, dmi_vendor_table
[j
].vendor
)) {
172 log_debug("Virtualization %s found in DMI (%s)", s
, dmi_vendors
[i
]);
173 return dmi_vendor_table
[j
].id
;
178 log_debug("No virtualization found in DMI");
180 return VIRTUALIZATION_NONE
;
183 static int detect_vm_xen(void) {
185 /* Check for Dom0 will be executed later in detect_vm_xen_dom0
186 The presence of /proc/xen indicates some form of a Xen domain */
187 if (access("/proc/xen", F_OK
) < 0) {
188 log_debug("Virtualization XEN not found, /proc/xen does not exist");
189 return VIRTUALIZATION_NONE
;
192 log_debug("Virtualization XEN found (/proc/xen exists)");
193 return VIRTUALIZATION_XEN
;
196 #define XENFEAT_dom0 11 /* xen/include/public/features.h */
197 #define PATH_FEATURES "/sys/hypervisor/properties/features"
198 /* Returns -errno, or 0 for domU, or 1 for dom0 */
199 static int detect_vm_xen_dom0(void) {
200 _cleanup_free_
char *domcap
= NULL
;
203 r
= read_one_line_file(PATH_FEATURES
, &domcap
);
204 if (r
< 0 && r
!= -ENOENT
)
207 unsigned long features
;
209 /* Here, we need to use sscanf() instead of safe_atoul()
210 * as the string lacks the leading "0x". */
211 r
= sscanf(domcap
, "%lx", &features
);
213 r
= !!(features
& (1U << XENFEAT_dom0
));
214 log_debug("Virtualization XEN, found %s with value %08lx, "
215 "XENFEAT_dom0 (indicating the 'hardware domain') is%s set.",
216 PATH_FEATURES
, features
, r
? "" : " not");
219 log_debug("Virtualization XEN, found %s, unhandled content '%s'",
220 PATH_FEATURES
, domcap
);
223 r
= read_one_line_file("/proc/xen/capabilities", &domcap
);
225 log_debug("Virtualization XEN because /proc/xen/capabilities does not exist");
231 for (const char *i
= domcap
;;) {
232 _cleanup_free_
char *cap
= NULL
;
234 r
= extract_first_word(&i
, &cap
, ",", 0);
238 log_debug("Virtualization XEN DomU found (/proc/xen/capabilities)");
242 if (streq(cap
, "control_d")) {
243 log_debug("Virtualization XEN Dom0 ignored (/proc/xen/capabilities)");
249 static int detect_vm_hypervisor(void) {
250 _cleanup_free_
char *hvtype
= NULL
;
253 r
= read_one_line_file("/sys/hypervisor/type", &hvtype
);
255 return VIRTUALIZATION_NONE
;
259 log_debug("Virtualization %s found in /sys/hypervisor/type", hvtype
);
261 if (streq(hvtype
, "xen"))
262 return VIRTUALIZATION_XEN
;
264 return VIRTUALIZATION_VM_OTHER
;
267 static int detect_vm_uml(void) {
268 _cleanup_fclose_
FILE *f
= NULL
;
271 /* Detect User-Mode Linux by reading /proc/cpuinfo */
272 f
= fopen("/proc/cpuinfo", "re");
274 if (errno
== ENOENT
) {
275 log_debug("/proc/cpuinfo not found, assuming no UML virtualization.");
276 return VIRTUALIZATION_NONE
;
282 _cleanup_free_
char *line
= NULL
;
285 r
= read_line(f
, LONG_LINE_MAX
, &line
);
291 t
= startswith(line
, "vendor_id\t: ");
293 if (startswith(t
, "User Mode Linux")) {
294 log_debug("UML virtualization found in /proc/cpuinfo");
295 return VIRTUALIZATION_UML
;
302 log_debug("UML virtualization not found in /proc/cpuinfo.");
303 return VIRTUALIZATION_NONE
;
306 static int detect_vm_zvm(void) {
308 #if defined(__s390__)
309 _cleanup_free_
char *t
= NULL
;
312 r
= get_proc_field("/proc/sysinfo", "VM00 Control Program", WHITESPACE
, &t
);
314 return VIRTUALIZATION_NONE
;
318 log_debug("Virtualization %s found in /proc/sysinfo", t
);
319 if (streq(t
, "z/VM"))
320 return VIRTUALIZATION_ZVM
;
322 return VIRTUALIZATION_KVM
;
324 log_debug("This platform does not support /proc/sysinfo");
325 return VIRTUALIZATION_NONE
;
329 /* Returns a short identifier for the various VM implementations */
330 int detect_vm(void) {
331 static thread_local
int cached_found
= _VIRTUALIZATION_INVALID
;
335 if (cached_found
>= 0)
338 /* We have to use the correct order here:
340 * → First, try to detect Oracle Virtualbox, even if it uses KVM, as well as Xen even if it cloaks as Microsoft
343 * → Second, try to detect from CPUID, this will report KVM for whatever software is used even if info in DMI is
346 * → Third, try to detect from DMI. */
348 dmi
= detect_vm_dmi();
349 if (IN_SET(dmi
, VIRTUALIZATION_ORACLE
, VIRTUALIZATION_XEN
)) {
354 r
= detect_vm_cpuid();
357 if (r
== VIRTUALIZATION_VM_OTHER
)
359 else if (r
!= VIRTUALIZATION_NONE
)
362 /* Now, let's get back to DMI */
365 if (dmi
== VIRTUALIZATION_VM_OTHER
)
367 else if (dmi
!= VIRTUALIZATION_NONE
) {
372 /* x86 xen will most likely be detected by cpuid. If not (most likely
373 * because we're not an x86 guest), then we should try the /proc/xen
374 * directory next. If that's not found, then we check for the high-level
375 * hypervisor sysfs file.
381 if (r
== VIRTUALIZATION_VM_OTHER
)
383 else if (r
!= VIRTUALIZATION_NONE
)
386 r
= detect_vm_hypervisor();
389 if (r
== VIRTUALIZATION_VM_OTHER
)
391 else if (r
!= VIRTUALIZATION_NONE
)
394 r
= detect_vm_device_tree();
397 if (r
== VIRTUALIZATION_VM_OTHER
)
399 else if (r
!= VIRTUALIZATION_NONE
)
405 if (r
== VIRTUALIZATION_VM_OTHER
)
407 else if (r
!= VIRTUALIZATION_NONE
)
415 /* x86 xen Dom0 is detected as XEN in hypervisor and maybe others.
416 * In order to detect the Dom0 as not virtualization we need to
418 if (r
== VIRTUALIZATION_XEN
) {
421 dom0
= detect_vm_xen_dom0();
425 r
= VIRTUALIZATION_NONE
;
426 } else if (r
== VIRTUALIZATION_NONE
&& other
)
427 r
= VIRTUALIZATION_VM_OTHER
;
430 log_debug("Found VM virtualization %s", virtualization_to_string(r
));
434 int detect_container(void) {
435 static const struct {
439 { "lxc", VIRTUALIZATION_LXC
},
440 { "lxc-libvirt", VIRTUALIZATION_LXC_LIBVIRT
},
441 { "systemd-nspawn", VIRTUALIZATION_SYSTEMD_NSPAWN
},
442 { "docker", VIRTUALIZATION_DOCKER
},
443 { "podman", VIRTUALIZATION_PODMAN
},
444 { "rkt", VIRTUALIZATION_RKT
},
445 { "wsl", VIRTUALIZATION_WSL
},
448 static thread_local
int cached_found
= _VIRTUALIZATION_INVALID
;
449 _cleanup_free_
char *m
= NULL
;
450 _cleanup_free_
char *o
= NULL
;
451 const char *e
= NULL
;
455 if (cached_found
>= 0)
458 /* /proc/vz exists in container and outside of the container, /proc/bc only outside of the container. */
459 if (access("/proc/vz", F_OK
) >= 0 &&
460 access("/proc/bc", F_OK
) < 0) {
461 r
= VIRTUALIZATION_OPENVZ
;
465 /* "Official" way of detecting WSL https://github.com/Microsoft/WSL/issues/423#issuecomment-221627364 */
466 r
= read_one_line_file("/proc/sys/kernel/osrelease", &o
);
468 if (strstr(o
, "Microsoft") || strstr(o
, "WSL")) {
469 r
= VIRTUALIZATION_WSL
;
474 if (getpid_cached() == 1) {
475 /* If we are PID 1 we can just check our own environment variable, and that's authoritative.
476 * We distinguish three cases:
477 * - the variable is not defined → we jump to other checks
478 * - the variable is defined to an empty value → we are not in a container
479 * - anything else → some container, either one of the known ones or "container-other"
481 e
= getenv("container");
485 r
= VIRTUALIZATION_NONE
;
492 /* Otherwise, PID 1 might have dropped this information into a file in /run. This is better than accessing
493 * /proc/1/environ, since we don't need CAP_SYS_PTRACE for that. */
494 r
= read_one_line_file("/run/systemd/container", &m
);
499 if (!IN_SET(r
, -ENOENT
, 0))
500 return log_debug_errno(r
, "Failed to read /run/systemd/container: %m");
502 /* Fallback for cases where PID 1 was not systemd (for example, cases where init=/bin/sh is used. */
503 r
= getenv_for_pid(1, "container", &m
);
508 if (r
< 0) /* This only works if we have CAP_SYS_PTRACE, hence let's better ignore failures here */
509 log_debug_errno(r
, "Failed to read $container of PID 1, ignoring: %m");
511 /* Interestingly /proc/1/sched actually shows the host's PID for what we see as PID 1. If the PID
512 * shown there is not 1, we know we are in a PID namespace and hence a container. */
514 r
= read_one_line_file("/proc/1/sched", &m
);
522 if (!startswith(t
, "(1,")) {
523 r
= VIRTUALIZATION_CONTAINER_OTHER
;
526 } else if (r
!= -ENOENT
)
529 /* If that didn't work, give up, assume no container manager. */
530 r
= VIRTUALIZATION_NONE
;
534 for (j
= 0; j
< ELEMENTSOF(value_table
); j
++)
535 if (streq(e
, value_table
[j
].value
)) {
536 r
= value_table
[j
].id
;
540 r
= VIRTUALIZATION_CONTAINER_OTHER
;
543 log_debug("Found container virtualization %s.", virtualization_to_string(r
));
548 int detect_virtualization(void) {
551 r
= detect_container();
558 static int userns_has_mapping(const char *name
) {
559 _cleanup_fclose_
FILE *f
= NULL
;
560 _cleanup_free_
char *buf
= NULL
;
561 size_t n_allocated
= 0;
566 f
= fopen(name
, "re");
568 log_debug_errno(errno
, "Failed to open %s: %m", name
);
569 return errno
== ENOENT
? false : -errno
;
572 n
= getline(&buf
, &n_allocated
, f
);
575 log_debug("%s is empty, we're in an uninitialized user namespace", name
);
579 return log_debug_errno(errno
, "Failed to read %s: %m", name
);
582 r
= sscanf(buf
, "%"PRIu32
" %"PRIu32
" %"PRIu32
, &a
, &b
, &c
);
584 return log_debug_errno(errno
, "Failed to parse %s: %m", name
);
586 if (a
== 0 && b
== 0 && c
== UINT32_MAX
) {
587 /* The kernel calls mappings_overlap() and does not allow overlaps */
588 log_debug("%s has a full 1:1 mapping", name
);
592 /* Anything else implies that we are in a user namespace */
593 log_debug("Mapping found in %s, we're in a user namespace", name
);
597 int running_in_userns(void) {
598 _cleanup_free_
char *line
= NULL
;
601 r
= userns_has_mapping("/proc/self/uid_map");
605 r
= userns_has_mapping("/proc/self/gid_map");
609 /* "setgroups" file was added in kernel v3.18-rc6-15-g9cc46516dd. It is also
610 * possible to compile a kernel without CONFIG_USER_NS, in which case "setgroups"
611 * also does not exist. We cannot distinguish those two cases, so assume that
612 * we're running on a stripped-down recent kernel, rather than on an old one,
613 * and if the file is not found, return false.
615 r
= read_one_line_file("/proc/self/setgroups", &line
);
617 log_debug_errno(r
, "/proc/self/setgroups: %m");
618 return r
== -ENOENT
? false : r
;
622 r
= streq(line
, "deny");
623 /* See user_namespaces(7) for a description of this "setgroups" contents. */
624 log_debug("/proc/self/setgroups contains \"%s\", %s user namespace", line
, r
? "in" : "not in");
628 int running_in_chroot(void) {
631 if (getenv_bool("SYSTEMD_IGNORE_CHROOT") > 0)
634 r
= files_same("/proc/1/root", "/", 0);
641 static const char *const virtualization_table
[_VIRTUALIZATION_MAX
] = {
642 [VIRTUALIZATION_NONE
] = "none",
643 [VIRTUALIZATION_KVM
] = "kvm",
644 [VIRTUALIZATION_QEMU
] = "qemu",
645 [VIRTUALIZATION_BOCHS
] = "bochs",
646 [VIRTUALIZATION_XEN
] = "xen",
647 [VIRTUALIZATION_UML
] = "uml",
648 [VIRTUALIZATION_VMWARE
] = "vmware",
649 [VIRTUALIZATION_ORACLE
] = "oracle",
650 [VIRTUALIZATION_MICROSOFT
] = "microsoft",
651 [VIRTUALIZATION_ZVM
] = "zvm",
652 [VIRTUALIZATION_PARALLELS
] = "parallels",
653 [VIRTUALIZATION_BHYVE
] = "bhyve",
654 [VIRTUALIZATION_QNX
] = "qnx",
655 [VIRTUALIZATION_ACRN
] = "acrn",
656 [VIRTUALIZATION_VM_OTHER
] = "vm-other",
658 [VIRTUALIZATION_SYSTEMD_NSPAWN
] = "systemd-nspawn",
659 [VIRTUALIZATION_LXC_LIBVIRT
] = "lxc-libvirt",
660 [VIRTUALIZATION_LXC
] = "lxc",
661 [VIRTUALIZATION_OPENVZ
] = "openvz",
662 [VIRTUALIZATION_DOCKER
] = "docker",
663 [VIRTUALIZATION_PODMAN
] = "podman",
664 [VIRTUALIZATION_RKT
] = "rkt",
665 [VIRTUALIZATION_WSL
] = "wsl",
666 [VIRTUALIZATION_CONTAINER_OTHER
] = "container-other",
669 DEFINE_STRING_TABLE_LOOKUP(virtualization
, int);