2 This file is part of systemd.
4 Copyright 2011 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "alloc-util.h"
27 #include "dirent-util.h"
31 #include "process-util.h"
32 #include "stat-util.h"
33 #include "string-table.h"
34 #include "string-util.h"
37 #if 0 /// UNNEEDED by elogind
38 static int detect_vm_cpuid(void) {
40 /* CPUID is an x86 specific interface. */
41 #if defined(__i386__) || defined(__x86_64__)
46 } cpuid_vendor_table[] = {
47 { "XenVMMXenVMM", VIRTUALIZATION_XEN },
48 { "KVMKVMKVM", VIRTUALIZATION_KVM },
49 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
50 { "VMwareVMware", VIRTUALIZATION_VMWARE },
51 /* http://msdn.microsoft.com/en-us/library/ff542428.aspx */
52 { "Microsoft Hv", VIRTUALIZATION_MICROSOFT },
58 /* http://lwn.net/Articles/301888/ */
60 #if defined (__i386__)
63 #elif defined (__amd64__)
68 /* First detect whether there is a hypervisor */
70 __asm__ __volatile__ (
71 /* ebx/rbx is being used for PIC! */
72 " push %%"REG_b" \n\t"
76 : "=a" (eax), "=c" (ecx)
80 hypervisor = !!(ecx & 0x80000000U);
89 /* There is a hypervisor, see what it is */
91 __asm__ __volatile__ (
92 /* ebx/rbx is being used for PIC! */
93 " push %%"REG_b" \n\t"
98 : "=a" (eax), "=r" (sig.sig32[0]), "=c" (sig.sig32[1]), "=d" (sig.sig32[2])
102 log_debug("Virtualization found, CPUID=%s", sig.text);
104 for (j = 0; j < ELEMENTSOF(cpuid_vendor_table); j ++)
105 if (streq(sig.text, cpuid_vendor_table[j].cpuid))
106 return cpuid_vendor_table[j].id;
108 return VIRTUALIZATION_VM_OTHER;
111 log_debug("No virtualization found in CPUID");
113 return VIRTUALIZATION_NONE;
116 static int detect_vm_device_tree(void) {
117 #if defined(__arm__) || defined(__aarch64__) || defined(__powerpc__) || defined(__powerpc64__)
118 _cleanup_free_ char *hvtype = NULL;
121 r = read_one_line_file("/proc/device-tree/hypervisor/compatible", &hvtype);
123 _cleanup_closedir_ DIR *dir = NULL;
126 dir = opendir("/proc/device-tree");
128 if (errno == ENOENT) {
129 log_debug_errno(errno, "/proc/device-tree: %m");
130 return VIRTUALIZATION_NONE;
135 FOREACH_DIRENT(dent, dir, return -errno)
136 if (strstr(dent->d_name, "fw-cfg")) {
137 log_debug("Virtualization QEMU: \"fw-cfg\" present in /proc/device-tree/%s", dent->d_name);
138 return VIRTUALIZATION_QEMU;
141 log_debug("No virtualization found in /proc/device-tree/*");
142 return VIRTUALIZATION_NONE;
146 log_debug("Virtualization %s found in /proc/device-tree/hypervisor/compatible", hvtype);
147 if (streq(hvtype, "linux,kvm"))
148 return VIRTUALIZATION_KVM;
149 else if (strstr(hvtype, "xen"))
150 return VIRTUALIZATION_XEN;
152 return VIRTUALIZATION_VM_OTHER;
154 log_debug("This platform does not support /proc/device-tree");
155 return VIRTUALIZATION_NONE;
159 static int detect_vm_dmi(void) {
160 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
162 static const char *const dmi_vendors[] = {
163 "/sys/class/dmi/id/product_name", /* Test this before sys_vendor to detect KVM over QEMU */
164 "/sys/class/dmi/id/sys_vendor",
165 "/sys/class/dmi/id/board_vendor",
166 "/sys/class/dmi/id/bios_vendor"
169 static const struct {
172 } dmi_vendor_table[] = {
173 { "KVM", VIRTUALIZATION_KVM },
174 { "QEMU", VIRTUALIZATION_QEMU },
175 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
176 { "VMware", VIRTUALIZATION_VMWARE },
177 { "VMW", VIRTUALIZATION_VMWARE },
178 { "innotek GmbH", VIRTUALIZATION_ORACLE },
179 { "Xen", VIRTUALIZATION_XEN },
180 { "Bochs", VIRTUALIZATION_BOCHS },
181 { "Parallels", VIRTUALIZATION_PARALLELS },
186 for (i = 0; i < ELEMENTSOF(dmi_vendors); i++) {
187 _cleanup_free_ char *s = NULL;
190 r = read_one_line_file(dmi_vendors[i], &s);
200 for (j = 0; j < ELEMENTSOF(dmi_vendor_table); j++)
201 if (startswith(s, dmi_vendor_table[j].vendor)) {
202 log_debug("Virtualization %s found in DMI (%s)", s, dmi_vendors[i]);
203 return dmi_vendor_table[j].id;
208 log_debug("No virtualization found in DMI");
210 return VIRTUALIZATION_NONE;
213 static int detect_vm_xen(void) {
214 /* Check for Dom0 will be executed later in detect_vm_xen_dom0
215 Thats why we dont check the content of /proc/xen/capabilities here. */
216 if (access("/proc/xen/capabilities", F_OK) < 0) {
217 log_debug("Virtualization XEN not found, /proc/xen/capabilities does not exist");
218 return VIRTUALIZATION_NONE;
221 log_debug("Virtualization XEN found (/proc/xen/capabilities exists)");
222 return VIRTUALIZATION_XEN;
226 static bool detect_vm_xen_dom0(void) {
227 _cleanup_free_ char *domcap = NULL;
231 r = read_one_line_file("/proc/xen/capabilities", &domcap);
233 log_debug("Virtualization XEN not found, /proc/xen/capabilities does not exist");
240 while ((cap = strsep(&i, ",")))
241 if (streq(cap, "control_d"))
244 log_debug("Virtualization XEN DomU found (/proc/xen/capabilites)");
248 log_debug("Virtualization XEN Dom0 ignored (/proc/xen/capabilities)");
252 static int detect_vm_hypervisor(void) {
253 _cleanup_free_ char *hvtype = NULL;
256 r = read_one_line_file("/sys/hypervisor/type", &hvtype);
258 return VIRTUALIZATION_NONE;
262 log_debug("Virtualization %s found in /sys/hypervisor/type", hvtype);
264 if (streq(hvtype, "xen"))
265 return VIRTUALIZATION_XEN;
267 return VIRTUALIZATION_VM_OTHER;
270 static int detect_vm_uml(void) {
271 _cleanup_free_ char *cpuinfo_contents = NULL;
274 /* Detect User-Mode Linux by reading /proc/cpuinfo */
275 r = read_full_file("/proc/cpuinfo", &cpuinfo_contents, NULL);
279 if (strstr(cpuinfo_contents, "\nvendor_id\t: User Mode Linux\n")) {
280 log_debug("UML virtualization found in /proc/cpuinfo");
281 return VIRTUALIZATION_UML;
284 log_debug("No virtualization found in /proc/cpuinfo.");
285 return VIRTUALIZATION_NONE;
288 static int detect_vm_zvm(void) {
290 #if defined(__s390__)
291 _cleanup_free_ char *t = NULL;
294 r = get_proc_field("/proc/sysinfo", "VM00 Control Program", WHITESPACE, &t);
296 return VIRTUALIZATION_NONE;
300 log_debug("Virtualization %s found in /proc/sysinfo", t);
301 if (streq(t, "z/VM"))
302 return VIRTUALIZATION_ZVM;
304 return VIRTUALIZATION_KVM;
306 log_debug("This platform does not support /proc/sysinfo");
307 return VIRTUALIZATION_NONE;
311 /* Returns a short identifier for the various VM implementations */
312 int detect_vm(void) {
313 static thread_local int cached_found = _VIRTUALIZATION_INVALID;
316 if (cached_found >= 0)
319 /* We have to use the correct order here:
320 * Some virtualization technologies do use KVM hypervisor but are
321 * expected to be detected as something else. So detect DMI first.
323 * An example is Virtualbox since version 5.0, which uses KVM backend.
324 * Detection via DMI works corretly, the CPU ID would find KVM
329 if (r != VIRTUALIZATION_NONE)
332 r = detect_vm_cpuid();
335 if (r != VIRTUALIZATION_NONE)
338 /* x86 xen will most likely be detected by cpuid. If not (most likely
339 * because we're not an x86 guest), then we should try the xen capabilities
340 * file next. If that's not found, then we check for the high-level
341 * hypervisor sysfs file:
343 * https://bugs.freedesktop.org/show_bug.cgi?id=77271 */
348 if (r != VIRTUALIZATION_NONE)
351 r = detect_vm_hypervisor();
354 if (r != VIRTUALIZATION_NONE)
357 r = detect_vm_device_tree();
360 if (r != VIRTUALIZATION_NONE)
366 if (r != VIRTUALIZATION_NONE)
374 /* x86 xen Dom0 is detected as XEN in hypervisor and maybe others.
375 * In order to detect the Dom0 as not virtualization we need to
377 if (r == VIRTUALIZATION_XEN && detect_vm_xen_dom0())
378 r = VIRTUALIZATION_NONE;
381 log_debug("Found VM virtualization %s", virtualization_to_string(r));
386 int detect_container(void) {
388 static const struct {
392 { "lxc", VIRTUALIZATION_LXC },
393 { "lxc-libvirt", VIRTUALIZATION_LXC_LIBVIRT },
394 { "systemd-nspawn", VIRTUALIZATION_SYSTEMD_NSPAWN },
395 { "docker", VIRTUALIZATION_DOCKER },
396 { "rkt", VIRTUALIZATION_RKT },
399 static thread_local int cached_found = _VIRTUALIZATION_INVALID;
400 _cleanup_free_ char *m = NULL;
401 const char *e = NULL;
405 if (cached_found >= 0)
408 /* /proc/vz exists in container and outside of the container,
409 * /proc/bc only outside of the container. */
410 if (access("/proc/vz", F_OK) >= 0 &&
411 access("/proc/bc", F_OK) < 0) {
412 r = VIRTUALIZATION_OPENVZ;
417 /* If we are PID 1 we can just check our own
418 * environment variable */
420 e = getenv("container");
422 r = VIRTUALIZATION_NONE;
427 /* Otherwise, PID 1 dropped this information into a
428 * file in /run. This is better than accessing
429 * /proc/1/environ, since we don't need CAP_SYS_PTRACE
432 r = read_one_line_file("/run/systemd/container", &m);
435 /* Fallback for cases where PID 1 was not
436 * systemd (for example, cases where
437 * init=/bin/sh is used. */
439 r = getenv_for_pid(1, "container", &m);
442 /* If that didn't work, give up,
443 * assume no container manager.
445 * Note: This means we still cannot
446 * detect containers if init=/bin/sh
447 * is passed but privileges dropped,
448 * as /proc/1/environ is only readable
449 * with privileges. */
451 r = VIRTUALIZATION_NONE;
461 for (j = 0; j < ELEMENTSOF(value_table); j++)
462 if (streq(e, value_table[j].value)) {
463 r = value_table[j].id;
467 r = VIRTUALIZATION_CONTAINER_OTHER;
470 log_debug("Found container virtualization %s", virtualization_to_string(r));
475 #if 0 /// UNNEEDED by elogind
476 int detect_virtualization(void) {
479 r = detect_container();
487 int running_in_chroot(void) {
490 ret = files_same("/proc/1/root", "/");
497 static const char *const virtualization_table[_VIRTUALIZATION_MAX] = {
498 [VIRTUALIZATION_NONE] = "none",
499 [VIRTUALIZATION_KVM] = "kvm",
500 [VIRTUALIZATION_QEMU] = "qemu",
501 [VIRTUALIZATION_BOCHS] = "bochs",
502 [VIRTUALIZATION_XEN] = "xen",
503 [VIRTUALIZATION_UML] = "uml",
504 [VIRTUALIZATION_VMWARE] = "vmware",
505 [VIRTUALIZATION_ORACLE] = "oracle",
506 [VIRTUALIZATION_MICROSOFT] = "microsoft",
507 [VIRTUALIZATION_ZVM] = "zvm",
508 [VIRTUALIZATION_PARALLELS] = "parallels",
509 [VIRTUALIZATION_VM_OTHER] = "vm-other",
511 [VIRTUALIZATION_SYSTEMD_NSPAWN] = "systemd-nspawn",
512 [VIRTUALIZATION_LXC_LIBVIRT] = "lxc-libvirt",
513 [VIRTUALIZATION_LXC] = "lxc",
514 [VIRTUALIZATION_OPENVZ] = "openvz",
515 [VIRTUALIZATION_DOCKER] = "docker",
516 [VIRTUALIZATION_RKT] = "rkt",
517 [VIRTUALIZATION_CONTAINER_OTHER] = "container-other",
520 DEFINE_STRING_TABLE_LOOKUP(virtualization, int);