Add helper that will generate domain lifecycle events regarding to
snapshot revert operations and use it in a demonstration case in the
code.
The helper will be later used in internal snaphsot revert refactor more
widely.
---
src/qemu/qemu_driver.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 136 insertions(+), 6 deletions(-)
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 2d58b53..e794e02 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -13907,6 +13907,139 @@ qemuDomainSnapshotRevertInactive(virQEMUDriverPtr driver,
return ret > 0 ? -1 : ret;
}
+
+static int
+qemuDomainSnapshotEmitLifecycleEvent(virQEMUDriverPtr driver,
+ virDomainObjPtr vm,
+ virDomainState oldState,
+ virDomainState newState)
+{
+ int reason = 0;
+ int type = -1;
+ virObjectEventPtr event = NULL;
+
+ switch (newState) {
+ case VIR_DOMAIN_RUNNING:
+ switch (oldState) {
+ case VIR_DOMAIN_NOSTATE:
+ case VIR_DOMAIN_SHUTOFF:
+ case VIR_DOMAIN_CRASHED:
+ case VIR_DOMAIN_SHUTDOWN:
+ type = VIR_DOMAIN_EVENT_STARTED;
+ reason = VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT;
+ break;
+
+ case VIR_DOMAIN_PMSUSPENDED:
+ case VIR_DOMAIN_PAUSED:
+ type = VIR_DOMAIN_EVENT_RESUMED;
+ reason = VIR_DOMAIN_EVENT_RESUMED_FROM_SNAPSHOT;
+ break;
+
+ case VIR_DOMAIN_LAST:
+ case VIR_DOMAIN_RUNNING:
+ case VIR_DOMAIN_BLOCKED:
+ /* no event */
+ break;
+ }
+ break;
+
+ case VIR_DOMAIN_PAUSED:
+ switch (oldState) {
+ case VIR_DOMAIN_NOSTATE:
+ case VIR_DOMAIN_SHUTOFF:
+ case VIR_DOMAIN_CRASHED:
+ /* the machine was started here, so we need an additional event */
+ event = virDomainEventLifecycleNewFromObj(vm,
+ VIR_DOMAIN_EVENT_STARTED,
+
VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
+ if (!event)
+ goto cleanup;
+ qemuDomainEventQueue(driver, event);
+ /* fallthrough! */
+ case VIR_DOMAIN_PMSUSPENDED:
+ case VIR_DOMAIN_RUNNING:
+ case VIR_DOMAIN_SHUTDOWN:
+ type = VIR_DOMAIN_EVENT_SUSPENDED;
+ reason = VIR_DOMAIN_EVENT_SUSPENDED_FROM_SNAPSHOT;
+ break;
+
+ case VIR_DOMAIN_PAUSED:
+ case VIR_DOMAIN_LAST:
+ case VIR_DOMAIN_BLOCKED:
+ /* no event */
+ break;
+ }
+ break;
+
+ case VIR_DOMAIN_SHUTOFF:
+ switch (oldState) {
+ case VIR_DOMAIN_NOSTATE:
+ case VIR_DOMAIN_BLOCKED:
+ case VIR_DOMAIN_SHUTDOWN:
+ case VIR_DOMAIN_PAUSED:
+ case VIR_DOMAIN_RUNNING:
+ case VIR_DOMAIN_PMSUSPENDED:
+ type = VIR_DOMAIN_EVENT_STOPPED;
+ reason = VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT;
+ break;
+
+ case VIR_DOMAIN_SHUTOFF:
+ case VIR_DOMAIN_CRASHED:
+ case VIR_DOMAIN_LAST:
+ /* no event */
+ break;
+ }
+ break;
+
+ case VIR_DOMAIN_PMSUSPENDED:
+ switch (oldState) {
+ case VIR_DOMAIN_NOSTATE:
+ case VIR_DOMAIN_SHUTOFF:
+ case VIR_DOMAIN_CRASHED:
+ /* the machine was started here, so we need an additional event */
+ event = virDomainEventLifecycleNewFromObj(vm,
+ VIR_DOMAIN_EVENT_STARTED,
+
VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
+ if (!event)
+ goto cleanup;
+ qemuDomainEventQueue(driver, event);
+ /* fallthrough! */
+ case VIR_DOMAIN_RUNNING:
+ case VIR_DOMAIN_SHUTDOWN:
+ case VIR_DOMAIN_PAUSED:
+ type = VIR_DOMAIN_EVENT_PMSUSPENDED;
+ reason = VIR_DOMAIN_EVENT_PMSUSPENDED_FROM_SNAPSHOT;
+ break;
+
+ case VIR_DOMAIN_LAST:
+ case VIR_DOMAIN_BLOCKED:
+ case VIR_DOMAIN_PMSUSPENDED:
+ /* no event */
+ break;
+ }
+ break;
+
+ case VIR_DOMAIN_NOSTATE:
+ case VIR_DOMAIN_BLOCKED:
+ case VIR_DOMAIN_SHUTDOWN:
+ case VIR_DOMAIN_CRASHED:
+ case VIR_DOMAIN_LAST:
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Suspicious domain state '%d' after
snapshot"),
+ newState);
+ return -1;
+ }
+
+ if (!(event = virDomainEventLifecycleNewFromObj(vm, type, reason)))
+ goto cleanup;
+
+ qemuDomainEventQueue(driver, event);
+ return 0;
+
+ cleanup:
+ return -1;
+}
+
static int qemuDomainRevertToSnapshot(virDomainSnapshotPtr snapshot,
unsigned int flags)
{
@@ -14051,12 +14184,9 @@ static int qemuDomainRevertToSnapshot(virDomainSnapshotPtr
snapshot,
qemuProcessStop(driver, vm,
VIR_DOMAIN_SHUTOFF_FROM_SNAPSHOT, 0);
virDomainAuditStop(vm, "from-snapshot");
- detail = VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT;
- event = virDomainEventLifecycleNewFromObj(vm,
- VIR_DOMAIN_EVENT_STOPPED,
- detail);
- if (event)
- qemuDomainEventQueue(driver, event);
+ qemuDomainSnapshotEmitLifecycleEvent(driver, vm,
+ vm->state.state,
+ VIR_DOMAIN_SHUTOFF);
goto load;
}
--
2.0.0