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