[libvirt] [PATCH v3 05/11] esx: implement storagePoolListAllVolumes

Pino Toscano ptoscano at redhat.com
Fri Dec 20 14:58:07 UTC 2019


Implement the .storagePoolListAllVolumes storage API in the esx storage
driver, and in all its subdrivers.

Signed-off-by: Pino Toscano <ptoscano at redhat.com>
---
 src/esx/esx_storage_backend_iscsi.c | 68 +++++++++++++++++++++++
 src/esx/esx_storage_backend_vmfs.c  | 83 +++++++++++++++++++++++++++++
 src/esx/esx_storage_driver.c        | 19 +++++++
 3 files changed, 170 insertions(+)

diff --git a/src/esx/esx_storage_backend_iscsi.c b/src/esx/esx_storage_backend_iscsi.c
index 50de7d88ac..cd83a0fbfd 100644
--- a/src/esx/esx_storage_backend_iscsi.c
+++ b/src/esx/esx_storage_backend_iscsi.c
@@ -850,6 +850,73 @@ esxConnectListAllStoragePools(virConnectPtr conn,
 
 
 
+static int
+esxStoragePoolListAllVolumes(virStoragePoolPtr pool,
+                             virStorageVolPtr **vols,
+                             unsigned int flags)
+{
+    int ret = -1;
+    size_t count = 0;
+    esxPrivate *priv = pool->conn->privateData;
+    esxVI_HostScsiTopologyLun *hostScsiTopologyLunList = NULL;
+    esxVI_HostScsiTopologyLun *hostScsiTopologyLun;
+    esxVI_ScsiLun *scsiLunList = NULL;
+    esxVI_ScsiLun *scsiLun = NULL;
+    size_t i;
+
+    virCheckFlags(0, -1);
+
+    if (esxVI_LookupHostScsiTopologyLunListByTargetName
+          (priv->primary, pool->name, &hostScsiTopologyLunList) < 0) {
+        goto cleanup;
+    }
+
+    if (!hostScsiTopologyLunList) {
+        /* iSCSI adapter may not be enabled on ESX host */
+        return 0;
+    }
+
+    if (esxVI_LookupScsiLunList(priv->primary, &scsiLunList) < 0)
+        goto cleanup;
+
+    for (scsiLun = scsiLunList; scsiLun;
+         scsiLun = scsiLun->_next) {
+        for (hostScsiTopologyLun = hostScsiTopologyLunList;
+             hostScsiTopologyLun;
+             hostScsiTopologyLun = hostScsiTopologyLun->_next) {
+            if (STREQ(hostScsiTopologyLun->scsiLun, scsiLun->key)) {
+                virStorageVolPtr volume;
+
+                volume = scsiLunToStorageVol(pool->conn, scsiLun, pool->name);
+                if (!volume)
+                    goto cleanup;
+
+                if (VIR_APPEND_ELEMENT(*vols, count, volume) < 0)
+                    goto cleanup;
+            }
+
+        }
+    }
+
+    ret = count;
+
+ cleanup:
+    if (ret < 0) {
+        if (*vols) {
+            for (i = 0; i < count; ++i)
+                VIR_FREE((*vols)[i]);
+            VIR_FREE(*vols);
+        }
+    }
+
+    esxVI_HostScsiTopologyLun_Free(&hostScsiTopologyLunList);
+    esxVI_ScsiLun_Free(&scsiLunList);
+
+    return ret;
+}
+
+
+
 virStorageDriver esxStorageBackendISCSI = {
     .connectNumOfStoragePools = esxConnectNumOfStoragePools, /* 1.0.1 */
     .connectListStoragePools = esxConnectListStoragePools, /* 1.0.1 */
@@ -871,4 +938,5 @@ virStorageDriver esxStorageBackendISCSI = {
     .storageVolWipe = esxStorageVolWipe, /* 1.0.1 */
     .storageVolGetPath = esxStorageVolGetPath, /* 1.0.1 */
     .connectListAllStoragePools = esxConnectListAllStoragePools, /* 6.0.0 */
+    .storagePoolListAllVolumes = esxStoragePoolListAllVolumes, /* 6.0.0 */
 };
diff --git a/src/esx/esx_storage_backend_vmfs.c b/src/esx/esx_storage_backend_vmfs.c
index 1d7f9afda1..70005717cb 100644
--- a/src/esx/esx_storage_backend_vmfs.c
+++ b/src/esx/esx_storage_backend_vmfs.c
@@ -1565,6 +1565,88 @@ esxConnectListAllStoragePools(virConnectPtr conn,
 
 
 
+static int
+esxStoragePoolListAllVolumes(virStoragePoolPtr pool,
+                             virStorageVolPtr **vols,
+                             unsigned int flags)
+{
+    int ret = -1;
+    esxPrivate *priv = pool->conn->privateData;
+    esxVI_HostDatastoreBrowserSearchResults *searchResultsList = NULL;
+    esxVI_HostDatastoreBrowserSearchResults *searchResults = NULL;
+    esxVI_FileInfo *fileInfo = NULL;
+    char *directoryAndFileName = NULL;
+    size_t length;
+    size_t count = 0;
+    size_t i;
+
+    virCheckFlags(0, -1);
+
+    if (esxVI_LookupDatastoreContentByDatastoreName(priv->primary, pool->name,
+                                                    &searchResultsList) < 0) {
+        goto cleanup;
+    }
+
+    /* Interpret search result */
+    for (searchResults = searchResultsList; searchResults;
+         searchResults = searchResults->_next) {
+        VIR_FREE(directoryAndFileName);
+
+        if (esxUtil_ParseDatastorePath(searchResults->folderPath, NULL, NULL,
+                                       &directoryAndFileName) < 0) {
+            goto cleanup;
+        }
+
+        /* Strip trailing separators */
+        length = strlen(directoryAndFileName);
+
+        while (length > 0 && directoryAndFileName[length - 1] == '/') {
+            directoryAndFileName[length - 1] = '\0';
+            --length;
+        }
+
+        /* Build volume names */
+        for (fileInfo = searchResults->file; fileInfo;
+             fileInfo = fileInfo->_next) {
+            g_autofree char *datastorePath = NULL;
+            virStorageVolPtr volume;
+
+            if (length < 1) {
+                datastorePath = g_strdup(fileInfo->path);
+            } else {
+                datastorePath = g_strdup_printf("%s/%s",
+                                                directoryAndFileName,
+                                                fileInfo->path);
+            }
+
+            volume = datastorePathToStorageVol(pool->conn, pool->name, datastorePath);
+            if (!volume)
+                goto cleanup;
+
+            if (VIR_APPEND_ELEMENT(*vols, count, volume) < 0)
+                goto cleanup;
+        }
+    }
+
+    ret = count;
+
+ cleanup:
+    if (ret < 0) {
+        if (*vols) {
+            for (i = 0; i < count; ++i)
+                VIR_FREE((*vols)[i]);
+            VIR_FREE(*vols);
+        }
+    }
+
+    esxVI_HostDatastoreBrowserSearchResults_Free(&searchResultsList);
+    VIR_FREE(directoryAndFileName);
+
+    return ret;
+}
+
+
+
 virStorageDriver esxStorageBackendVMFS = {
     .connectNumOfStoragePools = esxConnectNumOfStoragePools, /* 0.8.2 */
     .connectListStoragePools = esxConnectListStoragePools, /* 0.8.2 */
@@ -1586,4 +1668,5 @@ virStorageDriver esxStorageBackendVMFS = {
     .storageVolGetXMLDesc = esxStorageVolGetXMLDesc, /* 0.8.4 */
     .storageVolGetPath = esxStorageVolGetPath, /* 0.8.4 */
     .connectListAllStoragePools = esxConnectListAllStoragePools, /* 6.0.0 */
+    .storagePoolListAllVolumes = esxStoragePoolListAllVolumes, /* 6.0.0 */
 };
diff --git a/src/esx/esx_storage_driver.c b/src/esx/esx_storage_driver.c
index 9e14b8b0ac..8470a31c68 100644
--- a/src/esx/esx_storage_driver.c
+++ b/src/esx/esx_storage_driver.c
@@ -581,6 +581,24 @@ esxConnectListAllStoragePools(virConnectPtr conn,
 
 
 
+static int
+esxStoragePoolListAllVolumes(virStoragePoolPtr pool,
+                             virStorageVolPtr **vols,
+                             unsigned int flags)
+{
+    esxPrivate *priv = pool->conn->privateData;
+    virStorageDriverPtr backend = pool->privateData;
+
+    virCheckNonNullArgReturn(pool->privateData, -1);
+
+    if (esxVI_EnsureSession(priv->primary) < 0)
+        return -1;
+
+    return backend->storagePoolListAllVolumes(pool, vols, flags);
+}
+
+
+
 virStorageDriver esxStorageDriver = {
     .connectNumOfStoragePools = esxConnectNumOfStoragePools, /* 0.8.2 */
     .connectListStoragePools = esxConnectListStoragePools, /* 0.8.2 */
@@ -609,4 +627,5 @@ virStorageDriver esxStorageDriver = {
     .storagePoolIsActive = esxStoragePoolIsActive, /* 0.8.2 */
     .storagePoolIsPersistent = esxStoragePoolIsPersistent, /* 0.8.2 */
     .connectListAllStoragePools = esxConnectListAllStoragePools, /* 6.0.0 */
+    .storagePoolListAllVolumes = esxStoragePoolListAllVolumes, /* 6.0.0 */
 };
-- 
2.24.1




More information about the libvir-list mailing list