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