Blob Blame History Raw
From c7c9c9aba8445df4dcdf46e67860d4ab1ee6cf6b Mon Sep 17 00:00:00 2001
Message-Id: <c7c9c9aba8445df4dcdf46e67860d4ab1ee6cf6b@dist-git>
From: Michal Privoznik <mprivozn@redhat.com>
Date: Fri, 22 Nov 2019 16:43:59 +0100
Subject: [PATCH] qemu: Forcibly mknod() even if it exists
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Another weird bug appeared concerning qemu namespaces. Basically
the problem is as follows:

1) Issue an API that causes libvirt to create a node in domain's
   namespace, say /dev/nvme0n1 with 8:0 as major:minor (the API can
   be attach-disk for instance). Or simply create the node from a
   console by hand.

2) Detach the disk from qemu.

3) Do something that makes /dev/nvme0n1 change it's minor number.

4) Try to attach the disk again.

The problem is, in a few cases - like disk-detach - we don't
remove the corresponding /dev node from the mount namespace
(because it may be used by some other disk's backing chain). But
this creates a problem, because if the node changes its MAJ:MIN
numbers we don't propagate the change into the domain's
namespace. We do plain mknod() and ignore EEXIST which obviously
is not enough because it doesn't guarantee that the node has
updated MAJ:MIN pair.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
(cherry picked from commit cdd8a6690ee3fa4b4b8ca1d4531924bd33be136a)

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1775680

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Message-Id: <ae34b78e6bac1be642301ba44c41c49f32db6cb0.1574437416.git.mprivozn@redhat.com>
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
---
 src/qemu/qemu_domain.c | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)

diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 249ec4d259..f2ff610750 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -11111,16 +11111,14 @@ qemuDomainCreateDeviceRecursive(const char *device,
                                             allow_noent, ttl - 1) < 0)
             goto cleanup;
     } else if (isDev) {
-        if (create &&
-            mknod(devicePath, sb.st_mode, sb.st_rdev) < 0) {
-            if (errno == EEXIST) {
-                ret = 0;
-            } else {
+        if (create) {
+            unlink(devicePath);
+            if (mknod(devicePath, sb.st_mode, sb.st_rdev) < 0) {
                 virReportSystemError(errno,
                                      _("Failed to make device %s"),
                                      devicePath);
+                goto cleanup;
             }
-            goto cleanup;
         }
     } else if (isReg) {
         if (create &&
@@ -11889,17 +11887,12 @@ qemuDomainAttachDeviceMknodHelper(pid_t pid ATTRIBUTE_UNUSED,
     } else if (isDev) {
         VIR_DEBUG("Creating dev %s (%d,%d)",
                   data->file, major(data->sb.st_rdev), minor(data->sb.st_rdev));
+        unlink(data->file);
         if (mknod(data->file, data->sb.st_mode, data->sb.st_rdev) < 0) {
-            /* Because we are not removing devices on hotunplug, or
-             * we might be creating part of backing chain that
-             * already exist due to a different disk plugged to
-             * domain, accept EEXIST. */
-            if (errno != EEXIST) {
-                virReportSystemError(errno,
-                                     _("Unable to create device %s"),
-                                     data->file);
-                goto cleanup;
-            }
+            virReportSystemError(errno,
+                                 _("Unable to create device %s"),
+                                 data->file);
+            goto cleanup;
         } else {
             delDevice = true;
         }
-- 
2.24.0