render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
Blob Blame History Raw
From a4d89750b7d61aa561b9b9956bf354e39f434b01 Mon Sep 17 00:00:00 2001
Message-Id: <a4d89750b7d61aa561b9b9956bf354e39f434b01@dist-git>
From: Peter Krempa <pkrempa@redhat.com>
Date: Tue, 20 Jun 2017 14:45:37 +0200
Subject: [PATCH] storage: Add helper to retrieve the backing store string of a
 storage volume

It is necessary for some parts of the code to refresh just data
based on the based on the backing store string. Add a convenience
function that will retrieve this data.

(cherry picked from commit d97cfdc891f262e1b0cd3ed05f7ca2bb7ef7de1c)

https://bugzilla.redhat.com/show_bug.cgi?id=1461303

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
---
 src/storage/storage_driver.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 src/storage/storage_driver.h |  3 +++
 2 files changed, 47 insertions(+)

diff --git a/src/storage/storage_driver.c b/src/storage/storage_driver.c
index 61c5e7eff1..21af949170 100644
--- a/src/storage/storage_driver.c
+++ b/src/storage/storage_driver.c
@@ -3370,6 +3370,50 @@ virStorageFileGetMetadata(virStorageSourcePtr src,
 }
 
 
+/**
+ * virStorageFileGetBackingStoreStr:
+ * @src: storage object
+ *
+ * Extracts the backing store string as stored in the storage volume described
+ * by @src and returns it to the user. Caller is responsible for freeing it.
+ * In case when the string can't be retrieved or does not exist NULL is
+ * returned.
+ */
+char *
+virStorageFileGetBackingStoreStr(virStorageSourcePtr src)
+{
+    virStorageSourcePtr tmp = NULL;
+    char *buf = NULL;
+    ssize_t headerLen;
+    char *ret = NULL;
+
+    /* exit if we can't load information about the current image */
+    if (!virStorageFileSupportsBackingChainTraversal(src))
+        return NULL;
+
+    if (virStorageFileAccess(src, F_OK) < 0)
+        return NULL;
+
+    if ((headerLen = virStorageFileReadHeader(src, VIR_STORAGE_MAX_HEADER,
+                                              &buf)) < 0)
+        return NULL;
+
+    if (!(tmp = virStorageSourceCopy(src, false)))
+        goto cleanup;
+
+    if (virStorageFileGetMetadataInternal(tmp, buf, headerLen, NULL) < 0)
+        goto cleanup;
+
+    VIR_STEAL_PTR(ret, tmp->backingStoreRaw);
+
+ cleanup:
+    VIR_FREE(buf);
+    virStorageSourceFree(tmp);
+
+    return ret;
+}
+
+
 static int
 virStorageAddISCSIPoolSourceHost(virDomainDiskDefPtr def,
                                  virStoragePoolDefPtr pooldef)
diff --git a/src/storage/storage_driver.h b/src/storage/storage_driver.h
index 530bc33880..ade05f7156 100644
--- a/src/storage/storage_driver.h
+++ b/src/storage/storage_driver.h
@@ -54,6 +54,9 @@ int virStorageFileGetMetadata(virStorageSourcePtr src,
                               bool report_broken)
     ATTRIBUTE_NONNULL(1);
 
+char *virStorageFileGetBackingStoreStr(virStorageSourcePtr src)
+    ATTRIBUTE_NONNULL(1);
+
 int virStorageTranslateDiskSourcePool(virConnectPtr conn,
                                       virDomainDiskDefPtr def);
 
-- 
2.13.1