+/* SPDX-License-Identifier: LGPL-2.1+ */
/***
This file is part of systemd.
#include "env-util.h"
#include "fd-util.h"
#include "fileio.h"
-#include "fs-util.h"
#include "macro.h"
#include "process-util.h"
#include "stat-util.h"
} cpuid_vendor_table[] = {
{ "XenVMMXenVMM", VIRTUALIZATION_XEN },
{ "KVMKVMKVM", VIRTUALIZATION_KVM },
+ { "TCGTCGTCGTCG", VIRTUALIZATION_QEMU },
/* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
{ "VMwareVMware", VIRTUALIZATION_VMWARE },
/* https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfs */
}
static int detect_vm_xen(void) {
+
/* Check for Dom0 will be executed later in detect_vm_xen_dom0
Thats why we dont check the content of /proc/xen/capabilities here. */
if (access("/proc/xen/capabilities", F_OK) < 0) {
}
log_debug("Virtualization XEN found (/proc/xen/capabilities exists)");
- return VIRTUALIZATION_XEN;
-
+ return VIRTUALIZATION_XEN;
}
static bool detect_vm_xen_dom0(void) {
/* Returns a short identifier for the various VM implementations */
int detect_vm(void) {
static thread_local int cached_found = _VIRTUALIZATION_INVALID;
- int r, cpuid;
+ int r, dmi;
+ bool other = false;
if (cached_found >= 0)
return cached_found;
/* We have to use the correct order here:
*
- * -> First try to detect qemu/kvm and return 'kvm'.
- * -> Some virtualization technologies do use KVM hypervisor but are
- * expected to be detected as something else. Virtualbox since
- * version 5.0 is an example. So detect DMI next.
- * -> Get infos from CPUID third. */
-
- cpuid = detect_vm_cpuid();
- r = detect_vm_dmi();
-
- if (r == VIRTUALIZATION_QEMU && cpuid == VIRTUALIZATION_KVM)
- return cpuid;
+ * -> First try to detect Oracle Virtualbox, even if it uses KVM.
+ * -> Second try to detect from cpuid, this will report KVM for
+ * whatever software is used even if info in dmi is overwritten.
+ * -> Third try to detect from dmi. */
+
+ dmi = detect_vm_dmi();
+ if (dmi == VIRTUALIZATION_ORACLE) {
+ r = dmi;
+ goto finish;
+ }
+ r = detect_vm_cpuid();
if (r < 0)
return r;
- if (r != VIRTUALIZATION_NONE)
- goto finish;
+ if (r != VIRTUALIZATION_NONE) {
+ if (r == VIRTUALIZATION_VM_OTHER)
+ other = true;
+ else
+ goto finish;
+ }
- r = cpuid;
+ r = dmi;
if (r < 0)
return r;
- if (r != VIRTUALIZATION_NONE)
- goto finish;
+ if (r != VIRTUALIZATION_NONE) {
+ if (r == VIRTUALIZATION_VM_OTHER)
+ other = true;
+ else
+ goto finish;
+ }
/* x86 xen will most likely be detected by cpuid. If not (most likely
* because we're not an x86 guest), then we should try the xen capabilities
r = detect_vm_xen();
if (r < 0)
return r;
- if (r != VIRTUALIZATION_NONE)
- goto finish;
+ if (r != VIRTUALIZATION_NONE) {
+ if (r == VIRTUALIZATION_VM_OTHER)
+ other = true;
+ else
+ goto finish;
+ }
r = detect_vm_hypervisor();
if (r < 0)
return r;
- if (r != VIRTUALIZATION_NONE)
- goto finish;
+ if (r != VIRTUALIZATION_NONE) {
+ if (r == VIRTUALIZATION_VM_OTHER)
+ other = true;
+ else
+ goto finish;
+ }
r = detect_vm_device_tree();
if (r < 0)
return r;
- if (r != VIRTUALIZATION_NONE)
- goto finish;
+ if (r != VIRTUALIZATION_NONE) {
+ if (r == VIRTUALIZATION_VM_OTHER)
+ other = true;
+ else
+ goto finish;
+ }
r = detect_vm_uml();
if (r < 0)
return r;
- if (r != VIRTUALIZATION_NONE)
- goto finish;
+ if (r != VIRTUALIZATION_NONE) {
+ if (r == VIRTUALIZATION_VM_OTHER)
+ other = true;
+ else
+ goto finish;
+ }
r = detect_vm_zvm();
if (r < 0)
* double-check it */
if (r == VIRTUALIZATION_XEN && detect_vm_xen_dom0())
r = VIRTUALIZATION_NONE;
+ else if (r == VIRTUALIZATION_NONE && other)
+ r = VIRTUALIZATION_VM_OTHER;
cached_found = r;
log_debug("Found VM virtualization %s", virtualization_to_string(r));
goto finish;
}
- if (getpid() == 1) {
+ if (getpid_cached() == 1) {
/* If we are PID 1 we can just check our own environment variable, and that's authoritative. */
e = getenv("container");
}
int running_in_chroot(void) {
- _cleanup_free_ char *self_mnt = NULL, *pid1_mnt = NULL;
- int r;
-
- /* Try to detect whether we are running in a chroot() environment. Specifically, check whether we have a
- * different root directory than PID 1, even though we live in the same mount namespace as it. */
+ int ret;
if (getenv_bool("SYSTEMD_IGNORE_CHROOT") > 0)
return 0;
- r = files_same("/proc/1/root", "/");
- if (r < 0)
- return r;
- if (r > 0)
- return 0;
-
- r = readlink_malloc("/proc/self/ns/mnt", &self_mnt);
- if (r < 0)
- return r;
-
- r = readlink_malloc("/proc/1/ns/mnt", &pid1_mnt);
- if (r < 0)
- return r;
+ ret = files_same("/proc/1/root", "/", 0);
+ if (ret < 0)
+ return ret;
- return streq(self_mnt, pid1_mnt); /* Only if we live in the same namespace! */
+ return ret == 0;
}
static const char *const virtualization_table[_VIRTUALIZATION_MAX] = {