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
},
45 uint32_t eax
, ebx
, ecx
, edx
;
48 /* http://lwn.net/Articles/301888/ */
50 /* First detect whether there is a hypervisor */
51 if (__get_cpuid(1, &eax
, &ebx
, &ecx
, &edx
) == 0)
52 return VIRTUALIZATION_NONE
;
54 hypervisor
= ecx
& 0x80000000U
;
63 /* There is a hypervisor, see what it is */
64 __cpuid(0x40000000U
, eax
, ebx
, ecx
, edx
);
70 log_debug("Virtualization found, CPUID=%s", sig
.text
);
72 for (j
= 0; j
< ELEMENTSOF(cpuid_vendor_table
); j
++)
73 if (streq(sig
.text
, cpuid_vendor_table
[j
].cpuid
))
74 return cpuid_vendor_table
[j
].id
;
76 return VIRTUALIZATION_VM_OTHER
;
79 log_debug("No virtualization found in CPUID");
81 return VIRTUALIZATION_NONE
;
84 static int detect_vm_device_tree(void) {
85 #if defined(__arm__) || defined(__aarch64__) || defined(__powerpc__) || defined(__powerpc64__)
86 _cleanup_free_
char *hvtype
= NULL
;
89 r
= read_one_line_file("/proc/device-tree/hypervisor/compatible", &hvtype
);
91 _cleanup_closedir_
DIR *dir
= NULL
;
94 dir
= opendir("/proc/device-tree");
96 if (errno
== ENOENT
) {
97 log_debug_errno(errno
, "/proc/device-tree: %m");
98 return VIRTUALIZATION_NONE
;
103 FOREACH_DIRENT(dent
, dir
, return -errno
)
104 if (strstr(dent
->d_name
, "fw-cfg")) {
105 log_debug("Virtualization QEMU: \"fw-cfg\" present in /proc/device-tree/%s", dent
->d_name
);
106 return VIRTUALIZATION_QEMU
;
109 log_debug("No virtualization found in /proc/device-tree/*");
110 return VIRTUALIZATION_NONE
;
114 log_debug("Virtualization %s found in /proc/device-tree/hypervisor/compatible", hvtype
);
115 if (streq(hvtype
, "linux,kvm"))
116 return VIRTUALIZATION_KVM
;
117 else if (strstr(hvtype
, "xen"))
118 return VIRTUALIZATION_XEN
;
120 return VIRTUALIZATION_VM_OTHER
;
122 log_debug("This platform does not support /proc/device-tree");
123 return VIRTUALIZATION_NONE
;
127 static int detect_vm_dmi(void) {
128 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
130 static const char *const dmi_vendors
[] = {
131 "/sys/class/dmi/id/product_name", /* Test this before sys_vendor to detect KVM over QEMU */
132 "/sys/class/dmi/id/sys_vendor",
133 "/sys/class/dmi/id/board_vendor",
134 "/sys/class/dmi/id/bios_vendor"
137 static const struct {
140 } dmi_vendor_table
[] = {
141 { "KVM", VIRTUALIZATION_KVM
},
142 { "QEMU", VIRTUALIZATION_QEMU
},
143 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
144 { "VMware", VIRTUALIZATION_VMWARE
},
145 { "VMW", VIRTUALIZATION_VMWARE
},
146 { "innotek GmbH", VIRTUALIZATION_ORACLE
},
147 { "Xen", VIRTUALIZATION_XEN
},
148 { "Bochs", VIRTUALIZATION_BOCHS
},
149 { "Parallels", VIRTUALIZATION_PARALLELS
},
150 /* https://wiki.freebsd.org/bhyve */
151 { "BHYVE", VIRTUALIZATION_BHYVE
},
156 for (i
= 0; i
< ELEMENTSOF(dmi_vendors
); i
++) {
157 _cleanup_free_
char *s
= NULL
;
160 r
= read_one_line_file(dmi_vendors
[i
], &s
);
168 for (j
= 0; j
< ELEMENTSOF(dmi_vendor_table
); j
++)
169 if (startswith(s
, dmi_vendor_table
[j
].vendor
)) {
170 log_debug("Virtualization %s found in DMI (%s)", s
, dmi_vendors
[i
]);
171 return dmi_vendor_table
[j
].id
;
176 log_debug("No virtualization found in DMI");
178 return VIRTUALIZATION_NONE
;
181 static int detect_vm_xen(void) {
183 /* Check for Dom0 will be executed later in detect_vm_xen_dom0
184 The presence of /proc/xen indicates some form of a Xen domain */
185 if (access("/proc/xen", F_OK
) < 0) {
186 log_debug("Virtualization XEN not found, /proc/xen does not exist");
187 return VIRTUALIZATION_NONE
;
190 log_debug("Virtualization XEN found (/proc/xen exists)");
191 return VIRTUALIZATION_XEN
;
194 #define XENFEAT_dom0 11 /* xen/include/public/features.h */
195 #define PATH_FEATURES "/sys/hypervisor/properties/features"
196 /* Returns -errno, or 0 for domU, or 1 for dom0 */
197 static int detect_vm_xen_dom0(void) {
198 _cleanup_free_
char *domcap
= NULL
;
202 r
= read_one_line_file(PATH_FEATURES
, &domcap
);
203 if (r
< 0 && r
!= -ENOENT
)
206 unsigned long features
;
208 /* Here, we need to use sscanf() instead of safe_atoul()
209 * as the string lacks the leading "0x". */
210 r
= sscanf(domcap
, "%lx", &features
);
212 r
= !!(features
& (1U << XENFEAT_dom0
));
213 log_debug("Virtualization XEN, found %s with value %08lx, "
214 "XENFEAT_dom0 (indicating the 'hardware domain') is%s set.",
215 PATH_FEATURES
, features
, r
? "" : " not");
218 log_debug("Virtualization XEN, found %s, unhandled content '%s'",
219 PATH_FEATURES
, domcap
);
222 r
= read_one_line_file("/proc/xen/capabilities", &domcap
);
224 log_debug("Virtualization XEN because /proc/xen/capabilities does not exist");
231 while ((cap
= strsep(&i
, ",")))
232 if (streq(cap
, "control_d"))
235 log_debug("Virtualization XEN DomU found (/proc/xen/capabilites)");
239 log_debug("Virtualization XEN Dom0 ignored (/proc/xen/capabilities)");
243 static int detect_vm_hypervisor(void) {
244 _cleanup_free_
char *hvtype
= NULL
;
247 r
= read_one_line_file("/sys/hypervisor/type", &hvtype
);
249 return VIRTUALIZATION_NONE
;
253 log_debug("Virtualization %s found in /sys/hypervisor/type", hvtype
);
255 if (streq(hvtype
, "xen"))
256 return VIRTUALIZATION_XEN
;
258 return VIRTUALIZATION_VM_OTHER
;
261 static int detect_vm_uml(void) {
262 _cleanup_free_
char *cpuinfo_contents
= NULL
;
265 /* Detect User-Mode Linux by reading /proc/cpuinfo */
266 r
= read_full_file("/proc/cpuinfo", &cpuinfo_contents
, NULL
);
268 log_debug("/proc/cpuinfo not found, assuming no UML virtualization.");
269 return VIRTUALIZATION_NONE
;
274 if (strstr(cpuinfo_contents
, "\nvendor_id\t: User Mode Linux\n")) {
275 log_debug("UML virtualization found in /proc/cpuinfo");
276 return VIRTUALIZATION_UML
;
279 log_debug("UML virtualization not found in /proc/cpuinfo.");
280 return VIRTUALIZATION_NONE
;
283 static int detect_vm_zvm(void) {
285 #if defined(__s390__)
286 _cleanup_free_
char *t
= NULL
;
289 r
= get_proc_field("/proc/sysinfo", "VM00 Control Program", WHITESPACE
, &t
);
291 return VIRTUALIZATION_NONE
;
295 log_debug("Virtualization %s found in /proc/sysinfo", t
);
296 if (streq(t
, "z/VM"))
297 return VIRTUALIZATION_ZVM
;
299 return VIRTUALIZATION_KVM
;
301 log_debug("This platform does not support /proc/sysinfo");
302 return VIRTUALIZATION_NONE
;
306 /* Returns a short identifier for the various VM implementations */
307 int detect_vm(void) {
308 static thread_local
int cached_found
= _VIRTUALIZATION_INVALID
;
312 if (cached_found
>= 0)
315 /* We have to use the correct order here:
317 * → First, try to detect Oracle Virtualbox, even if it uses KVM, as well as Xen even if it cloaks as Microsoft
320 * → Second, try to detect from CPUID, this will report KVM for whatever software is used even if info in DMI is
323 * → Third, try to detect from DMI. */
325 dmi
= detect_vm_dmi();
326 if (IN_SET(dmi
, VIRTUALIZATION_ORACLE
, VIRTUALIZATION_XEN
)) {
331 r
= detect_vm_cpuid();
334 if (r
== VIRTUALIZATION_VM_OTHER
)
336 else if (r
!= VIRTUALIZATION_NONE
)
339 /* Now, let's get back to DMI */
342 if (dmi
== VIRTUALIZATION_VM_OTHER
)
344 else if (dmi
!= VIRTUALIZATION_NONE
) {
349 /* x86 xen will most likely be detected by cpuid. If not (most likely
350 * because we're not an x86 guest), then we should try the /proc/xen
351 * directory next. If that's not found, then we check for the high-level
352 * hypervisor sysfs file.
358 if (r
== VIRTUALIZATION_VM_OTHER
)
360 else if (r
!= VIRTUALIZATION_NONE
)
363 r
= detect_vm_hypervisor();
366 if (r
== VIRTUALIZATION_VM_OTHER
)
368 else if (r
!= VIRTUALIZATION_NONE
)
371 r
= detect_vm_device_tree();
374 if (r
== VIRTUALIZATION_VM_OTHER
)
376 else if (r
!= VIRTUALIZATION_NONE
)
382 if (r
== VIRTUALIZATION_VM_OTHER
)
384 else if (r
!= VIRTUALIZATION_NONE
)
392 /* x86 xen Dom0 is detected as XEN in hypervisor and maybe others.
393 * In order to detect the Dom0 as not virtualization we need to
395 if (r
== VIRTUALIZATION_XEN
) {
398 dom0
= detect_vm_xen_dom0();
402 r
= VIRTUALIZATION_NONE
;
403 } else if (r
== VIRTUALIZATION_NONE
&& other
)
404 r
= VIRTUALIZATION_VM_OTHER
;
407 log_debug("Found VM virtualization %s", virtualization_to_string(r
));
411 int detect_container(void) {
413 static const struct {
417 { "lxc", VIRTUALIZATION_LXC
},
418 { "lxc-libvirt", VIRTUALIZATION_LXC_LIBVIRT
},
419 { "systemd-nspawn", VIRTUALIZATION_SYSTEMD_NSPAWN
},
420 { "docker", VIRTUALIZATION_DOCKER
},
421 { "rkt", VIRTUALIZATION_RKT
},
424 static thread_local
int cached_found
= _VIRTUALIZATION_INVALID
;
425 _cleanup_free_
char *m
= NULL
;
426 const char *e
= NULL
;
430 if (cached_found
>= 0)
433 /* /proc/vz exists in container and outside of the container, /proc/bc only outside of the container. */
434 if (access("/proc/vz", F_OK
) >= 0 &&
435 access("/proc/bc", F_OK
) < 0) {
436 r
= VIRTUALIZATION_OPENVZ
;
440 if (getpid_cached() == 1) {
441 /* If we are PID 1 we can just check our own environment variable, and that's authoritative. */
443 e
= getenv("container");
445 r
= VIRTUALIZATION_NONE
;
452 /* Otherwise, PID 1 might have dropped this information into a file in /run. This is better than accessing
453 * /proc/1/environ, since we don't need CAP_SYS_PTRACE for that. */
454 r
= read_one_line_file("/run/systemd/container", &m
);
460 return log_debug_errno(r
, "Failed to read /run/systemd/container: %m");
462 /* Fallback for cases where PID 1 was not systemd (for example, cases where init=/bin/sh is used. */
463 r
= getenv_for_pid(1, "container", &m
);
468 if (r
< 0) /* This only works if we have CAP_SYS_PTRACE, hence let's better ignore failures here */
469 log_debug_errno(r
, "Failed to read $container of PID 1, ignoring: %m");
471 /* Interestingly /proc/1/sched actually shows the host's PID for what we see as PID 1. Hence, if the PID shown
472 * there is not 1, we know we are in a PID namespace. and hence a container. */
473 r
= read_one_line_file("/proc/1/sched", &m
);
481 if (!startswith(t
, "(1,")) {
482 r
= VIRTUALIZATION_CONTAINER_OTHER
;
485 } else if (r
!= -ENOENT
)
488 /* If that didn't work, give up, assume no container manager. */
489 r
= VIRTUALIZATION_NONE
;
493 for (j
= 0; j
< ELEMENTSOF(value_table
); j
++)
494 if (streq(e
, value_table
[j
].value
)) {
495 r
= value_table
[j
].id
;
499 r
= VIRTUALIZATION_CONTAINER_OTHER
;
502 log_debug("Found container virtualization %s.", virtualization_to_string(r
));
507 int detect_virtualization(void) {
510 r
= detect_container();
517 static int userns_has_mapping(const char *name
) {
518 _cleanup_fclose_
FILE *f
= NULL
;
519 _cleanup_free_
char *buf
= NULL
;
520 size_t n_allocated
= 0;
525 f
= fopen(name
, "re");
527 log_debug_errno(errno
, "Failed to open %s: %m", name
);
528 return errno
== ENOENT
? false : -errno
;
531 n
= getline(&buf
, &n_allocated
, f
);
534 log_debug("%s is empty, we're in an uninitialized user namespace", name
);
538 return log_debug_errno(errno
, "Failed to read %s: %m", name
);
541 r
= sscanf(buf
, "%"PRIu32
" %"PRIu32
" %"PRIu32
, &a
, &b
, &c
);
543 return log_debug_errno(errno
, "Failed to parse %s: %m", name
);
545 if (a
== 0 && b
== 0 && c
== UINT32_MAX
) {
546 /* The kernel calls mappings_overlap() and does not allow overlaps */
547 log_debug("%s has a full 1:1 mapping", name
);
551 /* Anything else implies that we are in a user namespace */
552 log_debug("Mapping found in %s, we're in a user namespace", name
);
556 int running_in_userns(void) {
557 _cleanup_free_
char *line
= NULL
;
560 r
= userns_has_mapping("/proc/self/uid_map");
564 r
= userns_has_mapping("/proc/self/gid_map");
568 /* "setgroups" file was added in kernel v3.18-rc6-15-g9cc46516dd. It is also
569 * possible to compile a kernel without CONFIG_USER_NS, in which case "setgroups"
570 * also does not exist. We cannot distinguish those two cases, so assume that
571 * we're running on a stripped-down recent kernel, rather than on an old one,
572 * and if the file is not found, return false.
574 r
= read_one_line_file("/proc/self/setgroups", &line
);
576 log_debug_errno(r
, "/proc/self/setgroups: %m");
577 return r
== -ENOENT
? false : r
;
581 r
= streq(line
, "deny");
582 /* See user_namespaces(7) for a description of this "setgroups" contents. */
583 log_debug("/proc/self/setgroups contains \"%s\", %s user namespace", line
, r
? "in" : "not in");
587 int running_in_chroot(void) {
590 if (getenv_bool("SYSTEMD_IGNORE_CHROOT") > 0)
593 r
= files_same("/proc/1/root", "/", 0);
600 static const char *const virtualization_table
[_VIRTUALIZATION_MAX
] = {
601 [VIRTUALIZATION_NONE
] = "none",
602 [VIRTUALIZATION_KVM
] = "kvm",
603 [VIRTUALIZATION_QEMU
] = "qemu",
604 [VIRTUALIZATION_BOCHS
] = "bochs",
605 [VIRTUALIZATION_XEN
] = "xen",
606 [VIRTUALIZATION_UML
] = "uml",
607 [VIRTUALIZATION_VMWARE
] = "vmware",
608 [VIRTUALIZATION_ORACLE
] = "oracle",
609 [VIRTUALIZATION_MICROSOFT
] = "microsoft",
610 [VIRTUALIZATION_ZVM
] = "zvm",
611 [VIRTUALIZATION_PARALLELS
] = "parallels",
612 [VIRTUALIZATION_BHYVE
] = "bhyve",
613 [VIRTUALIZATION_QNX
] = "qnx",
614 [VIRTUALIZATION_VM_OTHER
] = "vm-other",
616 [VIRTUALIZATION_SYSTEMD_NSPAWN
] = "systemd-nspawn",
617 [VIRTUALIZATION_LXC_LIBVIRT
] = "lxc-libvirt",
618 [VIRTUALIZATION_LXC
] = "lxc",
619 [VIRTUALIZATION_OPENVZ
] = "openvz",
620 [VIRTUALIZATION_DOCKER
] = "docker",
621 [VIRTUALIZATION_RKT
] = "rkt",
622 [VIRTUALIZATION_CONTAINER_OTHER
] = "container-other",
625 DEFINE_STRING_TABLE_LOOKUP(virtualization
, int);