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