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 /* Returns a short identifier for the various VM implementations */
31 int detect_vm(const char **id) {
33 #if defined(__i386__) || defined(__x86_64__)
35 /* Both CPUID and DMI are x86 specific interfaces... */
37 static const char *const dmi_vendors[] = {
38 "/sys/class/dmi/id/sys_vendor",
39 "/sys/class/dmi/id/board_vendor",
40 "/sys/class/dmi/id/bios_vendor"
43 static const char dmi_vendor_table[] =
45 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
48 "Microsoft Corporation\0" "microsoft\0"
49 "innotek GmbH\0" "oracle\0"
53 static const char cpuid_vendor_table[] =
54 "XenVMMXenVMM\0" "xen\0"
56 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
57 "VMwareVMware\0" "vmware\0"
58 /* http://msdn.microsoft.com/en-us/library/ff542428.aspx */
59 "Microsoft Hv\0" "microsoft\0";
69 _cleanup_free_ char *hvtype = NULL;
72 /* Try high-level hypervisor sysfs file first:
74 * https://bugs.freedesktop.org/show_bug.cgi?id=61491 */
75 r = read_one_line_file("/sys/hypervisor/type", &hvtype);
77 if (streq(hvtype, "xen")) {
83 } else if (r != -ENOENT)
86 /* http://lwn.net/Articles/301888/ */
88 #if defined (__i386__)
91 #elif defined (__amd64__)
96 /* First detect whether there is a hypervisor */
98 __asm__ __volatile__ (
99 /* ebx/rbx is being used for PIC! */
100 " push %%"REG_b" \n\t"
102 " pop %%"REG_b" \n\t"
104 : "=a" (eax), "=c" (ecx)
108 hypervisor = !!(ecx & 0x80000000U);
112 /* There is a hypervisor, see what it is */
114 __asm__ __volatile__ (
115 /* ebx/rbx is being used for PIC! */
116 " push %%"REG_b" \n\t"
118 " mov %%ebx, %1 \n\t"
119 " pop %%"REG_b" \n\t"
121 : "=a" (eax), "=r" (sig.sig32[0]), "=c" (sig.sig32[1]), "=d" (sig.sig32[2])
125 NULSTR_FOREACH_PAIR(j, k, cpuid_vendor_table)
126 if (streq(sig.text, j)) {
135 for (i = 0; i < ELEMENTSOF(dmi_vendors); i++) {
136 _cleanup_free_ char *s = NULL;
137 const char *found = NULL;
139 r = read_one_line_file(dmi_vendors[i], &s);
147 NULSTR_FOREACH_PAIR(j, k, dmi_vendor_table)
148 if (startswith(s, j))
159 if (hypervisor || hvtype) {
170 int detect_container(const char **id) {
171 _cleanup_free_ char *e = NULL;
174 /* Unfortunately many of these operations require root access
175 * in one way or another */
177 r = running_in_chroot();
188 /* /proc/vz exists in container and outside of the container,
189 * /proc/bc only outside of the container. */
190 if (access("/proc/vz", F_OK) >= 0 &&
191 access("/proc/bc", F_OK) < 0) {
199 r = getenv_for_pid(1, "container", &e);
203 /* We only recognize a selected few here, since we want to
204 * enforce a redacted namespace */
205 if (streq(e, "lxc")) {
208 } else if (streq(e, "lxc-libvirt")) {
211 } else if (streq(e, "systemd-nspawn")) {
213 *id = "systemd-nspawn";
222 /* Returns a short identifier for the various VM/container implementations */
223 Virtualization detect_virtualization(const char **id) {
225 static __thread Virtualization cached_virt = _VIRTUALIZATION_INVALID;
226 static __thread const char *cached_id = NULL;
232 if (_likely_(cached_virt >= 0)) {
234 if (id && cached_virt > 0)
240 r = detect_container(&_id);
245 v = VIRTUALIZATION_CONTAINER;
254 v = VIRTUALIZATION_VM;
258 v = VIRTUALIZATION_NONE;