Blame SOURCES/libvirt-qemu_namespace-Fix-a-corner-case-in-qemuDomainGetPreservedMounts.patch

e9d682
From 6b3a0480cf2de402abce168aa0b093a8dc4f7a57 Mon Sep 17 00:00:00 2001
e9d682
Message-Id: <6b3a0480cf2de402abce168aa0b093a8dc4f7a57@dist-git>
e9d682
From: Michal Privoznik <mprivozn@redhat.com>
e9d682
Date: Tue, 6 Sep 2022 13:43:22 +0200
e9d682
Subject: [PATCH] qemu_namespace: Fix a corner case in
e9d682
 qemuDomainGetPreservedMounts()
e9d682
e9d682
When setting up namespace for QEMU we look at mount points under
e9d682
/dev (like /dev/pts, /dev/mqueue/, etc.) because we want to
e9d682
preserve those (which is done by moving them to a temp location,
e9d682
unshare(), and then moving them back). We have a convenience
e9d682
helper - qemuDomainGetPreservedMounts() - that processes the
e9d682
mount table and (optionally) moves the other filesystems too.
e9d682
This helper is also used when attempting to create a path in NS,
e9d682
because the path, while starting with "/dev/" prefix, may
e9d682
actually lead to one of those filesystems that we preserved.
e9d682
e9d682
And here comes the corner case: while we require the parent mount
e9d682
table to be in shared mode (equivalent of `mount --make-rshared /'),
e9d682
these mount events propagate iff the target path exist inside the
e9d682
slave mount table (= QEMU's private namespace). And since we
e9d682
create only a subset of /dev nodes, well, that assumption is not
e9d682
always the case.
e9d682
e9d682
For instance, assume that a domain is already running, no
e9d682
hugepages were configured for it nor any hugetlbfs is mounted.
e9d682
Now, when a hugetlbfs is mounted into '/dev/hugepages', this is
e9d682
propagated into the QEMU's namespace, but since the target dir
e9d682
does not exist in the private /dev, the FS is not mounted in the
e9d682
namespace.
e9d682
e9d682
Fortunately, this difference between namespaces is visible when
e9d682
comparing /proc/mounts and /proc/$PID/mounts (where PID is the
e9d682
QEMU's PID). Therefore, if possible we should look at the latter.
e9d682
e9d682
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
e9d682
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
e9d682
(cherry picked from commit 46b03819ae8d833b11c2aaccb2c2a0361727f51b)
e9d682
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2152083
e9d682
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
e9d682
---
e9d682
 src/qemu/qemu_namespace.c | 10 +++++++++-
e9d682
 1 file changed, 9 insertions(+), 1 deletion(-)
e9d682
e9d682
diff --git a/src/qemu/qemu_namespace.c b/src/qemu/qemu_namespace.c
e9d682
index 71e3366ca5..807ec37c91 100644
e9d682
--- a/src/qemu/qemu_namespace.c
e9d682
+++ b/src/qemu/qemu_namespace.c
e9d682
@@ -109,6 +109,8 @@ qemuDomainGetPreservedMountPath(virQEMUDriverConfig *cfg,
e9d682
  * b) generate backup path for all the entries in a)
e9d682
  *
e9d682
  * Any of the return pointers can be NULL. Both arrays are NULL-terminated.
e9d682
+ * Get the mount table either from @vm's PID (if running), or from the
e9d682
+ * namespace we're in (if @vm's not running).
e9d682
  *
e9d682
  * Returns 0 on success, -1 otherwise (with error reported)
e9d682
  */
e9d682
@@ -123,12 +125,18 @@ qemuDomainGetPreservedMounts(virQEMUDriverConfig *cfg,
e9d682
     size_t nmounts = 0;
e9d682
     g_auto(GStrv) paths = NULL;
e9d682
     g_auto(GStrv) savePaths = NULL;
e9d682
+    g_autofree char *mountsPath = NULL;
e9d682
     size_t i;
e9d682
 
e9d682
     if (ndevPath)
e9d682
         *ndevPath = 0;
e9d682
 
e9d682
-    if (virFileGetMountSubtree(QEMU_PROC_MOUNTS, "/dev", &mounts, &nmounts) < 0)
e9d682
+    if (vm->pid > 0)
e9d682
+        mountsPath = g_strdup_printf("/proc/%lld/mounts", (long long) vm->pid);
e9d682
+    else
e9d682
+        mountsPath = g_strdup(QEMU_PROC_MOUNTS);
e9d682
+
e9d682
+    if (virFileGetMountSubtree(mountsPath, "/dev", &mounts, &nmounts) < 0)
e9d682
         return -1;
e9d682
 
e9d682
     if (nmounts == 0)
e9d682
-- 
e9d682
2.39.0
e9d682