My RFC for snapshot support [1] proposes several rules for when it is
safe to delete or revert to an external snapshot, predicated on
the existence of new API flags. These will be incrementally added
in future patches, but until then, blindly mishandling a disk
snapshot risks corrupting internal state, so it is better to
outright reject the attempts until the other pieces are in place,
thus incrementally relaxing the restrictions added in this patch.
[1]
https://www.redhat.com/archives/libvir-list/2011-August/msg00361.html
* src/qemu/qemu_driver.c (qemuDomainSnapshotCountExternal): New
function.
(qemuDomainDestroyFlags, qemuDomainUndefineFlags)
(qemuDomainSnapshotDelete): Use it to add safety valve.
(qemuDomainRevertToSnapshot): Add safety valve.
---
src/qemu/qemu_driver.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 59 insertions(+), 0 deletions(-)
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index aeb8ad9..5652e62 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -1777,6 +1777,19 @@ qemuDomainSnapshotDiscardAll(void *payload,
curr->err = err;
}
+/* Count how many snapshots in a set have external disk snapshots. */
+static void
+qemuDomainSnapshotCountExternal(void *payload,
+ const void *name ATTRIBUTE_UNUSED,
+ void *data)
+{
+ virDomainSnapshotObjPtr snap = payload;
+ int *count = data;
+
+ if (snap->def->state == VIR_DOMAIN_DISK_SNAPSHOT)
+ (*count)++;
+}
+
static int
qemuDomainDestroyFlags(virDomainPtr dom,
unsigned int flags)
@@ -1787,6 +1800,7 @@ qemuDomainDestroyFlags(virDomainPtr dom,
virDomainEventPtr event = NULL;
qemuDomainObjPrivatePtr priv;
int nsnapshots;
+ int external = 0;
virCheckFlags(VIR_DOMAIN_DESTROY_SNAPSHOTS_METADATA |
VIR_DOMAIN_DESTROY_SNAPSHOTS_FULL, -1);
@@ -1814,6 +1828,16 @@ qemuDomainDestroyFlags(virDomainPtr dom,
goto cleanup;
}
+ if ((flags & VIR_DOMAIN_DESTROY_SNAPSHOTS_FULL) &&
+ virHashForEach(vm->snapshots.objs, qemuDomainSnapshotCountExternal,
+ &external) &&
+ external) {
+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+ _("deletion of %d external disk snapshots not "
+ "supported yet"), external);
+ goto cleanup;
+ }
+
rem.driver = driver;
rem.vm = vm;
rem.metadata_only = !(flags & VIR_DOMAIN_DESTROY_SNAPSHOTS_FULL);
@@ -4943,6 +4967,7 @@ qemuDomainUndefineFlags(virDomainPtr dom,
char *name = NULL;
int ret = -1;
int nsnapshots;
+ int external = 0;
virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE |
VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA |
@@ -4972,6 +4997,16 @@ qemuDomainUndefineFlags(virDomainPtr dom,
goto cleanup;
}
+ if ((flags & VIR_DOMAIN_UNDEFINE_SNAPSHOTS_FULL) &&
+ virHashForEach(vm->snapshots.objs, qemuDomainSnapshotCountExternal,
+ &external) &&
+ external) {
+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+ _("deletion of %d external disk snapshots not "
+ "supported yet"), external);
+ goto cleanup;
+ }
+
rem.driver = driver;
rem.vm = vm;
rem.metadata_only = !(flags & VIR_DOMAIN_UNDEFINE_SNAPSHOTS_FULL);
@@ -9054,6 +9089,13 @@ static int qemuDomainRevertToSnapshot(virDomainSnapshotPtr
snapshot,
goto cleanup;
}
+ if (snap->def->state == VIR_DOMAIN_DISK_SNAPSHOT) {
+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+ _("revert to external disk snapshot not supported "
+ "yet"));
+ goto cleanup;
+ }
+
if (vm->current_snapshot) {
vm->current_snapshot->def->current = false;
if (qemuDomainSnapshotWriteMetadata(vm, vm->current_snapshot,
@@ -9273,6 +9315,7 @@ static int qemuDomainSnapshotDelete(virDomainSnapshotPtr snapshot,
struct snap_remove rem;
struct snap_reparent rep;
bool metadata_only = !!(flags & VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY);
+ int external = 0;
virCheckFlags(VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN |
VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY |
@@ -9295,6 +9338,22 @@ static int qemuDomainSnapshotDelete(virDomainSnapshotPtr snapshot,
goto cleanup;
}
+ if (!(flags & VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY)) {
+ if (!(flags & VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN_ONLY) &&
+ snap->def->state == VIR_DOMAIN_DISK_SNAPSHOT)
+ external++;
+ if (flags & VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN)
+ virDomainSnapshotForEachDescendant(&vm->snapshots, snap,
+ qemuDomainSnapshotCountExternal,
+ &external);
+ if (external) {
+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+ _("deletion of %d external disk snapshots not "
+ "supported yet"), external);
+ goto cleanup;
+ }
+ }
+
if (qemuDomainObjBeginJobWithDriver(driver, vm, QEMU_JOB_MODIFY) < 0)
goto cleanup;
--
1.7.4.4