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