]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/virt.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / basic / virt.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #if defined(__i386__) || defined(__x86_64__)
4 #include <cpuid.h>
5 #endif
6 #include <errno.h>
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11
12 #include "alloc-util.h"
13 #include "dirent-util.h"
14 #include "env-util.h"
15 #include "fd-util.h"
16 #include "fileio.h"
17 #include "macro.h"
18 #include "process-util.h"
19 #include "stat-util.h"
20 #include "string-table.h"
21 #include "string-util.h"
22 #include "virt.h"
23
24 static int detect_vm_cpuid(void) {
25
26 /* CPUID is an x86 specific interface. */
27 #if defined(__i386__) || defined(__x86_64__)
28
29 static const struct {
30 const char *cpuid;
31 int id;
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 },
43 };
44
45 uint32_t eax, ebx, ecx, edx;
46 bool hypervisor;
47
48 /* http://lwn.net/Articles/301888/ */
49
50 /* First detect whether there is a hypervisor */
51 if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) == 0)
52 return VIRTUALIZATION_NONE;
53
54 hypervisor = ecx & 0x80000000U;
55
56 if (hypervisor) {
57 union {
58 uint32_t sig32[3];
59 char text[13];
60 } sig = {};
61 unsigned j;
62
63 /* There is a hypervisor, see what it is */
64 __cpuid(0x40000000U, eax, ebx, ecx, edx);
65
66 sig.sig32[0] = ebx;
67 sig.sig32[1] = ecx;
68 sig.sig32[2] = edx;
69
70 log_debug("Virtualization found, CPUID=%s", sig.text);
71
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;
75
76 return VIRTUALIZATION_VM_OTHER;
77 }
78 #endif
79 log_debug("No virtualization found in CPUID");
80
81 return VIRTUALIZATION_NONE;
82 }
83
84 static int detect_vm_device_tree(void) {
85 #if defined(__arm__) || defined(__aarch64__) || defined(__powerpc__) || defined(__powerpc64__)
86 _cleanup_free_ char *hvtype = NULL;
87 int r;
88
89 r = read_one_line_file("/proc/device-tree/hypervisor/compatible", &hvtype);
90 if (r == -ENOENT) {
91 _cleanup_closedir_ DIR *dir = NULL;
92 struct dirent *dent;
93
94 dir = opendir("/proc/device-tree");
95 if (!dir) {
96 if (errno == ENOENT) {
97 log_debug_errno(errno, "/proc/device-tree: %m");
98 return VIRTUALIZATION_NONE;
99 }
100 return -errno;
101 }
102
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;
107 }
108
109 log_debug("No virtualization found in /proc/device-tree/*");
110 return VIRTUALIZATION_NONE;
111 } else if (r < 0)
112 return r;
113
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;
119 else
120 return VIRTUALIZATION_VM_OTHER;
121 #else
122 log_debug("This platform does not support /proc/device-tree");
123 return VIRTUALIZATION_NONE;
124 #endif
125 }
126
127 static int detect_vm_dmi(void) {
128 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
129
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"
135 };
136
137 static const struct {
138 const char *vendor;
139 int id;
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 },
152 };
153 unsigned i;
154 int r;
155
156 for (i = 0; i < ELEMENTSOF(dmi_vendors); i++) {
157 _cleanup_free_ char *s = NULL;
158 unsigned j;
159
160 r = read_one_line_file(dmi_vendors[i], &s);
161 if (r < 0) {
162 if (r == -ENOENT)
163 continue;
164
165 return r;
166 }
167
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;
172 }
173 }
174 #endif
175
176 log_debug("No virtualization found in DMI");
177
178 return VIRTUALIZATION_NONE;
179 }
180
181 static int detect_vm_xen(void) {
182
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;
188 }
189
190 log_debug("Virtualization XEN found (/proc/xen exists)");
191 return VIRTUALIZATION_XEN;
192 }
193
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;
199 char *cap, *i;
200 int r;
201
202 r = read_one_line_file(PATH_FEATURES, &domcap);
203 if (r < 0 && r != -ENOENT)
204 return r;
205 if (r >= 0) {
206 unsigned long features;
207
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);
211 if (r == 1) {
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");
216 return r;
217 }
218 log_debug("Virtualization XEN, found %s, unhandled content '%s'",
219 PATH_FEATURES, domcap);
220 }
221
222 r = read_one_line_file("/proc/xen/capabilities", &domcap);
223 if (r == -ENOENT) {
224 log_debug("Virtualization XEN because /proc/xen/capabilities does not exist");
225 return 0;
226 }
227 if (r < 0)
228 return r;
229
230 i = domcap;
231 while ((cap = strsep(&i, ",")))
232 if (streq(cap, "control_d"))
233 break;
234 if (!cap) {
235 log_debug("Virtualization XEN DomU found (/proc/xen/capabilites)");
236 return 0;
237 }
238
239 log_debug("Virtualization XEN Dom0 ignored (/proc/xen/capabilities)");
240 return 1;
241 }
242
243 static int detect_vm_hypervisor(void) {
244 _cleanup_free_ char *hvtype = NULL;
245 int r;
246
247 r = read_one_line_file("/sys/hypervisor/type", &hvtype);
248 if (r == -ENOENT)
249 return VIRTUALIZATION_NONE;
250 if (r < 0)
251 return r;
252
253 log_debug("Virtualization %s found in /sys/hypervisor/type", hvtype);
254
255 if (streq(hvtype, "xen"))
256 return VIRTUALIZATION_XEN;
257 else
258 return VIRTUALIZATION_VM_OTHER;
259 }
260
261 static int detect_vm_uml(void) {
262 _cleanup_fclose_ FILE *f = NULL;
263 int r;
264
265 /* Detect User-Mode Linux by reading /proc/cpuinfo */
266 f = fopen("/proc/cpuinfo", "re");
267 if (!f) {
268 if (errno == ENOENT) {
269 log_debug("/proc/cpuinfo not found, assuming no UML virtualization.");
270 return VIRTUALIZATION_NONE;
271 }
272 return -errno;
273 }
274
275 for (;;) {
276 _cleanup_free_ char *line = NULL;
277 const char *t;
278
279 r = read_line(f, LONG_LINE_MAX, &line);
280 if (r < 0)
281 return r;
282 if (r == 0)
283 break;
284
285 t = startswith(line, "vendor_id\t: ");
286 if (t) {
287 if (startswith(t, "User Mode Linux")) {
288 log_debug("UML virtualization found in /proc/cpuinfo");
289 return VIRTUALIZATION_UML;
290 }
291
292 break;
293 }
294 }
295
296 log_debug("UML virtualization not found in /proc/cpuinfo.");
297 return VIRTUALIZATION_NONE;
298 }
299
300 static int detect_vm_zvm(void) {
301
302 #if defined(__s390__)
303 _cleanup_free_ char *t = NULL;
304 int r;
305
306 r = get_proc_field("/proc/sysinfo", "VM00 Control Program", WHITESPACE, &t);
307 if (r == -ENOENT)
308 return VIRTUALIZATION_NONE;
309 if (r < 0)
310 return r;
311
312 log_debug("Virtualization %s found in /proc/sysinfo", t);
313 if (streq(t, "z/VM"))
314 return VIRTUALIZATION_ZVM;
315 else
316 return VIRTUALIZATION_KVM;
317 #else
318 log_debug("This platform does not support /proc/sysinfo");
319 return VIRTUALIZATION_NONE;
320 #endif
321 }
322
323 /* Returns a short identifier for the various VM implementations */
324 int detect_vm(void) {
325 static thread_local int cached_found = _VIRTUALIZATION_INVALID;
326 bool other = false;
327 int r, dmi;
328
329 if (cached_found >= 0)
330 return cached_found;
331
332 /* We have to use the correct order here:
333 *
334 * → First, try to detect Oracle Virtualbox, even if it uses KVM, as well as Xen even if it cloaks as Microsoft
335 * Hyper-V.
336 *
337 * → Second, try to detect from CPUID, this will report KVM for whatever software is used even if info in DMI is
338 * overwritten.
339 *
340 * → Third, try to detect from DMI. */
341
342 dmi = detect_vm_dmi();
343 if (IN_SET(dmi, VIRTUALIZATION_ORACLE, VIRTUALIZATION_XEN)) {
344 r = dmi;
345 goto finish;
346 }
347
348 r = detect_vm_cpuid();
349 if (r < 0)
350 return r;
351 if (r == VIRTUALIZATION_VM_OTHER)
352 other = true;
353 else if (r != VIRTUALIZATION_NONE)
354 goto finish;
355
356 /* Now, let's get back to DMI */
357 if (dmi < 0)
358 return dmi;
359 if (dmi == VIRTUALIZATION_VM_OTHER)
360 other = true;
361 else if (dmi != VIRTUALIZATION_NONE) {
362 r = dmi;
363 goto finish;
364 }
365
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.
370 */
371
372 r = detect_vm_xen();
373 if (r < 0)
374 return r;
375 if (r == VIRTUALIZATION_VM_OTHER)
376 other = true;
377 else if (r != VIRTUALIZATION_NONE)
378 goto finish;
379
380 r = detect_vm_hypervisor();
381 if (r < 0)
382 return r;
383 if (r == VIRTUALIZATION_VM_OTHER)
384 other = true;
385 else if (r != VIRTUALIZATION_NONE)
386 goto finish;
387
388 r = detect_vm_device_tree();
389 if (r < 0)
390 return r;
391 if (r == VIRTUALIZATION_VM_OTHER)
392 other = true;
393 else if (r != VIRTUALIZATION_NONE)
394 goto finish;
395
396 r = detect_vm_uml();
397 if (r < 0)
398 return r;
399 if (r == VIRTUALIZATION_VM_OTHER)
400 other = true;
401 else if (r != VIRTUALIZATION_NONE)
402 goto finish;
403
404 r = detect_vm_zvm();
405 if (r < 0)
406 return r;
407
408 finish:
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
411 * double-check it */
412 if (r == VIRTUALIZATION_XEN) {
413 int dom0;
414
415 dom0 = detect_vm_xen_dom0();
416 if (dom0 < 0)
417 return dom0;
418 if (dom0 > 0)
419 r = VIRTUALIZATION_NONE;
420 } else if (r == VIRTUALIZATION_NONE && other)
421 r = VIRTUALIZATION_VM_OTHER;
422
423 cached_found = r;
424 log_debug("Found VM virtualization %s", virtualization_to_string(r));
425 return r;
426 }
427
428 int detect_container(void) {
429
430 static const struct {
431 const char *value;
432 int id;
433 } value_table[] = {
434 { "lxc", VIRTUALIZATION_LXC },
435 { "lxc-libvirt", VIRTUALIZATION_LXC_LIBVIRT },
436 { "systemd-nspawn", VIRTUALIZATION_SYSTEMD_NSPAWN },
437 { "docker", VIRTUALIZATION_DOCKER },
438 { "rkt", VIRTUALIZATION_RKT },
439 };
440
441 static thread_local int cached_found = _VIRTUALIZATION_INVALID;
442 _cleanup_free_ char *m = NULL;
443 const char *e = NULL;
444 unsigned j;
445 int r;
446
447 if (cached_found >= 0)
448 return cached_found;
449
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;
454 goto finish;
455 }
456
457 if (getpid_cached() == 1) {
458 /* If we are PID 1 we can just check our own environment variable, and that's authoritative. */
459
460 e = getenv("container");
461 if (isempty(e)) {
462 r = VIRTUALIZATION_NONE;
463 goto finish;
464 }
465
466 goto translate_name;
467 }
468
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);
472 if (r > 0) {
473 e = m;
474 goto translate_name;
475 }
476 if (!IN_SET(r, -ENOENT, 0))
477 return log_debug_errno(r, "Failed to read /run/systemd/container: %m");
478
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);
481 if (r > 0) {
482 e = m;
483 goto translate_name;
484 }
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");
487
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);
491 if (r >= 0) {
492 const char *t;
493
494 t = strrchr(m, '(');
495 if (!t)
496 return -EIO;
497
498 if (!startswith(t, "(1,")) {
499 r = VIRTUALIZATION_CONTAINER_OTHER;
500 goto finish;
501 }
502 } else if (r != -ENOENT)
503 return r;
504
505 /* If that didn't work, give up, assume no container manager. */
506 r = VIRTUALIZATION_NONE;
507 goto finish;
508
509 translate_name:
510 for (j = 0; j < ELEMENTSOF(value_table); j++)
511 if (streq(e, value_table[j].value)) {
512 r = value_table[j].id;
513 goto finish;
514 }
515
516 r = VIRTUALIZATION_CONTAINER_OTHER;
517
518 finish:
519 log_debug("Found container virtualization %s.", virtualization_to_string(r));
520 cached_found = r;
521 return r;
522 }
523
524 int detect_virtualization(void) {
525 int r;
526
527 r = detect_container();
528 if (r == 0)
529 r = detect_vm();
530
531 return r;
532 }
533
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;
538 ssize_t n;
539 uint32_t a, b, c;
540 int r;
541
542 f = fopen(name, "re");
543 if (!f) {
544 log_debug_errno(errno, "Failed to open %s: %m", name);
545 return errno == ENOENT ? false : -errno;
546 }
547
548 n = getline(&buf, &n_allocated, f);
549 if (n < 0) {
550 if (feof(f)) {
551 log_debug("%s is empty, we're in an uninitialized user namespace", name);
552 return true;
553 }
554
555 return log_debug_errno(errno, "Failed to read %s: %m", name);
556 }
557
558 r = sscanf(buf, "%"PRIu32" %"PRIu32" %"PRIu32, &a, &b, &c);
559 if (r < 3)
560 return log_debug_errno(errno, "Failed to parse %s: %m", name);
561
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);
565 return false;
566 }
567
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);
570 return true;
571 }
572
573 int running_in_userns(void) {
574 _cleanup_free_ char *line = NULL;
575 int r;
576
577 r = userns_has_mapping("/proc/self/uid_map");
578 if (r != 0)
579 return r;
580
581 r = userns_has_mapping("/proc/self/gid_map");
582 if (r != 0)
583 return r;
584
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.
590 */
591 r = read_one_line_file("/proc/self/setgroups", &line);
592 if (r < 0) {
593 log_debug_errno(r, "/proc/self/setgroups: %m");
594 return r == -ENOENT ? false : r;
595 }
596
597 truncate_nl(line);
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");
601 return r;
602 }
603
604 int running_in_chroot(void) {
605 int r;
606
607 if (getenv_bool("SYSTEMD_IGNORE_CHROOT") > 0)
608 return 0;
609
610 r = files_same("/proc/1/root", "/", 0);
611 if (r < 0)
612 return r;
613
614 return r == 0;
615 }
616
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",
632
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",
640 };
641
642 DEFINE_STRING_TABLE_LOOKUP(virtualization, int);