From dfef23e1b6c0c89b17b7b52f6ec1537649a8c60b Mon Sep 17 00:00:00 2001 From: Thomas Blume Date: Fri, 6 Jun 2014 16:36:45 +0200 Subject: [PATCH] systemd-detect-virt: only discover Xen domU The current vm detection lacks the distinction between Xen dom0 and Xen domU. Both, dom0 and domU are running inside the hypervisor. Therefore systemd-detect-virt and the ConditionVirtualization directive detect dom0 as a virtual machine. dom0 is not using virtual devices but is accessing the real hardware. Therefore dom0 should be considered the virtualisation host and not a virtual machine. https://bugs.freedesktop.org/show_bug.cgi?id=77271 (cherry picked from commit 37287585b6ba9a55065c8f94458f6db3c0abe0af) (cherry picked from commit f22d2ebe687ad842f4bed11df918ed07a0bb3e78) Conflicts: src/shared/virt.c --- src/shared/virt.c | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/shared/virt.c b/src/shared/virt.c index 4e18638..e59f2b1 100644 --- a/src/shared/virt.c +++ b/src/shared/virt.c @@ -149,7 +149,7 @@ static int detect_vm_dmi(const char **_id) { /* Returns a short identifier for the various VM implementations */ int detect_vm(const char **id) { - _cleanup_free_ char *hvtype = NULL, *cpuinfo_contents = NULL; + _cleanup_free_ char *domcap = NULL, *cpuinfo_contents = NULL; static __thread int cached_found = -1; static __thread const char *cached_id = NULL; const char *_id = NULL; @@ -163,17 +163,37 @@ int detect_vm(const char **id) { return cached_found; } - /* Try high-level hypervisor sysfs file first: + /* Try xen capabilities file first, if not found try high-level hypervisor sysfs file: * - * https://bugs.freedesktop.org/show_bug.cgi?id=61491 */ - r = read_one_line_file("/sys/hypervisor/type", &hvtype); + * https://bugs.freedesktop.org/show_bug.cgi?id=77271 */ + r = read_one_line_file("/proc/xen/capabilities", &domcap); if (r >= 0) { - if (streq(hvtype, "xen")) { + char *cap, *i = domcap; + + while ((cap = strsep(&i, ","))) + if (streq(cap, "control_d")) + break; + + if (!i) { _id = "xen"; r = 1; - goto finish; } - } else if (r != -ENOENT) + + goto finish; + + } else if (r == -ENOENT) { + _cleanup_free_ char *hvtype = NULL; + + r = read_one_line_file("/sys/hypervisor/type", &hvtype); + if (r >= 0) { + if (streq(hvtype, "xen")) { + _id = "xen"; + r = 1; + goto finish; + } + } else if (r != -ENOENT) + return r; + } else return r; /* this will set _id to "other" and return 0 for unknown hypervisors */