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