summaryrefslogtreecommitdiff
path: root/src/basic/virt.c
diff options
context:
space:
mode:
authorOlaf Hering <olaf@aepfle.de>2017-12-08 22:21:42 +0100
committerSven Eden <yamakuzure@gmx.net>2017-12-08 22:21:42 +0100
commit2f0634c5b8b3c1dc31a6860a1fbf7b8955dab6e8 (patch)
treea7c6dece4eae8caba468400b089d7904649981c9 /src/basic/virt.c
parent6ec6a3e302de2f0af4373c174ee53dd0b2c081a0 (diff)
virt: use XENFEAT_dom0 to detect the hardware domain (#6442, #6662) (#7581)
The detection of ConditionVirtualisation= relies on the presence of /proc/xen/capabilities. If the file exists and contains the string "control_d", the running system is a dom0 and VIRTUALIZATION_NONE should be set. In case /proc/xen exists, or some sysfs files indicate "xen", VIRTUALIZATION_XEN should be set to indicate the system is a domU. With an (old) xenlinux based kernel, /proc/xen/capabilities is always available and the detection described above works always. But with a pvops based kernel, xenfs must be mounted on /proc/xen to get "capabilities". This is done by a proc-xen.mount unit, which is part of xen.git. Since the mounting happens "late", other units may be scheduled before "proc-xen.mount". If these other units make use of "ConditionVirtualisation=", the virtualization detection returns incorect results. detect_vm() will set VIRTUALIZATION_XEN because "xen" is found in sysfs. This value will be cached. Once xenfs is mounted, the next process that runs detect_vm() will get VIRTUALIZATION_NONE. This misdetection can be fixed by using /sys/hypervisor/properties/features, which exports the value returned by the "XENVER_get_features" hypercall. If the bit XENFEAT_dom0 is set, the domain is the "hardware domain". It is supposed to have permissions to access all hardware. The used sysfs file is available since v2.6.31. The commonly used term "dom0" refers to the control domain which runs the toolstack and has access to all hardware. But the virtualization host may be configured such that one dedicated domain becomes the "hardware domain", and another one the "toolstack domain".
Diffstat (limited to 'src/basic/virt.c')
-rw-r--r--src/basic/virt.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/basic/virt.c b/src/basic/virt.c
index 6a57edf41..406bd8214 100644
--- a/src/basic/virt.c
+++ b/src/basic/virt.c
@@ -227,12 +227,32 @@ static int detect_vm_xen(void) {
return VIRTUALIZATION_XEN;
}
+#define XENFEAT_dom0 11 /* xen/include/public/features.h */
+#define PATH_FEATURES "/sys/hypervisor/properties/features"
/* Returns -errno, or 0 for domU, or 1 for dom0 */
static int detect_vm_xen_dom0(void) {
_cleanup_free_ char *domcap = NULL;
char *cap, *i;
int r;
+ r = read_one_line_file(PATH_FEATURES, &domcap);
+ if (r < 0 && r != -ENOENT)
+ return r;
+ if (r == 0) {
+ unsigned long features;
+
+ r = safe_atolu(domcap, &features);
+ if (r == 0) {
+ r = !!(features & (1U << XENFEAT_dom0));
+ log_debug("Virtualization XEN, found %s with value %08lx, "
+ "XENFEAT_dom0 (indicating the 'hardware domain') is%s set.",
+ PATH_FEATURES, features, r ? "" : " not");
+ return r;
+ }
+ log_debug("Virtualization XEN, found %s, unhandled content '%s'",
+ PATH_FEATURES, domcap);
+ }
+
r = read_one_line_file("/proc/xen/capabilities", &domcap);
if (r == -ENOENT) {
log_debug("Virtualization XEN because /proc/xen/capabilities does not exist");