]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
qemu: Refactor qemuGetDriveSourceString to take virStorageSourcePtr
authorPeter Krempa <pkrempa@redhat.com>
Tue, 8 Apr 2014 15:50:50 +0000 (17:50 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 9 Apr 2014 12:39:34 +0000 (14:39 +0200)
Refactor the function to avoid multiple wrappers splitting identical
fields from the now common metadata struct.

The refactor is done by folding in the wrapper used for disk sources
which allows us to lookup secrets via the secret driver. This may allow
using stored secrets for snapshot disk images too in the future.

src/qemu/qemu_command.c
src/qemu/qemu_command.h
src/qemu/qemu_driver.c

index 8b58d5e68a51ea591e51349f7dd15427b62e70aa..3942a23fc339daa1c18caad895301deedfe79bd7 100644 (file)
@@ -3816,37 +3816,68 @@ qemuBuildNetworkDriveURI(int protocol,
 
 
 int
-qemuGetDriveSourceString(int type,
-                         const char *src,
-                         int protocol,
-                         size_t nhosts,
-                         virStorageNetHostDefPtr hosts,
-                         const char *username,
-                         const char *secret,
-                         char **path)
+qemuGetDriveSourceString(virStorageSourcePtr src,
+                         virConnectPtr conn,
+                         char **source)
 {
-    *path = NULL;
+    int actualType = virStorageSourceGetActualType(src);
+    char *secret = NULL;
+    char *username = NULL;
+    int ret = -1;
+
+    *source = NULL;
+
+    if (conn) {
+        if (actualType == VIR_STORAGE_TYPE_NETWORK &&
+            src->auth.username &&
+            (src->protocol == VIR_STORAGE_NET_PROTOCOL_ISCSI ||
+             src->protocol == VIR_STORAGE_NET_PROTOCOL_RBD)) {
+            bool encode = false;
+            int secretType = VIR_SECRET_USAGE_TYPE_ISCSI;
+            const char *protocol = virStorageNetProtocolTypeToString(src->protocol);
+
+            username = src->auth.username;
 
-    switch ((enum virStorageType) type) {
+            if (src->protocol == VIR_STORAGE_NET_PROTOCOL_RBD) {
+                /* qemu requires the secret to be encoded for RBD */
+                encode = true;
+                secretType = VIR_SECRET_USAGE_TYPE_CEPH;
+            }
+
+            if (!(secret = qemuGetSecretString(conn,
+                                               protocol,
+                                               encode,
+                                               src->auth.secretType,
+                                               username,
+                                               src->auth.secret.uuid,
+                                               src->auth.secret.usage,
+                                               secretType)))
+                goto cleanup;
+        }
+    }
+
+    switch ((enum virStorageType) actualType) {
     case VIR_STORAGE_TYPE_BLOCK:
     case VIR_STORAGE_TYPE_FILE:
     case VIR_STORAGE_TYPE_DIR:
-        if (!src)
-            return 1;
+        if (!src->path) {
+            ret = 1;
+            goto cleanup;
+        }
 
-        if (VIR_STRDUP(*path, src) < 0)
-            return -1;
+        if (VIR_STRDUP(*source, src->path) < 0)
+            goto cleanup;
 
         break;
 
     case VIR_STORAGE_TYPE_NETWORK:
-        if (!(*path = qemuBuildNetworkDriveURI(protocol,
-                                               src,
-                                               nhosts,
-                                               hosts,
-                                               username,
-                                               secret)))
-            return -1;
+        if (!(*source = qemuBuildNetworkDriveURI(src->protocol,
+                                                 src->path,
+                                                 src->nhosts,
+                                                 src->hosts,
+                                                 username,
+                                                 secret)))
+            goto cleanup;
         break;
 
     case VIR_STORAGE_TYPE_VOLUME:
@@ -3855,52 +3886,7 @@ qemuGetDriveSourceString(int type,
         break;
     }
 
-    return 0;
-}
-
-static int
-qemuDomainDiskGetSourceString(virConnectPtr conn,
-                              virDomainDiskDefPtr disk,
-                              char **source)
-{
-    int actualType = virStorageSourceGetActualType(&disk->src);
-    char *secret = NULL;
-    int ret = -1;
-
-    *source = NULL;
-
-    if (actualType == VIR_STORAGE_TYPE_NETWORK &&
-        disk->src.auth.username &&
-        (disk->src.protocol == VIR_STORAGE_NET_PROTOCOL_ISCSI ||
-         disk->src.protocol == VIR_STORAGE_NET_PROTOCOL_RBD)) {
-        bool encode = false;
-        int secretType = VIR_SECRET_USAGE_TYPE_ISCSI;
-
-        if (disk->src.protocol == VIR_STORAGE_NET_PROTOCOL_RBD) {
-            /* qemu requires the secret to be encoded for RBD */
-            encode = true;
-            secretType = VIR_SECRET_USAGE_TYPE_CEPH;
-        }
-
-        if (!(secret = qemuGetSecretString(conn,
-                                           virStorageNetProtocolTypeToString(disk->src.protocol),
-                                           encode,
-                                           disk->src.auth.secretType,
-                                           disk->src.auth.username,
-                                           disk->src.auth.secret.uuid,
-                                           disk->src.auth.secret.usage,
-                                           secretType)))
-            goto cleanup;
-    }
-
-    ret = qemuGetDriveSourceString(actualType,
-                                   disk->src.path,
-                                   disk->src.protocol,
-                                   disk->src.nhosts,
-                                   disk->src.hosts,
-                                   disk->src.auth.username,
-                                   secret,
-                                   source);
+    ret = 0;
 
  cleanup:
     VIR_FREE(secret);
@@ -4003,7 +3989,7 @@ qemuBuildDriveStr(virConnectPtr conn,
         break;
     }
 
-    if (qemuDomainDiskGetSourceString(conn, disk, &source) < 0)
+    if (qemuGetDriveSourceString(&disk->src, conn, &source) < 0)
         goto error;
 
     if (source &&
index 65ba42090d5be765954805ff2884e91af4803a34..d5e8a4d6713d80a5a12e73d8c9e78846c4b24d72 100644 (file)
@@ -314,13 +314,7 @@ qemuParseKeywords(const char *str,
                   int *retnkeywords,
                   int allowEmptyValue);
 
-int qemuGetDriveSourceString(int type,
-                             const char *src,
-                             int protocol,
-                             size_t nhosts,
-                             virStorageNetHostDefPtr hosts,
-                             const char *username,
-                             const char *secret,
-                             char **path);
-
+int qemuGetDriveSourceString(virStorageSourcePtr src,
+                             virConnectPtr conn,
+                             char **source);
 #endif /* __QEMU_COMMAND_H__*/
index 72834c25d993fa8f275425aa08401e6c5ea230ac..d26a22b67443efb0e6fc4271df393f6e2d78cfb6 100644 (file)
@@ -11999,23 +11999,6 @@ qemuDomainMigrateGetMaxSpeed(virDomainPtr dom,
 }
 
 
-static int
-qemuDomainSnapshotDiskGetSourceString(virDomainSnapshotDiskDefPtr disk,
-                                      char **source)
-{
-    *source = NULL;
-
-    return qemuGetDriveSourceString(virStorageSourceGetActualType(&disk->src),
-                                    disk->src.path,
-                                    disk->src.protocol,
-                                    disk->src.nhosts,
-                                    disk->src.hosts,
-                                    NULL,
-                                    NULL,
-                                    source);
-}
-
-
 typedef enum {
     VIR_DISK_CHAIN_NO_ACCESS,
     VIR_DISK_CHAIN_READ_ONLY,
@@ -12758,7 +12741,7 @@ qemuDomainSnapshotCreateSingleDiskActive(virQEMUDriverPtr driver,
     if (virStorageFileInit(&snap->src) < 0)
         goto cleanup;
 
-    if (qemuDomainSnapshotDiskGetSourceString(snap, &source) < 0)
+    if (qemuGetDriveSourceString(&snap->src, NULL, &source) < 0)
         goto cleanup;
 
     if (VIR_STRDUP(newsource, snap->src.path) < 0)