1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Wolfgang Bumiller <w.bumiller@proxmox.com>
3 Date: Wed, 9 Dec 2015 16:04:32 +0100
4 Subject: [PATCH] internal snapshot async
8 hmp-commands-info.hx | 13 ++
9 hmp-commands.hx | 32 +++
12 include/sysemu/sysemu.h | 5 +-
13 migration/savevm.c | 12 +-
14 qapi-schema.json | 68 +++++++
15 qemu-options.hx | 13 ++
16 savevm-async.c | 523 ++++++++++++++++++++++++++++++++++++++++++++++++
18 11 files changed, 730 insertions(+), 7 deletions(-)
19 create mode 100644 savevm-async.c
21 diff --git a/Makefile.objs b/Makefile.objs
22 index 6167e7b17d..fbfbbb7f70 100644
25 @@ -50,6 +50,7 @@ common-obj-$(CONFIG_LINUX) += fsdev/
27 common-obj-y += migration/
28 common-obj-y += page_cache.o #aio.o
29 +common-obj-y += savevm-async.o
31 common-obj-$(CONFIG_SPICE) += spice-qemu-char.o
33 diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
34 index a53f105c52..5fc57a2210 100644
35 --- a/hmp-commands-info.hx
36 +++ b/hmp-commands-info.hx
37 @@ -560,6 +560,19 @@ Show current migration xbzrle cache size.
44 + .help = "show savevm status",
45 + .cmd = hmp_info_savevm,
57 diff --git a/hmp-commands.hx b/hmp-commands.hx
58 index 88192817b2..58940a762b 100644
61 @@ -1777,3 +1777,35 @@ ETEXI
67 + .name = "savevm-start",
68 + .args_type = "statefile:s?",
69 + .params = "[statefile]",
70 + .help = "Prepare for snapshot and halt VM. Save VM state to statefile.",
71 + .cmd = hmp_savevm_start,
75 + .name = "snapshot-drive",
76 + .args_type = "device:s,name:s",
77 + .params = "device name",
78 + .help = "Create internal snapshot.",
79 + .cmd = hmp_snapshot_drive,
83 + .name = "delete-drive-snapshot",
84 + .args_type = "device:s,name:s",
85 + .params = "device name",
86 + .help = "Delete internal snapshot.",
87 + .cmd = hmp_delete_drive_snapshot,
91 + .name = "savevm-end",
94 + .help = "Resume VM after snaphot.",
95 + .cmd = hmp_savevm_end,
97 diff --git a/hmp.c b/hmp.c
98 index 904542d026..f725d061e6 100644
101 @@ -2207,6 +2207,63 @@ void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
102 qapi_free_MemoryDeviceInfoList(info_list);
105 +void hmp_savevm_start(Monitor *mon, const QDict *qdict)
107 + Error *errp = NULL;
108 + const char *statefile = qdict_get_try_str(qdict, "statefile");
110 + qmp_savevm_start(statefile != NULL, statefile, &errp);
111 + hmp_handle_error(mon, &errp);
114 +void hmp_snapshot_drive(Monitor *mon, const QDict *qdict)
116 + Error *errp = NULL;
117 + const char *name = qdict_get_str(qdict, "name");
118 + const char *device = qdict_get_str(qdict, "device");
120 + qmp_snapshot_drive(device, name, &errp);
121 + hmp_handle_error(mon, &errp);
124 +void hmp_delete_drive_snapshot(Monitor *mon, const QDict *qdict)
126 + Error *errp = NULL;
127 + const char *name = qdict_get_str(qdict, "name");
128 + const char *device = qdict_get_str(qdict, "device");
130 + qmp_delete_drive_snapshot(device, name, &errp);
131 + hmp_handle_error(mon, &errp);
134 +void hmp_savevm_end(Monitor *mon, const QDict *qdict)
136 + Error *errp = NULL;
138 + qmp_savevm_end(&errp);
139 + hmp_handle_error(mon, &errp);
142 +void hmp_info_savevm(Monitor *mon, const QDict *qdict)
145 + info = qmp_query_savevm(NULL);
147 + if (info->has_status) {
148 + monitor_printf(mon, "savevm status: %s\n", info->status);
149 + monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
152 + monitor_printf(mon, "savevm status: not running\n");
154 + if (info->has_bytes) {
155 + monitor_printf(mon, "Bytes saved: %"PRIu64"\n", info->bytes);
157 + if (info->has_error) {
158 + monitor_printf(mon, "Error: %s\n", info->error);
162 void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
164 IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
165 diff --git a/hmp.h b/hmp.h
166 index 799fd371fa..0497afbf65 100644
169 @@ -26,6 +26,7 @@ void hmp_info_status(Monitor *mon, const QDict *qdict);
170 void hmp_info_uuid(Monitor *mon, const QDict *qdict);
171 void hmp_info_chardev(Monitor *mon, const QDict *qdict);
172 void hmp_info_mice(Monitor *mon, const QDict *qdict);
173 +void hmp_info_savevm(Monitor *mon, const QDict *qdict);
174 void hmp_info_migrate(Monitor *mon, const QDict *qdict);
175 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
176 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict);
177 @@ -92,6 +93,10 @@ void hmp_netdev_add(Monitor *mon, const QDict *qdict);
178 void hmp_netdev_del(Monitor *mon, const QDict *qdict);
179 void hmp_getfd(Monitor *mon, const QDict *qdict);
180 void hmp_closefd(Monitor *mon, const QDict *qdict);
181 +void hmp_savevm_start(Monitor *mon, const QDict *qdict);
182 +void hmp_snapshot_drive(Monitor *mon, const QDict *qdict);
183 +void hmp_delete_drive_snapshot(Monitor *mon, const QDict *qdict);
184 +void hmp_savevm_end(Monitor *mon, const QDict *qdict);
185 void hmp_sendkey(Monitor *mon, const QDict *qdict);
186 void hmp_screendump(Monitor *mon, const QDict *qdict);
187 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict);
188 diff --git a/include/sysemu/sysemu.h b/include/sysemu/sysemu.h
189 index 576c7ce640..74623de16c 100644
190 --- a/include/sysemu/sysemu.h
191 +++ b/include/sysemu/sysemu.h
192 @@ -78,6 +78,7 @@ void qemu_remove_machine_init_done_notifier(Notifier *notify);
193 void hmp_savevm(Monitor *mon, const QDict *qdict);
194 int save_vmstate(Monitor *mon, const char *name);
195 int load_vmstate(const char *name);
196 +int load_state_from_blockdev(const char *filename);
197 void hmp_delvm(Monitor *mon, const QDict *qdict);
198 void hmp_info_snapshots(Monitor *mon, const QDict *qdict);
200 @@ -105,13 +106,13 @@ enum qemu_vm_cmd {
201 #define MAX_VM_CMD_PACKAGED_SIZE (1ul << 24)
203 bool qemu_savevm_state_blocked(Error **errp);
204 -void qemu_savevm_state_begin(QEMUFile *f,
205 +int qemu_savevm_state_begin(QEMUFile *f,
206 const MigrationParams *params);
207 void qemu_savevm_state_header(QEMUFile *f);
208 int qemu_savevm_state_iterate(QEMUFile *f, bool postcopy);
209 void qemu_savevm_state_cleanup(void);
210 void qemu_savevm_state_complete_postcopy(QEMUFile *f);
211 -void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only);
212 +int qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only);
213 void qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size,
214 uint64_t *res_non_postcopiable,
215 uint64_t *res_postcopiable);
216 diff --git a/migration/savevm.c b/migration/savevm.c
217 index 3b19a4a274..feb0dc6834 100644
218 --- a/migration/savevm.c
219 +++ b/migration/savevm.c
220 @@ -970,11 +970,11 @@ void qemu_savevm_state_header(QEMUFile *f)
224 -void qemu_savevm_state_begin(QEMUFile *f,
225 +int qemu_savevm_state_begin(QEMUFile *f,
226 const MigrationParams *params)
232 trace_savevm_state_begin();
233 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
234 @@ -1002,6 +1002,7 @@ void qemu_savevm_state_begin(QEMUFile *f,
242 @@ -1105,7 +1106,7 @@ void qemu_savevm_state_complete_postcopy(QEMUFile *f)
246 -void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
247 +int qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
251 @@ -1139,12 +1140,12 @@ void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
252 save_section_footer(f, se);
254 qemu_file_set_error(f, ret);
265 vmdesc = qjson_new();
266 @@ -1191,6 +1192,7 @@ void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
267 qjson_destroy(vmdesc);
270 + return qemu_file_get_error(f);
273 /* Give an estimate of the amount left to be transferred,
274 diff --git a/qapi-schema.json b/qapi-schema.json
275 index 1b14ff2476..361700d37c 100644
276 --- a/qapi-schema.json
277 +++ b/qapi-schema.json
279 '*error-desc': 'str'} }
284 +# Information about current migration process.
286 +# @status: string describing the current savevm status.
287 +# This can be 'active', 'completed', 'failed'.
288 +# If this field is not returned, no savevm process
289 +# has been initiated
291 +# @error: string containing error message is status is failed.
293 +# @total-time: total amount of milliseconds since savevm started.
294 +# If savevm has ended, it returns the total save time
296 +# @bytes: total amount of data transfered
300 +{ 'struct': 'SaveVMInfo',
301 + 'data': {'*status': 'str', '*error': 'str',
302 + '*total-time': 'int', '*bytes': 'int'} }
307 +# Returns information about current savevm process.
309 +# Returns: @SaveVMInfo
313 +{ 'command': 'query-savevm', 'returns': 'SaveVMInfo' }
318 # Returns information about current migration process. If migration
319 @@ -4735,9 +4769,43 @@
324 { 'command': 'query-target', 'returns': 'TargetInfo' }
329 +# Prepare for snapshot and halt VM. Save VM state to statefile.
332 +{ 'command': 'savevm-start', 'data': { '*statefile': 'str' } }
337 +# Create an internal drive snapshot.
340 +{ 'command': 'snapshot-drive', 'data': { 'device': 'str', 'name': 'str' } }
343 +# @delete-drive-snapshot:
345 +# Delete a drive snapshot.
348 +{ 'command': 'delete-drive-snapshot', 'data': { 'device': 'str', 'name': 'str' } }
353 +# Resume VM after a snapshot.
356 +{ 'command': 'savevm-end' }
362 # An enumeration of key name.
363 diff --git a/qemu-options.hx b/qemu-options.hx
364 index 48dfffd86a..cbcb27da9a 100644
365 --- a/qemu-options.hx
366 +++ b/qemu-options.hx
367 @@ -3396,6 +3396,19 @@ STEXI
368 Start right away with a saved state (@code{loadvm} in monitor)
371 +DEF("loadstate", HAS_ARG, QEMU_OPTION_loadstate, \
372 + "-loadstate file\n" \
373 + " start right away with a saved state\n",
376 +@item -loadstate @var{file}
378 +Start right away with a saved state. This option does not rollback
379 +disk state like @code{loadvm}, so user must make sure that disk
380 +have correct state. @var{file} can be any valid device URL. See the section
381 +for "Device URL Syntax" for more information.
385 DEF("daemonize", 0, QEMU_OPTION_daemonize, \
386 "-daemonize daemonize QEMU after initializing\n", QEMU_ARCH_ALL)
387 diff --git a/savevm-async.c b/savevm-async.c
389 index 0000000000..5fcb56d373
393 +#include "qemu/osdep.h"
394 +#include "qemu-common.h"
395 +#include "qapi/qmp/qerror.h"
396 +#include "qemu/error-report.h"
397 +#include "sysemu/sysemu.h"
398 +#include "qmp-commands.h"
399 +#include "qemu-options.h"
400 +#include "migration/qemu-file.h"
401 +#include "qom/qom-qobject.h"
402 +#include "migration/migration.h"
403 +#include "block/snapshot.h"
404 +#include "block/qapi.h"
405 +#include "block/block.h"
406 +#include "qemu/timer.h"
407 +#include "sysemu/block-backend.h"
408 +#include "qapi/qmp/qstring.h"
409 +#include "qemu/rcu.h"
410 +#include "qemu/thread.h"
411 +#include "qemu/cutils.h"
413 +/* #define DEBUG_SAVEVM_STATE */
415 +#define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
417 +#ifdef DEBUG_SAVEVM_STATE
418 +#define DPRINTF(fmt, ...) \
419 + do { printf("savevm-async: " fmt, ## __VA_ARGS__); } while (0)
421 +#define DPRINTF(fmt, ...) \
429 + SAVE_STATE_COMPLETED,
430 + SAVE_STATE_CANCELLED
434 +static struct SnapshotState {
435 + BlockBackend *target;
440 + int saved_vm_running;
442 + int64_t total_time;
445 +SaveVMInfo *qmp_query_savevm(Error **errp)
447 + SaveVMInfo *info = g_malloc0(sizeof(*info));
448 + struct SnapshotState *s = &snap_state;
450 + if (s->state != SAVE_STATE_DONE) {
451 + info->has_bytes = true;
452 + info->bytes = s->bs_pos;
453 + switch (s->state) {
454 + case SAVE_STATE_ERROR:
455 + info->has_status = true;
456 + info->status = g_strdup("failed");
457 + info->has_total_time = true;
458 + info->total_time = s->total_time;
460 + info->has_error = true;
461 + info->error = g_strdup(error_get_pretty(s->error));
464 + case SAVE_STATE_ACTIVE:
465 + info->has_status = true;
466 + info->status = g_strdup("active");
467 + info->has_total_time = true;
468 + info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
471 + case SAVE_STATE_COMPLETED:
472 + info->has_status = true;
473 + info->status = g_strdup("completed");
474 + info->has_total_time = true;
475 + info->total_time = s->total_time;
483 +static int save_snapshot_cleanup(void)
487 + DPRINTF("save_snapshot_cleanup\n");
489 + snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
490 + snap_state.total_time;
492 + if (snap_state.file) {
493 + ret = qemu_fclose(snap_state.file);
496 + if (snap_state.target) {
497 + /* try to truncate, but ignore errors (will fail on block devices).
498 + * note: bdrv_read() need whole blocks, so we round up
500 + size_t size = (snap_state.bs_pos + BDRV_SECTOR_SIZE) & BDRV_SECTOR_MASK;
501 + blk_truncate(snap_state.target, size, NULL);
502 + blk_op_unblock_all(snap_state.target, snap_state.blocker);
503 + error_free(snap_state.blocker);
504 + snap_state.blocker = NULL;
505 + blk_unref(snap_state.target);
506 + snap_state.target = NULL;
512 +static void save_snapshot_error(const char *fmt, ...)
518 + msg = g_strdup_vprintf(fmt, ap);
521 + DPRINTF("save_snapshot_error: %s\n", msg);
523 + if (!snap_state.error) {
524 + error_set(&snap_state.error, ERROR_CLASS_GENERIC_ERROR, "%s", msg);
529 + snap_state.state = SAVE_STATE_ERROR;
531 + save_snapshot_cleanup();
534 +static void save_snapshot_completed(void)
536 + DPRINTF("save_snapshot_completed\n");
538 + if (save_snapshot_cleanup() < 0) {
539 + snap_state.state = SAVE_STATE_ERROR;
541 + snap_state.state = SAVE_STATE_COMPLETED;
545 +static int block_state_close(void *opaque)
547 + snap_state.file = NULL;
548 + return blk_flush(snap_state.target);
551 +static ssize_t block_state_writev_buffer(void *opaque, struct iovec *iov,
552 + int iovcnt, int64_t pos)
557 + qemu_iovec_init_external(&qiov, iov, iovcnt);
558 + ret = blk_co_pwritev(snap_state.target, pos, qiov.size, &qiov, 0);
562 + snap_state.bs_pos += qiov.size;
566 +static int store_and_stop(void) {
567 + if (global_state_store()) {
568 + save_snapshot_error("Error saving global state");
571 + if (runstate_is_running()) {
572 + vm_stop(RUN_STATE_SAVE_VM);
577 +static void process_savevm_co(void *opaque)
581 + MigrationParams params = {
586 + snap_state.state = SAVE_STATE_ACTIVE;
588 + qemu_mutex_unlock_iothread();
589 + qemu_savevm_state_header(snap_state.file);
590 + ret = qemu_savevm_state_begin(snap_state.file, ¶ms);
591 + qemu_mutex_lock_iothread();
594 + save_snapshot_error("qemu_savevm_state_begin failed");
598 + while (snap_state.state == SAVE_STATE_ACTIVE) {
599 + uint64_t pending_size, pend_post, pend_nonpost;
601 + qemu_savevm_state_pending(snap_state.file, 0, &pend_nonpost, &pend_post);
602 + pending_size = pend_post + pend_nonpost;
604 + if (pending_size) {
605 + ret = qemu_savevm_state_iterate(snap_state.file, false);
607 + save_snapshot_error("qemu_savevm_state_iterate error %d", ret);
610 + DPRINTF("savevm inerate pending size %lu ret %d\n", pending_size, ret);
612 + DPRINTF("done iterating\n");
613 + if (store_and_stop())
615 + DPRINTF("savevm inerate finished\n");
616 + qemu_savevm_state_complete_precopy(snap_state.file, false);
617 + DPRINTF("save complete\n");
618 + save_snapshot_completed();
622 + /* stop the VM if we get to the end of available space,
623 + * or if pending_size is just a few MB
625 + maxlen = blk_getlength(snap_state.target) - 30*1024*1024;
626 + if ((pending_size < 100000) ||
627 + ((snap_state.bs_pos + pending_size) >= maxlen)) {
628 + if (store_and_stop())
633 + if(snap_state.state == SAVE_STATE_CANCELLED) {
634 + save_snapshot_completed();
635 + Error *errp = NULL;
636 + qmp_savevm_end(&errp);
641 +static const QEMUFileOps block_file_ops = {
642 + .writev_buffer = block_state_writev_buffer,
643 + .close = block_state_close,
647 +void qmp_savevm_start(bool has_statefile, const char *statefile, Error **errp)
649 + Error *local_err = NULL;
651 + int bdrv_oflags = BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_NO_FLUSH;
653 + if (snap_state.state != SAVE_STATE_DONE) {
654 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
655 + "VM snapshot already started\n");
659 + /* initialize snapshot info */
660 + snap_state.saved_vm_running = runstate_is_running();
661 + snap_state.bs_pos = 0;
662 + snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
663 + snap_state.blocker = NULL;
665 + if (snap_state.error) {
666 + error_free(snap_state.error);
667 + snap_state.error = NULL;
670 + if (!has_statefile) {
671 + vm_stop(RUN_STATE_SAVE_VM);
672 + snap_state.state = SAVE_STATE_COMPLETED;
676 + if (qemu_savevm_state_blocked(errp)) {
680 + /* Open the image */
681 + QDict *options = NULL;
682 + options = qdict_new();
683 + qdict_put(options, "driver", qstring_from_str("raw"));
684 + snap_state.target = blk_new_open(statefile, NULL, options, bdrv_oflags, &local_err);
685 + if (!snap_state.target) {
686 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
690 + snap_state.file = qemu_fopen_ops(&snap_state, &block_file_ops);
692 + if (!snap_state.file) {
693 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
698 + error_setg(&snap_state.blocker, "block device is in use by savevm");
699 + blk_op_block_all(snap_state.target, snap_state.blocker);
701 + Coroutine *co = qemu_coroutine_create(process_savevm_co, NULL);
702 + qemu_coroutine_enter(co);
708 + save_snapshot_error("setup failed");
710 + if (snap_state.saved_vm_running) {
715 +void qmp_savevm_end(Error **errp)
717 + if (snap_state.state == SAVE_STATE_DONE) {
718 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
719 + "VM snapshot not started\n");
723 + if (snap_state.state == SAVE_STATE_ACTIVE) {
724 + snap_state.state = SAVE_STATE_CANCELLED;
728 + if (snap_state.saved_vm_running) {
732 + snap_state.state = SAVE_STATE_DONE;
735 +void qmp_snapshot_drive(const char *device, const char *name, Error **errp)
738 + BlockDriverState *bs;
739 + QEMUSnapshotInfo sn1, *sn = &sn1;
747 + if (snap_state.state != SAVE_STATE_COMPLETED) {
748 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
749 + "VM snapshot not ready/started\n");
753 + blk = blk_by_name(device);
755 + error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
756 + "Device '%s' not found", device);
761 + if (!bdrv_is_inserted(bs)) {
762 + error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
766 + if (bdrv_is_read_only(bs)) {
767 + error_setg(errp, "Node '%s' is read only", device);
771 + if (!bdrv_can_snapshot(bs)) {
772 + error_setg(errp, QERR_UNSUPPORTED);
776 + if (bdrv_snapshot_find(bs, sn, name) >= 0) {
777 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
778 + "snapshot '%s' already exists", name);
783 + memset(sn, 0, sizeof(*sn));
787 + sn->date_sec = tb.time;
788 + sn->date_nsec = tb.millitm * 1000000;
790 + gettimeofday(&tv, NULL);
791 + sn->date_sec = tv.tv_sec;
792 + sn->date_nsec = tv.tv_usec * 1000;
794 + sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
796 + pstrcpy(sn->name, sizeof(sn->name), name);
798 + sn->vm_state_size = 0; /* do not save state */
800 + ret = bdrv_snapshot_create(bs, sn);
802 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
803 + "Error while creating snapshot on '%s'\n", device);
808 +void qmp_delete_drive_snapshot(const char *device, const char *name,
812 + BlockDriverState *bs;
813 + QEMUSnapshotInfo sn1, *sn = &sn1;
814 + Error *local_err = NULL;
818 + blk = blk_by_name(device);
820 + error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
821 + "Device '%s' not found", device);
826 + if (bdrv_is_read_only(bs)) {
827 + error_setg(errp, "Node '%s' is read only", device);
831 + if (!bdrv_can_snapshot(bs)) {
832 + error_setg(errp, QERR_UNSUPPORTED);
836 + if (bdrv_snapshot_find(bs, sn, name) < 0) {
837 + /* return success if snapshot does not exists */
841 + ret = bdrv_snapshot_delete(bs, NULL, name, &local_err);
843 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
844 + "Error while deleting snapshot on '%s'\n", device);
849 +static ssize_t loadstate_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
852 + BlockBackend *be = opaque;
853 + int64_t maxlen = blk_getlength(be);
854 + if (pos > maxlen) {
857 + if ((pos + size) > maxlen) {
858 + size = maxlen - pos - 1;
863 + return blk_pread(be, pos, buf, size);
866 +static const QEMUFileOps loadstate_file_ops = {
867 + .get_buffer = loadstate_get_buffer,
870 +int load_state_from_blockdev(const char *filename)
873 + Error *local_err = NULL;
874 + Error *blocker = NULL;
879 + be = blk_new_open(filename, NULL, NULL, 0, &local_err);
882 + error_report("Could not open VM state file");
886 + error_setg(&blocker, "block device is in use by load state");
887 + blk_op_block_all(be, blocker);
889 + /* restore the VM state */
890 + f = qemu_fopen_ops(be, &loadstate_file_ops);
892 + error_report("Could not open VM state file");
896 + qemu_system_reset(VMRESET_SILENT);
897 + ret = qemu_loadvm_state(f);
900 + migration_incoming_state_destroy();
902 + error_report("Error %d while loading VM state", ret);
910 + blk_op_unblock_all(be, blocker);
911 + error_free(blocker);
916 diff --git a/vl.c b/vl.c
917 index 46de1b9087..2132a77129 100644
920 @@ -2960,6 +2960,7 @@ int main(int argc, char **argv, char **envp)
923 const char *loadvm = NULL;
924 + const char *loadstate = NULL;
925 MachineClass *machine_class;
926 const char *cpu_model;
927 const char *vga_model = NULL;
928 @@ -3635,6 +3636,9 @@ int main(int argc, char **argv, char **envp)
929 case QEMU_OPTION_loadvm:
932 + case QEMU_OPTION_loadstate:
933 + loadstate = optarg;
935 case QEMU_OPTION_full_screen:
938 @@ -4693,6 +4697,10 @@ int main(int argc, char **argv, char **envp)
939 if (load_vmstate(loadvm) < 0) {
942 + } else if (loadstate) {
943 + if (load_state_from_blockdev(loadstate) < 0) {
948 qdev_prop_check_globals();