1 From 46fd4bb673a91d40352c95e9d3f62f63b5021053 Mon Sep 17 00:00:00 2001
2 From: Stefan Priebe <s.priebe@profihost.ag>
3 Date: Fri, 29 Nov 2013 22:17:03 +0100
4 Subject: [PATCH] internal-snapshot-async-qemu1.7.patch
9 hmp-commands.hx | 34 ++++
12 include/block/block.h | 1 +
13 include/sysemu/sysemu.h | 5 +-
15 qapi-schema.json | 46 +++++
16 qemu-options.hx | 13 ++
17 qmp-commands.hx | 31 +++
18 savevm-async.c | 478 +++++++++++++++++++++++++++++++++++++++++++++++
21 14 files changed, 692 insertions(+), 7 deletions(-)
22 create mode 100644 savevm-async.c
24 Index: new/Makefile.objs
25 ===================================================================
26 --- new.orig/Makefile.objs 2014-11-20 09:13:01.000000000 +0100
27 +++ new/Makefile.objs 2014-11-20 09:16:47.000000000 +0100
29 common-obj-y += qemu-char.o #aio.o
30 common-obj-y += page_cache.o
31 common-obj-y += qjson.o
32 +common-obj-y += savevm-async.o
34 common-obj-$(CONFIG_SPICE) += spice-qemu-char.o
37 ===================================================================
38 --- new.orig/block.c 2014-11-20 09:13:01.000000000 +0100
39 +++ new/block.c 2014-11-20 09:16:47.000000000 +0100
41 bdrv_attach_child(bs_top, bs_new, &child_backing);
44 -static void bdrv_delete(BlockDriverState *bs)
45 +void bdrv_delete(BlockDriverState *bs)
48 assert(bdrv_op_blocker_is_empty(bs));
49 Index: new/hmp-commands.hx
50 ===================================================================
51 --- new.orig/hmp-commands.hx 2014-11-20 09:13:01.000000000 +0100
52 +++ new/hmp-commands.hx 2014-11-20 09:16:47.000000000 +0100
54 show current migration parameters
55 @item info migrate_cache_size
56 show current migration XBZRLE cache size
60 show balloon information
62 @@ -1857,3 +1859,35 @@
68 + .name = "savevm-start",
69 + .args_type = "statefile:s?",
70 + .params = "[statefile]",
71 + .help = "Prepare for snapshot and halt VM. Save VM state to statefile.",
72 + .mhandler.cmd = hmp_savevm_start,
76 + .name = "snapshot-drive",
77 + .args_type = "device:s,name:s",
78 + .params = "device name",
79 + .help = "Create internal snapshot.",
80 + .mhandler.cmd = hmp_snapshot_drive,
84 + .name = "delete-drive-snapshot",
85 + .args_type = "device:s,name:s",
86 + .params = "device name",
87 + .help = "Delete internal snapshot.",
88 + .mhandler.cmd = hmp_delete_drive_snapshot,
92 + .name = "savevm-end",
95 + .help = "Resume VM after snaphot.",
96 + .mhandler.cmd = hmp_savevm_end,
99 ===================================================================
100 --- new.orig/hmp.c 2014-11-20 09:13:01.000000000 +0100
101 +++ new/hmp.c 2014-11-20 09:16:47.000000000 +0100
102 @@ -1849,6 +1849,63 @@
104 qapi_free_MemoryDeviceInfoList(info_list);
107 +void hmp_savevm_start(Monitor *mon, const QDict *qdict)
109 + Error *errp = NULL;
110 + const char *statefile = qdict_get_try_str(qdict, "statefile");
112 + qmp_savevm_start(statefile != NULL, statefile, &errp);
113 + hmp_handle_error(mon, &errp);
116 +void hmp_snapshot_drive(Monitor *mon, const QDict *qdict)
118 + Error *errp = NULL;
119 + const char *name = qdict_get_str(qdict, "name");
120 + const char *device = qdict_get_str(qdict, "device");
122 + qmp_snapshot_drive(device, name, &errp);
123 + hmp_handle_error(mon, &errp);
126 +void hmp_delete_drive_snapshot(Monitor *mon, const QDict *qdict)
128 + Error *errp = NULL;
129 + const char *name = qdict_get_str(qdict, "name");
130 + const char *device = qdict_get_str(qdict, "device");
132 + qmp_delete_drive_snapshot(device, name, &errp);
133 + hmp_handle_error(mon, &errp);
136 +void hmp_savevm_end(Monitor *mon, const QDict *qdict)
138 + Error *errp = NULL;
140 + qmp_savevm_end(&errp);
141 + hmp_handle_error(mon, &errp);
144 +void hmp_info_savevm(Monitor *mon, const QDict *qdict)
147 + info = qmp_query_savevm(NULL);
149 + if (info->has_status) {
150 + monitor_printf(mon, "savevm status: %s\n", info->status);
151 + monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
154 + monitor_printf(mon, "savevm status: not running\n");
156 + if (info->has_bytes) {
157 + monitor_printf(mon, "Bytes saved: %"PRIu64"\n", info->bytes);
159 + if (info->has_error) {
160 + monitor_printf(mon, "Error: %s\n", info->error);
164 void hmp_qom_list(Monitor *mon, const QDict *qdict)
167 ===================================================================
168 --- new.orig/hmp.h 2014-11-20 09:13:01.000000000 +0100
169 +++ new/hmp.h 2014-11-20 09:16:47.000000000 +0100
171 void hmp_info_uuid(Monitor *mon, const QDict *qdict);
172 void hmp_info_chardev(Monitor *mon, const QDict *qdict);
173 void hmp_info_mice(Monitor *mon, const QDict *qdict);
174 +void hmp_info_savevm(Monitor *mon, const QDict *qdict);
175 void hmp_info_migrate(Monitor *mon, const QDict *qdict);
176 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
177 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict);
179 void hmp_netdev_del(Monitor *mon, const QDict *qdict);
180 void hmp_getfd(Monitor *mon, const QDict *qdict);
181 void hmp_closefd(Monitor *mon, const QDict *qdict);
182 +void hmp_savevm_start(Monitor *mon, const QDict *qdict);
183 +void hmp_snapshot_drive(Monitor *mon, const QDict *qdict);
184 +void hmp_delete_drive_snapshot(Monitor *mon, const QDict *qdict);
185 +void hmp_savevm_end(Monitor *mon, const QDict *qdict);
186 void hmp_sendkey(Monitor *mon, const QDict *qdict);
187 void hmp_screendump(Monitor *mon, const QDict *qdict);
188 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict);
189 Index: new/include/block/block.h
190 ===================================================================
191 --- new.orig/include/block/block.h 2014-11-20 09:13:01.000000000 +0100
192 +++ new/include/block/block.h 2014-11-20 09:16:47.000000000 +0100
194 int bdrv_get_backing_file_depth(BlockDriverState *bs);
195 void bdrv_refresh_filename(BlockDriverState *bs);
196 int bdrv_truncate(BlockDriverState *bs, int64_t offset);
197 +void bdrv_delete(BlockDriverState *bs);
198 int64_t bdrv_nb_sectors(BlockDriverState *bs);
199 int64_t bdrv_getlength(BlockDriverState *bs);
200 int64_t bdrv_get_allocated_file_size(BlockDriverState *bs);
201 Index: new/include/sysemu/sysemu.h
202 ===================================================================
203 --- new.orig/include/sysemu/sysemu.h 2014-11-20 09:13:01.000000000 +0100
204 +++ new/include/sysemu/sysemu.h 2014-11-20 09:16:47.000000000 +0100
207 void hmp_savevm(Monitor *mon, const QDict *qdict);
208 int load_vmstate(const char *name);
209 +int load_state_from_blockdev(const char *filename);
210 void hmp_delvm(Monitor *mon, const QDict *qdict);
211 void hmp_info_snapshots(Monitor *mon, const QDict *qdict);
213 void qemu_announce_self(void);
215 bool qemu_savevm_state_blocked(Error **errp);
216 -void qemu_savevm_state_begin(QEMUFile *f,
217 +int qemu_savevm_state_begin(QEMUFile *f,
218 const MigrationParams *params);
219 void qemu_savevm_state_header(QEMUFile *f);
220 int qemu_savevm_state_iterate(QEMUFile *f);
221 -void qemu_savevm_state_complete(QEMUFile *f);
222 +int qemu_savevm_state_complete(QEMUFile *f);
223 void qemu_savevm_state_cancel(void);
224 uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size);
225 int qemu_loadvm_state(QEMUFile *f);
227 ===================================================================
228 --- new.orig/monitor.c 2014-11-20 09:13:01.000000000 +0100
229 +++ new/monitor.c 2014-11-20 09:16:47.000000000 +0100
230 @@ -2876,6 +2876,13 @@
231 .mhandler.cmd = hmp_info_migrate_cache_size,
237 + .help = "show savevm status",
238 + .mhandler.cmd = hmp_info_savevm,
244 Index: new/qapi-schema.json
245 ===================================================================
246 --- new.orig/qapi-schema.json 2014-11-20 09:13:01.000000000 +0100
247 +++ new/qapi-schema.json 2014-11-20 09:16:47.000000000 +0100
250 '*setup-time': 'int'} }
255 +# Information about current migration process.
257 +# @status: #optional string describing the current savevm status.
258 +# This can be 'active', 'completed', 'failed'.
259 +# If this field is not returned, no savevm process
260 +# has been initiated
262 +# @error: #optional string containing error message is status is failed.
264 +# @total-time: #optional total amount of milliseconds since savevm started.
265 +# If savevm has ended, it returns the total save time
267 +# @bytes: #optional total amount of data transfered
271 +{ 'struct': 'SaveVMInfo',
272 + 'data': {'*status': 'str', '*error': 'str',
273 + '*total-time': 'int', '*bytes': 'int'} }
278 +# Returns information about current savevm process.
280 +# Returns: @SaveVMInfo
284 +{ 'command': 'query-savevm', 'returns': 'SaveVMInfo' }
291 @@ -2649,8 +2685,18 @@
296 { 'command': 'query-target', 'returns': 'TargetInfo' }
298 +{ 'command': 'savevm-start', 'data': { '*statefile': 'str' } }
300 +{ 'command': 'snapshot-drive', 'data': { 'device': 'str', 'name': 'str' } }
302 +{ 'command': 'delete-drive-snapshot', 'data': { 'device': 'str', 'name': 'str' } }
304 +{ 'command': 'savevm-end' }
310 Index: new/qemu-options.hx
311 ===================================================================
312 --- new.orig/qemu-options.hx 2014-11-20 09:13:01.000000000 +0100
313 +++ new/qemu-options.hx 2014-11-20 09:16:47.000000000 +0100
314 @@ -2975,6 +2975,19 @@
315 Start right away with a saved state (@code{loadvm} in monitor)
318 +DEF("loadstate", HAS_ARG, QEMU_OPTION_loadstate, \
319 + "-loadstate file\n" \
320 + " start right away with a saved state\n",
323 +@item -loadstate @var{file}
325 +Start right away with a saved state. This option does not rollback
326 +disk state like @code{loadvm}, so user must make sure that disk
327 +have correct state. @var{file} can be any valid device URL. See the section
328 +for "Device URL Syntax" for more information.
332 DEF("daemonize", 0, QEMU_OPTION_daemonize, \
333 "-daemonize daemonize QEMU after initializing\n", QEMU_ARCH_ALL)
334 Index: new/qmp-commands.hx
335 ===================================================================
336 --- new.orig/qmp-commands.hx 2014-11-20 09:13:01.000000000 +0100
337 +++ new/qmp-commands.hx 2014-11-20 09:16:47.000000000 +0100
338 @@ -4200,6 +4200,36 @@
344 + .name = "savevm-start",
345 + .args_type = "statefile:s?",
346 + .mhandler.cmd_new = qmp_marshal_input_savevm_start,
350 + .name = "snapshot-drive",
351 + .args_type = "device:s,name:s",
352 + .mhandler.cmd_new = qmp_marshal_input_snapshot_drive,
356 + .name = "delete-drive-snapshot",
357 + .args_type = "device:s,name:s",
358 + .mhandler.cmd_new = qmp_marshal_input_delete_drive_snapshot,
362 + .name = "savevm-end",
364 + .mhandler.cmd_new = qmp_marshal_input_savevm_end,
368 + .name = "query-savevm",
370 + .mhandler.cmd_new = qmp_marshal_input_query_savevm,
374 .name = "query-rocker",
375 Index: new/savevm-async.c
376 ===================================================================
377 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
378 +++ new/savevm-async.c 2014-11-20 09:17:48.000000000 +0100
380 +#include "qemu-common.h"
381 +#include "qapi/qmp/qerror.h"
382 +#include "qemu/error-report.h"
383 +#include "sysemu/sysemu.h"
384 +#include "qmp-commands.h"
385 +#include "qemu-options.h"
386 +#include "migration/qemu-file.h"
387 +#include "qom/qom-qobject.h"
388 +#include "migration/migration.h"
389 +#include "block/snapshot.h"
390 +#include "block/qapi.h"
391 +#include "block/block.h"
392 +#include "qemu/timer.h"
393 +#include "sysemu/block-backend.h"
394 +#include "qapi/qmp/qstring.h"
396 +/* #define DEBUG_SAVEVM_STATE */
398 +#ifdef DEBUG_SAVEVM_STATE
399 +#define DPRINTF(fmt, ...) \
400 + do { printf("savevm-async: " fmt, ## __VA_ARGS__); } while (0)
402 +#define DPRINTF(fmt, ...) \
410 + SAVE_STATE_COMPLETED,
411 + SAVE_STATE_CANCELLED
415 +static struct SnapshotState {
416 + BlockDriverState *bs;
421 + int saved_vm_running;
423 + int64_t total_time;
426 +SaveVMInfo *qmp_query_savevm(Error **errp)
428 + SaveVMInfo *info = g_malloc0(sizeof(*info));
429 + struct SnapshotState *s = &snap_state;
431 + if (s->state != SAVE_STATE_DONE) {
432 + info->has_bytes = true;
433 + info->bytes = s->bs_pos;
434 + switch (s->state) {
435 + case SAVE_STATE_ERROR:
436 + info->has_status = true;
437 + info->status = g_strdup("failed");
438 + info->has_total_time = true;
439 + info->total_time = s->total_time;
441 + info->has_error = true;
442 + info->error = g_strdup(error_get_pretty(s->error));
445 + case SAVE_STATE_ACTIVE:
446 + info->has_status = true;
447 + info->status = g_strdup("active");
448 + info->has_total_time = true;
449 + info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
452 + case SAVE_STATE_COMPLETED:
453 + info->has_status = true;
454 + info->status = g_strdup("completed");
455 + info->has_total_time = true;
456 + info->total_time = s->total_time;
464 +static int save_snapshot_cleanup(void)
468 + DPRINTF("save_snapshot_cleanup\n");
470 + snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
471 + snap_state.total_time;
473 + if (snap_state.file) {
474 + ret = qemu_fclose(snap_state.file);
477 + if (snap_state.bs) {
478 + /* try to truncate, but ignore errors (will fail on block devices).
479 + * note: bdrv_read() need whole blocks, so we round up
481 + size_t size = (snap_state.bs_pos + BDRV_SECTOR_SIZE) & BDRV_SECTOR_MASK;
482 + bdrv_truncate(snap_state.bs, size);
483 + bdrv_op_unblock_all(snap_state.bs, snap_state.blocker);
484 + error_free(snap_state.blocker);
485 + snap_state.blocker = NULL;
486 + bdrv_unref(snap_state.bs);
487 + snap_state.bs = NULL;
493 +static void save_snapshot_error(const char *fmt, ...)
499 + msg = g_strdup_vprintf(fmt, ap);
502 + DPRINTF("save_snapshot_error: %s\n", msg);
504 + if (!snap_state.error) {
505 + error_set(&snap_state.error, ERROR_CLASS_GENERIC_ERROR, "%s", msg);
510 + snap_state.state = SAVE_STATE_ERROR;
512 + save_snapshot_cleanup();
515 +static void save_snapshot_completed(void)
517 + DPRINTF("save_snapshot_completed\n");
519 + if (save_snapshot_cleanup() < 0) {
520 + snap_state.state = SAVE_STATE_ERROR;
522 + snap_state.state = SAVE_STATE_COMPLETED;
526 +static int block_state_close(void *opaque)
528 + snap_state.file = NULL;
529 + return bdrv_flush(snap_state.bs);
532 +static int block_state_put_buffer(void *opaque, const uint8_t *buf,
533 + int64_t pos, int size)
537 + assert(pos == snap_state.bs_pos);
539 + if ((ret = bdrv_pwrite(snap_state.bs, snap_state.bs_pos, buf, size)) > 0) {
540 + snap_state.bs_pos += ret;
546 +static int store_and_stop(void) {
547 + if (global_state_store()) {
548 + save_snapshot_error("Error saving global state");
551 + if (runstate_is_running()) {
552 + vm_stop(RUN_STATE_SAVE_VM);
557 +static void process_savevm_co(void *opaque)
561 + MigrationParams params = {
566 + snap_state.state = SAVE_STATE_ACTIVE;
568 + qemu_mutex_unlock_iothread();
569 + qemu_savevm_state_header(snap_state.file);
570 + ret = qemu_savevm_state_begin(snap_state.file, ¶ms);
571 + qemu_mutex_lock_iothread();
574 + save_snapshot_error("qemu_savevm_state_begin failed");
578 + while (snap_state.state == SAVE_STATE_ACTIVE) {
579 + uint64_t pending_size;
581 + pending_size = qemu_savevm_state_pending(snap_state.file, 0);
583 + if (pending_size) {
584 + ret = qemu_savevm_state_iterate(snap_state.file);
586 + save_snapshot_error("qemu_savevm_state_iterate error %d", ret);
589 + DPRINTF("savevm inerate pending size %lu ret %d\n", pending_size, ret);
591 + DPRINTF("done iterating\n");
592 + if (store_and_stop())
594 + DPRINTF("savevm inerate finished\n");
595 + qemu_savevm_state_complete(snap_state.file);
596 + DPRINTF("save complete\n");
597 + save_snapshot_completed();
601 + /* stop the VM if we get to the end of available space,
602 + * or if pending_size is just a few MB
604 + maxlen = bdrv_getlength(snap_state.bs) - 30*1024*1024;
605 + if ((pending_size < 100000) ||
606 + ((snap_state.bs_pos + pending_size) >= maxlen)) {
607 + if (store_and_stop())
612 + if(snap_state.state == SAVE_STATE_CANCELLED) {
613 + save_snapshot_completed();
614 + Error *errp = NULL;
615 + qmp_savevm_end(&errp);
620 +static const QEMUFileOps block_file_ops = {
621 + .put_buffer = block_state_put_buffer,
622 + .close = block_state_close,
626 +void qmp_savevm_start(bool has_statefile, const char *statefile, Error **errp)
628 + BlockDriver *drv = NULL;
629 + Error *local_err = NULL;
631 + int bdrv_oflags = BDRV_O_CACHE_WB | BDRV_O_RDWR;
634 + if (snap_state.state != SAVE_STATE_DONE) {
635 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
636 + "VM snapshot already started\n");
640 + /* initialize snapshot info */
641 + snap_state.saved_vm_running = runstate_is_running();
642 + snap_state.bs_pos = 0;
643 + snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
644 + snap_state.blocker = NULL;
646 + if (snap_state.error) {
647 + error_free(snap_state.error);
648 + snap_state.error = NULL;
651 + if (!has_statefile) {
652 + vm_stop(RUN_STATE_SAVE_VM);
653 + snap_state.state = SAVE_STATE_COMPLETED;
657 + if (qemu_savevm_state_blocked(errp)) {
661 + /* Open the image */
662 + snap_state.bs = bdrv_new();
664 + QDict *options = NULL;
665 + options = qdict_new();
666 + qdict_put(options, "driver", qstring_from_str("raw"));
667 + ret = bdrv_open(&snap_state.bs, statefile, NULL, options, bdrv_oflags, drv, &local_err);
669 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
673 + snap_state.file = qemu_fopen_ops(&snap_state, &block_file_ops);
675 + if (!snap_state.file) {
676 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
681 + error_setg(&snap_state.blocker, "block device is in use by savevm");
682 + bdrv_op_block_all(snap_state.bs, snap_state.blocker);
684 + Coroutine *co = qemu_coroutine_create(process_savevm_co);
685 + qemu_coroutine_enter(co, NULL);
691 + save_snapshot_error("setup failed");
693 + if (snap_state.saved_vm_running) {
698 +void qmp_savevm_end(Error **errp)
700 + if (snap_state.state == SAVE_STATE_DONE) {
701 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
702 + "VM snapshot not started\n");
706 + if (snap_state.state == SAVE_STATE_ACTIVE) {
707 + snap_state.state = SAVE_STATE_CANCELLED;
711 + if (snap_state.saved_vm_running) {
715 + snap_state.state = SAVE_STATE_DONE;
718 +void qmp_snapshot_drive(const char *device, const char *name, Error **errp)
721 + BlockDriverState *bs;
722 + QEMUSnapshotInfo sn1, *sn = &sn1;
730 + if (snap_state.state != SAVE_STATE_COMPLETED) {
731 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
732 + "VM snapshot not ready/started\n");
736 + blk = blk_by_name(device);
738 + error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
739 + "Device '%s' not found", device);
744 + if (!bdrv_is_inserted(bs)) {
745 + error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
749 + if (bdrv_is_read_only(bs)) {
750 + error_setg(errp, "Node '%s' is read only", device);
754 + if (!bdrv_can_snapshot(bs)) {
755 + error_setg(errp, QERR_UNSUPPORTED);
759 + if (bdrv_snapshot_find(bs, sn, name) >= 0) {
760 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
761 + "snapshot '%s' already exists", name);
766 + memset(sn, 0, sizeof(*sn));
770 + sn->date_sec = tb.time;
771 + sn->date_nsec = tb.millitm * 1000000;
773 + gettimeofday(&tv, NULL);
774 + sn->date_sec = tv.tv_sec;
775 + sn->date_nsec = tv.tv_usec * 1000;
777 + sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
779 + pstrcpy(sn->name, sizeof(sn->name), name);
781 + sn->vm_state_size = 0; /* do not save state */
783 + ret = bdrv_snapshot_create(bs, sn);
785 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
786 + "Error while creating snapshot on '%s'\n", device);
791 +void qmp_delete_drive_snapshot(const char *device, const char *name,
795 + BlockDriverState *bs;
796 + QEMUSnapshotInfo sn1, *sn = &sn1;
797 + Error *local_err = NULL;
801 + blk = blk_by_name(device);
803 + error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
804 + "Device '%s' not found", device);
809 + if (bdrv_is_read_only(bs)) {
810 + error_setg(errp, "Node '%s' is read only", device);
814 + if (!bdrv_can_snapshot(bs)) {
815 + error_setg(errp, QERR_UNSUPPORTED);
819 + if (bdrv_snapshot_find(bs, sn, name) < 0) {
820 + /* return success if snapshot does not exists */
824 + ret = bdrv_snapshot_delete(bs, NULL, name, &local_err);
826 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
827 + "Error while deleting snapshot on '%s'\n", device);
832 +static int loadstate_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
835 + BlockDriverState *bs = (BlockDriverState *)opaque;
836 + int64_t maxlen = bdrv_getlength(bs);
837 + if (pos > maxlen) {
840 + if ((pos + size) > maxlen) {
841 + size = maxlen - pos - 1;
846 + return bdrv_pread(bs, pos, buf, size);
849 +static const QEMUFileOps loadstate_file_ops = {
850 + .get_buffer = loadstate_get_buffer,
853 +int load_state_from_blockdev(const char *filename)
855 + BlockDriverState *bs = NULL;
856 + BlockDriver *drv = NULL;
857 + Error *local_err = NULL;
858 + Error *blocker = NULL;
864 + ret = bdrv_open(&bs, filename, NULL, NULL, BDRV_O_CACHE_WB, drv, &local_err);
865 + error_setg(&blocker, "block device is in use by load state");
866 + bdrv_op_block_all(bs, blocker);
869 + error_report("Could not open VM state file");
873 + /* restore the VM state */
874 + f = qemu_fopen_ops(bs, &loadstate_file_ops);
876 + error_report("Could not open VM state file");
881 + qemu_system_reset(VMRESET_SILENT);
882 + migration_incoming_state_new(f);
883 + ret = qemu_loadvm_state(f);
886 + migration_incoming_state_destroy();
888 + error_report("Error %d while loading VM state", ret);
896 + bdrv_op_unblock_all(bs, blocker);
897 + error_free(blocker);
903 ===================================================================
904 --- new.orig/migration/savevm.c 2014-11-20 09:13:01.000000000 +0100
905 +++ new/migration/savevm.c 2014-11-20 09:16:47.000000000 +0100
906 @@ -718,11 +718,11 @@
907 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
910 -void qemu_savevm_state_begin(QEMUFile *f,
911 +int qemu_savevm_state_begin(QEMUFile *f,
912 const MigrationParams *params)
918 trace_savevm_state_begin();
919 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
929 return !machine->suppress_vmdesc;
932 -void qemu_savevm_state_complete(QEMUFile *f)
933 +int qemu_savevm_state_complete(QEMUFile *f)
938 save_section_footer(f, se);
940 qemu_file_set_error(f, ret);
947 object_unref(OBJECT(vmdesc));
950 + return qemu_file_get_error(f);
953 uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
955 ===================================================================
956 --- new.orig/vl.c 2014-11-20 09:13:01.000000000 +0100
957 +++ new/vl.c 2014-11-20 09:16:47.000000000 +0100
958 @@ -2760,6 +2760,7 @@
961 const char *loadvm = NULL;
962 + const char *loadstate = NULL;
963 MachineClass *machine_class;
964 const char *cpu_model;
965 const char *vga_model = NULL;
966 @@ -3457,6 +3458,9 @@
967 case QEMU_OPTION_loadvm:
970 + case QEMU_OPTION_loadstate:
971 + loadstate = optarg;
973 case QEMU_OPTION_full_screen:
976 @@ -4428,6 +4432,10 @@
977 if (load_vmstate(loadvm) < 0) {
980 + } else if (loadstate) {
981 + if (load_state_from_blockdev(loadstate) < 0) {
986 qdev_prop_check_globals();