[libvirt] PATCH: 9/11: Dynamically register storage backends

Daniel P. Berrange berrange at redhat.com
Thu Oct 30 13:41:45 UTC 2008


There is an unfortunate cylic dependancy between the storage_backend.c
file and all the storage backend implementations due to the statically
declared table of drivers. This patch addresses the problem by making
the table of backends dynamically registered. The storage driver will
thus register each backend explicitly at its startup. The storage_backend.c
file is thus now completely generic, and has no compile time dep on the
individual drivers. I should have written it this way in the first
place :-)

 storage_backend.c |   44 +++++++++++---------------------------------
 storage_backend.h |    1 +
 storage_driver.c  |   37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 49 insertions(+), 33 deletions(-)

Daniel

diff -r d70577bacf78 src/storage_backend.c
--- a/src/storage_backend.c	Wed Oct 29 17:51:07 2008 +0000
+++ b/src/storage_backend.c	Wed Oct 29 18:01:58 2008 +0000
@@ -47,49 +47,27 @@
 
 #include "storage_backend.h"
 
-#if WITH_STORAGE_LVM
-#include "storage_backend_logical.h"
-#endif
-#if WITH_STORAGE_ISCSI
-#include "storage_backend_iscsi.h"
-#endif
-#if WITH_STORAGE_DISK
-#include "storage_backend_disk.h"
-#endif
-#if WITH_STORAGE_DIR
-#include "storage_backend_fs.h"
-#endif
-
 VIR_ENUM_IMPL(virStorageBackendPartTable,
               VIR_STORAGE_POOL_DISK_LAST,
               "unknown", "dos", "dvh", "gpt",
               "mac", "bsd", "pc98", "sun", "lvm2");
 
-static virStorageBackendPtr backends[] = {
-#if WITH_STORAGE_DIR
-    &virStorageBackendDirectory,
-#endif
-#if WITH_STORAGE_FS
-    &virStorageBackendFileSystem,
-    &virStorageBackendNetFileSystem,
-#endif
-#if WITH_STORAGE_LVM
-    &virStorageBackendLogical,
-#endif
-#if WITH_STORAGE_ISCSI
-    &virStorageBackendISCSI,
-#endif
-#if WITH_STORAGE_DISK
-    &virStorageBackendDisk,
-#endif
-    NULL
-};
+static unsigned nbackends = 0;
+static virStorageBackendPtr *backends = NULL;
 
+
+int virStorageBackendRegister(virStorageBackendPtr bk) {
+    if (VIR_REALLOC_N(backends, nbackends+1) < 0)
+        return -1;
+
+    backends[nbackends++] = bk;
+    return 0;
+}
 
 virStorageBackendPtr
 virStorageBackendForType(int type) {
     unsigned int i;
-    for (i = 0; backends[i]; i++)
+    for (i = 0; i < nbackends; i++)
         if (backends[i]->type == type)
             return backends[i];
 
diff -r d70577bacf78 src/storage_backend.h
--- a/src/storage_backend.h	Wed Oct 29 17:51:07 2008 +0000
+++ b/src/storage_backend.h	Wed Oct 29 18:01:58 2008 +0000
@@ -120,6 +120,7 @@
     int volType;
 };
 
+int virStorageBackendRegister(virStorageBackendPtr bk);
 
 virStorageBackendPtr virStorageBackendForType(int type);
 virStorageBackendPoolOptionsPtr virStorageBackendPoolOptionsForType(int type);
diff -r d70577bacf78 src/storage_driver.c
--- a/src/storage_driver.c	Wed Oct 29 17:51:07 2008 +0000
+++ b/src/storage_driver.c	Wed Oct 29 18:01:58 2008 +0000
@@ -40,6 +40,20 @@
 #include "storage_conf.h"
 #include "memory.h"
 #include "storage_backend.h"
+
+
+#if WITH_STORAGE_LVM
+#include "storage_backend_logical.h"
+#endif
+#if WITH_STORAGE_ISCSI
+#include "storage_backend_iscsi.h"
+#endif
+#if WITH_STORAGE_DISK
+#include "storage_backend_disk.h"
+#endif
+#if WITH_STORAGE_DIR
+#include "storage_backend_fs.h"
+#endif
 
 #define storageLog(msg...) fprintf(stderr, msg)
 
@@ -96,6 +110,29 @@
     struct passwd *pw;
     char *base = NULL;
     char driverConf[PATH_MAX];
+
+#if WITH_STORAGE_DIR
+    if (virStorageBackendRegister(&virStorageBackendDirectory) < 0)
+        return -1;
+#endif
+#if WITH_STORAGE_FS
+    if (virStorageBackendRegister(&virStorageBackendFileSystem) < 0)
+        return -1;
+    if (virStorageBackendRegister(&virStorageBackendNetFileSystem) < 0)
+        return -1;
+#endif
+#if WITH_STORAGE_LVM
+    if (virStorageBackendRegister(&virStorageBackendLogical) < 0)
+        return -1;
+#endif
+#if WITH_STORAGE_ISCSI
+    if (virStorageBackendRegister(&virStorageBackendISCSI) < 0)
+        return -1;
+#endif
+#if WITH_STORAGE_DISK
+    if (virStorageBackendRegister(&virStorageBackendDisk) < 0)
+        return -1;
+#endif
 
     if (VIR_ALLOC(driverState) < 0)
         return -1;

-- 
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|




More information about the libvir-list mailing list