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

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

 



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@xxxxxxxxxxxxx>
---
 src/storage/storage_backend.c | 99 +++++++++++++++++++++++++++++++++++++++++--
 src/storage/storage_driver.c  | 51 +++++++++++++++++++++-
 2 files changed, 146 insertions(+), 4 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;
 }
 
 
diff --git a/src/storage/storage_driver.c b/src/storage/storage_driver.c
index 1d96618..883b4be 100644
--- a/src/storage/storage_driver.c
+++ b/src/storage/storage_driver.c
@@ -63,6 +63,7 @@ typedef struct _virStorageVolStreamInfo virStorageVolStreamInfo;
 typedef virStorageVolStreamInfo *virStorageVolStreamInfoPtr;
 struct _virStorageVolStreamInfo {
     char *pool_name;
+    char *vol_path;
 };
 
 static void storageDriverLock(void)
@@ -2204,6 +2205,48 @@ virStorageVolPoolRefreshDataFree(void *opaque)
     VIR_FREE(cbdata);
 }
 
+static int
+virStorageBackendPloopRestoreDesc(char *path)
+{
+    int ret = -1;
+    virCommandPtr cmd = NULL;
+    char *refresh_tool = NULL;
+    char *desc = NULL;
+
+    if (virAsprintf(&desc, "%s/DiskDescriptor.xml", path) < 0)
+        return ret;
+
+    if (virFileRemove(desc, 0, 0) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("refresh ploop failed:"
+                         " unuble to delete DiskDescriptor.xml"));
+        goto cleanup;
+    }
+
+    refresh_tool = virFindFileInPath("ploop");
+    if (!refresh_tool) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("unable to find ploop, please install ploop tools"));
+        goto cleanup;
+    }
+
+    cmd = virCommandNewArgList(refresh_tool, "restore-descriptor",
+                               path, NULL);
+    virCommandAddArgFormat(cmd, "%s/root.hds", path);
+    if (virCommandRun(cmd, NULL) < 0)
+        goto cleanup;
+
+    ret = 0;
+
+ cleanup:
+    VIR_FREE(refresh_tool);
+    virCommandFree(cmd);
+    VIR_FREE(desc);
+    return ret;
+}
+
+
+
 /**
  * Thread to handle the pool refresh
  *
@@ -2219,6 +2262,10 @@ virStorageVolPoolRefreshThread(void *opaque)
     virStorageBackendPtr backend;
 
     storageDriverLock();
+    if (cbdata->vol_path) {
+        if (virStorageBackendPloopRestoreDesc(cbdata->vol_path) < 0)
+            goto cleanup;
+    }
     if (!(pool = virStoragePoolObjFindByName(&driver->pools,
                                              cbdata->pool_name)))
         goto cleanup;
@@ -2296,7 +2343,6 @@ storageVolUpload(virStorageVolPtr obj,
                        vol->name);
         goto cleanup;
     }
-
     if (!backend->uploadVol) {
         virReportError(VIR_ERR_NO_SUPPORT, "%s",
                        _("storage pool doesn't support volume upload"));
@@ -2313,6 +2359,9 @@ storageVolUpload(virStorageVolPtr obj,
         if (VIR_ALLOC(cbdata) < 0 ||
             VIR_STRDUP(cbdata->pool_name, pool->def->name) < 0)
             goto cleanup;
+            if (vol->target.type == VIR_STORAGE_VOL_PLOOP &&
+              VIR_STRDUP(cbdata->vol_path, vol->target.path) < 0)
+            goto cleanup;
     }
 
     if ((ret = backend->uploadVol(obj->conn, pool, vol, stream,
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]