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
},
57 /* http://lwn.net/Articles/301888/ */
59 #if defined (__i386__)
62 #elif defined (__amd64__)
67 /* First detect whether there is a hypervisor */
69 __asm__
__volatile__ (
70 /* ebx/rbx is being used for PIC! */
71 " push %%"REG_b
" \n\t"
75 : "=a" (eax
), "=c" (ecx
)
79 hypervisor
= !!(ecx
& 0x80000000U
);
88 /* There is a hypervisor, see what it is */
90 __asm__
__volatile__ (
91 /* ebx/rbx is being used for PIC! */
92 " push %%"REG_b
" \n\t"
97 : "=a" (eax
), "=r" (sig
.sig32
[0]), "=c" (sig
.sig32
[1]), "=d" (sig
.sig32
[2])
101 log_debug("Virtualization found, CPUID=%s", sig
.text
);
103 for (j
= 0; j
< ELEMENTSOF(cpuid_vendor_table
); j
++)
104 if (streq(sig
.text
, cpuid_vendor_table
[j
].cpuid
))
105 return cpuid_vendor_table
[j
].id
;
107 return VIRTUALIZATION_VM_OTHER
;
110 log_debug("No virtualization found in CPUID");
112 return VIRTUALIZATION_NONE
;
115 static int detect_vm_device_tree(void) {
116 #if defined(__arm__) || defined(__aarch64__) || defined(__powerpc__) || defined(__powerpc64__)
117 _cleanup_free_
char *hvtype
= NULL
;
120 r
= read_one_line_file("/proc/device-tree/hypervisor/compatible", &hvtype
);
122 _cleanup_closedir_
DIR *dir
= NULL
;
125 dir
= opendir("/proc/device-tree");
127 if (errno
== ENOENT
) {
128 log_debug_errno(errno
, "/proc/device-tree: %m");
129 return VIRTUALIZATION_NONE
;
134 FOREACH_DIRENT(dent
, dir
, return -errno
)
135 if (strstr(dent
->d_name
, "fw-cfg")) {
136 log_debug("Virtualization QEMU: \"fw-cfg\" present in /proc/device-tree/%s", dent
->d_name
);
137 return VIRTUALIZATION_QEMU
;
140 log_debug("No virtualization found in /proc/device-tree/*");
141 return VIRTUALIZATION_NONE
;
145 log_debug("Virtualization %s found in /proc/device-tree/hypervisor/compatible", hvtype
);
146 if (streq(hvtype
, "linux,kvm"))
147 return VIRTUALIZATION_KVM
;
148 else if (strstr(hvtype
, "xen"))
149 return VIRTUALIZATION_XEN
;
151 return VIRTUALIZATION_VM_OTHER
;
153 log_debug("This platform does not support /proc/device-tree");
154 return VIRTUALIZATION_NONE
;
158 static int detect_vm_dmi(void) {
159 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
161 static const char *const dmi_vendors
[] = {
162 "/sys/class/dmi/id/product_name", /* Test this before sys_vendor to detect KVM over QEMU */
163 "/sys/class/dmi/id/sys_vendor",
164 "/sys/class/dmi/id/board_vendor",
165 "/sys/class/dmi/id/bios_vendor"
168 static const struct {
171 } dmi_vendor_table
[] = {
172 { "KVM", VIRTUALIZATION_KVM
},
173 { "QEMU", VIRTUALIZATION_QEMU
},
174 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
175 { "VMware", VIRTUALIZATION_VMWARE
},
176 { "VMW", VIRTUALIZATION_VMWARE
},
177 { "innotek GmbH", VIRTUALIZATION_ORACLE
},
178 { "Xen", VIRTUALIZATION_XEN
},
179 { "Bochs", VIRTUALIZATION_BOCHS
},
180 { "Parallels", VIRTUALIZATION_PARALLELS
},
185 for (i
= 0; i
< ELEMENTSOF(dmi_vendors
); i
++) {
186 _cleanup_free_
char *s
= NULL
;
189 r
= read_one_line_file(dmi_vendors
[i
], &s
);
199 for (j
= 0; j
< ELEMENTSOF(dmi_vendor_table
); j
++)
200 if (startswith(s
, dmi_vendor_table
[j
].vendor
)) {
201 log_debug("Virtualization %s found in DMI (%s)", s
, dmi_vendors
[i
]);
202 return dmi_vendor_table
[j
].id
;
207 log_debug("No virtualization found in DMI");
209 return VIRTUALIZATION_NONE
;
212 static int detect_vm_xen(void) {
213 /* Check for Dom0 will be executed later in detect_vm_xen_dom0
214 Thats why we dont check the content of /proc/xen/capabilities here. */
215 if (access("/proc/xen/capabilities", F_OK
) < 0) {
216 log_debug("Virtualization XEN not found, /proc/xen/capabilities does not exist");
217 return VIRTUALIZATION_NONE
;
220 log_debug("Virtualization XEN found (/proc/xen/capabilities exists)");
221 return VIRTUALIZATION_XEN
;
225 static bool detect_vm_xen_dom0(void) {
226 _cleanup_free_
char *domcap
= NULL
;
230 r
= read_one_line_file("/proc/xen/capabilities", &domcap
);
232 log_debug("Virtualization XEN not found, /proc/xen/capabilities does not exist");
239 while ((cap
= strsep(&i
, ",")))
240 if (streq(cap
, "control_d"))
243 log_debug("Virtualization XEN DomU found (/proc/xen/capabilites)");
247 log_debug("Virtualization XEN Dom0 ignored (/proc/xen/capabilities)");
251 static int detect_vm_hypervisor(void) {
252 _cleanup_free_
char *hvtype
= NULL
;
255 r
= read_one_line_file("/sys/hypervisor/type", &hvtype
);
257 return VIRTUALIZATION_NONE
;
261 log_debug("Virtualization %s found in /sys/hypervisor/type", hvtype
);
263 if (streq(hvtype
, "xen"))
264 return VIRTUALIZATION_XEN
;
266 return VIRTUALIZATION_VM_OTHER
;
269 static int detect_vm_uml(void) {
270 _cleanup_free_
char *cpuinfo_contents
= NULL
;
273 /* Detect User-Mode Linux by reading /proc/cpuinfo */
274 r
= read_full_file("/proc/cpuinfo", &cpuinfo_contents
, NULL
);
278 if (strstr(cpuinfo_contents
, "\nvendor_id\t: User Mode Linux\n")) {
279 log_debug("UML virtualization found in /proc/cpuinfo");
280 return VIRTUALIZATION_UML
;
283 log_debug("No virtualization found in /proc/cpuinfo.");
284 return VIRTUALIZATION_NONE
;
287 static int detect_vm_zvm(void) {
289 #if defined(__s390__)
290 _cleanup_free_
char *t
= NULL
;
293 r
= get_proc_field("/proc/sysinfo", "VM00 Control Program", WHITESPACE
, &t
);
295 return VIRTUALIZATION_NONE
;
299 log_debug("Virtualization %s found in /proc/sysinfo", t
);
300 if (streq(t
, "z/VM"))
301 return VIRTUALIZATION_ZVM
;
303 return VIRTUALIZATION_KVM
;
305 log_debug("This platform does not support /proc/sysinfo");
306 return VIRTUALIZATION_NONE
;
310 /* Returns a short identifier for the various VM implementations */
311 int detect_vm(void) {
312 static thread_local
int cached_found
= _VIRTUALIZATION_INVALID
;
315 if (cached_found
>= 0)
318 /* We have to use the correct order here:
319 * Some virtualization technologies do use KVM hypervisor but are
320 * expected to be detected as something else. So detect DMI first.
322 * An example is Virtualbox since version 5.0, which uses KVM backend.
323 * Detection via DMI works corretly, the CPU ID would find KVM
328 if (r
!= VIRTUALIZATION_NONE
)
331 r
= detect_vm_cpuid();
334 if (r
!= VIRTUALIZATION_NONE
)
337 /* x86 xen will most likely be detected by cpuid. If not (most likely
338 * because we're not an x86 guest), then we should try the xen capabilities
339 * file next. If that's not found, then we check for the high-level
340 * hypervisor sysfs file:
342 * https://bugs.freedesktop.org/show_bug.cgi?id=77271 */
347 if (r
!= VIRTUALIZATION_NONE
)
350 r
= detect_vm_hypervisor();
353 if (r
!= VIRTUALIZATION_NONE
)
356 r
= detect_vm_device_tree();
359 if (r
!= VIRTUALIZATION_NONE
)
365 if (r
!= VIRTUALIZATION_NONE
)
373 /* x86 xen Dom0 is detected as XEN in hypervisor and maybe others.
374 * In order to detect the Dom0 as not virtualization we need to
376 if (r
== VIRTUALIZATION_XEN
&& detect_vm_xen_dom0())
377 r
= VIRTUALIZATION_NONE
;
380 log_debug("Found VM virtualization %s", virtualization_to_string(r
));
384 int detect_container(void) {
386 static const struct {
390 { "lxc", VIRTUALIZATION_LXC
},
391 { "lxc-libvirt", VIRTUALIZATION_LXC_LIBVIRT
},
392 { "systemd-nspawn", VIRTUALIZATION_SYSTEMD_NSPAWN
},
393 { "docker", VIRTUALIZATION_DOCKER
},
394 { "rkt", VIRTUALIZATION_RKT
},
397 static thread_local
int cached_found
= _VIRTUALIZATION_INVALID
;
398 _cleanup_free_
char *m
= NULL
;
399 const char *e
= NULL
;
403 if (cached_found
>= 0)
406 /* /proc/vz exists in container and outside of the container,
407 * /proc/bc only outside of the container. */
408 if (access("/proc/vz", F_OK
) >= 0 &&
409 access("/proc/bc", F_OK
) < 0) {
410 r
= VIRTUALIZATION_OPENVZ
;
415 /* If we are PID 1 we can just check our own
416 * environment variable */
418 e
= getenv("container");
420 r
= VIRTUALIZATION_NONE
;
425 /* Otherwise, PID 1 dropped this information into a
426 * file in /run. This is better than accessing
427 * /proc/1/environ, since we don't need CAP_SYS_PTRACE
430 r
= read_one_line_file("/run/systemd/container", &m
);
433 /* Fallback for cases where PID 1 was not
434 * systemd (for example, cases where
435 * init=/bin/sh is used. */
437 r
= getenv_for_pid(1, "container", &m
);
440 /* If that didn't work, give up,
441 * assume no container manager.
443 * Note: This means we still cannot
444 * detect containers if init=/bin/sh
445 * is passed but privileges dropped,
446 * as /proc/1/environ is only readable
447 * with privileges. */
449 r
= VIRTUALIZATION_NONE
;
459 for (j
= 0; j
< ELEMENTSOF(value_table
); j
++)
460 if (streq(e
, value_table
[j
].value
)) {
461 r
= value_table
[j
].id
;
465 r
= VIRTUALIZATION_CONTAINER_OTHER
;
468 log_debug("Found container virtualization %s", virtualization_to_string(r
));
473 int detect_virtualization(void) {
476 r
= detect_container();
483 int running_in_chroot(void) {
486 ret
= files_same("/proc/1/root", "/");
493 static const char *const virtualization_table
[_VIRTUALIZATION_MAX
] = {
494 [VIRTUALIZATION_NONE
] = "none",
495 [VIRTUALIZATION_KVM
] = "kvm",
496 [VIRTUALIZATION_QEMU
] = "qemu",
497 [VIRTUALIZATION_BOCHS
] = "bochs",
498 [VIRTUALIZATION_XEN
] = "xen",
499 [VIRTUALIZATION_UML
] = "uml",
500 [VIRTUALIZATION_VMWARE
] = "vmware",
501 [VIRTUALIZATION_ORACLE
] = "oracle",
502 [VIRTUALIZATION_MICROSOFT
] = "microsoft",
503 [VIRTUALIZATION_ZVM
] = "zvm",
504 [VIRTUALIZATION_PARALLELS
] = "parallels",
505 [VIRTUALIZATION_VM_OTHER
] = "vm-other",
507 [VIRTUALIZATION_SYSTEMD_NSPAWN
] = "systemd-nspawn",
508 [VIRTUALIZATION_LXC_LIBVIRT
] = "lxc-libvirt",
509 [VIRTUALIZATION_LXC
] = "lxc",
510 [VIRTUALIZATION_OPENVZ
] = "openvz",
511 [VIRTUALIZATION_DOCKER
] = "docker",
512 [VIRTUALIZATION_RKT
] = "rkt",
513 [VIRTUALIZATION_CONTAINER_OTHER
] = "container-other",
516 DEFINE_STRING_TABLE_LOOKUP(virtualization
, int);