Merge commit 'b1c28b464d5f21805d322b67f008e114a484146d' into upstream-merge
[qemu-kvm/markmc.git] / savevm.c
blobe22c98139988fba427d46e37515475a88f4bed51
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
30 #include <zlib.h>
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
35 #ifndef _WIN32
36 #include <sys/times.h>
37 #include <sys/wait.h>
38 #include <termios.h>
39 #include <sys/mman.h>
40 #include <sys/ioctl.h>
41 #include <sys/resource.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <net/if.h>
45 #include <arpa/inet.h>
46 #include <dirent.h>
47 #include <netdb.h>
48 #include <sys/select.h>
49 #ifdef CONFIG_BSD
50 #include <sys/stat.h>
51 #if defined(__FreeBSD__) || defined(__DragonFly__)
52 #include <libutil.h>
53 #else
54 #include <util.h>
55 #endif
56 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
57 #include <freebsd/stdlib.h>
58 #else
59 #ifdef __linux__
60 #include <pty.h>
61 #include <malloc.h>
62 #include <linux/rtc.h>
63 #endif
64 #endif
65 #endif
67 #ifdef _WIN32
68 #include <windows.h>
69 #include <malloc.h>
70 #include <sys/timeb.h>
71 #include <mmsystem.h>
72 #define getopt_long_only getopt_long
73 #define memalign(align, size) malloc(size)
74 #endif
76 #include "qemu-common.h"
77 #include "hw/hw.h"
78 #include "net.h"
79 #include "monitor.h"
80 #include "sysemu.h"
81 #include "qemu-timer.h"
82 #include "qemu-char.h"
83 #include "block.h"
84 #include "audio/audio.h"
85 #include "migration.h"
86 #include "qemu_socket.h"
87 #include "qemu-queue.h"
89 /* point to the block driver where the snapshots are managed */
90 static BlockDriverState *bs_snapshots;
92 #define SELF_ANNOUNCE_ROUNDS 5
94 #ifndef ETH_P_RARP
95 #define ETH_P_RARP 0x0835
96 #endif
97 #define ARP_HTYPE_ETH 0x0001
98 #define ARP_PTYPE_IP 0x0800
99 #define ARP_OP_REQUEST_REV 0x3
101 static int announce_self_create(uint8_t *buf,
102 uint8_t *mac_addr)
104 /* Ethernet header. */
105 memset(buf, 0xff, 6); /* destination MAC addr */
106 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
107 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
109 /* RARP header. */
110 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
111 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
112 *(buf + 18) = 6; /* hardware addr length (ethernet) */
113 *(buf + 19) = 4; /* protocol addr length (IPv4) */
114 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
115 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
116 memset(buf + 28, 0x00, 4); /* source protocol addr */
117 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
118 memset(buf + 38, 0x00, 4); /* target protocol addr */
120 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
121 memset(buf + 42, 0x00, 18);
123 return 60; /* len (FCS will be added by hardware) */
126 static void qemu_announce_self_once(void *opaque)
128 int i, len;
129 uint8_t buf[60];
130 static int count = SELF_ANNOUNCE_ROUNDS;
131 QEMUTimer *timer = *(QEMUTimer **)opaque;
133 for (i = 0; i < MAX_NICS; i++) {
134 if (!nd_table[i].used)
135 continue;
136 len = announce_self_create(buf, nd_table[i].macaddr);
137 qemu_send_packet_raw(nd_table[i].vc, buf, len);
139 if (--count) {
140 /* delay 50ms, 150ms, 250ms, ... */
141 qemu_mod_timer(timer, qemu_get_clock(rt_clock) +
142 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
143 } else {
144 qemu_del_timer(timer);
145 qemu_free_timer(timer);
149 void qemu_announce_self(void)
151 static QEMUTimer *timer;
152 timer = qemu_new_timer(rt_clock, qemu_announce_self_once, &timer);
153 qemu_announce_self_once(&timer);
156 /***********************************************************/
157 /* savevm/loadvm support */
159 #define IO_BUF_SIZE 32768
161 struct QEMUFile {
162 QEMUFilePutBufferFunc *put_buffer;
163 QEMUFileGetBufferFunc *get_buffer;
164 QEMUFileCloseFunc *close;
165 QEMUFileRateLimit *rate_limit;
166 QEMUFileSetRateLimit *set_rate_limit;
167 void *opaque;
168 int is_write;
170 int64_t buf_offset; /* start of buffer when writing, end of buffer
171 when reading */
172 int buf_index;
173 int buf_size; /* 0 when writing */
174 uint8_t buf[IO_BUF_SIZE];
176 int has_error;
179 typedef struct QEMUFileStdio
181 FILE *stdio_file;
182 QEMUFile *file;
183 } QEMUFileStdio;
185 typedef struct QEMUFileSocket
187 int fd;
188 QEMUFile *file;
189 } QEMUFileSocket;
191 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
193 QEMUFileSocket *s = opaque;
194 ssize_t len;
196 do {
197 len = recv(s->fd, (void *)buf, size, 0);
198 } while (len == -1 && socket_error() == EINTR);
200 if (len == -1)
201 len = -socket_error();
203 return len;
206 static int socket_close(void *opaque)
208 QEMUFileSocket *s = opaque;
209 qemu_free(s);
210 return 0;
213 static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
215 QEMUFileStdio *s = opaque;
216 return fwrite(buf, 1, size, s->stdio_file);
219 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
221 QEMUFileStdio *s = opaque;
222 FILE *fp = s->stdio_file;
223 int bytes;
225 do {
226 clearerr(fp);
227 bytes = fread(buf, 1, size, fp);
228 } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
229 return bytes;
232 static int stdio_pclose(void *opaque)
234 QEMUFileStdio *s = opaque;
235 pclose(s->stdio_file);
236 qemu_free(s);
237 return 0;
240 static int stdio_fclose(void *opaque)
242 QEMUFileStdio *s = opaque;
243 fclose(s->stdio_file);
244 qemu_free(s);
245 return 0;
248 QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
250 QEMUFileStdio *s;
252 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
253 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
254 return NULL;
257 s = qemu_mallocz(sizeof(QEMUFileStdio));
259 s->stdio_file = stdio_file;
261 if(mode[0] == 'r') {
262 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose, NULL, NULL);
263 } else {
264 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose, NULL, NULL);
266 return s->file;
269 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
271 FILE *popen_file;
273 popen_file = popen(command, mode);
274 if(popen_file == NULL) {
275 return NULL;
278 return qemu_popen(popen_file, mode);
281 int qemu_stdio_fd(QEMUFile *f)
283 QEMUFileStdio *p;
284 int fd;
286 p = (QEMUFileStdio *)f->opaque;
287 fd = fileno(p->stdio_file);
289 return fd;
292 QEMUFile *qemu_fdopen(int fd, const char *mode)
294 QEMUFileStdio *s;
296 if (mode == NULL ||
297 (mode[0] != 'r' && mode[0] != 'w') ||
298 mode[1] != 'b' || mode[2] != 0) {
299 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
300 return NULL;
303 s = qemu_mallocz(sizeof(QEMUFileStdio));
304 s->stdio_file = fdopen(fd, mode);
305 if (!s->stdio_file)
306 goto fail;
308 if(mode[0] == 'r') {
309 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose, NULL, NULL);
310 } else {
311 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose, NULL, NULL);
313 return s->file;
315 fail:
316 qemu_free(s);
317 return NULL;
320 QEMUFile *qemu_fopen_socket(int fd)
322 QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
324 s->fd = fd;
325 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, NULL, NULL);
326 return s->file;
329 static int file_put_buffer(void *opaque, const uint8_t *buf,
330 int64_t pos, int size)
332 QEMUFileStdio *s = opaque;
333 fseek(s->stdio_file, pos, SEEK_SET);
334 fwrite(buf, 1, size, s->stdio_file);
335 return size;
338 static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
340 QEMUFileStdio *s = opaque;
341 fseek(s->stdio_file, pos, SEEK_SET);
342 return fread(buf, 1, size, s->stdio_file);
345 QEMUFile *qemu_fopen(const char *filename, const char *mode)
347 QEMUFileStdio *s;
349 if (mode == NULL ||
350 (mode[0] != 'r' && mode[0] != 'w') ||
351 mode[1] != 'b' || mode[2] != 0) {
352 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
353 return NULL;
356 s = qemu_mallocz(sizeof(QEMUFileStdio));
358 s->stdio_file = fopen(filename, mode);
359 if (!s->stdio_file)
360 goto fail;
362 if(mode[0] == 'w') {
363 s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose, NULL, NULL);
364 } else {
365 s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose, NULL, NULL);
367 return s->file;
368 fail:
369 qemu_free(s);
370 return NULL;
373 static int block_put_buffer(void *opaque, const uint8_t *buf,
374 int64_t pos, int size)
376 bdrv_save_vmstate(opaque, buf, pos, size);
377 return size;
380 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
382 return bdrv_load_vmstate(opaque, buf, pos, size);
385 static int bdrv_fclose(void *opaque)
387 return 0;
390 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
392 if (is_writable)
393 return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose, NULL, NULL);
394 return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL);
397 QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
398 QEMUFileGetBufferFunc *get_buffer,
399 QEMUFileCloseFunc *close,
400 QEMUFileRateLimit *rate_limit,
401 QEMUFileSetRateLimit *set_rate_limit)
403 QEMUFile *f;
405 f = qemu_mallocz(sizeof(QEMUFile));
407 f->opaque = opaque;
408 f->put_buffer = put_buffer;
409 f->get_buffer = get_buffer;
410 f->close = close;
411 f->rate_limit = rate_limit;
412 f->set_rate_limit = set_rate_limit;
413 f->is_write = 0;
415 return f;
418 int qemu_file_has_error(QEMUFile *f)
420 return f->has_error;
423 void qemu_file_set_error(QEMUFile *f)
425 f->has_error = 1;
428 void qemu_fflush(QEMUFile *f)
430 if (!f->put_buffer)
431 return;
433 if (f->is_write && f->buf_index > 0) {
434 int len;
436 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
437 if (len > 0)
438 f->buf_offset += f->buf_index;
439 else
440 f->has_error = 1;
441 f->buf_index = 0;
445 static void qemu_fill_buffer(QEMUFile *f)
447 int len;
449 if (!f->get_buffer)
450 return;
452 if (f->is_write)
453 abort();
455 len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
456 if (len > 0) {
457 f->buf_index = 0;
458 f->buf_size = len;
459 f->buf_offset += len;
460 } else if (len != -EAGAIN)
461 f->has_error = 1;
464 int qemu_fclose(QEMUFile *f)
466 int ret = 0;
467 qemu_fflush(f);
468 if (f->close)
469 ret = f->close(f->opaque);
470 qemu_free(f);
471 return ret;
474 void qemu_file_put_notify(QEMUFile *f)
476 f->put_buffer(f->opaque, NULL, 0, 0);
479 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
481 int l;
483 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
484 fprintf(stderr,
485 "Attempted to write to buffer while read buffer is not empty\n");
486 abort();
489 while (!f->has_error && size > 0) {
490 l = IO_BUF_SIZE - f->buf_index;
491 if (l > size)
492 l = size;
493 memcpy(f->buf + f->buf_index, buf, l);
494 f->is_write = 1;
495 f->buf_index += l;
496 buf += l;
497 size -= l;
498 if (f->buf_index >= IO_BUF_SIZE)
499 qemu_fflush(f);
503 void qemu_put_byte(QEMUFile *f, int v)
505 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
506 fprintf(stderr,
507 "Attempted to write to buffer while read buffer is not empty\n");
508 abort();
511 f->buf[f->buf_index++] = v;
512 f->is_write = 1;
513 if (f->buf_index >= IO_BUF_SIZE)
514 qemu_fflush(f);
517 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
519 int size, l;
521 if (f->is_write)
522 abort();
524 size = size1;
525 while (size > 0) {
526 l = f->buf_size - f->buf_index;
527 if (l == 0) {
528 qemu_fill_buffer(f);
529 l = f->buf_size - f->buf_index;
530 if (l == 0)
531 break;
533 if (l > size)
534 l = size;
535 memcpy(buf, f->buf + f->buf_index, l);
536 f->buf_index += l;
537 buf += l;
538 size -= l;
540 return size1 - size;
543 int qemu_get_byte(QEMUFile *f)
545 if (f->is_write)
546 abort();
548 if (f->buf_index >= f->buf_size) {
549 qemu_fill_buffer(f);
550 if (f->buf_index >= f->buf_size)
551 return 0;
553 return f->buf[f->buf_index++];
556 int64_t qemu_ftell(QEMUFile *f)
558 return f->buf_offset - f->buf_size + f->buf_index;
561 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
563 if (whence == SEEK_SET) {
564 /* nothing to do */
565 } else if (whence == SEEK_CUR) {
566 pos += qemu_ftell(f);
567 } else {
568 /* SEEK_END not supported */
569 return -1;
571 if (f->put_buffer) {
572 qemu_fflush(f);
573 f->buf_offset = pos;
574 } else {
575 f->buf_offset = pos;
576 f->buf_index = 0;
577 f->buf_size = 0;
579 return pos;
582 int qemu_file_rate_limit(QEMUFile *f)
584 if (f->rate_limit)
585 return f->rate_limit(f->opaque);
587 return 0;
590 size_t qemu_file_set_rate_limit(QEMUFile *f, size_t new_rate)
592 /* any failed or completed migration keeps its state to allow probing of
593 * migration data, but has no associated file anymore */
594 if (f && f->set_rate_limit)
595 return f->set_rate_limit(f->opaque, new_rate);
597 return 0;
600 void qemu_put_be16(QEMUFile *f, unsigned int v)
602 qemu_put_byte(f, v >> 8);
603 qemu_put_byte(f, v);
606 void qemu_put_be32(QEMUFile *f, unsigned int v)
608 qemu_put_byte(f, v >> 24);
609 qemu_put_byte(f, v >> 16);
610 qemu_put_byte(f, v >> 8);
611 qemu_put_byte(f, v);
614 void qemu_put_be64(QEMUFile *f, uint64_t v)
616 qemu_put_be32(f, v >> 32);
617 qemu_put_be32(f, v);
620 unsigned int qemu_get_be16(QEMUFile *f)
622 unsigned int v;
623 v = qemu_get_byte(f) << 8;
624 v |= qemu_get_byte(f);
625 return v;
628 unsigned int qemu_get_be32(QEMUFile *f)
630 unsigned int v;
631 v = qemu_get_byte(f) << 24;
632 v |= qemu_get_byte(f) << 16;
633 v |= qemu_get_byte(f) << 8;
634 v |= qemu_get_byte(f);
635 return v;
638 uint64_t qemu_get_be64(QEMUFile *f)
640 uint64_t v;
641 v = (uint64_t)qemu_get_be32(f) << 32;
642 v |= qemu_get_be32(f);
643 return v;
646 /* 8 bit int */
648 static int get_int8(QEMUFile *f, void *pv, size_t size)
650 int8_t *v = pv;
651 qemu_get_s8s(f, v);
652 return 0;
655 static void put_int8(QEMUFile *f, void *pv, size_t size)
657 int8_t *v = pv;
658 qemu_put_s8s(f, v);
661 const VMStateInfo vmstate_info_int8 = {
662 .name = "int8",
663 .get = get_int8,
664 .put = put_int8,
667 /* 16 bit int */
669 static int get_int16(QEMUFile *f, void *pv, size_t size)
671 int16_t *v = pv;
672 qemu_get_sbe16s(f, v);
673 return 0;
676 static void put_int16(QEMUFile *f, void *pv, size_t size)
678 int16_t *v = pv;
679 qemu_put_sbe16s(f, v);
682 const VMStateInfo vmstate_info_int16 = {
683 .name = "int16",
684 .get = get_int16,
685 .put = put_int16,
688 /* 32 bit int */
690 static int get_int32(QEMUFile *f, void *pv, size_t size)
692 int32_t *v = pv;
693 qemu_get_sbe32s(f, v);
694 return 0;
697 static void put_int32(QEMUFile *f, void *pv, size_t size)
699 int32_t *v = pv;
700 qemu_put_sbe32s(f, v);
703 const VMStateInfo vmstate_info_int32 = {
704 .name = "int32",
705 .get = get_int32,
706 .put = put_int32,
709 /* 32 bit int. See that the received value is the same than the one
710 in the field */
712 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
714 int32_t *v = pv;
715 int32_t v2;
716 qemu_get_sbe32s(f, &v2);
718 if (*v == v2)
719 return 0;
720 return -EINVAL;
723 const VMStateInfo vmstate_info_int32_equal = {
724 .name = "int32 equal",
725 .get = get_int32_equal,
726 .put = put_int32,
729 /* 32 bit int. See that the received value is the less or the same
730 than the one in the field */
732 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
734 int32_t *old = pv;
735 int32_t new;
736 qemu_get_sbe32s(f, &new);
738 if (*old <= new)
739 return 0;
740 return -EINVAL;
743 const VMStateInfo vmstate_info_int32_le = {
744 .name = "int32 equal",
745 .get = get_int32_le,
746 .put = put_int32,
749 /* 64 bit int */
751 static int get_int64(QEMUFile *f, void *pv, size_t size)
753 int64_t *v = pv;
754 qemu_get_sbe64s(f, v);
755 return 0;
758 static void put_int64(QEMUFile *f, void *pv, size_t size)
760 int64_t *v = pv;
761 qemu_put_sbe64s(f, v);
764 const VMStateInfo vmstate_info_int64 = {
765 .name = "int64",
766 .get = get_int64,
767 .put = put_int64,
770 /* 8 bit unsigned int */
772 static int get_uint8(QEMUFile *f, void *pv, size_t size)
774 uint8_t *v = pv;
775 qemu_get_8s(f, v);
776 return 0;
779 static void put_uint8(QEMUFile *f, void *pv, size_t size)
781 uint8_t *v = pv;
782 qemu_put_8s(f, v);
785 const VMStateInfo vmstate_info_uint8 = {
786 .name = "uint8",
787 .get = get_uint8,
788 .put = put_uint8,
791 /* 16 bit unsigned int */
793 static int get_uint16(QEMUFile *f, void *pv, size_t size)
795 uint16_t *v = pv;
796 qemu_get_be16s(f, v);
797 return 0;
800 static void put_uint16(QEMUFile *f, void *pv, size_t size)
802 uint16_t *v = pv;
803 qemu_put_be16s(f, v);
806 const VMStateInfo vmstate_info_uint16 = {
807 .name = "uint16",
808 .get = get_uint16,
809 .put = put_uint16,
812 /* 32 bit unsigned int */
814 static int get_uint32(QEMUFile *f, void *pv, size_t size)
816 uint32_t *v = pv;
817 qemu_get_be32s(f, v);
818 return 0;
821 static void put_uint32(QEMUFile *f, void *pv, size_t size)
823 uint32_t *v = pv;
824 qemu_put_be32s(f, v);
827 const VMStateInfo vmstate_info_uint32 = {
828 .name = "uint32",
829 .get = get_uint32,
830 .put = put_uint32,
833 /* 64 bit unsigned int */
835 static int get_uint64(QEMUFile *f, void *pv, size_t size)
837 uint64_t *v = pv;
838 qemu_get_be64s(f, v);
839 return 0;
842 static void put_uint64(QEMUFile *f, void *pv, size_t size)
844 uint64_t *v = pv;
845 qemu_put_be64s(f, v);
848 const VMStateInfo vmstate_info_uint64 = {
849 .name = "uint64",
850 .get = get_uint64,
851 .put = put_uint64,
854 /* 64 bit linux kernel unsigned int */
856 #ifdef __linux__
857 static int get_u64(QEMUFile *f, void *pv, size_t size)
859 __u64 *v = pv;
860 qemu_get_be64s(f, (uint64_t *)v);
861 return 0;
864 static void put_u64(QEMUFile *f, void *pv, size_t size)
866 __u64 *v = pv;
867 qemu_put_be64s(f, (uint64_t *)v);
870 const VMStateInfo vmstate_info_u64 = {
871 .name = "__u64",
872 .get = get_u64,
873 .put = put_u64,
875 #endif /* __linux__ */
877 /* 8 bit int. See that the received value is the same than the one
878 in the field */
880 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
882 uint8_t *v = pv;
883 uint8_t v2;
884 qemu_get_8s(f, &v2);
886 if (*v == v2)
887 return 0;
888 return -EINVAL;
891 const VMStateInfo vmstate_info_uint8_equal = {
892 .name = "uint8 equal",
893 .get = get_uint8_equal,
894 .put = put_uint8,
897 /* 16 bit unsigned int int. See that the received value is the same than the one
898 in the field */
900 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
902 uint16_t *v = pv;
903 uint16_t v2;
904 qemu_get_be16s(f, &v2);
906 if (*v == v2)
907 return 0;
908 return -EINVAL;
911 const VMStateInfo vmstate_info_uint16_equal = {
912 .name = "uint16 equal",
913 .get = get_uint16_equal,
914 .put = put_uint16,
917 /* timers */
919 static int get_timer(QEMUFile *f, void *pv, size_t size)
921 QEMUTimer *v = pv;
922 qemu_get_timer(f, v);
923 return 0;
926 static void put_timer(QEMUFile *f, void *pv, size_t size)
928 QEMUTimer *v = pv;
929 qemu_put_timer(f, v);
932 const VMStateInfo vmstate_info_timer = {
933 .name = "timer",
934 .get = get_timer,
935 .put = put_timer,
938 /* uint8_t buffers */
940 static int get_buffer(QEMUFile *f, void *pv, size_t size)
942 uint8_t *v = pv;
943 qemu_get_buffer(f, v, size);
944 return 0;
947 static void put_buffer(QEMUFile *f, void *pv, size_t size)
949 uint8_t *v = pv;
950 qemu_put_buffer(f, v, size);
953 const VMStateInfo vmstate_info_buffer = {
954 .name = "buffer",
955 .get = get_buffer,
956 .put = put_buffer,
959 /* unused buffers: space that was used for some fields that are
960 not usefull anymore */
962 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
964 qemu_fseek(f, size, SEEK_CUR);
965 return 0;
968 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
970 qemu_fseek(f, size, SEEK_CUR);
973 const VMStateInfo vmstate_info_unused_buffer = {
974 .name = "unused_buffer",
975 .get = get_unused_buffer,
976 .put = put_unused_buffer,
979 typedef struct SaveStateEntry {
980 QTAILQ_ENTRY(SaveStateEntry) entry;
981 char idstr[256];
982 int instance_id;
983 int version_id;
984 int section_id;
985 SaveLiveStateHandler *save_live_state;
986 SaveStateHandler *save_state;
987 LoadStateHandler *load_state;
988 const VMStateDescription *vmsd;
989 void *opaque;
990 } SaveStateEntry;
992 static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
993 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
994 static int global_section_id;
996 static int calculate_new_instance_id(const char *idstr)
998 SaveStateEntry *se;
999 int instance_id = 0;
1001 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1002 if (strcmp(idstr, se->idstr) == 0
1003 && instance_id <= se->instance_id) {
1004 instance_id = se->instance_id + 1;
1007 return instance_id;
1010 /* TODO: Individual devices generally have very little idea about the rest
1011 of the system, so instance_id should be removed/replaced.
1012 Meanwhile pass -1 as instance_id if you do not already have a clearly
1013 distinguishing id for all instances of your device class. */
1014 int register_savevm_live(const char *idstr,
1015 int instance_id,
1016 int version_id,
1017 SaveLiveStateHandler *save_live_state,
1018 SaveStateHandler *save_state,
1019 LoadStateHandler *load_state,
1020 void *opaque)
1022 SaveStateEntry *se;
1024 se = qemu_malloc(sizeof(SaveStateEntry));
1025 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1026 se->version_id = version_id;
1027 se->section_id = global_section_id++;
1028 se->save_live_state = save_live_state;
1029 se->save_state = save_state;
1030 se->load_state = load_state;
1031 se->opaque = opaque;
1032 se->vmsd = NULL;
1034 if (instance_id == -1) {
1035 se->instance_id = calculate_new_instance_id(idstr);
1036 } else {
1037 se->instance_id = instance_id;
1039 /* add at the end of list */
1040 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1041 return 0;
1044 int register_savevm(const char *idstr,
1045 int instance_id,
1046 int version_id,
1047 SaveStateHandler *save_state,
1048 LoadStateHandler *load_state,
1049 void *opaque)
1051 return register_savevm_live(idstr, instance_id, version_id,
1052 NULL, save_state, load_state, opaque);
1055 void unregister_savevm(const char *idstr, void *opaque)
1057 SaveStateEntry *se, *new_se;
1059 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1060 if (strcmp(se->idstr, idstr) == 0 && se->opaque == opaque) {
1061 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1062 qemu_free(se);
1067 int vmstate_register(int instance_id, const VMStateDescription *vmsd,
1068 void *opaque)
1070 SaveStateEntry *se;
1072 se = qemu_malloc(sizeof(SaveStateEntry));
1073 pstrcpy(se->idstr, sizeof(se->idstr), vmsd->name);
1074 se->version_id = vmsd->version_id;
1075 se->section_id = global_section_id++;
1076 se->save_live_state = NULL;
1077 se->save_state = NULL;
1078 se->load_state = NULL;
1079 se->opaque = opaque;
1080 se->vmsd = vmsd;
1082 if (instance_id == -1) {
1083 se->instance_id = calculate_new_instance_id(vmsd->name);
1084 } else {
1085 se->instance_id = instance_id;
1087 /* add at the end of list */
1088 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1089 return 0;
1092 void vmstate_unregister(const VMStateDescription *vmsd, void *opaque)
1094 SaveStateEntry *se, *new_se;
1096 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1097 if (se->vmsd == vmsd && se->opaque == opaque) {
1098 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1099 qemu_free(se);
1104 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1105 void *opaque, int version_id)
1107 VMStateField *field = vmsd->fields;
1109 if (version_id > vmsd->version_id) {
1110 return -EINVAL;
1112 if (version_id < vmsd->minimum_version_id_old) {
1113 return -EINVAL;
1115 if (version_id < vmsd->minimum_version_id) {
1116 return vmsd->load_state_old(f, opaque, version_id);
1118 if (vmsd->pre_load) {
1119 int ret = vmsd->pre_load(opaque);
1120 if (ret)
1121 return ret;
1123 while(field->name) {
1124 if ((field->field_exists &&
1125 field->field_exists(opaque, version_id)) ||
1126 (!field->field_exists &&
1127 field->version_id <= version_id)) {
1128 void *base_addr = opaque + field->offset;
1129 int ret, i, n_elems = 1;
1131 if (field->flags & VMS_ARRAY) {
1132 n_elems = field->num;
1133 } else if (field->flags & VMS_VARRAY_INT32) {
1134 n_elems = *(int32_t *)(opaque+field->num_offset);
1135 } else if (field->flags & VMS_VARRAY_UINT16) {
1136 n_elems = *(uint16_t *)(opaque+field->num_offset);
1138 if (field->flags & VMS_POINTER) {
1139 base_addr = *(void **)base_addr;
1141 for (i = 0; i < n_elems; i++) {
1142 void *addr = base_addr + field->size * i;
1144 if (field->flags & VMS_ARRAY_OF_POINTER) {
1145 addr = *(void **)addr;
1147 if (field->flags & VMS_STRUCT) {
1148 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
1149 } else {
1150 ret = field->info->get(f, addr, field->size);
1153 if (ret < 0) {
1154 return ret;
1158 field++;
1160 if (vmsd->post_load) {
1161 return vmsd->post_load(opaque, version_id);
1163 return 0;
1166 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1167 void *opaque)
1169 VMStateField *field = vmsd->fields;
1171 if (vmsd->pre_save) {
1172 vmsd->pre_save(opaque);
1174 while(field->name) {
1175 if (!field->field_exists ||
1176 field->field_exists(opaque, vmsd->version_id)) {
1177 void *base_addr = opaque + field->offset;
1178 int i, n_elems = 1;
1180 if (field->flags & VMS_ARRAY) {
1181 n_elems = field->num;
1182 } else if (field->flags & VMS_VARRAY_INT32) {
1183 n_elems = *(int32_t *)(opaque+field->num_offset);
1184 } else if (field->flags & VMS_VARRAY_UINT16) {
1185 n_elems = *(uint16_t *)(opaque+field->num_offset);
1187 if (field->flags & VMS_POINTER) {
1188 base_addr = *(void **)base_addr;
1190 for (i = 0; i < n_elems; i++) {
1191 void *addr = base_addr + field->size * i;
1193 if (field->flags & VMS_STRUCT) {
1194 vmstate_save_state(f, field->vmsd, addr);
1195 } else {
1196 field->info->put(f, addr, field->size);
1200 field++;
1202 if (vmsd->post_save) {
1203 vmsd->post_save(opaque);
1207 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1209 if (!se->vmsd) { /* Old style */
1210 return se->load_state(f, se->opaque, version_id);
1212 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
1215 static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1217 if (!se->vmsd) { /* Old style */
1218 se->save_state(f, se->opaque);
1219 return;
1221 vmstate_save_state(f,se->vmsd, se->opaque);
1224 #define QEMU_VM_FILE_MAGIC 0x5145564d
1225 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1226 #define QEMU_VM_FILE_VERSION 0x00000003
1228 #define QEMU_VM_EOF 0x00
1229 #define QEMU_VM_SECTION_START 0x01
1230 #define QEMU_VM_SECTION_PART 0x02
1231 #define QEMU_VM_SECTION_END 0x03
1232 #define QEMU_VM_SECTION_FULL 0x04
1234 int qemu_savevm_state_begin(QEMUFile *f)
1236 SaveStateEntry *se;
1238 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1239 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1241 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1242 int len;
1244 if (se->save_live_state == NULL)
1245 continue;
1247 /* Section type */
1248 qemu_put_byte(f, QEMU_VM_SECTION_START);
1249 qemu_put_be32(f, se->section_id);
1251 /* ID string */
1252 len = strlen(se->idstr);
1253 qemu_put_byte(f, len);
1254 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1256 qemu_put_be32(f, se->instance_id);
1257 qemu_put_be32(f, se->version_id);
1259 se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
1262 if (qemu_file_has_error(f))
1263 return -EIO;
1265 return 0;
1268 int qemu_savevm_state_iterate(QEMUFile *f)
1270 SaveStateEntry *se;
1271 int ret = 1;
1273 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1274 if (se->save_live_state == NULL)
1275 continue;
1277 /* Section type */
1278 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1279 qemu_put_be32(f, se->section_id);
1281 ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
1284 if (ret)
1285 return 1;
1287 if (qemu_file_has_error(f))
1288 return -EIO;
1290 return 0;
1293 int qemu_savevm_state_complete(QEMUFile *f)
1295 SaveStateEntry *se;
1297 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1298 if (se->save_live_state == NULL)
1299 continue;
1301 /* Section type */
1302 qemu_put_byte(f, QEMU_VM_SECTION_END);
1303 qemu_put_be32(f, se->section_id);
1305 se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
1308 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1309 int len;
1311 if (se->save_state == NULL && se->vmsd == NULL)
1312 continue;
1314 /* Section type */
1315 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1316 qemu_put_be32(f, se->section_id);
1318 /* ID string */
1319 len = strlen(se->idstr);
1320 qemu_put_byte(f, len);
1321 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1323 qemu_put_be32(f, se->instance_id);
1324 qemu_put_be32(f, se->version_id);
1326 vmstate_save(f, se);
1329 qemu_put_byte(f, QEMU_VM_EOF);
1331 if (qemu_file_has_error(f))
1332 return -EIO;
1334 return 0;
1337 int qemu_savevm_state(QEMUFile *f)
1339 int saved_vm_running;
1340 int ret;
1342 saved_vm_running = vm_running;
1343 vm_stop(0);
1345 bdrv_flush_all();
1347 ret = qemu_savevm_state_begin(f);
1348 if (ret < 0)
1349 goto out;
1351 do {
1352 ret = qemu_savevm_state_iterate(f);
1353 if (ret < 0)
1354 goto out;
1355 } while (ret == 0);
1357 ret = qemu_savevm_state_complete(f);
1359 out:
1360 if (qemu_file_has_error(f))
1361 ret = -EIO;
1363 if (!ret && saved_vm_running)
1364 vm_start();
1366 return ret;
1369 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1371 SaveStateEntry *se;
1373 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1374 if (!strcmp(se->idstr, idstr) &&
1375 instance_id == se->instance_id)
1376 return se;
1378 return NULL;
1381 typedef struct LoadStateEntry {
1382 QLIST_ENTRY(LoadStateEntry) entry;
1383 SaveStateEntry *se;
1384 int section_id;
1385 int version_id;
1386 } LoadStateEntry;
1388 int qemu_loadvm_state(QEMUFile *f)
1390 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1391 QLIST_HEAD_INITIALIZER(loadvm_handlers);
1392 LoadStateEntry *le, *new_le;
1393 uint8_t section_type;
1394 unsigned int v;
1395 int ret;
1397 v = qemu_get_be32(f);
1398 if (v != QEMU_VM_FILE_MAGIC)
1399 return -EINVAL;
1401 v = qemu_get_be32(f);
1402 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1403 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1404 return -ENOTSUP;
1406 if (v != QEMU_VM_FILE_VERSION)
1407 return -ENOTSUP;
1409 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1410 uint32_t instance_id, version_id, section_id;
1411 SaveStateEntry *se;
1412 char idstr[257];
1413 int len;
1415 switch (section_type) {
1416 case QEMU_VM_SECTION_START:
1417 case QEMU_VM_SECTION_FULL:
1418 /* Read section start */
1419 section_id = qemu_get_be32(f);
1420 len = qemu_get_byte(f);
1421 qemu_get_buffer(f, (uint8_t *)idstr, len);
1422 idstr[len] = 0;
1423 instance_id = qemu_get_be32(f);
1424 version_id = qemu_get_be32(f);
1426 /* Find savevm section */
1427 se = find_se(idstr, instance_id);
1428 if (se == NULL) {
1429 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1430 ret = -EINVAL;
1431 goto out;
1434 /* Validate version */
1435 if (version_id > se->version_id) {
1436 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1437 version_id, idstr, se->version_id);
1438 ret = -EINVAL;
1439 goto out;
1442 /* Add entry */
1443 le = qemu_mallocz(sizeof(*le));
1445 le->se = se;
1446 le->section_id = section_id;
1447 le->version_id = version_id;
1448 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
1450 ret = vmstate_load(f, le->se, le->version_id);
1451 if (ret < 0) {
1452 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1453 instance_id, idstr);
1454 goto out;
1456 break;
1457 case QEMU_VM_SECTION_PART:
1458 case QEMU_VM_SECTION_END:
1459 section_id = qemu_get_be32(f);
1461 QLIST_FOREACH(le, &loadvm_handlers, entry) {
1462 if (le->section_id == section_id) {
1463 break;
1466 if (le == NULL) {
1467 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1468 ret = -EINVAL;
1469 goto out;
1472 ret = vmstate_load(f, le->se, le->version_id);
1473 if (ret < 0) {
1474 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1475 section_id);
1476 goto out;
1478 break;
1479 default:
1480 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1481 ret = -EINVAL;
1482 goto out;
1486 ret = 0;
1488 out:
1489 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1490 QLIST_REMOVE(le, entry);
1491 qemu_free(le);
1494 if (qemu_file_has_error(f))
1495 ret = -EIO;
1497 return ret;
1500 /* device can contain snapshots */
1501 static int bdrv_can_snapshot(BlockDriverState *bs)
1503 return (bs &&
1504 !bdrv_is_removable(bs) &&
1505 !bdrv_is_read_only(bs));
1508 /* device must be snapshots in order to have a reliable snapshot */
1509 static int bdrv_has_snapshot(BlockDriverState *bs)
1511 return (bs &&
1512 !bdrv_is_removable(bs) &&
1513 !bdrv_is_read_only(bs));
1516 static BlockDriverState *get_bs_snapshots(void)
1518 BlockDriverState *bs;
1519 DriveInfo *dinfo;
1521 if (bs_snapshots)
1522 return bs_snapshots;
1523 QTAILQ_FOREACH(dinfo, &drives, next) {
1524 bs = dinfo->bdrv;
1525 if (bdrv_can_snapshot(bs))
1526 goto ok;
1528 return NULL;
1530 bs_snapshots = bs;
1531 return bs;
1534 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1535 const char *name)
1537 QEMUSnapshotInfo *sn_tab, *sn;
1538 int nb_sns, i, ret;
1540 ret = -ENOENT;
1541 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1542 if (nb_sns < 0)
1543 return ret;
1544 for(i = 0; i < nb_sns; i++) {
1545 sn = &sn_tab[i];
1546 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1547 *sn_info = *sn;
1548 ret = 0;
1549 break;
1552 qemu_free(sn_tab);
1553 return ret;
1556 void do_savevm(Monitor *mon, const QDict *qdict)
1558 DriveInfo *dinfo;
1559 BlockDriverState *bs, *bs1;
1560 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
1561 int must_delete, ret;
1562 QEMUFile *f;
1563 int saved_vm_running;
1564 uint32_t vm_state_size;
1565 #ifdef _WIN32
1566 struct _timeb tb;
1567 #else
1568 struct timeval tv;
1569 #endif
1570 const char *name = qdict_get_try_str(qdict, "name");
1572 bs = get_bs_snapshots();
1573 if (!bs) {
1574 monitor_printf(mon, "No block device can accept snapshots\n");
1575 return;
1578 /* ??? Should this occur after vm_stop? */
1579 qemu_aio_flush();
1581 saved_vm_running = vm_running;
1582 vm_stop(0);
1584 must_delete = 0;
1585 if (name) {
1586 ret = bdrv_snapshot_find(bs, old_sn, name);
1587 if (ret >= 0) {
1588 must_delete = 1;
1591 memset(sn, 0, sizeof(*sn));
1592 if (must_delete) {
1593 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1594 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1595 } else {
1596 if (name)
1597 pstrcpy(sn->name, sizeof(sn->name), name);
1600 /* fill auxiliary fields */
1601 #ifdef _WIN32
1602 _ftime(&tb);
1603 sn->date_sec = tb.time;
1604 sn->date_nsec = tb.millitm * 1000000;
1605 #else
1606 gettimeofday(&tv, NULL);
1607 sn->date_sec = tv.tv_sec;
1608 sn->date_nsec = tv.tv_usec * 1000;
1609 #endif
1610 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1612 /* save the VM state */
1613 f = qemu_fopen_bdrv(bs, 1);
1614 if (!f) {
1615 monitor_printf(mon, "Could not open VM state file\n");
1616 goto the_end;
1618 ret = qemu_savevm_state(f);
1619 vm_state_size = qemu_ftell(f);
1620 qemu_fclose(f);
1621 if (ret < 0) {
1622 monitor_printf(mon, "Error %d while writing VM\n", ret);
1623 goto the_end;
1626 /* create the snapshots */
1628 QTAILQ_FOREACH(dinfo, &drives, next) {
1629 bs1 = dinfo->bdrv;
1630 if (bdrv_has_snapshot(bs1)) {
1631 if (must_delete) {
1632 ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
1633 if (ret < 0) {
1634 monitor_printf(mon,
1635 "Error while deleting snapshot on '%s'\n",
1636 bdrv_get_device_name(bs1));
1639 /* Write VM state size only to the image that contains the state */
1640 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
1641 ret = bdrv_snapshot_create(bs1, sn);
1642 if (ret < 0) {
1643 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
1644 bdrv_get_device_name(bs1));
1649 the_end:
1650 if (saved_vm_running)
1651 vm_start();
1654 int load_vmstate(Monitor *mon, const char *name)
1656 DriveInfo *dinfo;
1657 BlockDriverState *bs, *bs1;
1658 QEMUSnapshotInfo sn;
1659 QEMUFile *f;
1660 int ret;
1662 bs = get_bs_snapshots();
1663 if (!bs) {
1664 monitor_printf(mon, "No block device supports snapshots\n");
1665 return -EINVAL;
1668 /* Flush all IO requests so they don't interfere with the new state. */
1669 qemu_aio_flush();
1671 QTAILQ_FOREACH(dinfo, &drives, next) {
1672 bs1 = dinfo->bdrv;
1673 if (bdrv_has_snapshot(bs1)) {
1674 ret = bdrv_snapshot_goto(bs1, name);
1675 if (ret < 0) {
1676 if (bs != bs1)
1677 monitor_printf(mon, "Warning: ");
1678 switch(ret) {
1679 case -ENOTSUP:
1680 monitor_printf(mon,
1681 "Snapshots not supported on device '%s'\n",
1682 bdrv_get_device_name(bs1));
1683 break;
1684 case -ENOENT:
1685 monitor_printf(mon, "Could not find snapshot '%s' on "
1686 "device '%s'\n",
1687 name, bdrv_get_device_name(bs1));
1688 break;
1689 default:
1690 monitor_printf(mon, "Error %d while activating snapshot on"
1691 " '%s'\n", ret, bdrv_get_device_name(bs1));
1692 break;
1694 /* fatal on snapshot block device */
1695 if (bs == bs1)
1696 return 0;
1701 /* Don't even try to load empty VM states */
1702 ret = bdrv_snapshot_find(bs, &sn, name);
1703 if ((ret >= 0) && (sn.vm_state_size == 0))
1704 return -EINVAL;
1706 /* restore the VM state */
1707 f = qemu_fopen_bdrv(bs, 0);
1708 if (!f) {
1709 monitor_printf(mon, "Could not open VM state file\n");
1710 return -EINVAL;
1712 ret = qemu_loadvm_state(f);
1713 qemu_fclose(f);
1714 if (ret < 0) {
1715 monitor_printf(mon, "Error %d while loading VM state\n", ret);
1716 return ret;
1718 return 0;
1721 void do_delvm(Monitor *mon, const QDict *qdict)
1723 DriveInfo *dinfo;
1724 BlockDriverState *bs, *bs1;
1725 int ret;
1726 const char *name = qdict_get_str(qdict, "name");
1728 bs = get_bs_snapshots();
1729 if (!bs) {
1730 monitor_printf(mon, "No block device supports snapshots\n");
1731 return;
1734 QTAILQ_FOREACH(dinfo, &drives, next) {
1735 bs1 = dinfo->bdrv;
1736 if (bdrv_has_snapshot(bs1)) {
1737 ret = bdrv_snapshot_delete(bs1, name);
1738 if (ret < 0) {
1739 if (ret == -ENOTSUP)
1740 monitor_printf(mon,
1741 "Snapshots not supported on device '%s'\n",
1742 bdrv_get_device_name(bs1));
1743 else
1744 monitor_printf(mon, "Error %d while deleting snapshot on "
1745 "'%s'\n", ret, bdrv_get_device_name(bs1));
1751 void do_info_snapshots(Monitor *mon)
1753 DriveInfo *dinfo;
1754 BlockDriverState *bs, *bs1;
1755 QEMUSnapshotInfo *sn_tab, *sn;
1756 int nb_sns, i;
1757 char buf[256];
1759 bs = get_bs_snapshots();
1760 if (!bs) {
1761 monitor_printf(mon, "No available block device supports snapshots\n");
1762 return;
1764 monitor_printf(mon, "Snapshot devices:");
1765 QTAILQ_FOREACH(dinfo, &drives, next) {
1766 bs1 = dinfo->bdrv;
1767 if (bdrv_has_snapshot(bs1)) {
1768 if (bs == bs1)
1769 monitor_printf(mon, " %s", bdrv_get_device_name(bs1));
1772 monitor_printf(mon, "\n");
1774 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1775 if (nb_sns < 0) {
1776 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1777 return;
1779 monitor_printf(mon, "Snapshot list (from %s):\n",
1780 bdrv_get_device_name(bs));
1781 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1782 for(i = 0; i < nb_sns; i++) {
1783 sn = &sn_tab[i];
1784 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1786 qemu_free(sn_tab);