virt.c revision c4cd1d4d93e4a45a088edb6517555aa7e06e5f86
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "util.h"
#include "process-util.h"
#include "virt.h"
#include "fileio.h"
static int detect_vm_cpuid(void) {
/* Both CPUID and DMI are x86 specific interfaces... */
#if defined(__i386__) || defined(__x86_64__)
static const struct {
const char *cpuid;
int id;
} cpuid_vendor_table[] = {
{ "XenVMMXenVMM", VIRTUALIZATION_XEN },
{ "KVMKVMKVM", VIRTUALIZATION_KVM },
{ "VMwareVMware", VIRTUALIZATION_VMWARE },
{ "Microsoft Hv", VIRTUALIZATION_MICROSOFT },
};
bool hypervisor;
#if defined (__i386__)
#define REG_a "eax"
#define REG_b "ebx"
#define REG_a "rax"
#define REG_b "rbx"
#endif
/* First detect whether there is a hypervisor */
eax = 1;
" cpuid \n\t"
: "0" (eax)
);
if (hypervisor) {
union {
char text[13];
} sig = {};
unsigned j;
/* There is a hypervisor, see what it is */
eax = 0x40000000U;
" cpuid \n\t"
" mov %%ebx, %1 \n\t"
: "0" (eax)
);
for (j = 0; j < ELEMENTSOF(cpuid_vendor_table); j ++)
return cpuid_vendor_table[j].id;
return VIRTUALIZATION_VM_OTHER;
}
#endif
return VIRTUALIZATION_NONE;
}
static int detect_vm_device_tree(void) {
int r;
if (r == -ENOENT) {
if (!dir) {
return VIRTUALIZATION_NONE;
return -errno;
}
return VIRTUALIZATION_QEMU;
return VIRTUALIZATION_NONE;
} else if (r < 0)
return r;
return VIRTUALIZATION_KVM;
return VIRTUALIZATION_XEN;
else
return VIRTUALIZATION_VM_OTHER;
#else
return VIRTUALIZATION_NONE;
#endif
}
static int detect_vm_dmi(void) {
/* Both CPUID and DMI are x86 specific interfaces... */
#if defined(__i386__) || defined(__x86_64__)
static const char *const dmi_vendors[] = {
"/sys/class/dmi/id/sys_vendor",
"/sys/class/dmi/id/board_vendor",
"/sys/class/dmi/id/bios_vendor"
};
static const struct {
const char *vendor;
int id;
} dmi_vendor_table[] = {
{ "QEMU", VIRTUALIZATION_QEMU },
{ "VMware", VIRTUALIZATION_VMWARE },
{ "VMW", VIRTUALIZATION_VMWARE },
{ "innotek GmbH", VIRTUALIZATION_ORACLE },
{ "Xen", VIRTUALIZATION_XEN },
{ "Bochs", VIRTUALIZATION_BOCHS },
{ "Parallels", VIRTUALIZATION_PARALLELS },
};
unsigned i;
int r;
for (i = 0; i < ELEMENTSOF(dmi_vendors); i++) {
_cleanup_free_ char *s = NULL;
unsigned j;
r = read_one_line_file(dmi_vendors[i], &s);
if (r < 0) {
if (r == -ENOENT)
continue;
return r;
}
for (j = 0; j < ELEMENTSOF(dmi_vendor_table); j++)
return dmi_vendor_table[j].id;
}
#endif
return VIRTUALIZATION_NONE;
}
static int detect_vm_xen(void) {
char *cap, *i;
int r;
if (r == -ENOENT)
return VIRTUALIZATION_NONE;
i = domcap;
break;
}
static int detect_vm_hypervisor(void) {
int r;
if (r == -ENOENT)
return VIRTUALIZATION_NONE;
if (r < 0)
return r;
return VIRTUALIZATION_XEN;
else
return VIRTUALIZATION_VM_OTHER;
}
static int detect_vm_uml(void) {
int r;
if (r < 0)
return r;
return VIRTUALIZATION_UML;
return VIRTUALIZATION_NONE;
}
static int detect_vm_zvm(void) {
#if defined(__s390__)
_cleanup_free_ char *t = NULL;
int r;
if (r == -ENOENT)
return VIRTUALIZATION_NONE;
if (r < 0)
return r;
return VIRTUALIZATION_ZVM;
else
return VIRTUALIZATION_KVM;
#else
return VIRTUALIZATION_NONE;
#endif
}
/* Returns a short identifier for the various VM implementations */
int detect_vm(void) {
int r;
if (cached_found >= 0)
return cached_found;
/* Try xen capabilities file first, if not found try
* high-level hypervisor sysfs file:
*
r = detect_vm_xen();
if (r < 0)
return r;
if (r != VIRTUALIZATION_NONE)
goto finish;
r = detect_vm_dmi();
if (r < 0)
return r;
if (r != VIRTUALIZATION_NONE)
goto finish;
r = detect_vm_cpuid();
if (r < 0)
return r;
if (r != VIRTUALIZATION_NONE)
goto finish;
r = detect_vm_hypervisor();
if (r < 0)
return r;
if (r != VIRTUALIZATION_NONE)
goto finish;
r = detect_vm_device_tree();
if (r < 0)
return r;
if (r != VIRTUALIZATION_NONE)
goto finish;
r = detect_vm_uml();
if (r < 0)
return r;
if (r != VIRTUALIZATION_NONE)
goto finish;
r = detect_vm_zvm();
if (r < 0)
return r;
cached_found = r;
return r;
}
int detect_container(void) {
static const struct {
const char *value;
int id;
} value_table[] = {
{ "lxc", VIRTUALIZATION_LXC },
{ "lxc-libvirt", VIRTUALIZATION_LXC_LIBVIRT },
{ "systemd-nspawn", VIRTUALIZATION_SYSTEMD_NSPAWN },
{ "docker", VIRTUALIZATION_DOCKER },
};
_cleanup_free_ char *m = NULL;
const char *e = NULL;
unsigned j;
int r;
if (cached_found >= 0)
return cached_found;
goto finish;
}
if (getpid() == 1) {
/* If we are PID 1 we can just check our own
* environment variable */
e = getenv("container");
if (isempty(e)) {
r = VIRTUALIZATION_NONE;
goto finish;
}
} else {
/* Otherwise, PID 1 dropped this information into a
* file in /run. This is better than accessing
* /proc/1/environ, since we don't need CAP_SYS_PTRACE
* for that. */
r = read_one_line_file("/run/systemd/container", &m);
if (r == -ENOENT) {
/* Fallback for cases where PID 1 was not
* systemd (for example, cases where
if (r <= 0) {
/* If that didn't work, give up,
* assume no container manager.
*
* Note: This means we still cannot
* is passed but privileges dropped,
* as /proc/1/environ is only readable
* with privileges. */
r = VIRTUALIZATION_NONE;
goto finish;
}
}
if (r < 0)
return r;
e = m;
}
for (j = 0; j < ELEMENTSOF(value_table); j++)
r = value_table[j].id;
goto finish;
}
r = VIRTUALIZATION_NONE;
cached_found = r;
return r;
}
int detect_virtualization(void) {
int r;
r = detect_container();
if (r != 0)
return r;
return detect_vm();
}
static const char *const virtualization_table[_VIRTUALIZATION_MAX] = {
[VIRTUALIZATION_NONE] = "none",
[VIRTUALIZATION_KVM] = "kvm",
[VIRTUALIZATION_QEMU] = "qemu",
[VIRTUALIZATION_BOCHS] = "bochs",
[VIRTUALIZATION_XEN] = "xen",
[VIRTUALIZATION_UML] = "uml",
[VIRTUALIZATION_VMWARE] = "vmware",
[VIRTUALIZATION_ORACLE] = "oracle",
[VIRTUALIZATION_MICROSOFT] = "microsoft",
[VIRTUALIZATION_ZVM] = "zvm",
[VIRTUALIZATION_PARALLELS] = "parallels",
[VIRTUALIZATION_VM_OTHER] = "vm-other",
[VIRTUALIZATION_SYSTEMD_NSPAWN] = "systemd-nspawn",
[VIRTUALIZATION_LXC_LIBVIRT] = "lxc-libvirt",
[VIRTUALIZATION_LXC] = "lxc",
[VIRTUALIZATION_OPENVZ] = "openvz",
[VIRTUALIZATION_DOCKER] = "docker",
[VIRTUALIZATION_CONTAINER_OTHER] = "container-other",
};