]> git.proxmox.com Git - pve-qemu.git/blob - debian/patches/pve/0017-PVE-add-savevm-async-for-background-state-snapshots.patch
PVE backup: don't call no_co_wrapper function from coroutine
[pve-qemu.git] / debian / patches / pve / 0017-PVE-add-savevm-async-for-background-state-snapshots.patch
1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Dietmar Maurer <dietmar@proxmox.com>
3 Date: Mon, 6 Apr 2020 12:16:46 +0200
4 Subject: [PATCH] PVE: add savevm-async for background state snapshots
5
6 Put qemu_savevm_state_{header,setup} into the main loop and the rest
7 of the iteration into a coroutine. The former need to lock the
8 iothread (and we can't unlock it in the coroutine), and the latter
9 can't deal with being in a separate thread, so a coroutine it must
10 be.
11
12 Truncate output file at 1024 boundary.
13
14 Do not block the VM and save the state on aborting a snapshot, as the
15 snapshot will be invalid anyway.
16
17 Also, when aborting, wait for the target file to be closed, otherwise a
18 client might run into race-conditions when trying to remove the file
19 still opened by QEMU.
20
21 Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
22 Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
23 Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
24 [improve aborting]
25 Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
26 [FE: further improve aborting
27 adapt to removal of QEMUFileOps
28 improve condition for entering final stage
29 adapt to QAPI and other changes for 8.0]
30 Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
31 ---
32 hmp-commands-info.hx | 13 +
33 hmp-commands.hx | 33 +++
34 include/migration/snapshot.h | 2 +
35 include/monitor/hmp.h | 5 +
36 migration/meson.build | 1 +
37 migration/savevm-async.c | 535 +++++++++++++++++++++++++++++++++++
38 monitor/hmp-cmds.c | 58 ++++
39 qapi/migration.json | 34 +++
40 qapi/misc.json | 32 +++
41 qemu-options.hx | 12 +
42 softmmu/vl.c | 10 +
43 11 files changed, 735 insertions(+)
44 create mode 100644 migration/savevm-async.c
45
46 diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
47 index 47d63d26db..a166bff3d5 100644
48 --- a/hmp-commands-info.hx
49 +++ b/hmp-commands-info.hx
50 @@ -540,6 +540,19 @@ SRST
51 Show current migration parameters.
52 ERST
53
54 + {
55 + .name = "savevm",
56 + .args_type = "",
57 + .params = "",
58 + .help = "show savevm status",
59 + .cmd = hmp_info_savevm,
60 + },
61 +
62 +SRST
63 + ``info savevm``
64 + Show savevm status.
65 +ERST
66 +
67 {
68 .name = "balloon",
69 .args_type = "",
70 diff --git a/hmp-commands.hx b/hmp-commands.hx
71 index bb85ee1d26..b66d7fc4ab 100644
72 --- a/hmp-commands.hx
73 +++ b/hmp-commands.hx
74 @@ -1846,3 +1846,36 @@ SRST
75 List event channels in the guest
76 ERST
77 #endif
78 +
79 + {
80 + .name = "savevm-start",
81 + .args_type = "statefile:s?",
82 + .params = "[statefile]",
83 + .help = "Prepare for snapshot and halt VM. Save VM state to statefile.",
84 + .cmd = hmp_savevm_start,
85 + },
86 +
87 + {
88 + .name = "snapshot-drive",
89 + .args_type = "device:s,name:s",
90 + .params = "device name",
91 + .help = "Create internal snapshot.",
92 + .cmd = hmp_snapshot_drive,
93 + },
94 +
95 + {
96 + .name = "delete-drive-snapshot",
97 + .args_type = "device:s,name:s",
98 + .params = "device name",
99 + .help = "Delete internal snapshot.",
100 + .cmd = hmp_delete_drive_snapshot,
101 + },
102 +
103 + {
104 + .name = "savevm-end",
105 + .args_type = "",
106 + .params = "",
107 + .help = "Resume VM after snaphot.",
108 + .cmd = hmp_savevm_end,
109 + .coroutine = true,
110 + },
111 diff --git a/include/migration/snapshot.h b/include/migration/snapshot.h
112 index e72083b117..c846d37806 100644
113 --- a/include/migration/snapshot.h
114 +++ b/include/migration/snapshot.h
115 @@ -61,4 +61,6 @@ bool delete_snapshot(const char *name,
116 bool has_devices, strList *devices,
117 Error **errp);
118
119 +int load_snapshot_from_blockdev(const char *filename, Error **errp);
120 +
121 #endif
122 diff --git a/include/monitor/hmp.h b/include/monitor/hmp.h
123 index fdb69b7f9c..c012bad741 100644
124 --- a/include/monitor/hmp.h
125 +++ b/include/monitor/hmp.h
126 @@ -28,6 +28,7 @@ void hmp_info_status(Monitor *mon, const QDict *qdict);
127 void hmp_info_uuid(Monitor *mon, const QDict *qdict);
128 void hmp_info_chardev(Monitor *mon, const QDict *qdict);
129 void hmp_info_mice(Monitor *mon, const QDict *qdict);
130 +void hmp_info_savevm(Monitor *mon, const QDict *qdict);
131 void hmp_info_migrate(Monitor *mon, const QDict *qdict);
132 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
133 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict);
134 @@ -94,6 +95,10 @@ void hmp_closefd(Monitor *mon, const QDict *qdict);
135 void hmp_mouse_move(Monitor *mon, const QDict *qdict);
136 void hmp_mouse_button(Monitor *mon, const QDict *qdict);
137 void hmp_mouse_set(Monitor *mon, const QDict *qdict);
138 +void hmp_savevm_start(Monitor *mon, const QDict *qdict);
139 +void hmp_snapshot_drive(Monitor *mon, const QDict *qdict);
140 +void hmp_delete_drive_snapshot(Monitor *mon, const QDict *qdict);
141 +void hmp_savevm_end(Monitor *mon, const QDict *qdict);
142 void hmp_sendkey(Monitor *mon, const QDict *qdict);
143 void coroutine_fn hmp_screendump(Monitor *mon, const QDict *qdict);
144 void hmp_chardev_add(Monitor *mon, const QDict *qdict);
145 diff --git a/migration/meson.build b/migration/meson.build
146 index 8a142fc7a9..a7824b5266 100644
147 --- a/migration/meson.build
148 +++ b/migration/meson.build
149 @@ -25,6 +25,7 @@ softmmu_ss.add(files(
150 'multifd-zlib.c',
151 'postcopy-ram.c',
152 'savevm.c',
153 + 'savevm-async.c',
154 'socket.c',
155 'tls.c',
156 'threadinfo.c',
157 diff --git a/migration/savevm-async.c b/migration/savevm-async.c
158 new file mode 100644
159 index 0000000000..24660af014
160 --- /dev/null
161 +++ b/migration/savevm-async.c
162 @@ -0,0 +1,535 @@
163 +#include "qemu/osdep.h"
164 +#include "migration/channel-savevm-async.h"
165 +#include "migration/migration.h"
166 +#include "migration/savevm.h"
167 +#include "migration/snapshot.h"
168 +#include "migration/global_state.h"
169 +#include "migration/ram.h"
170 +#include "migration/qemu-file.h"
171 +#include "sysemu/sysemu.h"
172 +#include "sysemu/runstate.h"
173 +#include "block/block.h"
174 +#include "sysemu/block-backend.h"
175 +#include "qapi/error.h"
176 +#include "qapi/qmp/qerror.h"
177 +#include "qapi/qmp/qdict.h"
178 +#include "qapi/qapi-commands-migration.h"
179 +#include "qapi/qapi-commands-misc.h"
180 +#include "qapi/qapi-commands-block.h"
181 +#include "qemu/cutils.h"
182 +#include "qemu/timer.h"
183 +#include "qemu/main-loop.h"
184 +#include "qemu/rcu.h"
185 +
186 +/* #define DEBUG_SAVEVM_STATE */
187 +
188 +#ifdef DEBUG_SAVEVM_STATE
189 +#define DPRINTF(fmt, ...) \
190 + do { printf("savevm-async: " fmt, ## __VA_ARGS__); } while (0)
191 +#else
192 +#define DPRINTF(fmt, ...) \
193 + do { } while (0)
194 +#endif
195 +
196 +enum {
197 + SAVE_STATE_DONE,
198 + SAVE_STATE_ERROR,
199 + SAVE_STATE_ACTIVE,
200 + SAVE_STATE_COMPLETED,
201 + SAVE_STATE_CANCELLED
202 +};
203 +
204 +
205 +static struct SnapshotState {
206 + BlockBackend *target;
207 + size_t bs_pos;
208 + int state;
209 + Error *error;
210 + Error *blocker;
211 + int saved_vm_running;
212 + QEMUFile *file;
213 + int64_t total_time;
214 + QEMUBH *finalize_bh;
215 + Coroutine *co;
216 + QemuCoSleep target_close_wait;
217 +} snap_state;
218 +
219 +static bool savevm_aborted(void)
220 +{
221 + return snap_state.state == SAVE_STATE_CANCELLED ||
222 + snap_state.state == SAVE_STATE_ERROR;
223 +}
224 +
225 +SaveVMInfo *qmp_query_savevm(Error **errp)
226 +{
227 + SaveVMInfo *info = g_malloc0(sizeof(*info));
228 + struct SnapshotState *s = &snap_state;
229 +
230 + if (s->state != SAVE_STATE_DONE) {
231 + info->has_bytes = true;
232 + info->bytes = s->bs_pos;
233 + switch (s->state) {
234 + case SAVE_STATE_ERROR:
235 + info->status = g_strdup("failed");
236 + info->has_total_time = true;
237 + info->total_time = s->total_time;
238 + if (s->error) {
239 + info->error = g_strdup(error_get_pretty(s->error));
240 + }
241 + break;
242 + case SAVE_STATE_ACTIVE:
243 + info->status = g_strdup("active");
244 + info->has_total_time = true;
245 + info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
246 + - s->total_time;
247 + break;
248 + case SAVE_STATE_COMPLETED:
249 + info->status = g_strdup("completed");
250 + info->has_total_time = true;
251 + info->total_time = s->total_time;
252 + break;
253 + }
254 + }
255 +
256 + return info;
257 +}
258 +
259 +static int save_snapshot_cleanup(void)
260 +{
261 + int ret = 0;
262 +
263 + DPRINTF("save_snapshot_cleanup\n");
264 +
265 + snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
266 + snap_state.total_time;
267 +
268 + if (snap_state.file) {
269 + ret = qemu_fclose(snap_state.file);
270 + snap_state.file = NULL;
271 + }
272 +
273 + if (snap_state.target) {
274 + if (!savevm_aborted()) {
275 + /* try to truncate, but ignore errors (will fail on block devices).
276 + * note1: bdrv_read() need whole blocks, so we need to round up
277 + * note2: PVE requires 1024 (BDRV_SECTOR_SIZE*2) alignment
278 + */
279 + size_t size = QEMU_ALIGN_UP(snap_state.bs_pos, BDRV_SECTOR_SIZE*2);
280 + blk_truncate(snap_state.target, size, false, PREALLOC_MODE_OFF, 0, NULL);
281 + }
282 + blk_op_unblock_all(snap_state.target, snap_state.blocker);
283 + error_free(snap_state.blocker);
284 + snap_state.blocker = NULL;
285 + blk_unref(snap_state.target);
286 + snap_state.target = NULL;
287 +
288 + qemu_co_sleep_wake(&snap_state.target_close_wait);
289 + }
290 +
291 + return ret;
292 +}
293 +
294 +static void save_snapshot_error(const char *fmt, ...)
295 +{
296 + va_list ap;
297 + char *msg;
298 +
299 + va_start(ap, fmt);
300 + msg = g_strdup_vprintf(fmt, ap);
301 + va_end(ap);
302 +
303 + DPRINTF("save_snapshot_error: %s\n", msg);
304 +
305 + if (!snap_state.error) {
306 + error_set(&snap_state.error, ERROR_CLASS_GENERIC_ERROR, "%s", msg);
307 + }
308 +
309 + g_free (msg);
310 +
311 + snap_state.state = SAVE_STATE_ERROR;
312 +}
313 +
314 +static void process_savevm_finalize(void *opaque)
315 +{
316 + int ret;
317 + AioContext *iohandler_ctx = iohandler_get_aio_context();
318 + MigrationState *ms = migrate_get_current();
319 +
320 + bool aborted = savevm_aborted();
321 +
322 +#ifdef DEBUG_SAVEVM_STATE
323 + int64_t start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
324 +#endif
325 +
326 + qemu_bh_delete(snap_state.finalize_bh);
327 + snap_state.finalize_bh = NULL;
328 + snap_state.co = NULL;
329 +
330 + /* We need to own the target bdrv's context for the following functions,
331 + * so move it back. It can stay in the main context and live out its live
332 + * there, since we're done with it after this method ends anyway.
333 + */
334 + aio_context_acquire(iohandler_ctx);
335 + blk_set_aio_context(snap_state.target, qemu_get_aio_context(), NULL);
336 + aio_context_release(iohandler_ctx);
337 +
338 + ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
339 + if (ret < 0) {
340 + save_snapshot_error("vm_stop_force_state error %d", ret);
341 + }
342 +
343 + if (!aborted) {
344 + /* skip state saving if we aborted, snapshot will be invalid anyway */
345 + (void)qemu_savevm_state_complete_precopy(snap_state.file, false, false);
346 + ret = qemu_file_get_error(snap_state.file);
347 + if (ret < 0) {
348 + save_snapshot_error("qemu_savevm_state_complete_precopy error %d", ret);
349 + }
350 + }
351 +
352 + DPRINTF("state saving complete\n");
353 + DPRINTF("timing: process_savevm_finalize (state saving) took %ld ms\n",
354 + qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - start_time);
355 +
356 + /* clear migration state */
357 + migrate_set_state(&ms->state, MIGRATION_STATUS_SETUP,
358 + ret || aborted ? MIGRATION_STATUS_FAILED : MIGRATION_STATUS_COMPLETED);
359 + ms->to_dst_file = NULL;
360 +
361 + qemu_savevm_state_cleanup();
362 +
363 + ret = save_snapshot_cleanup();
364 + if (ret < 0) {
365 + save_snapshot_error("save_snapshot_cleanup error %d", ret);
366 + } else if (snap_state.state == SAVE_STATE_ACTIVE) {
367 + snap_state.state = SAVE_STATE_COMPLETED;
368 + } else if (aborted) {
369 + /*
370 + * If there was an error, there's no need to set a new one here.
371 + * If the snapshot was canceled, leave setting the state to
372 + * qmp_savevm_end(), which is waked by save_snapshot_cleanup().
373 + */
374 + } else {
375 + save_snapshot_error("process_savevm_cleanup: invalid state: %d",
376 + snap_state.state);
377 + }
378 + if (snap_state.saved_vm_running) {
379 + vm_start();
380 + snap_state.saved_vm_running = false;
381 + }
382 +
383 + DPRINTF("timing: process_savevm_finalize (full) took %ld ms\n",
384 + qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - start_time);
385 +}
386 +
387 +static void coroutine_fn process_savevm_co(void *opaque)
388 +{
389 + int ret;
390 + int64_t maxlen;
391 + BdrvNextIterator it;
392 + BlockDriverState *bs = NULL;
393 +
394 +#ifdef DEBUG_SAVEVM_STATE
395 + int64_t start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
396 +#endif
397 +
398 + ret = qemu_file_get_error(snap_state.file);
399 + if (ret < 0) {
400 + save_snapshot_error("qemu_savevm_state_setup failed");
401 + return;
402 + }
403 +
404 + while (snap_state.state == SAVE_STATE_ACTIVE) {
405 + uint64_t pending_size, pend_precopy, pend_postcopy;
406 +
407 + /* pending is expected to be called without iothread lock */
408 + qemu_mutex_unlock_iothread();
409 + qemu_savevm_state_pending_exact(&pend_precopy, &pend_postcopy);
410 + qemu_mutex_lock_iothread();
411 +
412 + pending_size = pend_precopy + pend_postcopy;
413 +
414 + /*
415 + * A guest reaching this cutoff is dirtying lots of RAM. It should be
416 + * large enough so that the guest can't dirty this much between the
417 + * check and the guest actually being stopped, but it should be small
418 + * enough to avoid long downtimes for non-hibernation snapshots.
419 + */
420 + maxlen = blk_getlength(snap_state.target) - 100*1024*1024;
421 +
422 + /* Note that there is no progress for pend_postcopy when iterating */
423 + if (pending_size - pend_postcopy > 400000 && snap_state.bs_pos + pending_size < maxlen) {
424 + ret = qemu_savevm_state_iterate(snap_state.file, false);
425 + if (ret < 0) {
426 + save_snapshot_error("qemu_savevm_state_iterate error %d", ret);
427 + break;
428 + }
429 + DPRINTF("savevm iterate pending size %lu ret %d\n", pending_size, ret);
430 + } else {
431 + qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
432 + ret = global_state_store();
433 + if (ret) {
434 + save_snapshot_error("global_state_store error %d", ret);
435 + break;
436 + }
437 +
438 + DPRINTF("savevm iterate complete\n");
439 + break;
440 + }
441 + }
442 +
443 + DPRINTF("timing: process_savevm_co took %ld ms\n",
444 + qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - start_time);
445 +
446 +#ifdef DEBUG_SAVEVM_STATE
447 + int64_t start_time_flush = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
448 +#endif
449 + /* If a drive runs in an IOThread we can flush it async, and only
450 + * need to sync-flush whatever IO happens between now and
451 + * vm_stop_force_state. bdrv_next can only be called from main AioContext,
452 + * so move there now and after every flush.
453 + */
454 + aio_co_reschedule_self(qemu_get_aio_context());
455 + for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
456 + /* target has BDRV_O_NO_FLUSH, no sense calling bdrv_flush on it */
457 + if (bs == blk_bs(snap_state.target)) {
458 + continue;
459 + }
460 +
461 + AioContext *bs_ctx = bdrv_get_aio_context(bs);
462 + if (bs_ctx != qemu_get_aio_context()) {
463 + DPRINTF("savevm: async flushing drive %s\n", bs->filename);
464 + aio_co_reschedule_self(bs_ctx);
465 + bdrv_graph_co_rdlock();
466 + bdrv_flush(bs);
467 + bdrv_graph_co_rdunlock();
468 + aio_co_reschedule_self(qemu_get_aio_context());
469 + }
470 + }
471 +
472 + DPRINTF("timing: async flushing took %ld ms\n",
473 + qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - start_time_flush);
474 +
475 + qemu_bh_schedule(snap_state.finalize_bh);
476 +}
477 +
478 +void qmp_savevm_start(const char *statefile, Error **errp)
479 +{
480 + Error *local_err = NULL;
481 + MigrationState *ms = migrate_get_current();
482 + AioContext *iohandler_ctx = iohandler_get_aio_context();
483 +
484 + int bdrv_oflags = BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_NO_FLUSH;
485 +
486 + if (snap_state.state != SAVE_STATE_DONE) {
487 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
488 + "VM snapshot already started\n");
489 + return;
490 + }
491 +
492 + if (migration_is_running(ms->state)) {
493 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, QERR_MIGRATION_ACTIVE);
494 + return;
495 + }
496 +
497 + if (migrate_use_block()) {
498 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
499 + "Block migration and snapshots are incompatible");
500 + return;
501 + }
502 +
503 + /* initialize snapshot info */
504 + snap_state.saved_vm_running = runstate_is_running();
505 + snap_state.bs_pos = 0;
506 + snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
507 + snap_state.blocker = NULL;
508 + snap_state.target_close_wait = (QemuCoSleep){ .to_wake = NULL };
509 +
510 + if (snap_state.error) {
511 + error_free(snap_state.error);
512 + snap_state.error = NULL;
513 + }
514 +
515 + if (!statefile) {
516 + vm_stop(RUN_STATE_SAVE_VM);
517 + snap_state.state = SAVE_STATE_COMPLETED;
518 + return;
519 + }
520 +
521 + if (qemu_savevm_state_blocked(errp)) {
522 + return;
523 + }
524 +
525 + /* Open the image */
526 + QDict *options = NULL;
527 + options = qdict_new();
528 + qdict_put_str(options, "driver", "raw");
529 + snap_state.target = blk_new_open(statefile, NULL, options, bdrv_oflags, &local_err);
530 + if (!snap_state.target) {
531 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
532 + goto restart;
533 + }
534 +
535 + QIOChannel *ioc = QIO_CHANNEL(qio_channel_savevm_async_new(snap_state.target,
536 + &snap_state.bs_pos));
537 + snap_state.file = qemu_file_new_output(ioc);
538 +
539 + if (!snap_state.file) {
540 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
541 + goto restart;
542 + }
543 +
544 + /*
545 + * qemu_savevm_* paths use migration code and expect a migration state.
546 + * State is cleared in process_savevm_co, but has to be initialized
547 + * here (blocking main thread, from QMP) to avoid race conditions.
548 + */
549 + migrate_init(ms);
550 + memset(&ram_counters, 0, sizeof(ram_counters));
551 + ms->to_dst_file = snap_state.file;
552 +
553 + error_setg(&snap_state.blocker, "block device is in use by savevm");
554 + blk_op_block_all(snap_state.target, snap_state.blocker);
555 +
556 + snap_state.state = SAVE_STATE_ACTIVE;
557 + snap_state.finalize_bh = qemu_bh_new(process_savevm_finalize, &snap_state);
558 + snap_state.co = qemu_coroutine_create(&process_savevm_co, NULL);
559 + qemu_mutex_unlock_iothread();
560 + qemu_savevm_state_header(snap_state.file);
561 + qemu_savevm_state_setup(snap_state.file);
562 + qemu_mutex_lock_iothread();
563 +
564 + /* Async processing from here on out happens in iohandler context, so let
565 + * the target bdrv have its home there.
566 + */
567 + blk_set_aio_context(snap_state.target, iohandler_ctx, &local_err);
568 +
569 + aio_co_schedule(iohandler_ctx, snap_state.co);
570 +
571 + return;
572 +
573 +restart:
574 +
575 + save_snapshot_error("setup failed");
576 +
577 + if (snap_state.saved_vm_running) {
578 + vm_start();
579 + snap_state.saved_vm_running = false;
580 + }
581 +}
582 +
583 +void coroutine_fn qmp_savevm_end(Error **errp)
584 +{
585 + int64_t timeout;
586 +
587 + if (snap_state.state == SAVE_STATE_DONE) {
588 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
589 + "VM snapshot not started\n");
590 + return;
591 + }
592 +
593 + if (snap_state.state == SAVE_STATE_ACTIVE) {
594 + snap_state.state = SAVE_STATE_CANCELLED;
595 + goto wait_for_close;
596 + }
597 +
598 + if (snap_state.saved_vm_running) {
599 + vm_start();
600 + snap_state.saved_vm_running = false;
601 + }
602 +
603 + snap_state.state = SAVE_STATE_DONE;
604 +
605 +wait_for_close:
606 + if (!snap_state.target) {
607 + DPRINTF("savevm-end: no target file open\n");
608 + return;
609 + }
610 +
611 + /* wait until cleanup is done before returning, this ensures that after this
612 + * call exits the statefile will be closed and can be removed immediately */
613 + DPRINTF("savevm-end: waiting for cleanup\n");
614 + timeout = 30L * 1000 * 1000 * 1000;
615 + qemu_co_sleep_ns_wakeable(&snap_state.target_close_wait,
616 + QEMU_CLOCK_REALTIME, timeout);
617 + if (snap_state.target) {
618 + save_snapshot_error("timeout waiting for target file close in "
619 + "qmp_savevm_end");
620 + /* we cannot assume the snapshot finished in this case, so leave the
621 + * state alone - caller has to figure something out */
622 + return;
623 + }
624 +
625 + // File closed and no other error, so ensure next snapshot can be started.
626 + if (snap_state.state != SAVE_STATE_ERROR) {
627 + snap_state.state = SAVE_STATE_DONE;
628 + }
629 +
630 + DPRINTF("savevm-end: cleanup done\n");
631 +}
632 +
633 +// FIXME: Deprecated
634 +void qmp_snapshot_drive(const char *device, const char *name, Error **errp)
635 +{
636 + // Compatibility to older qemu-server.
637 + qmp_blockdev_snapshot_internal_sync(device, name, errp);
638 +}
639 +
640 +// FIXME: Deprecated
641 +void qmp_delete_drive_snapshot(const char *device, const char *name,
642 + Error **errp)
643 +{
644 + // Compatibility to older qemu-server.
645 + (void)qmp_blockdev_snapshot_delete_internal_sync(device, NULL, name, errp);
646 +}
647 +
648 +int load_snapshot_from_blockdev(const char *filename, Error **errp)
649 +{
650 + BlockBackend *be;
651 + Error *local_err = NULL;
652 + Error *blocker = NULL;
653 +
654 + QEMUFile *f;
655 + size_t bs_pos = 0;
656 + int ret = -EINVAL;
657 +
658 + be = blk_new_open(filename, NULL, NULL, 0, &local_err);
659 +
660 + if (!be) {
661 + error_setg(errp, "Could not open VM state file");
662 + goto the_end;
663 + }
664 +
665 + error_setg(&blocker, "block device is in use by load state");
666 + blk_op_block_all(be, blocker);
667 +
668 + /* restore the VM state */
669 + f = qemu_file_new_input(QIO_CHANNEL(qio_channel_savevm_async_new(be, &bs_pos)));
670 + if (!f) {
671 + error_setg(errp, "Could not open VM state file");
672 + goto the_end;
673 + }
674 +
675 + qemu_system_reset(SHUTDOWN_CAUSE_NONE);
676 + ret = qemu_loadvm_state(f);
677 +
678 + /* dirty bitmap migration has a special case we need to trigger manually */
679 + dirty_bitmap_mig_before_vm_start();
680 +
681 + qemu_fclose(f);
682 + migration_incoming_state_destroy();
683 + if (ret < 0) {
684 + error_setg_errno(errp, -ret, "Error while loading VM state");
685 + goto the_end;
686 + }
687 +
688 + ret = 0;
689 +
690 + the_end:
691 + if (be) {
692 + blk_op_unblock_all(be, blocker);
693 + error_free(blocker);
694 + blk_unref(be);
695 + }
696 + return ret;
697 +}
698 diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
699 index 6c559b48c8..435f9334f9 100644
700 --- a/monitor/hmp-cmds.c
701 +++ b/monitor/hmp-cmds.c
702 @@ -22,6 +22,7 @@
703 #include "monitor/monitor-internal.h"
704 #include "qapi/error.h"
705 #include "qapi/qapi-commands-control.h"
706 +#include "qapi/qapi-commands-migration.h"
707 #include "qapi/qapi-commands-misc.h"
708 #include "qapi/qmp/qdict.h"
709 #include "qapi/qmp/qerror.h"
710 @@ -443,3 +444,60 @@ void hmp_info_mtree(Monitor *mon, const QDict *qdict)
711
712 mtree_info(flatview, dispatch_tree, owner, disabled);
713 }
714 +
715 +void hmp_savevm_start(Monitor *mon, const QDict *qdict)
716 +{
717 + Error *errp = NULL;
718 + const char *statefile = qdict_get_try_str(qdict, "statefile");
719 +
720 + qmp_savevm_start(statefile, &errp);
721 + hmp_handle_error(mon, errp);
722 +}
723 +
724 +void hmp_snapshot_drive(Monitor *mon, const QDict *qdict)
725 +{
726 + Error *errp = NULL;
727 + const char *name = qdict_get_str(qdict, "name");
728 + const char *device = qdict_get_str(qdict, "device");
729 +
730 + qmp_snapshot_drive(device, name, &errp);
731 + hmp_handle_error(mon, errp);
732 +}
733 +
734 +void hmp_delete_drive_snapshot(Monitor *mon, const QDict *qdict)
735 +{
736 + Error *errp = NULL;
737 + const char *name = qdict_get_str(qdict, "name");
738 + const char *device = qdict_get_str(qdict, "device");
739 +
740 + qmp_delete_drive_snapshot(device, name, &errp);
741 + hmp_handle_error(mon, errp);
742 +}
743 +
744 +void coroutine_fn hmp_savevm_end(Monitor *mon, const QDict *qdict)
745 +{
746 + Error *errp = NULL;
747 +
748 + qmp_savevm_end(&errp);
749 + hmp_handle_error(mon, errp);
750 +}
751 +
752 +void hmp_info_savevm(Monitor *mon, const QDict *qdict)
753 +{
754 + SaveVMInfo *info;
755 + info = qmp_query_savevm(NULL);
756 +
757 + if (info->status) {
758 + monitor_printf(mon, "savevm status: %s\n", info->status);
759 + monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
760 + info->total_time);
761 + } else {
762 + monitor_printf(mon, "savevm status: not running\n");
763 + }
764 + if (info->has_bytes) {
765 + monitor_printf(mon, "Bytes saved: %"PRIu64"\n", info->bytes);
766 + }
767 + if (info->error) {
768 + monitor_printf(mon, "Error: %s\n", info->error);
769 + }
770 +}
771 diff --git a/qapi/migration.json b/qapi/migration.json
772 index c84fa10e86..1702b92553 100644
773 --- a/qapi/migration.json
774 +++ b/qapi/migration.json
775 @@ -261,6 +261,40 @@
776 '*compression': 'CompressionStats',
777 '*socket-address': ['SocketAddress'] } }
778
779 +##
780 +# @SaveVMInfo:
781 +#
782 +# Information about current migration process.
783 +#
784 +# @status: string describing the current savevm status.
785 +# This can be 'active', 'completed', 'failed'.
786 +# If this field is not returned, no savevm process
787 +# has been initiated
788 +#
789 +# @error: string containing error message is status is failed.
790 +#
791 +# @total-time: total amount of milliseconds since savevm started.
792 +# If savevm has ended, it returns the total save time
793 +#
794 +# @bytes: total amount of data transfered
795 +#
796 +# Since: 1.3
797 +##
798 +{ 'struct': 'SaveVMInfo',
799 + 'data': {'*status': 'str', '*error': 'str',
800 + '*total-time': 'int', '*bytes': 'int'} }
801 +
802 +##
803 +# @query-savevm:
804 +#
805 +# Returns information about current savevm process.
806 +#
807 +# Returns: @SaveVMInfo
808 +#
809 +# Since: 1.3
810 +##
811 +{ 'command': 'query-savevm', 'returns': 'SaveVMInfo' }
812 +
813 ##
814 # @query-migrate:
815 #
816 diff --git a/qapi/misc.json b/qapi/misc.json
817 index 6ddd16ea28..098c9bbe93 100644
818 --- a/qapi/misc.json
819 +++ b/qapi/misc.json
820 @@ -469,6 +469,38 @@
821 ##
822 { 'command': 'query-fdsets', 'returns': ['FdsetInfo'] }
823
824 +##
825 +# @savevm-start:
826 +#
827 +# Prepare for snapshot and halt VM. Save VM state to statefile.
828 +#
829 +##
830 +{ 'command': 'savevm-start', 'data': { '*statefile': 'str' } }
831 +
832 +##
833 +# @snapshot-drive:
834 +#
835 +# Create an internal drive snapshot.
836 +#
837 +##
838 +{ 'command': 'snapshot-drive', 'data': { 'device': 'str', 'name': 'str' } }
839 +
840 +##
841 +# @delete-drive-snapshot:
842 +#
843 +# Delete a drive snapshot.
844 +#
845 +##
846 +{ 'command': 'delete-drive-snapshot', 'data': { 'device': 'str', 'name': 'str' } }
847 +
848 +##
849 +# @savevm-end:
850 +#
851 +# Resume VM after a snapshot.
852 +#
853 +##
854 +{ 'command': 'savevm-end', 'coroutine': true }
855 +
856 ##
857 # @CommandLineParameterType:
858 #
859 diff --git a/qemu-options.hx b/qemu-options.hx
860 index 59bdf67a2c..fc6cb23dd9 100644
861 --- a/qemu-options.hx
862 +++ b/qemu-options.hx
863 @@ -4378,6 +4378,18 @@ SRST
864 Start right away with a saved state (``loadvm`` in monitor)
865 ERST
866
867 +DEF("loadstate", HAS_ARG, QEMU_OPTION_loadstate, \
868 + "-loadstate file\n" \
869 + " start right away with a saved state\n",
870 + QEMU_ARCH_ALL)
871 +SRST
872 +``-loadstate file``
873 + Start right away with a saved state. This option does not rollback
874 + disk state like @code{loadvm}, so user must make sure that disk
875 + have correct state. @var{file} can be any valid device URL. See the section
876 + for "Device URL Syntax" for more information.
877 +ERST
878 +
879 #ifndef _WIN32
880 DEF("daemonize", 0, QEMU_OPTION_daemonize, \
881 "-daemonize daemonize QEMU after initializing\n", QEMU_ARCH_ALL)
882 diff --git a/softmmu/vl.c b/softmmu/vl.c
883 index ea20b23e4c..0eabc71b68 100644
884 --- a/softmmu/vl.c
885 +++ b/softmmu/vl.c
886 @@ -164,6 +164,7 @@ static const char *accelerators;
887 static bool have_custom_ram_size;
888 static const char *ram_memdev_id;
889 static QDict *machine_opts_dict;
890 +static const char *loadstate;
891 static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
892 static QTAILQ_HEAD(, DeviceOption) device_opts = QTAILQ_HEAD_INITIALIZER(device_opts);
893 static int display_remote;
894 @@ -2612,6 +2613,12 @@ void qmp_x_exit_preconfig(Error **errp)
895
896 if (loadvm) {
897 load_snapshot(loadvm, NULL, false, NULL, &error_fatal);
898 + } else if (loadstate) {
899 + Error *local_err = NULL;
900 + if (load_snapshot_from_blockdev(loadstate, &local_err) < 0) {
901 + error_report_err(local_err);
902 + autostart = 0;
903 + }
904 }
905 if (replay_mode != REPLAY_MODE_NONE) {
906 replay_vmstate_init();
907 @@ -3159,6 +3166,9 @@ void qemu_init(int argc, char **argv)
908 case QEMU_OPTION_loadvm:
909 loadvm = optarg;
910 break;
911 + case QEMU_OPTION_loadstate:
912 + loadstate = optarg;
913 + break;
914 case QEMU_OPTION_full_screen:
915 dpy.has_full_screen = true;
916 dpy.full_screen = true;