[libvirt] [PATCH 6/6] storage: create separate loadable modules for storage file drivers

Daniel P. Berrangé berrange at redhat.com
Wed Apr 25 15:52:43 UTC 2018


The storage file drivers are currently loaded as a side effect of
loading the storage driver. This is a bogus dependancy because the
storage file code has no interaction with the storage drivers, and
even ultimately be running in a completely separate daemon.

Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
---
 libvirt.spec.in                       |  4 ++
 src/storage/Makefile.inc.am           | 40 +++++++++++++++++++
 src/storage/storage_backend_fs.c      |  4 --
 src/storage/storage_backend_gluster.c |  4 --
 src/util/virstoragefile.c             | 10 +++--
 src/util/virstoragefilebackend.c      | 72 +++++++++++++++++++++++++++--------
 src/util/virstoragefilebackend.h      |  9 ++---
 7 files changed, 111 insertions(+), 32 deletions(-)

diff --git a/libvirt.spec.in b/libvirt.spec.in
index 5090dfa2aa..e8705bbb48 100644
--- a/libvirt.spec.in
+++ b/libvirt.spec.in
@@ -1384,6 +1384,8 @@ rm -f $RPM_BUILD_ROOT%{_libdir}/libvirt/connection-driver/*.la
 rm -f $RPM_BUILD_ROOT%{_libdir}/libvirt/connection-driver/*.a
 rm -f $RPM_BUILD_ROOT%{_libdir}/libvirt/storage-backend/*.la
 rm -f $RPM_BUILD_ROOT%{_libdir}/libvirt/storage-backend/*.a
+rm -f $RPM_BUILD_ROOT%{_libdir}/libvirt/storage-file/*.la
+rm -f $RPM_BUILD_ROOT%{_libdir}/libvirt/storage-file/*.a
 %if %{with_wireshark}
 rm -f $RPM_BUILD_ROOT%{_libdir}/wireshark/plugins/libvirt.la
 %endif
@@ -1886,6 +1888,7 @@ exit 0
 %attr(0755, root, root) %{_libexecdir}/libvirt_parthelper
 %{_libdir}/%{name}/connection-driver/libvirt_driver_storage.so
 %{_libdir}/%{name}/storage-backend/libvirt_storage_backend_fs.so
+%{_libdir}/%{name}/storage-file/libvirt_storage_file_fs.so
 
 %files daemon-driver-storage-disk
 %{_libdir}/%{name}/storage-backend/libvirt_storage_backend_disk.so
@@ -1905,6 +1908,7 @@ exit 0
 %if %{with_storage_gluster}
 %files daemon-driver-storage-gluster
 %{_libdir}/%{name}/storage-backend/libvirt_storage_backend_gluster.so
+%{_libdir}/%{name}/storage-file/libvirt_storage_file_gluster.so
 %endif
 
 %if %{with_storage_rbd}
diff --git a/src/storage/Makefile.inc.am b/src/storage/Makefile.inc.am
index af2c97ab93..ea98c0ee52 100644
--- a/src/storage/Makefile.inc.am
+++ b/src/storage/Makefile.inc.am
@@ -14,6 +14,9 @@ STORAGE_DRIVER_SOURCES = \
 STORAGE_DRIVER_FS_SOURCES = \
 	storage/storage_backend_fs.h \
 	storage/storage_backend_fs.c \
+	$(NULL)
+
+STORAGE_FILE_FS_SOURCES = \
 	storage/storage_file_fs.h \
 	storage/storage_file_fs.c \
 	$(NULL)
@@ -57,6 +60,9 @@ STORAGE_DRIVER_SHEEPDOG_SOURCES = \
 STORAGE_DRIVER_GLUSTER_SOURCES = \
 	storage/storage_backend_gluster.h \
 	storage/storage_backend_gluster.c \
+	$(NULL)
+
+STORAGE_FILE_GLUSTER_SOURCES = \
 	storage/storage_file_gluster.h \
 	storage/storage_file_gluster.c \
 	$(NULL)
@@ -80,6 +86,7 @@ STATEFUL_DRIVER_SOURCE_FILES += $(STORAGE_DRIVER_SOURCES)
 EXTRA_DIST += \
 	$(STORAGE_DRIVER_SOURCES) \
 	$(STORAGE_DRIVER_FS_SOURCES) \
+	$(STORAGE_FILE_FS_SOURCES) \
 	$(STORAGE_DRIVER_LVM_SOURCES) \
 	$(STORAGE_DRIVER_ISCSI_SOURCES) \
 	$(STORAGE_DRIVER_SCSI_SOURCES) \
@@ -88,6 +95,7 @@ EXTRA_DIST += \
 	$(STORAGE_DRIVER_RBD_SOURCES) \
 	$(STORAGE_DRIVER_SHEEPDOG_SOURCES) \
 	$(STORAGE_DRIVER_GLUSTER_SOURCES) \
+	$(STORAGE_FILE_GLUSTER_SOURCES) \
 	$(STORAGE_DRIVER_ZFS_SOURCES) \
 	$(STORAGE_DRIVER_VSTORAGE_SOURCES) \
 	$(STORAGE_HELPER_DISK_SOURCES) \
@@ -96,6 +104,9 @@ EXTRA_DIST += \
 storagebackenddir = $(libdir)/libvirt/storage-backend
 storagebackend_LTLIBRARIES =
 
+storagefiledir = $(libdir)/libvirt/storage-file
+storagefile_LTLIBRARIES =
+
 # Needed to keep automake quiet about conditionals
 libvirt_driver_storage_impl_la_SOURCES =
 libvirt_driver_storage_impl_la_CFLAGS = \
@@ -136,6 +147,19 @@ libvirt_storage_backend_fs_la_LIBADD = \
 	libvirt.la \
 	../gnulib/lib/libgnu.la \
 	$(NULL)
+
+libvirt_storage_file_fs_la_SOURCES = $(STORAGE_FILE_FS_SOURCES)
+libvirt_storage_file_fs_la_CFLAGS = \
+	-I$(srcdir)/conf \
+	$(AM_CFLAGS) \
+	$(NULL)
+
+storagefile_LTLIBRARIES += libvirt_storage_file_fs.la
+libvirt_storage_file_fs_la_LDFLAGS = $(AM_LDFLAGS_MOD)
+libvirt_storage_file_fs_la_LIBADD = \
+	libvirt.la \
+	../gnulib/lib/libgnu.la \
+	$(NULL)
 endif WITH_STORAGE
 
 if WITH_STORAGE_LVM
@@ -270,6 +294,22 @@ libvirt_storage_backend_gluster_la_CFLAGS = \
 
 storagebackend_LTLIBRARIES += libvirt_storage_backend_gluster.la
 libvirt_storage_backend_gluster_la_LDFLAGS = $(AM_LDFLAGS_MOD)
+
+
+libvirt_storage_file_gluster_la_SOURCES = $(STORAGE_FILE_GLUSTER_SOURCES)
+libvirt_storage_file_gluster_la_LIBADD = \
+	libvirt.la \
+	$(GLUSTERFS_LIBS) \
+	../gnulib/lib/libgnu.la \
+	$(NULL)
+libvirt_storage_file_gluster_la_CFLAGS = \
+	-I$(srcdir)/conf \
+	$(GLUSTERFS_CFLAGS) \
+	$(AM_CFLAGS) \
+	$(NULL)
+
+storagefile_LTLIBRARIES += libvirt_storage_file_gluster.la
+libvirt_storage_file_gluster_la_LDFLAGS = $(AM_LDFLAGS_MOD)
 endif WITH_STORAGE_GLUSTER
 
 if WITH_STORAGE_ZFS
diff --git a/src/storage/storage_backend_fs.c b/src/storage/storage_backend_fs.c
index bface86b43..37fea8c941 100644
--- a/src/storage/storage_backend_fs.c
+++ b/src/storage/storage_backend_fs.c
@@ -32,7 +32,6 @@
 
 #include "virerror.h"
 #include "storage_backend_fs.h"
-#include "storage_file_fs.h"
 #include "storage_util.h"
 #include "storage_conf.h"
 #include "vircommand.h"
@@ -713,8 +712,5 @@ virStorageBackendFsRegister(void)
         return -1;
 #endif /* WITH_STORAGE_FS */
 
