952 lines
26 KiB
Diff
952 lines
26 KiB
Diff
From 27d7cf1b94244a56cd0e8a408af3e40a84dace81 Mon Sep 17 00:00:00 2001
|
|
From: Wolfgang Bumiller <w.bumiller@proxmox.com>
|
|
Date: Wed, 9 Dec 2015 16:04:32 +0100
|
|
Subject: [PATCH 10/28] internal snapshot async
|
|
|
|
---
|
|
Makefile.objs | 1 +
|
|
hmp-commands-info.hx | 13 ++
|
|
hmp-commands.hx | 32 +++
|
|
hmp.c | 57 ++++++
|
|
hmp.h | 5 +
|
|
include/sysemu/sysemu.h | 5 +-
|
|
migration/savevm.c | 12 +-
|
|
qapi-schema.json | 68 +++++++
|
|
qemu-options.hx | 13 ++
|
|
savevm-async.c | 523 ++++++++++++++++++++++++++++++++++++++++++++++++
|
|
vl.c | 8 +
|
|
11 files changed, 730 insertions(+), 7 deletions(-)
|
|
create mode 100644 savevm-async.c
|
|
|
|
diff --git a/Makefile.objs b/Makefile.objs
|
|
index 6167e7b17d..fbfbbb7f70 100644
|
|
--- a/Makefile.objs
|
|
+++ b/Makefile.objs
|
|
@@ -50,6 +50,7 @@ common-obj-$(CONFIG_LINUX) += fsdev/
|
|
|
|
common-obj-y += migration/
|
|
common-obj-y += page_cache.o #aio.o
|
|
+common-obj-y += savevm-async.o
|
|
|
|
common-obj-$(CONFIG_SPICE) += spice-qemu-char.o
|
|
|
|
diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
|
|
index a53f105c52..5fc57a2210 100644
|
|
--- a/hmp-commands-info.hx
|
|
+++ b/hmp-commands-info.hx
|
|
@@ -560,6 +560,19 @@ Show current migration xbzrle cache size.
|
|
ETEXI
|
|
|
|
{
|
|
+ .name = "savevm",
|
|
+ .args_type = "",
|
|
+ .params = "",
|
|
+ .help = "show savevm status",
|
|
+ .cmd = hmp_info_savevm,
|
|
+ },
|
|
+
|
|
+STEXI
|
|
+@item info savevm
|
|
+show savevm status
|
|
+ETEXI
|
|
+
|
|
+ {
|
|
.name = "balloon",
|
|
.args_type = "",
|
|
.params = "",
|
|
diff --git a/hmp-commands.hx b/hmp-commands.hx
|
|
index 88192817b2..58940a762b 100644
|
|
--- a/hmp-commands.hx
|
|
+++ b/hmp-commands.hx
|
|
@@ -1777,3 +1777,35 @@ ETEXI
|
|
STEXI
|
|
@end table
|
|
ETEXI
|
|
+
|
|
+ {
|
|
+ .name = "savevm-start",
|
|
+ .args_type = "statefile:s?",
|
|
+ .params = "[statefile]",
|
|
+ .help = "Prepare for snapshot and halt VM. Save VM state to statefile.",
|
|
+ .cmd = hmp_savevm_start,
|
|
+ },
|
|
+
|
|
+ {
|
|
+ .name = "snapshot-drive",
|
|
+ .args_type = "device:s,name:s",
|
|
+ .params = "device name",
|
|
+ .help = "Create internal snapshot.",
|
|
+ .cmd = hmp_snapshot_drive,
|
|
+ },
|
|
+
|
|
+ {
|
|
+ .name = "delete-drive-snapshot",
|
|
+ .args_type = "device:s,name:s",
|
|
+ .params = "device name",
|
|
+ .help = "Delete internal snapshot.",
|
|
+ .cmd = hmp_delete_drive_snapshot,
|
|
+ },
|
|
+
|
|
+ {
|
|
+ .name = "savevm-end",
|
|
+ .args_type = "",
|
|
+ .params = "",
|
|
+ .help = "Resume VM after snaphot.",
|
|
+ .cmd = hmp_savevm_end,
|
|
+ },
|
|
diff --git a/hmp.c b/hmp.c
|
|
index 904542d026..f725d061e6 100644
|
|
--- a/hmp.c
|
|
+++ b/hmp.c
|
|
@@ -2207,6 +2207,63 @@ void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
|
|
qapi_free_MemoryDeviceInfoList(info_list);
|
|
}
|
|
|
|
+void hmp_savevm_start(Monitor *mon, const QDict *qdict)
|
|
+{
|
|
+ Error *errp = NULL;
|
|
+ const char *statefile = qdict_get_try_str(qdict, "statefile");
|
|
+
|
|
+ qmp_savevm_start(statefile != NULL, statefile, &errp);
|
|
+ hmp_handle_error(mon, &errp);
|
|
+}
|
|
+
|
|
+void hmp_snapshot_drive(Monitor *mon, const QDict *qdict)
|
|
+{
|
|
+ Error *errp = NULL;
|
|
+ const char *name = qdict_get_str(qdict, "name");
|
|
+ const char *device = qdict_get_str(qdict, "device");
|
|
+
|
|
+ qmp_snapshot_drive(device, name, &errp);
|
|
+ hmp_handle_error(mon, &errp);
|
|
+}
|
|
+
|
|
+void hmp_delete_drive_snapshot(Monitor *mon, const QDict *qdict)
|
|
+{
|
|
+ Error *errp = NULL;
|
|
+ const char *name = qdict_get_str(qdict, "name");
|
|
+ const char *device = qdict_get_str(qdict, "device");
|
|
+
|
|
+ qmp_delete_drive_snapshot(device, name, &errp);
|
|
+ hmp_handle_error(mon, &errp);
|
|
+}
|
|
+
|
|
+void hmp_savevm_end(Monitor *mon, const QDict *qdict)
|
|
+{
|
|
+ Error *errp = NULL;
|
|
+
|
|
+ qmp_savevm_end(&errp);
|
|
+ hmp_handle_error(mon, &errp);
|
|
+}
|
|
+
|
|
+void hmp_info_savevm(Monitor *mon, const QDict *qdict)
|
|
+{
|
|
+ SaveVMInfo *info;
|
|
+ info = qmp_query_savevm(NULL);
|
|
+
|
|
+ if (info->has_status) {
|
|
+ monitor_printf(mon, "savevm status: %s\n", info->status);
|
|
+ monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
|
|
+ info->total_time);
|
|
+ } else {
|
|
+ monitor_printf(mon, "savevm status: not running\n");
|
|
+ }
|
|
+ if (info->has_bytes) {
|
|
+ monitor_printf(mon, "Bytes saved: %"PRIu64"\n", info->bytes);
|
|
+ }
|
|
+ if (info->has_error) {
|
|
+ monitor_printf(mon, "Error: %s\n", info->error);
|
|
+ }
|
|
+}
|
|
+
|
|
void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
|
|
{
|
|
IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
|
|
diff --git a/hmp.h b/hmp.h
|
|
index 799fd371fa..0497afbf65 100644
|
|
--- a/hmp.h
|
|
+++ b/hmp.h
|
|
@@ -26,6 +26,7 @@ void hmp_info_status(Monitor *mon, const QDict *qdict);
|
|
void hmp_info_uuid(Monitor *mon, const QDict *qdict);
|
|
void hmp_info_chardev(Monitor *mon, const QDict *qdict);
|
|
void hmp_info_mice(Monitor *mon, const QDict *qdict);
|
|
+void hmp_info_savevm(Monitor *mon, const QDict *qdict);
|
|
void hmp_info_migrate(Monitor *mon, const QDict *qdict);
|
|
void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
|
|
void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict);
|
|
@@ -92,6 +93,10 @@ void hmp_netdev_add(Monitor *mon, const QDict *qdict);
|
|
void hmp_netdev_del(Monitor *mon, const QDict *qdict);
|
|
void hmp_getfd(Monitor *mon, const QDict *qdict);
|
|
void hmp_closefd(Monitor *mon, const QDict *qdict);
|
|
+void hmp_savevm_start(Monitor *mon, const QDict *qdict);
|
|
+void hmp_snapshot_drive(Monitor *mon, const QDict *qdict);
|
|
+void hmp_delete_drive_snapshot(Monitor *mon, const QDict *qdict);
|
|
+void hmp_savevm_end(Monitor *mon, const QDict *qdict);
|
|
void hmp_sendkey(Monitor *mon, const QDict *qdict);
|
|
void hmp_screendump(Monitor *mon, const QDict *qdict);
|
|
void hmp_nbd_server_start(Monitor *mon, const QDict *qdict);
|
|
diff --git a/include/sysemu/sysemu.h b/include/sysemu/sysemu.h
|
|
index 576c7ce640..74623de16c 100644
|
|
--- a/include/sysemu/sysemu.h
|
|
+++ b/include/sysemu/sysemu.h
|
|
@@ -78,6 +78,7 @@ void qemu_remove_machine_init_done_notifier(Notifier *notify);
|
|
void hmp_savevm(Monitor *mon, const QDict *qdict);
|
|
int save_vmstate(Monitor *mon, const char *name);
|
|
int load_vmstate(const char *name);
|
|
+int load_state_from_blockdev(const char *filename);
|
|
void hmp_delvm(Monitor *mon, const QDict *qdict);
|
|
void hmp_info_snapshots(Monitor *mon, const QDict *qdict);
|
|
|
|
@@ -105,13 +106,13 @@ enum qemu_vm_cmd {
|
|
#define MAX_VM_CMD_PACKAGED_SIZE (1ul << 24)
|
|
|
|
bool qemu_savevm_state_blocked(Error **errp);
|
|
-void qemu_savevm_state_begin(QEMUFile *f,
|
|
+int qemu_savevm_state_begin(QEMUFile *f,
|
|
const MigrationParams *params);
|
|
void qemu_savevm_state_header(QEMUFile *f);
|
|
int qemu_savevm_state_iterate(QEMUFile *f, bool postcopy);
|
|
void qemu_savevm_state_cleanup(void);
|
|
void qemu_savevm_state_complete_postcopy(QEMUFile *f);
|
|
-void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only);
|
|
+int qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only);
|
|
void qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size,
|
|
uint64_t *res_non_postcopiable,
|
|
uint64_t *res_postcopiable);
|
|
diff --git a/migration/savevm.c b/migration/savevm.c
|
|
index 3b19a4a274..feb0dc6834 100644
|
|
--- a/migration/savevm.c
|
|
+++ b/migration/savevm.c
|
|
@@ -970,11 +970,11 @@ void qemu_savevm_state_header(QEMUFile *f)
|
|
|
|
}
|
|
|
|
-void qemu_savevm_state_begin(QEMUFile *f,
|
|
+int qemu_savevm_state_begin(QEMUFile *f,
|
|
const MigrationParams *params)
|
|
{
|
|
SaveStateEntry *se;
|
|
- int ret;
|
|
+ int ret = 0;
|
|
|
|
trace_savevm_state_begin();
|
|
QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
|
|
@@ -1002,6 +1002,7 @@ void qemu_savevm_state_begin(QEMUFile *f,
|
|
break;
|
|
}
|
|
}
|
|
+ return ret;
|
|
}
|
|
|
|
/*
|
|
@@ -1105,7 +1106,7 @@ void qemu_savevm_state_complete_postcopy(QEMUFile *f)
|
|
qemu_fflush(f);
|
|
}
|
|
|
|
-void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
|
|
+int qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
|
|
{
|
|
QJSON *vmdesc;
|
|
int vmdesc_len;
|
|
@@ -1139,12 +1140,12 @@ void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
|
|
save_section_footer(f, se);
|
|
if (ret < 0) {
|
|
qemu_file_set_error(f, ret);
|
|
- return;
|
|
+ return ret;
|
|
}
|
|
}
|
|
|
|
if (iterable_only) {
|
|
- return;
|
|
+ return ret;
|
|
}
|
|
|
|
vmdesc = qjson_new();
|
|
@@ -1191,6 +1192,7 @@ void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
|
|
qjson_destroy(vmdesc);
|
|
|
|
qemu_fflush(f);
|
|
+ return qemu_file_get_error(f);
|
|
}
|
|
|
|
/* Give an estimate of the amount left to be transferred,
|
|
diff --git a/qapi-schema.json b/qapi-schema.json
|
|
index 1b14ff2476..361700d37c 100644
|
|
--- a/qapi-schema.json
|
|
+++ b/qapi-schema.json
|
|
@@ -723,6 +723,40 @@
|
|
'*error-desc': 'str'} }
|
|
|
|
##
|
|
+# @SaveVMInfo:
|
|
+#
|
|
+# Information about current migration process.
|
|
+#
|
|
+# @status: string describing the current savevm status.
|
|
+# This can be 'active', 'completed', 'failed'.
|
|
+# If this field is not returned, no savevm process
|
|
+# has been initiated
|
|
+#
|
|
+# @error: string containing error message is status is failed.
|
|
+#
|
|
+# @total-time: total amount of milliseconds since savevm started.
|
|
+# If savevm has ended, it returns the total save time
|
|
+#
|
|
+# @bytes: total amount of data transfered
|
|
+#
|
|
+# Since: 1.3
|
|
+##
|
|
+{ 'struct': 'SaveVMInfo',
|
|
+ 'data': {'*status': 'str', '*error': 'str',
|
|
+ '*total-time': 'int', '*bytes': 'int'} }
|
|
+
|
|
+##
|
|
+# @query-savevm:
|
|
+#
|
|
+# Returns information about current savevm process.
|
|
+#
|
|
+# Returns: @SaveVMInfo
|
|
+#
|
|
+# Since: 1.3
|
|
+##
|
|
+{ 'command': 'query-savevm', 'returns': 'SaveVMInfo' }
|
|
+
|
|
+##
|
|
# @query-migrate:
|
|
#
|
|
# Returns information about current migration process. If migration
|
|
@@ -4735,9 +4769,43 @@
|
|
#
|
|
# Since: 1.2.0
|
|
##
|
|
+
|
|
{ 'command': 'query-target', 'returns': 'TargetInfo' }
|
|
|
|
##
|
|
+# @savevm-start:
|
|
+#
|
|
+# Prepare for snapshot and halt VM. Save VM state to statefile.
|
|
+#
|
|
+##
|
|
+{ 'command': 'savevm-start', 'data': { '*statefile': 'str' } }
|
|
+
|
|
+##
|
|
+# @snapshot-drive:
|
|
+#
|
|
+# Create an internal drive snapshot.
|
|
+#
|
|
+##
|
|
+{ 'command': 'snapshot-drive', 'data': { 'device': 'str', 'name': 'str' } }
|
|
+
|
|
+##
|
|
+# @delete-drive-snapshot:
|
|
+#
|
|
+# Delete a drive snapshot.
|
|
+#
|
|
+##
|
|
+{ 'command': 'delete-drive-snapshot', 'data': { 'device': 'str', 'name': 'str' } }
|
|
+
|
|
+##
|
|
+# @savevm-end:
|
|
+#
|
|
+# Resume VM after a snapshot.
|
|
+#
|
|
+##
|
|
+{ 'command': 'savevm-end' }
|
|
+
|
|
+
|
|
+##
|
|
# @QKeyCode:
|
|
#
|
|
# An enumeration of key name.
|
|
diff --git a/qemu-options.hx b/qemu-options.hx
|
|
index 48dfffd86a..cbcb27da9a 100644
|
|
--- a/qemu-options.hx
|
|
+++ b/qemu-options.hx
|
|
@@ -3396,6 +3396,19 @@ STEXI
|
|
Start right away with a saved state (@code{loadvm} in monitor)
|
|
ETEXI
|
|
|
|
+DEF("loadstate", HAS_ARG, QEMU_OPTION_loadstate, \
|
|
+ "-loadstate file\n" \
|
|
+ " start right away with a saved state\n",
|
|
+ QEMU_ARCH_ALL)
|
|
+STEXI
|
|
+@item -loadstate @var{file}
|
|
+@findex -loadstate
|
|
+Start right away with a saved state. This option does not rollback
|
|
+disk state like @code{loadvm}, so user must make sure that disk
|
|
+have correct state. @var{file} can be any valid device URL. See the section
|
|
+for "Device URL Syntax" for more information.
|
|
+ETEXI
|
|
+
|
|
#ifndef _WIN32
|
|
DEF("daemonize", 0, QEMU_OPTION_daemonize, \
|
|
"-daemonize daemonize QEMU after initializing\n", QEMU_ARCH_ALL)
|
|
diff --git a/savevm-async.c b/savevm-async.c
|
|
new file mode 100644
|
|
index 0000000000..5fcb56d373
|
|
--- /dev/null
|
|
+++ b/savevm-async.c
|
|
@@ -0,0 +1,523 @@
|
|
+#include "qemu/osdep.h"
|
|
+#include "qemu-common.h"
|
|
+#include "qapi/qmp/qerror.h"
|
|
+#include "qemu/error-report.h"
|
|
+#include "sysemu/sysemu.h"
|
|
+#include "qmp-commands.h"
|
|
+#include "qemu-options.h"
|
|
+#include "migration/qemu-file.h"
|
|
+#include "qom/qom-qobject.h"
|
|
+#include "migration/migration.h"
|
|
+#include "block/snapshot.h"
|
|
+#include "block/qapi.h"
|
|
+#include "block/block.h"
|
|
+#include "qemu/timer.h"
|
|
+#include "sysemu/block-backend.h"
|
|
+#include "qapi/qmp/qstring.h"
|
|
+#include "qemu/rcu.h"
|
|
+#include "qemu/thread.h"
|
|
+#include "qemu/cutils.h"
|
|
+
|
|
+/* #define DEBUG_SAVEVM_STATE */
|
|
+
|
|
+#define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
|
|
+
|
|
+#ifdef DEBUG_SAVEVM_STATE
|
|
+#define DPRINTF(fmt, ...) \
|
|
+ do { printf("savevm-async: " fmt, ## __VA_ARGS__); } while (0)
|
|
+#else
|
|
+#define DPRINTF(fmt, ...) \
|
|
+ do { } while (0)
|
|
+#endif
|
|
+
|
|
+enum {
|
|
+ SAVE_STATE_DONE,
|
|
+ SAVE_STATE_ERROR,
|
|
+ SAVE_STATE_ACTIVE,
|
|
+ SAVE_STATE_COMPLETED,
|
|
+ SAVE_STATE_CANCELLED
|
|
+};
|
|
+
|
|
+
|
|
+static struct SnapshotState {
|
|
+ BlockBackend *target;
|
|
+ size_t bs_pos;
|
|
+ int state;
|
|
+ Error *error;
|
|
+ Error *blocker;
|
|
+ int saved_vm_running;
|
|
+ QEMUFile *file;
|
|
+ int64_t total_time;
|
|
+} snap_state;
|
|
+
|
|
+SaveVMInfo *qmp_query_savevm(Error **errp)
|
|
+{
|
|
+ SaveVMInfo *info = g_malloc0(sizeof(*info));
|
|
+ struct SnapshotState *s = &snap_state;
|
|
+
|
|
+ if (s->state != SAVE_STATE_DONE) {
|
|
+ info->has_bytes = true;
|
|
+ info->bytes = s->bs_pos;
|
|
+ switch (s->state) {
|
|
+ case SAVE_STATE_ERROR:
|
|
+ info->has_status = true;
|
|
+ info->status = g_strdup("failed");
|
|
+ info->has_total_time = true;
|
|
+ info->total_time = s->total_time;
|
|
+ if (s->error) {
|
|
+ info->has_error = true;
|
|
+ info->error = g_strdup(error_get_pretty(s->error));
|
|
+ }
|
|
+ break;
|
|
+ case SAVE_STATE_ACTIVE:
|
|
+ info->has_status = true;
|
|
+ info->status = g_strdup("active");
|
|
+ info->has_total_time = true;
|
|
+ info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
|
|
+ - s->total_time;
|
|
+ break;
|
|
+ case SAVE_STATE_COMPLETED:
|
|
+ info->has_status = true;
|
|
+ info->status = g_strdup("completed");
|
|
+ info->has_total_time = true;
|
|
+ info->total_time = s->total_time;
|
|
+ break;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ return info;
|
|
+}
|
|
+
|
|
+static int save_snapshot_cleanup(void)
|
|
+{
|
|
+ int ret = 0;
|
|
+
|
|
+ DPRINTF("save_snapshot_cleanup\n");
|
|
+
|
|
+ snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
|
|
+ snap_state.total_time;
|
|
+
|
|
+ if (snap_state.file) {
|
|
+ ret = qemu_fclose(snap_state.file);
|
|
+ }
|
|
+
|
|
+ if (snap_state.target) {
|
|
+ /* try to truncate, but ignore errors (will fail on block devices).
|
|
+ * note: bdrv_read() need whole blocks, so we round up
|
|
+ */
|
|
+ size_t size = (snap_state.bs_pos + BDRV_SECTOR_SIZE) & BDRV_SECTOR_MASK;
|
|
+ blk_truncate(snap_state.target, size, NULL);
|
|
+ blk_op_unblock_all(snap_state.target, snap_state.blocker);
|
|
+ error_free(snap_state.blocker);
|
|
+ snap_state.blocker = NULL;
|
|
+ blk_unref(snap_state.target);
|
|
+ snap_state.target = NULL;
|
|
+ }
|
|
+
|
|
+ return ret;
|
|
+}
|
|
+
|
|
+static void save_snapshot_error(const char *fmt, ...)
|
|
+{
|
|
+ va_list ap;
|
|
+ char *msg;
|
|
+
|
|
+ va_start(ap, fmt);
|
|
+ msg = g_strdup_vprintf(fmt, ap);
|
|
+ va_end(ap);
|
|
+
|
|
+ DPRINTF("save_snapshot_error: %s\n", msg);
|
|
+
|
|
+ if (!snap_state.error) {
|
|
+ error_set(&snap_state.error, ERROR_CLASS_GENERIC_ERROR, "%s", msg);
|
|
+ }
|
|
+
|
|
+ g_free (msg);
|
|
+
|
|
+ snap_state.state = SAVE_STATE_ERROR;
|
|
+
|
|
+ save_snapshot_cleanup();
|
|
+}
|
|
+
|
|
+static void save_snapshot_completed(void)
|
|
+{
|
|
+ DPRINTF("save_snapshot_completed\n");
|
|
+
|
|
+ if (save_snapshot_cleanup() < 0) {
|
|
+ snap_state.state = SAVE_STATE_ERROR;
|
|
+ } else {
|
|
+ snap_state.state = SAVE_STATE_COMPLETED;
|
|
+ }
|
|
+}
|
|
+
|
|
+static int block_state_close(void *opaque)
|
|
+{
|
|
+ snap_state.file = NULL;
|
|
+ return blk_flush(snap_state.target);
|
|
+}
|
|
+
|
|
+static ssize_t block_state_writev_buffer(void *opaque, struct iovec *iov,
|
|
+ int iovcnt, int64_t pos)
|
|
+{
|
|
+ int ret;
|
|
+ QEMUIOVector qiov;
|
|
+
|
|
+ qemu_iovec_init_external(&qiov, iov, iovcnt);
|
|
+ ret = blk_co_pwritev(snap_state.target, pos, qiov.size, &qiov, 0);
|
|
+ if (ret < 0) {
|
|
+ return ret;
|
|
+ }
|
|
+ snap_state.bs_pos += qiov.size;
|
|
+ return qiov.size;
|
|
+}
|
|
+
|
|
+static int store_and_stop(void) {
|
|
+ if (global_state_store()) {
|
|
+ save_snapshot_error("Error saving global state");
|
|
+ return 1;
|
|
+ }
|
|
+ if (runstate_is_running()) {
|
|
+ vm_stop(RUN_STATE_SAVE_VM);
|
|
+ }
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+static void process_savevm_co(void *opaque)
|
|
+{
|
|
+ int ret;
|
|
+ int64_t maxlen;
|
|
+ MigrationParams params = {
|
|
+ .blk = 0,
|
|
+ .shared = 0
|
|
+ };
|
|
+
|
|
+ snap_state.state = SAVE_STATE_ACTIVE;
|
|
+
|
|
+ qemu_mutex_unlock_iothread();
|
|
+ qemu_savevm_state_header(snap_state.file);
|
|
+ ret = qemu_savevm_state_begin(snap_state.file, ¶ms);
|
|
+ qemu_mutex_lock_iothread();
|
|
+
|
|
+ if (ret < 0) {
|
|
+ save_snapshot_error("qemu_savevm_state_begin failed");
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ while (snap_state.state == SAVE_STATE_ACTIVE) {
|
|
+ uint64_t pending_size, pend_post, pend_nonpost;
|
|
+
|
|
+ qemu_savevm_state_pending(snap_state.file, 0, &pend_nonpost, &pend_post);
|
|
+ pending_size = pend_post + pend_nonpost;
|
|
+
|
|
+ if (pending_size) {
|
|
+ ret = qemu_savevm_state_iterate(snap_state.file, false);
|
|
+ if (ret < 0) {
|
|
+ save_snapshot_error("qemu_savevm_state_iterate error %d", ret);
|
|
+ break;
|
|
+ }
|
|
+ DPRINTF("savevm inerate pending size %lu ret %d\n", pending_size, ret);
|
|
+ } else {
|
|
+ DPRINTF("done iterating\n");
|
|
+ if (store_and_stop())
|
|
+ break;
|
|
+ DPRINTF("savevm inerate finished\n");
|
|
+ qemu_savevm_state_complete_precopy(snap_state.file, false);
|
|
+ DPRINTF("save complete\n");
|
|
+ save_snapshot_completed();
|
|
+ break;
|
|
+ }
|
|
+
|
|
+ /* stop the VM if we get to the end of available space,
|
|
+ * or if pending_size is just a few MB
|
|
+ */
|
|
+ maxlen = blk_getlength(snap_state.target) - 30*1024*1024;
|
|
+ if ((pending_size < 100000) ||
|
|
+ ((snap_state.bs_pos + pending_size) >= maxlen)) {
|
|
+ if (store_and_stop())
|
|
+ break;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ if(snap_state.state == SAVE_STATE_CANCELLED) {
|
|
+ save_snapshot_completed();
|
|
+ Error *errp = NULL;
|
|
+ qmp_savevm_end(&errp);
|
|
+ }
|
|
+
|
|
+}
|
|
+
|
|
+static const QEMUFileOps block_file_ops = {
|
|
+ .writev_buffer = block_state_writev_buffer,
|
|
+ .close = block_state_close,
|
|
+};
|
|
+
|
|
+
|
|
+void qmp_savevm_start(bool has_statefile, const char *statefile, Error **errp)
|
|
+{
|
|
+ Error *local_err = NULL;
|
|
+
|
|
+ int bdrv_oflags = BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_NO_FLUSH;
|
|
+
|
|
+ if (snap_state.state != SAVE_STATE_DONE) {
|
|
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
|
|
+ "VM snapshot already started\n");
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ /* initialize snapshot info */
|
|
+ snap_state.saved_vm_running = runstate_is_running();
|
|
+ snap_state.bs_pos = 0;
|
|
+ snap_state.total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
|
|
+ snap_state.blocker = NULL;
|
|
+
|
|
+ if (snap_state.error) {
|
|
+ error_free(snap_state.error);
|
|
+ snap_state.error = NULL;
|
|
+ }
|
|
+
|
|
+ if (!has_statefile) {
|
|
+ vm_stop(RUN_STATE_SAVE_VM);
|
|
+ snap_state.state = SAVE_STATE_COMPLETED;
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ if (qemu_savevm_state_blocked(errp)) {
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ /* Open the image */
|
|
+ QDict *options = NULL;
|
|
+ options = qdict_new();
|
|
+ qdict_put(options, "driver", qstring_from_str("raw"));
|
|
+ snap_state.target = blk_new_open(statefile, NULL, options, bdrv_oflags, &local_err);
|
|
+ if (!snap_state.target) {
|
|
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
|
|
+ goto restart;
|
|
+ }
|
|
+
|
|
+ snap_state.file = qemu_fopen_ops(&snap_state, &block_file_ops);
|
|
+
|
|
+ if (!snap_state.file) {
|
|
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR, "failed to open '%s'", statefile);
|
|
+ goto restart;
|
|
+ }
|
|
+
|
|
+
|
|
+ error_setg(&snap_state.blocker, "block device is in use by savevm");
|
|
+ blk_op_block_all(snap_state.target, snap_state.blocker);
|
|
+
|
|
+ Coroutine *co = qemu_coroutine_create(process_savevm_co, NULL);
|
|
+ qemu_coroutine_enter(co);
|
|
+
|
|
+ return;
|
|
+
|
|
+restart:
|
|
+
|
|
+ save_snapshot_error("setup failed");
|
|
+
|
|
+ if (snap_state.saved_vm_running) {
|
|
+ vm_start();
|
|
+ }
|
|
+}
|
|
+
|
|
+void qmp_savevm_end(Error **errp)
|
|
+{
|
|
+ if (snap_state.state == SAVE_STATE_DONE) {
|
|
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
|
|
+ "VM snapshot not started\n");
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ if (snap_state.state == SAVE_STATE_ACTIVE) {
|
|
+ snap_state.state = SAVE_STATE_CANCELLED;
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ if (snap_state.saved_vm_running) {
|
|
+ vm_start();
|
|
+ }
|
|
+
|
|
+ snap_state.state = SAVE_STATE_DONE;
|
|
+}
|
|
+
|
|
+void qmp_snapshot_drive(const char *device, const char *name, Error **errp)
|
|
+{
|
|
+ BlockBackend *blk;
|
|
+ BlockDriverState *bs;
|
|
+ QEMUSnapshotInfo sn1, *sn = &sn1;
|
|
+ int ret;
|
|
+#ifdef _WIN32
|
|
+ struct _timeb tb;
|
|
+#else
|
|
+ struct timeval tv;
|
|
+#endif
|
|
+
|
|
+ if (snap_state.state != SAVE_STATE_COMPLETED) {
|
|
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
|
|
+ "VM snapshot not ready/started\n");
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ blk = blk_by_name(device);
|
|
+ if (!blk) {
|
|
+ error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
|
|
+ "Device '%s' not found", device);
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ bs = blk_bs(blk);
|
|
+ if (!bdrv_is_inserted(bs)) {
|
|
+ error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ if (bdrv_is_read_only(bs)) {
|
|
+ error_setg(errp, "Node '%s' is read only", device);
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ if (!bdrv_can_snapshot(bs)) {
|
|
+ error_setg(errp, QERR_UNSUPPORTED);
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ if (bdrv_snapshot_find(bs, sn, name) >= 0) {
|
|
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
|
|
+ "snapshot '%s' already exists", name);
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ sn = &sn1;
|
|
+ memset(sn, 0, sizeof(*sn));
|
|
+
|
|
+#ifdef _WIN32
|
|
+ _ftime(&tb);
|
|
+ sn->date_sec = tb.time;
|
|
+ sn->date_nsec = tb.millitm * 1000000;
|
|
+#else
|
|
+ gettimeofday(&tv, NULL);
|
|
+ sn->date_sec = tv.tv_sec;
|
|
+ sn->date_nsec = tv.tv_usec * 1000;
|
|
+#endif
|
|
+ sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
|
+
|
|
+ pstrcpy(sn->name, sizeof(sn->name), name);
|
|
+
|
|
+ sn->vm_state_size = 0; /* do not save state */
|
|
+
|
|
+ ret = bdrv_snapshot_create(bs, sn);
|
|
+ if (ret < 0) {
|
|
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
|
|
+ "Error while creating snapshot on '%s'\n", device);
|
|
+ return;
|
|
+ }
|
|
+}
|
|
+
|
|
+void qmp_delete_drive_snapshot(const char *device, const char *name,
|
|
+ Error **errp)
|
|
+{
|
|
+ BlockBackend *blk;
|
|
+ BlockDriverState *bs;
|
|
+ QEMUSnapshotInfo sn1, *sn = &sn1;
|
|
+ Error *local_err = NULL;
|
|
+
|
|
+ int ret;
|
|
+
|
|
+ blk = blk_by_name(device);
|
|
+ if (!blk) {
|
|
+ error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
|
|
+ "Device '%s' not found", device);
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ bs = blk_bs(blk);
|
|
+ if (bdrv_is_read_only(bs)) {
|
|
+ error_setg(errp, "Node '%s' is read only", device);
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ if (!bdrv_can_snapshot(bs)) {
|
|
+ error_setg(errp, QERR_UNSUPPORTED);
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ if (bdrv_snapshot_find(bs, sn, name) < 0) {
|
|
+ /* return success if snapshot does not exists */
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ ret = bdrv_snapshot_delete(bs, NULL, name, &local_err);
|
|
+ if (ret < 0) {
|
|
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
|
|
+ "Error while deleting snapshot on '%s'\n", device);
|
|
+ return;
|
|
+ }
|
|
+}
|
|
+
|
|
+static ssize_t loadstate_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
|
|
+ size_t size)
|
|
+{
|
|
+ BlockBackend *be = opaque;
|
|
+ int64_t maxlen = blk_getlength(be);
|
|
+ if (pos > maxlen) {
|
|
+ return -EIO;
|
|
+ }
|
|
+ if ((pos + size) > maxlen) {
|
|
+ size = maxlen - pos - 1;
|
|
+ }
|
|
+ if (size == 0) {
|
|
+ return 0;
|
|
+ }
|
|
+ return blk_pread(be, pos, buf, size);
|
|
+}
|
|
+
|
|
+static const QEMUFileOps loadstate_file_ops = {
|
|
+ .get_buffer = loadstate_get_buffer,
|
|
+};
|
|
+
|
|
+int load_state_from_blockdev(const char *filename)
|
|
+{
|
|
+ BlockBackend *be;
|
|
+ Error *local_err = NULL;
|
|
+ Error *blocker = NULL;
|
|
+
|
|
+ QEMUFile *f;
|
|
+ int ret = -EINVAL;
|
|
+
|
|
+ be = blk_new_open(filename, NULL, NULL, 0, &local_err);
|
|
+
|
|
+ if (!be) {
|
|
+ error_report("Could not open VM state file");
|
|
+ goto the_end;
|
|
+ }
|
|
+
|
|
+ error_setg(&blocker, "block device is in use by load state");
|
|
+ blk_op_block_all(be, blocker);
|
|
+
|
|
+ /* restore the VM state */
|
|
+ f = qemu_fopen_ops(be, &loadstate_file_ops);
|
|
+ if (!f) {
|
|
+ error_report("Could not open VM state file");
|
|
+ goto the_end;
|
|
+ }
|
|
+
|
|
+ qemu_system_reset(VMRESET_SILENT);
|
|
+ ret = qemu_loadvm_state(f);
|
|
+
|
|
+ qemu_fclose(f);
|
|
+ migration_incoming_state_destroy();
|
|
+ if (ret < 0) {
|
|
+ error_report("Error %d while loading VM state", ret);
|
|
+ goto the_end;
|
|
+ }
|
|
+
|
|
+ ret = 0;
|
|
+
|
|
+ the_end:
|
|
+ if (be) {
|
|
+ blk_op_unblock_all(be, blocker);
|
|
+ error_free(blocker);
|
|
+ blk_unref(be);
|
|
+ }
|
|
+ return ret;
|
|
+}
|
|
diff --git a/vl.c b/vl.c
|
|
index 46de1b9087..2132a77129 100644
|
|
--- a/vl.c
|
|
+++ b/vl.c
|
|
@@ -2960,6 +2960,7 @@ int main(int argc, char **argv, char **envp)
|
|
int optind;
|
|
const char *optarg;
|
|
const char *loadvm = NULL;
|
|
+ const char *loadstate = NULL;
|
|
MachineClass *machine_class;
|
|
const char *cpu_model;
|
|
const char *vga_model = NULL;
|
|
@@ -3635,6 +3636,9 @@ int main(int argc, char **argv, char **envp)
|
|
case QEMU_OPTION_loadvm:
|
|
loadvm = optarg;
|
|
break;
|
|
+ case QEMU_OPTION_loadstate:
|
|
+ loadstate = optarg;
|
|
+ break;
|
|
case QEMU_OPTION_full_screen:
|
|
full_screen = 1;
|
|
break;
|
|
@@ -4693,6 +4697,10 @@ int main(int argc, char **argv, char **envp)
|
|
if (load_vmstate(loadvm) < 0) {
|
|
autostart = 0;
|
|
}
|
|
+ } else if (loadstate) {
|
|
+ if (load_state_from_blockdev(loadstate) < 0) {
|
|
+ autostart = 0;
|
|
+ }
|
|
}
|
|
|
|
qdev_prop_check_globals();
|
|
--
|
|
2.11.0
|
|
|