[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH v2 4/7] qemu: Translate the iscsi pool/volume disk source



The difference with already supported pool types (dir, fs, block)
is: there are two modes for iscsi pool (or network pools in future),
one can specify it either to use the volume target path (the path
showed up on host) with mode='host', or to use the remote URI qemu
supports (e.g. file=iscsi://example.org:6000/iqn.1992-01.com.example/1)
with mode='uri'.

For 'host' mode, it copies the volume target path into disk->src. For
'uri' mode, the corresponding info in the *one* pool source host def
are copied to disk->hosts[0].
---
 src/conf/domain_conf.h  |  1 +
 src/qemu/qemu_command.c | 15 +++++++-
 src/qemu/qemu_conf.c    | 94 ++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 108 insertions(+), 2 deletions(-)

diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 05400ce..82f74ca 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -677,6 +677,7 @@ struct _virDomainDiskSourcePoolDef {
     char *pool; /* pool name */
     char *volume; /* volume name */
     int voltype; /* enum virStorageVolType, internal only */
+    int pooltype; /* enum virStoragePoolType, internal only */
     int mode; /* enum virDomainDiskSourcePoolMode */
 };
 typedef virDomainDiskSourcePoolDef *virDomainDiskSourcePoolDefPtr;
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 591279b..b5c1400 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -3204,7 +3204,20 @@ qemuBuildDriveStr(virConnectPtr conn ATTRIBUTE_UNUSED,
                                      "block type volume"));
                     goto error;
                 }
-                virBufferEscape(&opt, ',', ",", "file=%s,", disk->src);
+
+                if (disk->srcpool->pooltype == VIR_STORAGE_POOL_ISCSI) {
+                    if (disk->srcpool->mode ==
+                        VIR_DOMAIN_DISK_SOURCE_POOL_MODE_URI) {
+                        if (qemuBuildISCSIString(conn, disk, &opt) < 0)
+                            goto error;
+                        virBufferAddChar(&opt, ',');
+                    } else if (disk->srcpool->mode ==
+                               VIR_DOMAIN_DISK_SOURCE_POOL_MODE_HOST) {
+                        virBufferEscape(&opt, ',', ",", "file=%s,", disk->src);
+                    }
+                } else {
+                    virBufferEscape(&opt, ',', ",", "file=%s,", disk->src);
+                }
                 break;
             case VIR_STORAGE_VOL_FILE:
                 virBufferEscape(&opt, ',', ",", "file=%s,", disk->src);
diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c
index 4fa0055..8fb3cda 100644
--- a/src/qemu/qemu_conf.c
+++ b/src/qemu/qemu_conf.c
@@ -1418,12 +1418,68 @@ int qemuDriverAllocateID(virQEMUDriverPtr driver)
     return virAtomicIntInc(&driver->nextvmid);
 }
 
+static int
+qemuAddDiskISCSIUri(virDomainDiskDefPtr def,
+                    virStoragePoolDefPtr pooldef)
+{
+    int ret = -1;
+    char **tokens = NULL;
+
+    /* iscsi pool only supports one host */
+    def->nhosts = 1;
+
+    if (VIR_ALLOC_N(def->hosts, def->nhosts) < 0)
+        goto cleanup;
+
+    if (VIR_STRDUP(def->hosts[0].name, pooldef->source.hosts[0].name) < 0)
+        goto cleanup;
+
+    if (virAsprintf(&def->hosts[0].port, "%d",
+                    pooldef->source.hosts[0].port ?
+                    pooldef->source.hosts[0].port :
+                    3260) < 0)
+        goto cleanup;
+
+    /* iscsi volume has name like "unit:0:0:1" */
+    if (!(tokens = virStringSplit(def->srcpool->volume, ":", 0)))
+        goto cleanup;
+
+    if (virStringListLength(tokens) != 4) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("unexpected iscsi volume name '%s'"),
+                       def->srcpool->volume);
+        goto cleanup;
+    }
+
+    /* iscsi pool has only one source device path */
+    if (virAsprintf(&def->src, "%s/%s",
+                    pooldef->source.devices[0].path,
+                    tokens[3]) < 0)
+        goto cleanup;
+
+    /* Storage pool have not supported these 2 attributes yet,
+     * use the defaults.
+     */
+    def->hosts[0].transport = VIR_DOMAIN_DISK_PROTO_TRANS_TCP;
+    def->hosts[0].socket = NULL;
+
+    def->protocol = VIR_DOMAIN_DISK_PROTOCOL_ISCSI;
+
+    ret = 0;
+
+cleanup:
+    virStringFreeList(tokens);
+    return ret;
+}
+
 int
 qemuTranslateDiskSourcePool(virConnectPtr conn,
                             virDomainDiskDefPtr def)
 {
+    virStoragePoolDefPtr pooldef = NULL;
     virStoragePoolPtr pool = NULL;
     virStorageVolPtr vol = NULL;
+    char *poolxml = NULL;
     virStorageVolInfo info;
     int ret = -1;
 
@@ -1451,11 +1507,45 @@ qemuTranslateDiskSourcePool(virConnectPtr conn,
 
     switch (info.type) {
     case VIR_STORAGE_VOL_FILE:
-    case VIR_STORAGE_VOL_BLOCK:
     case VIR_STORAGE_VOL_DIR:
         if (!(def->src = virStorageVolGetPath(vol)))
             goto cleanup;
         break;
+    case VIR_STORAGE_VOL_BLOCK:
+        if (!(poolxml = virStoragePoolGetXMLDesc(pool, 0)))
+            goto cleanup;
+
+        if (!(pooldef = virStoragePoolDefParseString(poolxml)))
+            goto cleanup;
+
+        if (def->srcpool->mode && pooldef->type != VIR_STORAGE_POOL_ISCSI) {
+            virReportError(VIR_ERR_XML_ERROR, "%s",
+                           _("disk source mode is only valid when "
+                             "storage pool is of iscsi type"));
+            goto cleanup;
+        }
+
+        def->srcpool->pooltype = pooldef->type;
+        if (pooldef->type == VIR_STORAGE_POOL_ISCSI) {
+            /* Default to use the LUN's path on host */
+            if (!def->srcpool->mode)
+                def->srcpool->mode = VIR_DOMAIN_DISK_SOURCE_POOL_MODE_HOST;
+
+            if (def->srcpool->mode ==
+                VIR_DOMAIN_DISK_SOURCE_POOL_MODE_URI) {
+                if (qemuAddDiskISCSIUri(def, pooldef) < 0)
+                    goto cleanup;
+            } else if (def->srcpool->mode ==
+                       VIR_DOMAIN_DISK_SOURCE_POOL_MODE_HOST) {
+                if (!(def->src = virStorageVolGetPath(vol)))
+                    goto cleanup;
+            }
+        } else {
+            if (!(def->src = virStorageVolGetPath(vol)))
+                goto cleanup;
+        }
+
+        break;
     case VIR_STORAGE_VOL_NETWORK:
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                        _("Using network volume as disk source is not supported"));
@@ -1467,5 +1557,7 @@ qemuTranslateDiskSourcePool(virConnectPtr conn,
 cleanup:
     virStoragePoolFree(pool);
     virStorageVolFree(vol);
+    VIR_FREE(poolxml);
+    virStoragePoolDefFree(pooldef);
     return ret;
 }
-- 
1.8.1.4


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]