-    if (virStorageFileFsRegister() < 0)
-        return -1;
-
     return 0;
 }
diff --git a/src/storage/storage_backend_gluster.c b/src/storage/storage_backend_gluster.c
index aca772676c..5d4b920a60 100644
--- a/src/storage/storage_backend_gluster.c
+++ b/src/storage/storage_backend_gluster.c
@@ -24,7 +24,6 @@
 #include <glusterfs/api/glfs.h>
 
 #include "storage_backend_gluster.h"
-#include "storage_file_gluster.h"
 #include "storage_conf.h"
 #include "viralloc.h"
 #include "virerror.h"
@@ -567,8 +566,5 @@ virStorageBackendGlusterRegister(void)
     if (virStorageBackendRegister(&virStorageBackendGluster) < 0)
         return -1;
 
-    if (virStorageFileGlusterRegister() < 0)
-        return -1;
-
     return 0;
 }
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index da13d51d32..73937b527a 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -4117,7 +4117,9 @@ virStorageFileGetBackendForSupportCheck(const virStorageSource *src,
 
     actualType = virStorageSourceGetActualType(src);
 
-    *backend = virStorageFileBackendForTypeInternal(actualType, src->protocol, false);
+    if (virStorageFileBackendForType(actualType, src->protocol, false, backend) < 0)
+        return -1;
+
     return 0;
 }
 
