1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
30 static int detect_vm_cpuid(const char **_id) {
32 /* Both CPUID and DMI are x86 specific interfaces... */
33 #if defined(__i386__) || defined(__x86_64__)
35 static const char cpuid_vendor_table[] =
36 "XenVMMXenVMM\0" "xen\0"
38 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
39 "VMwareVMware\0" "vmware\0"
40 /* http://msdn.microsoft.com/en-us/library/ff542428.aspx */
41 "Microsoft Hv\0" "microsoft\0";
51 /* http://lwn.net/Articles/301888/ */
53 #if defined (__i386__)
56 #elif defined (__amd64__)
61 /* First detect whether there is a hypervisor */
63 __asm__ __volatile__ (
64 /* ebx/rbx is being used for PIC! */
65 " push %%"REG_b" \n\t"
69 : "=a" (eax), "=c" (ecx)
73 hypervisor = !!(ecx & 0x80000000U);
77 /* There is a hypervisor, see what it is */
79 __asm__ __volatile__ (
80 /* ebx/rbx is being used for PIC! */
81 " push %%"REG_b" \n\t"
86 : "=a" (eax), "=r" (sig.sig32[0]), "=c" (sig.sig32[1]), "=d" (sig.sig32[2])
90 NULSTR_FOREACH_PAIR(j, k, cpuid_vendor_table)
91 if (streq(sig.text, j)) {
104 static int detect_vm_devicetree(const char **_id) {
105 #if defined(__powerpc__) || defined(__powerpc64__)
106 _cleanup_free_ char *hvtype = NULL;
109 r = read_one_line_file("/sys/firmware/devicetree/base/hypervisor/compatible", &hvtype);
111 if (streq(hvtype, "linux,kvm")) {
120 static int detect_vm_dmi(const char **_id) {
122 /* Both CPUID and DMI are x86 specific interfaces... */
123 #if defined(__i386__) || defined(__x86_64__)
125 static const char *const dmi_vendors[] = {
126 "/sys/class/dmi/id/sys_vendor",
127 "/sys/class/dmi/id/board_vendor",
128 "/sys/class/dmi/id/bios_vendor"
131 static const char dmi_vendor_table[] =
133 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
134 "VMware\0" "vmware\0"
136 "innotek GmbH\0" "oracle\0"
141 for (i = 0; i < ELEMENTSOF(dmi_vendors); i++) {
142 _cleanup_free_ char *s = NULL;
146 r = read_one_line_file(dmi_vendors[i], &s);
154 NULSTR_FOREACH_PAIR(j, k, dmi_vendor_table)
155 if (startswith(s, j)) {
165 /* Returns a short identifier for the various VM implementations */
166 int detect_vm(const char **id) {
167 _cleanup_free_ char *domcap = NULL, *cpuinfo_contents = NULL;
168 static thread_local int cached_found = -1;
169 static thread_local const char *cached_id = NULL;
170 const char *_id = NULL;
173 if (_likely_(cached_found >= 0)) {
181 /* Try xen capabilities file first, if not found try high-level hypervisor sysfs file:
183 * https://bugs.freedesktop.org/show_bug.cgi?id=77271 */
184 r = read_one_line_file("/proc/xen/capabilities", &domcap);
186 char *cap, *i = domcap;
188 while ((cap = strsep(&i, ",")))
189 if (streq(cap, "control_d"))
199 } else if (r == -ENOENT) {
200 _cleanup_free_ char *hvtype = NULL;
202 r = read_one_line_file("/sys/hypervisor/type", &hvtype);
204 if (streq(hvtype, "xen")) {
209 } else if (r != -ENOENT)
214 /* this will set _id to "other" and return 0 for unknown hypervisors */
215 r = detect_vm_cpuid(&_id);
219 r = detect_vm_dmi(&_id);
223 r = detect_vm_devicetree(&_id);
233 /* Detect User-Mode Linux by reading /proc/cpuinfo */
234 r = read_full_file("/proc/cpuinfo", &cpuinfo_contents, NULL);
237 if (strstr(cpuinfo_contents, "\nvendor_id\t: User Mode Linux\n")) {
243 #if defined(__s390__)
245 _cleanup_free_ char *t = NULL;
247 r = get_status_field("/proc/sysinfo", "VM00 Control Program:", &t);
249 if (streq(t, "z/VM"))
272 int detect_container(const char **id) {
274 static thread_local int cached_found = -1;
275 static thread_local const char *cached_id = NULL;
277 _cleanup_free_ char *m = NULL;
278 const char *_id = NULL, *e = NULL;
281 if (_likely_(cached_found >= 0)) {
289 /* /proc/vz exists in container and outside of the container,
290 * /proc/bc only outside of the container. */
291 if (access("/proc/vz", F_OK) >= 0 &&
292 access("/proc/bc", F_OK) < 0) {
299 /* If we are PID 1 we can just check our own
300 * environment variable */
302 e = getenv("container");
309 /* Otherwise, PID 1 dropped this information into a
310 * file in /run. This is better than accessing
311 * /proc/1/environ, since we don't need CAP_SYS_PTRACE
314 r = read_one_line_file("/run/systemd/container", &m);
317 /* Fallback for cases where PID 1 was not
318 * systemd (for example, cases where
319 * init=/bin/sh is used. */
321 r = getenv_for_pid(1, "container", &m);
324 /* If that didn't work, give up,
325 * assume no container manager.
327 * Note: This means we still cannot
328 * detect containers if init=/bin/sh
329 * is passed but privileges dropped,
330 * as /proc/1/environ is only readable
331 * with privileges. */
343 /* We only recognize a selected few here, since we want to
344 * enforce a redacted namespace */
347 else if (streq(e, "lxc-libvirt"))
349 else if (streq(e, "systemd-nspawn"))
350 _id = "systemd-nspawn";
351 else if (streq(e, "docker"))
368 /* Returns a short identifier for the various VM/container implementations */
369 int detect_virtualization(const char **id) {
372 r = detect_container(id);
376 return VIRTUALIZATION_CONTAINER;
382 return VIRTUALIZATION_VM;
384 return VIRTUALIZATION_NONE;