Revert "char: emit the OPENED event only when a new char connection is opened"
[qemu.git] / savevm.c
blobb7abf43cee77f33c4f7812878efad2afafe74243
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 /* 8 bit int. See that the received value is the same than the one
855 in the field */
857 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
859 uint8_t *v = pv;
860 uint8_t v2;
861 qemu_get_8s(f, &v2);
863 if (*v == v2)
864 return 0;
865 return -EINVAL;
868 const VMStateInfo vmstate_info_uint8_equal = {
869 .name = "uint8 equal",
870 .get = get_uint8_equal,
871 .put = put_uint8,
874 /* 16 bit unsigned int int. See that the received value is the same than the one
875 in the field */
877 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
879 uint16_t *v = pv;
880 uint16_t v2;
881 qemu_get_be16s(f, &v2);
883 if (*v == v2)
884 return 0;
885 return -EINVAL;
888 const VMStateInfo vmstate_info_uint16_equal = {
889 .name = "uint16 equal",
890 .get = get_uint16_equal,
891 .put = put_uint16,
894 /* timers */
896 static int get_timer(QEMUFile *f, void *pv, size_t size)
898 QEMUTimer *v = pv;
899 qemu_get_timer(f, v);
900 return 0;
903 static void put_timer(QEMUFile *f, void *pv, size_t size)
905 QEMUTimer *v = pv;
906 qemu_put_timer(f, v);
909 const VMStateInfo vmstate_info_timer = {
910 .name = "timer",
911 .get = get_timer,
912 .put = put_timer,
915 /* uint8_t buffers */
917 static int get_buffer(QEMUFile *f, void *pv, size_t size)
919 uint8_t *v = pv;
920 qemu_get_buffer(f, v, size);
921 return 0;
924 static void put_buffer(QEMUFile *f, void *pv, size_t size)
926 uint8_t *v = pv;
927 qemu_put_buffer(f, v, size);
930 const VMStateInfo vmstate_info_buffer = {
931 .name = "buffer",
932 .get = get_buffer,
933 .put = put_buffer,
936 /* unused buffers: space that was used for some fields that are
937 not usefull anymore */
939 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
941 qemu_fseek(f, size, SEEK_CUR);
942 return 0;
945 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
947 qemu_fseek(f, size, SEEK_CUR);
950 const VMStateInfo vmstate_info_unused_buffer = {
951 .name = "unused_buffer",
952 .get = get_unused_buffer,
953 .put = put_unused_buffer,
956 typedef struct SaveStateEntry {
957 QTAILQ_ENTRY(SaveStateEntry) entry;
958 char idstr[256];
959 int instance_id;
960 int version_id;
961 int section_id;
962 SaveLiveStateHandler *save_live_state;
963 SaveStateHandler *save_state;
964 LoadStateHandler *load_state;
965 const VMStateDescription *vmsd;
966 void *opaque;
967 } SaveStateEntry;
969 static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
970 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
971 static int global_section_id;
973 static int calculate_new_instance_id(const char *idstr)
975 SaveStateEntry *se;
976 int instance_id = 0;
978 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
979 if (strcmp(idstr, se->idstr) == 0
980 && instance_id <= se->instance_id) {
981 instance_id = se->instance_id + 1;
984 return instance_id;
987 /* TODO: Individual devices generally have very little idea about the rest
988 of the system, so instance_id should be removed/replaced.
989 Meanwhile pass -1 as instance_id if you do not already have a clearly
990 distinguishing id for all instances of your device class. */
991 int register_savevm_live(const char *idstr,
992 int instance_id,
993 int version_id,
994 SaveLiveStateHandler *save_live_state,
995 SaveStateHandler *save_state,
996 LoadStateHandler *load_state,
997 void *opaque)
999 SaveStateEntry *se;
1001 se = qemu_malloc(sizeof(SaveStateEntry));
1002 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1003 se->version_id = version_id;
1004 se->section_id = global_section_id++;
1005 se->save_live_state = save_live_state;
1006 se->save_state = save_state;
1007 se->load_state = load_state;
1008 se->opaque = opaque;
1009 se->vmsd = NULL;
1011 if (instance_id == -1) {
1012 se->instance_id = calculate_new_instance_id(idstr);
1013 } else {
1014 se->instance_id = instance_id;
1016 /* add at the end of list */
1017 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1018 return 0;
1021 int register_savevm(const char *idstr,
1022 int instance_id,
1023 int version_id,
1024 SaveStateHandler *save_state,
1025 LoadStateHandler *load_state,
1026 void *opaque)
1028 return register_savevm_live(idstr, instance_id, version_id,
1029 NULL, save_state, load_state, opaque);
1032 void unregister_savevm(const char *idstr, void *opaque)
1034 SaveStateEntry *se, *new_se;
1036 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1037 if (strcmp(se->idstr, idstr) == 0 && se->opaque == opaque) {
1038 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1039 qemu_free(se);
1044 int vmstate_register(int instance_id, const VMStateDescription *vmsd,
1045 void *opaque)
1047 SaveStateEntry *se;
1049 se = qemu_malloc(sizeof(SaveStateEntry));
1050 pstrcpy(se->idstr, sizeof(se->idstr), vmsd->name);
1051 se->version_id = vmsd->version_id;
1052 se->section_id = global_section_id++;
1053 se->save_live_state = NULL;
1054 se->save_state = NULL;
1055 se->load_state = NULL;
1056 se->opaque = opaque;
1057 se->vmsd = vmsd;
1059 if (instance_id == -1) {
1060 se->instance_id = calculate_new_instance_id(vmsd->name);
1061 } else {
1062 se->instance_id = instance_id;
1064 /* add at the end of list */
1065 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1066 return 0;
1069 void vmstate_unregister(const VMStateDescription *vmsd, void *opaque)
1071 SaveStateEntry *se, *new_se;
1073 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1074 if (se->vmsd == vmsd && se->opaque == opaque) {
1075 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1076 qemu_free(se);
1081 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1082 void *opaque, int version_id)
1084 VMStateField *field = vmsd->fields;
1086 if (version_id > vmsd->version_id) {
1087 return -EINVAL;
1089 if (version_id < vmsd->minimum_version_id_old) {
1090 return -EINVAL;
1092 if (version_id < vmsd->minimum_version_id) {
1093 return vmsd->load_state_old(f, opaque, version_id);
1095 if (vmsd->pre_load) {
1096 int ret = vmsd->pre_load(opaque);
1097 if (ret)
1098 return ret;
1100 while(field->name) {
1101 if ((field->field_exists &&
1102 field->field_exists(opaque, version_id)) ||
1103 (!field->field_exists &&
1104 field->version_id <= version_id)) {
1105 void *base_addr = opaque + field->offset;
1106 int ret, i, n_elems = 1;
1108 if (field->flags & VMS_ARRAY) {
1109 n_elems = field->num;
1110 } else if (field->flags & VMS_VARRAY_INT32) {
1111 n_elems = *(int32_t *)(opaque+field->num_offset);
1112 } else if (field->flags & VMS_VARRAY_UINT16) {
1113 n_elems = *(uint16_t *)(opaque+field->num_offset);
1115 if (field->flags & VMS_POINTER) {
1116 base_addr = *(void **)base_addr;
1118 for (i = 0; i < n_elems; i++) {
1119 void *addr = base_addr + field->size * i;
1121 if (field->flags & VMS_ARRAY_OF_POINTER) {
1122 addr = *(void **)addr;
1124 if (field->flags & VMS_STRUCT) {
1125 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
1126 } else {
1127 ret = field->info->get(f, addr, field->size);
1130 if (ret < 0) {
1131 return ret;
1135 field++;
1137 if (vmsd->post_load) {
1138 return vmsd->post_load(opaque, version_id);
1140 return 0;
1143 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1144 void *opaque)
1146 VMStateField *field = vmsd->fields;
1148 if (vmsd->pre_save) {
1149 vmsd->pre_save(opaque);
1151 while(field->name) {
1152 if (!field->field_exists ||
1153 field->field_exists(opaque, vmsd->version_id)) {
1154 void *base_addr = opaque + field->offset;
1155 int i, n_elems = 1;
1157 if (field->flags & VMS_ARRAY) {
1158 n_elems = field->num;
1159 } else if (field->flags & VMS_VARRAY_INT32) {
1160 n_elems = *(int32_t *)(opaque+field->num_offset);
1161 } else if (field->flags & VMS_VARRAY_UINT16) {
1162 n_elems = *(uint16_t *)(opaque+field->num_offset);
1164 if (field->flags & VMS_POINTER) {
1165 base_addr = *(void **)base_addr;
1167 for (i = 0; i < n_elems; i++) {
1168 void *addr = base_addr + field->size * i;
1170 if (field->flags & VMS_STRUCT) {
1171 vmstate_save_state(f, field->vmsd, addr);
1172 } else {
1173 field->info->put(f, addr, field->size);
1177 field++;
1179 if (vmsd->post_save) {
1180 vmsd->post_save(opaque);
1184 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1186 if (!se->vmsd) { /* Old style */
1187 return se->load_state(f, se->opaque, version_id);
1189 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
1192 static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1194 if (!se->vmsd) { /* Old style */
1195 se->save_state(f, se->opaque);
1196 return;
1198 vmstate_save_state(f,se->vmsd, se->opaque);
1201 #define QEMU_VM_FILE_MAGIC 0x5145564d
1202 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1203 #define QEMU_VM_FILE_VERSION 0x00000003
1205 #define QEMU_VM_EOF 0x00
1206 #define QEMU_VM_SECTION_START 0x01
1207 #define QEMU_VM_SECTION_PART 0x02
1208 #define QEMU_VM_SECTION_END 0x03
1209 #define QEMU_VM_SECTION_FULL 0x04
1211 int qemu_savevm_state_begin(QEMUFile *f)
1213 SaveStateEntry *se;
1215 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1216 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1218 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1219 int len;
1221 if (se->save_live_state == NULL)
1222 continue;
1224 /* Section type */
1225 qemu_put_byte(f, QEMU_VM_SECTION_START);
1226 qemu_put_be32(f, se->section_id);
1228 /* ID string */
1229 len = strlen(se->idstr);
1230 qemu_put_byte(f, len);
1231 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1233 qemu_put_be32(f, se->instance_id);
1234 qemu_put_be32(f, se->version_id);
1236 se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
1239 if (qemu_file_has_error(f))
1240 return -EIO;
1242 return 0;
1245 int qemu_savevm_state_iterate(QEMUFile *f)
1247 SaveStateEntry *se;
1248 int ret = 1;
1250 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1251 if (se->save_live_state == NULL)
1252 continue;
1254 /* Section type */
1255 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1256 qemu_put_be32(f, se->section_id);
1258 ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
1261 if (ret)
1262 return 1;
1264 if (qemu_file_has_error(f))
1265 return -EIO;
1267 return 0;
1270 int qemu_savevm_state_complete(QEMUFile *f)
1272 SaveStateEntry *se;
1274 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1275 if (se->save_live_state == NULL)
1276 continue;
1278 /* Section type */
1279 qemu_put_byte(f, QEMU_VM_SECTION_END);
1280 qemu_put_be32(f, se->section_id);
1282 se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
1285 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1286 int len;
1288 if (se->save_state == NULL && se->vmsd == NULL)
1289 continue;
1291 /* Section type */
1292 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1293 qemu_put_be32(f, se->section_id);
1295 /* ID string */
1296 len = strlen(se->idstr);
1297 qemu_put_byte(f, len);
1298 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1300 qemu_put_be32(f, se->instance_id);
1301 qemu_put_be32(f, se->version_id);
1303 vmstate_save(f, se);
1306 qemu_put_byte(f, QEMU_VM_EOF);
1308 if (qemu_file_has_error(f))
1309 return -EIO;
1311 return 0;
1314 int qemu_savevm_state(QEMUFile *f)
1316 int saved_vm_running;
1317 int ret;
1319 saved_vm_running = vm_running;
1320 vm_stop(0);
1322 bdrv_flush_all();
1324 ret = qemu_savevm_state_begin(f);
1325 if (ret < 0)
1326 goto out;
1328 do {
1329 ret = qemu_savevm_state_iterate(f);
1330 if (ret < 0)
1331 goto out;
1332 } while (ret == 0);
1334 ret = qemu_savevm_state_complete(f);
1336 out:
1337 if (qemu_file_has_error(f))
1338 ret = -EIO;
1340 if (!ret && saved_vm_running)
1341 vm_start();
1343 return ret;
1346 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1348 SaveStateEntry *se;
1350 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1351 if (!strcmp(se->idstr, idstr) &&
1352 instance_id == se->instance_id)
1353 return se;
1355 return NULL;
1358 typedef struct LoadStateEntry {
1359 QLIST_ENTRY(LoadStateEntry) entry;
1360 SaveStateEntry *se;
1361 int section_id;
1362 int version_id;
1363 } LoadStateEntry;
1365 int qemu_loadvm_state(QEMUFile *f)
1367 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1368 QLIST_HEAD_INITIALIZER(loadvm_handlers);
1369 LoadStateEntry *le, *new_le;
1370 uint8_t section_type;
1371 unsigned int v;
1372 int ret;
1374 v = qemu_get_be32(f);
1375 if (v != QEMU_VM_FILE_MAGIC)
1376 return -EINVAL;
1378 v = qemu_get_be32(f);
1379 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1380 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1381 return -ENOTSUP;
1383 if (v != QEMU_VM_FILE_VERSION)
1384 return -ENOTSUP;
1386 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1387 uint32_t instance_id, version_id, section_id;
1388 SaveStateEntry *se;
1389 char idstr[257];
1390 int len;
1392 switch (section_type) {
1393 case QEMU_VM_SECTION_START:
1394 case QEMU_VM_SECTION_FULL:
1395 /* Read section start */
1396 section_id = qemu_get_be32(f);
1397 len = qemu_get_byte(f);
1398 qemu_get_buffer(f, (uint8_t *)idstr, len);
1399 idstr[len] = 0;
1400 instance_id = qemu_get_be32(f);
1401 version_id = qemu_get_be32(f);
1403 /* Find savevm section */
1404 se = find_se(idstr, instance_id);
1405 if (se == NULL) {
1406 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1407 ret = -EINVAL;
1408 goto out;
1411 /* Validate version */
1412 if (version_id > se->version_id) {
1413 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1414 version_id, idstr, se->version_id);
1415 ret = -EINVAL;
1416 goto out;
1419 /* Add entry */
1420 le = qemu_mallocz(sizeof(*le));
1422 le->se = se;
1423 le->section_id = section_id;
1424 le->version_id = version_id;
1425 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
1427 ret = vmstate_load(f, le->se, le->version_id);
1428 if (ret < 0) {
1429 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1430 instance_id, idstr);
1431 goto out;
1433 break;
1434 case QEMU_VM_SECTION_PART:
1435 case QEMU_VM_SECTION_END:
1436 section_id = qemu_get_be32(f);
1438 QLIST_FOREACH(le, &loadvm_handlers, entry) {
1439 if (le->section_id == section_id) {
1440 break;
1443 if (le == NULL) {
1444 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1445 ret = -EINVAL;
1446 goto out;
1449 ret = vmstate_load(f, le->se, le->version_id);
1450 if (ret < 0) {
1451 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1452 section_id);
1453 goto out;
1455 break;
1456 default:
1457 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1458 ret = -EINVAL;
1459 goto out;
1463 ret = 0;
1465 out:
1466 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1467 QLIST_REMOVE(le, entry);
1468 qemu_free(le);
1471 if (qemu_file_has_error(f))
1472 ret = -EIO;
1474 return ret;
1477 /* device can contain snapshots */
1478 static int bdrv_can_snapshot(BlockDriverState *bs)
1480 return (bs &&
1481 !bdrv_is_removable(bs) &&
1482 !bdrv_is_read_only(bs));
1485 /* device must be snapshots in order to have a reliable snapshot */
1486 static int bdrv_has_snapshot(BlockDriverState *bs)
1488 return (bs &&
1489 !bdrv_is_removable(bs) &&
1490 !bdrv_is_read_only(bs));
1493 static BlockDriverState *get_bs_snapshots(void)
1495 BlockDriverState *bs;
1496 DriveInfo *dinfo;
1498 if (bs_snapshots)
1499 return bs_snapshots;
1500 QTAILQ_FOREACH(dinfo, &drives, next) {
1501 bs = dinfo->bdrv;
1502 if (bdrv_can_snapshot(bs))
1503 goto ok;
1505 return NULL;
1507 bs_snapshots = bs;
1508 return bs;
1511 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1512 const char *name)
1514 QEMUSnapshotInfo *sn_tab, *sn;
1515 int nb_sns, i, ret;
1517 ret = -ENOENT;
1518 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1519 if (nb_sns < 0)
1520 return ret;
1521 for(i = 0; i < nb_sns; i++) {
1522 sn = &sn_tab[i];
1523 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1524 *sn_info = *sn;
1525 ret = 0;
1526 break;
1529 qemu_free(sn_tab);
1530 return ret;
1533 void do_savevm(Monitor *mon, const QDict *qdict)
1535 DriveInfo *dinfo;
1536 BlockDriverState *bs, *bs1;
1537 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
1538 int must_delete, ret;
1539 QEMUFile *f;
1540 int saved_vm_running;
1541 uint32_t vm_state_size;
1542 #ifdef _WIN32
1543 struct _timeb tb;
1544 #else
1545 struct timeval tv;
1546 #endif
1547 const char *name = qdict_get_try_str(qdict, "name");
1549 bs = get_bs_snapshots();
1550 if (!bs) {
1551 monitor_printf(mon, "No block device can accept snapshots\n");
1552 return;
1555 /* ??? Should this occur after vm_stop? */
1556 qemu_aio_flush();
1558 saved_vm_running = vm_running;
1559 vm_stop(0);
1561 must_delete = 0;
1562 if (name) {
1563 ret = bdrv_snapshot_find(bs, old_sn, name);
1564 if (ret >= 0) {
1565 must_delete = 1;
1568 memset(sn, 0, sizeof(*sn));
1569 if (must_delete) {
1570 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1571 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1572 } else {
1573 if (name)
1574 pstrcpy(sn->name, sizeof(sn->name), name);
1577 /* fill auxiliary fields */
1578 #ifdef _WIN32
1579 _ftime(&tb);
1580 sn->date_sec = tb.time;
1581 sn->date_nsec = tb.millitm * 1000000;
1582 #else
1583 gettimeofday(&tv, NULL);
1584 sn->date_sec = tv.tv_sec;
1585 sn->date_nsec = tv.tv_usec * 1000;
1586 #endif
1587 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1589 /* save the VM state */
1590 f = qemu_fopen_bdrv(bs, 1);
1591 if (!f) {
1592 monitor_printf(mon, "Could not open VM state file\n");
1593 goto the_end;
1595 ret = qemu_savevm_state(f);
1596 vm_state_size = qemu_ftell(f);
1597 qemu_fclose(f);
1598 if (ret < 0) {
1599 monitor_printf(mon, "Error %d while writing VM\n", ret);
1600 goto the_end;
1603 /* create the snapshots */
1605 QTAILQ_FOREACH(dinfo, &drives, next) {
1606 bs1 = dinfo->bdrv;
1607 if (bdrv_has_snapshot(bs1)) {
1608 if (must_delete) {
1609 ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
1610 if (ret < 0) {
1611 monitor_printf(mon,
1612 "Error while deleting snapshot on '%s'\n",
1613 bdrv_get_device_name(bs1));
1616 /* Write VM state size only to the image that contains the state */
1617 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
1618 ret = bdrv_snapshot_create(bs1, sn);
1619 if (ret < 0) {
1620 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
1621 bdrv_get_device_name(bs1));
1626 the_end:
1627 if (saved_vm_running)
1628 vm_start();
1631 int load_vmstate(Monitor *mon, const char *name)
1633 DriveInfo *dinfo;
1634 BlockDriverState *bs, *bs1;
1635 QEMUSnapshotInfo sn;
1636 QEMUFile *f;
1637 int ret;
1639 bs = get_bs_snapshots();
1640 if (!bs) {
1641 monitor_printf(mon, "No block device supports snapshots\n");
1642 return -EINVAL;
1645 /* Flush all IO requests so they don't interfere with the new state. */
1646 qemu_aio_flush();
1648 QTAILQ_FOREACH(dinfo, &drives, next) {
1649 bs1 = dinfo->bdrv;
1650 if (bdrv_has_snapshot(bs1)) {
1651 ret = bdrv_snapshot_goto(bs1, name);
1652 if (ret < 0) {
1653 if (bs != bs1)
1654 monitor_printf(mon, "Warning: ");
1655 switch(ret) {
1656 case -ENOTSUP:
1657 monitor_printf(mon,
1658 "Snapshots not supported on device '%s'\n",
1659 bdrv_get_device_name(bs1));
1660 break;
1661 case -ENOENT:
1662 monitor_printf(mon, "Could not find snapshot '%s' on "
1663 "device '%s'\n",
1664 name, bdrv_get_device_name(bs1));
1665 break;
1666 default:
1667 monitor_printf(mon, "Error %d while activating snapshot on"
1668 " '%s'\n", ret, bdrv_get_device_name(bs1));
1669 break;
1671 /* fatal on snapshot block device */
1672 if (bs == bs1)
1673 return 0;
1678 /* Don't even try to load empty VM states */
1679 ret = bdrv_snapshot_find(bs, &sn, name);
1680 if ((ret >= 0) && (sn.vm_state_size == 0))
1681 return -EINVAL;
1683 /* restore the VM state */
1684 f = qemu_fopen_bdrv(bs, 0);
1685 if (!f) {
1686 monitor_printf(mon, "Could not open VM state file\n");
1687 return -EINVAL;
1689 ret = qemu_loadvm_state(f);
1690 qemu_fclose(f);
1691 if (ret < 0) {
1692 monitor_printf(mon, "Error %d while loading VM state\n", ret);
1693 return ret;
1695 return 0;
1698 void do_delvm(Monitor *mon, const QDict *qdict)
1700 DriveInfo *dinfo;
1701 BlockDriverState *bs, *bs1;
1702 int ret;
1703 const char *name = qdict_get_str(qdict, "name");
1705 bs = get_bs_snapshots();
1706 if (!bs) {
1707 monitor_printf(mon, "No block device supports snapshots\n");
1708 return;
1711 QTAILQ_FOREACH(dinfo, &drives, next) {
1712 bs1 = dinfo->bdrv;
1713 if (bdrv_has_snapshot(bs1)) {
1714 ret = bdrv_snapshot_delete(bs1, name);
1715 if (ret < 0) {
1716 if (ret == -ENOTSUP)
1717 monitor_printf(mon,
1718 "Snapshots not supported on device '%s'\n",
1719 bdrv_get_device_name(bs1));
1720 else
1721 monitor_printf(mon, "Error %d while deleting snapshot on "
1722 "'%s'\n", ret, bdrv_get_device_name(bs1));
1728 void do_info_snapshots(Monitor *mon)
1730 DriveInfo *dinfo;
1731 BlockDriverState *bs, *bs1;
1732 QEMUSnapshotInfo *sn_tab, *sn;
1733 int nb_sns, i;
1734 char buf[256];
1736 bs = get_bs_snapshots();
1737 if (!bs) {
1738 monitor_printf(mon, "No available block device supports snapshots\n");
1739 return;
1741 monitor_printf(mon, "Snapshot devices:");
1742 QTAILQ_FOREACH(dinfo, &drives, next) {
1743 bs1 = dinfo->bdrv;
1744 if (bdrv_has_snapshot(bs1)) {
1745 if (bs == bs1)
1746 monitor_printf(mon, " %s", bdrv_get_device_name(bs1));
1749 monitor_printf(mon, "\n");
1751 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1752 if (nb_sns < 0) {
1753 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1754 return;
1756 monitor_printf(mon, "Snapshot list (from %s):\n",
1757 bdrv_get_device_name(bs));
1758 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1759 for(i = 0; i < nb_sns; i++) {
1760 sn = &sn_tab[i];
1761 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1763 qemu_free(sn_tab);