1 Index: new/qapi-schema.json
2 ===================================================================
3 --- new.orig/qapi-schema.json 2012-09-19 12:53:31.000000000 +0200
4 +++ new/qapi-schema.json 2012-09-19 12:54:14.000000000 +0200
8 { 'command': 'query-target', 'returns': 'TargetInfo' }
11 +{ 'command': 'snapshot-start' 'data': { '*statefile': 'str' } }
13 +{ 'command': 'snapshot-drive', 'data': { 'device': 'str', 'name': 'str' } }
15 +{ 'command': 'delete-drive-snapshot', 'data': { 'device': 'str', 'name': 'str' } }
17 +{ 'command': 'snapshot-end' }
19 ===================================================================
20 --- new.orig/qmp.c 2012-09-19 12:53:31.000000000 +0200
21 +++ new/qmp.c 2012-09-19 14:04:48.000000000 +0200
23 return arch_query_cpu_definitions(errp);
26 +static struct SnapshotState {
28 + int saved_vm_running;
31 +void qmp_snapshot_start(bool has_statefile, const char *statefile, Error **errp)
33 + BlockDriverState *bs = NULL;
34 + BlockDriver *drv = NULL;
35 + int bdrv_oflags = BDRV_O_CACHE_WB | BDRV_O_RDWR;
40 + if (snap_state.in_progress) {
41 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
42 + "VM snapshot already started\n");
46 + snap_state.in_progress = 1;
48 + snap_state.saved_vm_running = runstate_is_running();
50 + vm_stop(RUN_STATE_SAVE_VM);
52 + if (!has_statefile) {
56 + /* Open the image */
57 + bs = bdrv_new("vmstate");
58 + ret = bdrv_open(bs, statefile, bdrv_oflags, drv);
60 + error_set(errp, QERR_OPEN_FILE_FAILED, statefile);
64 + f = qemu_fopen_bdrv(bs, 1);
66 + error_set(errp, QERR_OPEN_FILE_FAILED, statefile);
70 + ret = qemu_savevm_state(f);
72 + // try to truncate, but ignore errors (will fail on block devices).
73 + // note: bdrv_read() need whole blocks, so we round up
74 + pos = qemu_ftell(f);
75 + bdrv_truncate(bs, (pos + BDRV_SECTOR_SIZE) & BDRV_SECTOR_MASK);
80 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
81 + "Error %d while writing VM state\n", ret);
92 + snap_state.in_progress = 0;
94 + if (snap_state.saved_vm_running) {
101 +void qmp_snapshot_end(Error **errp)
103 + if (!snap_state.in_progress) {
104 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
105 + "VM snapshot not started\n");
108 + snap_state.in_progress = 0;
110 + if (snap_state.saved_vm_running) {
115 +void qmp_snapshot_drive(const char *device, const char *name, Error **errp)
117 + BlockDriverState *bs;
118 + QEMUSnapshotInfo sn1, *sn = &sn1;
126 + if (!snap_state.in_progress) {
127 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
128 + "VM snapshot not started\n");
132 + bs = bdrv_find(device);
134 + error_set(errp, QERR_DEVICE_NOT_FOUND, device);
138 + if (!bdrv_is_inserted(bs)) {
139 + error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
143 + if (bdrv_is_read_only(bs)) {
144 + error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
148 + if (!bdrv_can_snapshot(bs)) {
149 + error_set(errp, QERR_NOT_SUPPORTED);
153 + if (bdrv_snapshot_find(bs, sn, name) >= 0) {
154 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
155 + "snapshot '%s' already exists", name);
160 + memset(sn, 0, sizeof(*sn));
164 + sn->date_sec = tb.time;
165 + sn->date_nsec = tb.millitm * 1000000;
167 + gettimeofday(&tv, NULL);
168 + sn->date_sec = tv.tv_sec;
169 + sn->date_nsec = tv.tv_usec * 1000;
171 + sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
173 + pstrcpy(sn->name, sizeof(sn->name), name);
175 + sn->vm_state_size = 0; /* do not save state */
177 + ret = bdrv_snapshot_create(bs, sn);
179 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
180 + "Error while creating snapshot on '%s'\n", device);
185 +void qmp_delete_drive_snapshot(const char *device, const char *name,
188 + BlockDriverState *bs;
189 + QEMUSnapshotInfo sn1, *sn = &sn1;
192 + bs = bdrv_find(device);
194 + error_set(errp, QERR_DEVICE_NOT_FOUND, device);
197 + if (bdrv_is_read_only(bs)) {
198 + error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
202 + if (!bdrv_can_snapshot(bs)) {
203 + error_set(errp, QERR_NOT_SUPPORTED);
207 + if (bdrv_snapshot_find(bs, sn, name) < 0) {
208 + /* return success if snapshot does not exists */
212 + ret = bdrv_snapshot_delete(bs, name);
214 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
215 + "Error while deleting snapshot on '%s'\n", device);
219 Index: new/qmp-commands.hx
220 ===================================================================
221 --- new.orig/qmp-commands.hx 2012-09-19 12:53:31.000000000 +0200
222 +++ new/qmp-commands.hx 2012-09-19 12:54:14.000000000 +0200
223 @@ -2514,3 +2514,27 @@
225 .mhandler.cmd_new = qmp_marshal_input_query_target,
229 + .name = "snapshot-start",
230 + .args_type = "statefile:s?",
231 + .mhandler.cmd_new = qmp_marshal_input_snapshot_start,
235 + .name = "snapshot-drive",
236 + .args_type = "device:s,name:s",
237 + .mhandler.cmd_new = qmp_marshal_input_snapshot_drive,
241 + .name = "delete-drive-snapshot",
242 + .args_type = "device:s,name:s",
243 + .mhandler.cmd_new = qmp_marshal_input_delete_drive_snapshot,
247 + .name = "snapshot-end",
249 + .mhandler.cmd_new = qmp_marshal_input_snapshot_end,
252 ===================================================================
253 --- new.orig/hmp.c 2012-09-19 12:53:31.000000000 +0200
254 +++ new/hmp.c 2012-09-19 12:54:14.000000000 +0200
255 @@ -1102,3 +1102,40 @@
256 qmp_closefd(fdname, &errp);
257 hmp_handle_error(mon, &errp);
260 +void hmp_snapshot_start(Monitor *mon, const QDict *qdict)
262 + Error *errp = NULL;
263 + const char *statefile = qdict_get_try_str(qdict, "statefile");
265 + qmp_snapshot_start(statefile != NULL, statefile, &errp);
266 + hmp_handle_error(mon, &errp);
269 +void hmp_snapshot_drive(Monitor *mon, const QDict *qdict)
271 + Error *errp = NULL;
272 + const char *name = qdict_get_str(qdict, "name");
273 + const char *device = qdict_get_str(qdict, "device");
275 + qmp_snapshot_drive(device, name, &errp);
276 + hmp_handle_error(mon, &errp);
279 +void hmp_delete_drive_snapshot(Monitor *mon, const QDict *qdict)
281 + Error *errp = NULL;
282 + const char *name = qdict_get_str(qdict, "name");
283 + const char *device = qdict_get_str(qdict, "device");
285 + qmp_delete_drive_snapshot(device, name, &errp);
286 + hmp_handle_error(mon, &errp);
289 +void hmp_snapshot_end(Monitor *mon, const QDict *qdict)
291 + Error *errp = NULL;
293 + qmp_snapshot_end(&errp);
294 + hmp_handle_error(mon, &errp);
297 ===================================================================
298 --- new.orig/hmp.h 2012-09-19 12:53:31.000000000 +0200
299 +++ new/hmp.h 2012-09-19 12:54:14.000000000 +0200
301 void hmp_netdev_del(Monitor *mon, const QDict *qdict);
302 void hmp_getfd(Monitor *mon, const QDict *qdict);
303 void hmp_closefd(Monitor *mon, const QDict *qdict);
304 +void hmp_snapshot_start(Monitor *mon, const QDict *qdict);
305 +void hmp_snapshot_drive(Monitor *mon, const QDict *qdict);
306 +void hmp_delete_drive_snapshot(Monitor *mon, const QDict *qdict);
307 +void hmp_snapshot_end(Monitor *mon, const QDict *qdict);
310 Index: new/hmp-commands.hx
311 ===================================================================
312 --- new.orig/hmp-commands.hx 2012-09-19 12:53:31.000000000 +0200
313 +++ new/hmp-commands.hx 2012-09-19 12:54:14.000000000 +0200
314 @@ -1494,3 +1494,35 @@
320 + .name = "snapshot-start",
321 + .args_type = "statefile:s?",
322 + .params = "[statefile]",
323 + .help = "Prepare for snapshot and halt VM. Save VM state to statefile.",
324 + .mhandler.cmd = hmp_snapshot_start,
328 + .name = "snapshot-drive",
329 + .args_type = "device:s,name:s",
330 + .params = "device name",
331 + .help = "Create internal snapshot.",
332 + .mhandler.cmd = hmp_snapshot_drive,
336 + .name = "delete-drive-snapshot",
337 + .args_type = "device:s,name:s",
338 + .params = "device name",
339 + .help = "Delete internal snapshot.",
340 + .mhandler.cmd = hmp_delete_drive_snapshot,
344 + .name = "snapshot-end",
347 + .help = "Resume VM after snaphot.",
348 + .mhandler.cmd = hmp_snapshot_end,
351 ===================================================================
352 --- new.orig/sysemu.h 2012-09-19 12:53:31.000000000 +0200
353 +++ new/sysemu.h 2012-09-19 12:54:14.000000000 +0200
355 int qemu_savevm_state_iterate(QEMUFile *f);
356 int qemu_savevm_state_complete(QEMUFile *f);
357 void qemu_savevm_state_cancel(QEMUFile *f);
358 +int qemu_savevm_state(QEMUFile *f);
359 int qemu_loadvm_state(QEMUFile *f);
363 ===================================================================
364 --- new.orig/savevm.c 2012-09-19 12:54:11.000000000 +0200
365 +++ new/savevm.c 2012-09-19 14:09:29.000000000 +0200
368 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
371 int64_t maxlen = bdrv_getlength(opaque);
374 @@ -1754,7 +1753,7 @@
378 -static int qemu_savevm_state(QEMUFile *f)
379 +int qemu_savevm_state(QEMUFile *f)
382 MigrationParams params = {