1 From 3c93adeb6d594c56619fd34dc345dffa3139c2cb 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 22/41] internal snapshot async
9 hmp-commands-info.hx | 13 ++
10 hmp-commands.hx | 32 +++
13 include/block/block.h | 1 +
14 include/sysemu/sysemu.h | 5 +-
15 migration/savevm.c | 12 +-
16 qapi-schema.json | 46 +++++
17 qemu-options.hx | 13 ++
18 qmp-commands.hx | 30 +++
19 savevm-async.c | 526 ++++++++++++++++++++++++++++++++++++++++++++++++
21 14 files changed, 743 insertions(+), 8 deletions(-)
22 create mode 100644 savevm-async.c
24 diff --git a/Makefile.objs b/Makefile.objs
25 index 845edd0..7d9d2d7 100644
28 @@ -53,6 +53,7 @@ common-obj-$(CONFIG_LINUX) += fsdev/
29 common-obj-y += migration/
30 common-obj-y += qemu-char.o #aio.o
31 common-obj-y += page_cache.o
32 +common-obj-y += savevm-async.o
34 common-obj-$(CONFIG_SPICE) += spice-qemu-char.o
36 diff --git a/block.c b/block.c
37 index 30d64e6..95c1d32 100644
40 @@ -2288,7 +2288,7 @@ void bdrv_replace_in_backing_chain(BlockDriverState *old, BlockDriverState *new)
44 -static void bdrv_delete(BlockDriverState *bs)
45 +void bdrv_delete(BlockDriverState *bs)
48 assert(bdrv_op_blocker_is_empty(bs));
49 diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
50 index 7616fe2..3046f9d 100644
51 --- a/hmp-commands-info.hx
52 +++ b/hmp-commands-info.hx
53 @@ -588,6 +588,19 @@ Show current migration xbzrle cache size.
60 + .help = "show savevm status",
61 + .mhandler.cmd = hmp_info_savevm,
73 diff --git a/hmp-commands.hx b/hmp-commands.hx
74 index 0e20ef9..4d735cb 100644
77 @@ -1791,3 +1791,35 @@ ETEXI
83 + .name = "savevm-start",
84 + .args_type = "statefile:s?",
85 + .params = "[statefile]",
86 + .help = "Prepare for snapshot and halt VM. Save VM state to statefile.",
87 + .mhandler.cmd = hmp_savevm_start,
91 + .name = "snapshot-drive",
92 + .args_type = "device:s,name:s",
93 + .params = "device name",
94 + .help = "Create internal snapshot.",
95 + .mhandler.cmd = hmp_snapshot_drive,
99 + .name = "delete-drive-snapshot",
100 + .args_type = "device:s,name:s",
101 + .params = "device name",
102 + .help = "Delete internal snapshot.",
103 + .mhandler.cmd = hmp_delete_drive_snapshot,
107 + .name = "savevm-end",
110 + .help = "Resume VM after snaphot.",
111 + .mhandler.cmd = hmp_savevm_end,
113 diff --git a/hmp.c b/hmp.c
114 index c292354..7bd319f 100644
117 @@ -2116,6 +2116,63 @@ void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
118 qapi_free_MemoryDeviceInfoList(info_list);
121 +void hmp_savevm_start(Monitor *mon, const QDict *qdict)
123 + Error *errp = NULL;
124 + const char *statefile = qdict_get_try_str(qdict, "statefile");
126 + qmp_savevm_start(statefile != NULL, statefile, &errp);
127 + hmp_handle_error(mon, &errp);
130 +void hmp_snapshot_drive(Monitor *mon, const QDict *qdict)
132 + Error *errp = NULL;
133 + const char *name = qdict_get_str(qdict, "name");
134 + const char *device = qdict_get_str(qdict, "device");
136 + qmp_snapshot_drive(device, name, &errp);
137 + hmp_handle_error(mon, &errp);
140 +void hmp_delete_drive_snapshot(Monitor *mon, const QDict *qdict)
142 + Error *errp = NULL;
143 + const char *name = qdict_get_str(qdict, "name");
144 + const char *device = qdict_get_str(qdict, "device");
146 + qmp_delete_drive_snapshot(device, name, &errp);
147 + hmp_handle_error(mon, &errp);
150 +void hmp_savevm_end(Monitor *mon, const QDict *qdict)
152 + Error *errp = NULL;
154 + qmp_savevm_end(&errp);
155 + hmp_handle_error(mon, &errp);
158 +void hmp_info_savevm(Monitor *mon, const QDict *qdict)
161 + info = qmp_query_savevm(NULL);
163 + if (info->has_status) {
164 + monitor_printf(mon, "savevm status: %s\n", info->status);
165 + monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
168 + monitor_printf(mon, "savevm status: not running\n");
170 + if (info->has_bytes) {
171 + monitor_printf(mon, "Bytes saved: %"PRIu64"\n", info->bytes);
173 + if (info->has_error) {
174 + monitor_printf(mon, "Error: %s\n", info->error);
178 void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
180 IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
181 diff --git a/hmp.h b/hmp.h
182 index 9a4c1f6..b74ddbf 100644
185 @@ -26,6 +26,7 @@ void hmp_info_status(Monitor *mon, const QDict *qdict);
186 void hmp_info_uuid(Monitor *mon, const QDict *qdict);
187 void hmp_info_chardev(Monitor *mon, const QDict *qdict);
188 void hmp_info_mice(Monitor *mon, const QDict *qdict);
189 +void hmp_info_savevm(Monitor *mon, const QDict *qdict);
190 void hmp_info_migrate(Monitor *mon, const QDict *qdict);
191 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
192 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict);
193 @@ -92,6 +93,10 @@ void hmp_netdev_add(Monitor *mon, const QDict *qdict);
194 void hmp_netdev_del(Monitor *mon, const QDict *qdict);
195 void hmp_getfd(Monitor *mon, const QDict *qdict);
196 void hmp_closefd(Monitor *mon, const QDict *qdict);
197 +void hmp_savevm_start(Monitor *mon, const QDict *qdict);
198 +void hmp_snapshot_drive(Monitor *mon, const QDict *qdict);
199 +void hmp_delete_drive_snapshot(Monitor *mon, const QDict *qdict);
200 +void hmp_savevm_end(Monitor *mon, const QDict *qdict);
201 void hmp_sendkey(Monitor *mon, const QDict *qdict);
202 void hmp_screendump(Monitor *mon, const QDict *qdict);
203 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict);
204 diff --git a/include/block/block.h b/include/block/block.h
205 index 11c162d..6822b91 100644
206 --- a/include/block/block.h
207 +++ b/include/block/block.h
208 @@ -256,6 +256,7 @@ BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
209 int bdrv_get_backing_file_depth(BlockDriverState *bs);
210 void bdrv_refresh_filename(BlockDriverState *bs);
211 int bdrv_truncate(BlockDriverState *bs, int64_t offset);
212 +void bdrv_delete(BlockDriverState *bs);
213 int64_t bdrv_nb_sectors(BlockDriverState *bs);
214 int64_t bdrv_getlength(BlockDriverState *bs);
215 int64_t bdrv_get_allocated_file_size(BlockDriverState *bs);
216 diff --git a/include/sysemu/sysemu.h b/include/sysemu/sysemu.h
217 index ee7c760..4875441 100644
218 --- a/include/sysemu/sysemu.h
219 +++ b/include/sysemu/sysemu.h
220 @@ -79,6 +79,7 @@ void qemu_remove_machine_init_done_notifier(Notifier *notify);
222 void hmp_savevm(Monitor *mon, const QDict *qdict);
223 int load_vmstate(const char *name);
224 +int load_state_from_blockdev(const char *filename);
225 void hmp_delvm(Monitor *mon, const QDict *qdict);
226 void hmp_info_snapshots(Monitor *mon, const QDict *qdict);
228 @@ -106,13 +107,13 @@ enum qemu_vm_cmd {
229 #define MAX_VM_CMD_PACKAGED_SIZE (1ul << 24)
231 bool qemu_savevm_state_blocked(Error **errp);
232 -void qemu_savevm_state_begin(QEMUFile *f,
233 +int qemu_savevm_state_begin(QEMUFile *f,
234 const MigrationParams *params);
235 void qemu_savevm_state_header(QEMUFile *f);
236 int qemu_savevm_state_iterate(QEMUFile *f, bool postcopy);
237 void qemu_savevm_state_cleanup(void);
238 void qemu_savevm_state_complete_postcopy(QEMUFile *f);
239 -void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only);
240 +int qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only);
241 void qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size,
242 uint64_t *res_non_postcopiable,
243 uint64_t *res_postcopiable);
244 diff --git a/migration/savevm.c b/migration/savevm.c
245 index 33a2911..b1bdfb6 100644
246 --- a/migration/savevm.c
247 +++ b/migration/savevm.c
248 @@ -879,11 +879,11 @@ void qemu_savevm_state_header(QEMUFile *f)
252 -void qemu_savevm_state_begin(QEMUFile *f,
253 +int qemu_savevm_state_begin(QEMUFile *f,
254 const MigrationParams *params)
260 trace_savevm_state_begin();
261 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
262 @@ -911,6 +911,7 @@ void qemu_savevm_state_begin(QEMUFile *f,
270 @@ -1014,7 +1015,7 @@ void qemu_savevm_state_complete_postcopy(QEMUFile *f)
274 -void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
275 +int qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
279 @@ -1048,12 +1049,12 @@ void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
280 save_section_footer(f, se);
282 qemu_file_set_error(f, ret);
293 vmdesc = qjson_new();
294 @@ -1100,6 +1101,7 @@ void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
295 qjson_destroy(vmdesc);
298 + return qemu_file_get_error(f);
301 /* Give an estimate of the amount left to be transferred,
302 diff --git a/qapi-schema.json b/qapi-schema.json
303 index 147137d..0c0faf7 100644
304 --- a/qapi-schema.json
305 +++ b/qapi-schema.json
307 '*cpu-throttle-percentage': 'int',
308 '*error-desc': 'str'} }
313 +# Information about current migration process.
315 +# @status: #optional string describing the current savevm status.
316 +# This can be 'active', 'completed', 'failed'.
317 +# If this field is not returned, no savevm process
318 +# has been initiated
320 +# @error: #optional string containing error message is status is failed.
322 +# @total-time: #optional total amount of milliseconds since savevm started.
323 +# If savevm has ended, it returns the total save time
325 +# @bytes: #optional total amount of data transfered
329 +{ 'struct': 'SaveVMInfo',
330 + 'data': {'*status': 'str', '*error': 'str',
331 + '*total-time': 'int', '*bytes': 'int'} }
336 +# Returns information about current savevm process.
338 +# Returns: @SaveVMInfo
342 +{ 'command': 'query-savevm', 'returns': 'SaveVMInfo' }
349 @@ -3286,8 +3322,18 @@
354 { 'command': 'query-target', 'returns': 'TargetInfo' }
356 +{ 'command': 'savevm-start', 'data': { '*statefile': 'str' } }
358 +{ 'command': 'snapshot-drive', 'data': { 'device': 'str', 'name': 'str' } }
360 +{ 'command': 'delete-drive-snapshot', 'data': { 'device': 'str', 'name': 'str' } }
362 +{ 'command': 'savevm-end' }
368 diff --git a/qemu-options.hx b/qemu-options.hx
369 index a71aaf8..37fad3b 100644
370 --- a/qemu-options.hx
371 +++ b/qemu-options.hx
372 @@ -3302,6 +3302,19 @@ STEXI
373 Start right away with a saved state (@code{loadvm} in monitor)
376 +DEF("loadstate", HAS_ARG, QEMU_OPTION_loadstate, \
377 + "-loadstate file\n" \
378 + " start right away with a saved state\n",
381 +@item -loadstate @var{file}
383 +Start right away with a saved state. This option does not rollback
384 +disk state like @code{loadvm}, so user must make sure that disk
385 +have correct state. @var{file} can be any valid device URL. See the section
386 +for "Device URL Syntax" for more information.
390 DEF("daemonize", 0, QEMU_OPTION_daemonize, \
391 "-daemonize daemonize QEMU after initializing\n", QEMU_ARCH_ALL)
392 diff --git a/qmp-commands.hx b/qmp-commands.hx
393 index a8e8522..6342cd2 100644
394 --- a/qmp-commands.hx
395 +++ b/qmp-commands.hx
396 @@ -4904,6 +4904,36 @@ Example:
400 + .name = "savevm-start",
401 + .args_type = "statefile:s?",
402 + .mhandler.cmd_new = qmp_marshal_savevm_start,
406 + .name = "snapshot-drive",
407 + .args_type = "device:s,name:s",
408 + .mhandler.cmd_new = qmp_marshal_snapshot_drive,
412 + .name = "delete-drive-snapshot",
413 + .args_type = "device:s,name:s",
414 + .mhandler.cmd_new = qmp_marshal_delete_drive_snapshot,
418 + .name = "savevm-end",
420 + .mhandler.cmd_new = qmp_marshal_savevm_end,
424 + .name = "query-savevm",
426 + .mhandler.cmd_new = qmp_marshal_query_savevm,
430 .name = "query-rocker",
431 .args_type = "name:s",
432 .mhandler.cmd_new = qmp_marshal_query_rocker,
433 diff --git a/savevm-async.c b/savevm-async.c
435 index 0000000..ae7ea84
439 +#include "qemu/osdep.h"
440 +#include "qemu-common.h"
441 +#include "qapi/qmp/qerror.h"
442 +#include "qemu/error-report.h"
443 +#include "sysemu/sysemu.h"
444 +#include "qmp-commands.h"
445 +#include "qemu-options.h"
446 +#include "migration/qemu-file.h"
447 +#include "qom/qom-qobject.h"
448 +#include "migration/migration.h"
449 +#include "block/snapshot.h"
450 +#include "block/qapi.h"
451 +#include "block/block.h"
452 +#include "qemu/timer.h"
453 +#include "sysemu/block-backend.h"
454 +#include "qapi/qmp/qstring.h"
455 +#include "qemu/rcu.h"
456 +#include "qemu/thread.h"
457 +#include "qemu/cutils.h"
459 +/* #define DEBUG_SAVEVM_STATE */
461 +#ifdef DEBUG_SAVEVM_STATE
462 +#define DPRINTF(fmt, ...) \
463 + do { printf("savevm-async: " fmt, ## __VA_ARGS__); } while (0)
465 +#define DPRINTF(fmt, ...) \
473 + SAVE_STATE_COMPLETED,
474 + SAVE_STATE_CANCELLED
478 +static struct SnapshotState {
479 + BlockDriverState *bs;
484 + int saved_vm_running;
486 + int64_t total_time;
489 +SaveVMInfo *qmp_query_savevm(Error **errp)
491 + SaveVMInfo *info = g_malloc0(sizeof(*info));
492 + struct SnapshotState *s = &snap_state;
494 + if (s->state != SAVE_STATE_DONE) {
495 + info->has_bytes = true;
496 + info->bytes = s->bs_pos;
497 + switch (s->state) {
498 + case SAVE_STATE_ERROR:
499 + info->has_status = true;
500 + info->status = g_strdup("failed");
501 + info->has_total_time = true;
502 + info->total_time = s->total_time;
504 + info->has_error = true;
505 + info->error = g_strdup(error_get_pretty(s->error));
508 + case SAVE_STATE_ACTIVE:
509 + info->has_status = true;
510 + info->status = g_strdup("active");
511 + info->has_total_time = true;
512 + info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
515 + case SAVE_STATE_COMPLETED:
516 + info->has_status = true;
517 + info->status = g_strdup("completed");
518 + info->has_total_time = true;
519 + info->total_time = s->total_time;
527 +static int save_snapshot_cleanup(void)
531 + DPRINTF("save_snapshot_cleanup\n");
533 + snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
534 + snap_state.total_time;
536 + if (snap_state.file) {
537 + ret = qemu_fclose(snap_state.file);
540 + if (snap_state.bs) {
541 + /* try to truncate, but ignore errors (will fail on block devices).
542 + * note: bdrv_read() need whole blocks, so we round up
544 + size_t size = (snap_state.bs_pos + BDRV_SECTOR_SIZE) & BDRV_SECTOR_MASK;
545 + bdrv_truncate(snap_state.bs, size);
546 + bdrv_op_unblock_all(snap_state.bs, snap_state.blocker);
547 + error_free(snap_state.blocker);
548 + snap_state.blocker = NULL;
549 + bdrv_unref(snap_state.bs);
550 + snap_state.bs = NULL;
556 +static void save_snapshot_error(const char *fmt, ...)
562 + msg = g_strdup_vprintf(fmt, ap);
565 + DPRINTF("save_snapshot_error: %s\n", msg);
567 + if (!snap_state.error) {
568 + error_set(&snap_state.error, ERROR_CLASS_GENERIC_ERROR, "%s", msg);
573 + snap_state.state = SAVE_STATE_ERROR;
575 + save_snapshot_cleanup();
578 +static void save_snapshot_completed(void)
580 + DPRINTF("save_snapshot_completed\n");
582 + if (save_snapshot_cleanup() < 0) {
583 + snap_state.state = SAVE_STATE_ERROR;
585 + snap_state.state = SAVE_STATE_COMPLETED;
589 +static int block_state_close(void *opaque)
591 + snap_state.file = NULL;
592 + return bdrv_flush(snap_state.bs);
595 +static int block_state_put_buffer(void *opaque, const uint8_t *buf,
596 + int64_t pos, int size)
600 + assert(pos == snap_state.bs_pos);
602 + if ((ret = bdrv_pwrite(snap_state.bs, snap_state.bs_pos, buf, size)) > 0) {
603 + snap_state.bs_pos += ret;
609 +static int store_and_stop(void) {
610 + if (global_state_store()) {
611 + save_snapshot_error("Error saving global state");
614 + if (runstate_is_running()) {
615 + vm_stop(RUN_STATE_SAVE_VM);
620 +static void process_savevm_co(void *opaque)
624 + MigrationParams params = {
629 + snap_state.state = SAVE_STATE_ACTIVE;
631 + qemu_mutex_unlock_iothread();
632 + qemu_savevm_state_header(snap_state.file);
633 + ret = qemu_savevm_state_begin(snap_state.file, ¶ms);
634 + qemu_mutex_lock_iothread();
637 + save_snapshot_error("qemu_savevm_state_begin failed");
641 + while (snap_state.state == SAVE_STATE_ACTIVE) {
642 + uint64_t pending_size;
644 + pending_size = qemu_savevm_state_pending(snap_state.file, 0);
646 + if (pending_size) {
647 + ret = qemu_savevm_state_iterate(snap_state.file);
649 + save_snapshot_error("qemu_savevm_state_iterate error %d", ret);
652 + DPRINTF("savevm inerate pending size %lu ret %d\n", pending_size, ret);
654 + DPRINTF("done iterating\n");
655 + if (store_and_stop())
657 + DPRINTF("savevm inerate finished\n");
658 + qemu_savevm_state_complete_precopy(snap_state.file);
659 + DPRINTF("save complete\n");
660 + save_snapshot_completed();
664 + /* stop the VM if we get to the end of available space,
665 + * or if pending_size is just a few MB
667 + maxlen = bdrv_getlength(snap_state.bs) - 30*1024*1024;
668 + if ((pending_size < 100000) ||
669 + ((snap_state.bs_pos + pending_size) >= maxlen)) {
670 + if (store_and_stop())
675 + if(snap_state.state == SAVE_STATE_CANCELLED) {
676 + save_snapshot_completed();
677 + Error *errp = NULL;
678 + qmp_savevm_end(&errp);
683 +static const QEMUFileOps block_file_ops = {
684 + .put_buffer = block_state_put_buffer,
685 + .close = block_state_close,
689 +void qmp_savevm_start(bool has_statefile, const char *statefile, Error **errp)
691 + BlockDriver *drv = NULL;
692 + Error *local_err = NULL;
694 + int bdrv_oflags = BDRV_O_RDWR;
697 + if (snap_state.state != SAVE_STATE_DONE) {
698 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
699 + "VM snapshot already started\n");
703 + /* initialize snapshot info */
704 + snap_state.saved_vm_running = runstate_is_running();
705 + snap_state.bs_pos = 0;
706 + snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
707 + snap_state.blocker = NULL;
709 + if (snap_state.error) {
710 + error_free(snap_state.error);
711 + snap_state.error = NULL;
714 + if (!has_statefile) {
715 + vm_stop(RUN_STATE_SAVE_VM);
716 + snap_state.state = SAVE_STATE_COMPLETED;
720 + if (qemu_savevm_state_blocked(errp)) {
724 + /* Open the image */
725 + snap_state.bs = bdrv_new();
727 + QDict *options = NULL;
728 + options = qdict_new();
729 + qdict_put(options, "driver", qstring_from_str("raw"));
730 + ret = bdrv_open(&snap_state.bs, statefile, NULL, options, bdrv_oflags, drv, &local_err);
732 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
736 + snap_state.file = qemu_fopen_ops(&snap_state, &block_file_ops);
738 + if (!snap_state.file) {
739 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
744 + error_setg(&snap_state.blocker, "block device is in use by savevm");
745 + bdrv_op_block_all(snap_state.bs, snap_state.blocker);
747 + Coroutine *co = qemu_coroutine_create(process_savevm_co);
748 + qemu_coroutine_enter(co);
754 + save_snapshot_error("setup failed");
756 + if (snap_state.saved_vm_running) {
761 +void qmp_savevm_end(Error **errp)
763 + if (snap_state.state == SAVE_STATE_DONE) {
764 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
765 + "VM snapshot not started\n");
769 + if (snap_state.state == SAVE_STATE_ACTIVE) {
770 + snap_state.state = SAVE_STATE_CANCELLED;
774 + if (snap_state.saved_vm_running) {
778 + snap_state.state = SAVE_STATE_DONE;
781 +void qmp_snapshot_drive(const char *device, const char *name, Error **errp)
784 + BlockDriverState *bs;
785 + QEMUSnapshotInfo sn1, *sn = &sn1;
793 + if (snap_state.state != SAVE_STATE_COMPLETED) {
794 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
795 + "VM snapshot not ready/started\n");
799 + blk = blk_by_name(device);
801 + error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
802 + "Device '%s' not found", device);
807 + if (!bdrv_is_inserted(bs)) {
808 + error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
812 + if (bdrv_is_read_only(bs)) {
813 + error_setg(errp, "Node '%s' is read only", device);
817 + if (!bdrv_can_snapshot(bs)) {
818 + error_setg(errp, QERR_UNSUPPORTED);
822 + if (bdrv_snapshot_find(bs, sn, name) >= 0) {
823 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
824 + "snapshot '%s' already exists", name);
829 + memset(sn, 0, sizeof(*sn));
833 + sn->date_sec = tb.time;
834 + sn->date_nsec = tb.millitm * 1000000;
836 + gettimeofday(&tv, NULL);
837 + sn->date_sec = tv.tv_sec;
838 + sn->date_nsec = tv.tv_usec * 1000;
840 + sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
842 + pstrcpy(sn->name, sizeof(sn->name), name);
844 + sn->vm_state_size = 0; /* do not save state */
846 + ret = bdrv_snapshot_create(bs, sn);
848 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
849 + "Error while creating snapshot on '%s'\n", device);
854 +void qmp_delete_drive_snapshot(const char *device, const char *name,
858 + BlockDriverState *bs;
859 + QEMUSnapshotInfo sn1, *sn = &sn1;
860 + Error *local_err = NULL;
864 + blk = blk_by_name(device);
866 + error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
867 + "Device '%s' not found", device);
872 + if (bdrv_is_read_only(bs)) {
873 + error_setg(errp, "Node '%s' is read only", device);
877 + if (!bdrv_can_snapshot(bs)) {
878 + error_setg(errp, QERR_UNSUPPORTED);
882 + if (bdrv_snapshot_find(bs, sn, name) < 0) {
883 + /* return success if snapshot does not exists */
887 + ret = bdrv_snapshot_delete(bs, NULL, name, &local_err);
889 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
890 + "Error while deleting snapshot on '%s'\n", device);
895 +static int loadstate_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
898 + BlockDriverState *bs = (BlockDriverState *)opaque;
899 + int64_t maxlen = bdrv_getlength(bs);
900 + if (pos > maxlen) {
903 + if ((pos + size) > maxlen) {
904 + size = maxlen - pos - 1;
909 + return bdrv_pread(bs, pos, buf, size);
912 +static const QEMUFileOps loadstate_file_ops = {
913 + .get_buffer = loadstate_get_buffer,
916 +int load_state_from_blockdev(const char *filename)
918 + BlockDriverState *bs = NULL;
919 + BlockDriver *drv = NULL;
920 + Error *local_err = NULL;
921 + Error *blocker = NULL;
927 + ret = bdrv_open(&bs, filename, NULL, NULL, 0, drv, &local_err);
928 + error_setg(&blocker, "block device is in use by load state");
929 + bdrv_op_block_all(bs, blocker);
932 + error_report("Could not open VM state file");
936 + /* restore the VM state */
937 + f = qemu_fopen_ops(bs, &loadstate_file_ops);
939 + error_report("Could not open VM state file");
944 + qemu_system_reset(VMRESET_SILENT);
945 + migration_incoming_state_new(f);
946 + ret = qemu_loadvm_state(f);
949 + migration_incoming_state_destroy();
951 + error_report("Error %d while loading VM state", ret);
959 + bdrv_op_unblock_all(bs, blocker);
960 + error_free(blocker);
965 diff --git a/vl.c b/vl.c
966 index bdf4fdc..85033e3 100644
969 @@ -2948,6 +2948,7 @@ int main(int argc, char **argv, char **envp)
972 const char *loadvm = NULL;
973 + const char *loadstate = NULL;
974 MachineClass *machine_class;
975 const char *cpu_model;
976 const char *vga_model = NULL;
977 @@ -3589,6 +3590,9 @@ int main(int argc, char **argv, char **envp)
978 case QEMU_OPTION_loadvm:
981 + case QEMU_OPTION_loadstate:
982 + loadstate = optarg;
984 case QEMU_OPTION_full_screen:
987 @@ -4583,6 +4587,10 @@ int main(int argc, char **argv, char **envp)
988 if (load_vmstate(loadvm) < 0) {
991 + } else if (loadstate) {
992 + if (load_state_from_blockdev(loadstate) < 0) {
997 qdev_prop_check_globals();