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 General Public License as published by
10 the Free Software Foundation; either version 2 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 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 /* Returns a short identifier for the various VM implementations */
30 int detect_vm(const char **id) {
32 #if defined(__i386__) || defined(__x86_64__)
34 /* Both CPUID and DMI are x86 specific interfaces... */
36 static const char *const dmi_vendors[] = {
37 "/sys/class/dmi/id/sys_vendor",
38 "/sys/class/dmi/id/board_vendor",
39 "/sys/class/dmi/id/bios_vendor"
42 static const char dmi_vendor_table[] =
44 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
47 "Microsoft Corporation\0" "microsoft\0"
48 "innotek GmbH\0" "oracle\0"
52 static const char cpuid_vendor_table[] =
53 "XenVMMXenVMM\0" "xen\0"
55 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
56 "VMwareVMware\0" "vmware\0"
57 /* http://msdn.microsoft.com/en-us/library/ff542428.aspx */
58 "Microsoft Hv\0" "microsoft\0";
69 /* http://lwn.net/Articles/301888/ */
72 #if defined (__i386__)
75 #elif defined (__amd64__)
80 /* First detect whether there is a hypervisor */
82 __asm__ __volatile__ (
83 /* ebx/rbx is being used for PIC! */
84 " push %%"REG_b" \n\t"
88 : "=a" (eax), "=c" (ecx)
92 hypervisor = !!(ecx & 0x80000000U);
96 /* There is a hypervisor, see what it is */
98 __asm__ __volatile__ (
99 /* ebx/rbx is being used for PIC! */
100 " push %%"REG_b" \n\t"
102 " mov %%ebx, %1 \n\t"
103 " pop %%"REG_b" \n\t"
105 : "=a" (eax), "=r" (sig.sig32[0]), "=c" (sig.sig32[1]), "=d" (sig.sig32[2])
109 NULSTR_FOREACH_PAIR(j, k, cpuid_vendor_table)
110 if (streq(sig.text, j)) {
119 for (i = 0; i < ELEMENTSOF(dmi_vendors); i++) {
122 const char *found = NULL;
124 if ((r = read_one_line_file(dmi_vendors[i], &s)) < 0) {
131 NULSTR_FOREACH_PAIR(j, k, dmi_vendor_table)
132 if (startswith(s, j))
155 int detect_container(const char **id) {
158 /* Unfortunately many of these operations require root access
159 * in one way or another */
164 if (running_in_chroot() > 0) {
172 /* /proc/vz exists in container and outside of the container,
173 * /proc/bc only outside of the container. */
174 if (access("/proc/vz", F_OK) >= 0 &&
175 access("/proc/bc", F_OK) < 0) {
183 f = fopen("/proc/1/environ", "re");
191 for (i = 0; i < sizeof(line)-1; i++) {
195 if (_unlikely_(c == EOF)) {
205 if (streq(line, "container=lxc")) {
212 } else if (streq(line, "container=systemd-nspawn")) {
216 *id = "systemd-nspawn";
219 } else if (startswith(line, "container=")) {
232 f = fopen("/proc/self/cgroup", "re");
236 char line[LINE_MAX], *p;
238 if (!fgets(line, sizeof(line), f))
241 p = strchr(strstrip(line), ':');
245 if (strncmp(p, ":ns:", 4))
248 if (!streq(p, ":ns:/")) {
264 /* Returns a short identifier for the various VM/container implementations */
265 Virtualization detect_virtualization(const char **id) {
267 static __thread Virtualization cached_virt = _VIRTUALIZATION_INVALID;
268 static __thread const char *cached_id = NULL;
274 if (_likely_(cached_virt >= 0)) {
276 if (id && cached_virt > 0)
282 r = detect_container(&_id);
287 v = VIRTUALIZATION_CONTAINER;
296 v = VIRTUALIZATION_VM;
300 v = VIRTUALIZATION_NONE;