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 register yank before migration_incoming_state_destroy]
26 Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
27 [FE: further improve aborting
28 adapt to removal of QEMUFileOps
29 improve condition for entering final stage
30 adapt to QAPI and other changes for 8.0]
31 Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
33 hmp-commands-info.hx | 13 +
34 hmp-commands.hx | 17 ++
35 include/migration/snapshot.h | 2 +
36 include/monitor/hmp.h | 3 +
37 migration/meson.build | 1 +
38 migration/savevm-async.c | 533 +++++++++++++++++++++++++++++++++++
39 monitor/hmp-cmds.c | 38 +++
40 qapi/migration.json | 34 +++
41 qapi/misc.json | 16 ++
42 qemu-options.hx | 12 +
44 11 files changed, 679 insertions(+)
45 create mode 100644 migration/savevm-async.c
47 diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
48 index 47d63d26db..a166bff3d5 100644
49 --- a/hmp-commands-info.hx
50 +++ b/hmp-commands-info.hx
51 @@ -540,6 +540,19 @@ SRST
52 Show current migration parameters.
59 + .help = "show savevm status",
60 + .cmd = hmp_info_savevm,
71 diff --git a/hmp-commands.hx b/hmp-commands.hx
72 index bb85ee1d26..d9f9f42d11 100644
75 @@ -1846,3 +1846,20 @@ SRST
76 List event channels in the guest
81 + .name = "savevm-start",
82 + .args_type = "statefile:s?",
83 + .params = "[statefile]",
84 + .help = "Prepare for snapshot and halt VM. Save VM state to statefile.",
85 + .cmd = hmp_savevm_start,
89 + .name = "savevm-end",
92 + .help = "Resume VM after snaphot.",
93 + .cmd = hmp_savevm_end,
96 diff --git a/include/migration/snapshot.h b/include/migration/snapshot.h
97 index e72083b117..c846d37806 100644
98 --- a/include/migration/snapshot.h
99 +++ b/include/migration/snapshot.h
100 @@ -61,4 +61,6 @@ bool delete_snapshot(const char *name,
101 bool has_devices, strList *devices,
104 +int load_snapshot_from_blockdev(const char *filename, Error **errp);
107 diff --git a/include/monitor/hmp.h b/include/monitor/hmp.h
108 index fdb69b7f9c..fdf6b45fb8 100644
109 --- a/include/monitor/hmp.h
110 +++ b/include/monitor/hmp.h
111 @@ -28,6 +28,7 @@ void hmp_info_status(Monitor *mon, const QDict *qdict);
112 void hmp_info_uuid(Monitor *mon, const QDict *qdict);
113 void hmp_info_chardev(Monitor *mon, const QDict *qdict);
114 void hmp_info_mice(Monitor *mon, const QDict *qdict);
115 +void hmp_info_savevm(Monitor *mon, const QDict *qdict);
116 void hmp_info_migrate(Monitor *mon, const QDict *qdict);
117 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
118 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict);
119 @@ -94,6 +95,8 @@ void hmp_closefd(Monitor *mon, const QDict *qdict);
120 void hmp_mouse_move(Monitor *mon, const QDict *qdict);
121 void hmp_mouse_button(Monitor *mon, const QDict *qdict);
122 void hmp_mouse_set(Monitor *mon, const QDict *qdict);
123 +void hmp_savevm_start(Monitor *mon, const QDict *qdict);
124 +void hmp_savevm_end(Monitor *mon, const QDict *qdict);
125 void hmp_sendkey(Monitor *mon, const QDict *qdict);
126 void coroutine_fn hmp_screendump(Monitor *mon, const QDict *qdict);
127 void hmp_chardev_add(Monitor *mon, const QDict *qdict);
128 diff --git a/migration/meson.build b/migration/meson.build
129 index 8a142fc7a9..a7824b5266 100644
130 --- a/migration/meson.build
131 +++ b/migration/meson.build
132 @@ -25,6 +25,7 @@ softmmu_ss.add(files(
140 diff --git a/migration/savevm-async.c b/migration/savevm-async.c
142 index 0000000000..ac1fac6378
144 +++ b/migration/savevm-async.c
146 +#include "qemu/osdep.h"
147 +#include "migration/channel-savevm-async.h"
148 +#include "migration/migration.h"
149 +#include "migration/savevm.h"
150 +#include "migration/snapshot.h"
151 +#include "migration/global_state.h"
152 +#include "migration/ram.h"
153 +#include "migration/qemu-file.h"
154 +#include "sysemu/sysemu.h"
155 +#include "sysemu/runstate.h"
156 +#include "block/block.h"
157 +#include "sysemu/block-backend.h"
158 +#include "qapi/error.h"
159 +#include "qapi/qmp/qerror.h"
160 +#include "qapi/qmp/qdict.h"
161 +#include "qapi/qapi-commands-migration.h"
162 +#include "qapi/qapi-commands-misc.h"
163 +#include "qapi/qapi-commands-block.h"
164 +#include "qemu/cutils.h"
165 +#include "qemu/timer.h"
166 +#include "qemu/main-loop.h"
167 +#include "qemu/rcu.h"
168 +#include "qemu/yank.h"
170 +/* #define DEBUG_SAVEVM_STATE */
172 +#ifdef DEBUG_SAVEVM_STATE
173 +#define DPRINTF(fmt, ...) \
174 + do { printf("savevm-async: " fmt, ## __VA_ARGS__); } while (0)
176 +#define DPRINTF(fmt, ...) \
184 + SAVE_STATE_COMPLETED,
185 + SAVE_STATE_CANCELLED
189 +static struct SnapshotState {
190 + BlockBackend *target;
195 + int saved_vm_running;
197 + int64_t total_time;
198 + QEMUBH *finalize_bh;
200 + QemuCoSleep target_close_wait;
203 +static bool savevm_aborted(void)
205 + return snap_state.state == SAVE_STATE_CANCELLED ||
206 + snap_state.state == SAVE_STATE_ERROR;
209 +SaveVMInfo *qmp_query_savevm(Error **errp)
211 + SaveVMInfo *info = g_malloc0(sizeof(*info));
212 + struct SnapshotState *s = &snap_state;
214 + if (s->state != SAVE_STATE_DONE) {
215 + info->has_bytes = true;
216 + info->bytes = s->bs_pos;
217 + switch (s->state) {
218 + case SAVE_STATE_ERROR:
219 + info->status = g_strdup("failed");
220 + info->has_total_time = true;
221 + info->total_time = s->total_time;
223 + info->error = g_strdup(error_get_pretty(s->error));
226 + case SAVE_STATE_ACTIVE:
227 + info->status = g_strdup("active");
228 + info->has_total_time = true;
229 + info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
232 + case SAVE_STATE_COMPLETED:
233 + info->status = g_strdup("completed");
234 + info->has_total_time = true;
235 + info->total_time = s->total_time;
243 +static int save_snapshot_cleanup(void)
247 + DPRINTF("save_snapshot_cleanup\n");
249 + snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
250 + snap_state.total_time;
252 + if (snap_state.file) {
253 + ret = qemu_fclose(snap_state.file);
254 + snap_state.file = NULL;
257 + if (snap_state.target) {
258 + if (!savevm_aborted()) {
259 + /* try to truncate, but ignore errors (will fail on block devices).
260 + * note1: bdrv_read() need whole blocks, so we need to round up
261 + * note2: PVE requires 1024 (BDRV_SECTOR_SIZE*2) alignment
263 + size_t size = QEMU_ALIGN_UP(snap_state.bs_pos, BDRV_SECTOR_SIZE*2);
264 + blk_truncate(snap_state.target, size, false, PREALLOC_MODE_OFF, 0, NULL);
266 + blk_op_unblock_all(snap_state.target, snap_state.blocker);
267 + error_free(snap_state.blocker);
268 + snap_state.blocker = NULL;
269 + blk_unref(snap_state.target);
270 + snap_state.target = NULL;
272 + qemu_co_sleep_wake(&snap_state.target_close_wait);
278 +static void save_snapshot_error(const char *fmt, ...)
284 + msg = g_strdup_vprintf(fmt, ap);
287 + DPRINTF("save_snapshot_error: %s\n", msg);
289 + if (!snap_state.error) {
290 + error_set(&snap_state.error, ERROR_CLASS_GENERIC_ERROR, "%s", msg);
295 + snap_state.state = SAVE_STATE_ERROR;
298 +static void process_savevm_finalize(void *opaque)
301 + AioContext *iohandler_ctx = iohandler_get_aio_context();
302 + MigrationState *ms = migrate_get_current();
304 + bool aborted = savevm_aborted();
306 +#ifdef DEBUG_SAVEVM_STATE
307 + int64_t start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
310 + qemu_bh_delete(snap_state.finalize_bh);
311 + snap_state.finalize_bh = NULL;
312 + snap_state.co = NULL;
314 + /* We need to own the target bdrv's context for the following functions,
315 + * so move it back. It can stay in the main context and live out its live
316 + * there, since we're done with it after this method ends anyway.
318 + aio_context_acquire(iohandler_ctx);
319 + blk_set_aio_context(snap_state.target, qemu_get_aio_context(), NULL);
320 + aio_context_release(iohandler_ctx);
322 + ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
324 + save_snapshot_error("vm_stop_force_state error %d", ret);
328 + /* skip state saving if we aborted, snapshot will be invalid anyway */
329 + (void)qemu_savevm_state_complete_precopy(snap_state.file, false, false);
330 + ret = qemu_file_get_error(snap_state.file);
332 + save_snapshot_error("qemu_savevm_state_complete_precopy error %d", ret);
336 + DPRINTF("state saving complete\n");
337 + DPRINTF("timing: process_savevm_finalize (state saving) took %ld ms\n",
338 + qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - start_time);
340 + /* clear migration state */
341 + migrate_set_state(&ms->state, MIGRATION_STATUS_SETUP,
342 + ret || aborted ? MIGRATION_STATUS_FAILED : MIGRATION_STATUS_COMPLETED);
343 + ms->to_dst_file = NULL;
345 + qemu_savevm_state_cleanup();
347 + ret = save_snapshot_cleanup();
349 + save_snapshot_error("save_snapshot_cleanup error %d", ret);
350 + } else if (snap_state.state == SAVE_STATE_ACTIVE) {
351 + snap_state.state = SAVE_STATE_COMPLETED;
352 + } else if (aborted) {
354 + * If there was an error, there's no need to set a new one here.
355 + * If the snapshot was canceled, leave setting the state to
356 + * qmp_savevm_end(), which is waked by save_snapshot_cleanup().
359 + save_snapshot_error("process_savevm_cleanup: invalid state: %d",
362 + if (snap_state.saved_vm_running) {
364 + snap_state.saved_vm_running = false;
367 + DPRINTF("timing: process_savevm_finalize (full) took %ld ms\n",
368 + qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - start_time);
371 +static void coroutine_fn process_savevm_co(void *opaque)
375 + BdrvNextIterator it;
376 + BlockDriverState *bs = NULL;
378 +#ifdef DEBUG_SAVEVM_STATE
379 + int64_t start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
382 + ret = qemu_file_get_error(snap_state.file);
384 + save_snapshot_error("qemu_savevm_state_setup failed");
388 + while (snap_state.state == SAVE_STATE_ACTIVE) {
389 + uint64_t pending_size, pend_precopy, pend_postcopy;
390 + uint64_t threshold = 400 * 1000;
393 + * pending_{estimate,exact} are expected to be called without iothread
394 + * lock. Similar to what is done in migration.c, call the exact variant
395 + * only once pend_precopy in the estimate is below the threshold.
397 + qemu_mutex_unlock_iothread();
398 + qemu_savevm_state_pending_estimate(&pend_precopy, &pend_postcopy);
399 + if (pend_precopy <= threshold) {
400 + qemu_savevm_state_pending_exact(&pend_precopy, &pend_postcopy);
402 + qemu_mutex_lock_iothread();
403 + pending_size = pend_precopy + pend_postcopy;
406 + * A guest reaching this cutoff is dirtying lots of RAM. It should be
407 + * large enough so that the guest can't dirty this much between the
408 + * check and the guest actually being stopped, but it should be small
409 + * enough to avoid long downtimes for non-hibernation snapshots.
411 + maxlen = blk_getlength(snap_state.target) - 100*1024*1024;
413 + /* Note that there is no progress for pend_postcopy when iterating */
414 + if (pend_precopy > threshold && snap_state.bs_pos + pending_size < maxlen) {
415 + ret = qemu_savevm_state_iterate(snap_state.file, false);
417 + save_snapshot_error("qemu_savevm_state_iterate error %d", ret);
420 + DPRINTF("savevm iterate pending size %lu ret %d\n", pending_size, ret);
422 + qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
423 + ret = global_state_store();
425 + save_snapshot_error("global_state_store error %d", ret);
429 + DPRINTF("savevm iterate complete\n");
434 + DPRINTF("timing: process_savevm_co took %ld ms\n",
435 + qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - start_time);
437 +#ifdef DEBUG_SAVEVM_STATE
438 + int64_t start_time_flush = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
440 + /* If a drive runs in an IOThread we can flush it async, and only
441 + * need to sync-flush whatever IO happens between now and
442 + * vm_stop_force_state. bdrv_next can only be called from main AioContext,
443 + * so move there now and after every flush.
445 + aio_co_reschedule_self(qemu_get_aio_context());
446 + for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
447 + /* target has BDRV_O_NO_FLUSH, no sense calling bdrv_flush on it */
448 + if (bs == blk_bs(snap_state.target)) {
452 + AioContext *bs_ctx = bdrv_get_aio_context(bs);
453 + if (bs_ctx != qemu_get_aio_context()) {
454 + DPRINTF("savevm: async flushing drive %s\n", bs->filename);
455 + aio_co_reschedule_self(bs_ctx);
456 + bdrv_graph_co_rdlock();
458 + bdrv_graph_co_rdunlock();
459 + aio_co_reschedule_self(qemu_get_aio_context());
463 + DPRINTF("timing: async flushing took %ld ms\n",
464 + qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - start_time_flush);
466 + qemu_bh_schedule(snap_state.finalize_bh);
469 +void qmp_savevm_start(const char *statefile, Error **errp)
471 + Error *local_err = NULL;
472 + MigrationState *ms = migrate_get_current();
473 + AioContext *iohandler_ctx = iohandler_get_aio_context();
475 + int bdrv_oflags = BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_NO_FLUSH;
477 + if (snap_state.state != SAVE_STATE_DONE) {
478 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
479 + "VM snapshot already started\n");
483 + if (migration_is_running(ms->state)) {
484 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, QERR_MIGRATION_ACTIVE);
488 + if (migrate_use_block()) {
489 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
490 + "Block migration and snapshots are incompatible");
494 + /* initialize snapshot info */
495 + snap_state.saved_vm_running = runstate_is_running();
496 + snap_state.bs_pos = 0;
497 + snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
498 + snap_state.blocker = NULL;
499 + snap_state.target_close_wait = (QemuCoSleep){ .to_wake = NULL };
501 + if (snap_state.error) {
502 + error_free(snap_state.error);
503 + snap_state.error = NULL;
507 + vm_stop(RUN_STATE_SAVE_VM);
508 + snap_state.state = SAVE_STATE_COMPLETED;
512 + if (qemu_savevm_state_blocked(errp)) {
516 + /* Open the image */
517 + QDict *options = NULL;
518 + options = qdict_new();
519 + qdict_put_str(options, "driver", "raw");
520 + snap_state.target = blk_new_open(statefile, NULL, options, bdrv_oflags, &local_err);
521 + if (!snap_state.target) {
522 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
526 + QIOChannel *ioc = QIO_CHANNEL(qio_channel_savevm_async_new(snap_state.target,
527 + &snap_state.bs_pos));
528 + snap_state.file = qemu_file_new_output(ioc);
530 + if (!snap_state.file) {
531 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
536 + * qemu_savevm_* paths use migration code and expect a migration state.
537 + * State is cleared in process_savevm_co, but has to be initialized
538 + * here (blocking main thread, from QMP) to avoid race conditions.
541 + memset(&ram_counters, 0, sizeof(ram_counters));
542 + memset(&compression_counters, 0, sizeof(compression_counters));
543 + ms->to_dst_file = snap_state.file;
545 + error_setg(&snap_state.blocker, "block device is in use by savevm");
546 + blk_op_block_all(snap_state.target, snap_state.blocker);
548 + snap_state.state = SAVE_STATE_ACTIVE;
549 + snap_state.finalize_bh = qemu_bh_new(process_savevm_finalize, &snap_state);
550 + snap_state.co = qemu_coroutine_create(&process_savevm_co, NULL);
551 + qemu_mutex_unlock_iothread();
552 + qemu_savevm_state_header(snap_state.file);
553 + qemu_savevm_state_setup(snap_state.file);
554 + qemu_mutex_lock_iothread();
556 + /* Async processing from here on out happens in iohandler context, so let
557 + * the target bdrv have its home there.
559 + blk_set_aio_context(snap_state.target, iohandler_ctx, &local_err);
561 + aio_co_schedule(iohandler_ctx, snap_state.co);
567 + save_snapshot_error("setup failed");
569 + if (snap_state.saved_vm_running) {
571 + snap_state.saved_vm_running = false;
575 +void coroutine_fn qmp_savevm_end(Error **errp)
579 + if (snap_state.state == SAVE_STATE_DONE) {
580 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
581 + "VM snapshot not started\n");
585 + if (snap_state.state == SAVE_STATE_ACTIVE) {
586 + snap_state.state = SAVE_STATE_CANCELLED;
587 + goto wait_for_close;
590 + if (snap_state.saved_vm_running) {
592 + snap_state.saved_vm_running = false;
595 + snap_state.state = SAVE_STATE_DONE;
598 + if (!snap_state.target) {
599 + DPRINTF("savevm-end: no target file open\n");
603 + /* wait until cleanup is done before returning, this ensures that after this
604 + * call exits the statefile will be closed and can be removed immediately */
605 + DPRINTF("savevm-end: waiting for cleanup\n");
606 + timeout = 30L * 1000 * 1000 * 1000;
607 + qemu_co_sleep_ns_wakeable(&snap_state.target_close_wait,
608 + QEMU_CLOCK_REALTIME, timeout);
609 + if (snap_state.target) {
610 + save_snapshot_error("timeout waiting for target file close in "
612 + /* we cannot assume the snapshot finished in this case, so leave the
613 + * state alone - caller has to figure something out */
617 + // File closed and no other error, so ensure next snapshot can be started.
618 + if (snap_state.state != SAVE_STATE_ERROR) {
619 + snap_state.state = SAVE_STATE_DONE;
622 + DPRINTF("savevm-end: cleanup done\n");
625 +int load_snapshot_from_blockdev(const char *filename, Error **errp)
628 + Error *local_err = NULL;
629 + Error *blocker = NULL;
635 + be = blk_new_open(filename, NULL, NULL, 0, &local_err);
638 + error_setg(errp, "Could not open VM state file");
642 + error_setg(&blocker, "block device is in use by load state");
643 + blk_op_block_all(be, blocker);
645 + /* restore the VM state */
646 + f = qemu_file_new_input(QIO_CHANNEL(qio_channel_savevm_async_new(be, &bs_pos)));
648 + error_setg(errp, "Could not open VM state file");
652 + qemu_system_reset(SHUTDOWN_CAUSE_NONE);
653 + ret = qemu_loadvm_state(f);
655 + /* dirty bitmap migration has a special case we need to trigger manually */
656 + dirty_bitmap_mig_before_vm_start();
660 + /* state_destroy assumes a real migration which would have added a yank */
661 + yank_register_instance(MIGRATION_YANK_INSTANCE, &error_abort);
663 + migration_incoming_state_destroy();
665 + error_setg_errno(errp, -ret, "Error while loading VM state");
673 + blk_op_unblock_all(be, blocker);
674 + error_free(blocker);
679 diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
680 index 6c559b48c8..91be698308 100644
681 --- a/monitor/hmp-cmds.c
682 +++ b/monitor/hmp-cmds.c
684 #include "monitor/monitor-internal.h"
685 #include "qapi/error.h"
686 #include "qapi/qapi-commands-control.h"
687 +#include "qapi/qapi-commands-migration.h"
688 #include "qapi/qapi-commands-misc.h"
689 #include "qapi/qmp/qdict.h"
690 #include "qapi/qmp/qerror.h"
691 @@ -443,3 +444,40 @@ void hmp_info_mtree(Monitor *mon, const QDict *qdict)
693 mtree_info(flatview, dispatch_tree, owner, disabled);
696 +void hmp_savevm_start(Monitor *mon, const QDict *qdict)
698 + Error *errp = NULL;
699 + const char *statefile = qdict_get_try_str(qdict, "statefile");
701 + qmp_savevm_start(statefile, &errp);
702 + hmp_handle_error(mon, errp);
705 +void coroutine_fn hmp_savevm_end(Monitor *mon, const QDict *qdict)
707 + Error *errp = NULL;
709 + qmp_savevm_end(&errp);
710 + hmp_handle_error(mon, errp);
713 +void hmp_info_savevm(Monitor *mon, const QDict *qdict)
716 + info = qmp_query_savevm(NULL);
718 + if (info->status) {
719 + monitor_printf(mon, "savevm status: %s\n", info->status);
720 + monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
723 + monitor_printf(mon, "savevm status: not running\n");
725 + if (info->has_bytes) {
726 + monitor_printf(mon, "Bytes saved: %"PRIu64"\n", info->bytes);
729 + monitor_printf(mon, "Error: %s\n", info->error);
732 diff --git a/qapi/migration.json b/qapi/migration.json
733 index c84fa10e86..1702b92553 100644
734 --- a/qapi/migration.json
735 +++ b/qapi/migration.json
737 '*compression': 'CompressionStats',
738 '*socket-address': ['SocketAddress'] } }
743 +# Information about current migration process.
745 +# @status: string describing the current savevm status.
746 +# This can be 'active', 'completed', 'failed'.
747 +# If this field is not returned, no savevm process
748 +# has been initiated
750 +# @error: string containing error message is status is failed.
752 +# @total-time: total amount of milliseconds since savevm started.
753 +# If savevm has ended, it returns the total save time
755 +# @bytes: total amount of data transfered
759 +{ 'struct': 'SaveVMInfo',
760 + 'data': {'*status': 'str', '*error': 'str',
761 + '*total-time': 'int', '*bytes': 'int'} }
766 +# Returns information about current savevm process.
768 +# Returns: @SaveVMInfo
772 +{ 'command': 'query-savevm', 'returns': 'SaveVMInfo' }
777 diff --git a/qapi/misc.json b/qapi/misc.json
778 index 6ddd16ea28..e5681ae8a2 100644
783 { 'command': 'query-fdsets', 'returns': ['FdsetInfo'] }
788 +# Prepare for snapshot and halt VM. Save VM state to statefile.
791 +{ 'command': 'savevm-start', 'data': { '*statefile': 'str' } }
796 +# Resume VM after a snapshot.
799 +{ 'command': 'savevm-end', 'coroutine': true }
802 # @CommandLineParameterType:
804 diff --git a/qemu-options.hx b/qemu-options.hx
805 index fdddfab6ff..fdd551c2bb 100644
806 --- a/qemu-options.hx
807 +++ b/qemu-options.hx
808 @@ -4398,6 +4398,18 @@ SRST
809 Start right away with a saved state (``loadvm`` in monitor)
812 +DEF("loadstate", HAS_ARG, QEMU_OPTION_loadstate, \
813 + "-loadstate file\n" \
814 + " start right away with a saved state\n",
818 + Start right away with a saved state. This option does not rollback
819 + disk state like @code{loadvm}, so user must make sure that disk
820 + have correct state. @var{file} can be any valid device URL. See the section
821 + for "Device URL Syntax" for more information.
825 DEF("daemonize", 0, QEMU_OPTION_daemonize, \
826 "-daemonize daemonize QEMU after initializing\n", QEMU_ARCH_ALL)
827 diff --git a/softmmu/vl.c b/softmmu/vl.c
828 index ea20b23e4c..0eabc71b68 100644
831 @@ -164,6 +164,7 @@ static const char *accelerators;
832 static bool have_custom_ram_size;
833 static const char *ram_memdev_id;
834 static QDict *machine_opts_dict;
835 +static const char *loadstate;
836 static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
837 static QTAILQ_HEAD(, DeviceOption) device_opts = QTAILQ_HEAD_INITIALIZER(device_opts);
838 static int display_remote;
839 @@ -2612,6 +2613,12 @@ void qmp_x_exit_preconfig(Error **errp)
842 load_snapshot(loadvm, NULL, false, NULL, &error_fatal);
843 + } else if (loadstate) {
844 + Error *local_err = NULL;
845 + if (load_snapshot_from_blockdev(loadstate, &local_err) < 0) {
846 + error_report_err(local_err);
850 if (replay_mode != REPLAY_MODE_NONE) {
851 replay_vmstate_init();
852 @@ -3159,6 +3166,9 @@ void qemu_init(int argc, char **argv)
853 case QEMU_OPTION_loadvm:
856 + case QEMU_OPTION_loadstate:
857 + loadstate = optarg;
859 case QEMU_OPTION_full_screen:
860 dpy.has_full_screen = true;
861 dpy.full_screen = true;