1 #include "qemu-common.h"
4 #include "qmp-commands.h"
6 #include "qemu/qom-qobject.h"
7 #include "buffered_file.h"
10 //#define DEBUG_SAVEVM_STATE
12 #ifdef DEBUG_SAVEVM_STATE
13 #define DPRINTF(fmt, ...) \
14 do { printf("savevm-async: " fmt, ## __VA_ARGS__); } while (0)
16 #define DPRINTF(fmt, ...) \
27 static struct SnapshotState
{
37 SaveVMInfo
*qmp_query_savevm(Error
**errp
)
39 SaveVMInfo
*info
= g_malloc0(sizeof(*info
));
40 struct SnapshotState
*s
= &snap_state
;
42 if (s
->state
!= SAVE_STATE_DONE
) {
43 info
->has_bytes
= true;
44 info
->bytes
= s
->bs_pos
;
46 case SAVE_STATE_ERROR
:
47 info
->has_status
= true;
48 info
->status
= g_strdup("failed");
49 info
->has_total_time
= true;
50 info
->total_time
= s
->total_time
;
52 info
->has_error
= true;
53 info
->error
= g_strdup(error_get_pretty(s
->error
));
56 case SAVE_STATE_ACTIVE
:
57 info
->has_status
= true;
58 info
->status
= g_strdup("active");
59 info
->has_total_time
= true;
60 info
->total_time
= qemu_get_clock_ms(rt_clock
)
63 case SAVE_STATE_COMPLETED
:
64 info
->has_status
= true;
65 info
->status
= g_strdup("completed");
66 info
->has_total_time
= true;
67 info
->total_time
= s
->total_time
;
75 static int save_snapshot_cleanup(void)
79 DPRINTF("save_snapshot_cleanup\n");
81 snap_state
.total_time
= qemu_get_clock_ms(rt_clock
) -
82 snap_state
.total_time
;
84 if (snap_state
.file
) {
85 ret
= qemu_fclose(snap_state
.file
);
89 // try to truncate, but ignore errors (will fail on block devices).
90 // note: bdrv_read() need whole blocks, so we round up
91 size_t size
= (snap_state
.bs_pos
+ BDRV_SECTOR_SIZE
) & BDRV_SECTOR_MASK
;
92 bdrv_truncate(snap_state
.bs
, size
);
94 bdrv_delete(snap_state
.bs
);
101 static void save_snapshot_error(const char *fmt
, ...)
107 msg
= g_strdup_vprintf(fmt
, ap
);
110 DPRINTF("save_snapshot_error: %s\n", msg
);
112 if (!snap_state
.error
) {
113 error_set(&snap_state
.error
, ERROR_CLASS_GENERIC_ERROR
, "%s", msg
);
118 snap_state
.state
= SAVE_STATE_ERROR
;
120 save_snapshot_cleanup();
123 static void save_snapshot_completed(void)
125 DPRINTF("save_snapshot_completed\n");
127 if (save_snapshot_cleanup() < 0) {
128 snap_state
.state
= SAVE_STATE_ERROR
;
130 snap_state
.state
= SAVE_STATE_COMPLETED
;
134 static int block_state_close(void *opaque
)
136 snap_state
.file
= NULL
;
137 return bdrv_flush(snap_state
.bs
);
140 static ssize_t
block_state_put_buffer(void *opaque
, const void *buf
,
145 if ((ret
= bdrv_pwrite(snap_state
.bs
, snap_state
.bs_pos
, buf
, size
)) > 0) {
146 snap_state
.bs_pos
+= ret
;
152 static void block_state_put_ready(void *opaque
)
156 if (snap_state
.state
!= SAVE_STATE_ACTIVE
) {
157 save_snapshot_error("put_ready returning because of non-active state");
161 if (!runstate_check(RUN_STATE_SAVE_VM
)) {
162 save_snapshot_error("put_ready returning because of wrong run state");
166 ret
= qemu_savevm_state_iterate(snap_state
.file
);
168 save_snapshot_error("qemu_savevm_state_iterate error %d", ret
);
170 } else if (ret
== 1) {
171 DPRINTF("savevm inerate finished\n");
172 if ((ret
= qemu_savevm_state_complete(snap_state
.file
)) < 0) {
173 save_snapshot_error("qemu_savevm_state_complete error %d", ret
);
176 DPRINTF("save complete\n");
177 save_snapshot_completed();
183 static void block_state_wait_for_unfreeze(void *opaque
)
185 /* do nothing here - should not be called */
188 void qmp_savevm_start(bool has_statefile
, const char *statefile
, Error
**errp
)
190 BlockDriver
*drv
= NULL
;
191 int bdrv_oflags
= BDRV_O_CACHE_WB
| BDRV_O_RDWR
;
192 MigrationParams params
= {
198 if (snap_state
.state
!= SAVE_STATE_DONE
) {
199 error_set(errp
, ERROR_CLASS_GENERIC_ERROR
,
200 "VM snapshot already started\n");
204 /* initialize snapshot info */
205 snap_state
.saved_vm_running
= runstate_is_running();
206 snap_state
.bs_pos
= 0;
207 snap_state
.total_time
= qemu_get_clock_ms(rt_clock
);
209 if (snap_state
.error
) {
210 error_free(snap_state
.error
);
211 snap_state
.error
= NULL
;
215 vm_stop(RUN_STATE_SAVE_VM
);
217 if (!has_statefile
) {
218 snap_state
.state
= SAVE_STATE_COMPLETED
;
222 if (qemu_savevm_state_blocked(errp
)) {
227 snap_state
.bs
= bdrv_new("vmstate");
228 ret
= bdrv_open(snap_state
.bs
, statefile
, bdrv_oflags
, drv
);
230 error_set(errp
, QERR_OPEN_FILE_FAILED
, statefile
);
234 snap_state
.file
= qemu_fopen_ops_buffered(&snap_state
, 1000000000,
235 block_state_put_buffer
,
236 block_state_put_ready
,
237 block_state_wait_for_unfreeze
,
240 if (!snap_state
.file
) {
241 error_set(errp
, QERR_OPEN_FILE_FAILED
, statefile
);
245 snap_state
.state
= SAVE_STATE_ACTIVE
;
247 ret
= qemu_savevm_state_begin(snap_state
.file
, ¶ms
);
249 error_set(errp
, ERROR_CLASS_GENERIC_ERROR
,
250 "qemu_savevm_state_begin failed\n");
254 block_state_put_ready(&snap_state
);
260 save_snapshot_error("setup failed");
262 if (snap_state
.saved_vm_running
) {
267 void qmp_savevm_end(Error
**errp
)
269 if (snap_state
.state
== SAVE_STATE_DONE
) {
270 error_set(errp
, ERROR_CLASS_GENERIC_ERROR
,
271 "VM snapshot not started\n");
275 if (snap_state
.saved_vm_running
) {
279 snap_state
.state
= SAVE_STATE_DONE
;
282 void qmp_snapshot_drive(const char *device
, const char *name
, Error
**errp
)
284 BlockDriverState
*bs
;
285 QEMUSnapshotInfo sn1
, *sn
= &sn1
;
293 if (snap_state
.state
!= SAVE_STATE_COMPLETED
) {
294 error_set(errp
, ERROR_CLASS_GENERIC_ERROR
,
295 "VM snapshot not ready/started\n");
299 bs
= bdrv_find(device
);
301 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
305 if (!bdrv_is_inserted(bs
)) {
306 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
310 if (bdrv_is_read_only(bs
)) {
311 error_set(errp
, QERR_DEVICE_IS_READ_ONLY
, device
);
315 if (!bdrv_can_snapshot(bs
)) {
316 error_set(errp
, QERR_NOT_SUPPORTED
);
320 if (bdrv_snapshot_find(bs
, sn
, name
) >= 0) {
321 error_set(errp
, ERROR_CLASS_GENERIC_ERROR
,
322 "snapshot '%s' already exists", name
);
327 memset(sn
, 0, sizeof(*sn
));
331 sn
->date_sec
= tb
.time
;
332 sn
->date_nsec
= tb
.millitm
* 1000000;
334 gettimeofday(&tv
, NULL
);
335 sn
->date_sec
= tv
.tv_sec
;
336 sn
->date_nsec
= tv
.tv_usec
* 1000;
338 sn
->vm_clock_nsec
= qemu_get_clock_ns(vm_clock
);
340 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
342 sn
->vm_state_size
= 0; /* do not save state */
344 ret
= bdrv_snapshot_create(bs
, sn
);
346 error_set(errp
, ERROR_CLASS_GENERIC_ERROR
,
347 "Error while creating snapshot on '%s'\n", device
);
352 void qmp_delete_drive_snapshot(const char *device
, const char *name
,
355 BlockDriverState
*bs
;
356 QEMUSnapshotInfo sn1
, *sn
= &sn1
;
359 bs
= bdrv_find(device
);
361 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
364 if (bdrv_is_read_only(bs
)) {
365 error_set(errp
, QERR_DEVICE_IS_READ_ONLY
, device
);
369 if (!bdrv_can_snapshot(bs
)) {
370 error_set(errp
, QERR_NOT_SUPPORTED
);
374 if (bdrv_snapshot_find(bs
, sn
, name
) < 0) {
375 /* return success if snapshot does not exists */
379 ret
= bdrv_snapshot_delete(bs
, name
);
381 error_set(errp
, ERROR_CLASS_GENERIC_ERROR
,
382 "Error while deleting snapshot on '%s'\n", device
);
387 static int loadstate_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
389 BlockDriverState
*bs
= (BlockDriverState
*)opaque
;
390 int64_t maxlen
= bdrv_getlength(bs
);
394 if ((pos
+ size
) > maxlen
) {
395 size
= maxlen
- pos
- 1;
400 return bdrv_pread(bs
, pos
, buf
, size
);
403 int load_state_from_blockdev(const char *filename
)
405 BlockDriverState
*bs
= NULL
;
406 BlockDriver
*drv
= NULL
;
410 bs
= bdrv_new("vmstate");
411 ret
= bdrv_open(bs
, filename
, BDRV_O_CACHE_WB
, drv
);
413 error_report("Could not open VM state file");
417 /* restore the VM state */
418 f
= qemu_fopen_ops(bs
, NULL
, loadstate_get_buffer
, NULL
, NULL
, NULL
, NULL
);
420 error_report("Could not open VM state file");
425 qemu_system_reset(VMRESET_SILENT
);
426 ret
= qemu_loadvm_state(f
);
430 error_report("Error %d while loading VM state", ret
);