qemu: block: use the delete flag to delete snapshot images if requested

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>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
This commit is contained in:
Pavel Mores 2019-12-10 17:25:39 +01:00 committed by Peter Krempa
parent cb03fd9340
commit 9e5c98e84f

View File

@ -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 */