]> xenbits.xensource.com Git - libvirt.git/commitdiff
Load nbd module before running qemu-nbd
authorCédric Bosdonnat <cbosdonnat@suse.com>
Mon, 20 Jul 2015 12:37:48 +0000 (14:37 +0200)
committerCédric Bosdonnat <cbosdonnat@suse.com>
Thu, 30 Jul 2015 07:55:37 +0000 (09:55 +0200)
So far qemu-nbd is run even if the nbd kernel module isn't loaded. This
leads to errors when the user starts his lxc container while libvirt
could easily load the nbd module automatically.

src/util/virfile.c

index 61f6e4dec2db37db813637ac3ccdd8e55e1bb5b3..76f1b7a249808f7fc02b7717dfc59b73b95cc7a4 100644 (file)
@@ -63,6 +63,7 @@
 #include "vircommand.h"
 #include "virerror.h"
 #include "virfile.h"
+#include "virkmod.h"
 #include "virlog.h"
 #include "virprocess.h"
 #include "virstring.h"
@@ -745,6 +746,7 @@ int virFileLoopDeviceAssociate(const char *file,
 
 
 # define SYSFS_BLOCK_DIR "/sys/block"
+# define NBD_DRIVER "nbd"
 
 
 static int
@@ -811,18 +813,42 @@ virFileNBDDeviceFindUnused(void)
     return ret;
 }
 
+static bool
+virFileNBDLoadDriver(void)
+{
+    if (virKModIsBlacklisted(NBD_DRIVER)) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Failed to load nbd module: "
+                         "administratively prohibited"));
+        return false;
+    } else {
+        char *errbuf = NULL;
+
+        if ((errbuf = virKModLoad(NBD_DRIVER, true))) {
+            VIR_FREE(errbuf);
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Failed to load nbd module"));
+            return false;
+        }
+        VIR_FREE(errbuf);
+    }
+    return true;
+}
 
 int virFileNBDDeviceAssociate(const char *file,
                               virStorageFileFormat fmt,
                               bool readonly,
                               char **dev)
 {
-    char *nbddev;
+    char *nbddev = NULL;
     char *qemunbd = NULL;
     virCommandPtr cmd = NULL;
     int ret = -1;
     const char *fmtstr = NULL;
 
+    if (!virFileNBDLoadDriver())
+        goto cleanup;
+
     if (!(nbddev = virFileNBDDeviceFindUnused()))
         goto cleanup;