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_fclose_
FILE *f
= NULL
;
265 /* Detect User-Mode Linux by reading /proc/cpuinfo */
266 f
= fopen("/proc/cpuinfo", "re");
268 if (errno
== ENOENT
) {
269 log_debug("/proc/cpuinfo not found, assuming no UML virtualization.");
270 return VIRTUALIZATION_NONE
;
276 _cleanup_free_
char *line
= NULL
;
279 r
= read_line(f
, LONG_LINE_MAX
, &line
);
285 t
= startswith(line
, "vendor_id\t: ");
287 if (startswith(t
, "User Mode Linux")) {
288 log_debug("UML virtualization found in /proc/cpuinfo");
289 return VIRTUALIZATION_UML
;
296 log_debug("UML virtualization not found in /proc/cpuinfo.");
297 return VIRTUALIZATION_NONE
;
300 static int detect_vm_zvm(void) {
302 #if defined(__s390__)
303 _cleanup_free_
char *t
= NULL
;
306 r
= get_proc_field("/proc/sysinfo", "VM00 Control Program", WHITESPACE
, &t
);
308 return VIRTUALIZATION_NONE
;
312 log_debug("Virtualization %s found in /proc/sysinfo", t
);
313 if (streq(t
, "z/VM"))
314 return VIRTUALIZATION_ZVM
;
316 return VIRTUALIZATION_KVM
;
318 log_debug("This platform does not support /proc/sysinfo");
319 return VIRTUALIZATION_NONE
;
323 /* Returns a short identifier for the various VM implementations */
324 int detect_vm(void) {
325 static thread_local
int cached_found
= _VIRTUALIZATION_INVALID
;
329 if (cached_found
>= 0)
332 /* We have to use the correct order here:
334 * → First, try to detect Oracle Virtualbox, even if it uses KVM, as well as Xen even if it cloaks as Microsoft
337 * → Second, try to detect from CPUID, this will report KVM for whatever software is used even if info in DMI is
340 * → Third, try to detect from DMI. */
342 dmi
= detect_vm_dmi();
343 if (IN_SET(dmi
, VIRTUALIZATION_ORACLE
, VIRTUALIZATION_XEN
)) {
348 r
= detect_vm_cpuid();
351 if (r
== VIRTUALIZATION_VM_OTHER
)
353 else if (r
!= VIRTUALIZATION_NONE
)
356 /* Now, let's get back to DMI */
359 if (dmi
== VIRTUALIZATION_VM_OTHER
)
361 else if (dmi
!= VIRTUALIZATION_NONE
) {
366 /* x86 xen will most likely be detected by cpuid. If not (most likely
367 * because we're not an x86 guest), then we should try the /proc/xen
368 * directory next. If that's not found, then we check for the high-level
369 * hypervisor sysfs file.
375 if (r
== VIRTUALIZATION_VM_OTHER
)
377 else if (r
!= VIRTUALIZATION_NONE
)
380 r
= detect_vm_hypervisor();
383 if (r
== VIRTUALIZATION_VM_OTHER
)
385 else if (r
!= VIRTUALIZATION_NONE
)
388 r
= detect_vm_device_tree();
391 if (r
== VIRTUALIZATION_VM_OTHER
)
393 else if (r
!= VIRTUALIZATION_NONE
)
399 if (r
== VIRTUALIZATION_VM_OTHER
)
401 else if (r
!= VIRTUALIZATION_NONE
)
409 /* x86 xen Dom0 is detected as XEN in hypervisor and maybe others.
410 * In order to detect the Dom0 as not virtualization we need to
412 if (r
== VIRTUALIZATION_XEN
) {
415 dom0
= detect_vm_xen_dom0();
419 r
= VIRTUALIZATION_NONE
;
420 } else if (r
== VIRTUALIZATION_NONE
&& other
)
421 r
= VIRTUALIZATION_VM_OTHER
;
424 log_debug("Found VM virtualization %s", virtualization_to_string(r
));
428 int detect_container(void) {
430 static const struct {
434 { "lxc", VIRTUALIZATION_LXC
},
435 { "lxc-libvirt", VIRTUALIZATION_LXC_LIBVIRT
},
436 { "systemd-nspawn", VIRTUALIZATION_SYSTEMD_NSPAWN
},
437 { "docker", VIRTUALIZATION_DOCKER
},
438 { "rkt", VIRTUALIZATION_RKT
},
441 static thread_local
int cached_found
= _VIRTUALIZATION_INVALID
;
442 _cleanup_free_
char *m
= NULL
;
443 const char *e
= NULL
;
447 if (cached_found
>= 0)
450 /* /proc/vz exists in container and outside of the container, /proc/bc only outside of the container. */
451 if (access("/proc/vz", F_OK
) >= 0 &&
452 access("/proc/bc", F_OK
) < 0) {
453 r
= VIRTUALIZATION_OPENVZ
;
457 if (getpid_cached() == 1) {
458 /* If we are PID 1 we can just check our own environment variable, and that's authoritative. */
460 e
= getenv("container");
462 r
= VIRTUALIZATION_NONE
;
469 /* Otherwise, PID 1 might have dropped this information into a file in /run. This is better than accessing
470 * /proc/1/environ, since we don't need CAP_SYS_PTRACE for that. */
471 r
= read_one_line_file("/run/systemd/container", &m
);
476 if (!IN_SET(r
, -ENOENT
, 0))
477 return log_debug_errno(r
, "Failed to read /run/systemd/container: %m");
479 /* Fallback for cases where PID 1 was not systemd (for example, cases where init=/bin/sh is used. */
480 r
= getenv_for_pid(1, "container", &m
);
485 if (r
< 0) /* This only works if we have CAP_SYS_PTRACE, hence let's better ignore failures here */
486 log_debug_errno(r
, "Failed to read $container of PID 1, ignoring: %m");
488 /* Interestingly /proc/1/sched actually shows the host's PID for what we see as PID 1. Hence, if the PID shown
489 * there is not 1, we know we are in a PID namespace. and hence a container. */
490 r
= read_one_line_file("/proc/1/sched", &m
);
498 if (!startswith(t
, "(1,")) {
499 r
= VIRTUALIZATION_CONTAINER_OTHER
;
502 } else if (r
!= -ENOENT
)
505 /* If that didn't work, give up, assume no container manager. */
506 r
= VIRTUALIZATION_NONE
;
510 for (j
= 0; j
< ELEMENTSOF(value_table
); j
++)
511 if (streq(e
, value_table
[j
].value
)) {
512 r
= value_table
[j
].id
;
516 r
= VIRTUALIZATION_CONTAINER_OTHER
;
519 log_debug("Found container virtualization %s.", virtualization_to_string(r
));
524 int detect_virtualization(void) {
527 r
= detect_container();
534 static int userns_has_mapping(const char *name
) {
535 _cleanup_fclose_
FILE *f
= NULL
;
536 _cleanup_free_
char *buf
= NULL
;
537 size_t n_allocated
= 0;
542 f
= fopen(name
, "re");
544 log_debug_errno(errno
, "Failed to open %s: %m", name
);
545 return errno
== ENOENT
? false : -errno
;
548 n
= getline(&buf
, &n_allocated
, f
);
551 log_debug("%s is empty, we're in an uninitialized user namespace", name
);
555 return log_debug_errno(errno
, "Failed to read %s: %m", name
);
558 r
= sscanf(buf
, "%"PRIu32
" %"PRIu32
" %"PRIu32
, &a
, &b
, &c
);
560 return log_debug_errno(errno
, "Failed to parse %s: %m", name
);
562 if (a
== 0 && b
== 0 && c
== UINT32_MAX
) {
563 /* The kernel calls mappings_overlap() and does not allow overlaps */
564 log_debug("%s has a full 1:1 mapping", name
);
568 /* Anything else implies that we are in a user namespace */
569 log_debug("Mapping found in %s, we're in a user namespace", name
);
573 int running_in_userns(void) {
574 _cleanup_free_
char *line
= NULL
;
577 r
= userns_has_mapping("/proc/self/uid_map");
581 r
= userns_has_mapping("/proc/self/gid_map");
585 /* "setgroups" file was added in kernel v3.18-rc6-15-g9cc46516dd. It is also
586 * possible to compile a kernel without CONFIG_USER_NS, in which case "setgroups"
587 * also does not exist. We cannot distinguish those two cases, so assume that
588 * we're running on a stripped-down recent kernel, rather than on an old one,
589 * and if the file is not found, return false.
591 r
= read_one_line_file("/proc/self/setgroups", &line
);
593 log_debug_errno(r
, "/proc/self/setgroups: %m");
594 return r
== -ENOENT
? false : r
;
598 r
= streq(line
, "deny");
599 /* See user_namespaces(7) for a description of this "setgroups" contents. */
600 log_debug("/proc/self/setgroups contains \"%s\", %s user namespace", line
, r
? "in" : "not in");
604 int running_in_chroot(void) {
607 if (getenv_bool("SYSTEMD_IGNORE_CHROOT") > 0)
610 r
= files_same("/proc/1/root", "/", 0);
617 static const char *const virtualization_table
[_VIRTUALIZATION_MAX
] = {
618 [VIRTUALIZATION_NONE
] = "none",
619 [VIRTUALIZATION_KVM
] = "kvm",
620 [VIRTUALIZATION_QEMU
] = "qemu",
621 [VIRTUALIZATION_BOCHS
] = "bochs",
622 [VIRTUALIZATION_XEN
] = "xen",
623 [VIRTUALIZATION_UML
] = "uml",
624 [VIRTUALIZATION_VMWARE
] = "vmware",
625 [VIRTUALIZATION_ORACLE
] = "oracle",
626 [VIRTUALIZATION_MICROSOFT
] = "microsoft",
627 [VIRTUALIZATION_ZVM
] = "zvm",
628 [VIRTUALIZATION_PARALLELS
] = "parallels",
629 [VIRTUALIZATION_BHYVE
] = "bhyve",
630 [VIRTUALIZATION_QNX
] = "qnx",
631 [VIRTUALIZATION_VM_OTHER
] = "vm-other",
633 [VIRTUALIZATION_SYSTEMD_NSPAWN
] = "systemd-nspawn",
634 [VIRTUALIZATION_LXC_LIBVIRT
] = "lxc-libvirt",
635 [VIRTUALIZATION_LXC
] = "lxc",
636 [VIRTUALIZATION_OPENVZ
] = "openvz",
637 [VIRTUALIZATION_DOCKER
] = "docker",
638 [VIRTUALIZATION_RKT
] = "rkt",
639 [VIRTUALIZATION_CONTAINER_OTHER
] = "container-other",
642 DEFINE_STRING_TABLE_LOOKUP(virtualization
, int);