]> git.proxmox.com Git - pve-qemu-kvm.git/blame - debian/patches/internal-snapshot-async.patch
update to qemu 2.2.0-rc2
[pve-qemu-kvm.git] / debian / patches / internal-snapshot-async.patch
CommitLineData
fb40cdcd
AD
1From 46fd4bb673a91d40352c95e9d3f62f63b5021053 Mon Sep 17 00:00:00 2001
2From: Stefan Priebe <s.priebe@profihost.ag>
3Date: Fri, 29 Nov 2013 22:17:03 +0100
4Subject: [PATCH] internal-snapshot-async-qemu1.7.patch
5
6---
7 Makefile.objs | 1 +
8 block.c | 2 +-
9 hmp-commands.hx | 34 ++++
10 hmp.c | 57 ++++++
11 hmp.h | 5 +
12 include/block/block.h | 1 +
13 include/sysemu/sysemu.h | 5 +-
14 monitor.c | 7 +
15 qapi-schema.json | 46 +++++
16 qemu-options.hx | 13 ++
17 qmp-commands.hx | 31 +++
18 savevm-async.c | 478 +++++++++++++++++++++++++++++++++++++++++++++++
19 savevm.c | 10 +-
20 vl.c | 9 +
21 14 files changed, 692 insertions(+), 7 deletions(-)
22 create mode 100644 savevm-async.c
23
b08067ba
DM
24Index: new/Makefile.objs
25===================================================================
24bb7da3
DM
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
28@@ -56,6 +56,7 @@
fb40cdcd
AD
29 common-obj-y += qemu-char.o #aio.o
30 common-obj-y += block-migration.o
31 common-obj-y += page_cache.o xbzrle.o
32+common-obj-y += savevm-async.o
cd983153 33
fb40cdcd 34 common-obj-$(CONFIG_POSIX) += migration-exec.o migration-unix.o migration-fd.o
e96de165 35
b08067ba
DM
36Index: new/block.c
37===================================================================
24bb7da3
DM
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
40@@ -2119,7 +2119,7 @@
fbe817e2 41 bdrv_set_backing_hd(bs_top, bs_new);
fb40cdcd 42 }
92bf040c 43
fb40cdcd
AD
44-static void bdrv_delete(BlockDriverState *bs)
45+void bdrv_delete(BlockDriverState *bs)
46 {
fb40cdcd 47 assert(!bs->job);
24bb7da3 48 assert(bdrv_op_blocker_is_empty(bs));
b08067ba
DM
49Index: new/hmp-commands.hx
50===================================================================
24bb7da3
DM
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
53@@ -1799,6 +1799,8 @@
fb40cdcd
AD
54 show current migration capabilities
55 @item info migrate_cache_size
56 show current migration XBZRLE cache size
57+@item info savevm
58+show savevm status
59 @item info balloon
60 show balloon information
61 @item info qtree
24bb7da3 62@@ -1822,3 +1824,35 @@
fb40cdcd
AD
63 STEXI
64 @end table
65 ETEXI
cd983153
DM
66+
67+ {
1f416f3a 68+ .name = "savevm-start",
cd983153 69+ .args_type = "statefile:s?",
fb40cdcd
AD
70+ .params = "[statefile]",
71+ .help = "Prepare for snapshot and halt VM. Save VM state to statefile.",
72+ .mhandler.cmd = hmp_savevm_start,
cd983153
DM
73+ },
74+
75+ {
76+ .name = "snapshot-drive",
77+ .args_type = "device:s,name:s",
fb40cdcd
AD
78+ .params = "device name",
79+ .help = "Create internal snapshot.",
80+ .mhandler.cmd = hmp_snapshot_drive,
cd983153
DM
81+ },
82+
83+ {
84+ .name = "delete-drive-snapshot",
85+ .args_type = "device:s,name:s",
fb40cdcd
AD
86+ .params = "device name",
87+ .help = "Delete internal snapshot.",
88+ .mhandler.cmd = hmp_delete_drive_snapshot,
cd983153
DM
89+ },
90+
91+ {
1f416f3a 92+ .name = "savevm-end",
cd983153 93+ .args_type = "",
fb40cdcd
AD
94+ .params = "",
95+ .help = "Resume VM after snaphot.",
96+ .mhandler.cmd = hmp_savevm_end,
629cf2f4 97+ },
b08067ba
DM
98Index: new/hmp.c
99===================================================================
24bb7da3
DM
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,3 +1849,60 @@
3fe80761 103
24bb7da3 104 qapi_free_MemoryDeviceInfoList(info_list);
cd983153
DM
105 }
106+
1f416f3a 107+void hmp_savevm_start(Monitor *mon, const QDict *qdict)
cd983153
DM
108+{
109+ Error *errp = NULL;
110+ const char *statefile = qdict_get_try_str(qdict, "statefile");
111+
1f416f3a 112+ qmp_savevm_start(statefile != NULL, statefile, &errp);
cd983153
DM
113+ hmp_handle_error(mon, &errp);
114+}
115+
116+void hmp_snapshot_drive(Monitor *mon, const QDict *qdict)
117+{
118+ Error *errp = NULL;
119+ const char *name = qdict_get_str(qdict, "name");
120+ const char *device = qdict_get_str(qdict, "device");
121+
122+ qmp_snapshot_drive(device, name, &errp);
123+ hmp_handle_error(mon, &errp);
124+}
125+
126+void hmp_delete_drive_snapshot(Monitor *mon, const QDict *qdict)
127+{
128+ Error *errp = NULL;
129+ const char *name = qdict_get_str(qdict, "name");
130+ const char *device = qdict_get_str(qdict, "device");
131+
132+ qmp_delete_drive_snapshot(device, name, &errp);
133+ hmp_handle_error(mon, &errp);
134+}
135+
1f416f3a 136+void hmp_savevm_end(Monitor *mon, const QDict *qdict)
cd983153
DM
137+{
138+ Error *errp = NULL;
139+
1f416f3a 140+ qmp_savevm_end(&errp);
cd983153
DM
141+ hmp_handle_error(mon, &errp);
142+}
143+
92bf040c 144+void hmp_info_savevm(Monitor *mon, const QDict *qdict)
cd983153
DM
145+{
146+ SaveVMInfo *info;
147+ info = qmp_query_savevm(NULL);
148+
149+ if (info->has_status) {
150+ monitor_printf(mon, "savevm status: %s\n", info->status);
151+ monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
152+ info->total_time);
153+ } else {
154+ monitor_printf(mon, "savevm status: not running\n");
155+ }
156+ if (info->has_bytes) {
157+ monitor_printf(mon, "Bytes saved: %"PRIu64"\n", info->bytes);
158+ }
159+ if (info->has_error) {
160+ monitor_printf(mon, "Error: %s\n", info->error);
161+ }
162+}
b08067ba
DM
163Index: new/hmp.h
164===================================================================
24bb7da3
DM
165--- new.orig/hmp.h 2014-11-20 09:13:01.000000000 +0100
166+++ new/hmp.h 2014-11-20 09:16:47.000000000 +0100
fbe817e2 167@@ -26,6 +26,7 @@
92bf040c
DM
168 void hmp_info_uuid(Monitor *mon, const QDict *qdict);
169 void hmp_info_chardev(Monitor *mon, const QDict *qdict);
170 void hmp_info_mice(Monitor *mon, const QDict *qdict);
171+void hmp_info_savevm(Monitor *mon, const QDict *qdict);
172 void hmp_info_migrate(Monitor *mon, const QDict *qdict);
173 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
174 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict);
fbe817e2 175@@ -85,6 +86,10 @@
cd983153
DM
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);
1f416f3a 179+void hmp_savevm_start(Monitor *mon, const QDict *qdict);
cd983153
DM
180+void hmp_snapshot_drive(Monitor *mon, const QDict *qdict);
181+void hmp_delete_drive_snapshot(Monitor *mon, const QDict *qdict);
1f416f3a 182+void hmp_savevm_end(Monitor *mon, const QDict *qdict);
e96de165
DM
183 void hmp_send_key(Monitor *mon, const QDict *qdict);
184 void hmp_screen_dump(Monitor *mon, const QDict *qdict);
185 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict);
b08067ba
DM
186Index: new/include/block/block.h
187===================================================================
24bb7da3
DM
188--- new.orig/include/block/block.h 2014-11-20 09:13:01.000000000 +0100
189+++ new/include/block/block.h 2014-11-20 09:16:47.000000000 +0100
190@@ -235,6 +235,7 @@
fb40cdcd 191 int bdrv_get_backing_file_depth(BlockDriverState *bs);
24bb7da3 192 void bdrv_refresh_filename(BlockDriverState *bs);
fb40cdcd
AD
193 int bdrv_truncate(BlockDriverState *bs, int64_t offset);
194+void bdrv_delete(BlockDriverState *bs);
24bb7da3 195 int64_t bdrv_nb_sectors(BlockDriverState *bs);
fb40cdcd
AD
196 int64_t bdrv_getlength(BlockDriverState *bs);
197 int64_t bdrv_get_allocated_file_size(BlockDriverState *bs);
b08067ba
DM
198Index: new/include/sysemu/sysemu.h
199===================================================================
24bb7da3
DM
200--- new.orig/include/sysemu/sysemu.h 2014-11-20 09:13:01.000000000 +0100
201+++ new/include/sysemu/sysemu.h 2014-11-20 09:16:47.000000000 +0100
3fe80761 202@@ -76,16 +76,17 @@
fb40cdcd
AD
203
204 void do_savevm(Monitor *mon, const QDict *qdict);
205 int load_vmstate(const char *name);
206+int load_state_from_blockdev(const char *filename);
207 void do_delvm(Monitor *mon, const QDict *qdict);
208 void do_info_snapshots(Monitor *mon, const QDict *qdict);
209
210 void qemu_announce_self(void);
211
212 bool qemu_savevm_state_blocked(Error **errp);
213-void qemu_savevm_state_begin(QEMUFile *f,
214+int qemu_savevm_state_begin(QEMUFile *f,
215 const MigrationParams *params);
216 int qemu_savevm_state_iterate(QEMUFile *f);
217-void qemu_savevm_state_complete(QEMUFile *f);
218+int qemu_savevm_state_complete(QEMUFile *f);
219 void qemu_savevm_state_cancel(void);
220 uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size);
221 int qemu_loadvm_state(QEMUFile *f);
b08067ba
DM
222Index: new/monitor.c
223===================================================================
24bb7da3
DM
224--- new.orig/monitor.c 2014-11-20 09:13:01.000000000 +0100
225+++ new/monitor.c 2014-11-20 09:16:47.000000000 +0100
3fe80761 226@@ -2876,6 +2876,13 @@
fb40cdcd
AD
227 .mhandler.cmd = hmp_info_migrate_cache_size,
228 },
229 {
230+ .name = "savevm",
231+ .args_type = "",
232+ .params = "",
233+ .help = "show savevm status",
234+ .mhandler.cmd = hmp_info_savevm,
235+ },
236+ {
237 .name = "balloon",
238 .args_type = "",
239 .params = "",
b08067ba
DM
240Index: new/qapi-schema.json
241===================================================================
24bb7da3
DM
242--- new.orig/qapi-schema.json 2014-11-20 09:13:01.000000000 +0100
243+++ new/qapi-schema.json 2014-11-20 09:16:47.000000000 +0100
244@@ -550,6 +550,42 @@
fb40cdcd
AD
245 '*downtime': 'int',
246 '*setup-time': 'int'} }
247
248+
249+# @SaveVMInfo
250+#
251+# Information about current migration process.
252+#
253+# @status: #optional string describing the current savevm status.
254+# This can be 'active', 'completed', 'failed'.
255+# If this field is not returned, no savevm process
256+# has been initiated
257+#
258+# @error: #optional string containing error message is status is failed.
259+#
260+# @total-time: #optional total amount of milliseconds since savevm started.
261+# If savevm has ended, it returns the total save time
262+#
263+# @bytes: #optional total amount of data transfered
264+#
265+# Since: 1.3
266+##
267+{ 'type': 'SaveVMInfo',
268+ 'data': {'*status': 'str', '*error': 'str',
269+ '*total-time': 'int', '*bytes': 'int'} }
270+
271+##
272+# @query-savevm
273+#
274+# Returns information about current savevm process.
275+#
276+# Returns: @SaveVMInfo
277+#
278+# Since: 1.3
279+##
280+{ 'command': 'query-savevm', 'returns': 'SaveVMInfo' }
281+
282+##
283+
284 ##
285 # @query-migrate
286 #
24bb7da3 287@@ -2649,8 +2685,18 @@
fb40cdcd
AD
288 #
289 # Since: 1.2.0
290 ##
291+
292 { 'command': 'query-target', 'returns': 'TargetInfo' }
293
294+{ 'command': 'savevm-start', 'data': { '*statefile': 'str' } }
295+
296+{ 'command': 'snapshot-drive', 'data': { 'device': 'str', 'name': 'str' } }
297+
298+{ 'command': 'delete-drive-snapshot', 'data': { 'device': 'str', 'name': 'str' } }
299+
300+{ 'command': 'savevm-end' }
301+
302+
303 ##
304 # @QKeyCode:
305 #
b08067ba
DM
306Index: new/qemu-options.hx
307===================================================================
24bb7da3
DM
308--- new.orig/qemu-options.hx 2014-11-20 09:13:01.000000000 +0100
309+++ new/qemu-options.hx 2014-11-20 09:16:47.000000000 +0100
310@@ -2975,6 +2975,19 @@
fb40cdcd 311 Start right away with a saved state (@code{loadvm} in monitor)
cd983153 312 ETEXI
fb40cdcd
AD
313
314+DEF("loadstate", HAS_ARG, QEMU_OPTION_loadstate, \
315+ "-loadstate file\n" \
316+ " start right away with a saved state\n",
317+ QEMU_ARCH_ALL)
318+STEXI
319+@item -loadstate @var{file}
320+@findex -loadstate
321+Start right away with a saved state. This option does not rollback
322+disk state like @code{loadvm}, so user must make sure that disk
323+have correct state. @var{file} can be any valid device URL. See the section
324+for "Device URL Syntax" for more information.
325+ETEXI
326+
327 #ifndef _WIN32
328 DEF("daemonize", 0, QEMU_OPTION_daemonize, \
329 "-daemonize daemonize QEMU after initializing\n", QEMU_ARCH_ALL)
b08067ba
DM
330Index: new/qmp-commands.hx
331===================================================================
24bb7da3
DM
332--- new.orig/qmp-commands.hx 2014-11-20 09:13:01.000000000 +0100
333+++ new/qmp-commands.hx 2014-11-20 09:16:47.000000000 +0100
334@@ -3883,3 +3883,34 @@
3fe80761 335 <- { "return": {} }
fb40cdcd
AD
336
337 EQMP
338+
cd983153
DM
339+
340+ {
1f416f3a 341+ .name = "savevm-start",
cd983153 342+ .args_type = "statefile:s?",
fb40cdcd 343+ .mhandler.cmd_new = qmp_marshal_input_savevm_start,
cd983153
DM
344+ },
345+
346+ {
347+ .name = "snapshot-drive",
348+ .args_type = "device:s,name:s",
fb40cdcd 349+ .mhandler.cmd_new = qmp_marshal_input_snapshot_drive,
cd983153
DM
350+ },
351+
352+ {
353+ .name = "delete-drive-snapshot",
354+ .args_type = "device:s,name:s",
fb40cdcd 355+ .mhandler.cmd_new = qmp_marshal_input_delete_drive_snapshot,
cd983153
DM
356+ },
357+
358+ {
1f416f3a 359+ .name = "savevm-end",
cd983153 360+ .args_type = "",
fb40cdcd
AD
361+ .mhandler.cmd_new = qmp_marshal_input_savevm_end,
362+ },
363+
364+ {
365+ .name = "query-savevm",
366+ .args_type = "",
367+ .mhandler.cmd_new = qmp_marshal_input_query_savevm,
cd983153 368+ },
b08067ba
DM
369Index: new/savevm-async.c
370===================================================================
371--- /dev/null 1970-01-01 00:00:00.000000000 +0000
24bb7da3
DM
372+++ new/savevm-async.c 2014-11-20 09:17:48.000000000 +0100
373@@ -0,0 +1,484 @@
fb40cdcd
AD
374+#include "qemu-common.h"
375+#include "qapi/qmp/qerror.h"
376+#include "sysemu/sysemu.h"
cd983153 377+#include "qmp-commands.h"
fb40cdcd
AD
378+#include "qemu-options.h"
379+#include "migration/qemu-file.h"
380+#include "qom/qom-qobject.h"
381+#include "migration/migration.h"
382+#include "block/snapshot.h"
383+#include "block/qapi.h"
384+#include "block/block.h"
385+#include "qemu/timer.h"
cd983153 386+
b84aa9c4 387+/* #define DEBUG_SAVEVM_STATE */
cd983153
DM
388+
389+#ifdef DEBUG_SAVEVM_STATE
390+#define DPRINTF(fmt, ...) \
391+ do { printf("savevm-async: " fmt, ## __VA_ARGS__); } while (0)
392+#else
393+#define DPRINTF(fmt, ...) \
394+ do { } while (0)
395+#endif
396+
397+enum {
398+ SAVE_STATE_DONE,
399+ SAVE_STATE_ERROR,
400+ SAVE_STATE_ACTIVE,
401+ SAVE_STATE_COMPLETED,
402+};
403+
fb40cdcd 404+
cd983153
DM
405+static struct SnapshotState {
406+ BlockDriverState *bs;
407+ size_t bs_pos;
408+ int state;
409+ Error *error;
fbe817e2 410+ Error *blocker;
cd983153
DM
411+ int saved_vm_running;
412+ QEMUFile *file;
413+ int64_t total_time;
414+} snap_state;
415+
416+SaveVMInfo *qmp_query_savevm(Error **errp)
417+{
418+ SaveVMInfo *info = g_malloc0(sizeof(*info));
419+ struct SnapshotState *s = &snap_state;
420+
421+ if (s->state != SAVE_STATE_DONE) {
422+ info->has_bytes = true;
423+ info->bytes = s->bs_pos;
424+ switch (s->state) {
425+ case SAVE_STATE_ERROR:
426+ info->has_status = true;
427+ info->status = g_strdup("failed");
428+ info->has_total_time = true;
429+ info->total_time = s->total_time;
430+ if (s->error) {
431+ info->has_error = true;
432+ info->error = g_strdup(error_get_pretty(s->error));
433+ }
434+ break;
435+ case SAVE_STATE_ACTIVE:
436+ info->has_status = true;
437+ info->status = g_strdup("active");
438+ info->has_total_time = true;
fb40cdcd 439+ info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
cd983153
DM
440+ - s->total_time;
441+ break;
442+ case SAVE_STATE_COMPLETED:
443+ info->has_status = true;
444+ info->status = g_strdup("completed");
445+ info->has_total_time = true;
446+ info->total_time = s->total_time;
447+ break;
448+ }
449+ }
450+
451+ return info;
452+}
453+
454+static int save_snapshot_cleanup(void)
455+{
456+ int ret = 0;
457+
458+ DPRINTF("save_snapshot_cleanup\n");
459+
fb40cdcd 460+ snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
cd983153
DM
461+ snap_state.total_time;
462+
463+ if (snap_state.file) {
464+ ret = qemu_fclose(snap_state.file);
465+ }
466+
467+ if (snap_state.bs) {
b84aa9c4
DM
468+ /* try to truncate, but ignore errors (will fail on block devices).
469+ * note: bdrv_read() need whole blocks, so we round up
470+ */
cd983153
DM
471+ size_t size = (snap_state.bs_pos + BDRV_SECTOR_SIZE) & BDRV_SECTOR_MASK;
472+ bdrv_truncate(snap_state.bs, size);
fbe817e2
DM
473+ bdrv_op_unblock_all(snap_state.bs, snap_state.blocker);
474+ error_free(snap_state.blocker);
475+ snap_state.blocker = NULL;
fb40cdcd 476+ bdrv_unref(snap_state.bs);
cd983153
DM
477+ snap_state.bs = NULL;
478+ }
479+
480+ return ret;
481+}
482+
483+static void save_snapshot_error(const char *fmt, ...)
484+{
485+ va_list ap;
486+ char *msg;
487+
488+ va_start(ap, fmt);
489+ msg = g_strdup_vprintf(fmt, ap);
490+ va_end(ap);
491+
492+ DPRINTF("save_snapshot_error: %s\n", msg);
493+
494+ if (!snap_state.error) {
495+ error_set(&snap_state.error, ERROR_CLASS_GENERIC_ERROR, "%s", msg);
496+ }
497+
498+ g_free (msg);
499+
500+ snap_state.state = SAVE_STATE_ERROR;
501+
502+ save_snapshot_cleanup();
503+}
504+
505+static void save_snapshot_completed(void)
506+{
507+ DPRINTF("save_snapshot_completed\n");
508+
509+ if (save_snapshot_cleanup() < 0) {
510+ snap_state.state = SAVE_STATE_ERROR;
511+ } else {
512+ snap_state.state = SAVE_STATE_COMPLETED;
513+ }
514+}
515+
516+static int block_state_close(void *opaque)
517+{
518+ snap_state.file = NULL;
519+ return bdrv_flush(snap_state.bs);
520+}
521+
b84aa9c4
DM
522+static int block_state_put_buffer(void *opaque, const uint8_t *buf,
523+ int64_t pos, int size)
cd983153
DM
524+{
525+ int ret;
526+
b84aa9c4
DM
527+ assert(pos == snap_state.bs_pos);
528+
cd983153
DM
529+ if ((ret = bdrv_pwrite(snap_state.bs, snap_state.bs_pos, buf, size)) > 0) {
530+ snap_state.bs_pos += ret;
531+ }
532+
533+ return ret;
534+}
535+
b84aa9c4 536+static void process_savevm_co(void *opaque)
cd983153
DM
537+{
538+ int ret;
b84aa9c4
DM
539+ int64_t maxlen;
540+ MigrationParams params = {
541+ .blk = 0,
542+ .shared = 0
543+ };
cd983153 544+
b84aa9c4 545+ snap_state.state = SAVE_STATE_ACTIVE;
cd983153 546+
fb40cdcd 547+ qemu_mutex_unlock_iothread();
b84aa9c4 548+ ret = qemu_savevm_state_begin(snap_state.file, &params);
fb40cdcd
AD
549+ qemu_mutex_lock_iothread();
550+
b84aa9c4
DM
551+ if (ret < 0) {
552+ save_snapshot_error("qemu_savevm_state_begin failed");
cd983153
DM
553+ return;
554+ }
555+
b84aa9c4 556+ while (snap_state.state == SAVE_STATE_ACTIVE) {
92bf040c
DM
557+ uint64_t pending_size;
558+
559+ pending_size = qemu_savevm_state_pending(snap_state.file, 0);
560+
561+ if (pending_size) {
562+ ret = qemu_savevm_state_iterate(snap_state.file);
563+ if (ret < 0) {
564+ save_snapshot_error("qemu_savevm_state_iterate error %d", ret);
565+ break;
566+ }
567+ DPRINTF("savevm inerate pending size %lu ret %d\n", pending_size, ret);
568+ } else {
569+ DPRINTF("done iterating\n");
b84aa9c4
DM
570+ if (runstate_is_running()) {
571+ vm_stop(RUN_STATE_SAVE_VM);
572+ }
573+ DPRINTF("savevm inerate finished\n");
fb40cdcd
AD
574+ qemu_savevm_state_complete(snap_state.file);
575+ DPRINTF("save complete\n");
576+ save_snapshot_completed();
577+ break;
92bf040c
DM
578+ }
579+
580+ /* stop the VM if we get to the end of available space,
581+ * or if pending_size is just a few MB
582+ */
583+ maxlen = bdrv_getlength(snap_state.bs) - 30*1024*1024;
584+ if ((pending_size < 100000) ||
585+ ((snap_state.bs_pos + pending_size) >= maxlen)) {
586+ if (runstate_is_running()) {
587+ vm_stop(RUN_STATE_SAVE_VM);
b84aa9c4
DM
588+ }
589+ }
cd983153
DM
590+ }
591+}
592+
b84aa9c4
DM
593+static const QEMUFileOps block_file_ops = {
594+ .put_buffer = block_state_put_buffer,
595+ .close = block_state_close,
596+};
597+
cd983153 598+
1f416f3a 599+void qmp_savevm_start(bool has_statefile, const char *statefile, Error **errp)
cd983153
DM
600+{
601+ BlockDriver *drv = NULL;
fb40cdcd
AD
602+ Error *local_err = NULL;
603+
cd983153 604+ int bdrv_oflags = BDRV_O_CACHE_WB | BDRV_O_RDWR;
cd983153
DM
605+ int ret;
606+
607+ if (snap_state.state != SAVE_STATE_DONE) {
608+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
609+ "VM snapshot already started\n");
610+ return;
611+ }
612+
613+ /* initialize snapshot info */
614+ snap_state.saved_vm_running = runstate_is_running();
615+ snap_state.bs_pos = 0;
fb40cdcd 616+ snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
fbe817e2 617+ snap_state.blocker = NULL;
cd983153
DM
618+
619+ if (snap_state.error) {
620+ error_free(snap_state.error);
621+ snap_state.error = NULL;
622+ }
623+
cd983153 624+ if (!has_statefile) {
b84aa9c4 625+ vm_stop(RUN_STATE_SAVE_VM);
cd983153
DM
626+ snap_state.state = SAVE_STATE_COMPLETED;
627+ return;
628+ }
629+
630+ if (qemu_savevm_state_blocked(errp)) {
631+ return;
632+ }
633+
634+ /* Open the image */
24bb7da3
DM
635+ snap_state.bs = bdrv_new();
636+
62d638ff 637+ ret = bdrv_open(&snap_state.bs, statefile, NULL, NULL, bdrv_oflags, drv, &local_err);
cd983153 638+ if (ret < 0) {
fb40cdcd 639+ error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
cd983153
DM
640+ goto restart;
641+ }
642+
b84aa9c4 643+ snap_state.file = qemu_fopen_ops(&snap_state, &block_file_ops);
cd983153
DM
644+
645+ if (!snap_state.file) {
fb40cdcd 646+ error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
cd983153
DM
647+ goto restart;
648+ }
649+
fb40cdcd 650+
fbe817e2
DM
651+ error_setg(&snap_state.blocker, "block device is in use by savevm");
652+ bdrv_op_block_all(snap_state.bs, snap_state.blocker);
fb40cdcd 653+
b84aa9c4
DM
654+ Coroutine *co = qemu_coroutine_create(process_savevm_co);
655+ qemu_coroutine_enter(co, NULL);
cd983153
DM
656+
657+ return;
658+
659+restart:
660+
661+ save_snapshot_error("setup failed");
662+
663+ if (snap_state.saved_vm_running) {
664+ vm_start();
665+ }
666+}
667+
1f416f3a 668+void qmp_savevm_end(Error **errp)
cd983153
DM
669+{
670+ if (snap_state.state == SAVE_STATE_DONE) {
671+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
672+ "VM snapshot not started\n");
673+ return;
674+ }
675+
676+ if (snap_state.saved_vm_running) {
677+ vm_start();
678+ }
679+
680+ snap_state.state = SAVE_STATE_DONE;
681+}
682+
683+void qmp_snapshot_drive(const char *device, const char *name, Error **errp)
684+{
685+ BlockDriverState *bs;
686+ QEMUSnapshotInfo sn1, *sn = &sn1;
687+ int ret;
688+#ifdef _WIN32
689+ struct _timeb tb;
690+#else
691+ struct timeval tv;
692+#endif
693+
694+ if (snap_state.state != SAVE_STATE_COMPLETED) {
695+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
696+ "VM snapshot not ready/started\n");
697+ return;
698+ }
699+
700+ bs = bdrv_find(device);
701+ if (!bs) {
702+ error_set(errp, QERR_DEVICE_NOT_FOUND, device);
703+ return;
704+ }
705+
706+ if (!bdrv_is_inserted(bs)) {
707+ error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
708+ return;
709+ }
710+
711+ if (bdrv_is_read_only(bs)) {
712+ error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
713+ return;
714+ }
715+
716+ if (!bdrv_can_snapshot(bs)) {
fbe817e2 717+ error_set(errp, QERR_UNSUPPORTED);
cd983153
DM
718+ return;
719+ }
720+
721+ if (bdrv_snapshot_find(bs, sn, name) >= 0) {
722+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
723+ "snapshot '%s' already exists", name);
724+ return;
725+ }
726+
727+ sn = &sn1;
728+ memset(sn, 0, sizeof(*sn));
729+
730+#ifdef _WIN32
731+ _ftime(&tb);
732+ sn->date_sec = tb.time;
733+ sn->date_nsec = tb.millitm * 1000000;
734+#else
735+ gettimeofday(&tv, NULL);
736+ sn->date_sec = tv.tv_sec;
737+ sn->date_nsec = tv.tv_usec * 1000;
738+#endif
fb40cdcd 739+ sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
cd983153
DM
740+
741+ pstrcpy(sn->name, sizeof(sn->name), name);
742+
743+ sn->vm_state_size = 0; /* do not save state */
744+
745+ ret = bdrv_snapshot_create(bs, sn);
746+ if (ret < 0) {
747+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
748+ "Error while creating snapshot on '%s'\n", device);
749+ return;
750+ }
751+}
752+
753+void qmp_delete_drive_snapshot(const char *device, const char *name,
754+ Error **errp)
755+{
756+ BlockDriverState *bs;
757+ QEMUSnapshotInfo sn1, *sn = &sn1;
fb40cdcd
AD
758+ Error *local_err = NULL;
759+
cd983153
DM
760+ int ret;
761+
762+ bs = bdrv_find(device);
763+ if (!bs) {
764+ error_set(errp, QERR_DEVICE_NOT_FOUND, device);
765+ return;
766+ }
767+ if (bdrv_is_read_only(bs)) {
768+ error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
769+ return;
770+ }
771+
772+ if (!bdrv_can_snapshot(bs)) {
fbe817e2 773+ error_set(errp, QERR_UNSUPPORTED);
cd983153
DM
774+ return;
775+ }
776+
777+ if (bdrv_snapshot_find(bs, sn, name) < 0) {
778+ /* return success if snapshot does not exists */
779+ return;
780+ }
781+
fb40cdcd 782+ ret = bdrv_snapshot_delete(bs, NULL, name, &local_err);
cd983153
DM
783+ if (ret < 0) {
784+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
785+ "Error while deleting snapshot on '%s'\n", device);
786+ return;
787+ }
788+}
789+
b84aa9c4
DM
790+static int loadstate_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
791+ int size)
cd983153
DM
792+{
793+ BlockDriverState *bs = (BlockDriverState *)opaque;
794+ int64_t maxlen = bdrv_getlength(bs);
795+ if (pos > maxlen) {
796+ return -EIO;
797+ }
798+ if ((pos + size) > maxlen) {
799+ size = maxlen - pos - 1;
800+ }
801+ if (size == 0) {
802+ return 0;
803+ }
804+ return bdrv_pread(bs, pos, buf, size);
805+}
806+
b84aa9c4
DM
807+static const QEMUFileOps loadstate_file_ops = {
808+ .get_buffer = loadstate_get_buffer,
809+};
810+
cd983153
DM
811+int load_state_from_blockdev(const char *filename)
812+{
813+ BlockDriverState *bs = NULL;
814+ BlockDriver *drv = NULL;
fb40cdcd 815+ Error *local_err = NULL;
fbe817e2 816+ Error *blocker = NULL;
fb40cdcd 817+
cd983153
DM
818+ QEMUFile *f;
819+ int ret = -1;
820+
24bb7da3 821+ bs = bdrv_new();
c1de5992 822+ ret = bdrv_open(&bs, filename, NULL, NULL, BDRV_O_CACHE_WB, drv, &local_err);
fbe817e2
DM
823+ error_setg(&blocker, "block device is in use by load state");
824+ bdrv_op_block_all(bs, blocker);
fb40cdcd 825+
cd983153
DM
826+ if (ret < 0) {
827+ error_report("Could not open VM state file");
828+ goto the_end;
829+ }
830+
831+ /* restore the VM state */
b84aa9c4 832+ f = qemu_fopen_ops(bs, &loadstate_file_ops);
cd983153
DM
833+ if (!f) {
834+ error_report("Could not open VM state file");
835+ ret = -EINVAL;
836+ goto the_end;
837+ }
838+
839+ qemu_system_reset(VMRESET_SILENT);
840+ ret = qemu_loadvm_state(f);
841+
842+ qemu_fclose(f);
843+ if (ret < 0) {
844+ error_report("Error %d while loading VM state", ret);
845+ goto the_end;
846+ }
847+
848+ ret = 0;
849+
850+ the_end:
851+ if (bs) {
fbe817e2
DM
852+ bdrv_op_unblock_all(bs, blocker);
853+ error_free(blocker);
fb40cdcd 854+ bdrv_unref(bs);
cd983153
DM
855+ }
856+ return ret;
857+}
b08067ba
DM
858Index: new/savevm.c
859===================================================================
24bb7da3
DM
860--- new.orig/savevm.c 2014-11-20 09:13:01.000000000 +0100
861+++ new/savevm.c 2014-11-20 09:16:47.000000000 +0100
3fe80761 862@@ -596,11 +596,11 @@
fb40cdcd
AD
863 return false;
864 }
cd983153 865
fb40cdcd
AD
866-void qemu_savevm_state_begin(QEMUFile *f,
867+int qemu_savevm_state_begin(QEMUFile *f,
868 const MigrationParams *params)
869 {
870 SaveStateEntry *se;
871- int ret;
872+ int ret = 0;
cd983153 873
c1de5992 874 trace_savevm_state_begin();
fb40cdcd 875 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
3fe80761 876@@ -642,6 +642,7 @@
fb40cdcd
AD
877 break;
878 }
879 }
880+ return ret;
881 }
882
883 /*
3fe80761 884@@ -690,7 +691,7 @@
fb40cdcd
AD
885 return ret;
886 }
887
888-void qemu_savevm_state_complete(QEMUFile *f)
889+int qemu_savevm_state_complete(QEMUFile *f)
890 {
891 SaveStateEntry *se;
892 int ret;
3fe80761 893@@ -717,7 +718,7 @@
c1de5992 894 trace_savevm_section_end(se->idstr, se->section_id);
fb40cdcd
AD
895 if (ret < 0) {
896 qemu_file_set_error(f, ret);
897- return;
898+ return ret;
899 }
900 }
901
3fe80761 902@@ -746,6 +747,7 @@
fb40cdcd
AD
903
904 qemu_put_byte(f, QEMU_VM_EOF);
905 qemu_fflush(f);
906+ return qemu_file_get_error(f);
907 }
908
909 uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
b08067ba
DM
910Index: new/vl.c
911===================================================================
24bb7da3
DM
912--- new.orig/vl.c 2014-11-20 09:13:01.000000000 +0100
913+++ new/vl.c 2014-11-20 09:16:47.000000000 +0100
914@@ -2760,6 +2760,7 @@
cd983153
DM
915 int optind;
916 const char *optarg;
917 const char *loadvm = NULL;
918+ const char *loadstate = NULL;
c1de5992 919 MachineClass *machine_class;
cd983153 920 const char *cpu_model;
fbe817e2 921 const char *vga_model = NULL;
24bb7da3 922@@ -3457,6 +3458,9 @@
cd983153
DM
923 case QEMU_OPTION_loadvm:
924 loadvm = optarg;
925 break;
926+ case QEMU_OPTION_loadstate:
927+ loadstate = optarg;
928+ break;
929 case QEMU_OPTION_full_screen:
930 full_screen = 1;
931 break;
24bb7da3 932@@ -4428,6 +4432,10 @@
cd983153
DM
933 if (load_vmstate(loadvm) < 0) {
934 autostart = 0;
935 }
936+ } else if (loadstate) {
937+ if (load_state_from_blockdev(loadstate) < 0) {
938+ autostart = 0;
939+ }
940 }
941
24bb7da3 942 qdev_prop_check_globals();