[PATCH v2 2/2] virdevmapper: Handle kernel without device-mapper support

Michal Privoznik mprivozn at redhat.com
Tue Aug 18 09:30:25 UTC 2020


In one of my latest patch (v6.6.0~30) I was trying to remove
libdevmapper use in favor of our own implementation. However, the
code did not take into account that device mapper can be not
compiled into the kernel (e.g. be a separate module that's not
loaded) in which case /proc/devices won't have the device-mapper
major number and thus virDevMapperGetTargets() and/or
virIsDevMapperDevice() fails.

Fixes: 22494556542c676d1b9e7f1c1f2ea13ac17e1e3e
Reported-by: Andrea Bolognani <abologna at redhat.com>
Reported-by: Christian Ehrhardt <christian.ehrhardt at canonical.com>
Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 src/util/virdevmapper.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/src/util/virdevmapper.c b/src/util/virdevmapper.c
index b24d9377f8..3cc399f382 100644
--- a/src/util/virdevmapper.c
+++ b/src/util/virdevmapper.c
@@ -54,6 +54,9 @@ virDevMapperGetMajor(unsigned int *major)
     VIR_AUTOSTRINGLIST lines = NULL;
     size_t i;
 
+    if (!virFileExists(CONTROL_PATH))
+        return -2;
+
     if (virFileReadAll(PROC_DEVICES, BUF_SIZE, &buf) < 0)
         return -1;
 
@@ -126,8 +129,13 @@ virDMOpen(void)
 
     memset(&dm, 0, sizeof(dm));
 
-    if ((controlFD = open(CONTROL_PATH, O_RDWR)) < 0)
+    if ((controlFD = open(CONTROL_PATH, O_RDWR)) < 0) {
+        if (errno == ENOENT)
+            return -2;
+
+        virReportSystemError(errno, _("Unable to open %s"), CONTROL_PATH);
         return -1;
+    }
 
     if (!virDMIoctl(controlFD, DM_VERSION, &dm, &tmp)) {
         virReportSystemError(errno, "%s",
@@ -299,8 +307,16 @@ virDevMapperGetTargets(const char *path,
      * consist of devices or yet another targets. If that's the
      * case, we have to stop recursion somewhere. */
 
-    if ((controlFD = virDMOpen()) < 0)
+    if ((controlFD = virDMOpen()) < 0) {
+        if (controlFD == -2) {
+            /* The CONTROL_PATH doesn't exist. Probably the
+             * module isn't loaded, yet. Don't error out, just
+             * exit. */
+            return 0;
+        }
+
         return -1;
+    }
 
     return virDevMapperGetTargetsImpl(controlFD, path, devPaths, ttl);
 }
-- 
2.26.2




More information about the libvir-list mailing list