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 static int detect_vm_cpuid(void) {
39 /* CPUID is an x86 specific interface. */
40 #if defined(__i386__) || defined(__x86_64__)
45 } cpuid_vendor_table
[] = {
46 { "XenVMMXenVMM", VIRTUALIZATION_XEN
},
47 { "KVMKVMKVM", VIRTUALIZATION_KVM
},
48 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
49 { "VMwareVMware", VIRTUALIZATION_VMWARE
},
50 /* http://msdn.microsoft.com/en-us/library/ff542428.aspx */
51 { "Microsoft Hv", VIRTUALIZATION_MICROSOFT
},
52 /* https://wiki.freebsd.org/bhyve */
53 { "bhyve bhyve ", VIRTUALIZATION_BHYVE
},
59 /* http://lwn.net/Articles/301888/ */
61 #if defined (__i386__)
64 #elif defined (__amd64__)
69 /* First detect whether there is a hypervisor */
71 __asm__
__volatile__ (
72 /* ebx/rbx is being used for PIC! */
73 " push %%"REG_b
" \n\t"
77 : "=a" (eax
), "=c" (ecx
)
81 hypervisor
= !!(ecx
& 0x80000000U
);
90 /* There is a hypervisor, see what it is */
92 __asm__
__volatile__ (
93 /* ebx/rbx is being used for PIC! */
94 " push %%"REG_b
" \n\t"
99 : "=a" (eax
), "=r" (sig
.sig32
[0]), "=c" (sig
.sig32
[1]), "=d" (sig
.sig32
[2])
103 log_debug("Virtualization found, CPUID=%s", sig
.text
);
105 for (j
= 0; j
< ELEMENTSOF(cpuid_vendor_table
); j
++)
106 if (streq(sig
.text
, cpuid_vendor_table
[j
].cpuid
))
107 return cpuid_vendor_table
[j
].id
;
109 return VIRTUALIZATION_VM_OTHER
;
112 log_debug("No virtualization found in CPUID");
114 return VIRTUALIZATION_NONE
;
117 static int detect_vm_device_tree(void) {
118 #if defined(__arm__) || defined(__aarch64__) || defined(__powerpc__) || defined(__powerpc64__)
119 _cleanup_free_
char *hvtype
= NULL
;
122 r
= read_one_line_file("/proc/device-tree/hypervisor/compatible", &hvtype
);
124 _cleanup_closedir_
DIR *dir
= NULL
;
127 dir
= opendir("/proc/device-tree");
129 if (errno
== ENOENT
) {
130 log_debug_errno(errno
, "/proc/device-tree: %m");
131 return VIRTUALIZATION_NONE
;
136 FOREACH_DIRENT(dent
, dir
, return -errno
)
137 if (strstr(dent
->d_name
, "fw-cfg")) {
138 log_debug("Virtualization QEMU: \"fw-cfg\" present in /proc/device-tree/%s", dent
->d_name
);
139 return VIRTUALIZATION_QEMU
;
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
;
153 return VIRTUALIZATION_VM_OTHER
;
155 log_debug("This platform does not support /proc/device-tree");
156 return VIRTUALIZATION_NONE
;
160 static int detect_vm_dmi(void) {
161 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
163 static const char *const dmi_vendors
[] = {
164 "/sys/class/dmi/id/product_name", /* Test this before sys_vendor to detect KVM over QEMU */
165 "/sys/class/dmi/id/sys_vendor",
166 "/sys/class/dmi/id/board_vendor",
167 "/sys/class/dmi/id/bios_vendor"
170 static const struct {
173 } dmi_vendor_table
[] = {
174 { "KVM", VIRTUALIZATION_KVM
},
175 { "QEMU", VIRTUALIZATION_QEMU
},
176 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
177 { "VMware", VIRTUALIZATION_VMWARE
},
178 { "VMW", VIRTUALIZATION_VMWARE
},
179 { "innotek GmbH", VIRTUALIZATION_ORACLE
},
180 { "Xen", VIRTUALIZATION_XEN
},
181 { "Bochs", VIRTUALIZATION_BOCHS
},
182 { "Parallels", VIRTUALIZATION_PARALLELS
},
183 /* https://wiki.freebsd.org/bhyve */
184 { "BHYVE", VIRTUALIZATION_BHYVE
},
189 for (i
= 0; i
< ELEMENTSOF(dmi_vendors
); i
++) {
190 _cleanup_free_
char *s
= NULL
;
193 r
= read_one_line_file(dmi_vendors
[i
], &s
);
203 for (j
= 0; j
< ELEMENTSOF(dmi_vendor_table
); j
++)
204 if (startswith(s
, dmi_vendor_table
[j
].vendor
)) {
205 log_debug("Virtualization %s found in DMI (%s)", s
, dmi_vendors
[i
]);
206 return dmi_vendor_table
[j
].id
;
211 log_debug("No virtualization found in DMI");
213 return VIRTUALIZATION_NONE
;
216 static int detect_vm_xen(void) {
217 /* Check for Dom0 will be executed later in detect_vm_xen_dom0
218 Thats why we dont check the content of /proc/xen/capabilities here. */
219 if (access("/proc/xen/capabilities", F_OK
) < 0) {
220 log_debug("Virtualization XEN not found, /proc/xen/capabilities does not exist");
221 return VIRTUALIZATION_NONE
;
224 log_debug("Virtualization XEN found (/proc/xen/capabilities exists)");
225 return VIRTUALIZATION_XEN
;
229 static bool detect_vm_xen_dom0(void) {
230 _cleanup_free_
char *domcap
= NULL
;
234 r
= read_one_line_file("/proc/xen/capabilities", &domcap
);
236 log_debug("Virtualization XEN not found, /proc/xen/capabilities does not exist");
243 while ((cap
= strsep(&i
, ",")))
244 if (streq(cap
, "control_d"))
247 log_debug("Virtualization XEN DomU found (/proc/xen/capabilites)");
251 log_debug("Virtualization XEN Dom0 ignored (/proc/xen/capabilities)");
255 static int detect_vm_hypervisor(void) {
256 _cleanup_free_
char *hvtype
= NULL
;
259 r
= read_one_line_file("/sys/hypervisor/type", &hvtype
);
261 return VIRTUALIZATION_NONE
;
265 log_debug("Virtualization %s found in /sys/hypervisor/type", hvtype
);
267 if (streq(hvtype
, "xen"))
268 return VIRTUALIZATION_XEN
;
270 return VIRTUALIZATION_VM_OTHER
;
273 static int detect_vm_uml(void) {
274 _cleanup_free_
char *cpuinfo_contents
= NULL
;
277 /* Detect User-Mode Linux by reading /proc/cpuinfo */
278 r
= read_full_file("/proc/cpuinfo", &cpuinfo_contents
, NULL
);
282 if (strstr(cpuinfo_contents
, "\nvendor_id\t: User Mode Linux\n")) {
283 log_debug("UML virtualization found in /proc/cpuinfo");
284 return VIRTUALIZATION_UML
;
287 log_debug("No virtualization found in /proc/cpuinfo.");
288 return VIRTUALIZATION_NONE
;
291 static int detect_vm_zvm(void) {
293 #if defined(__s390__)
294 _cleanup_free_
char *t
= NULL
;
297 r
= get_proc_field("/proc/sysinfo", "VM00 Control Program", WHITESPACE
, &t
);
299 return VIRTUALIZATION_NONE
;
303 log_debug("Virtualization %s found in /proc/sysinfo", t
);
304 if (streq(t
, "z/VM"))
305 return VIRTUALIZATION_ZVM
;
307 return VIRTUALIZATION_KVM
;
309 log_debug("This platform does not support /proc/sysinfo");
310 return VIRTUALIZATION_NONE
;
314 /* Returns a short identifier for the various VM implementations */
315 int detect_vm(void) {
316 static thread_local
int cached_found
= _VIRTUALIZATION_INVALID
;
319 if (cached_found
>= 0)
322 /* We have to use the correct order here:
323 * Some virtualization technologies do use KVM hypervisor but are
324 * expected to be detected as something else. So detect DMI first.
326 * An example is Virtualbox since version 5.0, which uses KVM backend.
327 * Detection via DMI works corretly, the CPU ID would find KVM
332 if (r
!= VIRTUALIZATION_NONE
)
335 r
= detect_vm_cpuid();
338 if (r
!= VIRTUALIZATION_NONE
)
341 /* x86 xen will most likely be detected by cpuid. If not (most likely
342 * because we're not an x86 guest), then we should try the xen capabilities
343 * file next. If that's not found, then we check for the high-level
344 * hypervisor sysfs file:
346 * https://bugs.freedesktop.org/show_bug.cgi?id=77271 */
351 if (r
!= VIRTUALIZATION_NONE
)
354 r
= detect_vm_hypervisor();
357 if (r
!= VIRTUALIZATION_NONE
)
360 r
= detect_vm_device_tree();
363 if (r
!= VIRTUALIZATION_NONE
)
369 if (r
!= VIRTUALIZATION_NONE
)
377 /* x86 xen Dom0 is detected as XEN in hypervisor and maybe others.
378 * In order to detect the Dom0 as not virtualization we need to
380 if (r
== VIRTUALIZATION_XEN
&& detect_vm_xen_dom0())
381 r
= VIRTUALIZATION_NONE
;
384 log_debug("Found VM virtualization %s", virtualization_to_string(r
));
388 int detect_container(void) {
390 static const struct {
394 { "lxc", VIRTUALIZATION_LXC
},
395 { "lxc-libvirt", VIRTUALIZATION_LXC_LIBVIRT
},
396 { "systemd-nspawn", VIRTUALIZATION_SYSTEMD_NSPAWN
},
397 { "docker", VIRTUALIZATION_DOCKER
},
398 { "rkt", VIRTUALIZATION_RKT
},
401 static thread_local
int cached_found
= _VIRTUALIZATION_INVALID
;
402 _cleanup_free_
char *m
= NULL
;
403 const char *e
= NULL
;
407 if (cached_found
>= 0)
410 /* /proc/vz exists in container and outside of the container,
411 * /proc/bc only outside of the container. */
412 if (access("/proc/vz", F_OK
) >= 0 &&
413 access("/proc/bc", F_OK
) < 0) {
414 r
= VIRTUALIZATION_OPENVZ
;
419 /* If we are PID 1 we can just check our own
420 * environment variable */
422 e
= getenv("container");
424 r
= VIRTUALIZATION_NONE
;
429 /* Otherwise, PID 1 dropped this information into a
430 * file in /run. This is better than accessing
431 * /proc/1/environ, since we don't need CAP_SYS_PTRACE
434 r
= read_one_line_file("/run/systemd/container", &m
);
437 /* Fallback for cases where PID 1 was not
438 * systemd (for example, cases where
439 * init=/bin/sh is used. */
441 r
= getenv_for_pid(1, "container", &m
);
444 /* If that didn't work, give up,
445 * assume no container manager.
447 * Note: This means we still cannot
448 * detect containers if init=/bin/sh
449 * is passed but privileges dropped,
450 * as /proc/1/environ is only readable
451 * with privileges. */
453 r
= VIRTUALIZATION_NONE
;
463 for (j
= 0; j
< ELEMENTSOF(value_table
); j
++)
464 if (streq(e
, value_table
[j
].value
)) {
465 r
= value_table
[j
].id
;
469 r
= VIRTUALIZATION_CONTAINER_OTHER
;
472 log_debug("Found container virtualization %s", virtualization_to_string(r
));
477 int detect_virtualization(void) {
480 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_BHYVE
] = "bhyve",
510 [VIRTUALIZATION_VM_OTHER
] = "vm-other",
512 [VIRTUALIZATION_SYSTEMD_NSPAWN
] = "systemd-nspawn",
513 [VIRTUALIZATION_LXC_LIBVIRT
] = "lxc-libvirt",
514 [VIRTUALIZATION_LXC
] = "lxc",
515 [VIRTUALIZATION_OPENVZ
] = "openvz",
516 [VIRTUALIZATION_DOCKER
] = "docker",
517 [VIRTUALIZATION_RKT
] = "rkt",
518 [VIRTUALIZATION_CONTAINER_OTHER
] = "container-other",
521 DEFINE_STRING_TABLE_LOOKUP(virtualization
, int);