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

[libvirt] [PATCH 7/7] storage:dir: adapts .uploadVol .dowloadVol for ploop volume



In case of ploop volume, target path of the volume is the path to the
directory that contains image file named root.hds and DiskDescriptor.xml.
While using uploadVol and downloadVol callbacks we need to open root.hds
itself.
Upload or download operations with ploop volume are only allowed when
images do not have snapshots. Otherwise operation fails.

Signed-off-by: Olga Krishtal <okrishtal virtuozzo com>
---
 src/storage/storage_backend.c | 99 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 96 insertions(+), 3 deletions(-)

diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c
index e159334..372f5b1 100644
--- a/src/storage/storage_backend.c
+++ b/src/storage/storage_backend.c
@@ -2070,6 +2070,47 @@ virStorageBackendStablePath(virStoragePoolObjPtr pool,
     return stablepath;
 }
 
+/*
+ *  Check whether the ploop image has snapshots.
+ *  return: -1 - failed to check
+ *           0 - no snapshots
+ *           1 - at least one snapshot
+ */
+static int
+virStorageBackendPloopHasSnapshots(char *path)
+{
+    virCommandPtr cmd = NULL;
+    char *output = NULL;
+    char *snap_tool = NULL;
+    int ret = -1;
+
+    snap_tool = virFindFileInPath("ploop");
+    if (!snap_tool) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       "%s", _("can't upload volume,"
+                               " please install ploop tool "));
+        return ret;
+    }
+
+    cmd = virCommandNewArgList(snap_tool, "snapshot-list", NULL);
+    virCommandAddArgFormat(cmd, "%s/DiskDescriptor.xml", path);
+    virCommandSetOutputBuffer(cmd, &output);
+
+    if ((ret = virCommandRun(cmd, NULL)) < 0)
+        goto cleanup;
+
+    if (!strstr(output, "root.hds.")) {
+        ret = 1;
+        goto cleanup;
+    }
+    ret = 0;
+
+ cleanup:
+    VIR_FREE(output);
+    virCommandFree(cmd);
+    return ret;
+}
+
 int
 virStorageBackendVolUploadLocal(virConnectPtr conn ATTRIBUTE_UNUSED,
                                 virStoragePoolObjPtr pool ATTRIBUTE_UNUSED,
@@ -2079,12 +2120,41 @@ virStorageBackendVolUploadLocal(virConnectPtr conn ATTRIBUTE_UNUSED,
                                 unsigned long long len,
                                 unsigned int flags)
 {
+    char *path = NULL;
+    char *target_path = vol->target.path;
+    int ret = -1;
+    int has_snap = 0;
+
     virCheckFlags(0, -1);
+    /* if volume has target format VIR_STORAGE_FILE_PLOOP
+     * we need to restore DiskDescriptor.xml, according to
+     * new contents of volume. This operation will be perfomed
+     * when volUpload is fully finished. */
+    if (vol->target.format == VIR_STORAGE_FILE_PLOOP) {
+        /* Fail if the volume contains snapshots or we failed to check it.*/
+        has_snap = virStorageBackendPloopHasSnapshots(vol->target.path);
+        if (has_snap < 0) {
+            goto cleanup;
+        } else if (!has_snap) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("can't upload volume, all existing snapshots"
+                             " will be lost"));
+            goto cleanup;
+        }
+
+        if (virAsprintf(&path, "%s/root.hds", vol->target.path) < 0)
+            return -1;
+        target_path = path;
+    }
 
     /* Not using O_CREAT because the file is required to already exist at
      * this point */
-    return virFDStreamOpenBlockDevice(stream, vol->target.path,
-                                      offset, len, O_WRONLY);
+    ret = virFDStreamOpenBlockDevice(stream, target_path,
+                                     offset, len, O_WRONLY);
+
+ cleanup:
+    VIR_FREE(path);
+    return ret;
 }
 
 int
@@ -2096,10 +2166,33 @@ virStorageBackendVolDownloadLocal(virConnectPtr conn ATTRIBUTE_UNUSED,
                                   unsigned long long len,
                                   unsigned int flags)
 {
+    char *path = NULL;
+    char *target_path = vol->target.path;
+    int ret = -1;
+    int has_snap = 0;
+
     virCheckFlags(0, -1);
+    if (vol->target.format == VIR_STORAGE_FILE_PLOOP) {
+        has_snap = virStorageBackendPloopHasSnapshots(vol->target.path);
+        if (has_snap < 0) {
+            goto cleanup;
+        } else if (!has_snap) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("can't upload volume, all existing snapshots"
+                             " will be lost"));
+            goto cleanup;
+        }
+        if (virAsprintf(&path, "%s/root.hds", vol->target.path) < 0)
+            goto cleanup;
+        target_path = path;
+    }
 
-    return virFDStreamOpenBlockDevice(stream, vol->target.path,
+    ret = virFDStreamOpenBlockDevice(stream, target_path,
                                       offset, len, O_RDONLY);
+
+ cleanup:
+    VIR_FREE(path);
+    return ret;
 }
 
 
-- 
1.8.3.1


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