@@ -4234,8 +4236,10 @@ virStorageFileInitAs(virStorageSourcePtr src,
     else
         src->drv->gid = gid;
 
-    if (!(src->drv->backend = virStorageFileBackendForType(actualType,
-                                                           src->protocol)))
+    if (virStorageFileBackendForType(actualType,
+                                     src->protocol,
+                                     true,
+                                     &src->drv->backend) < 0)
         goto error;
 
     if (src->drv->backend->backendInit &&
diff --git a/src/util/virstoragefilebackend.c b/src/util/virstoragefilebackend.c
index df86ee39c2..ac087dabac 100644
--- a/src/util/virstoragefilebackend.c
+++ b/src/util/virstoragefilebackend.c
@@ -32,6 +32,7 @@
 #include "internal.h"
 #include "virstoragefilebackend.h"
 #include "virlog.h"
+#include "virmodule.h"
 #include "virfile.h"
 #include "configmake.h"
 
@@ -44,6 +45,46 @@ VIR_LOG_INIT("storage.storage_source_backend");
 static virStorageFileBackendPtr virStorageFileBackends[VIR_STORAGE_BACKENDS_MAX];
 static size_t virStorageFileBackendsCount;
 
+#define STORAGE_FILE_MODULE_DIR LIBDIR "/libvirt/storage-file"
+
+static int
+virStorageFileLoadBackendModule(const char *name,
+                                const char *regfunc,
+                                bool forceload)
+{
+    char *modfile = NULL;
+    int ret;
+
+    if (!(modfile = virFileFindResourceFull(name,
+                                            "libvirt_storage_file_",
+                                            ".so",
+                                            abs_topbuilddir "/src/.libs",
+                                            STORAGE_FILE_MODULE_DIR,
+                                            "LIBVIRT_STORAGE_FILE_DIR")))
+        return -1;
+
+    ret = virModuleLoad(modfile, regfunc, forceload);
+
+    VIR_FREE(modfile);
+
+    return ret;
+}
+
+
+static int virStorageFileBackendOnceInit(void)
+{
+#if WITH_STORAGE_DIR || WITH_STORAGE_FS
+    if (virStorageFileLoadBackendModule("fs", "virStorageFileFsRegister", false) < 0)
+        return -1;
+#endif /* WITH_STORAGE_DIR || WITH_STORAGE_FS */
+#if WITH_STORAGE_GLUSTER
+    if (virStorageFileLoadBackendModule("gluster", "virStorageFileGlusterRegister", false) < 0)
+        return -1;
+#endif /* WITH_STORAGE_GLUSTER */
+    return 0;
+}
+
+VIR_ONCE_GLOBAL_INIT(virStorageFileBackend)
 
 int
 virStorageFileBackendRegister(virStorageFileBackendPtr backend)
@@ -65,25 +106,32 @@ virStorageFileBackendRegister(virStorageFileBackendPtr backend)
     return 0;
 }
 
-virStorageFileBackendPtr
-virStorageFileBackendForTypeInternal(int type,
-                                     int protocol,
-                                     bool report)
+int
+virStorageFileBackendForType(int type,
+                             int protocol,
+                             bool required,
+                             virStorageFileBackendPtr *backend)
 {
     size_t i;
 
+    *backend = NULL;
+
+    if (virStorageFileBackendInitialize() < 0)
+        return -1;
+
     for (i = 0; i < virStorageFileBackendsCount; i++) {
         if (virStorageFileBackends[i]->type == type) {
             if (type == VIR_STORAGE_TYPE_NETWORK &&
                 virStorageFileBackends[i]->protocol != protocol)
                 continue;
 
-            return virStorageFileBackends[i];
+            *backend = virStorageFileBackends[i];
+            return 0;
         }
     }
 
-    if (!report)
-        return NULL;
+    if (!required)
+        return 0;
 
     if (type == VIR_STORAGE_TYPE_NETWORK) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -96,13 +144,5 @@ virStorageFileBackendForTypeInternal(int type,
                        virStorageTypeToString(type));
     }
 
-    return NULL;
-}
-
-
-virStorageFileBackendPtr
-virStorageFileBackendForType(int type,
-                             int protocol)
-{
-    return virStorageFileBackendForTypeInternal(type, protocol, true);
+    return -1;
 }
diff --git a/src/util/virstoragefilebackend.h b/src/util/virstoragefilebackend.h
index 6cd51750ee..6686864367 100644
--- a/src/util/virstoragefilebackend.h
+++ b/src/util/virstoragefilebackend.h
@@ -71,11 +71,10 @@ typedef int
                               uid_t uid,
                               gid_t gid);
 
-virStorageFileBackendPtr virStorageFileBackendForType(int type, int protocol);
-virStorageFileBackendPtr virStorageFileBackendForTypeInternal(int type,
-                                                              int protocol,
-                                                              bool report);
-
+int virStorageFileBackendForType(int type,
+                                 int protocol,
+                                 bool required,
+                                 virStorageFileBackendPtr *backend);
 
 struct _virStorageFileBackend {
     int type;
-- 
2.14.3




More information about the libvir-list mailing list