1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #if defined(__i386__) || defined(__x86_64__)
10 #include "alloc-util.h"
11 #include "dirent-util.h"
13 #include "extract-word.h"
17 #include "namespace-util.h"
18 #include "parse-util.h"
20 #include "process-util.h"
21 #include "string-table.h"
22 #include "string-util.h"
29 SMBIOS_VM_BIT_UNKNOWN
,
32 static Virtualization
detect_vm_cpuid(void) {
34 /* CPUID is an x86 specific interface. */
35 #if defined(__i386__) || defined(__x86_64__)
41 { "XenVMMXenVMM", VIRTUALIZATION_XEN
},
42 { "KVMKVMKVM", VIRTUALIZATION_KVM
}, /* qemu with KVM */
43 { "Linux KVM Hv", VIRTUALIZATION_KVM
}, /* qemu with KVM + HyperV Enlightenments */
44 { "TCGTCGTCGTCG", VIRTUALIZATION_QEMU
}, /* qemu without KVM */
45 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
46 { "VMwareVMware", VIRTUALIZATION_VMWARE
},
47 /* https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfs */
48 { "Microsoft Hv", VIRTUALIZATION_MICROSOFT
},
49 /* https://wiki.freebsd.org/bhyve */
50 { "bhyve bhyve ", VIRTUALIZATION_BHYVE
},
51 { "QNXQVMBSQG", VIRTUALIZATION_QNX
},
52 /* https://projectacrn.org */
53 { "ACRNACRNACRN", VIRTUALIZATION_ACRN
},
54 /* https://www.lockheedmartin.com/en-us/products/Hardened-Security-for-Intel-Processors.html */
55 { "SRESRESRESRE", VIRTUALIZATION_SRE
},
56 { "Apple VZ", VIRTUALIZATION_APPLE
},
59 uint32_t eax
, ebx
, ecx
, edx
;
62 /* http://lwn.net/Articles/301888/ */
64 /* First detect whether there is a hypervisor */
65 if (__get_cpuid(1, &eax
, &ebx
, &ecx
, &edx
) == 0)
66 return VIRTUALIZATION_NONE
;
68 hypervisor
= ecx
& 0x80000000U
;
76 /* There is a hypervisor, see what it is */
77 __cpuid(0x40000000U
, eax
, ebx
, ecx
, edx
);
83 log_debug("Virtualization found, CPUID=%s", sig
.text
);
85 FOREACH_ELEMENT(vm
, vm_table
)
86 if (memcmp_nn(sig
.text
, sizeof(sig
.text
),
87 vm
->sig
, sizeof(vm
->sig
)) == 0)
90 log_debug("Unknown virtualization with CPUID=%s. Add to vm_table[]?", sig
.text
);
91 return VIRTUALIZATION_VM_OTHER
;
94 log_debug("No virtualization found in CPUID");
96 return VIRTUALIZATION_NONE
;
99 static Virtualization
detect_vm_device_tree(void) {
100 #if defined(__arm__) || defined(__aarch64__) || defined(__powerpc__) || defined(__powerpc64__) || defined(__riscv)
101 _cleanup_free_
char *hvtype
= NULL
;
104 r
= read_one_line_file("/proc/device-tree/hypervisor/compatible", &hvtype
);
106 if (access("/proc/device-tree/ibm,partition-name", F_OK
) == 0 &&
107 access("/proc/device-tree/hmc-managed?", F_OK
) == 0 &&
108 access("/proc/device-tree/chosen/qemu,graphic-width", F_OK
) != 0)
109 return VIRTUALIZATION_POWERVM
;
111 _cleanup_closedir_
DIR *dir
= opendir("/proc/device-tree");
113 if (errno
== ENOENT
) {
114 log_debug_errno(errno
, "/proc/device-tree/ does not exist");
115 return VIRTUALIZATION_NONE
;
117 return log_debug_errno(errno
, "Opening /proc/device-tree/ failed: %m");
120 FOREACH_DIRENT(de
, dir
, return log_debug_errno(errno
, "Failed to enumerate /proc/device-tree/ contents: %m"))
121 if (strstr(de
->d_name
, "fw-cfg")) {
122 log_debug("Virtualization QEMU: \"fw-cfg\" present in /proc/device-tree/%s", de
->d_name
);
123 return VIRTUALIZATION_QEMU
;
126 _cleanup_free_
char *compat
= NULL
;
127 r
= read_one_line_file("/proc/device-tree/compatible", &compat
);
128 if (r
< 0 && r
!= -ENOENT
)
129 return log_debug_errno(r
, "Failed to read /proc/device-tree/compatible: %m");
131 if (streq(compat
, "qemu,pseries")) {
132 log_debug("Virtualization %s found in /proc/device-tree/compatible", compat
);
133 return VIRTUALIZATION_QEMU
;
135 if (streq(compat
, "linux,dummy-virt")) {
136 /* https://www.kernel.org/doc/Documentation/devicetree/bindings/arm/linux%2Cdummy-virt.yaml */
137 log_debug("Generic virtualization %s found in /proc/device-tree/compatible", compat
);
138 return VIRTUALIZATION_VM_OTHER
;
142 log_debug("No virtualization found in /proc/device-tree/*");
143 return VIRTUALIZATION_NONE
;
147 log_debug("Virtualization %s found in /proc/device-tree/hypervisor/compatible", hvtype
);
148 if (streq(hvtype
, "linux,kvm"))
149 return VIRTUALIZATION_KVM
;
150 else if (strstr(hvtype
, "xen"))
151 return VIRTUALIZATION_XEN
;
152 else if (strstr(hvtype
, "vmware"))
153 return VIRTUALIZATION_VMWARE
;
155 return VIRTUALIZATION_VM_OTHER
;
157 log_debug("This platform does not support /proc/device-tree");
158 return VIRTUALIZATION_NONE
;
162 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__) || defined(__loongarch_lp64) || defined(__riscv)
163 static Virtualization
detect_vm_dmi_vendor(void) {
164 static const char* const dmi_vendors
[] = {
165 "/sys/class/dmi/id/product_name", /* Test this before sys_vendor to detect KVM over QEMU */
166 "/sys/class/dmi/id/sys_vendor",
167 "/sys/class/dmi/id/board_vendor",
168 "/sys/class/dmi/id/bios_vendor",
169 "/sys/class/dmi/id/product_version", /* For Hyper-V VMs test */
173 static const struct {
176 } dmi_vendor_table
[] = {
177 { "KVM", VIRTUALIZATION_KVM
},
178 { "OpenStack", VIRTUALIZATION_KVM
}, /* Detect OpenStack instance as KVM in non x86 architecture */
179 { "KubeVirt", VIRTUALIZATION_KVM
}, /* Detect KubeVirt instance as KVM in non x86 architecture */
180 { "Amazon EC2", VIRTUALIZATION_AMAZON
},
181 { "QEMU", VIRTUALIZATION_QEMU
},
182 { "VMware", VIRTUALIZATION_VMWARE
}, /* https://kb.vmware.com/s/article/1009458 */
183 { "VMW", VIRTUALIZATION_VMWARE
},
184 { "innotek GmbH", VIRTUALIZATION_ORACLE
},
185 { "VirtualBox", VIRTUALIZATION_ORACLE
},
186 { "Oracle Corporation", VIRTUALIZATION_ORACLE
}, /* Detect VirtualBox on some proprietary systems via the board_vendor */
187 { "Xen", VIRTUALIZATION_XEN
},
188 { "Bochs", VIRTUALIZATION_BOCHS
},
189 { "Parallels", VIRTUALIZATION_PARALLELS
},
190 /* https://wiki.freebsd.org/bhyve */
191 { "BHYVE", VIRTUALIZATION_BHYVE
},
192 { "Hyper-V", VIRTUALIZATION_MICROSOFT
},
193 { "Apple Virtualization", VIRTUALIZATION_APPLE
},
194 { "Google Compute Engine", VIRTUALIZATION_GOOGLE
}, /* https://cloud.google.com/run/docs/container-contract#sandbox */
198 STRV_FOREACH(vendor
, dmi_vendors
) {
199 _cleanup_free_
char *s
= NULL
;
201 r
= read_one_line_file(*vendor
, &s
);
209 FOREACH_ELEMENT(dmi_vendor
, dmi_vendor_table
)
210 if (startswith(s
, dmi_vendor
->vendor
)) {
211 log_debug("Virtualization %s found in DMI (%s)", s
, *vendor
);
212 return dmi_vendor
->id
;
215 log_debug("No virtualization found in DMI vendor table.");
216 return VIRTUALIZATION_NONE
;
219 static int detect_vm_smbios(void) {
220 /* The SMBIOS BIOS Characteristics Extension Byte 2 (Section 2.1.2.2 of
221 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.4.0.pdf), specifies that
222 * the 4th bit being set indicates a VM. The BIOS Characteristics table is exposed via the kernel in
223 * /sys/firmware/dmi/entries/0-0. Note that in the general case, this bit being unset should not
224 * imply that the system is running on bare-metal. For example, QEMU 3.1.0 (with or without KVM)
225 * with SeaBIOS does not set this bit. */
226 _cleanup_free_
char *s
= NULL
;
230 r
= read_full_virtual_file("/sys/firmware/dmi/entries/0-0/raw", &s
, &readsize
);
232 log_debug_errno(r
, "Unable to read /sys/firmware/dmi/entries/0-0/raw, "
233 "using the virtualization information found in DMI vendor table, ignoring: %m");
234 return SMBIOS_VM_BIT_UNKNOWN
;
236 if (readsize
< 20 || s
[1] < 20) {
237 /* The spec indicates that byte 1 contains the size of the table, 0x12 + the number of
238 * extension bytes. The data we're interested in is in extension byte 2, which would be at
239 * 0x13. If we didn't read that much data, or if the BIOS indicates that we don't have that
240 * much data, we don't infer anything from the SMBIOS. */
241 log_debug("Only read %zu bytes from /sys/firmware/dmi/entries/0-0/raw (expected 20). "
242 "Using the virtualization information found in DMI vendor table.", readsize
);
243 return SMBIOS_VM_BIT_UNKNOWN
;
246 uint8_t byte
= (uint8_t) s
[19];
247 if (byte
& (1U<<4)) {
248 log_debug("DMI BIOS Extension table indicates virtualization.");
249 return SMBIOS_VM_BIT_SET
;
251 log_debug("DMI BIOS Extension table does not indicate virtualization.");
252 return SMBIOS_VM_BIT_UNSET
;
254 #endif /* defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__) || defined(__loongarch_lp64) */
256 static Virtualization
detect_vm_dmi(void) {
257 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__) || defined(__loongarch_lp64)
260 r
= detect_vm_dmi_vendor();
262 /* The DMI vendor tables in /sys/class/dmi/id don't help us distinguish between Amazon EC2
263 * virtual machines and bare-metal instances, so we need to look at SMBIOS. */
264 if (r
== VIRTUALIZATION_AMAZON
) {
265 switch (detect_vm_smbios()) {
266 case SMBIOS_VM_BIT_SET
:
267 return VIRTUALIZATION_AMAZON
;
268 case SMBIOS_VM_BIT_UNSET
:
269 return VIRTUALIZATION_NONE
;
270 case SMBIOS_VM_BIT_UNKNOWN
: {
271 /* The DMI information we are after is only accessible to the root user,
272 * so we fallback to using the product name which is less restricted
273 * to distinguish metal systems from virtualized instances */
274 _cleanup_free_
char *s
= NULL
;
277 r
= read_full_virtual_file("/sys/class/dmi/id/product_name", &s
, NULL
);
278 /* In EC2, virtualized is much more common than metal, so if for some reason
279 * we fail to read the DMI data, assume we are virtualized. */
281 log_debug_errno(r
, "Can't read /sys/class/dmi/id/product_name,"
282 " assuming virtualized: %m");
283 return VIRTUALIZATION_AMAZON
;
285 e
= strstrafter(truncate_nl(s
), ".metal");
286 if (e
&& IN_SET(*e
, 0, '-')) {
287 log_debug("DMI product name has '.metal', assuming no virtualization");
288 return VIRTUALIZATION_NONE
;
290 return VIRTUALIZATION_AMAZON
;
293 assert_not_reached();
297 /* If we haven't identified a VM, but the firmware indicates that there is one, indicate as much. We
298 * have no further information about what it is. */
299 if (r
== VIRTUALIZATION_NONE
&& detect_vm_smbios() == SMBIOS_VM_BIT_SET
)
300 return VIRTUALIZATION_VM_OTHER
;
303 return VIRTUALIZATION_NONE
;
307 #define XENFEAT_dom0 11 /* xen/include/public/features.h */
308 #define PATH_FEATURES "/sys/hypervisor/properties/features"
309 /* Returns -errno, or 0 for domU, or 1 for dom0 */
310 static int detect_vm_xen_dom0(void) {
311 _cleanup_free_
char *domcap
= NULL
;
314 r
= read_one_line_file(PATH_FEATURES
, &domcap
);
315 if (r
< 0 && r
!= -ENOENT
)
318 unsigned long features
;
320 /* Here, we need to use sscanf() instead of safe_atoul()
321 * as the string lacks the leading "0x". */
322 r
= sscanf(domcap
, "%lx", &features
);
324 r
= !!(features
& (1U << XENFEAT_dom0
));
325 log_debug("Virtualization XEN, found %s with value %08lx, "
326 "XENFEAT_dom0 (indicating the 'hardware domain') is%s set.",
327 PATH_FEATURES
, features
, r
? "" : " not");
330 log_debug("Virtualization XEN, found %s, unhandled content '%s'",
331 PATH_FEATURES
, domcap
);
334 r
= read_one_line_file("/proc/xen/capabilities", &domcap
);
336 log_debug("Virtualization XEN because /proc/xen/capabilities does not exist");
342 for (const char *i
= domcap
;;) {
343 _cleanup_free_
char *cap
= NULL
;
345 r
= extract_first_word(&i
, &cap
, ",", 0);
349 log_debug("Virtualization XEN DomU found (/proc/xen/capabilities)");
353 if (streq(cap
, "control_d")) {
354 log_debug("Virtualization XEN Dom0 ignored (/proc/xen/capabilities)");
360 static Virtualization
detect_vm_xen(void) {
361 /* The presence of /proc/xen indicates some form of a Xen domain
362 The check for Dom0 is handled outside this function */
363 if (access("/proc/xen", F_OK
) < 0) {
364 log_debug("Virtualization XEN not found, /proc/xen does not exist");
365 return VIRTUALIZATION_NONE
;
367 log_debug("Virtualization XEN found (/proc/xen exists)");
368 return VIRTUALIZATION_XEN
;
371 static Virtualization
detect_vm_hypervisor(void) {
372 _cleanup_free_
char *hvtype
= NULL
;
375 r
= read_one_line_file("/sys/hypervisor/type", &hvtype
);
377 return VIRTUALIZATION_NONE
;
381 log_debug("Virtualization %s found in /sys/hypervisor/type", hvtype
);
383 if (streq(hvtype
, "xen"))
384 return VIRTUALIZATION_XEN
;
386 return VIRTUALIZATION_VM_OTHER
;
389 static Virtualization
detect_vm_uml(void) {
390 _cleanup_fclose_
FILE *f
= NULL
;
393 /* Detect User-Mode Linux by reading /proc/cpuinfo */
394 f
= fopen("/proc/cpuinfo", "re");
396 if (errno
== ENOENT
) {
397 log_debug("/proc/cpuinfo not found, assuming no UML virtualization.");
398 return VIRTUALIZATION_NONE
;
404 _cleanup_free_
char *line
= NULL
;
407 r
= read_line(f
, LONG_LINE_MAX
, &line
);
413 t
= startswith(line
, "vendor_id\t: ");
415 if (startswith(t
, "User Mode Linux")) {
416 log_debug("UML virtualization found in /proc/cpuinfo");
417 return VIRTUALIZATION_UML
;
424 log_debug("UML virtualization not found in /proc/cpuinfo.");
425 return VIRTUALIZATION_NONE
;
428 static Virtualization
detect_vm_zvm(void) {
430 #if defined(__s390__)
431 _cleanup_free_
char *t
= NULL
;
434 r
= get_proc_field("/proc/sysinfo", "VM00 Control Program", &t
);
435 if (IN_SET(r
, -ENOENT
, -ENODATA
))
436 return VIRTUALIZATION_NONE
;
440 log_debug("Virtualization %s found in /proc/sysinfo", t
);
441 if (streq(t
, "z/VM"))
442 return VIRTUALIZATION_ZVM
;
444 return VIRTUALIZATION_KVM
;
446 log_debug("This platform does not support /proc/sysinfo");
447 return VIRTUALIZATION_NONE
;
451 /* Returns a short identifier for the various VM implementations */
452 Virtualization
detect_vm(void) {
453 static thread_local Virtualization cached_found
= _VIRTUALIZATION_INVALID
;
454 bool other
= false, hyperv
= false;
456 Virtualization v
, dmi
;
458 if (cached_found
>= 0)
461 /* We have to use the correct order here:
463 * → First, try to detect Oracle Virtualbox, Amazon EC2 Nitro, Parallels, and Google Compute Engine,
464 * even if they use KVM, as well as Xen, even if it cloaks as Microsoft Hyper-V. Attempt to detect
465 * UML at this stage too, since it runs as a user-process nested inside other VMs. Also check for
466 * Xen now, because Xen PV mode does not override CPUID when nested inside another hypervisor.
468 * → Second, try to detect from CPUID. This will report KVM for whatever software is used even if
469 * info in DMI is overwritten.
471 * → Third, try to detect from DMI. */
473 dmi
= detect_vm_dmi();
475 VIRTUALIZATION_ORACLE
,
477 VIRTUALIZATION_AMAZON
,
478 VIRTUALIZATION_PARALLELS
,
479 VIRTUALIZATION_GOOGLE
)) {
488 if (v
!= VIRTUALIZATION_NONE
)
495 if (v
== VIRTUALIZATION_XEN
) {
496 /* If we are Dom0, then we expect to not report as a VM. However, as we might be nested
497 * inside another hypervisor which can be detected via the CPUID check, wait to report this
498 * until after the CPUID check. */
499 xen_dom0
= detect_vm_xen_dom0();
504 } else if (v
!= VIRTUALIZATION_NONE
)
505 assert_not_reached();
507 /* Detect from CPUID */
508 v
= detect_vm_cpuid();
511 if (v
== VIRTUALIZATION_MICROSOFT
)
512 /* QEMU sets the CPUID string to hyperv's, in case it provides hyperv enlightenments. Let's
513 * hence not return Microsoft here but just use the other mechanisms first to make a better
516 else if (v
== VIRTUALIZATION_VM_OTHER
)
518 else if (v
!= VIRTUALIZATION_NONE
)
521 /* If we are in Dom0 and have not yet finished, finish with the result of detect_vm_cpuid */
525 /* Now, let's get back to DMI */
528 if (dmi
== VIRTUALIZATION_VM_OTHER
)
530 else if (dmi
!= VIRTUALIZATION_NONE
) {
535 /* Check high-level hypervisor sysfs file */
536 v
= detect_vm_hypervisor();
539 if (v
== VIRTUALIZATION_VM_OTHER
)
541 else if (v
!= VIRTUALIZATION_NONE
)
544 v
= detect_vm_device_tree();
547 if (v
== VIRTUALIZATION_VM_OTHER
)
549 else if (v
!= VIRTUALIZATION_NONE
)
557 /* None of the checks above gave us a clear answer, hence let's now use fallback logic: if hyperv
558 * enlightenments are available but the VMM wasn't recognized as anything yet, it's probably
560 if (v
== VIRTUALIZATION_NONE
) {
562 v
= VIRTUALIZATION_MICROSOFT
;
564 v
= VIRTUALIZATION_VM_OTHER
;
568 log_debug("Found VM virtualization %s", virtualization_to_string(v
));
572 static const char *const container_table
[_VIRTUALIZATION_MAX
] = {
573 [VIRTUALIZATION_LXC
] = "lxc",
574 [VIRTUALIZATION_LXC_LIBVIRT
] = "lxc-libvirt",
575 [VIRTUALIZATION_SYSTEMD_NSPAWN
] = "systemd-nspawn",
576 [VIRTUALIZATION_DOCKER
] = "docker",
577 [VIRTUALIZATION_PODMAN
] = "podman",
578 [VIRTUALIZATION_RKT
] = "rkt",
579 [VIRTUALIZATION_WSL
] = "wsl",
580 [VIRTUALIZATION_PROOT
] = "proot",
581 [VIRTUALIZATION_POUCH
] = "pouch",
584 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(container
, int);
586 static int running_in_pidns(void) {
589 r
= namespace_is_init(NAMESPACE_PID
);
591 return log_debug_errno(r
, "Failed to test if in root PID namespace, ignoring: %m");
596 static Virtualization
detect_container_files(void) {
597 static const struct {
598 const char *file_path
;
600 } container_file_table
[] = {
601 /* https://github.com/containers/podman/issues/6192 */
602 /* https://github.com/containers/podman/issues/3586#issuecomment-661918679 */
603 { "/run/.containerenv", VIRTUALIZATION_PODMAN
},
604 /* https://github.com/moby/moby/issues/18355 */
605 /* Docker must be the last in this table, see below. */
606 { "/.dockerenv", VIRTUALIZATION_DOCKER
},
609 FOREACH_ELEMENT(file
, container_file_table
) {
610 if (access(file
->file_path
, F_OK
) >= 0)
614 log_debug_errno(errno
,
615 "Checking if %s exists failed, ignoring: %m",
619 return VIRTUALIZATION_NONE
;
622 Virtualization
detect_container(void) {
623 static thread_local Virtualization cached_found
= _VIRTUALIZATION_INVALID
;
624 _cleanup_free_
char *m
= NULL
, *o
= NULL
, *p
= NULL
;
625 const char *e
= NULL
;
629 if (cached_found
>= 0)
632 /* /proc/vz exists in container and outside of the container, /proc/bc only outside of the container. */
633 if (access("/proc/vz", F_OK
) < 0) {
635 log_debug_errno(errno
, "Failed to check if /proc/vz exists, ignoring: %m");
636 } else if (access("/proc/bc", F_OK
) < 0) {
637 if (errno
== ENOENT
) {
638 v
= VIRTUALIZATION_OPENVZ
;
642 log_debug_errno(errno
, "Failed to check if /proc/bc exists, ignoring: %m");
645 /* "Official" way of detecting WSL https://github.com/Microsoft/WSL/issues/423#issuecomment-221627364 */
646 r
= read_one_line_file("/proc/sys/kernel/osrelease", &o
);
648 log_debug_errno(r
, "Failed to read /proc/sys/kernel/osrelease, ignoring: %m");
649 else if (strstr(o
, "Microsoft") || strstr(o
, "WSL")) {
650 v
= VIRTUALIZATION_WSL
;
654 /* proot doesn't use PID namespacing, so we can just check if we have a matching tracer for this
655 * invocation without worrying about it being elsewhere.
657 r
= get_proc_field("/proc/self/status", "TracerPid", &p
);
659 log_debug_errno(r
, "Failed to read our own trace PID, ignoring: %m");
660 else if (!streq(p
, "0")) {
663 r
= parse_pid(p
, &ptrace_pid
);
665 log_debug_errno(r
, "Failed to parse our own tracer PID, ignoring: %m");
667 _cleanup_free_
char *ptrace_comm
= NULL
;
670 pf
= procfs_file_alloca(ptrace_pid
, "comm");
671 r
= read_one_line_file(pf
, &ptrace_comm
);
673 log_debug_errno(r
, "Failed to read %s, ignoring: %m", pf
);
674 else if (startswith(ptrace_comm
, "proot")) {
675 v
= VIRTUALIZATION_PROOT
;
681 /* The container manager might have placed this in the /run/host/ hierarchy for us, which is best
682 * because we can be consumed just like that, without special privileges. */
683 r
= read_one_line_file("/run/host/container-manager", &m
);
688 if (!IN_SET(r
, -ENOENT
, 0))
689 return log_debug_errno(r
, "Failed to read /run/host/container-manager: %m");
691 if (getpid_cached() == 1) {
692 /* If we are PID 1 we can just check our own environment variable, and that's authoritative.
693 * We distinguish three cases:
694 * - the variable is not defined → we jump to other checks
695 * - the variable is defined to an empty value → we are not in a container
696 * - anything else → some container, either one of the known ones or "container-other"
698 e
= getenv("container");
702 v
= VIRTUALIZATION_NONE
;
709 /* Otherwise, PID 1 might have dropped this information into a file in /run. This is better than accessing
710 * /proc/1/environ, since we don't need CAP_SYS_PTRACE for that. */
711 r
= read_one_line_file("/run/systemd/container", &m
);
716 if (!IN_SET(r
, -ENOENT
, 0))
717 return log_debug_errno(r
, "Failed to read /run/systemd/container: %m");
719 /* Fallback for cases where PID 1 was not systemd (for example, cases where init=/bin/sh is used. */
720 r
= getenv_for_pid(1, "container", &m
);
725 if (r
< 0) /* This only works if we have CAP_SYS_PTRACE, hence let's better ignore failures here */
726 log_debug_errno(r
, "Failed to read $container of PID 1, ignoring: %m");
729 /* Check for existence of some well-known files. We only do this after checking
730 * for other specific container managers, otherwise we risk mistaking another
731 * container manager for Docker: the /.dockerenv file could inadvertently end up
732 * in a file system image. */
733 v
= detect_container_files();
736 if (v
!= VIRTUALIZATION_NONE
)
739 /* Finally, the root pid namespace has an hardcoded inode number of 0xEFFFFFFC since kernel 3.8, so
740 * if all else fails we can check the inode number of our pid namespace and compare it. */
741 if (running_in_pidns() > 0) {
742 log_debug("Running in a pid namespace, assuming unknown container manager.");
743 v
= VIRTUALIZATION_CONTAINER_OTHER
;
747 /* If none of that worked, give up, assume no container manager. */
748 v
= VIRTUALIZATION_NONE
;
752 if (streq(e
, "oci")) {
753 /* Some images hardcode container=oci, but OCI is not a specific container manager.
754 * Try to detect one based on well-known files. */
755 v
= detect_container_files();
756 if (v
== VIRTUALIZATION_NONE
)
757 v
= VIRTUALIZATION_CONTAINER_OTHER
;
760 v
= container_from_string(e
);
762 v
= VIRTUALIZATION_CONTAINER_OTHER
;
765 log_debug("Found container virtualization %s.", virtualization_to_string(v
));
770 Virtualization
detect_virtualization(void) {
773 v
= detect_container();
774 if (v
!= VIRTUALIZATION_NONE
)
780 int running_in_userns(void) {
783 r
= namespace_is_init(NAMESPACE_USER
);
785 return log_debug_errno(r
, "Failed to test if in root user namespace, ignoring: %m");
790 int running_in_chroot(void) {
793 /* If we're PID1, /proc may not be mounted (and most likely we're not in a chroot). But PID1 will
794 * mount /proc, so all other programs can assume that if /proc is *not* available, we're in some
797 r
= getenv_bool("SYSTEMD_IN_CHROOT");
801 log_debug_errno(r
, "Failed to parse $SYSTEMD_IN_CHROOT, ignoring: %m");
803 /* Deprecated but kept for backwards compatibility. */
804 if (getenv_bool("SYSTEMD_IGNORE_CHROOT") > 0)
807 r
= pidref_from_same_root_fs(&PIDREF_MAKE_FROM_PID(1), NULL
);
809 if (getpid_cached() == 1)
810 return false; /* We will mount /proc, assuming we're not in a chroot. */
812 log_debug("/proc/ is not mounted, assuming we're in a chroot.");
815 if (r
== -ESRCH
) /* We must have a fake /proc/, we can't do the check properly. */
823 #if defined(__i386__) || defined(__x86_64__)
824 struct cpuid_table_entry
{
829 static const struct cpuid_table_entry leaf1_edx
[] = {
855 static const struct cpuid_table_entry leaf1_ecx
[] = {
874 static const struct cpuid_table_entry leaf7_ebx
[] = {
883 static const struct cpuid_table_entry leaf81_edx
[] = {
889 static const struct cpuid_table_entry leaf81_ecx
[] = {
894 static const struct cpuid_table_entry leaf87_edx
[] = {
895 { 8, "constant_tsc" },
898 static bool given_flag_in_set(const char *flag
, const struct cpuid_table_entry
*set
, size_t set_size
, uint32_t val
) {
899 for (size_t i
= 0; i
< set_size
; i
++) {
900 if ((UINT32_C(1) << set
[i
].flag_bit
) & val
&&
901 streq(flag
, set
[i
].name
))
907 static bool real_has_cpu_with_flag(const char *flag
) {
908 uint32_t eax
, ebx
, ecx
, edx
;
910 if (__get_cpuid(1, &eax
, &ebx
, &ecx
, &edx
)) {
911 if (given_flag_in_set(flag
, leaf1_ecx
, ELEMENTSOF(leaf1_ecx
), ecx
))
914 if (given_flag_in_set(flag
, leaf1_edx
, ELEMENTSOF(leaf1_edx
), edx
))
918 if (__get_cpuid_count(7, 0, &eax
, &ebx
, &ecx
, &edx
)) {
919 if (given_flag_in_set(flag
, leaf7_ebx
, ELEMENTSOF(leaf7_ebx
), ebx
))
923 if (__get_cpuid(0x80000001U
, &eax
, &ebx
, &ecx
, &edx
)) {
924 if (given_flag_in_set(flag
, leaf81_ecx
, ELEMENTSOF(leaf81_ecx
), ecx
))
927 if (given_flag_in_set(flag
, leaf81_edx
, ELEMENTSOF(leaf81_edx
), edx
))
931 if (__get_cpuid(0x80000007U
, &eax
, &ebx
, &ecx
, &edx
))
932 if (given_flag_in_set(flag
, leaf87_edx
, ELEMENTSOF(leaf87_edx
), edx
))
939 bool has_cpu_with_flag(const char *flag
) {
940 /* CPUID is an x86 specific interface. Assume on all others that no CPUs have those flags. */
941 #if defined(__i386__) || defined(__x86_64__)
942 return real_has_cpu_with_flag(flag
);
948 static const char *const virtualization_table
[_VIRTUALIZATION_MAX
] = {
949 [VIRTUALIZATION_NONE
] = "none",
950 [VIRTUALIZATION_KVM
] = "kvm",
951 [VIRTUALIZATION_AMAZON
] = "amazon",
952 [VIRTUALIZATION_QEMU
] = "qemu",
953 [VIRTUALIZATION_BOCHS
] = "bochs",
954 [VIRTUALIZATION_XEN
] = "xen",
955 [VIRTUALIZATION_UML
] = "uml",
956 [VIRTUALIZATION_VMWARE
] = "vmware",
957 [VIRTUALIZATION_ORACLE
] = "oracle",
958 [VIRTUALIZATION_MICROSOFT
] = "microsoft",
959 [VIRTUALIZATION_ZVM
] = "zvm",
960 [VIRTUALIZATION_PARALLELS
] = "parallels",
961 [VIRTUALIZATION_BHYVE
] = "bhyve",
962 [VIRTUALIZATION_QNX
] = "qnx",
963 [VIRTUALIZATION_ACRN
] = "acrn",
964 [VIRTUALIZATION_POWERVM
] = "powervm",
965 [VIRTUALIZATION_APPLE
] = "apple",
966 [VIRTUALIZATION_SRE
] = "sre",
967 [VIRTUALIZATION_GOOGLE
] = "google",
968 [VIRTUALIZATION_VM_OTHER
] = "vm-other",
970 [VIRTUALIZATION_SYSTEMD_NSPAWN
] = "systemd-nspawn",
971 [VIRTUALIZATION_LXC_LIBVIRT
] = "lxc-libvirt",
972 [VIRTUALIZATION_LXC
] = "lxc",
973 [VIRTUALIZATION_OPENVZ
] = "openvz",
974 [VIRTUALIZATION_DOCKER
] = "docker",
975 [VIRTUALIZATION_PODMAN
] = "podman",
976 [VIRTUALIZATION_RKT
] = "rkt",
977 [VIRTUALIZATION_WSL
] = "wsl",
978 [VIRTUALIZATION_PROOT
] = "proot",
979 [VIRTUALIZATION_POUCH
] = "pouch",
980 [VIRTUALIZATION_CONTAINER_OTHER
] = "container-other",
983 DEFINE_STRING_TABLE_LOOKUP(virtualization
, Virtualization
);