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>
26 [FE: further improve aborting
27 adapt to removal of QEMUFileOps
28 improve condition for entering final stage]
29 Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
31 hmp-commands-info.hx | 13 +
32 hmp-commands.hx | 33 +++
33 include/migration/snapshot.h | 2 +
34 include/monitor/hmp.h | 5 +
35 migration/meson.build | 1 +
36 migration/savevm-async.c | 532 +++++++++++++++++++++++++++++++++++
37 monitor/hmp-cmds.c | 57 ++++
38 qapi/migration.json | 34 +++
39 qapi/misc.json | 32 +++
40 qemu-options.hx | 12 +
42 11 files changed, 731 insertions(+)
43 create mode 100644 migration/savevm-async.c
45 diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
46 index 754b1e8408..489c524e9e 100644
47 --- a/hmp-commands-info.hx
48 +++ b/hmp-commands-info.hx
49 @@ -540,6 +540,19 @@ SRST
50 Show current migration parameters.
57 + .help = "show savevm status",
58 + .cmd = hmp_info_savevm,
69 diff --git a/hmp-commands.hx b/hmp-commands.hx
70 index 673e39a697..039be0033d 100644
73 @@ -1815,3 +1815,36 @@ SRST
74 Dump the FDT in dtb format to *filename*.
79 + .name = "savevm-start",
80 + .args_type = "statefile:s?",
81 + .params = "[statefile]",
82 + .help = "Prepare for snapshot and halt VM. Save VM state to statefile.",
83 + .cmd = hmp_savevm_start,
87 + .name = "snapshot-drive",
88 + .args_type = "device:s,name:s",
89 + .params = "device name",
90 + .help = "Create internal snapshot.",
91 + .cmd = hmp_snapshot_drive,
95 + .name = "delete-drive-snapshot",
96 + .args_type = "device:s,name:s",
97 + .params = "device name",
98 + .help = "Delete internal snapshot.",
99 + .cmd = hmp_delete_drive_snapshot,
103 + .name = "savevm-end",
106 + .help = "Resume VM after snaphot.",
107 + .cmd = hmp_savevm_end,
110 diff --git a/include/migration/snapshot.h b/include/migration/snapshot.h
111 index e72083b117..c846d37806 100644
112 --- a/include/migration/snapshot.h
113 +++ b/include/migration/snapshot.h
114 @@ -61,4 +61,6 @@ bool delete_snapshot(const char *name,
115 bool has_devices, strList *devices,
118 +int load_snapshot_from_blockdev(const char *filename, Error **errp);
121 diff --git a/include/monitor/hmp.h b/include/monitor/hmp.h
122 index dfbc0c9a2f..440f86aba8 100644
123 --- a/include/monitor/hmp.h
124 +++ b/include/monitor/hmp.h
125 @@ -27,6 +27,7 @@ void hmp_info_status(Monitor *mon, const QDict *qdict);
126 void hmp_info_uuid(Monitor *mon, const QDict *qdict);
127 void hmp_info_chardev(Monitor *mon, const QDict *qdict);
128 void hmp_info_mice(Monitor *mon, const QDict *qdict);
129 +void hmp_info_savevm(Monitor *mon, const QDict *qdict);
130 void hmp_info_migrate(Monitor *mon, const QDict *qdict);
131 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
132 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict);
133 @@ -81,6 +82,10 @@ void hmp_netdev_add(Monitor *mon, const QDict *qdict);
134 void hmp_netdev_del(Monitor *mon, const QDict *qdict);
135 void hmp_getfd(Monitor *mon, const QDict *qdict);
136 void hmp_closefd(Monitor *mon, const QDict *qdict);
137 +void hmp_savevm_start(Monitor *mon, const QDict *qdict);
138 +void hmp_snapshot_drive(Monitor *mon, const QDict *qdict);
139 +void hmp_delete_drive_snapshot(Monitor *mon, const QDict *qdict);
140 +void hmp_savevm_end(Monitor *mon, const QDict *qdict);
141 void hmp_sendkey(Monitor *mon, const QDict *qdict);
142 void coroutine_fn hmp_screendump(Monitor *mon, const QDict *qdict);
143 void hmp_chardev_add(Monitor *mon, const QDict *qdict);
144 diff --git a/migration/meson.build b/migration/meson.build
145 index 8cac83c06c..0842d00cd2 100644
146 --- a/migration/meson.build
147 +++ b/migration/meson.build
148 @@ -24,6 +24,7 @@ softmmu_ss.add(files(
156 diff --git a/migration/savevm-async.c b/migration/savevm-async.c
158 index 0000000000..4a4e91a26d
160 +++ b/migration/savevm-async.c
162 +#include "qemu/osdep.h"
163 +#include "migration/channel-savevm-async.h"
164 +#include "migration/migration.h"
165 +#include "migration/savevm.h"
166 +#include "migration/snapshot.h"
167 +#include "migration/global_state.h"
168 +#include "migration/ram.h"
169 +#include "migration/qemu-file.h"
170 +#include "sysemu/sysemu.h"
171 +#include "sysemu/runstate.h"
172 +#include "block/block.h"
173 +#include "sysemu/block-backend.h"
174 +#include "qapi/error.h"
175 +#include "qapi/qmp/qerror.h"
176 +#include "qapi/qmp/qdict.h"
177 +#include "qapi/qapi-commands-migration.h"
178 +#include "qapi/qapi-commands-misc.h"
179 +#include "qapi/qapi-commands-block.h"
180 +#include "qemu/cutils.h"
181 +#include "qemu/timer.h"
182 +#include "qemu/main-loop.h"
183 +#include "qemu/rcu.h"
185 +/* #define DEBUG_SAVEVM_STATE */
187 +#ifdef DEBUG_SAVEVM_STATE
188 +#define DPRINTF(fmt, ...) \
189 + do { printf("savevm-async: " fmt, ## __VA_ARGS__); } while (0)
191 +#define DPRINTF(fmt, ...) \
199 + SAVE_STATE_COMPLETED,
200 + SAVE_STATE_CANCELLED
204 +static struct SnapshotState {
205 + BlockBackend *target;
210 + int saved_vm_running;
212 + int64_t total_time;
213 + QEMUBH *finalize_bh;
215 + QemuCoSleep target_close_wait;
218 +static bool savevm_aborted(void)
220 + return snap_state.state == SAVE_STATE_CANCELLED ||
221 + snap_state.state == SAVE_STATE_ERROR;
224 +SaveVMInfo *qmp_query_savevm(Error **errp)
226 + SaveVMInfo *info = g_malloc0(sizeof(*info));
227 + struct SnapshotState *s = &snap_state;
229 + if (s->state != SAVE_STATE_DONE) {
230 + info->has_bytes = true;
231 + info->bytes = s->bs_pos;
232 + switch (s->state) {
233 + case SAVE_STATE_ERROR:
234 + info->has_status = true;
235 + info->status = g_strdup("failed");
236 + info->has_total_time = true;
237 + info->total_time = s->total_time;
239 + info->has_error = true;
240 + info->error = g_strdup(error_get_pretty(s->error));
243 + case SAVE_STATE_ACTIVE:
244 + info->has_status = true;
245 + info->status = g_strdup("active");
246 + info->has_total_time = true;
247 + info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
250 + case SAVE_STATE_COMPLETED:
251 + info->has_status = true;
252 + info->status = g_strdup("completed");
253 + info->has_total_time = true;
254 + info->total_time = s->total_time;
262 +static int save_snapshot_cleanup(void)
266 + DPRINTF("save_snapshot_cleanup\n");
268 + snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
269 + snap_state.total_time;
271 + if (snap_state.file) {
272 + ret = qemu_fclose(snap_state.file);
273 + snap_state.file = NULL;
276 + if (snap_state.target) {
277 + if (!savevm_aborted()) {
278 + /* try to truncate, but ignore errors (will fail on block devices).
279 + * note1: bdrv_read() need whole blocks, so we need to round up
280 + * note2: PVE requires 1024 (BDRV_SECTOR_SIZE*2) alignment
282 + size_t size = QEMU_ALIGN_UP(snap_state.bs_pos, BDRV_SECTOR_SIZE*2);
283 + blk_truncate(snap_state.target, size, false, PREALLOC_MODE_OFF, 0, NULL);
285 + blk_op_unblock_all(snap_state.target, snap_state.blocker);
286 + error_free(snap_state.blocker);
287 + snap_state.blocker = NULL;
288 + blk_unref(snap_state.target);
289 + snap_state.target = NULL;
291 + qemu_co_sleep_wake(&snap_state.target_close_wait);
297 +static void save_snapshot_error(const char *fmt, ...)
303 + msg = g_strdup_vprintf(fmt, ap);
306 + DPRINTF("save_snapshot_error: %s\n", msg);
308 + if (!snap_state.error) {
309 + error_set(&snap_state.error, ERROR_CLASS_GENERIC_ERROR, "%s", msg);
314 + snap_state.state = SAVE_STATE_ERROR;
317 +static void process_savevm_finalize(void *opaque)
320 + AioContext *iohandler_ctx = iohandler_get_aio_context();
321 + MigrationState *ms = migrate_get_current();
323 + bool aborted = savevm_aborted();
325 +#ifdef DEBUG_SAVEVM_STATE
326 + int64_t start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
329 + qemu_bh_delete(snap_state.finalize_bh);
330 + snap_state.finalize_bh = NULL;
331 + snap_state.co = NULL;
333 + /* We need to own the target bdrv's context for the following functions,
334 + * so move it back. It can stay in the main context and live out its live
335 + * there, since we're done with it after this method ends anyway.
337 + aio_context_acquire(iohandler_ctx);
338 + blk_set_aio_context(snap_state.target, qemu_get_aio_context(), NULL);
339 + aio_context_release(iohandler_ctx);
341 + ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
343 + save_snapshot_error("vm_stop_force_state error %d", ret);
347 + /* skip state saving if we aborted, snapshot will be invalid anyway */
348 + (void)qemu_savevm_state_complete_precopy(snap_state.file, false, false);
349 + ret = qemu_file_get_error(snap_state.file);
351 + save_snapshot_error("qemu_savevm_state_complete_precopy error %d", ret);
355 + DPRINTF("state saving complete\n");
356 + DPRINTF("timing: process_savevm_finalize (state saving) took %ld ms\n",
357 + qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - start_time);
359 + /* clear migration state */
360 + migrate_set_state(&ms->state, MIGRATION_STATUS_SETUP,
361 + ret || aborted ? MIGRATION_STATUS_FAILED : MIGRATION_STATUS_COMPLETED);
362 + ms->to_dst_file = NULL;
364 + qemu_savevm_state_cleanup();
366 + ret = save_snapshot_cleanup();
368 + save_snapshot_error("save_snapshot_cleanup error %d", ret);
369 + } else if (snap_state.state == SAVE_STATE_ACTIVE) {
370 + snap_state.state = SAVE_STATE_COMPLETED;
371 + } else if (aborted) {
373 + * If there was an error, there's no need to set a new one here.
374 + * If the snapshot was canceled, leave setting the state to
375 + * qmp_savevm_end(), which is waked by save_snapshot_cleanup().
378 + save_snapshot_error("process_savevm_cleanup: invalid state: %d",
381 + if (snap_state.saved_vm_running) {
383 + snap_state.saved_vm_running = false;
386 + DPRINTF("timing: process_savevm_finalize (full) took %ld ms\n",
387 + qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - start_time);
390 +static void coroutine_fn process_savevm_co(void *opaque)
394 + BdrvNextIterator it;
395 + BlockDriverState *bs = NULL;
397 +#ifdef DEBUG_SAVEVM_STATE
398 + int64_t start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
401 + ret = qemu_file_get_error(snap_state.file);
403 + save_snapshot_error("qemu_savevm_state_setup failed");
407 + while (snap_state.state == SAVE_STATE_ACTIVE) {
408 + uint64_t pending_size, pend_precopy, pend_compatible, pend_postcopy;
410 + /* pending is expected to be called without iothread lock */
411 + qemu_mutex_unlock_iothread();
412 + qemu_savevm_state_pending(snap_state.file, 0, &pend_precopy, &pend_compatible, &pend_postcopy);
413 + qemu_mutex_lock_iothread();
415 + pending_size = pend_precopy + pend_compatible + pend_postcopy;
417 + maxlen = blk_getlength(snap_state.target) - 30*1024*1024;
419 + /* Note that there is no progress for pend_postcopy when iterating */
420 + if (pending_size - pend_postcopy > 400000 && snap_state.bs_pos + pending_size < maxlen) {
421 + ret = qemu_savevm_state_iterate(snap_state.file, false);
423 + save_snapshot_error("qemu_savevm_state_iterate error %d", ret);
426 + DPRINTF("savevm iterate pending size %lu ret %d\n", pending_size, ret);
428 + qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
429 + ret = global_state_store();
431 + save_snapshot_error("global_state_store error %d", ret);
435 + DPRINTF("savevm iterate complete\n");
440 + DPRINTF("timing: process_savevm_co took %ld ms\n",
441 + qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - start_time);
443 +#ifdef DEBUG_SAVEVM_STATE
444 + int64_t start_time_flush = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
446 + /* If a drive runs in an IOThread we can flush it async, and only
447 + * need to sync-flush whatever IO happens between now and
448 + * vm_stop_force_state. bdrv_next can only be called from main AioContext,
449 + * so move there now and after every flush.
451 + aio_co_reschedule_self(qemu_get_aio_context());
452 + for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
453 + /* target has BDRV_O_NO_FLUSH, no sense calling bdrv_flush on it */
454 + if (bs == blk_bs(snap_state.target)) {
458 + AioContext *bs_ctx = bdrv_get_aio_context(bs);
459 + if (bs_ctx != qemu_get_aio_context()) {
460 + DPRINTF("savevm: async flushing drive %s\n", bs->filename);
461 + aio_co_reschedule_self(bs_ctx);
463 + aio_co_reschedule_self(qemu_get_aio_context());
467 + DPRINTF("timing: async flushing took %ld ms\n",
468 + qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - start_time_flush);
470 + qemu_bh_schedule(snap_state.finalize_bh);
473 +void qmp_savevm_start(bool has_statefile, const char *statefile, Error **errp)
475 + Error *local_err = NULL;
476 + MigrationState *ms = migrate_get_current();
477 + AioContext *iohandler_ctx = iohandler_get_aio_context();
479 + int bdrv_oflags = BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_NO_FLUSH;
481 + if (snap_state.state != SAVE_STATE_DONE) {
482 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
483 + "VM snapshot already started\n");
487 + if (migration_is_running(ms->state)) {
488 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, QERR_MIGRATION_ACTIVE);
492 + if (migrate_use_block()) {
493 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
494 + "Block migration and snapshots are incompatible");
498 + /* initialize snapshot info */
499 + snap_state.saved_vm_running = runstate_is_running();
500 + snap_state.bs_pos = 0;
501 + snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
502 + snap_state.blocker = NULL;
503 + snap_state.target_close_wait = (QemuCoSleep){ .to_wake = NULL };
505 + if (snap_state.error) {
506 + error_free(snap_state.error);
507 + snap_state.error = NULL;
510 + if (!has_statefile) {
511 + vm_stop(RUN_STATE_SAVE_VM);
512 + snap_state.state = SAVE_STATE_COMPLETED;
516 + if (qemu_savevm_state_blocked(errp)) {
520 + /* Open the image */
521 + QDict *options = NULL;
522 + options = qdict_new();
523 + qdict_put_str(options, "driver", "raw");
524 + snap_state.target = blk_new_open(statefile, NULL, options, bdrv_oflags, &local_err);
525 + if (!snap_state.target) {
526 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
530 + QIOChannel *ioc = QIO_CHANNEL(qio_channel_savevm_async_new(snap_state.target,
531 + &snap_state.bs_pos));
532 + snap_state.file = qemu_file_new_output(ioc);
534 + if (!snap_state.file) {
535 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
540 + * qemu_savevm_* paths use migration code and expect a migration state.
541 + * State is cleared in process_savevm_co, but has to be initialized
542 + * here (blocking main thread, from QMP) to avoid race conditions.
545 + memset(&ram_counters, 0, sizeof(ram_counters));
546 + ms->to_dst_file = snap_state.file;
548 + error_setg(&snap_state.blocker, "block device is in use by savevm");
549 + blk_op_block_all(snap_state.target, snap_state.blocker);
551 + snap_state.state = SAVE_STATE_ACTIVE;
552 + snap_state.finalize_bh = qemu_bh_new(process_savevm_finalize, &snap_state);
553 + snap_state.co = qemu_coroutine_create(&process_savevm_co, NULL);
554 + qemu_mutex_unlock_iothread();
555 + qemu_savevm_state_header(snap_state.file);
556 + qemu_savevm_state_setup(snap_state.file);
557 + qemu_mutex_lock_iothread();
559 + /* Async processing from here on out happens in iohandler context, so let
560 + * the target bdrv have its home there.
562 + blk_set_aio_context(snap_state.target, iohandler_ctx, &local_err);
564 + aio_co_schedule(iohandler_ctx, snap_state.co);
570 + save_snapshot_error("setup failed");
572 + if (snap_state.saved_vm_running) {
574 + snap_state.saved_vm_running = false;
578 +void coroutine_fn qmp_savevm_end(Error **errp)
582 + if (snap_state.state == SAVE_STATE_DONE) {
583 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
584 + "VM snapshot not started\n");
588 + if (snap_state.state == SAVE_STATE_ACTIVE) {
589 + snap_state.state = SAVE_STATE_CANCELLED;
590 + goto wait_for_close;
593 + if (snap_state.saved_vm_running) {
595 + snap_state.saved_vm_running = false;
598 + snap_state.state = SAVE_STATE_DONE;
601 + if (!snap_state.target) {
602 + DPRINTF("savevm-end: no target file open\n");
606 + /* wait until cleanup is done before returning, this ensures that after this
607 + * call exits the statefile will be closed and can be removed immediately */
608 + DPRINTF("savevm-end: waiting for cleanup\n");
609 + timeout = 30L * 1000 * 1000 * 1000;
610 + qemu_co_sleep_ns_wakeable(&snap_state.target_close_wait,
611 + QEMU_CLOCK_REALTIME, timeout);
612 + if (snap_state.target) {
613 + save_snapshot_error("timeout waiting for target file close in "
615 + /* we cannot assume the snapshot finished in this case, so leave the
616 + * state alone - caller has to figure something out */
620 + // File closed and no other error, so ensure next snapshot can be started.
621 + if (snap_state.state != SAVE_STATE_ERROR) {
622 + snap_state.state = SAVE_STATE_DONE;
625 + DPRINTF("savevm-end: cleanup done\n");
628 +// FIXME: Deprecated
629 +void qmp_snapshot_drive(const char *device, const char *name, Error **errp)
631 + // Compatibility to older qemu-server.
632 + qmp_blockdev_snapshot_internal_sync(device, name, errp);
635 +// FIXME: Deprecated
636 +void qmp_delete_drive_snapshot(const char *device, const char *name,
639 + // Compatibility to older qemu-server.
640 + (void)qmp_blockdev_snapshot_delete_internal_sync(device, false, NULL,
644 +int load_snapshot_from_blockdev(const char *filename, Error **errp)
647 + Error *local_err = NULL;
648 + Error *blocker = NULL;
654 + be = blk_new_open(filename, NULL, NULL, 0, &local_err);
657 + error_setg(errp, "Could not open VM state file");
661 + error_setg(&blocker, "block device is in use by load state");
662 + blk_op_block_all(be, blocker);
664 + /* restore the VM state */
665 + f = qemu_file_new_input(QIO_CHANNEL(qio_channel_savevm_async_new(be, &bs_pos)));
667 + error_setg(errp, "Could not open VM state file");
671 + qemu_system_reset(SHUTDOWN_CAUSE_NONE);
672 + ret = qemu_loadvm_state(f);
674 + /* dirty bitmap migration has a special case we need to trigger manually */
675 + dirty_bitmap_mig_before_vm_start();
678 + migration_incoming_state_destroy();
680 + error_setg_errno(errp, -ret, "Error while loading VM state");
688 + blk_op_unblock_all(be, blocker);
689 + error_free(blocker);
694 diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
695 index 480b798963..cfebfd1db5 100644
696 --- a/monitor/hmp-cmds.c
697 +++ b/monitor/hmp-cmds.c
698 @@ -1906,6 +1906,63 @@ void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
699 hmp_handle_error(mon, err);
702 +void hmp_savevm_start(Monitor *mon, const QDict *qdict)
704 + Error *errp = NULL;
705 + const char *statefile = qdict_get_try_str(qdict, "statefile");
707 + qmp_savevm_start(statefile != NULL, statefile, &errp);
708 + hmp_handle_error(mon, errp);
711 +void hmp_snapshot_drive(Monitor *mon, const QDict *qdict)
713 + Error *errp = NULL;
714 + const char *name = qdict_get_str(qdict, "name");
715 + const char *device = qdict_get_str(qdict, "device");
717 + qmp_snapshot_drive(device, name, &errp);
718 + hmp_handle_error(mon, errp);
721 +void hmp_delete_drive_snapshot(Monitor *mon, const QDict *qdict)
723 + Error *errp = NULL;
724 + const char *name = qdict_get_str(qdict, "name");
725 + const char *device = qdict_get_str(qdict, "device");
727 + qmp_delete_drive_snapshot(device, name, &errp);
728 + hmp_handle_error(mon, errp);
731 +void coroutine_fn hmp_savevm_end(Monitor *mon, const QDict *qdict)
733 + Error *errp = NULL;
735 + qmp_savevm_end(&errp);
736 + hmp_handle_error(mon, errp);
739 +void hmp_info_savevm(Monitor *mon, const QDict *qdict)
742 + info = qmp_query_savevm(NULL);
744 + if (info->has_status) {
745 + monitor_printf(mon, "savevm status: %s\n", info->status);
746 + monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
749 + monitor_printf(mon, "savevm status: not running\n");
751 + if (info->has_bytes) {
752 + monitor_printf(mon, "Bytes saved: %"PRIu64"\n", info->bytes);
754 + if (info->has_error) {
755 + monitor_printf(mon, "Error: %s\n", info->error);
759 void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
761 IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
762 diff --git a/qapi/migration.json b/qapi/migration.json
763 index 88ecf86ac8..4435866379 100644
764 --- a/qapi/migration.json
765 +++ b/qapi/migration.json
767 '*compression': 'CompressionStats',
768 '*socket-address': ['SocketAddress'] } }
773 +# Information about current migration process.
775 +# @status: string describing the current savevm status.
776 +# This can be 'active', 'completed', 'failed'.
777 +# If this field is not returned, no savevm process
778 +# has been initiated
780 +# @error: string containing error message is status is failed.
782 +# @total-time: total amount of milliseconds since savevm started.
783 +# If savevm has ended, it returns the total save time
785 +# @bytes: total amount of data transfered
789 +{ 'struct': 'SaveVMInfo',
790 + 'data': {'*status': 'str', '*error': 'str',
791 + '*total-time': 'int', '*bytes': 'int'} }
796 +# Returns information about current savevm process.
798 +# Returns: @SaveVMInfo
802 +{ 'command': 'query-savevm', 'returns': 'SaveVMInfo' }
807 diff --git a/qapi/misc.json b/qapi/misc.json
808 index 27ef5a2b20..b3ce75dcae 100644
813 { 'command': 'query-fdsets', 'returns': ['FdsetInfo'] }
818 +# Prepare for snapshot and halt VM. Save VM state to statefile.
821 +{ 'command': 'savevm-start', 'data': { '*statefile': 'str' } }
826 +# Create an internal drive snapshot.
829 +{ 'command': 'snapshot-drive', 'data': { 'device': 'str', 'name': 'str' } }
832 +# @delete-drive-snapshot:
834 +# Delete a drive snapshot.
837 +{ 'command': 'delete-drive-snapshot', 'data': { 'device': 'str', 'name': 'str' } }
842 +# Resume VM after a snapshot.
845 +{ 'command': 'savevm-end', 'coroutine': true }
848 # @CommandLineParameterType:
850 diff --git a/qemu-options.hx b/qemu-options.hx
851 index 7f99d15b23..54efb127c4 100644
852 --- a/qemu-options.hx
853 +++ b/qemu-options.hx
854 @@ -4391,6 +4391,18 @@ SRST
855 Start right away with a saved state (``loadvm`` in monitor)
858 +DEF("loadstate", HAS_ARG, QEMU_OPTION_loadstate, \
859 + "-loadstate file\n" \
860 + " start right away with a saved state\n",
864 + Start right away with a saved state. This option does not rollback
865 + disk state like @code{loadvm}, so user must make sure that disk
866 + have correct state. @var{file} can be any valid device URL. See the section
867 + for "Device URL Syntax" for more information.
871 DEF("daemonize", 0, QEMU_OPTION_daemonize, \
872 "-daemonize daemonize QEMU after initializing\n", QEMU_ARCH_ALL)
873 diff --git a/softmmu/vl.c b/softmmu/vl.c
874 index 5f7f6ca981..21f067d115 100644
877 @@ -164,6 +164,7 @@ static const char *accelerators;
878 static bool have_custom_ram_size;
879 static const char *ram_memdev_id;
880 static QDict *machine_opts_dict;
881 +static const char *loadstate;
882 static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
883 static QTAILQ_HEAD(, DeviceOption) device_opts = QTAILQ_HEAD_INITIALIZER(device_opts);
884 static int display_remote;
885 @@ -2607,6 +2608,12 @@ void qmp_x_exit_preconfig(Error **errp)
888 load_snapshot(loadvm, NULL, false, NULL, &error_fatal);
889 + } else if (loadstate) {
890 + Error *local_err = NULL;
891 + if (load_snapshot_from_blockdev(loadstate, &local_err) < 0) {
892 + error_report_err(local_err);
896 if (replay_mode != REPLAY_MODE_NONE) {
897 replay_vmstate_init();
898 @@ -3151,6 +3158,9 @@ void qemu_init(int argc, char **argv)
899 case QEMU_OPTION_loadvm:
902 + case QEMU_OPTION_loadstate:
903 + loadstate = optarg;
905 case QEMU_OPTION_full_screen:
906 dpy.has_full_screen = true;
907 dpy.full_screen = true;