1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Dietmar Maurer <dietmar@proxmox.com>
3 Date: Mon, 6 Apr 2020 12:16:46 +0200
4 Subject: [PATCH] PVE: add savevm-async for background state snapshots
6 Put qemu_savevm_state_{header,setup} into the main loop and the rest
7 of the iteration into a coroutine. The former need to lock the
8 iothread (and we can't unlock it in the coroutine), and the latter
9 can't deal with being in a separate thread, so a coroutine it must
12 Truncate output file at 1024 boundary.
14 Do not block the VM and save the state on aborting a snapshot, as the
15 snapshot will be invalid anyway.
17 Also, when aborting, wait for the target file to be closed, otherwise a
18 client might run into race-conditions when trying to remove the file
21 Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
22 Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
23 Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
25 Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
27 hmp-commands-info.hx | 13 +
28 hmp-commands.hx | 33 ++
29 include/migration/snapshot.h | 1 +
30 include/monitor/hmp.h | 5 +
31 migration/meson.build | 1 +
32 migration/savevm-async.c | 591 +++++++++++++++++++++++++++++++++++
33 monitor/hmp-cmds.c | 57 ++++
34 qapi/migration.json | 34 ++
35 qapi/misc.json | 32 ++
36 qemu-options.hx | 12 +
38 11 files changed, 789 insertions(+)
39 create mode 100644 migration/savevm-async.c
41 diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
42 index 117ba25f91..b3b797ca28 100644
43 --- a/hmp-commands-info.hx
44 +++ b/hmp-commands-info.hx
45 @@ -580,6 +580,19 @@ SRST
46 Show current migration xbzrle cache size.
53 + .help = "show savevm status",
54 + .cmd = hmp_info_savevm,
65 diff --git a/hmp-commands.hx b/hmp-commands.hx
66 index ff2d7aa8f3..d294c234a5 100644
69 @@ -1866,3 +1866,36 @@ ERST
75 + .name = "savevm-start",
76 + .args_type = "statefile:s?",
77 + .params = "[statefile]",
78 + .help = "Prepare for snapshot and halt VM. Save VM state to statefile.",
79 + .cmd = hmp_savevm_start,
83 + .name = "snapshot-drive",
84 + .args_type = "device:s,name:s",
85 + .params = "device name",
86 + .help = "Create internal snapshot.",
87 + .cmd = hmp_snapshot_drive,
91 + .name = "delete-drive-snapshot",
92 + .args_type = "device:s,name:s",
93 + .params = "device name",
94 + .help = "Delete internal snapshot.",
95 + .cmd = hmp_delete_drive_snapshot,
99 + .name = "savevm-end",
102 + .help = "Resume VM after snaphot.",
103 + .cmd = hmp_savevm_end,
106 diff --git a/include/migration/snapshot.h b/include/migration/snapshot.h
107 index c85b6ec75b..4411b7121d 100644
108 --- a/include/migration/snapshot.h
109 +++ b/include/migration/snapshot.h
112 int save_snapshot(const char *name, Error **errp);
113 int load_snapshot(const char *name, Error **errp);
114 +int load_snapshot_from_blockdev(const char *filename, Error **errp);
117 diff --git a/include/monitor/hmp.h b/include/monitor/hmp.h
118 index ed2913fd18..4e06f89e8e 100644
119 --- a/include/monitor/hmp.h
120 +++ b/include/monitor/hmp.h
121 @@ -25,6 +25,7 @@ void hmp_info_status(Monitor *mon, const QDict *qdict);
122 void hmp_info_uuid(Monitor *mon, const QDict *qdict);
123 void hmp_info_chardev(Monitor *mon, const QDict *qdict);
124 void hmp_info_mice(Monitor *mon, const QDict *qdict);
125 +void hmp_info_savevm(Monitor *mon, const QDict *qdict);
126 void hmp_info_migrate(Monitor *mon, const QDict *qdict);
127 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
128 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict);
129 @@ -83,6 +84,10 @@ void hmp_netdev_add(Monitor *mon, const QDict *qdict);
130 void hmp_netdev_del(Monitor *mon, const QDict *qdict);
131 void hmp_getfd(Monitor *mon, const QDict *qdict);
132 void hmp_closefd(Monitor *mon, const QDict *qdict);
133 +void hmp_savevm_start(Monitor *mon, const QDict *qdict);
134 +void hmp_snapshot_drive(Monitor *mon, const QDict *qdict);
135 +void hmp_delete_drive_snapshot(Monitor *mon, const QDict *qdict);
136 +void hmp_savevm_end(Monitor *mon, const QDict *qdict);
137 void hmp_sendkey(Monitor *mon, const QDict *qdict);
138 void hmp_screendump(Monitor *mon, const QDict *qdict);
139 void hmp_chardev_add(Monitor *mon, const QDict *qdict);
140 diff --git a/migration/meson.build b/migration/meson.build
141 index 980e37865c..e62b79b60f 100644
142 --- a/migration/meson.build
143 +++ b/migration/meson.build
144 @@ -23,6 +23,7 @@ softmmu_ss.add(files(
152 diff --git a/migration/savevm-async.c b/migration/savevm-async.c
154 index 0000000000..4e345c1a7d
156 +++ b/migration/savevm-async.c
158 +#include "qemu/osdep.h"
159 +#include "migration/migration.h"
160 +#include "migration/savevm.h"
161 +#include "migration/snapshot.h"
162 +#include "migration/global_state.h"
163 +#include "migration/ram.h"
164 +#include "migration/qemu-file.h"
165 +#include "sysemu/sysemu.h"
166 +#include "sysemu/runstate.h"
167 +#include "block/block.h"
168 +#include "sysemu/block-backend.h"
169 +#include "qapi/error.h"
170 +#include "qapi/qmp/qerror.h"
171 +#include "qapi/qmp/qdict.h"
172 +#include "qapi/qapi-commands-migration.h"
173 +#include "qapi/qapi-commands-misc.h"
174 +#include "qapi/qapi-commands-block.h"
175 +#include "qemu/cutils.h"
176 +#include "qemu/timer.h"
177 +#include "qemu/main-loop.h"
178 +#include "qemu/rcu.h"
180 +/* #define DEBUG_SAVEVM_STATE */
182 +/* used while emulated sync operation in progress */
183 +#define NOT_DONE -EINPROGRESS
185 +#ifdef DEBUG_SAVEVM_STATE
186 +#define DPRINTF(fmt, ...) \
187 + do { printf("savevm-async: " fmt, ## __VA_ARGS__); } while (0)
189 +#define DPRINTF(fmt, ...) \
197 + SAVE_STATE_COMPLETED,
198 + SAVE_STATE_CANCELLED
202 +static struct SnapshotState {
203 + BlockBackend *target;
208 + int saved_vm_running;
210 + int64_t total_time;
211 + QEMUBH *finalize_bh;
213 + QemuCoSleepState *target_close_wait;
216 +static bool savevm_aborted(void)
218 + return snap_state.state == SAVE_STATE_CANCELLED ||
219 + snap_state.state == SAVE_STATE_ERROR;
222 +SaveVMInfo *qmp_query_savevm(Error **errp)
224 + SaveVMInfo *info = g_malloc0(sizeof(*info));
225 + struct SnapshotState *s = &snap_state;
227 + if (s->state != SAVE_STATE_DONE) {
228 + info->has_bytes = true;
229 + info->bytes = s->bs_pos;
230 + switch (s->state) {
231 + case SAVE_STATE_ERROR:
232 + info->has_status = true;
233 + info->status = g_strdup("failed");
234 + info->has_total_time = true;
235 + info->total_time = s->total_time;
237 + info->has_error = true;
238 + info->error = g_strdup(error_get_pretty(s->error));
241 + case SAVE_STATE_ACTIVE:
242 + info->has_status = true;
243 + info->status = g_strdup("active");
244 + info->has_total_time = true;
245 + info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
248 + case SAVE_STATE_COMPLETED:
249 + info->has_status = true;
250 + info->status = g_strdup("completed");
251 + info->has_total_time = true;
252 + info->total_time = s->total_time;
260 +static int save_snapshot_cleanup(void)
264 + DPRINTF("save_snapshot_cleanup\n");
266 + snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
267 + snap_state.total_time;
269 + if (snap_state.file) {
270 + ret = qemu_fclose(snap_state.file);
273 + if (snap_state.target) {
274 + if (!savevm_aborted()) {
275 + /* try to truncate, but ignore errors (will fail on block devices).
276 + * note1: bdrv_read() need whole blocks, so we need to round up
277 + * note2: PVE requires 1024 (BDRV_SECTOR_SIZE*2) alignment
279 + size_t size = QEMU_ALIGN_UP(snap_state.bs_pos, BDRV_SECTOR_SIZE*2);
280 + blk_truncate(snap_state.target, size, false, PREALLOC_MODE_OFF, 0, NULL);
282 + blk_op_unblock_all(snap_state.target, snap_state.blocker);
283 + error_free(snap_state.blocker);
284 + snap_state.blocker = NULL;
285 + blk_unref(snap_state.target);
286 + snap_state.target = NULL;
288 + if (snap_state.target_close_wait) {
289 + qemu_co_sleep_wake(snap_state.target_close_wait);
296 +static void save_snapshot_error(const char *fmt, ...)
302 + msg = g_strdup_vprintf(fmt, ap);
305 + DPRINTF("save_snapshot_error: %s\n", msg);
307 + if (!snap_state.error) {
308 + error_set(&snap_state.error, ERROR_CLASS_GENERIC_ERROR, "%s", msg);
313 + snap_state.state = SAVE_STATE_ERROR;
316 +static int block_state_close(void *opaque, Error **errp)
318 + snap_state.file = NULL;
319 + return blk_flush(snap_state.target);
322 +typedef struct BlkRwCo {
324 + QEMUIOVector *qiov;
328 +static void coroutine_fn block_state_write_entry(void *opaque) {
329 + BlkRwCo *rwco = opaque;
330 + rwco->ret = blk_co_pwritev(snap_state.target, rwco->offset, rwco->qiov->size,
335 +static ssize_t block_state_writev_buffer(void *opaque, struct iovec *iov,
336 + int iovcnt, int64_t pos, Error **errp)
341 + assert(pos == snap_state.bs_pos);
348 + qemu_iovec_init_external(&qiov, iov, iovcnt);
350 + if (qemu_in_coroutine()) {
351 + block_state_write_entry(&rwco);
353 + Coroutine *co = qemu_coroutine_create(&block_state_write_entry, &rwco);
354 + bdrv_coroutine_enter(blk_bs(snap_state.target), co);
355 + BDRV_POLL_WHILE(blk_bs(snap_state.target), rwco.ret == NOT_DONE);
357 + if (rwco.ret < 0) {
361 + snap_state.bs_pos += qiov.size;
365 +static const QEMUFileOps block_file_ops = {
366 + .writev_buffer = block_state_writev_buffer,
367 + .close = block_state_close,
370 +static void process_savevm_finalize(void *opaque)
373 + AioContext *iohandler_ctx = iohandler_get_aio_context();
374 + MigrationState *ms = migrate_get_current();
376 + bool aborted = savevm_aborted();
378 +#ifdef DEBUG_SAVEVM_STATE
379 + int64_t start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
382 + qemu_bh_delete(snap_state.finalize_bh);
383 + snap_state.finalize_bh = NULL;
384 + snap_state.co = NULL;
386 + /* We need to own the target bdrv's context for the following functions,
387 + * so move it back. It can stay in the main context and live out its live
388 + * there, since we're done with it after this method ends anyway.
390 + aio_context_acquire(iohandler_ctx);
391 + blk_set_aio_context(snap_state.target, qemu_get_aio_context(), NULL);
392 + aio_context_release(iohandler_ctx);
394 + ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
396 + save_snapshot_error("vm_stop_force_state error %d", ret);
400 + /* skip state saving if we aborted, snapshot will be invalid anyway */
401 + (void)qemu_savevm_state_complete_precopy(snap_state.file, false, false);
402 + ret = qemu_file_get_error(snap_state.file);
404 + save_snapshot_error("qemu_savevm_state_iterate error %d", ret);
408 + DPRINTF("state saving complete\n");
409 + DPRINTF("timing: process_savevm_finalize (state saving) took %ld ms\n",
410 + qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - start_time);
412 + /* clear migration state */
413 + migrate_set_state(&ms->state, MIGRATION_STATUS_SETUP,
414 + ret || aborted ? MIGRATION_STATUS_FAILED : MIGRATION_STATUS_COMPLETED);
415 + ms->to_dst_file = NULL;
417 + qemu_savevm_state_cleanup();
419 + ret = save_snapshot_cleanup();
421 + save_snapshot_error("save_snapshot_cleanup error %d", ret);
422 + } else if (snap_state.state == SAVE_STATE_ACTIVE) {
423 + snap_state.state = SAVE_STATE_COMPLETED;
424 + } else if (aborted) {
425 + save_snapshot_error("process_savevm_cleanup: found aborted state: %d",
428 + save_snapshot_error("process_savevm_cleanup: invalid state: %d",
431 + if (snap_state.saved_vm_running) {
433 + snap_state.saved_vm_running = false;
436 + DPRINTF("timing: process_savevm_finalize (full) took %ld ms\n",
437 + qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - start_time);
440 +static void coroutine_fn process_savevm_co(void *opaque)
444 + BdrvNextIterator it;
445 + BlockDriverState *bs = NULL;
447 +#ifdef DEBUG_SAVEVM_STATE
448 + int64_t start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
451 + ret = qemu_file_get_error(snap_state.file);
453 + save_snapshot_error("qemu_savevm_state_setup failed");
457 + while (snap_state.state == SAVE_STATE_ACTIVE) {
458 + uint64_t pending_size, pend_precopy, pend_compatible, pend_postcopy;
460 + qemu_savevm_state_pending(snap_state.file, 0, &pend_precopy, &pend_compatible, &pend_postcopy);
461 + pending_size = pend_precopy + pend_compatible + pend_postcopy;
463 + maxlen = blk_getlength(snap_state.target) - 30*1024*1024;
465 + if (pending_size > 400000 && snap_state.bs_pos + pending_size < maxlen) {
466 + ret = qemu_savevm_state_iterate(snap_state.file, false);
468 + save_snapshot_error("qemu_savevm_state_iterate error %d", ret);
471 + DPRINTF("savevm iterate pending size %lu ret %d\n", pending_size, ret);
473 + qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
474 + ret = global_state_store();
476 + save_snapshot_error("global_state_store error %d", ret);
480 + DPRINTF("savevm iterate complete\n");
485 + DPRINTF("timing: process_savevm_co took %ld ms\n",
486 + qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - start_time);
488 +#ifdef DEBUG_SAVEVM_STATE
489 + int64_t start_time_flush = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
491 + /* If a drive runs in an IOThread we can flush it async, and only
492 + * need to sync-flush whatever IO happens between now and
493 + * vm_stop_force_state. bdrv_next can only be called from main AioContext,
494 + * so move there now and after every flush.
496 + aio_co_reschedule_self(qemu_get_aio_context());
497 + for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
498 + /* target has BDRV_O_NO_FLUSH, no sense calling bdrv_flush on it */
499 + if (bs == blk_bs(snap_state.target)) {
503 + AioContext *bs_ctx = bdrv_get_aio_context(bs);
504 + if (bs_ctx != qemu_get_aio_context()) {
505 + DPRINTF("savevm: async flushing drive %s\n", bs->filename);
506 + aio_co_reschedule_self(bs_ctx);
508 + aio_co_reschedule_self(qemu_get_aio_context());
512 + DPRINTF("timing: async flushing took %ld ms\n",
513 + qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - start_time_flush);
515 + qemu_bh_schedule(snap_state.finalize_bh);
518 +void qmp_savevm_start(bool has_statefile, const char *statefile, Error **errp)
520 + Error *local_err = NULL;
521 + MigrationState *ms = migrate_get_current();
522 + AioContext *iohandler_ctx = iohandler_get_aio_context();
524 + int bdrv_oflags = BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_NO_FLUSH;
526 + if (snap_state.state != SAVE_STATE_DONE) {
527 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
528 + "VM snapshot already started\n");
532 + if (migration_is_running(ms->state)) {
533 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, QERR_MIGRATION_ACTIVE);
537 + if (migrate_use_block()) {
538 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
539 + "Block migration and snapshots are incompatible");
543 + /* initialize snapshot info */
544 + snap_state.saved_vm_running = runstate_is_running();
545 + snap_state.bs_pos = 0;
546 + snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
547 + snap_state.blocker = NULL;
549 + if (snap_state.error) {
550 + error_free(snap_state.error);
551 + snap_state.error = NULL;
554 + if (!has_statefile) {
555 + vm_stop(RUN_STATE_SAVE_VM);
556 + snap_state.state = SAVE_STATE_COMPLETED;
560 + if (qemu_savevm_state_blocked(errp)) {
564 + /* Open the image */
565 + QDict *options = NULL;
566 + options = qdict_new();
567 + qdict_put_str(options, "driver", "raw");
568 + snap_state.target = blk_new_open(statefile, NULL, options, bdrv_oflags, &local_err);
569 + if (!snap_state.target) {
570 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
574 + snap_state.file = qemu_fopen_ops(&snap_state, &block_file_ops);
576 + if (!snap_state.file) {
577 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
582 + * qemu_savevm_* paths use migration code and expect a migration state.
583 + * State is cleared in process_savevm_co, but has to be initialized
584 + * here (blocking main thread, from QMP) to avoid race conditions.
587 + memset(&ram_counters, 0, sizeof(ram_counters));
588 + ms->to_dst_file = snap_state.file;
590 + error_setg(&snap_state.blocker, "block device is in use by savevm");
591 + blk_op_block_all(snap_state.target, snap_state.blocker);
593 + snap_state.state = SAVE_STATE_ACTIVE;
594 + snap_state.finalize_bh = qemu_bh_new(process_savevm_finalize, &snap_state);
595 + snap_state.co = qemu_coroutine_create(&process_savevm_co, NULL);
596 + qemu_mutex_unlock_iothread();
597 + qemu_savevm_state_header(snap_state.file);
598 + qemu_savevm_state_setup(snap_state.file);
599 + qemu_mutex_lock_iothread();
601 + /* Async processing from here on out happens in iohandler context, so let
602 + * the target bdrv have its home there.
604 + blk_set_aio_context(snap_state.target, iohandler_ctx, &local_err);
606 + aio_co_schedule(iohandler_ctx, snap_state.co);
612 + save_snapshot_error("setup failed");
614 + if (snap_state.saved_vm_running) {
616 + snap_state.saved_vm_running = false;
620 +void coroutine_fn qmp_savevm_end(Error **errp)
624 + if (snap_state.state == SAVE_STATE_DONE) {
625 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
626 + "VM snapshot not started\n");
630 + if (snap_state.state == SAVE_STATE_ACTIVE) {
631 + snap_state.state = SAVE_STATE_CANCELLED;
632 + goto wait_for_close;
635 + if (snap_state.saved_vm_running) {
637 + snap_state.saved_vm_running = false;
640 + snap_state.state = SAVE_STATE_DONE;
643 + if (!snap_state.target) {
644 + DPRINTF("savevm-end: no target file open\n");
648 + /* wait until cleanup is done before returning, this ensures that after this
649 + * call exits the statefile will be closed and can be removed immediately */
650 + DPRINTF("savevm-end: waiting for cleanup\n");
651 + timeout = 30L * 1000 * 1000 * 1000;
652 + qemu_co_sleep_ns_wakeable(QEMU_CLOCK_REALTIME, timeout,
653 + &snap_state.target_close_wait);
654 + snap_state.target_close_wait = NULL;
655 + if (snap_state.target) {
656 + save_snapshot_error("timeout waiting for target file close in "
658 + /* we cannot assume the snapshot finished in this case, so leave the
659 + * state alone - caller has to figure something out */
663 + DPRINTF("savevm-end: cleanup done\n");
666 +// FIXME: Deprecated
667 +void qmp_snapshot_drive(const char *device, const char *name, Error **errp)
669 + // Compatibility to older qemu-server.
670 + qmp_blockdev_snapshot_internal_sync(device, name, errp);
673 +// FIXME: Deprecated
674 +void qmp_delete_drive_snapshot(const char *device, const char *name,
677 + // Compatibility to older qemu-server.
678 + (void)qmp_blockdev_snapshot_delete_internal_sync(device, false, NULL,
682 +static ssize_t loadstate_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
683 + size_t size, Error **errp)
685 + BlockBackend *be = opaque;
686 + int64_t maxlen = blk_getlength(be);
687 + if (pos > maxlen) {
690 + if ((pos + size) > maxlen) {
691 + size = maxlen - pos - 1;
696 + return blk_pread(be, pos, buf, size);
699 +static const QEMUFileOps loadstate_file_ops = {
700 + .get_buffer = loadstate_get_buffer,
703 +int load_snapshot_from_blockdev(const char *filename, Error **errp)
706 + Error *local_err = NULL;
707 + Error *blocker = NULL;
712 + be = blk_new_open(filename, NULL, NULL, 0, &local_err);
715 + error_setg(errp, "Could not open VM state file");
719 + error_setg(&blocker, "block device is in use by load state");
720 + blk_op_block_all(be, blocker);
722 + /* restore the VM state */
723 + f = qemu_fopen_ops(be, &loadstate_file_ops);
725 + error_setg(errp, "Could not open VM state file");
729 + qemu_system_reset(SHUTDOWN_CAUSE_NONE);
730 + ret = qemu_loadvm_state(f);
733 + migration_incoming_state_destroy();
735 + error_setg_errno(errp, -ret, "Error while loading VM state");
743 + blk_op_unblock_all(be, blocker);
744 + error_free(blocker);
749 diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
750 index 705f08a8f1..77ab152aab 100644
751 --- a/monitor/hmp-cmds.c
752 +++ b/monitor/hmp-cmds.c
753 @@ -1949,6 +1949,63 @@ void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
754 hmp_handle_error(mon, err);
757 +void hmp_savevm_start(Monitor *mon, const QDict *qdict)
759 + Error *errp = NULL;
760 + const char *statefile = qdict_get_try_str(qdict, "statefile");
762 + qmp_savevm_start(statefile != NULL, statefile, &errp);
763 + hmp_handle_error(mon, errp);
766 +void hmp_snapshot_drive(Monitor *mon, const QDict *qdict)
768 + Error *errp = NULL;
769 + const char *name = qdict_get_str(qdict, "name");
770 + const char *device = qdict_get_str(qdict, "device");
772 + qmp_snapshot_drive(device, name, &errp);
773 + hmp_handle_error(mon, errp);
776 +void hmp_delete_drive_snapshot(Monitor *mon, const QDict *qdict)
778 + Error *errp = NULL;
779 + const char *name = qdict_get_str(qdict, "name");
780 + const char *device = qdict_get_str(qdict, "device");
782 + qmp_delete_drive_snapshot(device, name, &errp);
783 + hmp_handle_error(mon, errp);
786 +void coroutine_fn hmp_savevm_end(Monitor *mon, const QDict *qdict)
788 + Error *errp = NULL;
790 + qmp_savevm_end(&errp);
791 + hmp_handle_error(mon, errp);
794 +void hmp_info_savevm(Monitor *mon, const QDict *qdict)
797 + info = qmp_query_savevm(NULL);
799 + if (info->has_status) {
800 + monitor_printf(mon, "savevm status: %s\n", info->status);
801 + monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
804 + monitor_printf(mon, "savevm status: not running\n");
806 + if (info->has_bytes) {
807 + monitor_printf(mon, "Bytes saved: %"PRIu64"\n", info->bytes);
809 + if (info->has_error) {
810 + monitor_printf(mon, "Error: %s\n", info->error);
814 void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
816 IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
817 diff --git a/qapi/migration.json b/qapi/migration.json
818 index 3c75820527..cb3627884c 100644
819 --- a/qapi/migration.json
820 +++ b/qapi/migration.json
822 '*compression': 'CompressionStats',
823 '*socket-address': ['SocketAddress'] } }
828 +# Information about current migration process.
830 +# @status: string describing the current savevm status.
831 +# This can be 'active', 'completed', 'failed'.
832 +# If this field is not returned, no savevm process
833 +# has been initiated
835 +# @error: string containing error message is status is failed.
837 +# @total-time: total amount of milliseconds since savevm started.
838 +# If savevm has ended, it returns the total save time
840 +# @bytes: total amount of data transfered
844 +{ 'struct': 'SaveVMInfo',
845 + 'data': {'*status': 'str', '*error': 'str',
846 + '*total-time': 'int', '*bytes': 'int'} }
851 +# Returns information about current savevm process.
853 +# Returns: @SaveVMInfo
857 +{ 'command': 'query-savevm', 'returns': 'SaveVMInfo' }
862 diff --git a/qapi/misc.json b/qapi/misc.json
863 index 40df513856..4f5333d960 100644
868 { 'command': 'query-fdsets', 'returns': ['FdsetInfo'] }
873 +# Prepare for snapshot and halt VM. Save VM state to statefile.
876 +{ 'command': 'savevm-start', 'data': { '*statefile': 'str' } }
881 +# Create an internal drive snapshot.
884 +{ 'command': 'snapshot-drive', 'data': { 'device': 'str', 'name': 'str' } }
887 +# @delete-drive-snapshot:
889 +# Delete a drive snapshot.
892 +{ 'command': 'delete-drive-snapshot', 'data': { 'device': 'str', 'name': 'str' } }
897 +# Resume VM after a snapshot.
900 +{ 'command': 'savevm-end', 'coroutine': true }
903 # @CommandLineParameterType:
905 diff --git a/qemu-options.hx b/qemu-options.hx
906 index 104632ea34..c1352312c2 100644
907 --- a/qemu-options.hx
908 +++ b/qemu-options.hx
909 @@ -3903,6 +3903,18 @@ SRST
910 Start right away with a saved state (``loadvm`` in monitor)
913 +DEF("loadstate", HAS_ARG, QEMU_OPTION_loadstate, \
914 + "-loadstate file\n" \
915 + " start right away with a saved state\n",
919 + Start right away with a saved state. This option does not rollback
920 + disk state like @code{loadvm}, so user must make sure that disk
921 + have correct state. @var{file} can be any valid device URL. See the section
922 + for "Device URL Syntax" for more information.
926 DEF("daemonize", 0, QEMU_OPTION_daemonize, \
927 "-daemonize daemonize QEMU after initializing\n", QEMU_ARCH_ALL)
928 diff --git a/softmmu/vl.c b/softmmu/vl.c
929 index e6e0ad5a92..03152c816c 100644
932 @@ -2878,6 +2878,7 @@ void qemu_init(int argc, char **argv, char **envp)
935 const char *loadvm = NULL;
936 + const char *loadstate = NULL;
937 MachineClass *machine_class;
938 const char *cpu_option;
939 const char *vga_model = NULL;
940 @@ -3439,6 +3440,9 @@ void qemu_init(int argc, char **argv, char **envp)
941 case QEMU_OPTION_loadvm:
944 + case QEMU_OPTION_loadstate:
945 + loadstate = optarg;
947 case QEMU_OPTION_full_screen:
948 dpy.has_full_screen = true;
949 dpy.full_screen = true;
950 @@ -4478,6 +4482,12 @@ void qemu_init(int argc, char **argv, char **envp)
954 + } else if (loadstate) {
955 + Error *local_err = NULL;
956 + if (load_snapshot_from_blockdev(loadstate, &local_err) < 0) {
957 + error_report_err(local_err);
961 if (replay_mode != REPLAY_MODE_NONE) {
962 replay_vmstate_init();