[libvirt] [PATCH] esx: Fall back to path as key when QueryVirtualDiskUuid isn't available

Matthias Bolte matthias.bolte at googlemail.com
Fri Sep 3 23:36:08 UTC 2010


QueryVirtualDiskUuid is only available on an ESX(i) server. vCenter
returns an NotImplemented fault and a GSX server is missing the
VirtualDiskManager completely. Therefore only use QueryVirtualDiskUuid
with an ESX(i) server and fall back to path as storage volume key for
vCenter and GSX server.
---
 src/esx/esx_storage_driver.c |   36 +++++++++++++++++++++++------
 src/esx/esx_vi.c             |   50 +++++++++++++++++++++++++++--------------
 src/esx/esx_vi.h             |    1 +
 3 files changed, 62 insertions(+), 25 deletions(-)

diff --git a/src/esx/esx_storage_driver.c b/src/esx/esx_storage_driver.c
index 76f8769..329020b 100644
--- a/src/esx/esx_storage_driver.c
+++ b/src/esx/esx_storage_driver.c
@@ -783,6 +783,13 @@ esxStorageVolumeLookupByKey(virConnectPtr conn, const char *key)
         return esxStorageVolumeLookupByPath(conn, key);
     }
 
+    if (!priv->primary->hasQueryVirtualDiskUuid) {
+        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
+                  _("QueryVirtualDiskUuid not avialable, cannot lookup storage "
+                    "volume by UUID"));
+        return NULL;
+    }
+
     if (esxVI_EnsureSession(priv->primary) < 0) {
         return NULL;
     }
@@ -916,7 +923,7 @@ esxStorageVolumeCreateXML(virStoragePoolPtr pool, const char *xmldesc,
     esxVI_ManagedObjectReference *task = NULL;
     esxVI_TaskInfoState taskInfoState;
     char *uuid_string = NULL;
-    char key[VIR_UUID_STRING_BUFLEN] = "";
+    char *key = NULL;
 
     virCheckFlags(0, NULL);
 
@@ -1068,14 +1075,26 @@ esxStorageVolumeCreateXML(virStoragePoolPtr pool, const char *xmldesc,
             goto cleanup;
         }
 
-        if (esxVI_QueryVirtualDiskUuid(priv->primary, datastorePath,
-                                       priv->primary->datacenter->_reference,
-                                       &uuid_string) < 0) {
-            goto cleanup;
-        }
+        if (priv->primary->hasQueryVirtualDiskUuid) {
+            if (VIR_ALLOC_N(key, VIR_UUID_STRING_BUFLEN) < 0) {
+                virReportOOMError();
+                goto cleanup;
+            }
 
-        if (esxUtil_ReformatUuid(uuid_string, key) < 0) {
-            goto cleanup;
+            if (esxVI_QueryVirtualDiskUuid(priv->primary, datastorePath,
+                                           priv->primary->datacenter->_reference,
+                                           &uuid_string) < 0) {
+                goto cleanup;
+            }
+
+            if (esxUtil_ReformatUuid(uuid_string, key) < 0) {
+                goto cleanup;
+            }
+        } else {
+            /* Fall back to the path as key */
+            if (esxVI_String_DeepCopyValue(&key, datastorePath) < 0) {
+                goto cleanup;
+            }
         }
     } else {
         ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
@@ -1103,6 +1122,7 @@ esxStorageVolumeCreateXML(virStoragePoolPtr pool, const char *xmldesc,
     esxVI_FileBackedVirtualDiskSpec_Free(&virtualDiskSpec);
     esxVI_ManagedObjectReference_Free(&task);
     VIR_FREE(uuid_string);
+    VIR_FREE(key);
 
     return volume;
 }
diff --git a/src/esx/esx_vi.c b/src/esx/esx_vi.c
index 00e15f0..78cfdfd 100644
--- a/src/esx/esx_vi.c
+++ b/src/esx/esx_vi.c
@@ -453,6 +453,17 @@ esxVI_Context_Connect(esxVI_Context *ctx, const char *url,
         return -1;
     }
 
+    if (ctx->productVersion & esxVI_ProductVersion_ESX) {
+        /*
+         * FIXME: Actually this should be detected by really calling
+         * QueryVirtualDiskUuid and checking if a NotImplemented fault is
+         * returned. But currently we don't deserialized the details of a
+         * possbile fault and therefore we don't know if the fault was a
+         * NotImplemented fault or not.
+         */
+        ctx->hasQueryVirtualDiskUuid = true;
+    }
+
     if (esxVI_Login(ctx, username, password, NULL, &ctx->session) < 0 ||
         esxVI_BuildSelectSetCollection(ctx) < 0) {
         return -1;
@@ -3267,28 +3278,33 @@ esxVI_LookupStorageVolumeKeyByDatastorePath(esxVI_Context *ctx,
         return -1;
     }
 
-    if (esxVI_LookupFileInfoByDatastorePath(ctx, datastorePath, false, &fileInfo,
-                                            esxVI_Occurrence_RequiredItem) < 0) {
-        goto cleanup;
-    }
-
-    if (esxVI_VmDiskFileInfo_DynamicCast(fileInfo) != NULL) {
-        /* VirtualDisks have a UUID, use it as key */
-        if (esxVI_QueryVirtualDiskUuid(ctx, datastorePath,
-                                       ctx->datacenter->_reference,
-                                       &uuid_string) < 0) {
+    if (ctx->hasQueryVirtualDiskUuid) {
+        if (esxVI_LookupFileInfoByDatastorePath
+              (ctx, datastorePath, false, &fileInfo,
+               esxVI_Occurrence_RequiredItem) < 0) {
             goto cleanup;
         }
 
-        if (VIR_ALLOC_N(*key, VIR_UUID_STRING_BUFLEN) < 0) {
-            virReportOOMError();
-            goto cleanup;
-        }
+        if (esxVI_VmDiskFileInfo_DynamicCast(fileInfo) != NULL) {
+            /* VirtualDisks have a UUID, use it as key */
+            if (esxVI_QueryVirtualDiskUuid(ctx, datastorePath,
+                                           ctx->datacenter->_reference,
+                                           &uuid_string) < 0) {
+                goto cleanup;
+            }
 
-        if (esxUtil_ReformatUuid(uuid_string, *key) < 0) {
-            goto cleanup;
+            if (VIR_ALLOC_N(*key, VIR_UUID_STRING_BUFLEN) < 0) {
+                virReportOOMError();
+                goto cleanup;
+            }
+
+            if (esxUtil_ReformatUuid(uuid_string, *key) < 0) {
+                goto cleanup;
+            }
         }
-    } else {
+    }
+
+    if (*key == NULL) {
         /* Other files don't have a UUID, fall back to the path as key */
         if (esxVI_String_DeepCopyValue(key, datastorePath) < 0) {
             goto cleanup;
diff --git a/src/esx/esx_vi.h b/src/esx/esx_vi.h
index bd37b05..b205b8c 100644
--- a/src/esx/esx_vi.h
+++ b/src/esx/esx_vi.h
@@ -167,6 +167,7 @@ struct _esxVI_Context {
     esxVI_SelectionSpec *selectSet_hostSystemToDatastore;
     esxVI_SelectionSpec *selectSet_computeResourceToHost;
     esxVI_SelectionSpec *selectSet_computeResourceToParentToParent;
+    bool hasQueryVirtualDiskUuid;
 };
 
 int esxVI_Context_Alloc(esxVI_Context **ctx);
-- 
1.7.0.4




More information about the libvir-list mailing list