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
24 #include "qemu-common.h"
29 #include "qemu-timer.h"
30 #include "qemu-char.h"
32 #include "audio/audio.h"
33 #include "migration.h"
34 #include "qemu_socket.h"
45 #include <sys/times.h>
49 #include <sys/ioctl.h>
50 #include <sys/resource.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
54 #if defined(__NetBSD__)
55 #include <net/if_tap.h>
58 #include <linux/if_tun.h>
60 #include <arpa/inet.h>
63 #include <sys/select.h>
71 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
72 #include <freebsd/stdlib.h>
77 #include <linux/rtc.h>
84 #include <sys/timeb.h>
86 #define getopt_long_only getopt_long
87 #define memalign(align, size) malloc(size)
90 /* point to the block driver where the snapshots are managed */
91 static BlockDriverState
*bs_snapshots
;
93 #define SELF_ANNOUNCE_ROUNDS 5
94 #define ETH_P_EXPERIMENTAL 0x01F1 /* just a number */
95 //#define ETH_P_EXPERIMENTAL 0x0012 /* make it the size of the packet */
96 #define EXPERIMENTAL_MAGIC 0xf1f23f4f
98 static int announce_self_create(uint8_t *buf
,
101 uint32_t magic
= EXPERIMENTAL_MAGIC
;
102 uint16_t proto
= htons(ETH_P_EXPERIMENTAL
);
104 /* FIXME: should we send a different packet (arp/rarp/ping)? */
107 memset(buf
, 0xff, 6); /* h_dst */
108 memcpy(buf
+ 6, mac_addr
, 6); /* h_src */
109 memcpy(buf
+ 12, &proto
, 2); /* h_proto */
110 memcpy(buf
+ 14, &magic
, 4); /* magic */
115 static void qemu_announce_self_once(void *opaque
)
121 static int count
= SELF_ANNOUNCE_ROUNDS
;
122 QEMUTimer
*timer
= *(QEMUTimer
**)opaque
;
124 for (i
= 0; i
< MAX_NICS
; i
++) {
125 if (!nd_table
[i
].used
)
127 len
= announce_self_create(buf
, nd_table
[i
].macaddr
);
128 vlan
= nd_table
[i
].vlan
;
129 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
130 vc
->fd_read(vc
->opaque
, buf
, len
);
134 qemu_mod_timer(timer
, qemu_get_clock(rt_clock
) + 100);
136 qemu_del_timer(timer
);
137 qemu_free_timer(timer
);
141 void qemu_announce_self(void)
143 static QEMUTimer
*timer
;
144 timer
= qemu_new_timer(rt_clock
, qemu_announce_self_once
, &timer
);
145 qemu_announce_self_once(&timer
);
148 /***********************************************************/
149 /* savevm/loadvm support */
151 #define IO_BUF_SIZE 32768
154 QEMUFilePutBufferFunc
*put_buffer
;
155 QEMUFileGetBufferFunc
*get_buffer
;
156 QEMUFileCloseFunc
*close
;
157 QEMUFileRateLimit
*rate_limit
;
161 int64_t buf_offset
; /* start of buffer when writing, end of buffer
164 int buf_size
; /* 0 when writing */
165 uint8_t buf
[IO_BUF_SIZE
];
170 typedef struct QEMUFilePopen
176 typedef struct QEMUFileSocket
182 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
184 QEMUFileSocket
*s
= opaque
;
188 len
= recv(s
->fd
, buf
, size
, 0);
189 } while (len
== -1 && socket_error() == EINTR
);
192 len
= -socket_error();
197 static int socket_close(void *opaque
)
199 QEMUFileSocket
*s
= opaque
;
204 static int popen_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
206 QEMUFilePopen
*s
= opaque
;
207 return fwrite(buf
, 1, size
, s
->popen_file
);
210 static int popen_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
212 QEMUFilePopen
*s
= opaque
;
213 return fread(buf
, 1, size
, s
->popen_file
);
216 static int popen_close(void *opaque
)
218 QEMUFilePopen
*s
= opaque
;
219 pclose(s
->popen_file
);
224 QEMUFile
*qemu_popen(FILE *popen_file
, const char *mode
)
228 if (popen_file
== NULL
|| mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
229 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
233 s
= qemu_mallocz(sizeof(QEMUFilePopen
));
235 s
->popen_file
= popen_file
;
238 s
->file
= qemu_fopen_ops(s
, NULL
, popen_get_buffer
, popen_close
, NULL
);
240 s
->file
= qemu_fopen_ops(s
, popen_put_buffer
, NULL
, popen_close
, NULL
);
245 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
249 popen_file
= popen(command
, mode
);
250 if(popen_file
== NULL
) {
254 return qemu_popen(popen_file
, mode
);
257 int qemu_popen_fd(QEMUFile
*f
)
262 p
= (QEMUFilePopen
*)f
->opaque
;
263 fd
= fileno(p
->popen_file
);
268 QEMUFile
*qemu_fopen_socket(int fd
)
270 QEMUFileSocket
*s
= qemu_mallocz(sizeof(QEMUFileSocket
));
273 s
->file
= qemu_fopen_ops(s
, NULL
, socket_get_buffer
, socket_close
, NULL
);
277 typedef struct QEMUFileStdio
282 static int file_put_buffer(void *opaque
, const uint8_t *buf
,
283 int64_t pos
, int size
)
285 QEMUFileStdio
*s
= opaque
;
286 fseek(s
->outfile
, pos
, SEEK_SET
);
287 fwrite(buf
, 1, size
, s
->outfile
);
291 static int file_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
293 QEMUFileStdio
*s
= opaque
;
294 fseek(s
->outfile
, pos
, SEEK_SET
);
295 return fread(buf
, 1, size
, s
->outfile
);
298 static int file_close(void *opaque
)
300 QEMUFileStdio
*s
= opaque
;
306 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
310 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
312 s
->outfile
= fopen(filename
, mode
);
316 if (!strcmp(mode
, "wb"))
317 return qemu_fopen_ops(s
, file_put_buffer
, NULL
, file_close
, NULL
);
318 else if (!strcmp(mode
, "rb"))
319 return qemu_fopen_ops(s
, NULL
, file_get_buffer
, file_close
, NULL
);
328 typedef struct QEMUFileBdrv
330 BlockDriverState
*bs
;
334 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
335 int64_t pos
, int size
)
337 QEMUFileBdrv
*s
= opaque
;
338 bdrv_put_buffer(s
->bs
, buf
, s
->base_offset
+ pos
, size
);
342 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
344 QEMUFileBdrv
*s
= opaque
;
345 return bdrv_get_buffer(s
->bs
, buf
, s
->base_offset
+ pos
, size
);
348 static int bdrv_fclose(void *opaque
)
350 QEMUFileBdrv
*s
= opaque
;
355 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
359 s
= qemu_mallocz(sizeof(QEMUFileBdrv
));
362 s
->base_offset
= offset
;
365 return qemu_fopen_ops(s
, block_put_buffer
, NULL
, bdrv_fclose
, NULL
);
367 return qemu_fopen_ops(s
, NULL
, block_get_buffer
, bdrv_fclose
, NULL
);
370 QEMUFile
*qemu_fopen_ops(void *opaque
, QEMUFilePutBufferFunc
*put_buffer
,
371 QEMUFileGetBufferFunc
*get_buffer
,
372 QEMUFileCloseFunc
*close
,
373 QEMUFileRateLimit
*rate_limit
)
377 f
= qemu_mallocz(sizeof(QEMUFile
));
380 f
->put_buffer
= put_buffer
;
381 f
->get_buffer
= get_buffer
;
383 f
->rate_limit
= rate_limit
;
389 int qemu_file_has_error(QEMUFile
*f
)
394 void qemu_file_set_error(QEMUFile
*f
)
399 void qemu_fflush(QEMUFile
*f
)
404 if (f
->is_write
&& f
->buf_index
> 0) {
407 len
= f
->put_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, f
->buf_index
);
409 f
->buf_offset
+= f
->buf_index
;
416 static void qemu_fill_buffer(QEMUFile
*f
)
426 len
= f
->get_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, IO_BUF_SIZE
);
430 f
->buf_offset
+= len
;
431 } else if (len
!= -EAGAIN
)
435 int qemu_fclose(QEMUFile
*f
)
440 ret
= f
->close(f
->opaque
);
445 void qemu_file_put_notify(QEMUFile
*f
)
447 f
->put_buffer(f
->opaque
, NULL
, 0, 0);
450 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
454 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
456 "Attempted to write to buffer while read buffer is not empty\n");
460 while (!f
->has_error
&& size
> 0) {
461 l
= IO_BUF_SIZE
- f
->buf_index
;
464 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
469 if (f
->buf_index
>= IO_BUF_SIZE
)
474 void qemu_put_byte(QEMUFile
*f
, int v
)
476 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
478 "Attempted to write to buffer while read buffer is not empty\n");
482 f
->buf
[f
->buf_index
++] = v
;
484 if (f
->buf_index
>= IO_BUF_SIZE
)
488 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
497 l
= f
->buf_size
- f
->buf_index
;
500 l
= f
->buf_size
- f
->buf_index
;
506 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
514 int qemu_get_byte(QEMUFile
*f
)
519 if (f
->buf_index
>= f
->buf_size
) {
521 if (f
->buf_index
>= f
->buf_size
)
524 return f
->buf
[f
->buf_index
++];
527 int64_t qemu_ftell(QEMUFile
*f
)
529 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
532 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
534 if (whence
== SEEK_SET
) {
536 } else if (whence
== SEEK_CUR
) {
537 pos
+= qemu_ftell(f
);
539 /* SEEK_END not supported */
553 int qemu_file_rate_limit(QEMUFile
*f
)
556 return f
->rate_limit(f
->opaque
);
561 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
563 qemu_put_byte(f
, v
>> 8);
567 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
569 qemu_put_byte(f
, v
>> 24);
570 qemu_put_byte(f
, v
>> 16);
571 qemu_put_byte(f
, v
>> 8);
575 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
577 qemu_put_be32(f
, v
>> 32);
581 unsigned int qemu_get_be16(QEMUFile
*f
)
584 v
= qemu_get_byte(f
) << 8;
585 v
|= qemu_get_byte(f
);
589 unsigned int qemu_get_be32(QEMUFile
*f
)
592 v
= qemu_get_byte(f
) << 24;
593 v
|= qemu_get_byte(f
) << 16;
594 v
|= qemu_get_byte(f
) << 8;
595 v
|= qemu_get_byte(f
);
599 uint64_t qemu_get_be64(QEMUFile
*f
)
602 v
= (uint64_t)qemu_get_be32(f
) << 32;
603 v
|= qemu_get_be32(f
);
607 typedef struct SaveStateEntry
{
612 SaveLiveStateHandler
*save_live_state
;
613 SaveStateHandler
*save_state
;
614 LoadStateHandler
*load_state
;
616 struct SaveStateEntry
*next
;
619 static SaveStateEntry
*first_se
;
621 /* TODO: Individual devices generally have very little idea about the rest
622 of the system, so instance_id should be removed/replaced.
623 Meanwhile pass -1 as instance_id if you do not already have a clearly
624 distinguishing id for all instances of your device class. */
625 int register_savevm_live(const char *idstr
,
628 SaveLiveStateHandler
*save_live_state
,
629 SaveStateHandler
*save_state
,
630 LoadStateHandler
*load_state
,
633 SaveStateEntry
*se
, **pse
;
634 static int global_section_id
;
636 se
= qemu_malloc(sizeof(SaveStateEntry
));
637 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
638 se
->instance_id
= (instance_id
== -1) ? 0 : instance_id
;
639 se
->version_id
= version_id
;
640 se
->section_id
= global_section_id
++;
641 se
->save_live_state
= save_live_state
;
642 se
->save_state
= save_state
;
643 se
->load_state
= load_state
;
647 /* add at the end of list */
649 while (*pse
!= NULL
) {
650 if (instance_id
== -1
651 && strcmp(se
->idstr
, (*pse
)->idstr
) == 0
652 && se
->instance_id
<= (*pse
)->instance_id
)
653 se
->instance_id
= (*pse
)->instance_id
+ 1;
660 int register_savevm(const char *idstr
,
663 SaveStateHandler
*save_state
,
664 LoadStateHandler
*load_state
,
667 return register_savevm_live(idstr
, instance_id
, version_id
,
668 NULL
, save_state
, load_state
, opaque
);
671 void unregister_savevm(const char *idstr
, void *opaque
)
673 SaveStateEntry
**pse
;
676 while (*pse
!= NULL
) {
677 if (strcmp((*pse
)->idstr
, idstr
) == 0 && (*pse
)->opaque
== opaque
) {
678 SaveStateEntry
*next
= (*pse
)->next
;
687 #define QEMU_VM_FILE_MAGIC 0x5145564d
688 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
689 #define QEMU_VM_FILE_VERSION 0x00000003
691 #define QEMU_VM_EOF 0x00
692 #define QEMU_VM_SECTION_START 0x01
693 #define QEMU_VM_SECTION_PART 0x02
694 #define QEMU_VM_SECTION_END 0x03
695 #define QEMU_VM_SECTION_FULL 0x04
697 int qemu_savevm_state_begin(QEMUFile
*f
)
701 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
702 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
704 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
707 if (se
->save_live_state
== NULL
)
711 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
712 qemu_put_be32(f
, se
->section_id
);
715 len
= strlen(se
->idstr
);
716 qemu_put_byte(f
, len
);
717 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
719 qemu_put_be32(f
, se
->instance_id
);
720 qemu_put_be32(f
, se
->version_id
);
722 se
->save_live_state(f
, QEMU_VM_SECTION_START
, se
->opaque
);
725 if (qemu_file_has_error(f
))
731 int qemu_savevm_state_iterate(QEMUFile
*f
)
736 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
737 if (se
->save_live_state
== NULL
)
741 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
742 qemu_put_be32(f
, se
->section_id
);
744 ret
&= !!se
->save_live_state(f
, QEMU_VM_SECTION_PART
, se
->opaque
);
750 if (qemu_file_has_error(f
))
756 int qemu_savevm_state_complete(QEMUFile
*f
)
760 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
761 if (se
->save_live_state
== NULL
)
765 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
766 qemu_put_be32(f
, se
->section_id
);
768 se
->save_live_state(f
, QEMU_VM_SECTION_END
, se
->opaque
);
771 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
774 if (se
->save_state
== NULL
)
778 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
779 qemu_put_be32(f
, se
->section_id
);
782 len
= strlen(se
->idstr
);
783 qemu_put_byte(f
, len
);
784 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
786 qemu_put_be32(f
, se
->instance_id
);
787 qemu_put_be32(f
, se
->version_id
);
789 se
->save_state(f
, se
->opaque
);
792 qemu_put_byte(f
, QEMU_VM_EOF
);
794 if (qemu_file_has_error(f
))
800 int qemu_savevm_state(QEMUFile
*f
)
802 int saved_vm_running
;
805 saved_vm_running
= vm_running
;
810 ret
= qemu_savevm_state_begin(f
);
815 ret
= qemu_savevm_state_iterate(f
);
820 ret
= qemu_savevm_state_complete(f
);
823 if (qemu_file_has_error(f
))
826 if (!ret
&& saved_vm_running
)
832 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
836 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
837 if (!strcmp(se
->idstr
, idstr
) &&
838 instance_id
== se
->instance_id
)
844 typedef struct LoadStateEntry
{
848 struct LoadStateEntry
*next
;
851 static int qemu_loadvm_state_v2(QEMUFile
*f
)
854 int len
, ret
, instance_id
, record_len
, version_id
;
855 int64_t total_len
, end_pos
, cur_pos
;
858 total_len
= qemu_get_be64(f
);
859 end_pos
= total_len
+ qemu_ftell(f
);
861 if (qemu_ftell(f
) >= end_pos
)
863 len
= qemu_get_byte(f
);
864 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
866 instance_id
= qemu_get_be32(f
);
867 version_id
= qemu_get_be32(f
);
868 record_len
= qemu_get_be32(f
);
869 cur_pos
= qemu_ftell(f
);
870 se
= find_se(idstr
, instance_id
);
872 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
875 ret
= se
->load_state(f
, se
->opaque
, version_id
);
877 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
882 /* always seek to exact end of record */
883 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
886 if (qemu_file_has_error(f
))
892 int qemu_loadvm_state(QEMUFile
*f
)
894 LoadStateEntry
*first_le
= NULL
;
895 uint8_t section_type
;
899 v
= qemu_get_be32(f
);
900 if (v
!= QEMU_VM_FILE_MAGIC
)
903 v
= qemu_get_be32(f
);
904 if (v
== QEMU_VM_FILE_VERSION_COMPAT
)
905 return qemu_loadvm_state_v2(f
);
906 if (v
!= QEMU_VM_FILE_VERSION
)
909 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
910 uint32_t instance_id
, version_id
, section_id
;
916 switch (section_type
) {
917 case QEMU_VM_SECTION_START
:
918 case QEMU_VM_SECTION_FULL
:
919 /* Read section start */
920 section_id
= qemu_get_be32(f
);
921 len
= qemu_get_byte(f
);
922 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
924 instance_id
= qemu_get_be32(f
);
925 version_id
= qemu_get_be32(f
);
927 /* Find savevm section */
928 se
= find_se(idstr
, instance_id
);
930 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
935 /* Validate version */
936 if (version_id
> se
->version_id
) {
937 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
938 version_id
, idstr
, se
->version_id
);
944 le
= qemu_mallocz(sizeof(*le
));
947 le
->section_id
= section_id
;
948 le
->version_id
= version_id
;
952 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
954 case QEMU_VM_SECTION_PART
:
955 case QEMU_VM_SECTION_END
:
956 section_id
= qemu_get_be32(f
);
958 for (le
= first_le
; le
&& le
->section_id
!= section_id
; le
= le
->next
);
960 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
965 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
968 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
978 LoadStateEntry
*le
= first_le
;
979 first_le
= first_le
->next
;
983 if (qemu_file_has_error(f
))
989 /* device can contain snapshots */
990 static int bdrv_can_snapshot(BlockDriverState
*bs
)
993 !bdrv_is_removable(bs
) &&
994 !bdrv_is_read_only(bs
));
997 /* device must be snapshots in order to have a reliable snapshot */
998 static int bdrv_has_snapshot(BlockDriverState
*bs
)
1001 !bdrv_is_removable(bs
) &&
1002 !bdrv_is_read_only(bs
));
1005 static BlockDriverState
*get_bs_snapshots(void)
1007 BlockDriverState
*bs
;
1011 return bs_snapshots
;
1012 for(i
= 0; i
<= nb_drives
; i
++) {
1013 bs
= drives_table
[i
].bdrv
;
1014 if (bdrv_can_snapshot(bs
))
1023 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
1026 QEMUSnapshotInfo
*sn_tab
, *sn
;
1030 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1033 for(i
= 0; i
< nb_sns
; i
++) {
1035 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
1045 void do_savevm(const char *name
)
1047 BlockDriverState
*bs
, *bs1
;
1048 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1049 int must_delete
, ret
, i
;
1050 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
1052 int saved_vm_running
;
1053 uint32_t vm_state_size
;
1060 bs
= get_bs_snapshots();
1062 term_printf("No block device can accept snapshots\n");
1066 /* ??? Should this occur after vm_stop? */
1069 saved_vm_running
= vm_running
;
1074 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1079 memset(sn
, 0, sizeof(*sn
));
1081 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1082 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1085 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1088 /* fill auxiliary fields */
1091 sn
->date_sec
= tb
.time
;
1092 sn
->date_nsec
= tb
.millitm
* 1000000;
1094 gettimeofday(&tv
, NULL
);
1095 sn
->date_sec
= tv
.tv_sec
;
1096 sn
->date_nsec
= tv
.tv_usec
* 1000;
1098 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
1100 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
1101 term_printf("Device %s does not support VM state snapshots\n",
1102 bdrv_get_device_name(bs
));
1106 /* save the VM state */
1107 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
1109 term_printf("Could not open VM state file\n");
1112 ret
= qemu_savevm_state(f
);
1113 vm_state_size
= qemu_ftell(f
);
1116 term_printf("Error %d while writing VM\n", ret
);
1120 /* create the snapshots */
1122 for(i
= 0; i
< nb_drives
; i
++) {
1123 bs1
= drives_table
[i
].bdrv
;
1124 if (bdrv_has_snapshot(bs1
)) {
1126 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
1128 term_printf("Error while deleting snapshot on '%s'\n",
1129 bdrv_get_device_name(bs1
));
1132 /* Write VM state size only to the image that contains the state */
1133 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1134 ret
= bdrv_snapshot_create(bs1
, sn
);
1136 term_printf("Error while creating snapshot on '%s'\n",
1137 bdrv_get_device_name(bs1
));
1143 if (saved_vm_running
)
1147 void do_loadvm(const char *name
)
1149 BlockDriverState
*bs
, *bs1
;
1150 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
1151 QEMUSnapshotInfo sn
;
1154 int saved_vm_running
;
1156 bs
= get_bs_snapshots();
1158 term_printf("No block device supports snapshots\n");
1162 /* Flush all IO requests so they don't interfere with the new state. */
1165 saved_vm_running
= vm_running
;
1168 for(i
= 0; i
<= nb_drives
; i
++) {
1169 bs1
= drives_table
[i
].bdrv
;
1170 if (bdrv_has_snapshot(bs1
)) {
1171 ret
= bdrv_snapshot_goto(bs1
, name
);
1174 term_printf("Warning: ");
1177 term_printf("Snapshots not supported on device '%s'\n",
1178 bdrv_get_device_name(bs1
));
1181 term_printf("Could not find snapshot '%s' on device '%s'\n",
1182 name
, bdrv_get_device_name(bs1
));
1185 term_printf("Error %d while activating snapshot on '%s'\n",
1186 ret
, bdrv_get_device_name(bs1
));
1189 /* fatal on snapshot block device */
1196 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
1197 term_printf("Device %s does not support VM state snapshots\n",
1198 bdrv_get_device_name(bs
));
1202 /* Don't even try to load empty VM states */
1203 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1204 if ((ret
>= 0) && (sn
.vm_state_size
== 0))
1207 /* restore the VM state */
1208 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
1210 term_printf("Could not open VM state file\n");
1213 ret
= qemu_loadvm_state(f
);
1216 term_printf("Error %d while loading VM state\n", ret
);
1219 if (saved_vm_running
)
1223 void do_delvm(const char *name
)
1225 BlockDriverState
*bs
, *bs1
;
1228 bs
= get_bs_snapshots();
1230 term_printf("No block device supports snapshots\n");
1234 for(i
= 0; i
<= nb_drives
; i
++) {
1235 bs1
= drives_table
[i
].bdrv
;
1236 if (bdrv_has_snapshot(bs1
)) {
1237 ret
= bdrv_snapshot_delete(bs1
, name
);
1239 if (ret
== -ENOTSUP
)
1240 term_printf("Snapshots not supported on device '%s'\n",
1241 bdrv_get_device_name(bs1
));
1243 term_printf("Error %d while deleting snapshot on '%s'\n",
1244 ret
, bdrv_get_device_name(bs1
));
1250 void do_info_snapshots(void)
1252 BlockDriverState
*bs
, *bs1
;
1253 QEMUSnapshotInfo
*sn_tab
, *sn
;
1257 bs
= get_bs_snapshots();
1259 term_printf("No available block device supports snapshots\n");
1262 term_printf("Snapshot devices:");
1263 for(i
= 0; i
<= nb_drives
; i
++) {
1264 bs1
= drives_table
[i
].bdrv
;
1265 if (bdrv_has_snapshot(bs1
)) {
1267 term_printf(" %s", bdrv_get_device_name(bs1
));
1272 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1274 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
1277 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
1278 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
1279 for(i
= 0; i
< nb_sns
; i
++) {
1281 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));