]> git.proxmox.com Git - pve-qemu-kvm.git/blob - debian/patches/internal-snapshot-async.patch
savevm-async: move global_state_store to the end
[pve-qemu-kvm.git] / debian / patches / internal-snapshot-async.patch
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
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
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
28 @@ -53,6 +53,7 @@
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
33
34 common-obj-$(CONFIG_SPICE) += spice-qemu-char.o
35
36 Index: new/block.c
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
40 @@ -2202,7 +2202,7 @@
41 bdrv_attach_child(bs_top, bs_new, &child_backing);
42 }
43
44 -static void bdrv_delete(BlockDriverState *bs)
45 +void bdrv_delete(BlockDriverState *bs)
46 {
47 assert(!bs->job);
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
53 @@ -1809,6 +1809,8 @@
54 show current migration parameters
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
62 @@ -1857,3 +1859,35 @@
63 STEXI
64 @end table
65 ETEXI
66 +
67 + {
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,
73 + },
74 +
75 + {
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,
81 + },
82 +
83 + {
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,
89 + },
90 +
91 + {
92 + .name = "savevm-end",
93 + .args_type = "",
94 + .params = "",
95 + .help = "Resume VM after snaphot.",
96 + .mhandler.cmd = hmp_savevm_end,
97 + },
98 Index: new/hmp.c
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 @@
103
104 qapi_free_MemoryDeviceInfoList(info_list);
105 }
106 +
107 +void hmp_savevm_start(Monitor *mon, const QDict *qdict)
108 +{
109 + Error *errp = NULL;
110 + const char *statefile = qdict_get_try_str(qdict, "statefile");
111 +
112 + qmp_savevm_start(statefile != NULL, statefile, &errp);
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 +
136 +void hmp_savevm_end(Monitor *mon, const QDict *qdict)
137 +{
138 + Error *errp = NULL;
139 +
140 + qmp_savevm_end(&errp);
141 + hmp_handle_error(mon, &errp);
142 +}
143 +
144 +void hmp_info_savevm(Monitor *mon, const QDict *qdict)
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 +}
163
164 void hmp_qom_list(Monitor *mon, const QDict *qdict)
165 {
166 Index: new/hmp.h
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
170 @@ -26,6 +26,7 @@
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);
178 @@ -85,6 +86,10 @@
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
193 @@ -235,6 +235,7 @@
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
205 @@ -76,17 +76,18 @@
206
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);
212
213 void qemu_announce_self(void);
214
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);
226 Index: new/monitor.c
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,
232 },
233 {
234 + .name = "savevm",
235 + .args_type = "",
236 + .params = "",
237 + .help = "show savevm status",
238 + .mhandler.cmd = hmp_info_savevm,
239 + },
240 + {
241 .name = "balloon",
242 .args_type = "",
243 .params = "",
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
248 @@ -550,6 +550,42 @@
249 '*downtime': 'int',
250 '*setup-time': 'int'} }
251
252 +
253 +# @SaveVMInfo
254 +#
255 +# Information about current migration process.
256 +#
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
261 +#
262 +# @error: #optional string containing error message is status is failed.
263 +#
264 +# @total-time: #optional total amount of milliseconds since savevm started.
265 +# If savevm has ended, it returns the total save time
266 +#
267 +# @bytes: #optional total amount of data transfered
268 +#
269 +# Since: 1.3
270 +##
271 +{ 'struct': 'SaveVMInfo',
272 + 'data': {'*status': 'str', '*error': 'str',
273 + '*total-time': 'int', '*bytes': 'int'} }
274 +
275 +##
276 +# @query-savevm
277 +#
278 +# Returns information about current savevm process.
279 +#
280 +# Returns: @SaveVMInfo
281 +#
282 +# Since: 1.3
283 +##
284 +{ 'command': 'query-savevm', 'returns': 'SaveVMInfo' }
285 +
286 +##
287 +
288 ##
289 # @query-migrate
290 #
291 @@ -2649,8 +2685,18 @@
292 #
293 # Since: 1.2.0
294 ##
295 +
296 { 'command': 'query-target', 'returns': 'TargetInfo' }
297
298 +{ 'command': 'savevm-start', 'data': { '*statefile': 'str' } }
299 +
300 +{ 'command': 'snapshot-drive', 'data': { 'device': 'str', 'name': 'str' } }
301 +
302 +{ 'command': 'delete-drive-snapshot', 'data': { 'device': 'str', 'name': 'str' } }
303 +
304 +{ 'command': 'savevm-end' }
305 +
306 +
307 ##
308 # @QKeyCode:
309 #
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)
316 ETEXI
317
318 +DEF("loadstate", HAS_ARG, QEMU_OPTION_loadstate, \
319 + "-loadstate file\n" \
320 + " start right away with a saved state\n",
321 + QEMU_ARCH_ALL)
322 +STEXI
323 +@item -loadstate @var{file}
324 +@findex -loadstate
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.
329 +ETEXI
330 +
331 #ifndef _WIN32
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 @@
339 <- { "return": {} }
340
341 EQMP
342 +
343 + {
344 + .name = "savevm-start",
345 + .args_type = "statefile:s?",
346 + .mhandler.cmd_new = qmp_marshal_input_savevm_start,
347 + },
348 +
349 + {
350 + .name = "snapshot-drive",
351 + .args_type = "device:s,name:s",
352 + .mhandler.cmd_new = qmp_marshal_input_snapshot_drive,
353 + },
354 +
355 + {
356 + .name = "delete-drive-snapshot",
357 + .args_type = "device:s,name:s",
358 + .mhandler.cmd_new = qmp_marshal_input_delete_drive_snapshot,
359 + },
360 +
361 + {
362 + .name = "savevm-end",
363 + .args_type = "",
364 + .mhandler.cmd_new = qmp_marshal_input_savevm_end,
365 + },
366 +
367 + {
368 + .name = "query-savevm",
369 + .args_type = "",
370 + .mhandler.cmd_new = qmp_marshal_input_query_savevm,
371 + },
372
373 {
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
379 @@ -0,0 +1,518 @@
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 +
395 +/* #define DEBUG_SAVEVM_STATE */
396 +
397 +#ifdef DEBUG_SAVEVM_STATE
398 +#define DPRINTF(fmt, ...) \
399 + do { printf("savevm-async: " fmt, ## __VA_ARGS__); } while (0)
400 +#else
401 +#define DPRINTF(fmt, ...) \
402 + do { } while (0)
403 +#endif
404 +
405 +enum {
406 + SAVE_STATE_DONE,
407 + SAVE_STATE_ERROR,
408 + SAVE_STATE_ACTIVE,
409 + SAVE_STATE_COMPLETED,
410 + SAVE_STATE_CANCELLED
411 +};
412 +
413 +
414 +static struct SnapshotState {
415 + BlockDriverState *bs;
416 + size_t bs_pos;
417 + int state;
418 + Error *error;
419 + Error *blocker;
420 + int saved_vm_running;
421 + QEMUFile *file;
422 + int64_t total_time;
423 +} snap_state;
424 +
425 +SaveVMInfo *qmp_query_savevm(Error **errp)
426 +{
427 + SaveVMInfo *info = g_malloc0(sizeof(*info));
428 + struct SnapshotState *s = &snap_state;
429 +
430 + if (s->state != SAVE_STATE_DONE) {
431 + info->has_bytes = true;
432 + info->bytes = s->bs_pos;
433 + switch (s->state) {
434 + case SAVE_STATE_ERROR:
435 + info->has_status = true;
436 + info->status = g_strdup("failed");
437 + info->has_total_time = true;
438 + info->total_time = s->total_time;
439 + if (s->error) {
440 + info->has_error = true;
441 + info->error = g_strdup(error_get_pretty(s->error));
442 + }
443 + break;
444 + case SAVE_STATE_ACTIVE:
445 + info->has_status = true;
446 + info->status = g_strdup("active");
447 + info->has_total_time = true;
448 + info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
449 + - s->total_time;
450 + break;
451 + case SAVE_STATE_COMPLETED:
452 + info->has_status = true;
453 + info->status = g_strdup("completed");
454 + info->has_total_time = true;
455 + info->total_time = s->total_time;
456 + break;
457 + }
458 + }
459 +
460 + return info;
461 +}
462 +
463 +static int save_snapshot_cleanup(void)
464 +{
465 + int ret = 0;
466 +
467 + DPRINTF("save_snapshot_cleanup\n");
468 +
469 + snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
470 + snap_state.total_time;
471 +
472 + if (snap_state.file) {
473 + ret = qemu_fclose(snap_state.file);
474 + }
475 +
476 + if (snap_state.bs) {
477 + /* try to truncate, but ignore errors (will fail on block devices).
478 + * note: bdrv_read() need whole blocks, so we round up
479 + */
480 + size_t size = (snap_state.bs_pos + BDRV_SECTOR_SIZE) & BDRV_SECTOR_MASK;
481 + bdrv_truncate(snap_state.bs, size);
482 + bdrv_op_unblock_all(snap_state.bs, snap_state.blocker);
483 + error_free(snap_state.blocker);
484 + snap_state.blocker = NULL;
485 + bdrv_unref(snap_state.bs);
486 + snap_state.bs = NULL;
487 + }
488 +
489 + return ret;
490 +}
491 +
492 +static void save_snapshot_error(const char *fmt, ...)
493 +{
494 + va_list ap;
495 + char *msg;
496 +
497 + va_start(ap, fmt);
498 + msg = g_strdup_vprintf(fmt, ap);
499 + va_end(ap);
500 +
501 + DPRINTF("save_snapshot_error: %s\n", msg);
502 +
503 + if (!snap_state.error) {
504 + error_set(&snap_state.error, ERROR_CLASS_GENERIC_ERROR, "%s", msg);
505 + }
506 +
507 + g_free (msg);
508 +
509 + snap_state.state = SAVE_STATE_ERROR;
510 +
511 + save_snapshot_cleanup();
512 +}
513 +
514 +static void save_snapshot_completed(void)
515 +{
516 + DPRINTF("save_snapshot_completed\n");
517 +
518 + if (save_snapshot_cleanup() < 0) {
519 + snap_state.state = SAVE_STATE_ERROR;
520 + } else {
521 + snap_state.state = SAVE_STATE_COMPLETED;
522 + }
523 +}
524 +
525 +static int block_state_close(void *opaque)
526 +{
527 + snap_state.file = NULL;
528 + return bdrv_flush(snap_state.bs);
529 +}
530 +
531 +static int block_state_put_buffer(void *opaque, const uint8_t *buf,
532 + int64_t pos, int size)
533 +{
534 + int ret;
535 +
536 + assert(pos == snap_state.bs_pos);
537 +
538 + if ((ret = bdrv_pwrite(snap_state.bs, snap_state.bs_pos, buf, size)) > 0) {
539 + snap_state.bs_pos += ret;
540 + }
541 +
542 + return ret;
543 +}
544 +
545 +static int store_and_stop(void) {
546 + if (global_state_store()) {
547 + save_snapshot_error("Error saving global state");
548 + return 1;
549 + }
550 + if (runstate_is_running()) {
551 + vm_stop(RUN_STATE_SAVE_VM);
552 + }
553 + return 0;
554 +}
555 +
556 +static void process_savevm_co(void *opaque)
557 +{
558 + int ret;
559 + int64_t maxlen;
560 + MigrationParams params = {
561 + .blk = 0,
562 + .shared = 0
563 + };
564 +
565 + snap_state.state = SAVE_STATE_ACTIVE;
566 +
567 + qemu_mutex_unlock_iothread();
568 + qemu_savevm_state_header(snap_state.file);
569 + ret = qemu_savevm_state_begin(snap_state.file, &params);
570 + qemu_mutex_lock_iothread();
571 +
572 + if (ret < 0) {
573 + save_snapshot_error("qemu_savevm_state_begin failed");
574 + return;
575 + }
576 +
577 + while (snap_state.state == SAVE_STATE_ACTIVE) {
578 + uint64_t pending_size;
579 +
580 + pending_size = qemu_savevm_state_pending(snap_state.file, 0);
581 +
582 + if (pending_size) {
583 + ret = qemu_savevm_state_iterate(snap_state.file);
584 + if (ret < 0) {
585 + save_snapshot_error("qemu_savevm_state_iterate error %d", ret);
586 + break;
587 + }
588 + DPRINTF("savevm inerate pending size %lu ret %d\n", pending_size, ret);
589 + } else {
590 + DPRINTF("done iterating\n");
591 + if (store_and_stop())
592 + break;
593 + DPRINTF("savevm inerate finished\n");
594 + qemu_savevm_state_complete(snap_state.file);
595 + DPRINTF("save complete\n");
596 + save_snapshot_completed();
597 + break;
598 + }
599 +
600 + /* stop the VM if we get to the end of available space,
601 + * or if pending_size is just a few MB
602 + */
603 + maxlen = bdrv_getlength(snap_state.bs) - 30*1024*1024;
604 + if ((pending_size < 100000) ||
605 + ((snap_state.bs_pos + pending_size) >= maxlen)) {
606 + if (store_and_stop())
607 + break;
608 + }
609 + }
610 +
611 + if(snap_state.state == SAVE_STATE_CANCELLED) {
612 + save_snapshot_completed();
613 + Error *errp = NULL;
614 + qmp_savevm_end(&errp);
615 + }
616 +
617 +}
618 +
619 +static const QEMUFileOps block_file_ops = {
620 + .put_buffer = block_state_put_buffer,
621 + .close = block_state_close,
622 +};
623 +
624 +
625 +void qmp_savevm_start(bool has_statefile, const char *statefile, Error **errp)
626 +{
627 + BlockDriver *drv = NULL;
628 + Error *local_err = NULL;
629 +
630 + int bdrv_oflags = BDRV_O_CACHE_WB | BDRV_O_RDWR;
631 + int ret;
632 +
633 + if (snap_state.state != SAVE_STATE_DONE) {
634 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
635 + "VM snapshot already started\n");
636 + return;
637 + }
638 +
639 + /* initialize snapshot info */
640 + snap_state.saved_vm_running = runstate_is_running();
641 + snap_state.bs_pos = 0;
642 + snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
643 + snap_state.blocker = NULL;
644 +
645 + if (snap_state.error) {
646 + error_free(snap_state.error);
647 + snap_state.error = NULL;
648 + }
649 +
650 + if (!has_statefile) {
651 + vm_stop(RUN_STATE_SAVE_VM);
652 + snap_state.state = SAVE_STATE_COMPLETED;
653 + return;
654 + }
655 +
656 + if (qemu_savevm_state_blocked(errp)) {
657 + return;
658 + }
659 +
660 + /* Open the image */
661 + snap_state.bs = bdrv_new();
662 +
663 + ret = bdrv_open(&snap_state.bs, statefile, NULL, NULL, bdrv_oflags, drv, &local_err);
664 + if (ret < 0) {
665 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
666 + goto restart;
667 + }
668 +
669 + snap_state.file = qemu_fopen_ops(&snap_state, &block_file_ops);
670 +
671 + if (!snap_state.file) {
672 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
673 + goto restart;
674 + }
675 +
676 +
677 + error_setg(&snap_state.blocker, "block device is in use by savevm");
678 + bdrv_op_block_all(snap_state.bs, snap_state.blocker);
679 +
680 + Coroutine *co = qemu_coroutine_create(process_savevm_co);
681 + qemu_coroutine_enter(co, NULL);
682 +
683 + return;
684 +
685 +restart:
686 +
687 + save_snapshot_error("setup failed");
688 +
689 + if (snap_state.saved_vm_running) {
690 + vm_start();
691 + }
692 +}
693 +
694 +void qmp_savevm_end(Error **errp)
695 +{
696 + if (snap_state.state == SAVE_STATE_DONE) {
697 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
698 + "VM snapshot not started\n");
699 + return;
700 + }
701 +
702 + if (snap_state.state == SAVE_STATE_ACTIVE) {
703 + snap_state.state = SAVE_STATE_CANCELLED;
704 + return;
705 + }
706 +
707 + if (snap_state.saved_vm_running) {
708 + vm_start();
709 + }
710 +
711 + snap_state.state = SAVE_STATE_DONE;
712 +}
713 +
714 +void qmp_snapshot_drive(const char *device, const char *name, Error **errp)
715 +{
716 + BlockBackend *blk;
717 + BlockDriverState *bs;
718 + QEMUSnapshotInfo sn1, *sn = &sn1;
719 + int ret;
720 +#ifdef _WIN32
721 + struct _timeb tb;
722 +#else
723 + struct timeval tv;
724 +#endif
725 +
726 + if (snap_state.state != SAVE_STATE_COMPLETED) {
727 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
728 + "VM snapshot not ready/started\n");
729 + return;
730 + }
731 +
732 + blk = blk_by_name(device);
733 + if (!blk) {
734 + error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
735 + "Device '%s' not found", device);
736 + return;
737 + }
738 +
739 + bs = blk_bs(blk);
740 + if (!bdrv_is_inserted(bs)) {
741 + error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
742 + return;
743 + }
744 +
745 + if (bdrv_is_read_only(bs)) {
746 + error_setg(errp, "Node '%s' is read only", device);
747 + return;
748 + }
749 +
750 + if (!bdrv_can_snapshot(bs)) {
751 + error_setg(errp, QERR_UNSUPPORTED);
752 + return;
753 + }
754 +
755 + if (bdrv_snapshot_find(bs, sn, name) >= 0) {
756 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
757 + "snapshot '%s' already exists", name);
758 + return;
759 + }
760 +
761 + sn = &sn1;
762 + memset(sn, 0, sizeof(*sn));
763 +
764 +#ifdef _WIN32
765 + _ftime(&tb);
766 + sn->date_sec = tb.time;
767 + sn->date_nsec = tb.millitm * 1000000;
768 +#else
769 + gettimeofday(&tv, NULL);
770 + sn->date_sec = tv.tv_sec;
771 + sn->date_nsec = tv.tv_usec * 1000;
772 +#endif
773 + sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
774 +
775 + pstrcpy(sn->name, sizeof(sn->name), name);
776 +
777 + sn->vm_state_size = 0; /* do not save state */
778 +
779 + ret = bdrv_snapshot_create(bs, sn);
780 + if (ret < 0) {
781 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
782 + "Error while creating snapshot on '%s'\n", device);
783 + return;
784 + }
785 +}
786 +
787 +void qmp_delete_drive_snapshot(const char *device, const char *name,
788 + Error **errp)
789 +{
790 + BlockBackend *blk;
791 + BlockDriverState *bs;
792 + QEMUSnapshotInfo sn1, *sn = &sn1;
793 + Error *local_err = NULL;
794 +
795 + int ret;
796 +
797 + blk = blk_by_name(device);
798 + if (!blk) {
799 + error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
800 + "Device '%s' not found", device);
801 + return;
802 + }
803 +
804 + bs = blk_bs(blk);
805 + if (bdrv_is_read_only(bs)) {
806 + error_setg(errp, "Node '%s' is read only", device);
807 + return;
808 + }
809 +
810 + if (!bdrv_can_snapshot(bs)) {
811 + error_setg(errp, QERR_UNSUPPORTED);
812 + return;
813 + }
814 +
815 + if (bdrv_snapshot_find(bs, sn, name) < 0) {
816 + /* return success if snapshot does not exists */
817 + return;
818 + }
819 +
820 + ret = bdrv_snapshot_delete(bs, NULL, name, &local_err);
821 + if (ret < 0) {
822 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
823 + "Error while deleting snapshot on '%s'\n", device);
824 + return;
825 + }
826 +}
827 +
828 +static int loadstate_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
829 + int size)
830 +{
831 + BlockDriverState *bs = (BlockDriverState *)opaque;
832 + int64_t maxlen = bdrv_getlength(bs);
833 + if (pos > maxlen) {
834 + return -EIO;
835 + }
836 + if ((pos + size) > maxlen) {
837 + size = maxlen - pos - 1;
838 + }
839 + if (size == 0) {
840 + return 0;
841 + }
842 + return bdrv_pread(bs, pos, buf, size);
843 +}
844 +
845 +static const QEMUFileOps loadstate_file_ops = {
846 + .get_buffer = loadstate_get_buffer,
847 +};
848 +
849 +int load_state_from_blockdev(const char *filename)
850 +{
851 + BlockDriverState *bs = NULL;
852 + BlockDriver *drv = NULL;
853 + Error *local_err = NULL;
854 + Error *blocker = NULL;
855 +
856 + QEMUFile *f;
857 + int ret;
858 +
859 + bs = bdrv_new();
860 + ret = bdrv_open(&bs, filename, NULL, NULL, BDRV_O_CACHE_WB, drv, &local_err);
861 + error_setg(&blocker, "block device is in use by load state");
862 + bdrv_op_block_all(bs, blocker);
863 +
864 + if (ret < 0) {
865 + error_report("Could not open VM state file");
866 + goto the_end;
867 + }
868 +
869 + /* restore the VM state */
870 + f = qemu_fopen_ops(bs, &loadstate_file_ops);
871 + if (!f) {
872 + error_report("Could not open VM state file");
873 + ret = -EINVAL;
874 + goto the_end;
875 + }
876 +
877 + qemu_system_reset(VMRESET_SILENT);
878 + migration_incoming_state_new(f);
879 + ret = qemu_loadvm_state(f);
880 +
881 + qemu_fclose(f);
882 + migration_incoming_state_destroy();
883 + if (ret < 0) {
884 + error_report("Error %d while loading VM state", ret);
885 + goto the_end;
886 + }
887 +
888 + ret = 0;
889 +
890 + the_end:
891 + if (bs) {
892 + bdrv_op_unblock_all(bs, blocker);
893 + error_free(blocker);
894 + bdrv_unref(bs);
895 + }
896 + return ret;
897 +}
898 Index: new/savevm.c
899 ===================================================================
900 --- new.orig/migration/savevm.c 2014-11-20 09:13:01.000000000 +0100
901 +++ new/migration/savevm.c 2014-11-20 09:16:47.000000000 +0100
902 @@ -718,11 +718,11 @@
903 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
904 }
905
906 -void qemu_savevm_state_begin(QEMUFile *f,
907 +int qemu_savevm_state_begin(QEMUFile *f,
908 const MigrationParams *params)
909 {
910 SaveStateEntry *se;
911 - int ret;
912 + int ret = 0;
913
914 trace_savevm_state_begin();
915 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
916 @@ -642,6 +642,7 @@
917 break;
918 }
919 }
920 + return ret;
921 }
922
923 /*
924 @@ -690,7 +691,7 @@
925 return !machine->suppress_vmdesc;
926 }
927
928 -void qemu_savevm_state_complete(QEMUFile *f)
929 +int qemu_savevm_state_complete(QEMUFile *f)
930 {
931 QJSON *vmdesc;
932 int vmdesc_len;
933 @@ -838,7 +839,7 @@
934 save_section_footer(f, se);
935 if (ret < 0) {
936 qemu_file_set_error(f, ret);
937 - return;
938 + return ret;
939 }
940 }
941
942 @@ -746,6 +747,7 @@
943 object_unref(OBJECT(vmdesc));
944
945 qemu_fflush(f);
946 + return qemu_file_get_error(f);
947 }
948
949 uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
950 Index: new/vl.c
951 ===================================================================
952 --- new.orig/vl.c 2014-11-20 09:13:01.000000000 +0100
953 +++ new/vl.c 2014-11-20 09:16:47.000000000 +0100
954 @@ -2760,6 +2760,7 @@
955 int optind;
956 const char *optarg;
957 const char *loadvm = NULL;
958 + const char *loadstate = NULL;
959 MachineClass *machine_class;
960 const char *cpu_model;
961 const char *vga_model = NULL;
962 @@ -3457,6 +3458,9 @@
963 case QEMU_OPTION_loadvm:
964 loadvm = optarg;
965 break;
966 + case QEMU_OPTION_loadstate:
967 + loadstate = optarg;
968 + break;
969 case QEMU_OPTION_full_screen:
970 full_screen = 1;
971 break;
972 @@ -4428,6 +4432,10 @@
973 if (load_vmstate(loadvm) < 0) {
974 autostart = 0;
975 }
976 + } else if (loadstate) {
977 + if (load_state_from_blockdev(loadstate) < 0) {
978 + autostart = 0;
979 + }
980 }
981
982 qdev_prop_check_globals();