chiark / gitweb /
Fix service file to match installed elogind binary location
[elogind.git] / src / basic / virt.c
index e25a274e807566e709f10c58ed46e70ac36cd6d5..0060bb84602dc4c6aa98b53df4e41626ad3418d7 100644 (file)
@@ -315,31 +315,31 @@ static int detect_vm_zvm(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;
 
         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;
 
-        r = cpuid;
+        r = dmi;
         if (r < 0)
                 return r;
         if (r != VIRTUALIZATION_NONE)
@@ -572,32 +572,18 @@ int running_in_userns(void) {
 #endif // 0
 
 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 0 /// elogind does not allow to ignore chroots, we are never init!
         if (getenv_bool("SYSTEMD_IGNORE_CHROOT") > 0)
                 return 0;
 #endif // 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] = {