]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/virt.c
selinux: use raw variants of security_compute_create and setfscreatecon
[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
ZJS
27#include "dirent-util.h"
28#include "fd-util.h"
07630cea 29#include "fileio.h"
11c3a366 30#include "macro.h"
93cc7779 31#include "process-util.h"
b5efdb8a 32#include "stat-util.h"
8b43440b 33#include "string-table.h"
07630cea 34#include "string-util.h"
b52aae1d
LP
35#include "virt.h"
36
75f86906 37static int detect_vm_cpuid(void) {
b52aae1d 38
2ef8a4c4 39 /* CPUID is an x86 specific interface. */
bdb628ee 40#if defined(__i386__) || defined(__x86_64__)
b52aae1d 41
75f86906
LP
42 static const struct {
43 const char *cpuid;
44 int id;
45 } cpuid_vendor_table[] = {
46 { "XenVMMXenVMM", VIRTUALIZATION_XEN },
47 { "KVMKVMKVM", VIRTUALIZATION_KVM },
b52aae1d 48 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
75f86906 49 { "VMwareVMware", VIRTUALIZATION_VMWARE },
b52aae1d 50 /* http://msdn.microsoft.com/en-us/library/ff542428.aspx */
75f86906
LP
51 { "Microsoft Hv", VIRTUALIZATION_MICROSOFT },
52 };
b52aae1d
LP
53
54 uint32_t eax, ecx;
b52aae1d
LP
55 bool hypervisor;
56
57 /* http://lwn.net/Articles/301888/ */
b52aae1d
LP
58
59#if defined (__i386__)
60#define REG_a "eax"
61#define REG_b "ebx"
62#elif defined (__amd64__)
63#define REG_a "rax"
64#define REG_b "rbx"
65#endif
66
67 /* First detect whether there is a hypervisor */
68 eax = 1;
69 __asm__ __volatile__ (
70 /* ebx/rbx is being used for PIC! */
71 " push %%"REG_b" \n\t"
72 " cpuid \n\t"
73 " pop %%"REG_b" \n\t"
74
75 : "=a" (eax), "=c" (ecx)
76 : "0" (eax)
77 );
78
79 hypervisor = !!(ecx & 0x80000000U);
80
81 if (hypervisor) {
75f86906
LP
82 union {
83 uint32_t sig32[3];
84 char text[13];
85 } sig = {};
86 unsigned j;
b52aae1d
LP
87
88 /* There is a hypervisor, see what it is */
89 eax = 0x40000000U;
90 __asm__ __volatile__ (
91 /* ebx/rbx is being used for PIC! */
92 " push %%"REG_b" \n\t"
93 " cpuid \n\t"
94 " mov %%ebx, %1 \n\t"
95 " pop %%"REG_b" \n\t"
96
97 : "=a" (eax), "=r" (sig.sig32[0]), "=c" (sig.sig32[1]), "=d" (sig.sig32[2])
98 : "0" (eax)
99 );
100
9f63a08d
SS
101 log_debug("Virtualization found, CPUID=%s", sig.text);
102
75f86906
LP
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;
bdb628ee 106
75f86906 107 return VIRTUALIZATION_VM_OTHER;
b52aae1d 108 }
bdb628ee 109#endif
9f63a08d 110 log_debug("No virtualization found in CPUID");
bdb628ee 111
75f86906 112 return VIRTUALIZATION_NONE;
bdb628ee
ZJS
113}
114
75f86906 115static int detect_vm_device_tree(void) {
db6a8689 116#if defined(__arm__) || defined(__aarch64__) || defined(__powerpc__) || defined(__powerpc64__)
d831deb5
CA
117 _cleanup_free_ char *hvtype = NULL;
118 int r;
119
b8f1df82 120 r = read_one_line_file("/proc/device-tree/hypervisor/compatible", &hvtype);
75f86906 121 if (r == -ENOENT) {
ce09c71d
AJ
122 _cleanup_closedir_ DIR *dir = NULL;
123 struct dirent *dent;
124
125 dir = opendir("/proc/device-tree");
126 if (!dir) {
9f63a08d
SS
127 if (errno == ENOENT) {
128 log_debug_errno(errno, "/proc/device-tree: %m");
75f86906 129 return VIRTUALIZATION_NONE;
9f63a08d 130 }
ce09c71d
AJ
131 return -errno;
132 }
133
75f86906 134 FOREACH_DIRENT(dent, dir, return -errno)
9f63a08d
SS
135 if (strstr(dent->d_name, "fw-cfg")) {
136 log_debug("Virtualization QEMU: \"fw-cfg\" present in /proc/device-tree/%s", dent->d_name);
75f86906 137 return VIRTUALIZATION_QEMU;
9f63a08d 138 }
75f86906 139
9f63a08d 140 log_debug("No virtualization found in /proc/device-tree/*");
75f86906
LP
141 return VIRTUALIZATION_NONE;
142 } else if (r < 0)
143 return r;
144
9f63a08d 145 log_debug("Virtualization %s found in /proc/device-tree/hypervisor/compatible", hvtype);
75f86906
LP
146 if (streq(hvtype, "linux,kvm"))
147 return VIRTUALIZATION_KVM;
148 else if (strstr(hvtype, "xen"))
149 return VIRTUALIZATION_XEN;
150 else
151 return VIRTUALIZATION_VM_OTHER;
152#else
9f63a08d 153 log_debug("This platform does not support /proc/device-tree");
75f86906 154 return VIRTUALIZATION_NONE;
d831deb5 155#endif
d831deb5
CA
156}
157
75f86906 158static int detect_vm_dmi(void) {
2ef8a4c4 159#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
bdb628ee
ZJS
160
161 static const char *const dmi_vendors[] = {
3728dcde 162 "/sys/class/dmi/id/product_name", /* Test this before sys_vendor to detect KVM over QEMU */
bdb628ee
ZJS
163 "/sys/class/dmi/id/sys_vendor",
164 "/sys/class/dmi/id/board_vendor",
165 "/sys/class/dmi/id/bios_vendor"
166 };
167
75f86906
LP
168 static const struct {
169 const char *vendor;
170 int id;
171 } dmi_vendor_table[] = {
3728dcde 172 { "KVM", VIRTUALIZATION_KVM },
75f86906 173 { "QEMU", VIRTUALIZATION_QEMU },
bdb628ee 174 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
75f86906
LP
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 },
181 };
bdb628ee 182 unsigned i;
75f86906 183 int r;
b52aae1d
LP
184
185 for (i = 0; i < ELEMENTSOF(dmi_vendors); i++) {
b1b8e816 186 _cleanup_free_ char *s = NULL;
75f86906 187 unsigned j;
b52aae1d 188
b1b8e816
LP
189 r = read_one_line_file(dmi_vendors[i], &s);
190 if (r < 0) {
75f86906
LP
191 if (r == -ENOENT)
192 continue;
b52aae1d 193
75f86906 194 return r;
b52aae1d
LP
195 }
196
9f63a08d
SS
197
198
75f86906 199 for (j = 0; j < ELEMENTSOF(dmi_vendor_table); j++)
9f63a08d
SS
200 if (startswith(s, dmi_vendor_table[j].vendor)) {
201 log_debug("Virtualization %s found in DMI (%s)", s, dmi_vendors[i]);
75f86906 202 return dmi_vendor_table[j].id;
9f63a08d 203 }
b52aae1d 204 }
bdb628ee
ZJS
205#endif
206
9f63a08d
SS
207 log_debug("No virtualization found in DMI");
208
75f86906 209 return VIRTUALIZATION_NONE;
bdb628ee
ZJS
210}
211
75f86906 212static int detect_vm_xen(void) {
3f61278b
SS
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;
218 }
219
220 log_debug("Virtualization XEN found (/proc/xen/capabilities exists)");
221 return VIRTUALIZATION_XEN;
222
223}
224
225static bool detect_vm_xen_dom0(void) {
75f86906
LP
226 _cleanup_free_ char *domcap = NULL;
227 char *cap, *i;
bdb628ee 228 int r;
b52aae1d 229
75f86906 230 r = read_one_line_file("/proc/xen/capabilities", &domcap);
9f63a08d
SS
231 if (r == -ENOENT) {
232 log_debug("Virtualization XEN not found, /proc/xen/capabilities does not exist");
3f61278b 233 return false;
9f63a08d 234 }
d5b687e7
LP
235 if (r < 0)
236 return r;
bdb628ee 237
75f86906
LP
238 i = domcap;
239 while ((cap = strsep(&i, ",")))
240 if (streq(cap, "control_d"))
241 break;
9f63a08d
SS
242 if (!cap) {
243 log_debug("Virtualization XEN DomU found (/proc/xen/capabilites)");
3f61278b 244 return false;
9f63a08d
SS
245 }
246
247 log_debug("Virtualization XEN Dom0 ignored (/proc/xen/capabilities)");
3f61278b 248 return true;
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
LP
269static int detect_vm_uml(void) {
270 _cleanup_free_ char *cpuinfo_contents = NULL;
271 int r;
37287585 272
75f86906
LP
273 /* Detect User-Mode Linux by reading /proc/cpuinfo */
274 r = read_full_file("/proc/cpuinfo", &cpuinfo_contents, NULL);
275 if (r < 0)
bdb628ee 276 return r;
9f63a08d
SS
277
278 if (strstr(cpuinfo_contents, "\nvendor_id\t: User Mode Linux\n")) {
279 log_debug("UML virtualization found in /proc/cpuinfo");
75f86906 280 return VIRTUALIZATION_UML;
9f63a08d 281 }
bdb628ee 282
9f63a08d 283 log_debug("No virtualization found in /proc/cpuinfo (%s)", cpuinfo_contents);
75f86906
LP
284 return VIRTUALIZATION_NONE;
285}
e32886e0 286
75f86906 287static int detect_vm_zvm(void) {
bdb628ee 288
75f86906
LP
289#if defined(__s390__)
290 _cleanup_free_ char *t = NULL;
291 int r;
e32886e0 292
c4cd1d4d 293 r = get_proc_field("/proc/sysinfo", "VM00 Control Program", WHITESPACE, &t);
75f86906
LP
294 if (r == -ENOENT)
295 return VIRTUALIZATION_NONE;
296 if (r < 0)
297 return r;
e32886e0 298
9f63a08d 299 log_debug("Virtualization %s found in /proc/sysinfo", t);
75f86906
LP
300 if (streq(t, "z/VM"))
301 return VIRTUALIZATION_ZVM;
302 else
303 return VIRTUALIZATION_KVM;
304#else
9f63a08d 305 log_debug("This platform does not support /proc/sysinfo");
75f86906
LP
306 return VIRTUALIZATION_NONE;
307#endif
308}
e32886e0 309
75f86906
LP
310/* Returns a short identifier for the various VM implementations */
311int detect_vm(void) {
312 static thread_local int cached_found = _VIRTUALIZATION_INVALID;
313 int r;
e32886e0 314
75f86906
LP
315 if (cached_found >= 0)
316 return cached_found;
bdb628ee 317
f6875b0a
CH
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.
321 *
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
324 * only. */
050e65ad 325 r = detect_vm_dmi();
75f86906
LP
326 if (r < 0)
327 return r;
328 if (r != VIRTUALIZATION_NONE)
d831deb5
CA
329 goto finish;
330
050e65ad 331 r = detect_vm_cpuid();
75f86906
LP
332 if (r < 0)
333 return r;
334 if (r != VIRTUALIZATION_NONE)
0fb533a5 335 goto finish;
b52aae1d 336
42685451
AJ
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:
341 *
342 * https://bugs.freedesktop.org/show_bug.cgi?id=77271 */
343
344 r = detect_vm_xen();
7080ea16
RR
345 if (r < 0)
346 return r;
75f86906 347 if (r != VIRTUALIZATION_NONE)
0fb533a5 348 goto finish;
7080ea16 349
75f86906
LP
350 r = detect_vm_hypervisor();
351 if (r < 0)
352 return r;
353 if (r != VIRTUALIZATION_NONE)
354 goto finish;
f41925b4 355
75f86906
LP
356 r = detect_vm_device_tree();
357 if (r < 0)
358 return r;
359 if (r != VIRTUALIZATION_NONE)
360 goto finish;
f41925b4 361
75f86906
LP
362 r = detect_vm_uml();
363 if (r < 0)
364 return r;
365 if (r != VIRTUALIZATION_NONE)
366 goto finish;
f41925b4 367
75f86906
LP
368 r = detect_vm_zvm();
369 if (r < 0)
370 return r;
0fb533a5
LP
371
372finish:
3f61278b
SS
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
375 * double-check it */
376 if (r == VIRTUALIZATION_XEN && detect_vm_xen_dom0())
377 r = VIRTUALIZATION_NONE;
378
0fb533a5 379 cached_found = r;
9f63a08d 380 log_debug("Found VM virtualization %s", virtualization_to_string(r));
0fb533a5 381 return r;
b52aae1d
LP
382}
383
75f86906 384int detect_container(void) {
0fb533a5 385
75f86906
LP
386 static const struct {
387 const char *value;
388 int id;
389 } value_table[] = {
390 { "lxc", VIRTUALIZATION_LXC },
391 { "lxc-libvirt", VIRTUALIZATION_LXC_LIBVIRT },
392 { "systemd-nspawn", VIRTUALIZATION_SYSTEMD_NSPAWN },
393 { "docker", VIRTUALIZATION_DOCKER },
9fb16425 394 { "rkt", VIRTUALIZATION_RKT },
75f86906 395 };
0fb533a5 396
75f86906 397 static thread_local int cached_found = _VIRTUALIZATION_INVALID;
fdd25311 398 _cleanup_free_ char *m = NULL;
75f86906
LP
399 const char *e = NULL;
400 unsigned j;
ab94af92 401 int r;
b52aae1d 402
75f86906 403 if (cached_found >= 0)
0fb533a5 404 return cached_found;
0fb533a5 405
b52aae1d
LP
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) {
75f86906 410 r = VIRTUALIZATION_OPENVZ;
0fb533a5 411 goto finish;
b52aae1d
LP
412 }
413
fdd25311
LP
414 if (getpid() == 1) {
415 /* If we are PID 1 we can just check our own
416 * environment variable */
417
418 e = getenv("container");
419 if (isempty(e)) {
75f86906 420 r = VIRTUALIZATION_NONE;
fdd25311
LP
421 goto finish;
422 }
423 } else {
424
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
428 * for that. */
429
430 r = read_one_line_file("/run/systemd/container", &m);
431 if (r == -ENOENT) {
536bfdab
LP
432
433 /* Fallback for cases where PID 1 was not
434 * systemd (for example, cases where
435 * init=/bin/sh is used. */
436
437 r = getenv_for_pid(1, "container", &m);
438 if (r <= 0) {
439
440 /* If that didn't work, give up,
441 * assume no container manager.
442 *
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. */
448
75f86906 449 r = VIRTUALIZATION_NONE;
536bfdab
LP
450 goto finish;
451 }
fdd25311
LP
452 }
453 if (r < 0)
454 return r;
455
456 e = m;
457 }
b52aae1d 458
75f86906
LP
459 for (j = 0; j < ELEMENTSOF(value_table); j++)
460 if (streq(e, value_table[j].value)) {
461 r = value_table[j].id;
462 goto finish;
463 }
0fb533a5 464
f499daf4 465 r = VIRTUALIZATION_CONTAINER_OTHER;
fdd25311 466
0fb533a5 467finish:
9f63a08d 468 log_debug("Found container virtualization %s", virtualization_to_string(r));
0fb533a5 469 cached_found = r;
ab94af92 470 return r;
b52aae1d
LP
471}
472
75f86906 473int detect_virtualization(void) {
b52aae1d 474 int r;
b52aae1d 475
75f86906 476 r = detect_container();
9f63a08d
SS
477 if (r == 0)
478 r = detect_vm();
b52aae1d 479
9f63a08d 480 return r;
b52aae1d 481}
75f86906 482
7f4b3c5e
LP
483int running_in_chroot(void) {
484 int ret;
485
486 ret = files_same("/proc/1/root", "/");
487 if (ret < 0)
488 return ret;
489
490 return ret == 0;
491}
492
75f86906
LP
493static 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",
506
507 [VIRTUALIZATION_SYSTEMD_NSPAWN] = "systemd-nspawn",
508 [VIRTUALIZATION_LXC_LIBVIRT] = "lxc-libvirt",
509 [VIRTUALIZATION_LXC] = "lxc",
510 [VIRTUALIZATION_OPENVZ] = "openvz",
511 [VIRTUALIZATION_DOCKER] = "docker",
9fb16425 512 [VIRTUALIZATION_RKT] = "rkt",
75f86906
LP
513 [VIRTUALIZATION_CONTAINER_OTHER] = "container-other",
514};
515
516DEFINE_STRING_TABLE_LOOKUP(virtualization, int);