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

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