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)? */
106 memset(buf
, 0xff, 6); /* h_dst */
107 memcpy(buf
+ 6, mac_addr
, 6); /* h_src */
108 memcpy(buf
+ 12, &proto
, 2); /* h_proto */
109 memcpy(buf
+ 14, &magic
, 4); /* magic */
114 void qemu_announce_self(void)
121 for (i
= 0; i
< nb_nics
; i
++) {
122 len
= announce_self_create(buf
, nd_table
[i
].macaddr
);
123 vlan
= nd_table
[i
].vlan
;
124 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
125 for (j
=0; j
< SELF_ANNOUNCE_ROUNDS
; j
++)
126 vc
->fd_read(vc
->opaque
, buf
, len
);
131 /***********************************************************/
132 /* savevm/loadvm support */
134 #define IO_BUF_SIZE 32768
137 QEMUFilePutBufferFunc
*put_buffer
;
138 QEMUFileGetBufferFunc
*get_buffer
;
139 QEMUFileCloseFunc
*close
;
140 QEMUFileRateLimit
*rate_limit
;
144 int64_t buf_offset
; /* start of buffer when writing, end of buffer
147 int buf_size
; /* 0 when writing */
148 uint8_t buf
[IO_BUF_SIZE
];
153 typedef struct QEMUFilePopen
159 typedef struct QEMUFileSocket
165 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
167 QEMUFileSocket
*s
= opaque
;
171 len
= recv(s
->fd
, buf
, size
, 0);
172 } while (len
== -1 && socket_error() == EINTR
);
175 len
= -socket_error();
180 static int socket_close(void *opaque
)
182 QEMUFileSocket
*s
= opaque
;
187 static int popen_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
189 QEMUFilePopen
*s
= opaque
;
190 return fwrite(buf
, 1, size
, s
->popen_file
);
193 static int popen_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
195 QEMUFilePopen
*s
= opaque
;
196 return fread(buf
, 1, size
, s
->popen_file
);
199 static int popen_close(void *opaque
)
201 QEMUFilePopen
*s
= opaque
;
202 pclose(s
->popen_file
);
207 QEMUFile
*qemu_popen(FILE *popen_file
, const char *mode
)
211 if (popen_file
== NULL
|| mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
212 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
216 s
= qemu_mallocz(sizeof(QEMUFilePopen
));
218 s
->popen_file
= popen_file
;
221 s
->file
= qemu_fopen_ops(s
, NULL
, popen_get_buffer
, popen_close
, NULL
);
223 s
->file
= qemu_fopen_ops(s
, popen_put_buffer
, NULL
, popen_close
, NULL
);
225 fprintf(stderr
, "qemu_popen: returning result of qemu_fopen_ops\n");
229 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
233 popen_file
= popen(command
, mode
);
234 if(popen_file
== NULL
) {
238 return qemu_popen(popen_file
, mode
);
241 QEMUFile
*qemu_fopen_socket(int fd
)
243 QEMUFileSocket
*s
= qemu_mallocz(sizeof(QEMUFileSocket
));
246 s
->file
= qemu_fopen_ops(s
, NULL
, socket_get_buffer
, socket_close
, NULL
);
250 typedef struct QEMUFileStdio
255 static int file_put_buffer(void *opaque
, const uint8_t *buf
,
256 int64_t pos
, int size
)
258 QEMUFileStdio
*s
= opaque
;
259 fseek(s
->outfile
, pos
, SEEK_SET
);
260 fwrite(buf
, 1, size
, s
->outfile
);
264 static int file_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
266 QEMUFileStdio
*s
= opaque
;
267 fseek(s
->outfile
, pos
, SEEK_SET
);
268 return fread(buf
, 1, size
, s
->outfile
);
271 static int file_close(void *opaque
)
273 QEMUFileStdio
*s
= opaque
;
279 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
283 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
285 s
->outfile
= fopen(filename
, mode
);
289 if (!strcmp(mode
, "wb"))
290 return qemu_fopen_ops(s
, file_put_buffer
, NULL
, file_close
, NULL
);
291 else if (!strcmp(mode
, "rb"))
292 return qemu_fopen_ops(s
, NULL
, file_get_buffer
, file_close
, NULL
);
301 typedef struct QEMUFileBdrv
303 BlockDriverState
*bs
;
307 static int bdrv_put_buffer(void *opaque
, const uint8_t *buf
,
308 int64_t pos
, int size
)
310 QEMUFileBdrv
*s
= opaque
;
311 bdrv_pwrite(s
->bs
, s
->base_offset
+ pos
, buf
, size
);
315 static int bdrv_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
317 QEMUFileBdrv
*s
= opaque
;
318 return bdrv_pread(s
->bs
, s
->base_offset
+ pos
, buf
, size
);
321 static int bdrv_fclose(void *opaque
)
323 QEMUFileBdrv
*s
= opaque
;
328 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
332 s
= qemu_mallocz(sizeof(QEMUFileBdrv
));
335 s
->base_offset
= offset
;
338 return qemu_fopen_ops(s
, bdrv_put_buffer
, NULL
, bdrv_fclose
, NULL
);
340 return qemu_fopen_ops(s
, NULL
, bdrv_get_buffer
, bdrv_fclose
, NULL
);
343 QEMUFile
*qemu_fopen_ops(void *opaque
, QEMUFilePutBufferFunc
*put_buffer
,
344 QEMUFileGetBufferFunc
*get_buffer
,
345 QEMUFileCloseFunc
*close
,
346 QEMUFileRateLimit
*rate_limit
)
350 f
= qemu_mallocz(sizeof(QEMUFile
));
353 f
->put_buffer
= put_buffer
;
354 f
->get_buffer
= get_buffer
;
356 f
->rate_limit
= rate_limit
;
362 int qemu_file_has_error(QEMUFile
*f
)
367 void qemu_fflush(QEMUFile
*f
)
372 if (f
->is_write
&& f
->buf_index
> 0) {
375 len
= f
->put_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, f
->buf_index
);
377 f
->buf_offset
+= f
->buf_index
;
384 static void qemu_fill_buffer(QEMUFile
*f
)
394 len
= f
->get_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, IO_BUF_SIZE
);
398 f
->buf_offset
+= len
;
399 } else if (len
!= -EAGAIN
)
403 int qemu_fclose(QEMUFile
*f
)
408 ret
= f
->close(f
->opaque
);
413 void qemu_file_put_notify(QEMUFile
*f
)
415 f
->put_buffer(f
->opaque
, NULL
, 0, 0);
418 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
422 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
424 "Attempted to write to buffer while read buffer is not empty\n");
428 while (!f
->has_error
&& size
> 0) {
429 l
= IO_BUF_SIZE
- f
->buf_index
;
432 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
437 if (f
->buf_index
>= IO_BUF_SIZE
)
442 void qemu_put_byte(QEMUFile
*f
, int v
)
444 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
446 "Attempted to write to buffer while read buffer is not empty\n");
450 f
->buf
[f
->buf_index
++] = v
;
452 if (f
->buf_index
>= IO_BUF_SIZE
)
456 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
465 l
= f
->buf_size
- f
->buf_index
;
468 l
= f
->buf_size
- f
->buf_index
;
474 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
482 int qemu_get_byte(QEMUFile
*f
)
487 if (f
->buf_index
>= f
->buf_size
) {
489 if (f
->buf_index
>= f
->buf_size
)
492 return f
->buf
[f
->buf_index
++];
495 int64_t qemu_ftell(QEMUFile
*f
)
497 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
500 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
502 if (whence
== SEEK_SET
) {
504 } else if (whence
== SEEK_CUR
) {
505 pos
+= qemu_ftell(f
);
507 /* SEEK_END not supported */
521 int qemu_file_rate_limit(QEMUFile
*f
)
524 return f
->rate_limit(f
->opaque
);
529 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
531 qemu_put_byte(f
, v
>> 8);
535 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
537 qemu_put_byte(f
, v
>> 24);
538 qemu_put_byte(f
, v
>> 16);
539 qemu_put_byte(f
, v
>> 8);
543 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
545 qemu_put_be32(f
, v
>> 32);
549 unsigned int qemu_get_be16(QEMUFile
*f
)
552 v
= qemu_get_byte(f
) << 8;
553 v
|= qemu_get_byte(f
);
557 unsigned int qemu_get_be32(QEMUFile
*f
)
560 v
= qemu_get_byte(f
) << 24;
561 v
|= qemu_get_byte(f
) << 16;
562 v
|= qemu_get_byte(f
) << 8;
563 v
|= qemu_get_byte(f
);
567 uint64_t qemu_get_be64(QEMUFile
*f
)
570 v
= (uint64_t)qemu_get_be32(f
) << 32;
571 v
|= qemu_get_be32(f
);
575 typedef struct SaveStateEntry
{
580 SaveLiveStateHandler
*save_live_state
;
581 SaveStateHandler
*save_state
;
582 LoadStateHandler
*load_state
;
584 struct SaveStateEntry
*next
;
587 static SaveStateEntry
*first_se
;
589 /* TODO: Individual devices generally have very little idea about the rest
590 of the system, so instance_id should be removed/replaced.
591 Meanwhile pass -1 as instance_id if you do not already have a clearly
592 distinguishing id for all instances of your device class. */
593 int register_savevm_live(const char *idstr
,
596 SaveLiveStateHandler
*save_live_state
,
597 SaveStateHandler
*save_state
,
598 LoadStateHandler
*load_state
,
601 SaveStateEntry
*se
, **pse
;
602 static int global_section_id
;
604 se
= qemu_malloc(sizeof(SaveStateEntry
));
605 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
606 se
->instance_id
= (instance_id
== -1) ? 0 : instance_id
;
607 se
->version_id
= version_id
;
608 se
->section_id
= global_section_id
++;
609 se
->save_live_state
= save_live_state
;
610 se
->save_state
= save_state
;
611 se
->load_state
= load_state
;
615 /* add at the end of list */
617 while (*pse
!= NULL
) {
618 if (instance_id
== -1
619 && strcmp(se
->idstr
, (*pse
)->idstr
) == 0
620 && se
->instance_id
<= (*pse
)->instance_id
)
621 se
->instance_id
= (*pse
)->instance_id
+ 1;
628 int register_savevm(const char *idstr
,
631 SaveStateHandler
*save_state
,
632 LoadStateHandler
*load_state
,
635 return register_savevm_live(idstr
, instance_id
, version_id
,
636 NULL
, save_state
, load_state
, opaque
);
639 #define QEMU_VM_FILE_MAGIC 0x5145564d
640 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
641 #define QEMU_VM_FILE_VERSION 0x00000003
643 #define QEMU_VM_EOF 0x00
644 #define QEMU_VM_SECTION_START 0x01
645 #define QEMU_VM_SECTION_PART 0x02
646 #define QEMU_VM_SECTION_END 0x03
647 #define QEMU_VM_SECTION_FULL 0x04
649 int qemu_savevm_state_begin(QEMUFile
*f
)
653 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
654 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
656 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
659 if (se
->save_live_state
== NULL
)
663 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
664 qemu_put_be32(f
, se
->section_id
);
667 len
= strlen(se
->idstr
);
668 qemu_put_byte(f
, len
);
669 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
671 qemu_put_be32(f
, se
->instance_id
);
672 qemu_put_be32(f
, se
->version_id
);
674 se
->save_live_state(f
, QEMU_VM_SECTION_START
, se
->opaque
);
677 if (qemu_file_has_error(f
))
683 int qemu_savevm_state_iterate(QEMUFile
*f
)
688 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
689 if (se
->save_live_state
== NULL
)
693 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
694 qemu_put_be32(f
, se
->section_id
);
696 ret
&= !!se
->save_live_state(f
, QEMU_VM_SECTION_PART
, se
->opaque
);
702 if (qemu_file_has_error(f
))
708 int qemu_savevm_state_complete(QEMUFile
*f
)
712 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
713 if (se
->save_live_state
== NULL
)
717 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
718 qemu_put_be32(f
, se
->section_id
);
720 se
->save_live_state(f
, QEMU_VM_SECTION_END
, se
->opaque
);
723 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
726 if (se
->save_state
== NULL
)
730 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
731 qemu_put_be32(f
, se
->section_id
);
734 len
= strlen(se
->idstr
);
735 qemu_put_byte(f
, len
);
736 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
738 qemu_put_be32(f
, se
->instance_id
);
739 qemu_put_be32(f
, se
->version_id
);
741 se
->save_state(f
, se
->opaque
);
744 qemu_put_byte(f
, QEMU_VM_EOF
);
746 if (qemu_file_has_error(f
))
752 int qemu_savevm_state(QEMUFile
*f
)
754 int saved_vm_running
;
757 saved_vm_running
= vm_running
;
762 ret
= qemu_savevm_state_begin(f
);
767 ret
= qemu_savevm_state_iterate(f
);
772 ret
= qemu_savevm_state_complete(f
);
775 if (qemu_file_has_error(f
))
778 if (!ret
&& saved_vm_running
)
784 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
788 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
789 if (!strcmp(se
->idstr
, idstr
) &&
790 instance_id
== se
->instance_id
)
796 typedef struct LoadStateEntry
{
800 struct LoadStateEntry
*next
;
803 static int qemu_loadvm_state_v2(QEMUFile
*f
)
806 int len
, ret
, instance_id
, record_len
, version_id
;
807 int64_t total_len
, end_pos
, cur_pos
;
810 total_len
= qemu_get_be64(f
);
811 end_pos
= total_len
+ qemu_ftell(f
);
813 if (qemu_ftell(f
) >= end_pos
)
815 len
= qemu_get_byte(f
);
816 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
818 instance_id
= qemu_get_be32(f
);
819 version_id
= qemu_get_be32(f
);
820 record_len
= qemu_get_be32(f
);
821 cur_pos
= qemu_ftell(f
);
822 se
= find_se(idstr
, instance_id
);
824 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
827 ret
= se
->load_state(f
, se
->opaque
, version_id
);
829 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
833 /* always seek to exact end of record */
834 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
837 if (qemu_file_has_error(f
))
843 int qemu_loadvm_state(QEMUFile
*f
)
845 LoadStateEntry
*first_le
= NULL
;
846 uint8_t section_type
;
850 v
= qemu_get_be32(f
);
851 if (v
!= QEMU_VM_FILE_MAGIC
)
854 v
= qemu_get_be32(f
);
855 if (v
== QEMU_VM_FILE_VERSION_COMPAT
)
856 return qemu_loadvm_state_v2(f
);
857 if (v
!= QEMU_VM_FILE_VERSION
)
860 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
861 uint32_t instance_id
, version_id
, section_id
;
867 switch (section_type
) {
868 case QEMU_VM_SECTION_START
:
869 case QEMU_VM_SECTION_FULL
:
870 /* Read section start */
871 section_id
= qemu_get_be32(f
);
872 len
= qemu_get_byte(f
);
873 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
875 instance_id
= qemu_get_be32(f
);
876 version_id
= qemu_get_be32(f
);
878 /* Find savevm section */
879 se
= find_se(idstr
, instance_id
);
881 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
886 /* Validate version */
887 if (version_id
> se
->version_id
) {
888 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
889 version_id
, idstr
, se
->version_id
);
895 le
= qemu_mallocz(sizeof(*le
));
898 le
->section_id
= section_id
;
899 le
->version_id
= version_id
;
903 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
905 case QEMU_VM_SECTION_PART
:
906 case QEMU_VM_SECTION_END
:
907 section_id
= qemu_get_be32(f
);
909 for (le
= first_le
; le
&& le
->section_id
!= section_id
; le
= le
->next
);
911 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
916 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
919 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
929 LoadStateEntry
*le
= first_le
;
930 first_le
= first_le
->next
;
934 if (qemu_file_has_error(f
))
940 /* device can contain snapshots */
941 static int bdrv_can_snapshot(BlockDriverState
*bs
)
944 !bdrv_is_removable(bs
) &&
945 !bdrv_is_read_only(bs
));
948 /* device must be snapshots in order to have a reliable snapshot */
949 static int bdrv_has_snapshot(BlockDriverState
*bs
)
952 !bdrv_is_removable(bs
) &&
953 !bdrv_is_read_only(bs
));
956 static BlockDriverState
*get_bs_snapshots(void)
958 BlockDriverState
*bs
;
963 for(i
= 0; i
<= nb_drives
; i
++) {
964 bs
= drives_table
[i
].bdrv
;
965 if (bdrv_can_snapshot(bs
))
974 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
977 QEMUSnapshotInfo
*sn_tab
, *sn
;
981 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
984 for(i
= 0; i
< nb_sns
; i
++) {
986 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
996 void do_savevm(const char *name
)
998 BlockDriverState
*bs
, *bs1
;
999 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1000 int must_delete
, ret
, i
;
1001 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
1003 int saved_vm_running
;
1004 uint32_t vm_state_size
;
1011 bs
= get_bs_snapshots();
1013 term_printf("No block device can accept snapshots\n");
1017 /* ??? Should this occur after vm_stop? */
1020 saved_vm_running
= vm_running
;
1025 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1030 memset(sn
, 0, sizeof(*sn
));
1032 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1033 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1036 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1039 /* fill auxiliary fields */
1042 sn
->date_sec
= tb
.time
;
1043 sn
->date_nsec
= tb
.millitm
* 1000000;
1045 gettimeofday(&tv
, NULL
);
1046 sn
->date_sec
= tv
.tv_sec
;
1047 sn
->date_nsec
= tv
.tv_usec
* 1000;
1049 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
1051 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
1052 term_printf("Device %s does not support VM state snapshots\n",
1053 bdrv_get_device_name(bs
));
1057 /* save the VM state */
1058 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
1060 term_printf("Could not open VM state file\n");
1063 ret
= qemu_savevm_state(f
);
1064 vm_state_size
= qemu_ftell(f
);
1067 term_printf("Error %d while writing VM\n", ret
);
1071 /* create the snapshots */
1073 for(i
= 0; i
< nb_drives
; i
++) {
1074 bs1
= drives_table
[i
].bdrv
;
1075 if (bdrv_has_snapshot(bs1
)) {
1077 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
1079 term_printf("Error while deleting snapshot on '%s'\n",
1080 bdrv_get_device_name(bs1
));
1083 /* Write VM state size only to the image that contains the state */
1084 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1085 ret
= bdrv_snapshot_create(bs1
, sn
);
1087 term_printf("Error while creating snapshot on '%s'\n",
1088 bdrv_get_device_name(bs1
));
1094 if (saved_vm_running
)
1098 void do_loadvm(const char *name
)
1100 BlockDriverState
*bs
, *bs1
;
1101 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
1102 QEMUSnapshotInfo sn
;
1105 int saved_vm_running
;
1107 bs
= get_bs_snapshots();
1109 term_printf("No block device supports snapshots\n");
1113 /* Flush all IO requests so they don't interfere with the new state. */
1116 saved_vm_running
= vm_running
;
1119 for(i
= 0; i
<= nb_drives
; i
++) {
1120 bs1
= drives_table
[i
].bdrv
;
1121 if (bdrv_has_snapshot(bs1
)) {
1122 ret
= bdrv_snapshot_goto(bs1
, name
);
1125 term_printf("Warning: ");
1128 term_printf("Snapshots not supported on device '%s'\n",
1129 bdrv_get_device_name(bs1
));
1132 term_printf("Could not find snapshot '%s' on device '%s'\n",
1133 name
, bdrv_get_device_name(bs1
));
1136 term_printf("Error %d while activating snapshot on '%s'\n",
1137 ret
, bdrv_get_device_name(bs1
));
1140 /* fatal on snapshot block device */
1147 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
1148 term_printf("Device %s does not support VM state snapshots\n",
1149 bdrv_get_device_name(bs
));
1153 /* Don't even try to load empty VM states */
1154 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1155 if ((ret
>= 0) && (sn
.vm_state_size
== 0))
1158 /* restore the VM state */
1159 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
1161 term_printf("Could not open VM state file\n");
1164 ret
= qemu_loadvm_state(f
);
1167 term_printf("Error %d while loading VM state\n", ret
);
1170 if (saved_vm_running
)
1174 void do_delvm(const char *name
)
1176 BlockDriverState
*bs
, *bs1
;
1179 bs
= get_bs_snapshots();
1181 term_printf("No block device supports snapshots\n");
1185 for(i
= 0; i
<= nb_drives
; i
++) {
1186 bs1
= drives_table
[i
].bdrv
;
1187 if (bdrv_has_snapshot(bs1
)) {
1188 ret
= bdrv_snapshot_delete(bs1
, name
);
1190 if (ret
== -ENOTSUP
)
1191 term_printf("Snapshots not supported on device '%s'\n",
1192 bdrv_get_device_name(bs1
));
1194 term_printf("Error %d while deleting snapshot on '%s'\n",
1195 ret
, bdrv_get_device_name(bs1
));
1201 void do_info_snapshots(void)
1203 BlockDriverState
*bs
, *bs1
;
1204 QEMUSnapshotInfo
*sn_tab
, *sn
;
1208 bs
= get_bs_snapshots();
1210 term_printf("No available block device supports snapshots\n");
1213 term_printf("Snapshot devices:");
1214 for(i
= 0; i
<= nb_drives
; i
++) {
1215 bs1
= drives_table
[i
].bdrv
;
1216 if (bdrv_has_snapshot(bs1
)) {
1218 term_printf(" %s", bdrv_get_device_name(bs1
));
1223 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1225 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
1228 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
1229 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
1230 for(i
= 0; i
< nb_sns
; i
++) {
1232 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));