[libvirt] [PATCH v4 2/4] qemu: block: use the delete flag to delete snapshot images if requested

Pavel Mores posted 4 patches 6 years, 2 months ago
[libvirt] [PATCH v4 2/4] qemu: block: use the delete flag to delete snapshot images if requested
Posted by Pavel Mores 6 years, 2 months ago
When blockcommit finishes successfully, one of the
qemuBlockJobProcessEventCompletedCommit() and
qemuBlockJobProcessEventCompletedActiveCommit() event handlers is called.
This is where the delete flag (stored in qemuBlockJobCommitData since the
previous commit) can actually be used to delete the committed snapshot
images if requested.

We use virFileRemove() instead of a simple unlink() to cover the case where
the image to be removed is on an NFS volume.

Signed-off-by: Pavel Mores <pmores@redhat.com>
---
 src/qemu/qemu_blockjob.c | 46 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/src/qemu/qemu_blockjob.c b/src/qemu/qemu_blockjob.c
index 718e311213..498e2a716f 100644
--- a/src/qemu/qemu_blockjob.c
+++ b/src/qemu/qemu_blockjob.c
@@ -1004,6 +1004,44 @@ qemuBlockJobProcessEventCompletedPull(virQEMUDriverPtr driver,
 }
 
 
+/**
+ * qemuBlockJobDeleteImages:
+ * @driver: qemu driver object
+ * @vm: domain object
+ * @disk: disk object that the chain to be deleted is associated with
+ * @top: top snapshot of the chain to be deleted
+ *
+ * Helper for removing snapshot images.  Intended for callers like
+ * qemuBlockJobProcessEventCompletedCommit() and
+ * qemuBlockJobProcessEventCompletedActiveCommit() as it relies on adjustments
+ * these functions perform on the 'backingStore' chain to function correctly.
+ *
+ * TODO look into removing backing store for non-local snapshots too
+ */
+static void
+qemuBlockJobDeleteImages(virQEMUDriverPtr driver,
+                         virDomainObjPtr vm,
+                         virDomainDiskDefPtr disk,
+                         virStorageSourcePtr top)
+{
+    virStorageSourcePtr p = top;
+    g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
+    uid_t uid;
+    gid_t gid;
+
+    for (; p != NULL; p = p->backingStore) {
+        if (virStorageSourceGetActualType(p) == VIR_STORAGE_TYPE_FILE) {
+
+            qemuDomainGetImageIds(cfg, vm, p, disk->src, &uid, &gid);
+
+            if (virFileRemove(p->path, uid, gid) < 0) {
+                VIR_WARN("Unable to remove snapshot image file '%s' (%s)",
+                         p->path, g_strerror(errno));
+            }
+        }
+    }
+}
+
 /**
  * qemuBlockJobProcessEventCompletedCommit:
  * @driver: qemu driver object
@@ -1070,6 +1108,10 @@ qemuBlockJobProcessEventCompletedCommit(virQEMUDriverPtr driver,
     job->data.commit.topparent->backingStore = job->data.commit.base;
 
     qemuBlockJobEventProcessConcludedRemoveChain(driver, vm, asyncJob, job->data.commit.top);
+
+    if (job->data.commit.deleteCommittedImages)
+        qemuBlockJobDeleteImages(driver, vm, job->disk, job->data.commit.top);
+
     virObjectUnref(job->data.commit.top);
     job->data.commit.top = NULL;
 
@@ -1160,6 +1202,10 @@ qemuBlockJobProcessEventCompletedActiveCommit(virQEMUDriverPtr driver,
     job->disk->src->readonly = job->data.commit.top->readonly;
 
     qemuBlockJobEventProcessConcludedRemoveChain(driver, vm, asyncJob, job->data.commit.top);
+
+    if (job->data.commit.deleteCommittedImages)
+        qemuBlockJobDeleteImages(driver, vm, job->disk, job->data.commit.top);
+
     virObjectUnref(job->data.commit.top);
     job->data.commit.top = NULL;
     /* the mirror element does not serve functional purpose for the commit job */
-- 
2.21.0

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

Re: [libvirt] [PATCH v4 2/4] qemu: block: use the delete flag to delete snapshot images if requested
Posted by Peter Krempa 6 years, 1 month ago
On Tue, Dec 10, 2019 at 17:25:39 +0100, Pavel Mores wrote:
> When blockcommit finishes successfully, one of the
> qemuBlockJobProcessEventCompletedCommit() and
> qemuBlockJobProcessEventCompletedActiveCommit() event handlers is called.
> This is where the delete flag (stored in qemuBlockJobCommitData since the
> previous commit) can actually be used to delete the committed snapshot
> images if requested.
> 
> We use virFileRemove() instead of a simple unlink() to cover the case where
> the image to be removed is on an NFS volume.
> 
> Signed-off-by: Pavel Mores <pmores@redhat.com>
> ---
>  src/qemu/qemu_blockjob.c | 46 ++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 46 insertions(+)

Reviewed-by: Peter Krempa <pkrempa@redhat.com>

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