]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/virt.c
detect-virt: also detect "microsoft" as WSL
[thirdparty/systemd.git] / src / basic / virt.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
b52aae1d 2
d31b0033
MG
3#if defined(__i386__) || defined(__x86_64__)
4#include <cpuid.h>
5#endif
b52aae1d 6#include <errno.h>
11c3a366
TA
7#include <stdint.h>
8#include <stdlib.h>
b52aae1d
LP
9#include <unistd.h>
10
b5efdb8a 11#include "alloc-util.h"
ade61d3b 12#include "dirent-util.h"
295ee984 13#include "env-util.h"
ade61d3b 14#include "fd-util.h"
07630cea 15#include "fileio.h"
11c3a366 16#include "macro.h"
93cc7779 17#include "process-util.h"
b5efdb8a 18#include "stat-util.h"
8b43440b 19#include "string-table.h"
07630cea 20#include "string-util.h"
b52aae1d
LP
21#include "virt.h"
22
680120bb 23#if defined(__i386__) || defined(__x86_64__)
735ea55f
YW
24static const char *const vm_table[_VIRTUALIZATION_MAX] = {
25 [VIRTUALIZATION_XEN] = "XenVMMXenVMM",
26 [VIRTUALIZATION_KVM] = "KVMKVMKVM",
27 [VIRTUALIZATION_QEMU] = "TCGTCGTCGTCG",
28 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
29 [VIRTUALIZATION_VMWARE] = "VMwareVMware",
30 /* https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfs */
31 [VIRTUALIZATION_MICROSOFT] = "Microsoft Hv",
32 /* https://wiki.freebsd.org/bhyve */
33 [VIRTUALIZATION_BHYVE] = "bhyve bhyve ",
34 [VIRTUALIZATION_QNX] = "QNXQVMBSQG",
35 /* https://projectacrn.org */
36 [VIRTUALIZATION_ACRN] = "ACRNACRNACRN",
37};
38
39DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(vm, int);
680120bb 40#endif
735ea55f 41
75f86906 42static int detect_vm_cpuid(void) {
b52aae1d 43
2ef8a4c4 44 /* CPUID is an x86 specific interface. */
bdb628ee 45#if defined(__i386__) || defined(__x86_64__)
b52aae1d 46
d31b0033 47 uint32_t eax, ebx, ecx, edx;
b52aae1d
LP
48 bool hypervisor;
49
50 /* http://lwn.net/Articles/301888/ */
b52aae1d 51
b52aae1d 52 /* First detect whether there is a hypervisor */
d31b0033
MG
53 if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) == 0)
54 return VIRTUALIZATION_NONE;
b52aae1d 55
5d904a6a 56 hypervisor = ecx & 0x80000000U;
b52aae1d
LP
57
58 if (hypervisor) {
75f86906
LP
59 union {
60 uint32_t sig32[3];
61 char text[13];
62 } sig = {};
735ea55f 63 int v;
b52aae1d
LP
64
65 /* There is a hypervisor, see what it is */
8481e3e7 66 __cpuid(0x40000000U, eax, ebx, ecx, edx);
d31b0033
MG
67
68 sig.sig32[0] = ebx;
69 sig.sig32[1] = ecx;
70 sig.sig32[2] = edx;
b52aae1d 71
9f63a08d
SS
72 log_debug("Virtualization found, CPUID=%s", sig.text);
73
735ea55f
YW
74 v = vm_from_string(sig.text);
75 if (v < 0)
76 return VIRTUALIZATION_VM_OTHER;
bdb628ee 77
735ea55f 78 return v;
b52aae1d 79 }
bdb628ee 80#endif
9f63a08d 81 log_debug("No virtualization found in CPUID");
bdb628ee 82
75f86906 83 return VIRTUALIZATION_NONE;
bdb628ee
ZJS
84}
85
75f86906 86static int detect_vm_device_tree(void) {
db6a8689 87#if defined(__arm__) || defined(__aarch64__) || defined(__powerpc__) || defined(__powerpc64__)
d831deb5
CA
88 _cleanup_free_ char *hvtype = NULL;
89 int r;
90
b8f1df82 91 r = read_one_line_file("/proc/device-tree/hypervisor/compatible", &hvtype);
75f86906 92 if (r == -ENOENT) {
ce09c71d
AJ
93 _cleanup_closedir_ DIR *dir = NULL;
94 struct dirent *dent;
95
96 dir = opendir("/proc/device-tree");
97 if (!dir) {
9f63a08d
SS
98 if (errno == ENOENT) {
99 log_debug_errno(errno, "/proc/device-tree: %m");
75f86906 100 return VIRTUALIZATION_NONE;
9f63a08d 101 }
ce09c71d
AJ
102 return -errno;
103 }
104
75f86906 105 FOREACH_DIRENT(dent, dir, return -errno)
9f63a08d
SS
106 if (strstr(dent->d_name, "fw-cfg")) {
107 log_debug("Virtualization QEMU: \"fw-cfg\" present in /proc/device-tree/%s", dent->d_name);
75f86906 108 return VIRTUALIZATION_QEMU;
9f63a08d 109 }
75f86906 110
9f63a08d 111 log_debug("No virtualization found in /proc/device-tree/*");
75f86906
LP
112 return VIRTUALIZATION_NONE;
113 } else if (r < 0)
114 return r;
115
9f63a08d 116 log_debug("Virtualization %s found in /proc/device-tree/hypervisor/compatible", hvtype);
75f86906
LP
117 if (streq(hvtype, "linux,kvm"))
118 return VIRTUALIZATION_KVM;
119 else if (strstr(hvtype, "xen"))
120 return VIRTUALIZATION_XEN;
4d4ac92c
CL
121 else if (strstr(hvtype, "vmware"))
122 return VIRTUALIZATION_VMWARE;
75f86906
LP
123 else
124 return VIRTUALIZATION_VM_OTHER;
125#else
9f63a08d 126 log_debug("This platform does not support /proc/device-tree");
75f86906 127 return VIRTUALIZATION_NONE;
d831deb5 128#endif
d831deb5
CA
129}
130
75f86906 131static int detect_vm_dmi(void) {
2ef8a4c4 132#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
bdb628ee
ZJS
133
134 static const char *const dmi_vendors[] = {
3728dcde 135 "/sys/class/dmi/id/product_name", /* Test this before sys_vendor to detect KVM over QEMU */
bdb628ee
ZJS
136 "/sys/class/dmi/id/sys_vendor",
137 "/sys/class/dmi/id/board_vendor",
138 "/sys/class/dmi/id/bios_vendor"
139 };
140
75f86906
LP
141 static const struct {
142 const char *vendor;
143 int id;
144 } dmi_vendor_table[] = {
87bc4b40 145 { "KVM", VIRTUALIZATION_KVM },
25454a0c 146 { "QEMU", VIRTUALIZATION_QEMU },
a86cbb0f 147 { "VMware", VIRTUALIZATION_VMWARE }, /* https://kb.vmware.com/s/article/1009458 */
87bc4b40
JL
148 { "VMW", VIRTUALIZATION_VMWARE },
149 { "innotek GmbH", VIRTUALIZATION_ORACLE },
150 { "Oracle Corporation", VIRTUALIZATION_ORACLE },
151 { "Xen", VIRTUALIZATION_XEN },
152 { "Bochs", VIRTUALIZATION_BOCHS },
153 { "Parallels", VIRTUALIZATION_PARALLELS },
aa0c3427 154 /* https://wiki.freebsd.org/bhyve */
87bc4b40 155 { "BHYVE", VIRTUALIZATION_BHYVE },
75f86906 156 };
bdb628ee 157 unsigned i;
75f86906 158 int r;
b52aae1d
LP
159
160 for (i = 0; i < ELEMENTSOF(dmi_vendors); i++) {
b1b8e816 161 _cleanup_free_ char *s = NULL;
75f86906 162 unsigned j;
b52aae1d 163
b1b8e816
LP
164 r = read_one_line_file(dmi_vendors[i], &s);
165 if (r < 0) {
75f86906
LP
166 if (r == -ENOENT)
167 continue;
b52aae1d 168
75f86906 169 return r;
b52aae1d
LP
170 }
171
75f86906 172 for (j = 0; j < ELEMENTSOF(dmi_vendor_table); j++)
9f63a08d
SS
173 if (startswith(s, dmi_vendor_table[j].vendor)) {
174 log_debug("Virtualization %s found in DMI (%s)", s, dmi_vendors[i]);
75f86906 175 return dmi_vendor_table[j].id;
9f63a08d 176 }
b52aae1d 177 }
bdb628ee
ZJS
178#endif
179
9f63a08d
SS
180 log_debug("No virtualization found in DMI");
181
75f86906 182 return VIRTUALIZATION_NONE;
bdb628ee
ZJS
183}
184
75f86906 185static int detect_vm_xen(void) {
45e1c7d5 186
3f61278b 187 /* Check for Dom0 will be executed later in detect_vm_xen_dom0
87dc723a
OH
188 The presence of /proc/xen indicates some form of a Xen domain */
189 if (access("/proc/xen", F_OK) < 0) {
190 log_debug("Virtualization XEN not found, /proc/xen does not exist");
3f61278b
SS
191 return VIRTUALIZATION_NONE;
192 }
193
87dc723a 194 log_debug("Virtualization XEN found (/proc/xen exists)");
45e1c7d5 195 return VIRTUALIZATION_XEN;
3f61278b
SS
196}
197
575e6588
OH
198#define XENFEAT_dom0 11 /* xen/include/public/features.h */
199#define PATH_FEATURES "/sys/hypervisor/properties/features"
1a8e4148
OH
200/* Returns -errno, or 0 for domU, or 1 for dom0 */
201static int detect_vm_xen_dom0(void) {
75f86906 202 _cleanup_free_ char *domcap = NULL;
bdb628ee 203 int r;
b52aae1d 204
575e6588
OH
205 r = read_one_line_file(PATH_FEATURES, &domcap);
206 if (r < 0 && r != -ENOENT)
207 return r;
d6062e3b 208 if (r >= 0) {
575e6588
OH
209 unsigned long features;
210
47dbb99a
YW
211 /* Here, we need to use sscanf() instead of safe_atoul()
212 * as the string lacks the leading "0x". */
13e0f9fe
OH
213 r = sscanf(domcap, "%lx", &features);
214 if (r == 1) {
575e6588
OH
215 r = !!(features & (1U << XENFEAT_dom0));
216 log_debug("Virtualization XEN, found %s with value %08lx, "
217 "XENFEAT_dom0 (indicating the 'hardware domain') is%s set.",
218 PATH_FEATURES, features, r ? "" : " not");
219 return r;
220 }
221 log_debug("Virtualization XEN, found %s, unhandled content '%s'",
222 PATH_FEATURES, domcap);
223 }
224
75f86906 225 r = read_one_line_file("/proc/xen/capabilities", &domcap);
9f63a08d 226 if (r == -ENOENT) {
1a8e4148
OH
227 log_debug("Virtualization XEN because /proc/xen/capabilities does not exist");
228 return 0;
9f63a08d 229 }
d5b687e7
LP
230 if (r < 0)
231 return r;
bdb628ee 232
31a9be23
YW
233 for (const char *i = domcap;;) {
234 _cleanup_free_ char *cap = NULL;
9f63a08d 235
31a9be23
YW
236 r = extract_first_word(&i, &cap, ",", 0);
237 if (r < 0)
238 return r;
239 if (r == 0) {
240 log_debug("Virtualization XEN DomU found (/proc/xen/capabilities)");
241 return 0;
242 }
243
244 if (streq(cap, "control_d")) {
245 log_debug("Virtualization XEN Dom0 ignored (/proc/xen/capabilities)");
246 return 1;
247 }
248 }
75f86906 249}
37287585 250
75f86906
LP
251static int detect_vm_hypervisor(void) {
252 _cleanup_free_ char *hvtype = NULL;
253 int r;
37287585 254
75f86906
LP
255 r = read_one_line_file("/sys/hypervisor/type", &hvtype);
256 if (r == -ENOENT)
257 return VIRTUALIZATION_NONE;
258 if (r < 0)
259 return r;
37287585 260
9f63a08d
SS
261 log_debug("Virtualization %s found in /sys/hypervisor/type", hvtype);
262
75f86906
LP
263 if (streq(hvtype, "xen"))
264 return VIRTUALIZATION_XEN;
265 else
266 return VIRTUALIZATION_VM_OTHER;
267}
37287585 268
75f86906 269static int detect_vm_uml(void) {
6058516a 270 _cleanup_fclose_ FILE *f = NULL;
75f86906 271 int r;
37287585 272
75f86906 273 /* Detect User-Mode Linux by reading /proc/cpuinfo */
6058516a
ZJS
274 f = fopen("/proc/cpuinfo", "re");
275 if (!f) {
276 if (errno == ENOENT) {
277 log_debug("/proc/cpuinfo not found, assuming no UML virtualization.");
278 return VIRTUALIZATION_NONE;
279 }
280 return -errno;
ef2a48aa 281 }
9f63a08d 282
6058516a
ZJS
283 for (;;) {
284 _cleanup_free_ char *line = NULL;
285 const char *t;
286
287 r = read_line(f, LONG_LINE_MAX, &line);
288 if (r < 0)
289 return r;
290 if (r == 0)
291 break;
292
293 t = startswith(line, "vendor_id\t: ");
294 if (t) {
295 if (startswith(t, "User Mode Linux")) {
296 log_debug("UML virtualization found in /proc/cpuinfo");
297 return VIRTUALIZATION_UML;
298 }
299
300 break;
301 }
9f63a08d 302 }
bdb628ee 303
ef2a48aa 304 log_debug("UML virtualization not found in /proc/cpuinfo.");
75f86906
LP
305 return VIRTUALIZATION_NONE;
306}
e32886e0 307
75f86906 308static int detect_vm_zvm(void) {
bdb628ee 309
75f86906
LP
310#if defined(__s390__)
311 _cleanup_free_ char *t = NULL;
312 int r;
e32886e0 313
c4cd1d4d 314 r = get_proc_field("/proc/sysinfo", "VM00 Control Program", WHITESPACE, &t);
75f86906
LP
315 if (r == -ENOENT)
316 return VIRTUALIZATION_NONE;
317 if (r < 0)
318 return r;
e32886e0 319
9f63a08d 320 log_debug("Virtualization %s found in /proc/sysinfo", t);
75f86906
LP
321 if (streq(t, "z/VM"))
322 return VIRTUALIZATION_ZVM;
323 else
324 return VIRTUALIZATION_KVM;
325#else
9f63a08d 326 log_debug("This platform does not support /proc/sysinfo");
75f86906
LP
327 return VIRTUALIZATION_NONE;
328#endif
329}
e32886e0 330
75f86906
LP
331/* Returns a short identifier for the various VM implementations */
332int detect_vm(void) {
333 static thread_local int cached_found = _VIRTUALIZATION_INVALID;
530c1c30 334 bool other = false;
c2b19b3c 335 int r, dmi;
e32886e0 336
75f86906
LP
337 if (cached_found >= 0)
338 return cached_found;
bdb628ee 339
f6875b0a 340 /* We have to use the correct order here:
f6875b0a 341 *
f2fe2865
LP
342 * → First, try to detect Oracle Virtualbox, even if it uses KVM, as well as Xen even if it cloaks as Microsoft
343 * Hyper-V.
344 *
345 * → Second, try to detect from CPUID, this will report KVM for whatever software is used even if info in DMI is
346 * overwritten.
347 *
348 * → Third, try to detect from DMI. */
5f1c788c 349
28b1a3ea 350 dmi = detect_vm_dmi();
f2fe2865 351 if (IN_SET(dmi, VIRTUALIZATION_ORACLE, VIRTUALIZATION_XEN)) {
2f8e375d
BR
352 r = dmi;
353 goto finish;
354 }
5f1c788c 355
28b1a3ea 356 r = detect_vm_cpuid();
75f86906
LP
357 if (r < 0)
358 return r;
c2b19b3c
LP
359 if (r == VIRTUALIZATION_VM_OTHER)
360 other = true;
361 else if (r != VIRTUALIZATION_NONE)
362 goto finish;
d831deb5 363
c2b19b3c
LP
364 /* Now, let's get back to DMI */
365 if (dmi < 0)
366 return dmi;
367 if (dmi == VIRTUALIZATION_VM_OTHER)
368 other = true;
369 else if (dmi != VIRTUALIZATION_NONE) {
370 r = dmi;
371 goto finish;
530c1c30 372 }
b52aae1d 373
42685451 374 /* x86 xen will most likely be detected by cpuid. If not (most likely
87dc723a
OH
375 * because we're not an x86 guest), then we should try the /proc/xen
376 * directory next. If that's not found, then we check for the high-level
377 * hypervisor sysfs file.
2f5fa62b 378 */
42685451
AJ
379
380 r = detect_vm_xen();
7080ea16
RR
381 if (r < 0)
382 return r;
c2b19b3c
LP
383 if (r == VIRTUALIZATION_VM_OTHER)
384 other = true;
385 else if (r != VIRTUALIZATION_NONE)
386 goto finish;
7080ea16 387
75f86906
LP
388 r = detect_vm_hypervisor();
389 if (r < 0)
390 return r;
c2b19b3c
LP
391 if (r == VIRTUALIZATION_VM_OTHER)
392 other = true;
393 else if (r != VIRTUALIZATION_NONE)
394 goto finish;
f41925b4 395
75f86906
LP
396 r = detect_vm_device_tree();
397 if (r < 0)
398 return r;
c2b19b3c
LP
399 if (r == VIRTUALIZATION_VM_OTHER)
400 other = true;
401 else if (r != VIRTUALIZATION_NONE)
402 goto finish;
f41925b4 403
75f86906
LP
404 r = detect_vm_uml();
405 if (r < 0)
406 return r;
c2b19b3c
LP
407 if (r == VIRTUALIZATION_VM_OTHER)
408 other = true;
409 else if (r != VIRTUALIZATION_NONE)
410 goto finish;
f41925b4 411
75f86906
LP
412 r = detect_vm_zvm();
413 if (r < 0)
414 return r;
0fb533a5
LP
415
416finish:
3f61278b
SS
417 /* x86 xen Dom0 is detected as XEN in hypervisor and maybe others.
418 * In order to detect the Dom0 as not virtualization we need to
419 * double-check it */
1a8e4148 420 if (r == VIRTUALIZATION_XEN) {
c2b19b3c
LP
421 int dom0;
422
423 dom0 = detect_vm_xen_dom0();
424 if (dom0 < 0)
425 return dom0;
426 if (dom0 > 0)
1a8e4148
OH
427 r = VIRTUALIZATION_NONE;
428 } else if (r == VIRTUALIZATION_NONE && other)
530c1c30 429 r = VIRTUALIZATION_VM_OTHER;
3f61278b 430
0fb533a5 431 cached_found = r;
9f63a08d 432 log_debug("Found VM virtualization %s", virtualization_to_string(r));
0fb533a5 433 return r;
b52aae1d
LP
434}
435
735ea55f
YW
436static const char *const container_table[_VIRTUALIZATION_MAX] = {
437 [VIRTUALIZATION_LXC] = "lxc",
438 [VIRTUALIZATION_LXC_LIBVIRT] = "lxc-libvirt",
439 [VIRTUALIZATION_SYSTEMD_NSPAWN] = "systemd-nspawn",
440 [VIRTUALIZATION_DOCKER] = "docker",
441 [VIRTUALIZATION_PODMAN] = "podman",
442 [VIRTUALIZATION_RKT] = "rkt",
443 [VIRTUALIZATION_WSL] = "wsl",
444};
0fb533a5 445
735ea55f
YW
446DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(container, int);
447
448int detect_container(void) {
75f86906 449 static thread_local int cached_found = _VIRTUALIZATION_INVALID;
fdd25311 450 _cleanup_free_ char *m = NULL;
6c8a2c67 451 _cleanup_free_ char *o = NULL;
75f86906 452 const char *e = NULL;
ab94af92 453 int r;
b52aae1d 454
75f86906 455 if (cached_found >= 0)
0fb533a5 456 return cached_found;
0fb533a5 457
8d6e8034 458 /* /proc/vz exists in container and outside of the container, /proc/bc only outside of the container. */
b52aae1d
LP
459 if (access("/proc/vz", F_OK) >= 0 &&
460 access("/proc/bc", F_OK) < 0) {
75f86906 461 r = VIRTUALIZATION_OPENVZ;
0fb533a5 462 goto finish;
b52aae1d
LP
463 }
464
a2f838d5
ZJS
465 /* "Official" way of detecting WSL https://github.com/Microsoft/WSL/issues/423#issuecomment-221627364,
466 * ... and a working one, since the official one doesn't actually work ;(
467 */
6c8a2c67 468 r = read_one_line_file("/proc/sys/kernel/osrelease", &o);
a2f838d5
ZJS
469 if (r >= 0 &&
470 (strstr(o, "Microsoft") || strstr(o, "microsoft") || strstr(o, "WSL"))) {
471 r = VIRTUALIZATION_WSL;
472 goto finish;
6c8a2c67
BR
473 }
474
df0ff127 475 if (getpid_cached() == 1) {
342bed02
ZJS
476 /* If we are PID 1 we can just check our own environment variable, and that's authoritative.
477 * We distinguish three cases:
478 * - the variable is not defined → we jump to other checks
479 * - the variable is defined to an empty value → we are not in a container
480 * - anything else → some container, either one of the known ones or "container-other"
481 */
fdd25311 482 e = getenv("container");
342bed02
ZJS
483 if (!e)
484 goto check_sched;
fdd25311 485 if (isempty(e)) {
75f86906 486 r = VIRTUALIZATION_NONE;
fdd25311
LP
487 goto finish;
488 }
fdd25311 489
8d6e8034
LP
490 goto translate_name;
491 }
492
493 /* Otherwise, PID 1 might have dropped this information into a file in /run. This is better than accessing
494 * /proc/1/environ, since we don't need CAP_SYS_PTRACE for that. */
495 r = read_one_line_file("/run/systemd/container", &m);
a2176045 496 if (r > 0) {
8d6e8034
LP
497 e = m;
498 goto translate_name;
499 }
a2176045 500 if (!IN_SET(r, -ENOENT, 0))
8d6e8034
LP
501 return log_debug_errno(r, "Failed to read /run/systemd/container: %m");
502
503 /* Fallback for cases where PID 1 was not systemd (for example, cases where init=/bin/sh is used. */
504 r = getenv_for_pid(1, "container", &m);
505 if (r > 0) {
fdd25311 506 e = m;
8d6e8034 507 goto translate_name;
fdd25311 508 }
8d6e8034
LP
509 if (r < 0) /* This only works if we have CAP_SYS_PTRACE, hence let's better ignore failures here */
510 log_debug_errno(r, "Failed to read $container of PID 1, ignoring: %m");
511
342bed02
ZJS
512 /* Interestingly /proc/1/sched actually shows the host's PID for what we see as PID 1. If the PID
513 * shown there is not 1, we know we are in a PID namespace and hence a container. */
514 check_sched:
8d6e8034
LP
515 r = read_one_line_file("/proc/1/sched", &m);
516 if (r >= 0) {
517 const char *t;
518
519 t = strrchr(m, '(');
520 if (!t)
521 return -EIO;
522
523 if (!startswith(t, "(1,")) {
524 r = VIRTUALIZATION_CONTAINER_OTHER;
525 goto finish;
526 }
527 } else if (r != -ENOENT)
528 return r;
529
530 /* If that didn't work, give up, assume no container manager. */
531 r = VIRTUALIZATION_NONE;
532 goto finish;
b52aae1d 533
8d6e8034 534translate_name:
735ea55f
YW
535 r = container_from_string(e);
536 if (r < 0)
537 r = VIRTUALIZATION_CONTAINER_OTHER;
fdd25311 538
0fb533a5 539finish:
8d6e8034 540 log_debug("Found container virtualization %s.", virtualization_to_string(r));
0fb533a5 541 cached_found = r;
ab94af92 542 return r;
b52aae1d
LP
543}
544
75f86906 545int detect_virtualization(void) {
b52aae1d 546 int r;
b52aae1d 547
75f86906 548 r = detect_container();
9f63a08d
SS
549 if (r == 0)
550 r = detect_vm();
b52aae1d 551
9f63a08d 552 return r;
b52aae1d 553}
75f86906 554
299a34c1
ZJS
555static int userns_has_mapping(const char *name) {
556 _cleanup_fclose_ FILE *f = NULL;
557 _cleanup_free_ char *buf = NULL;
558 size_t n_allocated = 0;
559 ssize_t n;
560 uint32_t a, b, c;
561 int r;
562
563 f = fopen(name, "re");
564 if (!f) {
565 log_debug_errno(errno, "Failed to open %s: %m", name);
abd67ce7 566 return errno == ENOENT ? false : -errno;
299a34c1
ZJS
567 }
568
569 n = getline(&buf, &n_allocated, f);
570 if (n < 0) {
571 if (feof(f)) {
572 log_debug("%s is empty, we're in an uninitialized user namespace", name);
573 return true;
574 }
575
576 return log_debug_errno(errno, "Failed to read %s: %m", name);
577 }
578
579 r = sscanf(buf, "%"PRIu32" %"PRIu32" %"PRIu32, &a, &b, &c);
580 if (r < 3)
581 return log_debug_errno(errno, "Failed to parse %s: %m", name);
582
583 if (a == 0 && b == 0 && c == UINT32_MAX) {
584 /* The kernel calls mappings_overlap() and does not allow overlaps */
585 log_debug("%s has a full 1:1 mapping", name);
586 return false;
587 }
588
589 /* Anything else implies that we are in a user namespace */
590 log_debug("Mapping found in %s, we're in a user namespace", name);
591 return true;
592}
593
594int running_in_userns(void) {
595 _cleanup_free_ char *line = NULL;
596 int r;
597
598 r = userns_has_mapping("/proc/self/uid_map");
599 if (r != 0)
600 return r;
601
602 r = userns_has_mapping("/proc/self/gid_map");
603 if (r != 0)
604 return r;
605
606 /* "setgroups" file was added in kernel v3.18-rc6-15-g9cc46516dd. It is also
607 * possible to compile a kernel without CONFIG_USER_NS, in which case "setgroups"
608 * also does not exist. We cannot distinguish those two cases, so assume that
609 * we're running on a stripped-down recent kernel, rather than on an old one,
610 * and if the file is not found, return false.
611 */
612 r = read_one_line_file("/proc/self/setgroups", &line);
613 if (r < 0) {
614 log_debug_errno(r, "/proc/self/setgroups: %m");
615 return r == -ENOENT ? false : r;
616 }
617
618 truncate_nl(line);
619 r = streq(line, "deny");
620 /* See user_namespaces(7) for a description of this "setgroups" contents. */
621 log_debug("/proc/self/setgroups contains \"%s\", %s user namespace", line, r ? "in" : "not in");
622 return r;
623}
624
7f4b3c5e 625int running_in_chroot(void) {
ef2a48aa 626 int r;
7f4b3c5e 627
08a28eec
LN
628 if (getenv_bool("SYSTEMD_IGNORE_CHROOT") > 0)
629 return 0;
630
ef2a48aa
ZJS
631 r = files_same("/proc/1/root", "/", 0);
632 if (r < 0)
633 return r;
7f4b3c5e 634
ef2a48aa 635 return r == 0;
7f4b3c5e
LP
636}
637
75f86906
LP
638static const char *const virtualization_table[_VIRTUALIZATION_MAX] = {
639 [VIRTUALIZATION_NONE] = "none",
640 [VIRTUALIZATION_KVM] = "kvm",
641 [VIRTUALIZATION_QEMU] = "qemu",
642 [VIRTUALIZATION_BOCHS] = "bochs",
643 [VIRTUALIZATION_XEN] = "xen",
644 [VIRTUALIZATION_UML] = "uml",
645 [VIRTUALIZATION_VMWARE] = "vmware",
646 [VIRTUALIZATION_ORACLE] = "oracle",
647 [VIRTUALIZATION_MICROSOFT] = "microsoft",
648 [VIRTUALIZATION_ZVM] = "zvm",
649 [VIRTUALIZATION_PARALLELS] = "parallels",
aa0c3427 650 [VIRTUALIZATION_BHYVE] = "bhyve",
1fdf07f5 651 [VIRTUALIZATION_QNX] = "qnx",
095b9cf4 652 [VIRTUALIZATION_ACRN] = "acrn",
75f86906
LP
653 [VIRTUALIZATION_VM_OTHER] = "vm-other",
654
655 [VIRTUALIZATION_SYSTEMD_NSPAWN] = "systemd-nspawn",
656 [VIRTUALIZATION_LXC_LIBVIRT] = "lxc-libvirt",
657 [VIRTUALIZATION_LXC] = "lxc",
658 [VIRTUALIZATION_OPENVZ] = "openvz",
659 [VIRTUALIZATION_DOCKER] = "docker",
90fb1f09 660 [VIRTUALIZATION_PODMAN] = "podman",
9fb16425 661 [VIRTUALIZATION_RKT] = "rkt",
6c8a2c67 662 [VIRTUALIZATION_WSL] = "wsl",
75f86906
LP
663 [VIRTUALIZATION_CONTAINER_OTHER] = "container-other",
664};
665
666DEFINE_STRING_TABLE_LOOKUP(virtualization, int);