]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/virt.c
Merge pull request #2724 from vcaputo/minor-formatting-cleanups
[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 };
53
54 uint32_t eax, ecx;
55 bool hypervisor;
56
57 /* http://lwn.net/Articles/301888/ */
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) {
82 union {
83 uint32_t sig32[3];
84 char text[13];
85 } sig = {};
86 unsigned j;
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
101 log_debug("Virtualization found, CPUID=%s", sig.text);
102
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;
106
107 return VIRTUALIZATION_VM_OTHER;
108 }
109 #endif
110 log_debug("No virtualization found in CPUID");
111
112 return VIRTUALIZATION_NONE;
113 }
114
115 static int detect_vm_device_tree(void) {
116 #if defined(__arm__) || defined(__aarch64__) || defined(__powerpc__) || defined(__powerpc64__)
117 _cleanup_free_ char *hvtype = NULL;
118 int r;
119
120 r = read_one_line_file("/proc/device-tree/hypervisor/compatible", &hvtype);
121 if (r == -ENOENT) {
122 _cleanup_closedir_ DIR *dir = NULL;
123 struct dirent *dent;
124
125 dir = opendir("/proc/device-tree");
126 if (!dir) {
127 if (errno == ENOENT) {
128 log_debug_errno(errno, "/proc/device-tree: %m");
129 return VIRTUALIZATION_NONE;
130 }
131 return -errno;
132 }
133
134 FOREACH_DIRENT(dent, dir, return -errno)
135 if (strstr(dent->d_name, "fw-cfg")) {
136 log_debug("Virtualization QEMU: \"fw-cfg\" present in /proc/device-tree/%s", dent->d_name);
137 return VIRTUALIZATION_QEMU;
138 }
139
140 log_debug("No virtualization found in /proc/device-tree/*");
141 return VIRTUALIZATION_NONE;
142 } else if (r < 0)
143 return r;
144
145 log_debug("Virtualization %s found in /proc/device-tree/hypervisor/compatible", hvtype);
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
153 log_debug("This platform does not support /proc/device-tree");
154 return VIRTUALIZATION_NONE;
155 #endif
156 }
157
158 static int detect_vm_dmi(void) {
159 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
160
161 static const char *const dmi_vendors[] = {
162 "/sys/class/dmi/id/product_name", /* Test this before sys_vendor to detect KVM over QEMU */
163 "/sys/class/dmi/id/sys_vendor",
164 "/sys/class/dmi/id/board_vendor",
165 "/sys/class/dmi/id/bios_vendor"
166 };
167
168 static const struct {
169 const char *vendor;
170 int id;
171 } dmi_vendor_table[] = {
172 { "KVM", VIRTUALIZATION_KVM },
173 { "QEMU", VIRTUALIZATION_QEMU },
174 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
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 };
182 unsigned i;
183 int r;
184
185 for (i = 0; i < ELEMENTSOF(dmi_vendors); i++) {
186 _cleanup_free_ char *s = NULL;
187 unsigned j;
188
189 r = read_one_line_file(dmi_vendors[i], &s);
190 if (r < 0) {
191 if (r == -ENOENT)
192 continue;
193
194 return r;
195 }
196
197
198
199 for (j = 0; j < ELEMENTSOF(dmi_vendor_table); j++)
200 if (startswith(s, dmi_vendor_table[j].vendor)) {
201 log_debug("Virtualization %s found in DMI (%s)", s, dmi_vendors[i]);
202 return dmi_vendor_table[j].id;
203 }
204 }
205 #endif
206
207 log_debug("No virtualization found in DMI");
208
209 return VIRTUALIZATION_NONE;
210 }
211
212 static int detect_vm_xen(void) {
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
225 static bool detect_vm_xen_dom0(void) {
226 _cleanup_free_ char *domcap = NULL;
227 char *cap, *i;
228 int r;
229
230 r = read_one_line_file("/proc/xen/capabilities", &domcap);
231 if (r == -ENOENT) {
232 log_debug("Virtualization XEN not found, /proc/xen/capabilities does not exist");
233 return false;
234 }
235 if (r < 0)
236 return r;
237
238 i = domcap;
239 while ((cap = strsep(&i, ",")))
240 if (streq(cap, "control_d"))
241 break;
242 if (!cap) {
243 log_debug("Virtualization XEN DomU found (/proc/xen/capabilites)");
244 return false;
245 }
246
247 log_debug("Virtualization XEN Dom0 ignored (/proc/xen/capabilities)");
248 return true;
249 }
250
251 static int detect_vm_hypervisor(void) {
252 _cleanup_free_ char *hvtype = NULL;
253 int r;
254
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;
260
261 log_debug("Virtualization %s found in /sys/hypervisor/type", hvtype);
262
263 if (streq(hvtype, "xen"))
264 return VIRTUALIZATION_XEN;
265 else
266 return VIRTUALIZATION_VM_OTHER;
267 }
268
269 static int detect_vm_uml(void) {
270 _cleanup_free_ char *cpuinfo_contents = NULL;
271 int r;
272
273 /* Detect User-Mode Linux by reading /proc/cpuinfo */
274 r = read_full_file("/proc/cpuinfo", &cpuinfo_contents, NULL);
275 if (r < 0)
276 return r;
277
278 if (strstr(cpuinfo_contents, "\nvendor_id\t: User Mode Linux\n")) {
279 log_debug("UML virtualization found in /proc/cpuinfo");
280 return VIRTUALIZATION_UML;
281 }
282
283 log_debug("No virtualization found in /proc/cpuinfo (%s)", cpuinfo_contents);
284 return VIRTUALIZATION_NONE;
285 }
286
287 static int detect_vm_zvm(void) {
288
289 #if defined(__s390__)
290 _cleanup_free_ char *t = NULL;
291 int r;
292
293 r = get_proc_field("/proc/sysinfo", "VM00 Control Program", WHITESPACE, &t);
294 if (r == -ENOENT)
295 return VIRTUALIZATION_NONE;
296 if (r < 0)
297 return r;
298
299 log_debug("Virtualization %s found in /proc/sysinfo", t);
300 if (streq(t, "z/VM"))
301 return VIRTUALIZATION_ZVM;
302 else
303 return VIRTUALIZATION_KVM;
304 #else
305 log_debug("This platform does not support /proc/sysinfo");
306 return VIRTUALIZATION_NONE;
307 #endif
308 }
309
310 /* Returns a short identifier for the various VM implementations */
311 int detect_vm(void) {
312 static thread_local int cached_found = _VIRTUALIZATION_INVALID;
313 int r;
314
315 if (cached_found >= 0)
316 return cached_found;
317
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. */
325 r = detect_vm_dmi();
326 if (r < 0)
327 return r;
328 if (r != VIRTUALIZATION_NONE)
329 goto finish;
330
331 r = detect_vm_cpuid();
332 if (r < 0)
333 return r;
334 if (r != VIRTUALIZATION_NONE)
335 goto finish;
336
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();
345 if (r < 0)
346 return r;
347 if (r != VIRTUALIZATION_NONE)
348 goto finish;
349
350 r = detect_vm_hypervisor();
351 if (r < 0)
352 return r;
353 if (r != VIRTUALIZATION_NONE)
354 goto finish;
355
356 r = detect_vm_device_tree();
357 if (r < 0)
358 return r;
359 if (r != VIRTUALIZATION_NONE)
360 goto finish;
361
362 r = detect_vm_uml();
363 if (r < 0)
364 return r;
365 if (r != VIRTUALIZATION_NONE)
366 goto finish;
367
368 r = detect_vm_zvm();
369 if (r < 0)
370 return r;
371
372 finish:
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
379 cached_found = r;
380 log_debug("Found VM virtualization %s", virtualization_to_string(r));
381 return r;
382 }
383
384 int detect_container(void) {
385
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 },
394 { "rkt", VIRTUALIZATION_RKT },
395 };
396
397 static thread_local int cached_found = _VIRTUALIZATION_INVALID;
398 _cleanup_free_ char *m = NULL;
399 const char *e = NULL;
400 unsigned j;
401 int r;
402
403 if (cached_found >= 0)
404 return cached_found;
405
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) {
410 r = VIRTUALIZATION_OPENVZ;
411 goto finish;
412 }
413
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)) {
420 r = VIRTUALIZATION_NONE;
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) {
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
449 r = VIRTUALIZATION_NONE;
450 goto finish;
451 }
452 }
453 if (r < 0)
454 return r;
455
456 e = m;
457 }
458
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 }
464
465 r = VIRTUALIZATION_CONTAINER_OTHER;
466
467 finish:
468 log_debug("Found container virtualization %s", virtualization_to_string(r));
469 cached_found = r;
470 return r;
471 }
472
473 int detect_virtualization(void) {
474 int r;
475
476 r = detect_container();
477 if (r == 0)
478 r = detect_vm();
479
480 return r;
481 }
482
483 int 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
493 static 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",
512 [VIRTUALIZATION_RKT] = "rkt",
513 [VIRTUALIZATION_CONTAINER_OTHER] = "container-other",
514 };
515
516 DEFINE_STRING_TABLE_LOOKUP(virtualization, int);