[systemd-devel] [PATCH resend] virt: detect_virtualization() do not abort on errors

Djalal Harouni tixxdz at opendz.org
Tue Feb 25 09:29:44 PST 2014


The detect_virtualization() logic is to test several mechanisms in order
to detect if we are in a container or a virtual machine. This implies that
these tests may fail at an undetermined point.

An example: detect_container() needs privileges where detect_vm() does
not, perhaps there are other cases.

Currently unprivileged code will fail in:
detect_virtualization() => detect_container() => running_in_chroot()

This makes detect_virtualization() fail even for non container machines
which prevents detection of virtual machines. Where in the other hand
unprivileged code is able to test the CPUID hypervisor bit without any
problem.

Since we are dealing here with some test mechanisms, then do not fail if
detect_container() returns error, just continue with detect_vm() and
give a chance to detect_vm_cpuid() and detect_vm_dmi(), then report
errors if any.

This makes "systemd-detect-virt" tool able to detect virtual machines
without any privileges.
---
For bugzilla records or backports:
 commit e9a2e453bbe352 fixed some perhaps unreported bugs, hostnamectl
 now reads data from the *privileged* "remote" systemd-hostnamed

Sorry for the double resend!

 src/shared/virt.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/src/shared/virt.c b/src/shared/virt.c
index ec2ddcf..4789c9e 100644
--- a/src/shared/virt.c
+++ b/src/shared/virt.c
@@ -279,19 +279,21 @@ finish:
 
 /* Returns a short identifier for the various VM/container implementations */
 int detect_virtualization(const char **id) {
-        int r;
+        int container, vm;
 
-        r = detect_container(id);
-        if (r < 0)
-                return r;
-        if (r > 0)
+        container = detect_container(id);
+        if (container > 0)
                 return VIRTUALIZATION_CONTAINER;
 
-        r = detect_vm(id);
-        if (r < 0)
-                return r;
-        if (r > 0)
+        vm = detect_vm(id);
+        if (vm > 0)
                 return VIRTUALIZATION_VM;
 
+        if (container < 0)
+                return container;
+
+        if (vm < 0)
+                return vm;
+
         return VIRTUALIZATION_NONE;
 }
-- 
1.8.5.3



More information about the systemd-devel mailing list