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 HOST_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>
57 #if defined(__FreeBSD__) || defined(__DragonFly__)
62 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
63 #include <freebsd/stdlib.h>
68 #include <linux/rtc.h>
76 #include <sys/timeb.h>
78 #define getopt_long_only getopt_long
79 #define memalign(align, size) malloc(size)
82 #include "qemu-common.h"
87 #include "qemu-timer.h"
88 #include "qemu-char.h"
90 #include "audio/audio.h"
91 #include "migration.h"
92 #include "qemu_socket.h"
94 /* point to the block driver where the snapshots are managed */
95 static BlockDriverState *bs_snapshots;
97 #define SELF_ANNOUNCE_ROUNDS 5
98 #define ETH_P_EXPERIMENTAL 0x01F1 /* just a number */
99 //#define ETH_P_EXPERIMENTAL 0x0012 /* make it the size of the packet */
100 #define EXPERIMENTAL_MAGIC 0xf1f23f4f
102 static int announce_self_create(uint8_t *buf,
105 uint32_t magic = EXPERIMENTAL_MAGIC;
106 uint16_t proto = htons(ETH_P_EXPERIMENTAL);
108 /* FIXME: should we send a different packet (arp/rarp/ping)? */
111 memset(buf, 0xff, 6); /* h_dst */
112 memcpy(buf + 6, mac_addr, 6); /* h_src */
113 memcpy(buf + 12, &proto, 2); /* h_proto */
114 memcpy(buf + 14, &magic, 4); /* magic */
119 static void qemu_announce_self_once(void *opaque)
125 static int count = SELF_ANNOUNCE_ROUNDS;
126 QEMUTimer *timer = *(QEMUTimer **)opaque;
128 for (i = 0; i < MAX_NICS; i++) {
129 if (!nd_table[i].used)
131 len = announce_self_create(buf, nd_table[i].macaddr);
132 vlan = nd_table[i].vlan;
133 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
134 vc->fd_read(vc->opaque, buf, len);
138 qemu_mod_timer(timer, qemu_get_clock(rt_clock) + 100);
140 qemu_del_timer(timer);
141 qemu_free_timer(timer);
145 void qemu_announce_self(void)
147 static QEMUTimer *timer;
148 timer = qemu_new_timer(rt_clock, qemu_announce_self_once, &timer);
149 qemu_announce_self_once(&timer);
152 /***********************************************************/
153 /* savevm/loadvm support */
155 #define IO_BUF_SIZE 32768
158 QEMUFilePutBufferFunc *put_buffer;
159 QEMUFileGetBufferFunc *get_buffer;
160 QEMUFileCloseFunc *close;
161 QEMUFileRateLimit *rate_limit;
165 int64_t buf_offset; /* start of buffer when writing, end of buffer
168 int buf_size; /* 0 when writing */
169 uint8_t buf[IO_BUF_SIZE];
174 typedef struct QEMUFilePopen
180 typedef struct QEMUFileSocket
186 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
188 QEMUFileSocket *s = opaque;
192 len = recv(s->fd, buf, size, 0);
193 } while (len == -1 && socket_error() == EINTR);
196 len = -socket_error();
201 static int socket_close(void *opaque)
203 QEMUFileSocket *s = opaque;
208 static int popen_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
210 QEMUFilePopen *s = opaque;
211 return fwrite(buf, 1, size, s->popen_file);
214 static int popen_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
216 QEMUFilePopen *s = opaque;
217 return fread(buf, 1, size, s->popen_file);
220 static int popen_close(void *opaque)
222 QEMUFilePopen *s = opaque;
223 pclose(s->popen_file);
228 QEMUFile *qemu_popen(FILE *popen_file, const char *mode)
232 if (popen_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
233 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
237 s = qemu_mallocz(sizeof(QEMUFilePopen));
239 s->popen_file = popen_file;
242 s->file = qemu_fopen_ops(s, NULL, popen_get_buffer, popen_close, NULL);
244 s->file = qemu_fopen_ops(s, popen_put_buffer, NULL, popen_close, NULL);
246 fprintf(stderr, "qemu_popen: returning result of qemu_fopen_ops\n");
250 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
254 popen_file = popen(command, mode);
255 if(popen_file == NULL) {
259 return qemu_popen(popen_file, mode);
262 QEMUFile *qemu_fopen_socket(int fd)
264 QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
267 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, NULL);
271 typedef struct QEMUFileStdio
276 static int file_put_buffer(void *opaque, const uint8_t *buf,
277 int64_t pos, int size)
279 QEMUFileStdio *s = opaque;
280 fseek(s->outfile, pos, SEEK_SET);
281 fwrite(buf, 1, size, s->outfile);
285 static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
287 QEMUFileStdio *s = opaque;
288 fseek(s->outfile, pos, SEEK_SET);
289 return fread(buf, 1, size, s->outfile);
292 static int file_close(void *opaque)
294 QEMUFileStdio *s = opaque;
300 QEMUFile *qemu_fopen(const char *filename, const char *mode)
304 s = qemu_mallocz(sizeof(QEMUFileStdio));
306 s->outfile = fopen(filename, mode);
310 if (!strcmp(mode, "wb"))
311 return qemu_fopen_ops(s, file_put_buffer, NULL, file_close, NULL);
312 else if (!strcmp(mode, "rb"))
313 return qemu_fopen_ops(s, NULL, file_get_buffer, file_close, NULL);
322 typedef struct QEMUFileBdrv
324 BlockDriverState *bs;
328 static int block_put_buffer(void *opaque, const uint8_t *buf,
329 int64_t pos, int size)
331 QEMUFileBdrv *s = opaque;
332 bdrv_put_buffer(s->bs, buf, s->base_offset + pos, size);
336 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
338 QEMUFileBdrv *s = opaque;
339 return bdrv_get_buffer(s->bs, buf, s->base_offset + pos, size);
342 static int bdrv_fclose(void *opaque)
344 QEMUFileBdrv *s = opaque;
349 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable)
353 s = qemu_mallocz(sizeof(QEMUFileBdrv));
356 s->base_offset = offset;
359 return qemu_fopen_ops(s, block_put_buffer, NULL, bdrv_fclose, NULL);
361 return qemu_fopen_ops(s, NULL, block_get_buffer, bdrv_fclose, NULL);
364 QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
365 QEMUFileGetBufferFunc *get_buffer,
366 QEMUFileCloseFunc *close,
367 QEMUFileRateLimit *rate_limit)
371 f = qemu_mallocz(sizeof(QEMUFile));
374 f->put_buffer = put_buffer;
375 f->get_buffer = get_buffer;
377 f->rate_limit = rate_limit;
383 int qemu_file_has_error(QEMUFile *f)
388 void qemu_file_set_error(QEMUFile *f)
393 void qemu_fflush(QEMUFile *f)
398 if (f->is_write && f->buf_index > 0) {
401 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
403 f->buf_offset += f->buf_index;
410 static void qemu_fill_buffer(QEMUFile *f)
420 len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
424 f->buf_offset += len;
425 } else if (len != -EAGAIN)
429 int qemu_fclose(QEMUFile *f)
434 ret = f->close(f->opaque);
439 void qemu_file_put_notify(QEMUFile *f)
441 f->put_buffer(f->opaque, NULL, 0, 0);
444 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
448 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
450 "Attempted to write to buffer while read buffer is not empty\n");
454 while (!f->has_error && size > 0) {
455 l = IO_BUF_SIZE - f->buf_index;
458 memcpy(f->buf + f->buf_index, buf, l);
463 if (f->buf_index >= IO_BUF_SIZE)
468 void qemu_put_byte(QEMUFile *f, int v)
470 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
472 "Attempted to write to buffer while read buffer is not empty\n");
476 f->buf[f->buf_index++] = v;
478 if (f->buf_index >= IO_BUF_SIZE)
482 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
491 l = f->buf_size - f->buf_index;
494 l = f->buf_size - f->buf_index;
500 memcpy(buf, f->buf + f->buf_index, l);
508 int qemu_get_byte(QEMUFile *f)
513 if (f->buf_index >= f->buf_size) {
515 if (f->buf_index >= f->buf_size)
518 return f->buf[f->buf_index++];
521 int64_t qemu_ftell(QEMUFile *f)
523 return f->buf_offset - f->buf_size + f->buf_index;
526 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
528 if (whence == SEEK_SET) {
530 } else if (whence == SEEK_CUR) {
531 pos += qemu_ftell(f);
533 /* SEEK_END not supported */
547 int qemu_file_rate_limit(QEMUFile *f)
550 return f->rate_limit(f->opaque);
555 void qemu_put_be16(QEMUFile *f, unsigned int v)
557 qemu_put_byte(f, v >> 8);
561 void qemu_put_be32(QEMUFile *f, unsigned int v)
563 qemu_put_byte(f, v >> 24);
564 qemu_put_byte(f, v >> 16);
565 qemu_put_byte(f, v >> 8);
569 void qemu_put_be64(QEMUFile *f, uint64_t v)
571 qemu_put_be32(f, v >> 32);
575 unsigned int qemu_get_be16(QEMUFile *f)
578 v = qemu_get_byte(f) << 8;
579 v |= qemu_get_byte(f);
583 unsigned int qemu_get_be32(QEMUFile *f)
586 v = qemu_get_byte(f) << 24;
587 v |= qemu_get_byte(f) << 16;
588 v |= qemu_get_byte(f) << 8;
589 v |= qemu_get_byte(f);
593 uint64_t qemu_get_be64(QEMUFile *f)
596 v = (uint64_t)qemu_get_be32(f) << 32;
597 v |= qemu_get_be32(f);
601 typedef struct SaveStateEntry {
606 SaveLiveStateHandler *save_live_state;
607 SaveStateHandler *save_state;
608 LoadStateHandler *load_state;
610 struct SaveStateEntry *next;
613 static SaveStateEntry *first_se;
615 /* TODO: Individual devices generally have very little idea about the rest
616 of the system, so instance_id should be removed/replaced.
617 Meanwhile pass -1 as instance_id if you do not already have a clearly
618 distinguishing id for all instances of your device class. */
619 int register_savevm_live(const char *idstr,
622 SaveLiveStateHandler *save_live_state,
623 SaveStateHandler *save_state,
624 LoadStateHandler *load_state,
627 SaveStateEntry *se, **pse;
628 static int global_section_id;
630 se = qemu_malloc(sizeof(SaveStateEntry));
631 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
632 se->instance_id = (instance_id == -1) ? 0 : instance_id;
633 se->version_id = version_id;
634 se->section_id = global_section_id++;
635 se->save_live_state = save_live_state;
636 se->save_state = save_state;
637 se->load_state = load_state;
641 /* add at the end of list */
643 while (*pse != NULL) {
644 if (instance_id == -1
645 && strcmp(se->idstr, (*pse)->idstr) == 0
646 && se->instance_id <= (*pse)->instance_id)
647 se->instance_id = (*pse)->instance_id + 1;
654 int register_savevm(const char *idstr,
657 SaveStateHandler *save_state,
658 LoadStateHandler *load_state,
661 return register_savevm_live(idstr, instance_id, version_id,
662 NULL, save_state, load_state, opaque);
665 void unregister_savevm(const char *idstr, void *opaque)
667 SaveStateEntry **pse;
670 while (*pse != NULL) {
671 if (strcmp((*pse)->idstr, idstr) == 0 && (*pse)->opaque == opaque) {
672 SaveStateEntry *next = (*pse)->next;
681 #define QEMU_VM_FILE_MAGIC 0x5145564d
682 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
683 #define QEMU_VM_FILE_VERSION 0x00000003
685 #define QEMU_VM_EOF 0x00
686 #define QEMU_VM_SECTION_START 0x01
687 #define QEMU_VM_SECTION_PART 0x02
688 #define QEMU_VM_SECTION_END 0x03
689 #define QEMU_VM_SECTION_FULL 0x04
691 int qemu_savevm_state_begin(QEMUFile *f)
695 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
696 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
698 for (se = first_se; se != NULL; se = se->next) {
701 if (se->save_live_state == NULL)
705 qemu_put_byte(f, QEMU_VM_SECTION_START);
706 qemu_put_be32(f, se->section_id);
709 len = strlen(se->idstr);
710 qemu_put_byte(f, len);
711 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
713 qemu_put_be32(f, se->instance_id);
714 qemu_put_be32(f, se->version_id);
716 se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
719 if (qemu_file_has_error(f))
725 int qemu_savevm_state_iterate(QEMUFile *f)
730 for (se = first_se; se != NULL; se = se->next) {
731 if (se->save_live_state == NULL)
735 qemu_put_byte(f, QEMU_VM_SECTION_PART);
736 qemu_put_be32(f, se->section_id);
738 ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
744 if (qemu_file_has_error(f))
750 int qemu_savevm_state_complete(QEMUFile *f)
754 for (se = first_se; se != NULL; se = se->next) {
755 if (se->save_live_state == NULL)
759 qemu_put_byte(f, QEMU_VM_SECTION_END);
760 qemu_put_be32(f, se->section_id);
762 se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
765 for(se = first_se; se != NULL; se = se->next) {
768 if (se->save_state == NULL)
772 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
773 qemu_put_be32(f, se->section_id);
776 len = strlen(se->idstr);
777 qemu_put_byte(f, len);
778 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
780 qemu_put_be32(f, se->instance_id);
781 qemu_put_be32(f, se->version_id);
783 se->save_state(f, se->opaque);
786 qemu_put_byte(f, QEMU_VM_EOF);
788 if (qemu_file_has_error(f))
794 int qemu_savevm_state(QEMUFile *f)
796 int saved_vm_running;
799 saved_vm_running = vm_running;
804 ret = qemu_savevm_state_begin(f);
809 ret = qemu_savevm_state_iterate(f);
814 ret = qemu_savevm_state_complete(f);
817 if (qemu_file_has_error(f))
820 if (!ret && saved_vm_running)
826 static SaveStateEntry *find_se(const char *idstr, int instance_id)
830 for(se = first_se; se != NULL; se = se->next) {
831 if (!strcmp(se->idstr, idstr) &&
832 instance_id == se->instance_id)
838 typedef struct LoadStateEntry {
842 struct LoadStateEntry *next;
845 static int qemu_loadvm_state_v2(QEMUFile *f)
848 int len, ret, instance_id, record_len, version_id;
849 int64_t total_len, end_pos, cur_pos;
852 total_len = qemu_get_be64(f);
853 end_pos = total_len + qemu_ftell(f);
855 if (qemu_ftell(f) >= end_pos)
857 len = qemu_get_byte(f);
858 qemu_get_buffer(f, (uint8_t *)idstr, len);
860 instance_id = qemu_get_be32(f);
861 version_id = qemu_get_be32(f);
862 record_len = qemu_get_be32(f);
863 cur_pos = qemu_ftell(f);
864 se = find_se(idstr, instance_id);
866 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
869 ret = se->load_state(f, se->opaque, version_id);
871 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
876 /* always seek to exact end of record */
877 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
880 if (qemu_file_has_error(f))
886 int qemu_loadvm_state(QEMUFile *f)
888 LoadStateEntry *first_le = NULL;
889 uint8_t section_type;
893 v = qemu_get_be32(f);
894 if (v != QEMU_VM_FILE_MAGIC)
897 v = qemu_get_be32(f);
898 if (v == QEMU_VM_FILE_VERSION_COMPAT)
899 return qemu_loadvm_state_v2(f);
900 if (v != QEMU_VM_FILE_VERSION)
903 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
904 uint32_t instance_id, version_id, section_id;
910 switch (section_type) {
911 case QEMU_VM_SECTION_START:
912 case QEMU_VM_SECTION_FULL:
913 /* Read section start */
914 section_id = qemu_get_be32(f);
915 len = qemu_get_byte(f);
916 qemu_get_buffer(f, (uint8_t *)idstr, len);
918 instance_id = qemu_get_be32(f);
919 version_id = qemu_get_be32(f);
921 /* Find savevm section */
922 se = find_se(idstr, instance_id);
924 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
929 /* Validate version */
930 if (version_id > se->version_id) {
931 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
932 version_id, idstr, se->version_id);
938 le = qemu_mallocz(sizeof(*le));
941 le->section_id = section_id;
942 le->version_id = version_id;
946 le->se->load_state(f, le->se->opaque, le->version_id);
948 case QEMU_VM_SECTION_PART:
949 case QEMU_VM_SECTION_END:
950 section_id = qemu_get_be32(f);
952 for (le = first_le; le && le->section_id != section_id; le = le->next);
954 fprintf(stderr, "Unknown savevm section %d\n", section_id);
959 le->se->load_state(f, le->se->opaque, le->version_id);
962 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
972 LoadStateEntry *le = first_le;
973 first_le = first_le->next;
977 if (qemu_file_has_error(f))
983 /* device can contain snapshots */
984 static int bdrv_can_snapshot(BlockDriverState *bs)
987 !bdrv_is_removable(bs) &&
988 !bdrv_is_read_only(bs));
991 /* device must be snapshots in order to have a reliable snapshot */
992 static int bdrv_has_snapshot(BlockDriverState *bs)
995 !bdrv_is_removable(bs) &&
996 !bdrv_is_read_only(bs));
999 static BlockDriverState *get_bs_snapshots(void)
1001 BlockDriverState *bs;
1005 return bs_snapshots;
1006 for(i = 0; i <= nb_drives; i++) {
1007 bs = drives_table[i].bdrv;
1008 if (bdrv_can_snapshot(bs))
1017 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1020 QEMUSnapshotInfo *sn_tab, *sn;
1024 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1027 for(i = 0; i < nb_sns; i++) {
1029 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1039 void do_savevm(Monitor *mon, const char *name)
1041 BlockDriverState *bs, *bs1;
1042 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
1043 int must_delete, ret, i;
1044 BlockDriverInfo bdi1, *bdi = &bdi1;
1046 int saved_vm_running;
1047 uint32_t vm_state_size;
1054 bs = get_bs_snapshots();
1056 monitor_printf(mon, "No block device can accept snapshots\n");
1060 /* ??? Should this occur after vm_stop? */
1063 saved_vm_running = vm_running;
1068 ret = bdrv_snapshot_find(bs, old_sn, name);
1073 memset(sn, 0, sizeof(*sn));
1075 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1076 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1079 pstrcpy(sn->name, sizeof(sn->name), name);
1082 /* fill auxiliary fields */
1085 sn->date_sec = tb.time;
1086 sn->date_nsec = tb.millitm * 1000000;
1088 gettimeofday(&tv, NULL);
1089 sn->date_sec = tv.tv_sec;
1090 sn->date_nsec = tv.tv_usec * 1000;
1092 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1094 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
1095 monitor_printf(mon, "Device %s does not support VM state snapshots\n",
1096 bdrv_get_device_name(bs));
1100 /* save the VM state */
1101 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
1103 monitor_printf(mon, "Could not open VM state file\n");
1106 ret = qemu_savevm_state(f);
1107 vm_state_size = qemu_ftell(f);
1110 monitor_printf(mon, "Error %d while writing VM\n", ret);
1114 /* create the snapshots */
1116 for(i = 0; i < nb_drives; i++) {
1117 bs1 = drives_table[i].bdrv;
1118 if (bdrv_has_snapshot(bs1)) {
1120 ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
1123 "Error while deleting snapshot on '%s'\n",
1124 bdrv_get_device_name(bs1));
1127 /* Write VM state size only to the image that contains the state */
1128 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
1129 ret = bdrv_snapshot_create(bs1, sn);
1131 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
1132 bdrv_get_device_name(bs1));
1138 if (saved_vm_running)
1142 void do_loadvm(Monitor *mon, const char *name)
1144 BlockDriverState *bs, *bs1;
1145 BlockDriverInfo bdi1, *bdi = &bdi1;
1146 QEMUSnapshotInfo sn;
1149 int saved_vm_running;
1151 bs = get_bs_snapshots();
1153 monitor_printf(mon, "No block device supports snapshots\n");
1157 /* Flush all IO requests so they don't interfere with the new state. */
1160 saved_vm_running = vm_running;
1163 for(i = 0; i <= nb_drives; i++) {
1164 bs1 = drives_table[i].bdrv;
1165 if (bdrv_has_snapshot(bs1)) {
1166 ret = bdrv_snapshot_goto(bs1, name);
1169 monitor_printf(mon, "Warning: ");
1173 "Snapshots not supported on device '%s'\n",
1174 bdrv_get_device_name(bs1));
1177 monitor_printf(mon, "Could not find snapshot '%s' on "
1179 name, bdrv_get_device_name(bs1));
1182 monitor_printf(mon, "Error %d while activating snapshot on"
1183 " '%s'\n", ret, bdrv_get_device_name(bs1));
1186 /* fatal on snapshot block device */
1193 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
1194 monitor_printf(mon, "Device %s does not support VM state snapshots\n",
1195 bdrv_get_device_name(bs));
1199 /* Don't even try to load empty VM states */
1200 ret = bdrv_snapshot_find(bs, &sn, name);
1201 if ((ret >= 0) && (sn.vm_state_size == 0))
1204 /* restore the VM state */
1205 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
1207 monitor_printf(mon, "Could not open VM state file\n");
1210 ret = qemu_loadvm_state(f);
1213 monitor_printf(mon, "Error %d while loading VM state\n", ret);
1216 if (saved_vm_running)
1220 void do_delvm(Monitor *mon, const char *name)
1222 BlockDriverState *bs, *bs1;
1225 bs = get_bs_snapshots();
1227 monitor_printf(mon, "No block device supports snapshots\n");
1231 for(i = 0; i <= nb_drives; i++) {
1232 bs1 = drives_table[i].bdrv;
1233 if (bdrv_has_snapshot(bs1)) {
1234 ret = bdrv_snapshot_delete(bs1, name);
1236 if (ret == -ENOTSUP)
1238 "Snapshots not supported on device '%s'\n",
1239 bdrv_get_device_name(bs1));
1241 monitor_printf(mon, "Error %d while deleting snapshot on "
1242 "'%s'\n", ret, bdrv_get_device_name(bs1));
1248 void do_info_snapshots(Monitor *mon)
1250 BlockDriverState *bs, *bs1;
1251 QEMUSnapshotInfo *sn_tab, *sn;
1255 bs = get_bs_snapshots();
1257 monitor_printf(mon, "No available block device supports snapshots\n");
1260 monitor_printf(mon, "Snapshot devices:");
1261 for(i = 0; i <= nb_drives; i++) {
1262 bs1 = drives_table[i].bdrv;
1263 if (bdrv_has_snapshot(bs1)) {
1265 monitor_printf(mon, " %s", bdrv_get_device_name(bs1));
1268 monitor_printf(mon, "\n");
1270 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1272 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1275 monitor_printf(mon, "Snapshot list (from %s):\n",
1276 bdrv_get_device_name(bs));
1277 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1278 for(i = 0; i < nb_sns; i++) {
1280 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));