1 From 9901e8c82fb25ce5ce82c9e18276d8190bb4aca7 Mon Sep 17 00:00:00 2001
2 From: Dietmar Maurer <dietmar@proxmox.com>
3 Date: Thu, 29 Nov 2012 10:46:49 +0100
4 Subject: [PATCH v3 6/6] add vm state to backups
7 Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
9 blockdev.c | 196 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
11 qapi-schema.json | 6 +-
12 3 files changed, 200 insertions(+), 5 deletions(-)
14 diff --git a/blockdev.c b/blockdev.c
15 index 99f3e02..cb9293b 100644
19 #include "arch_init.h"
22 +#include "qemu-file.h"
23 +#include "migration.h"
25 static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
27 @@ -1341,6 +1343,10 @@ static struct GenericBackupState {
31 + unsigned char buf[BACKUP_CLUSTER_SIZE];
33 + size_t buf_cluster_num;
34 + guint8 vmstate_dev_id;
37 typedef struct BackupCB {
38 @@ -1494,10 +1500,170 @@ static void backup_start_jobs(void)
39 backup_run_next_job();
42 +static int backup_state_close(void *opaque)
44 + if (!backup_state.buf_index) {
48 + size_t zero_bytes = 0;
49 + if (backup_state.buf_index < BACKUP_CLUSTER_SIZE) {
50 + memset(backup_state.buf + backup_state.buf_index, 0,
51 + BACKUP_CLUSTER_SIZE - backup_state.buf_index);
53 + int bytes = backup_state.driver->dump_cb(
54 + backup_state.writer, backup_state.vmstate_dev_id,
55 + backup_state.buf_cluster_num,
56 + backup_state.buf, &zero_bytes);
57 + backup_state.buf_index = 0;
59 + return bytes < 0 ? -1 : 0;
62 +static int backup_state_put_buffer(void *opaque, const uint8_t *buf,
63 + int64_t pos, int size)
65 + assert(backup_state.driver);
66 + assert(backup_state.writer);
67 + assert(backup_state.driver->dump_cb);
69 + /* Note: our backup driver expects to get whole clusters (64KB) */
74 + int l = BACKUP_CLUSTER_SIZE - backup_state.buf_index;
75 + l = l > size ? size : l;
76 + memcpy(backup_state.buf + backup_state.buf_index, buf, l);
77 + backup_state.buf_index += l;
80 + if (backup_state.buf_index == BACKUP_CLUSTER_SIZE) {
81 + size_t zero_bytes = 0;
82 + int bytes = backup_state.driver->dump_cb(
83 + backup_state.writer, backup_state.vmstate_dev_id,
84 + backup_state.buf_cluster_num++,
85 + backup_state.buf, &zero_bytes);
86 + backup_state.buf_index = 0;
96 +static const QEMUFileOps backup_file_ops = {
97 + .put_buffer = backup_state_put_buffer,
98 + .close = backup_state_close,
101 +static void coroutine_fn backup_start_savevm(void *opaque)
103 + assert(backup_state.driver);
104 + assert(backup_state.writer);
107 + uint64_t remaining;
109 + MigrationParams params = {
116 + QEMUFile *file = qemu_fopen_ops(NULL, &backup_file_ops);
118 + ret = qemu_savevm_state_begin(file, ¶ms);
121 + err = g_strdup("qemu_savevm_state_begin failed");
126 + ret = qemu_savevm_state_iterate(file);
127 + remaining = ram_bytes_remaining();
131 + err = g_strdup_printf("qemu_savevm_state_iterate error %d", ret);
135 + /* stop the VM if we use too much space,
136 + * or if remaining is just a few MB
138 + maxlen = ram_bytes_total();
139 + size_t cpos = backup_state.buf_cluster_num * BACKUP_CLUSTER_SIZE;
140 + if ((remaining < 100000) || ((cpos + remaining) >= maxlen)) {
141 + if (runstate_is_running()) {
143 + vm_stop(RUN_STATE_SAVE_VM);
147 + if (ret == 1) { /* finished */
148 + if (runstate_is_running()) {
150 + vm_stop(RUN_STATE_SAVE_VM);
153 + ret = qemu_savevm_state_complete(file);
156 + err = g_strdup("qemu_savevm_state_complete error");
160 + if (qemu_fclose(file) < 0) {
161 + error_setg(&backup_state.error,
162 + "backup_start_savevm: qemu_fclose failed");
165 + if (backup_state.driver->complete_cb(backup_state.writer,
166 + backup_state.vmstate_dev_id, 0) < 0) {
167 + err = g_strdup("backup_start_savevm: complete_cb failed");
170 + backup_run_next_job();
183 + backup_state.end_time = time(NULL);
185 + Error *local_err = NULL;
186 + backup_state.driver->close_cb(backup_state.writer, &local_err);
187 + backup_state.writer = NULL;
189 + error_propagate(&backup_state.error, local_err);
192 + if (!backup_state.error) {
193 + error_setg(&backup_state.error, "%s", err);
201 char *qmp_backup(const char *backupfile, bool has_format, BackupFormat format,
202 bool has_config_filename, const char *config_filename,
203 bool has_devlist, const char *devlist,
204 - bool has_speed, int64_t speed, Error **errp)
205 + bool has_speed, int64_t speed,
206 + bool has_state, bool state, Error **errp)
208 BlockDriverState *bs;
209 Error *local_err = NULL;
210 @@ -1506,6 +1672,8 @@ char *qmp_backup(const char *backupfile, bool has_format, BackupFormat format,
212 GList *bcblist = NULL;
214 + bool save_state = has_state ? state : false;
216 /* Todo: try to auto-detect format based on file name */
217 format = has_format ? format : BACKUP_FORMAT_VMA;
219 @@ -1586,6 +1754,22 @@ char *qmp_backup(const char *backupfile, bool has_format, BackupFormat format,
222 /* register all devices for vma writer */
224 + guint8 vmstate_dev_id = 0;
226 + /* Note: we pass ram_bytes_total() for vmstate size
227 + * The backup driver needs to be aware of the fact
228 + * that the real stream size can be different (we do
229 + * not know that size in advance).
231 + size_t ramsize = ram_bytes_total();
232 + vmstate_dev_id = driver->register_stream_cb(writer, "vmstate", ramsize);
233 + if (vmstate_dev_id <= 0) {
234 + error_setg(errp, "register vmstate stream failed");
241 BackupCB *bcb = l->data;
242 @@ -1651,6 +1835,9 @@ char *qmp_backup(const char *backupfile, bool has_format, BackupFormat format,
243 backup_state.total = total;
244 backup_state.transferred = 0;
245 backup_state.zero_bytes = 0;
246 + backup_state.buf_index = 0;
247 + backup_state.buf_cluster_num = 0;
248 + backup_state.vmstate_dev_id = vmstate_dev_id;
250 /* Grab a reference so hotplug does not delete the
251 * BlockDriverState from underneath us.
252 @@ -1662,7 +1849,12 @@ char *qmp_backup(const char *backupfile, bool has_format, BackupFormat format,
253 drive_get_ref(drive_get_by_blockdev(bcb->bs));
256 - backup_start_jobs();
258 + Coroutine *co = qemu_coroutine_create(backup_start_savevm);
259 + qemu_coroutine_enter(co, NULL);
261 + backup_start_jobs();
264 return g_strdup(backup_state.uuid_str);
266 diff --git a/hmp.c b/hmp.c
267 index 27fd421..56e1cdc 100644
270 @@ -1031,7 +1031,8 @@ void hmp_backup(Monitor *mon, const QDict *qdict)
273 qmp_backup(backupfile, true, BACKUP_FORMAT_VMA, false, NULL, !!devlist,
274 - devlist, qdict_haskey(qdict, "speed"), speed, &errp);
275 + devlist, qdict_haskey(qdict, "speed"), speed, false, false,
278 if (error_is_set(&errp)) {
279 monitor_printf(mon, "%s\n", error_get_pretty(errp));
280 diff --git a/qapi-schema.json b/qapi-schema.json
281 index 2d3699b..3f4889e 100644
282 --- a/qapi-schema.json
283 +++ b/qapi-schema.json
284 @@ -1822,13 +1822,15 @@
286 # @speed: #optional the maximum speed, in bytes per second
288 +# @state: #optional flag to include vm state
290 # Returns: the uuid of the backup job
294 { 'command': 'backup', 'data': { 'backupfile': 'str', '*format': 'BackupFormat',
295 - '*config-filename': 'str',
296 - '*devlist': 'str', '*speed': 'int' },
297 + '*config-filename': 'str', '*devlist': 'str',
298 + '*speed': 'int', '*state': 'bool' },