1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #if defined(__i386__) || defined(__x86_64__)
11 #include "alloc-util.h"
12 #include "dirent-util.h"
17 #include "process-util.h"
18 #include "stat-util.h"
19 #include "string-table.h"
20 #include "string-util.h"
23 #if defined(__i386__) || defined(__x86_64__)
24 static const char *const vm_table
[_VIRTUALIZATION_MAX
] = {
25 [VIRTUALIZATION_XEN
] = "XenVMMXenVMM",
26 [VIRTUALIZATION_KVM
] = "KVMKVMKVM",
27 [VIRTUALIZATION_QEMU
] = "TCGTCGTCGTCG",
28 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
29 [VIRTUALIZATION_VMWARE
] = "VMwareVMware",
30 /* https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfs */
31 [VIRTUALIZATION_MICROSOFT
] = "Microsoft Hv",
32 /* https://wiki.freebsd.org/bhyve */
33 [VIRTUALIZATION_BHYVE
] = "bhyve bhyve ",
34 [VIRTUALIZATION_QNX
] = "QNXQVMBSQG",
35 /* https://projectacrn.org */
36 [VIRTUALIZATION_ACRN
] = "ACRNACRNACRN",
39 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(vm
, int);
42 static int detect_vm_cpuid(void) {
44 /* CPUID is an x86 specific interface. */
45 #if defined(__i386__) || defined(__x86_64__)
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 v
= vm_from_string(sig
.text
);
76 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
;
121 else if (strstr(hvtype
, "vmware"))
122 return VIRTUALIZATION_VMWARE
;
124 return VIRTUALIZATION_VM_OTHER
;
126 log_debug("This platform does not support /proc/device-tree");
127 return VIRTUALIZATION_NONE
;
131 static int detect_vm_dmi(void) {
132 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
134 static const char *const dmi_vendors
[] = {
135 "/sys/class/dmi/id/product_name", /* Test this before sys_vendor to detect KVM over QEMU */
136 "/sys/class/dmi/id/sys_vendor",
137 "/sys/class/dmi/id/board_vendor",
138 "/sys/class/dmi/id/bios_vendor"
141 static const struct {
144 } dmi_vendor_table
[] = {
145 { "KVM", VIRTUALIZATION_KVM
},
146 { "QEMU", VIRTUALIZATION_QEMU
},
147 { "VMware", VIRTUALIZATION_VMWARE
}, /* https://kb.vmware.com/s/article/1009458 */
148 { "VMW", VIRTUALIZATION_VMWARE
},
149 { "innotek GmbH", VIRTUALIZATION_ORACLE
},
150 { "Oracle Corporation", VIRTUALIZATION_ORACLE
},
151 { "Xen", VIRTUALIZATION_XEN
},
152 { "Bochs", VIRTUALIZATION_BOCHS
},
153 { "Parallels", VIRTUALIZATION_PARALLELS
},
154 /* https://wiki.freebsd.org/bhyve */
155 { "BHYVE", VIRTUALIZATION_BHYVE
},
160 for (i
= 0; i
< ELEMENTSOF(dmi_vendors
); i
++) {
161 _cleanup_free_
char *s
= NULL
;
164 r
= read_one_line_file(dmi_vendors
[i
], &s
);
172 for (j
= 0; j
< ELEMENTSOF(dmi_vendor_table
); j
++)
173 if (startswith(s
, dmi_vendor_table
[j
].vendor
)) {
174 log_debug("Virtualization %s found in DMI (%s)", s
, dmi_vendors
[i
]);
175 return dmi_vendor_table
[j
].id
;
180 log_debug("No virtualization found in DMI");
182 return VIRTUALIZATION_NONE
;
185 static int detect_vm_xen(void) {
187 /* Check for Dom0 will be executed later in detect_vm_xen_dom0
188 The presence of /proc/xen indicates some form of a Xen domain */
189 if (access("/proc/xen", F_OK
) < 0) {
190 log_debug("Virtualization XEN not found, /proc/xen does not exist");
191 return VIRTUALIZATION_NONE
;
194 log_debug("Virtualization XEN found (/proc/xen exists)");
195 return VIRTUALIZATION_XEN
;
198 #define XENFEAT_dom0 11 /* xen/include/public/features.h */
199 #define PATH_FEATURES "/sys/hypervisor/properties/features"
200 /* Returns -errno, or 0 for domU, or 1 for dom0 */
201 static int detect_vm_xen_dom0(void) {
202 _cleanup_free_
char *domcap
= NULL
;
205 r
= read_one_line_file(PATH_FEATURES
, &domcap
);
206 if (r
< 0 && r
!= -ENOENT
)
209 unsigned long features
;
211 /* Here, we need to use sscanf() instead of safe_atoul()
212 * as the string lacks the leading "0x". */
213 r
= sscanf(domcap
, "%lx", &features
);
215 r
= !!(features
& (1U << XENFEAT_dom0
));
216 log_debug("Virtualization XEN, found %s with value %08lx, "
217 "XENFEAT_dom0 (indicating the 'hardware domain') is%s set.",
218 PATH_FEATURES
, features
, r
? "" : " not");
221 log_debug("Virtualization XEN, found %s, unhandled content '%s'",
222 PATH_FEATURES
, domcap
);
225 r
= read_one_line_file("/proc/xen/capabilities", &domcap
);
227 log_debug("Virtualization XEN because /proc/xen/capabilities does not exist");
233 for (const char *i
= domcap
;;) {
234 _cleanup_free_
char *cap
= NULL
;
236 r
= extract_first_word(&i
, &cap
, ",", 0);
240 log_debug("Virtualization XEN DomU found (/proc/xen/capabilities)");
244 if (streq(cap
, "control_d")) {
245 log_debug("Virtualization XEN Dom0 ignored (/proc/xen/capabilities)");
251 static int detect_vm_hypervisor(void) {
252 _cleanup_free_
char *hvtype
= NULL
;
255 r
= read_one_line_file("/sys/hypervisor/type", &hvtype
);
257 return VIRTUALIZATION_NONE
;
261 log_debug("Virtualization %s found in /sys/hypervisor/type", hvtype
);
263 if (streq(hvtype
, "xen"))
264 return VIRTUALIZATION_XEN
;
266 return VIRTUALIZATION_VM_OTHER
;
269 static int detect_vm_uml(void) {
270 _cleanup_fclose_
FILE *f
= NULL
;
273 /* Detect User-Mode Linux by reading /proc/cpuinfo */
274 f
= fopen("/proc/cpuinfo", "re");
276 if (errno
== ENOENT
) {
277 log_debug("/proc/cpuinfo not found, assuming no UML virtualization.");
278 return VIRTUALIZATION_NONE
;
284 _cleanup_free_
char *line
= NULL
;
287 r
= read_line(f
, LONG_LINE_MAX
, &line
);
293 t
= startswith(line
, "vendor_id\t: ");
295 if (startswith(t
, "User Mode Linux")) {
296 log_debug("UML virtualization found in /proc/cpuinfo");
297 return VIRTUALIZATION_UML
;
304 log_debug("UML virtualization not found in /proc/cpuinfo.");
305 return VIRTUALIZATION_NONE
;
308 static int detect_vm_zvm(void) {
310 #if defined(__s390__)
311 _cleanup_free_
char *t
= NULL
;
314 r
= get_proc_field("/proc/sysinfo", "VM00 Control Program", WHITESPACE
, &t
);
316 return VIRTUALIZATION_NONE
;
320 log_debug("Virtualization %s found in /proc/sysinfo", t
);
321 if (streq(t
, "z/VM"))
322 return VIRTUALIZATION_ZVM
;
324 return VIRTUALIZATION_KVM
;
326 log_debug("This platform does not support /proc/sysinfo");
327 return VIRTUALIZATION_NONE
;
331 /* Returns a short identifier for the various VM implementations */
332 int detect_vm(void) {
333 static thread_local
int cached_found
= _VIRTUALIZATION_INVALID
;
337 if (cached_found
>= 0)
340 /* We have to use the correct order here:
342 * → First, try to detect Oracle Virtualbox, even if it uses KVM, as well as Xen even if it cloaks as Microsoft
345 * → Second, try to detect from CPUID, this will report KVM for whatever software is used even if info in DMI is
348 * → Third, try to detect from DMI. */
350 dmi
= detect_vm_dmi();
351 if (IN_SET(dmi
, VIRTUALIZATION_ORACLE
, VIRTUALIZATION_XEN
)) {
356 r
= detect_vm_cpuid();
359 if (r
== VIRTUALIZATION_VM_OTHER
)
361 else if (r
!= VIRTUALIZATION_NONE
)
364 /* Now, let's get back to DMI */
367 if (dmi
== VIRTUALIZATION_VM_OTHER
)
369 else if (dmi
!= VIRTUALIZATION_NONE
) {
374 /* x86 xen will most likely be detected by cpuid. If not (most likely
375 * because we're not an x86 guest), then we should try the /proc/xen
376 * directory next. If that's not found, then we check for the high-level
377 * hypervisor sysfs file.
383 if (r
== VIRTUALIZATION_VM_OTHER
)
385 else if (r
!= VIRTUALIZATION_NONE
)
388 r
= detect_vm_hypervisor();
391 if (r
== VIRTUALIZATION_VM_OTHER
)
393 else if (r
!= VIRTUALIZATION_NONE
)
396 r
= detect_vm_device_tree();
399 if (r
== VIRTUALIZATION_VM_OTHER
)
401 else if (r
!= VIRTUALIZATION_NONE
)
407 if (r
== VIRTUALIZATION_VM_OTHER
)
409 else if (r
!= VIRTUALIZATION_NONE
)
417 /* x86 xen Dom0 is detected as XEN in hypervisor and maybe others.
418 * In order to detect the Dom0 as not virtualization we need to
420 if (r
== VIRTUALIZATION_XEN
) {
423 dom0
= detect_vm_xen_dom0();
427 r
= VIRTUALIZATION_NONE
;
428 } else if (r
== VIRTUALIZATION_NONE
&& other
)
429 r
= VIRTUALIZATION_VM_OTHER
;
432 log_debug("Found VM virtualization %s", virtualization_to_string(r
));
436 static const char *const container_table
[_VIRTUALIZATION_MAX
] = {
437 [VIRTUALIZATION_LXC
] = "lxc",
438 [VIRTUALIZATION_LXC_LIBVIRT
] = "lxc-libvirt",
439 [VIRTUALIZATION_SYSTEMD_NSPAWN
] = "systemd-nspawn",
440 [VIRTUALIZATION_DOCKER
] = "docker",
441 [VIRTUALIZATION_PODMAN
] = "podman",
442 [VIRTUALIZATION_RKT
] = "rkt",
443 [VIRTUALIZATION_WSL
] = "wsl",
446 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(container
, int);
448 int detect_container(void) {
449 static thread_local
int cached_found
= _VIRTUALIZATION_INVALID
;
450 _cleanup_free_
char *m
= NULL
;
451 _cleanup_free_
char *o
= NULL
;
452 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 * ... and a working one, since the official one doesn't actually work ;(
468 r
= read_one_line_file("/proc/sys/kernel/osrelease", &o
);
470 (strstr(o
, "Microsoft") || strstr(o
, "microsoft") || strstr(o
, "WSL"))) {
471 r
= VIRTUALIZATION_WSL
;
475 if (getpid_cached() == 1) {
476 /* If we are PID 1 we can just check our own environment variable, and that's authoritative.
477 * We distinguish three cases:
478 * - the variable is not defined → we jump to other checks
479 * - the variable is defined to an empty value → we are not in a container
480 * - anything else → some container, either one of the known ones or "container-other"
482 e
= getenv("container");
486 r
= VIRTUALIZATION_NONE
;
493 /* Otherwise, PID 1 might have dropped this information into a file in /run. This is better than accessing
494 * /proc/1/environ, since we don't need CAP_SYS_PTRACE for that. */
495 r
= read_one_line_file("/run/systemd/container", &m
);
500 if (!IN_SET(r
, -ENOENT
, 0))
501 return log_debug_errno(r
, "Failed to read /run/systemd/container: %m");
503 /* Fallback for cases where PID 1 was not systemd (for example, cases where init=/bin/sh is used. */
504 r
= getenv_for_pid(1, "container", &m
);
509 if (r
< 0) /* This only works if we have CAP_SYS_PTRACE, hence let's better ignore failures here */
510 log_debug_errno(r
, "Failed to read $container of PID 1, ignoring: %m");
512 /* Interestingly /proc/1/sched actually shows the host's PID for what we see as PID 1. If the PID
513 * shown there is not 1, we know we are in a PID namespace and hence a container. */
515 r
= read_one_line_file("/proc/1/sched", &m
);
523 if (!startswith(t
, "(1,")) {
524 r
= VIRTUALIZATION_CONTAINER_OTHER
;
527 } else if (r
!= -ENOENT
)
530 /* If that didn't work, give up, assume no container manager. */
531 r
= VIRTUALIZATION_NONE
;
535 r
= container_from_string(e
);
537 r
= VIRTUALIZATION_CONTAINER_OTHER
;
540 log_debug("Found container virtualization %s.", virtualization_to_string(r
));
545 int detect_virtualization(void) {
548 r
= detect_container();
555 static int userns_has_mapping(const char *name
) {
556 _cleanup_fclose_
FILE *f
= NULL
;
557 _cleanup_free_
char *buf
= NULL
;
558 size_t n_allocated
= 0;
563 f
= fopen(name
, "re");
565 log_debug_errno(errno
, "Failed to open %s: %m", name
);
566 return errno
== ENOENT
? false : -errno
;
569 n
= getline(&buf
, &n_allocated
, f
);
572 log_debug("%s is empty, we're in an uninitialized user namespace", name
);
576 return log_debug_errno(errno
, "Failed to read %s: %m", name
);
579 r
= sscanf(buf
, "%"PRIu32
" %"PRIu32
" %"PRIu32
, &a
, &b
, &c
);
581 return log_debug_errno(errno
, "Failed to parse %s: %m", name
);
583 if (a
== 0 && b
== 0 && c
== UINT32_MAX
) {
584 /* The kernel calls mappings_overlap() and does not allow overlaps */
585 log_debug("%s has a full 1:1 mapping", name
);
589 /* Anything else implies that we are in a user namespace */
590 log_debug("Mapping found in %s, we're in a user namespace", name
);
594 int running_in_userns(void) {
595 _cleanup_free_
char *line
= NULL
;
598 r
= userns_has_mapping("/proc/self/uid_map");
602 r
= userns_has_mapping("/proc/self/gid_map");
606 /* "setgroups" file was added in kernel v3.18-rc6-15-g9cc46516dd. It is also
607 * possible to compile a kernel without CONFIG_USER_NS, in which case "setgroups"
608 * also does not exist. We cannot distinguish those two cases, so assume that
609 * we're running on a stripped-down recent kernel, rather than on an old one,
610 * and if the file is not found, return false.
612 r
= read_one_line_file("/proc/self/setgroups", &line
);
614 log_debug_errno(r
, "/proc/self/setgroups: %m");
615 return r
== -ENOENT
? false : r
;
619 r
= streq(line
, "deny");
620 /* See user_namespaces(7) for a description of this "setgroups" contents. */
621 log_debug("/proc/self/setgroups contains \"%s\", %s user namespace", line
, r
? "in" : "not in");
625 int running_in_chroot(void) {
628 if (getenv_bool("SYSTEMD_IGNORE_CHROOT") > 0)
631 r
= files_same("/proc/1/root", "/", 0);
638 static const char *const virtualization_table
[_VIRTUALIZATION_MAX
] = {
639 [VIRTUALIZATION_NONE
] = "none",
640 [VIRTUALIZATION_KVM
] = "kvm",
641 [VIRTUALIZATION_QEMU
] = "qemu",
642 [VIRTUALIZATION_BOCHS
] = "bochs",
643 [VIRTUALIZATION_XEN
] = "xen",
644 [VIRTUALIZATION_UML
] = "uml",
645 [VIRTUALIZATION_VMWARE
] = "vmware",
646 [VIRTUALIZATION_ORACLE
] = "oracle",
647 [VIRTUALIZATION_MICROSOFT
] = "microsoft",
648 [VIRTUALIZATION_ZVM
] = "zvm",
649 [VIRTUALIZATION_PARALLELS
] = "parallels",
650 [VIRTUALIZATION_BHYVE
] = "bhyve",
651 [VIRTUALIZATION_QNX
] = "qnx",
652 [VIRTUALIZATION_ACRN
] = "acrn",
653 [VIRTUALIZATION_VM_OTHER
] = "vm-other",
655 [VIRTUALIZATION_SYSTEMD_NSPAWN
] = "systemd-nspawn",
656 [VIRTUALIZATION_LXC_LIBVIRT
] = "lxc-libvirt",
657 [VIRTUALIZATION_LXC
] = "lxc",
658 [VIRTUALIZATION_OPENVZ
] = "openvz",
659 [VIRTUALIZATION_DOCKER
] = "docker",
660 [VIRTUALIZATION_PODMAN
] = "podman",
661 [VIRTUALIZATION_RKT
] = "rkt",
662 [VIRTUALIZATION_WSL
] = "wsl",
663 [VIRTUALIZATION_CONTAINER_OTHER
] = "container-other",
666 DEFINE_STRING_TABLE_LOOKUP(virtualization
, int);