1 Index: new/qapi-schema.json
2 ===================================================================
3 --- new.orig/qapi-schema.json 2012-09-24 07:15:44.000000000 +0200
4 +++ new/qapi-schema.json 2012-09-24 07:15:58.000000000 +0200
6 '*total-time': 'int'} }
11 +# Information about current migration process.
13 +# @status: #optional string describing the current savevm status.
14 +# This can be 'active', 'completed', 'failed'.
15 +# If this field is not returned, no savevm process
18 +# @error: #optional string containing error message is status is failed.
20 +# @total-time: #optional total amount of milliseconds since savevm started.
21 +# If savevm has ended, it returns the total save time
23 +# @bytes: #optional total amount of data transfered
27 +{ 'type': 'SaveVMInfo',
28 + 'data': {'*status': 'str', '*error': 'str',
29 + '*total-time': 'int', '*bytes': 'int'} }
34 +# Returns information about current savevm process.
36 +# Returns: @SaveVMInfo
40 +{ 'command': 'query-savevm', 'returns': 'SaveVMInfo' }
45 # Returns information about current migration process.
46 @@ -2493,3 +2527,12 @@
49 { 'command': 'query-target', 'returns': 'TargetInfo' }
52 +{ 'command': 'savevm-start' 'data': { '*statefile': 'str' } }
54 +{ 'command': 'snapshot-drive', 'data': { 'device': 'str', 'name': 'str' } }
56 +{ 'command': 'delete-drive-snapshot', 'data': { 'device': 'str', 'name': 'str' } }
58 +{ 'command': 'savevm-end' }
59 Index: new/qmp-commands.hx
60 ===================================================================
61 --- new.orig/qmp-commands.hx 2012-09-24 07:15:44.000000000 +0200
62 +++ new/qmp-commands.hx 2012-09-24 08:58:56.000000000 +0200
63 @@ -2514,3 +2514,34 @@
65 .mhandler.cmd_new = qmp_marshal_input_query_target,
69 + .name = "savevm-start",
70 + .args_type = "statefile:s?",
71 + .mhandler.cmd_new = qmp_marshal_input_savevm_start,
75 + .name = "snapshot-drive",
76 + .args_type = "device:s,name:s",
77 + .mhandler.cmd_new = qmp_marshal_input_snapshot_drive,
81 + .name = "delete-drive-snapshot",
82 + .args_type = "device:s,name:s",
83 + .mhandler.cmd_new = qmp_marshal_input_delete_drive_snapshot,
87 + .name = "savevm-end",
89 + .mhandler.cmd_new = qmp_marshal_input_savevm_end,
93 + .name = "query-savevm",
95 + .mhandler.cmd_new = qmp_marshal_input_query_savevm,
99 ===================================================================
100 --- new.orig/hmp.c 2012-09-24 07:15:44.000000000 +0200
101 +++ new/hmp.c 2012-09-24 07:15:58.000000000 +0200
102 @@ -1102,3 +1102,60 @@
103 qmp_closefd(fdname, &errp);
104 hmp_handle_error(mon, &errp);
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)
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 ===================================================================
165 --- new.orig/hmp.h 2012-09-24 07:15:44.000000000 +0200
166 +++ new/hmp.h 2012-09-24 07:15:58.000000000 +0200
168 void hmp_info_uuid(Monitor *mon);
169 void hmp_info_chardev(Monitor *mon);
170 void hmp_info_mice(Monitor *mon);
171 +void hmp_info_savevm(Monitor *mon);
172 void hmp_info_migrate(Monitor *mon);
173 void hmp_info_migrate_capabilities(Monitor *mon);
174 void hmp_info_migrate_cache_size(Monitor *mon);
176 void hmp_netdev_del(Monitor *mon, const QDict *qdict);
177 void hmp_getfd(Monitor *mon, const QDict *qdict);
178 void hmp_closefd(Monitor *mon, const QDict *qdict);
179 +void hmp_savevm_start(Monitor *mon, const QDict *qdict);
180 +void hmp_snapshot_drive(Monitor *mon, const QDict *qdict);
181 +void hmp_delete_drive_snapshot(Monitor *mon, const QDict *qdict);
182 +void hmp_savevm_end(Monitor *mon, const QDict *qdict);
185 Index: new/hmp-commands.hx
186 ===================================================================
187 --- new.orig/hmp-commands.hx 2012-09-24 07:15:44.000000000 +0200
188 +++ new/hmp-commands.hx 2012-09-24 07:15:58.000000000 +0200
189 @@ -1468,6 +1468,8 @@
190 show current migration capabilities
191 @item info migrate_cache_size
192 show current migration XBZRLE cache size
196 show balloon information
198 @@ -1494,3 +1496,35 @@
204 + .name = "savevm-start",
205 + .args_type = "statefile:s?",
206 + .params = "[statefile]",
207 + .help = "Prepare for snapshot and halt VM. Save VM state to statefile.",
208 + .mhandler.cmd = hmp_savevm_start,
212 + .name = "snapshot-drive",
213 + .args_type = "device:s,name:s",
214 + .params = "device name",
215 + .help = "Create internal snapshot.",
216 + .mhandler.cmd = hmp_snapshot_drive,
220 + .name = "delete-drive-snapshot",
221 + .args_type = "device:s,name:s",
222 + .params = "device name",
223 + .help = "Delete internal snapshot.",
224 + .mhandler.cmd = hmp_delete_drive_snapshot,
228 + .name = "savevm-end",
231 + .help = "Resume VM after snaphot.",
232 + .mhandler.cmd = hmp_savevm_end,
234 Index: new/savevm-async.c
235 ===================================================================
236 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
237 +++ new/savevm-async.c 2012-09-24 09:17:54.000000000 +0200
239 +#include "qemu-common.h"
242 +#include "qmp-commands.h"
243 +#include "blockdev.h"
244 +#include "qemu/qom-qobject.h"
245 +#include "buffered_file.h"
246 +#include "migration.h"
248 +//#define DEBUG_SAVEVM_STATE
250 +#ifdef DEBUG_SAVEVM_STATE
251 +#define DPRINTF(fmt, ...) \
252 + do { printf("savevm-async: " fmt, ## __VA_ARGS__); } while (0)
254 +#define DPRINTF(fmt, ...) \
262 + SAVE_STATE_COMPLETED,
265 +static struct SnapshotState {
266 + BlockDriverState *bs;
270 + int saved_vm_running;
272 + int64_t total_time;
275 +SaveVMInfo *qmp_query_savevm(Error **errp)
277 + SaveVMInfo *info = g_malloc0(sizeof(*info));
278 + struct SnapshotState *s = &snap_state;
280 + if (s->state != SAVE_STATE_DONE) {
281 + info->has_bytes = true;
282 + info->bytes = s->bs_pos;
283 + switch (s->state) {
284 + case SAVE_STATE_ERROR:
285 + info->has_status = true;
286 + info->status = g_strdup("failed");
287 + info->has_total_time = true;
288 + info->total_time = s->total_time;
290 + info->has_error = true;
291 + info->error = g_strdup(error_get_pretty(s->error));
294 + case SAVE_STATE_ACTIVE:
295 + info->has_status = true;
296 + info->status = g_strdup("active");
297 + info->has_total_time = true;
298 + info->total_time = qemu_get_clock_ms(rt_clock)
301 + case SAVE_STATE_COMPLETED:
302 + info->has_status = true;
303 + info->status = g_strdup("completed");
304 + info->has_total_time = true;
305 + info->total_time = s->total_time;
313 +static int save_snapshot_cleanup(void)
317 + DPRINTF("save_snapshot_cleanup\n");
319 + snap_state.total_time = qemu_get_clock_ms(rt_clock) -
320 + snap_state.total_time;
322 + if (snap_state.file) {
323 + ret = qemu_fclose(snap_state.file);
326 + if (snap_state.bs) {
327 + // try to truncate, but ignore errors (will fail on block devices).
328 + // note: bdrv_read() need whole blocks, so we round up
329 + size_t size = (snap_state.bs_pos + BDRV_SECTOR_SIZE) & BDRV_SECTOR_MASK;
330 + bdrv_truncate(snap_state.bs, size);
332 + bdrv_delete(snap_state.bs);
333 + snap_state.bs = NULL;
339 +static void save_snapshot_error(const char *fmt, ...)
345 + msg = g_strdup_vprintf(fmt, ap);
348 + DPRINTF("save_snapshot_error: %s\n", msg);
350 + if (!snap_state.error) {
351 + error_set(&snap_state.error, ERROR_CLASS_GENERIC_ERROR, "%s", msg);
356 + snap_state.state = SAVE_STATE_ERROR;
358 + save_snapshot_cleanup();
361 +static void save_snapshot_completed(void)
363 + DPRINTF("save_snapshot_completed\n");
365 + if (save_snapshot_cleanup() < 0) {
366 + snap_state.state = SAVE_STATE_ERROR;
368 + snap_state.state = SAVE_STATE_COMPLETED;
372 +static int block_state_close(void *opaque)
374 + snap_state.file = NULL;
375 + return bdrv_flush(snap_state.bs);
378 +static ssize_t block_state_put_buffer(void *opaque, const void *buf,
383 + if ((ret = bdrv_pwrite(snap_state.bs, snap_state.bs_pos, buf, size)) > 0) {
384 + snap_state.bs_pos += ret;
390 +static void block_state_put_ready(void *opaque)
394 + if (snap_state.state != SAVE_STATE_ACTIVE) {
395 + save_snapshot_error("put_ready returning because of non-active state");
399 + if (!runstate_check(RUN_STATE_SAVE_VM)) {
400 + save_snapshot_error("put_ready returning because of wrong run state");
404 + ret = qemu_savevm_state_iterate(snap_state.file);
406 + save_snapshot_error("qemu_savevm_state_iterate error %d", ret);
408 + } else if (ret == 1) {
409 + DPRINTF("savevm inerate finished\n");
410 + if ((ret = qemu_savevm_state_complete(snap_state.file)) < 0) {
411 + save_snapshot_error("qemu_savevm_state_complete error %d", ret);
414 + DPRINTF("save complete\n");
415 + save_snapshot_completed();
421 +static void block_state_wait_for_unfreeze(void *opaque)
423 + /* do nothing here - should not be called */
426 +void qmp_savevm_start(bool has_statefile, const char *statefile, Error **errp)
428 + BlockDriver *drv = NULL;
429 + int bdrv_oflags = BDRV_O_CACHE_WB | BDRV_O_RDWR;
430 + MigrationParams params = {
436 + if (snap_state.state != SAVE_STATE_DONE) {
437 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
438 + "VM snapshot already started\n");
442 + /* initialize snapshot info */
443 + snap_state.saved_vm_running = runstate_is_running();
444 + snap_state.bs_pos = 0;
445 + snap_state.total_time = qemu_get_clock_ms(rt_clock);
447 + if (snap_state.error) {
448 + error_free(snap_state.error);
449 + snap_state.error = NULL;
453 + vm_stop(RUN_STATE_SAVE_VM);
455 + if (!has_statefile) {
456 + snap_state.state = SAVE_STATE_COMPLETED;
460 + if (qemu_savevm_state_blocked(errp)) {
464 + /* Open the image */
465 + snap_state.bs = bdrv_new("vmstate");
466 + ret = bdrv_open(snap_state.bs, statefile, bdrv_oflags, drv);
468 + error_set(errp, QERR_OPEN_FILE_FAILED, statefile);
472 + snap_state.file = qemu_fopen_ops_buffered(&snap_state, 1000000000,
473 + block_state_put_buffer,
474 + block_state_put_ready,
475 + block_state_wait_for_unfreeze,
476 + block_state_close);
478 + if (!snap_state.file) {
479 + error_set(errp, QERR_OPEN_FILE_FAILED, statefile);
483 + snap_state.state = SAVE_STATE_ACTIVE;
485 + ret = qemu_savevm_state_begin(snap_state.file, ¶ms);
487 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
488 + "qemu_savevm_state_begin failed\n");
492 + block_state_put_ready(&snap_state);
498 + save_snapshot_error("setup failed");
500 + if (snap_state.saved_vm_running) {
505 +void qmp_savevm_end(Error **errp)
507 + if (snap_state.state == SAVE_STATE_DONE) {
508 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
509 + "VM snapshot not started\n");
513 + if (snap_state.saved_vm_running) {
517 + snap_state.state = SAVE_STATE_DONE;
520 +void qmp_snapshot_drive(const char *device, const char *name, Error **errp)
522 + BlockDriverState *bs;
523 + QEMUSnapshotInfo sn1, *sn = &sn1;
531 + if (snap_state.state != SAVE_STATE_COMPLETED) {
532 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
533 + "VM snapshot not ready/started\n");
537 + bs = bdrv_find(device);
539 + error_set(errp, QERR_DEVICE_NOT_FOUND, device);
543 + if (!bdrv_is_inserted(bs)) {
544 + error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
548 + if (bdrv_is_read_only(bs)) {
549 + error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
553 + if (!bdrv_can_snapshot(bs)) {
554 + error_set(errp, QERR_NOT_SUPPORTED);
558 + if (bdrv_snapshot_find(bs, sn, name) >= 0) {
559 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
560 + "snapshot '%s' already exists", name);
565 + memset(sn, 0, sizeof(*sn));
569 + sn->date_sec = tb.time;
570 + sn->date_nsec = tb.millitm * 1000000;
572 + gettimeofday(&tv, NULL);
573 + sn->date_sec = tv.tv_sec;
574 + sn->date_nsec = tv.tv_usec * 1000;
576 + sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
578 + pstrcpy(sn->name, sizeof(sn->name), name);
580 + sn->vm_state_size = 0; /* do not save state */
582 + ret = bdrv_snapshot_create(bs, sn);
584 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
585 + "Error while creating snapshot on '%s'\n", device);
590 +void qmp_delete_drive_snapshot(const char *device, const char *name,
593 + BlockDriverState *bs;
594 + QEMUSnapshotInfo sn1, *sn = &sn1;
597 + bs = bdrv_find(device);
599 + error_set(errp, QERR_DEVICE_NOT_FOUND, device);
602 + if (bdrv_is_read_only(bs)) {
603 + error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
607 + if (!bdrv_can_snapshot(bs)) {
608 + error_set(errp, QERR_NOT_SUPPORTED);
612 + if (bdrv_snapshot_find(bs, sn, name) < 0) {
613 + /* return success if snapshot does not exists */
617 + ret = bdrv_snapshot_delete(bs, name);
619 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
620 + "Error while deleting snapshot on '%s'\n", device);
625 +static int loadstate_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
627 + BlockDriverState *bs = (BlockDriverState *)opaque;
628 + int64_t maxlen = bdrv_getlength(bs);
629 + if (pos > maxlen) {
632 + if ((pos + size) > maxlen) {
633 + size = maxlen - pos - 1;
638 + return bdrv_pread(bs, pos, buf, size);
641 +int load_state_from_blockdev(const char *filename)
643 + BlockDriverState *bs = NULL;
644 + BlockDriver *drv = NULL;
648 + bs = bdrv_new("vmstate");
649 + ret = bdrv_open(bs, filename, BDRV_O_CACHE_WB, drv);
651 + error_report("Could not open VM state file");
655 + /* restore the VM state */
656 + f = qemu_fopen_ops(bs, NULL, loadstate_get_buffer, NULL, NULL, NULL, NULL);
658 + error_report("Could not open VM state file");
663 + qemu_system_reset(VMRESET_SILENT);
664 + ret = qemu_loadvm_state(f);
668 + error_report("Error %d while loading VM state", ret);
680 Index: new/Makefile.objs
681 ===================================================================
682 --- new.orig/Makefile.objs 2012-09-24 07:15:44.000000000 +0200
683 +++ new/Makefile.objs 2012-09-24 07:15:58.000000000 +0200
685 common-obj-y += pflib.o
686 common-obj-y += bitmap.o bitops.o
687 common-obj-y += page_cache.o
688 +common-obj-y += savevm-async.o
690 common-obj-$(CONFIG_POSIX) += migration-exec.o migration-unix.o migration-fd.o
691 common-obj-$(CONFIG_WIN32) += version.o
693 ===================================================================
694 --- new.orig/sysemu.h 2012-09-24 07:15:44.000000000 +0200
695 +++ new/sysemu.h 2012-09-24 07:15:58.000000000 +0200
698 void do_savevm(Monitor *mon, const QDict *qdict);
699 int load_vmstate(const char *name);
700 +int load_state_from_blockdev(const char *filename);
701 void do_delvm(Monitor *mon, const QDict *qdict);
702 void do_info_snapshots(Monitor *mon);
704 Index: new/qemu-options.hx
705 ===================================================================
706 --- new.orig/qemu-options.hx 2012-09-24 07:15:51.000000000 +0200
707 +++ new/qemu-options.hx 2012-09-24 07:15:58.000000000 +0200
708 @@ -2477,6 +2477,19 @@
709 Start right away with a saved state (@code{loadvm} in monitor)
712 +DEF("loadstate", HAS_ARG, QEMU_OPTION_loadstate, \
713 + "-loadstate file\n" \
714 + " start right away with a saved state\n",
717 +@item -loadstate @var{file}
719 +Start right away with a saved state. This option does not rollback
720 +disk state like @code{loadvm}, so user must make sure that disk
721 +have correct state. @var{file} can be any valid device URL. See the section
722 +for "Device URL Syntax" for more information.
726 DEF("daemonize", 0, QEMU_OPTION_daemonize, \
727 "-daemonize daemonize QEMU after initializing\n", QEMU_ARCH_ALL)
729 ===================================================================
730 --- new.orig/vl.c 2012-09-24 07:15:53.000000000 +0200
731 +++ new/vl.c 2012-09-24 07:15:58.000000000 +0200
732 @@ -2364,6 +2364,7 @@
735 const char *loadvm = NULL;
736 + const char *loadstate = NULL;
737 QEMUMachine *machine;
738 const char *cpu_model;
739 const char *vga_model = "none";
740 @@ -2998,6 +2999,9 @@
741 case QEMU_OPTION_loadvm:
744 + case QEMU_OPTION_loadstate:
745 + loadstate = optarg;
747 case QEMU_OPTION_full_screen:
750 @@ -3821,6 +3825,10 @@
751 if (load_vmstate(loadvm) < 0) {
754 + } else if (loadstate) {
755 + if (load_state_from_blockdev(loadstate) < 0) {
762 ===================================================================
763 --- new.orig/monitor.c 2012-09-24 09:21:18.000000000 +0200
764 +++ new/monitor.c 2012-09-24 09:21:32.000000000 +0200
765 @@ -2952,6 +2952,13 @@
766 .mhandler.info = hmp_info_migrate_cache_size,
772 + .help = "show savevm status",
773 + .mhandler.info = hmp_info_savevm,