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
32 /* Needed early for _BSD etc. */
33 #include "config-host.h"
36 #include <sys/times.h>
40 #include <sys/ioctl.h>
41 #include <sys/resource.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
45 #if defined(__NetBSD__)
46 #include <net/if_tap.h>
49 #include <linux/if_tun.h>
51 #include <arpa/inet.h>
54 #include <sys/select.h>
62 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
63 #include <freebsd/stdlib.h>
68 #include <linux/rtc.h>
75 #include <sys/timeb.h>
77 #define getopt_long_only getopt_long
78 #define memalign(align, size) malloc(size)
81 #include "qemu-common.h"
86 #include "qemu-timer.h"
87 #include "qemu-char.h"
89 #include "audio/audio.h"
90 #include "migration.h"
91 #include "qemu_socket.h"
93 /* point to the block driver where the snapshots are managed */
94 static BlockDriverState *bs_snapshots;
96 #define SELF_ANNOUNCE_ROUNDS 5
97 #define ETH_P_EXPERIMENTAL 0x01F1 /* just a number */
98 //#define ETH_P_EXPERIMENTAL 0x0012 /* make it the size of the packet */
99 #define EXPERIMENTAL_MAGIC 0xf1f23f4f
101 static int announce_self_create(uint8_t *buf,
104 uint32_t magic = EXPERIMENTAL_MAGIC;
105 uint16_t proto = htons(ETH_P_EXPERIMENTAL);
107 /* FIXME: should we send a different packet (arp/rarp/ping)? */
109 memset(buf, 0xff, 6); /* h_dst */
110 memcpy(buf + 6, mac_addr, 6); /* h_src */
111 memcpy(buf + 12, &proto, 2); /* h_proto */
112 memcpy(buf + 14, &magic, 4); /* magic */
117 void qemu_announce_self(void)
124 for (i = 0; i < nb_nics; i++) {
125 len = announce_self_create(buf, nd_table[i].macaddr);
126 vlan = nd_table[i].vlan;
127 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
128 for (j=0; j < SELF_ANNOUNCE_ROUNDS; j++)
129 vc->fd_read(vc->opaque, buf, len);
134 /***********************************************************/
135 /* savevm/loadvm support */
137 #define IO_BUF_SIZE 32768
140 QEMUFilePutBufferFunc *put_buffer;
141 QEMUFileGetBufferFunc *get_buffer;
142 QEMUFileCloseFunc *close;
143 QEMUFileRateLimit *rate_limit;
147 int64_t buf_offset; /* start of buffer when writing, end of buffer
150 int buf_size; /* 0 when writing */
151 uint8_t buf[IO_BUF_SIZE];
156 typedef struct QEMUFilePopen
162 typedef struct QEMUFileSocket
168 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
170 QEMUFileSocket *s = opaque;
174 len = recv(s->fd, buf, size, 0);
175 } while (len == -1 && socket_error() == EINTR);
178 len = -socket_error();
183 static int socket_close(void *opaque)
185 QEMUFileSocket *s = opaque;
190 static int popen_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
192 QEMUFilePopen *s = opaque;
193 return fwrite(buf, 1, size, s->popen_file);
196 static int popen_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
198 QEMUFilePopen *s = opaque;
199 return fread(buf, 1, size, s->popen_file);
202 static int popen_close(void *opaque)
204 QEMUFilePopen *s = opaque;
205 pclose(s->popen_file);
210 QEMUFile *qemu_popen(FILE *popen_file, const char *mode)
214 if (popen_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
215 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
219 s = qemu_mallocz(sizeof(QEMUFilePopen));
221 s->popen_file = popen_file;
224 s->file = qemu_fopen_ops(s, NULL, popen_get_buffer, popen_close, NULL);
226 s->file = qemu_fopen_ops(s, popen_put_buffer, NULL, popen_close, NULL);
228 fprintf(stderr, "qemu_popen: returning result of qemu_fopen_ops\n");
232 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
236 popen_file = popen(command, mode);
237 if(popen_file == NULL) {
241 return qemu_popen(popen_file, mode);
244 QEMUFile *qemu_fopen_socket(int fd)
246 QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
249 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, NULL);
253 typedef struct QEMUFileStdio
258 static int file_put_buffer(void *opaque, const uint8_t *buf,
259 int64_t pos, int size)
261 QEMUFileStdio *s = opaque;
262 fseek(s->outfile, pos, SEEK_SET);
263 fwrite(buf, 1, size, s->outfile);
267 static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
269 QEMUFileStdio *s = opaque;
270 fseek(s->outfile, pos, SEEK_SET);
271 return fread(buf, 1, size, s->outfile);
274 static int file_close(void *opaque)
276 QEMUFileStdio *s = opaque;
282 QEMUFile *qemu_fopen(const char *filename, const char *mode)
286 s = qemu_mallocz(sizeof(QEMUFileStdio));
288 s->outfile = fopen(filename, mode);
292 if (!strcmp(mode, "wb"))
293 return qemu_fopen_ops(s, file_put_buffer, NULL, file_close, NULL);
294 else if (!strcmp(mode, "rb"))
295 return qemu_fopen_ops(s, NULL, file_get_buffer, file_close, NULL);
304 typedef struct QEMUFileBdrv
306 BlockDriverState *bs;
310 static int bdrv_put_buffer(void *opaque, const uint8_t *buf,
311 int64_t pos, int size)
313 QEMUFileBdrv *s = opaque;
314 bdrv_pwrite(s->bs, s->base_offset + pos, buf, size);
318 static int bdrv_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
320 QEMUFileBdrv *s = opaque;
321 return bdrv_pread(s->bs, s->base_offset + pos, buf, size);
324 static int bdrv_fclose(void *opaque)
326 QEMUFileBdrv *s = opaque;
331 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable)
335 s = qemu_mallocz(sizeof(QEMUFileBdrv));
338 s->base_offset = offset;
341 return qemu_fopen_ops(s, bdrv_put_buffer, NULL, bdrv_fclose, NULL);
343 return qemu_fopen_ops(s, NULL, bdrv_get_buffer, bdrv_fclose, NULL);
346 QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
347 QEMUFileGetBufferFunc *get_buffer,
348 QEMUFileCloseFunc *close,
349 QEMUFileRateLimit *rate_limit)
353 f = qemu_mallocz(sizeof(QEMUFile));
356 f->put_buffer = put_buffer;
357 f->get_buffer = get_buffer;
359 f->rate_limit = rate_limit;
365 int qemu_file_has_error(QEMUFile *f)
370 void qemu_fflush(QEMUFile *f)
375 if (f->is_write && f->buf_index > 0) {
378 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
380 f->buf_offset += f->buf_index;
387 static void qemu_fill_buffer(QEMUFile *f)
397 len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
401 f->buf_offset += len;
402 } else if (len != -EAGAIN)
406 int qemu_fclose(QEMUFile *f)
411 ret = f->close(f->opaque);
416 void qemu_file_put_notify(QEMUFile *f)
418 f->put_buffer(f->opaque, NULL, 0, 0);
421 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
425 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
427 "Attempted to write to buffer while read buffer is not empty\n");
431 while (!f->has_error && size > 0) {
432 l = IO_BUF_SIZE - f->buf_index;
435 memcpy(f->buf + f->buf_index, buf, l);
440 if (f->buf_index >= IO_BUF_SIZE)
445 void qemu_put_byte(QEMUFile *f, int v)
447 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
449 "Attempted to write to buffer while read buffer is not empty\n");
453 f->buf[f->buf_index++] = v;
455 if (f->buf_index >= IO_BUF_SIZE)
459 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
468 l = f->buf_size - f->buf_index;
471 l = f->buf_size - f->buf_index;
477 memcpy(buf, f->buf + f->buf_index, l);
485 int qemu_get_byte(QEMUFile *f)
490 if (f->buf_index >= f->buf_size) {
492 if (f->buf_index >= f->buf_size)
495 return f->buf[f->buf_index++];
498 int64_t qemu_ftell(QEMUFile *f)
500 return f->buf_offset - f->buf_size + f->buf_index;
503 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
505 if (whence == SEEK_SET) {
507 } else if (whence == SEEK_CUR) {
508 pos += qemu_ftell(f);
510 /* SEEK_END not supported */
524 int qemu_file_rate_limit(QEMUFile *f)
527 return f->rate_limit(f->opaque);
532 void qemu_put_be16(QEMUFile *f, unsigned int v)
534 qemu_put_byte(f, v >> 8);
538 void qemu_put_be32(QEMUFile *f, unsigned int v)
540 qemu_put_byte(f, v >> 24);
541 qemu_put_byte(f, v >> 16);
542 qemu_put_byte(f, v >> 8);
546 void qemu_put_be64(QEMUFile *f, uint64_t v)
548 qemu_put_be32(f, v >> 32);
552 unsigned int qemu_get_be16(QEMUFile *f)
555 v = qemu_get_byte(f) << 8;
556 v |= qemu_get_byte(f);
560 unsigned int qemu_get_be32(QEMUFile *f)
563 v = qemu_get_byte(f) << 24;
564 v |= qemu_get_byte(f) << 16;
565 v |= qemu_get_byte(f) << 8;
566 v |= qemu_get_byte(f);
570 uint64_t qemu_get_be64(QEMUFile *f)
573 v = (uint64_t)qemu_get_be32(f) << 32;
574 v |= qemu_get_be32(f);
578 typedef struct SaveStateEntry {
583 SaveLiveStateHandler *save_live_state;
584 SaveStateHandler *save_state;
585 LoadStateHandler *load_state;
587 struct SaveStateEntry *next;
590 static SaveStateEntry *first_se;
592 /* TODO: Individual devices generally have very little idea about the rest
593 of the system, so instance_id should be removed/replaced.
594 Meanwhile pass -1 as instance_id if you do not already have a clearly
595 distinguishing id for all instances of your device class. */
596 int register_savevm_live(const char *idstr,
599 SaveLiveStateHandler *save_live_state,
600 SaveStateHandler *save_state,
601 LoadStateHandler *load_state,
604 SaveStateEntry *se, **pse;
605 static int global_section_id;
607 se = qemu_malloc(sizeof(SaveStateEntry));
608 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
609 se->instance_id = (instance_id == -1) ? 0 : instance_id;
610 se->version_id = version_id;
611 se->section_id = global_section_id++;
612 se->save_live_state = save_live_state;
613 se->save_state = save_state;
614 se->load_state = load_state;
618 /* add at the end of list */
620 while (*pse != NULL) {
621 if (instance_id == -1
622 && strcmp(se->idstr, (*pse)->idstr) == 0
623 && se->instance_id <= (*pse)->instance_id)
624 se->instance_id = (*pse)->instance_id + 1;
631 int register_savevm(const char *idstr,
634 SaveStateHandler *save_state,
635 LoadStateHandler *load_state,
638 return register_savevm_live(idstr, instance_id, version_id,
639 NULL, save_state, load_state, opaque);
642 #define QEMU_VM_FILE_MAGIC 0x5145564d
643 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
644 #define QEMU_VM_FILE_VERSION 0x00000003
646 #define QEMU_VM_EOF 0x00
647 #define QEMU_VM_SECTION_START 0x01
648 #define QEMU_VM_SECTION_PART 0x02
649 #define QEMU_VM_SECTION_END 0x03
650 #define QEMU_VM_SECTION_FULL 0x04
652 int qemu_savevm_state_begin(QEMUFile *f)
656 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
657 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
659 for (se = first_se; se != NULL; se = se->next) {
662 if (se->save_live_state == NULL)
666 qemu_put_byte(f, QEMU_VM_SECTION_START);
667 qemu_put_be32(f, se->section_id);
670 len = strlen(se->idstr);
671 qemu_put_byte(f, len);
672 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
674 qemu_put_be32(f, se->instance_id);
675 qemu_put_be32(f, se->version_id);
677 se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
680 if (qemu_file_has_error(f))
686 int qemu_savevm_state_iterate(QEMUFile *f)
691 for (se = first_se; se != NULL; se = se->next) {
692 if (se->save_live_state == NULL)
696 qemu_put_byte(f, QEMU_VM_SECTION_PART);
697 qemu_put_be32(f, se->section_id);
699 ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
705 if (qemu_file_has_error(f))
711 int qemu_savevm_state_complete(QEMUFile *f)
715 for (se = first_se; se != NULL; se = se->next) {
716 if (se->save_live_state == NULL)
720 qemu_put_byte(f, QEMU_VM_SECTION_END);
721 qemu_put_be32(f, se->section_id);
723 se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
726 for(se = first_se; se != NULL; se = se->next) {
729 if (se->save_state == NULL)
733 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
734 qemu_put_be32(f, se->section_id);
737 len = strlen(se->idstr);
738 qemu_put_byte(f, len);
739 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
741 qemu_put_be32(f, se->instance_id);
742 qemu_put_be32(f, se->version_id);
744 se->save_state(f, se->opaque);
747 qemu_put_byte(f, QEMU_VM_EOF);
749 if (qemu_file_has_error(f))
755 int qemu_savevm_state(QEMUFile *f)
757 int saved_vm_running;
760 saved_vm_running = vm_running;
765 ret = qemu_savevm_state_begin(f);
770 ret = qemu_savevm_state_iterate(f);
775 ret = qemu_savevm_state_complete(f);
778 if (qemu_file_has_error(f))
781 if (!ret && saved_vm_running)
787 static SaveStateEntry *find_se(const char *idstr, int instance_id)
791 for(se = first_se; se != NULL; se = se->next) {
792 if (!strcmp(se->idstr, idstr) &&
793 instance_id == se->instance_id)
799 typedef struct LoadStateEntry {
803 struct LoadStateEntry *next;
806 static int qemu_loadvm_state_v2(QEMUFile *f)
809 int len, ret, instance_id, record_len, version_id;
810 int64_t total_len, end_pos, cur_pos;
813 total_len = qemu_get_be64(f);
814 end_pos = total_len + qemu_ftell(f);
816 if (qemu_ftell(f) >= end_pos)
818 len = qemu_get_byte(f);
819 qemu_get_buffer(f, (uint8_t *)idstr, len);
821 instance_id = qemu_get_be32(f);
822 version_id = qemu_get_be32(f);
823 record_len = qemu_get_be32(f);
824 cur_pos = qemu_ftell(f);
825 se = find_se(idstr, instance_id);
827 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
830 ret = se->load_state(f, se->opaque, version_id);
832 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
836 /* always seek to exact end of record */
837 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
840 if (qemu_file_has_error(f))
846 int qemu_loadvm_state(QEMUFile *f)
848 LoadStateEntry *first_le = NULL;
849 uint8_t section_type;
853 v = qemu_get_be32(f);
854 if (v != QEMU_VM_FILE_MAGIC)
857 v = qemu_get_be32(f);
858 if (v == QEMU_VM_FILE_VERSION_COMPAT)
859 return qemu_loadvm_state_v2(f);
860 if (v != QEMU_VM_FILE_VERSION)
863 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
864 uint32_t instance_id, version_id, section_id;
870 switch (section_type) {
871 case QEMU_VM_SECTION_START:
872 case QEMU_VM_SECTION_FULL:
873 /* Read section start */
874 section_id = qemu_get_be32(f);
875 len = qemu_get_byte(f);
876 qemu_get_buffer(f, (uint8_t *)idstr, len);
878 instance_id = qemu_get_be32(f);
879 version_id = qemu_get_be32(f);
881 /* Find savevm section */
882 se = find_se(idstr, instance_id);
884 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
889 /* Validate version */
890 if (version_id > se->version_id) {
891 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
892 version_id, idstr, se->version_id);
898 le = qemu_mallocz(sizeof(*le));
901 le->section_id = section_id;
902 le->version_id = version_id;
906 le->se->load_state(f, le->se->opaque, le->version_id);
908 case QEMU_VM_SECTION_PART:
909 case QEMU_VM_SECTION_END:
910 section_id = qemu_get_be32(f);
912 for (le = first_le; le && le->section_id != section_id; le = le->next);
914 fprintf(stderr, "Unknown savevm section %d\n", section_id);
919 le->se->load_state(f, le->se->opaque, le->version_id);
922 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
932 LoadStateEntry *le = first_le;
933 first_le = first_le->next;
937 if (qemu_file_has_error(f))
943 /* device can contain snapshots */
944 static int bdrv_can_snapshot(BlockDriverState *bs)
947 !bdrv_is_removable(bs) &&
948 !bdrv_is_read_only(bs));
951 /* device must be snapshots in order to have a reliable snapshot */
952 static int bdrv_has_snapshot(BlockDriverState *bs)
955 !bdrv_is_removable(bs) &&
956 !bdrv_is_read_only(bs));
959 static BlockDriverState *get_bs_snapshots(void)
961 BlockDriverState *bs;
966 for(i = 0; i <= nb_drives; i++) {
967 bs = drives_table[i].bdrv;
968 if (bdrv_can_snapshot(bs))
977 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
980 QEMUSnapshotInfo *sn_tab, *sn;
984 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
987 for(i = 0; i < nb_sns; i++) {
989 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
999 void do_savevm(Monitor *mon, const char *name)
1001 BlockDriverState *bs, *bs1;
1002 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
1003 int must_delete, ret, i;
1004 BlockDriverInfo bdi1, *bdi = &bdi1;
1006 int saved_vm_running;
1007 uint32_t vm_state_size;
1014 bs = get_bs_snapshots();
1016 monitor_printf(mon, "No block device can accept snapshots\n");
1020 /* ??? Should this occur after vm_stop? */
1023 saved_vm_running = vm_running;
1028 ret = bdrv_snapshot_find(bs, old_sn, name);
1033 memset(sn, 0, sizeof(*sn));
1035 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1036 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1039 pstrcpy(sn->name, sizeof(sn->name), name);
1042 /* fill auxiliary fields */
1045 sn->date_sec = tb.time;
1046 sn->date_nsec = tb.millitm * 1000000;
1048 gettimeofday(&tv, NULL);
1049 sn->date_sec = tv.tv_sec;
1050 sn->date_nsec = tv.tv_usec * 1000;
1052 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1054 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
1055 monitor_printf(mon, "Device %s does not support VM state snapshots\n",
1056 bdrv_get_device_name(bs));
1060 /* save the VM state */
1061 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
1063 monitor_printf(mon, "Could not open VM state file\n");
1066 ret = qemu_savevm_state(f);
1067 vm_state_size = qemu_ftell(f);
1070 monitor_printf(mon, "Error %d while writing VM\n", ret);
1074 /* create the snapshots */
1076 for(i = 0; i < nb_drives; i++) {
1077 bs1 = drives_table[i].bdrv;
1078 if (bdrv_has_snapshot(bs1)) {
1080 ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
1083 "Error while deleting snapshot on '%s'\n",
1084 bdrv_get_device_name(bs1));
1087 /* Write VM state size only to the image that contains the state */
1088 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
1089 ret = bdrv_snapshot_create(bs1, sn);
1091 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
1092 bdrv_get_device_name(bs1));
1098 if (saved_vm_running)
1102 void do_loadvm(Monitor *mon, const char *name)
1104 BlockDriverState *bs, *bs1;
1105 BlockDriverInfo bdi1, *bdi = &bdi1;
1106 QEMUSnapshotInfo sn;
1109 int saved_vm_running;
1111 bs = get_bs_snapshots();
1113 monitor_printf(mon, "No block device supports snapshots\n");
1117 /* Flush all IO requests so they don't interfere with the new state. */
1120 saved_vm_running = vm_running;
1123 for(i = 0; i <= nb_drives; i++) {
1124 bs1 = drives_table[i].bdrv;
1125 if (bdrv_has_snapshot(bs1)) {
1126 ret = bdrv_snapshot_goto(bs1, name);
1129 monitor_printf(mon, "Warning: ");
1133 "Snapshots not supported on device '%s'\n",
1134 bdrv_get_device_name(bs1));
1137 monitor_printf(mon, "Could not find snapshot '%s' on "
1139 name, bdrv_get_device_name(bs1));
1142 monitor_printf(mon, "Error %d while activating snapshot on"
1143 " '%s'\n", ret, bdrv_get_device_name(bs1));
1146 /* fatal on snapshot block device */
1153 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
1154 monitor_printf(mon, "Device %s does not support VM state snapshots\n",
1155 bdrv_get_device_name(bs));
1159 /* Don't even try to load empty VM states */
1160 ret = bdrv_snapshot_find(bs, &sn, name);
1161 if ((ret >= 0) && (sn.vm_state_size == 0))
1164 /* restore the VM state */
1165 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
1167 monitor_printf(mon, "Could not open VM state file\n");
1170 ret = qemu_loadvm_state(f);
1173 monitor_printf(mon, "Error %d while loading VM state\n", ret);
1176 if (saved_vm_running)
1180 void do_delvm(Monitor *mon, const char *name)
1182 BlockDriverState *bs, *bs1;
1185 bs = get_bs_snapshots();
1187 monitor_printf(mon, "No block device supports snapshots\n");
1191 for(i = 0; i <= nb_drives; i++) {
1192 bs1 = drives_table[i].bdrv;
1193 if (bdrv_has_snapshot(bs1)) {
1194 ret = bdrv_snapshot_delete(bs1, name);
1196 if (ret == -ENOTSUP)
1198 "Snapshots not supported on device '%s'\n",
1199 bdrv_get_device_name(bs1));
1201 monitor_printf(mon, "Error %d while deleting snapshot on "
1202 "'%s'\n", ret, bdrv_get_device_name(bs1));
1208 void do_info_snapshots(Monitor *mon)
1210 BlockDriverState *bs, *bs1;
1211 QEMUSnapshotInfo *sn_tab, *sn;
1215 bs = get_bs_snapshots();
1217 monitor_printf(mon, "No available block device supports snapshots\n");
1220 monitor_printf(mon, "Snapshot devices:");
1221 for(i = 0; i <= nb_drives; i++) {
1222 bs1 = drives_table[i].bdrv;
1223 if (bdrv_has_snapshot(bs1)) {
1225 monitor_printf(mon, " %s", bdrv_get_device_name(bs1));
1228 monitor_printf(mon, "\n");
1230 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1232 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1235 monitor_printf(mon, "Snapshot list (from %s):\n",
1236 bdrv_get_device_name(bs));
1237 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1238 for(i = 0; i < nb_sns; i++) {
1240 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));