Use the new storage conversion APIs to delete garbage left behind after
a failed snapshot attempt using the storage driver.
---
src/qemu/qemu_driver.c | 60 ++++++++++++++++++++++++++++++++++++++------------
1 file changed, 46 insertions(+), 14 deletions(-)
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 2afcb0a..f9ffe2e 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -12180,7 +12180,8 @@ cleanup:
/* The domain is expected to hold monitor lock. */
static int
-qemuDomainSnapshotCreateSingleDiskActive(virQEMUDriverPtr driver,
+qemuDomainSnapshotCreateSingleDiskActive(virConnectPtr conn,
+ virQEMUDriverPtr driver,
virDomainObjPtr vm,
virDomainSnapshotDiskDefPtr snap,
virDomainDiskDefPtr disk,
@@ -12197,6 +12198,7 @@ qemuDomainSnapshotCreateSingleDiskActive(virQEMUDriverPtr driver,
int ret = -1;
int fd = -1;
bool need_unlink = false;
+ virStorageEphemeralPtr temppool = NULL;
if (snap->snapshot != VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -12216,6 +12218,9 @@ qemuDomainSnapshotCreateSingleDiskActive(virQEMUDriverPtr driver,
virStorageFileFreeMetadata(disk->backingChain);
disk->backingChain = NULL;
+ if (!(temppool = virStorageEphemeralFromSnapshotDiskDef(conn, snap)))
+ goto cleanup;
+
switch (snap->type) {
case VIR_DOMAIN_DISK_TYPE_BLOCK:
reuse = true;
@@ -12275,8 +12280,16 @@ qemuDomainSnapshotCreateSingleDiskActive(virQEMUDriverPtr
driver,
}
cleanup:
- if (need_unlink && unlink(source))
- VIR_WARN("unable to unlink just-created %s", source);
+ if (need_unlink) {
+ if (virStorageVolDelete(temppool->vol, 0) < 0) {
+ char *path = virStorageVolGetPath(temppool->vol);
+ VIR_WARN("unable to remove just-created snapshot storage
'%s'",
+ NULLSTR(path));
+ VIR_FREE(path);
+ }
+ }
+
+ virStorageEphemeralFree(temppool);
VIR_FREE(device);
VIR_FREE(source);
VIR_FREE(persistSource);
@@ -12287,7 +12300,8 @@ cleanup:
* counterpart to qemuDomainSnapshotCreateSingleDiskActive, called
* only on a failed transaction. */
static void
-qemuDomainSnapshotUndoSingleDiskActive(virQEMUDriverPtr driver,
+qemuDomainSnapshotUndoSingleDiskActive(virConnectPtr conn,
+ virQEMUDriverPtr driver,
virDomainObjPtr vm,
virDomainDiskDefPtr origdisk,
virDomainDiskDefPtr disk,
@@ -12296,17 +12310,33 @@ qemuDomainSnapshotUndoSingleDiskActive(virQEMUDriverPtr driver,
{
char *source = NULL;
char *persistSource = NULL;
- struct stat st;
+
+ virStorageEphemeralPtr temppool = NULL;
if (VIR_STRDUP(source, origdisk->src) < 0 ||
(persistDisk && VIR_STRDUP(persistSource, source) < 0))
goto cleanup;
- qemuDomainPrepareDiskChainElement(driver, vm, disk, disk->src,
- VIR_DISK_CHAIN_NO_ACCESS);
- if (need_unlink && stat(disk->src, &st) == 0 &&
- S_ISREG(st.st_mode) && unlink(disk->src) < 0)
- VIR_WARN("Unable to remove just-created %s", disk->src);
+ switch (origdisk->type) {
+ case VIR_DOMAIN_DISK_TYPE_BLOCK:
+ case -1: /* type was not provided in snapshot conf */
+ case VIR_DOMAIN_DISK_TYPE_FILE:
+ qemuDomainPrepareDiskChainElement(driver, vm, disk, disk->src,
+ VIR_DISK_CHAIN_NO_ACCESS);
+ break;
+
+ default:
+ break;
+ }
+
+ temppool = virStorageEphemeralFromDiskDef(conn, disk);
+
+ if (need_unlink && temppool &&
+ virStorageVolDelete(temppool->vol, 0) < 0) {
+ char *path = virStorageVolGetPath(temppool->vol);
+ VIR_WARN("Unable to remove just-created '%s'", NULLSTR(path));
+ VIR_FREE(path);
+ }
/* Update vm in place to match changes. */
VIR_FREE(disk->src);
@@ -12323,13 +12353,15 @@ qemuDomainSnapshotUndoSingleDiskActive(virQEMUDriverPtr driver,
}
cleanup:
+ virStorageEphemeralFree(temppool);
VIR_FREE(source);
VIR_FREE(persistSource);
}
/* The domain is expected to be locked and active. */
static int
-qemuDomainSnapshotCreateDiskActive(virQEMUDriverPtr driver,
+qemuDomainSnapshotCreateDiskActive(virConnectPtr conn,
+ virQEMUDriverPtr driver,
virDomainObjPtr vm,
virDomainSnapshotObjPtr snap,
unsigned int flags,
@@ -12382,7 +12414,7 @@ qemuDomainSnapshotCreateDiskActive(virQEMUDriverPtr driver,
}
}
- ret = qemuDomainSnapshotCreateSingleDiskActive(driver, vm,
+ ret = qemuDomainSnapshotCreateSingleDiskActive(conn, driver, vm,
&snap->def->disks[i],
vm->def->disks[i],
persistDisk, actions,
@@ -12411,7 +12443,7 @@ qemuDomainSnapshotCreateDiskActive(virQEMUDriverPtr driver,
persistDisk = vm->newDef->disks[indx];
}
- qemuDomainSnapshotUndoSingleDiskActive(driver, vm,
+ qemuDomainSnapshotUndoSingleDiskActive(conn, driver, vm,
snap->def->dom->disks[i],
vm->def->disks[i],
persistDisk,
@@ -12554,7 +12586,7 @@ qemuDomainSnapshotCreateActiveExternal(virConnectPtr conn,
*
* Next we snapshot the disks.
*/
- if ((ret = qemuDomainSnapshotCreateDiskActive(driver, vm, snap, flags,
+ if ((ret = qemuDomainSnapshotCreateDiskActive(conn, driver, vm, snap, flags,
QEMU_ASYNC_JOB_SNAPSHOT)) < 0)
goto endjob;
--
1.8.5.2