]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/virt.c
Merge pull request #2471 from michaelolbrich/transient-mounts
[thirdparty/systemd.git] / src / basic / virt.c
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
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
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
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "alloc-util.h"
27 #include "dirent-util.h"
28 #include "fd-util.h"
29 #include "fileio.h"
30 #include "macro.h"
31 #include "process-util.h"
32 #include "stat-util.h"
33 #include "string-table.h"
34 #include "string-util.h"
35 #include "virt.h"
36
37 static int detect_vm_cpuid(void) {
38
39 /* CPUID is an x86 specific interface. */
40 #if defined(__i386__) || defined(__x86_64__)
41
42 static const struct {
43 const char *cpuid;
44 int id;
45 } cpuid_vendor_table[] = {
46 { "XenVMMXenVMM", VIRTUALIZATION_XEN },
47 { "KVMKVMKVM", VIRTUALIZATION_KVM },
48 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
49 { "VMwareVMware", VIRTUALIZATION_VMWARE },
50 /* http://msdn.microsoft.com/en-us/library/ff542428.aspx */
51 { "Microsoft Hv", VIRTUALIZATION_MICROSOFT },
52 /* https://wiki.freebsd.org/bhyve */
53 { "bhyve bhyve ", VIRTUALIZATION_BHYVE },
54 };
55
56 uint32_t eax, ecx;
57 bool hypervisor;
58
59 /* http://lwn.net/Articles/301888/ */
60
61 #if defined (__i386__)
62 #define REG_a "eax"
63 #define REG_b "ebx"
64 #elif defined (__amd64__)
65 #define REG_a "rax"
66 #define REG_b "rbx"
67 #endif
68
69 /* First detect whether there is a hypervisor */
70 eax = 1;
71 __asm__ __volatile__ (
72 /* ebx/rbx is being used for PIC! */
73 " push %%"REG_b" \n\t"
74 " cpuid \n\t"
75 " pop %%"REG_b" \n\t"
76
77 : "=a" (eax), "=c" (ecx)
78 : "0" (eax)
79 );
80
81 hypervisor = !!(ecx & 0x80000000U);
82
83 if (hypervisor) {
84 union {
85 uint32_t sig32[3];
86 char text[13];
87 } sig = {};
88 unsigned j;
89
90 /* There is a hypervisor, see what it is */
91 eax = 0x40000000U;
92 __asm__ __volatile__ (
93 /* ebx/rbx is being used for PIC! */
94 " push %%"REG_b" \n\t"
95 " cpuid \n\t"
96 " mov %%ebx, %1 \n\t"
97 " pop %%"REG_b" \n\t"
98
99 : "=a" (eax), "=r" (sig.sig32[0]), "=c" (sig.sig32[1]), "=d" (sig.sig32[2])
100 : "0" (eax)
101 );
102
103 log_debug("Virtualization found, CPUID=%s", sig.text);
104
105 for (j = 0; j < ELEMENTSOF(cpuid_vendor_table); j ++)
106 if (streq(sig.text, cpuid_vendor_table[j].cpuid))
107 return cpuid_vendor_table[j].id;
108
109 return VIRTUALIZATION_VM_OTHER;
110 }
111 #endif
112 log_debug("No virtualization found in CPUID");
113
114 return VIRTUALIZATION_NONE;
115 }
116
117 static int detect_vm_device_tree(void) {
118 #if defined(__arm__) || defined(__aarch64__) || defined(__powerpc__) || defined(__powerpc64__)
119 _cleanup_free_ char *hvtype = NULL;
120 int r;
121
122 r = read_one_line_file("/proc/device-tree/hypervisor/compatible", &hvtype);
123 if (r == -ENOENT) {
124 _cleanup_closedir_ DIR *dir = NULL;
125 struct dirent *dent;
126
127 dir = opendir("/proc/device-tree");
128 if (!dir) {
129 if (errno == ENOENT) {
130 log_debug_errno(errno, "/proc/device-tree: %m");
131 return VIRTUALIZATION_NONE;
132 }
133 return -errno;
134 }
135
136 FOREACH_DIRENT(dent, dir, return -errno)
137 if (strstr(dent->d_name, "fw-cfg")) {
138 log_debug("Virtualization QEMU: \"fw-cfg\" present in /proc/device-tree/%s", dent->d_name);
139 return VIRTUALIZATION_QEMU;
140 }
141
142 log_debug("No virtualization found in /proc/device-tree/*");
143 return VIRTUALIZATION_NONE;
144 } else if (r < 0)
145 return r;
146
147 log_debug("Virtualization %s found in /proc/device-tree/hypervisor/compatible", hvtype);
148 if (streq(hvtype, "linux,kvm"))
149 return VIRTUALIZATION_KVM;
150 else if (strstr(hvtype, "xen"))
151 return VIRTUALIZATION_XEN;
152 else
153 return VIRTUALIZATION_VM_OTHER;
154 #else
155 log_debug("This platform does not support /proc/device-tree");
156 return VIRTUALIZATION_NONE;
157 #endif
158 }
159
160 static int detect_vm_dmi(void) {
161 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
162
163 static const char *const dmi_vendors[] = {
164 "/sys/class/dmi/id/product_name", /* Test this before sys_vendor to detect KVM over QEMU */
165 "/sys/class/dmi/id/sys_vendor",
166 "/sys/class/dmi/id/board_vendor",
167 "/sys/class/dmi/id/bios_vendor"
168 };
169
170 static const struct {
171 const char *vendor;
172 int id;
173 } dmi_vendor_table[] = {
174 { "KVM", VIRTUALIZATION_KVM },
175 { "QEMU", VIRTUALIZATION_QEMU },
176 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
177 { "VMware", VIRTUALIZATION_VMWARE },
178 { "VMW", VIRTUALIZATION_VMWARE },
179 { "innotek GmbH", VIRTUALIZATION_ORACLE },
180 { "Xen", VIRTUALIZATION_XEN },
181 { "Bochs", VIRTUALIZATION_BOCHS },
182 { "Parallels", VIRTUALIZATION_PARALLELS },
183 /* https://wiki.freebsd.org/bhyve */
184 { "BHYVE", VIRTUALIZATION_BHYVE },
185 };
186 unsigned i;
187 int r;
188
189 for (i = 0; i < ELEMENTSOF(dmi_vendors); i++) {
190 _cleanup_free_ char *s = NULL;
191 unsigned j;
192
193 r = read_one_line_file(dmi_vendors[i], &s);
194 if (r < 0) {
195 if (r == -ENOENT)
196 continue;
197
198 return r;
199 }
200
201
202
203 for (j = 0; j < ELEMENTSOF(dmi_vendor_table); j++)
204 if (startswith(s, dmi_vendor_table[j].vendor)) {
205 log_debug("Virtualization %s found in DMI (%s)", s, dmi_vendors[i]);
206 return dmi_vendor_table[j].id;
207 }
208 }
209 #endif
210
211 log_debug("No virtualization found in DMI");
212
213 return VIRTUALIZATION_NONE;
214 }
215
216 static int detect_vm_xen(void) {
217 /* Check for Dom0 will be executed later in detect_vm_xen_dom0
218 Thats why we dont check the content of /proc/xen/capabilities here. */
219 if (access("/proc/xen/capabilities", F_OK) < 0) {
220 log_debug("Virtualization XEN not found, /proc/xen/capabilities does not exist");
221 return VIRTUALIZATION_NONE;
222 }
223
224 log_debug("Virtualization XEN found (/proc/xen/capabilities exists)");
225 return VIRTUALIZATION_XEN;
226
227 }
228
229 static bool detect_vm_xen_dom0(void) {
230 _cleanup_free_ char *domcap = NULL;
231 char *cap, *i;
232 int r;
233
234 r = read_one_line_file("/proc/xen/capabilities", &domcap);
235 if (r == -ENOENT) {
236 log_debug("Virtualization XEN not found, /proc/xen/capabilities does not exist");
237 return false;
238 }
239 if (r < 0)
240 return r;
241
242 i = domcap;
243 while ((cap = strsep(&i, ",")))
244 if (streq(cap, "control_d"))
245 break;
246 if (!cap) {
247 log_debug("Virtualization XEN DomU found (/proc/xen/capabilites)");
248 return false;
249 }
250
251 log_debug("Virtualization XEN Dom0 ignored (/proc/xen/capabilities)");
252 return true;
253 }
254
255 static int detect_vm_hypervisor(void) {
256 _cleanup_free_ char *hvtype = NULL;
257 int r;
258
259 r = read_one_line_file("/sys/hypervisor/type", &hvtype);
260 if (r == -ENOENT)
261 return VIRTUALIZATION_NONE;
262 if (r < 0)
263 return r;
264
265 log_debug("Virtualization %s found in /sys/hypervisor/type", hvtype);
266
267 if (streq(hvtype, "xen"))
268 return VIRTUALIZATION_XEN;
269 else
270 return VIRTUALIZATION_VM_OTHER;
271 }
272
273 static int detect_vm_uml(void) {
274 _cleanup_free_ char *cpuinfo_contents = NULL;
275 int r;
276
277 /* Detect User-Mode Linux by reading /proc/cpuinfo */
278 r = read_full_file("/proc/cpuinfo", &cpuinfo_contents, NULL);
279 if (r < 0)
280 return r;
281
282 if (strstr(cpuinfo_contents, "\nvendor_id\t: User Mode Linux\n")) {
283 log_debug("UML virtualization found in /proc/cpuinfo");
284 return VIRTUALIZATION_UML;
285 }
286
287 log_debug("No virtualization found in /proc/cpuinfo.");
288 return VIRTUALIZATION_NONE;
289 }
290
291 static int detect_vm_zvm(void) {
292
293 #if defined(__s390__)
294 _cleanup_free_ char *t = NULL;
295 int r;
296
297 r = get_proc_field("/proc/sysinfo", "VM00 Control Program", WHITESPACE, &t);
298 if (r == -ENOENT)
299 return VIRTUALIZATION_NONE;
300 if (r < 0)
301 return r;
302
303 log_debug("Virtualization %s found in /proc/sysinfo", t);
304 if (streq(t, "z/VM"))
305 return VIRTUALIZATION_ZVM;
306 else
307 return VIRTUALIZATION_KVM;
308 #else
309 log_debug("This platform does not support /proc/sysinfo");
310 return VIRTUALIZATION_NONE;
311 #endif
312 }
313
314 /* Returns a short identifier for the various VM implementations */
315 int detect_vm(void) {
316 static thread_local int cached_found = _VIRTUALIZATION_INVALID;
317 int r;
318
319 if (cached_found >= 0)
320 return cached_found;
321
322 /* We have to use the correct order here:
323 * Some virtualization technologies do use KVM hypervisor but are
324 * expected to be detected as something else. So detect DMI first.
325 *
326 * An example is Virtualbox since version 5.0, which uses KVM backend.
327 * Detection via DMI works corretly, the CPU ID would find KVM
328 * only. */
329 r = detect_vm_dmi();
330 if (r < 0)
331 return r;
332 if (r != VIRTUALIZATION_NONE)
333 goto finish;
334
335 r = detect_vm_cpuid();
336 if (r < 0)
337 return r;
338 if (r != VIRTUALIZATION_NONE)
339 goto finish;
340
341 /* x86 xen will most likely be detected by cpuid. If not (most likely
342 * because we're not an x86 guest), then we should try the xen capabilities
343 * file next. If that's not found, then we check for the high-level
344 * hypervisor sysfs file:
345 *
346 * https://bugs.freedesktop.org/show_bug.cgi?id=77271 */
347
348 r = detect_vm_xen();
349 if (r < 0)
350 return r;
351 if (r != VIRTUALIZATION_NONE)
352 goto finish;
353
354 r = detect_vm_hypervisor();
355 if (r < 0)
356 return r;
357 if (r != VIRTUALIZATION_NONE)
358 goto finish;
359
360 r = detect_vm_device_tree();
361 if (r < 0)
362 return r;
363 if (r != VIRTUALIZATION_NONE)
364 goto finish;
365
366 r = detect_vm_uml();
367 if (r < 0)
368 return r;
369 if (r != VIRTUALIZATION_NONE)
370 goto finish;
371
372 r = detect_vm_zvm();
373 if (r < 0)
374 return r;
375
376 finish:
377 /* x86 xen Dom0 is detected as XEN in hypervisor and maybe others.
378 * In order to detect the Dom0 as not virtualization we need to
379 * double-check it */
380 if (r == VIRTUALIZATION_XEN && detect_vm_xen_dom0())
381 r = VIRTUALIZATION_NONE;
382
383 cached_found = r;
384 log_debug("Found VM virtualization %s", virtualization_to_string(r));
385 return r;
386 }
387
388 int detect_container(void) {
389
390 static const struct {
391 const char *value;
392 int id;
393 } value_table[] = {
394 { "lxc", VIRTUALIZATION_LXC },
395 { "lxc-libvirt", VIRTUALIZATION_LXC_LIBVIRT },
396 { "systemd-nspawn", VIRTUALIZATION_SYSTEMD_NSPAWN },
397 { "docker", VIRTUALIZATION_DOCKER },
398 { "rkt", VIRTUALIZATION_RKT },
399 };
400
401 static thread_local int cached_found = _VIRTUALIZATION_INVALID;
402 _cleanup_free_ char *m = NULL;
403 const char *e = NULL;
404 unsigned j;
405 int r;
406
407 if (cached_found >= 0)
408 return cached_found;
409
410 /* /proc/vz exists in container and outside of the container,
411 * /proc/bc only outside of the container. */
412 if (access("/proc/vz", F_OK) >= 0 &&
413 access("/proc/bc", F_OK) < 0) {
414 r = VIRTUALIZATION_OPENVZ;
415 goto finish;
416 }
417
418 if (getpid() == 1) {
419 /* If we are PID 1 we can just check our own
420 * environment variable */
421
422 e = getenv("container");
423 if (isempty(e)) {
424 r = VIRTUALIZATION_NONE;
425 goto finish;
426 }
427 } else {
428
429 /* Otherwise, PID 1 dropped this information into a
430 * file in /run. This is better than accessing
431 * /proc/1/environ, since we don't need CAP_SYS_PTRACE
432 * for that. */
433
434 r = read_one_line_file("/run/systemd/container", &m);
435 if (r == -ENOENT) {
436
437 /* Fallback for cases where PID 1 was not
438 * systemd (for example, cases where
439 * init=/bin/sh is used. */
440
441 r = getenv_for_pid(1, "container", &m);
442 if (r <= 0) {
443
444 /* If that didn't work, give up,
445 * assume no container manager.
446 *
447 * Note: This means we still cannot
448 * detect containers if init=/bin/sh
449 * is passed but privileges dropped,
450 * as /proc/1/environ is only readable
451 * with privileges. */
452
453 r = VIRTUALIZATION_NONE;
454 goto finish;
455 }
456 }
457 if (r < 0)
458 return r;
459
460 e = m;
461 }
462
463 for (j = 0; j < ELEMENTSOF(value_table); j++)
464 if (streq(e, value_table[j].value)) {
465 r = value_table[j].id;
466 goto finish;
467 }
468
469 r = VIRTUALIZATION_CONTAINER_OTHER;
470
471 finish:
472 log_debug("Found container virtualization %s", virtualization_to_string(r));
473 cached_found = r;
474 return r;
475 }
476
477 int detect_virtualization(void) {
478 int r;
479
480 r = detect_container();
481 if (r == 0)
482 r = detect_vm();
483
484 return r;
485 }
486
487 int running_in_chroot(void) {
488 int ret;
489
490 ret = files_same("/proc/1/root", "/");
491 if (ret < 0)
492 return ret;
493
494 return ret == 0;
495 }
496
497 static const char *const virtualization_table[_VIRTUALIZATION_MAX] = {
498 [VIRTUALIZATION_NONE] = "none",
499 [VIRTUALIZATION_KVM] = "kvm",
500 [VIRTUALIZATION_QEMU] = "qemu",
501 [VIRTUALIZATION_BOCHS] = "bochs",
502 [VIRTUALIZATION_XEN] = "xen",
503 [VIRTUALIZATION_UML] = "uml",
504 [VIRTUALIZATION_VMWARE] = "vmware",
505 [VIRTUALIZATION_ORACLE] = "oracle",
506 [VIRTUALIZATION_MICROSOFT] = "microsoft",
507 [VIRTUALIZATION_ZVM] = "zvm",
508 [VIRTUALIZATION_PARALLELS] = "parallels",
509 [VIRTUALIZATION_BHYVE] = "bhyve",
510 [VIRTUALIZATION_VM_OTHER] = "vm-other",
511
512 [VIRTUALIZATION_SYSTEMD_NSPAWN] = "systemd-nspawn",
513 [VIRTUALIZATION_LXC_LIBVIRT] = "lxc-libvirt",
514 [VIRTUALIZATION_LXC] = "lxc",
515 [VIRTUALIZATION_OPENVZ] = "openvz",
516 [VIRTUALIZATION_DOCKER] = "docker",
517 [VIRTUALIZATION_RKT] = "rkt",
518 [VIRTUALIZATION_CONTAINER_OTHER] = "container-other",
519 };
520
521 DEFINE_STRING_TABLE_LOOKUP(virtualization, int);