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",
834 /* always seek to exact end of record */
835 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
838 if (qemu_file_has_error(f
))
844 int qemu_loadvm_state(QEMUFile
*f
)
846 LoadStateEntry
*first_le
= NULL
;
847 uint8_t section_type
;
851 v
= qemu_get_be32(f
);
852 if (v
!= QEMU_VM_FILE_MAGIC
)
855 v
= qemu_get_be32(f
);
856 if (v
== QEMU_VM_FILE_VERSION_COMPAT
)
857 return qemu_loadvm_state_v2(f
);
858 if (v
!= QEMU_VM_FILE_VERSION
)
861 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
862 uint32_t instance_id
, version_id
, section_id
;
868 switch (section_type
) {
869 case QEMU_VM_SECTION_START
:
870 case QEMU_VM_SECTION_FULL
:
871 /* Read section start */
872 section_id
= qemu_get_be32(f
);
873 len
= qemu_get_byte(f
);
874 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
876 instance_id
= qemu_get_be32(f
);
877 version_id
= qemu_get_be32(f
);
879 /* Find savevm section */
880 se
= find_se(idstr
, instance_id
);
882 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
887 /* Validate version */
888 if (version_id
> se
->version_id
) {
889 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
890 version_id
, idstr
, se
->version_id
);
896 le
= qemu_mallocz(sizeof(*le
));
899 le
->section_id
= section_id
;
900 le
->version_id
= version_id
;
904 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
906 case QEMU_VM_SECTION_PART
:
907 case QEMU_VM_SECTION_END
:
908 section_id
= qemu_get_be32(f
);
910 for (le
= first_le
; le
&& le
->section_id
!= section_id
; le
= le
->next
);
912 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
917 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
920 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
930 LoadStateEntry
*le
= first_le
;
931 first_le
= first_le
->next
;
935 if (qemu_file_has_error(f
))
941 /* device can contain snapshots */
942 static int bdrv_can_snapshot(BlockDriverState
*bs
)
945 !bdrv_is_removable(bs
) &&
946 !bdrv_is_read_only(bs
));
949 /* device must be snapshots in order to have a reliable snapshot */
950 static int bdrv_has_snapshot(BlockDriverState
*bs
)
953 !bdrv_is_removable(bs
) &&
954 !bdrv_is_read_only(bs
));
957 static BlockDriverState
*get_bs_snapshots(void)
959 BlockDriverState
*bs
;
964 for(i
= 0; i
<= nb_drives
; i
++) {
965 bs
= drives_table
[i
].bdrv
;
966 if (bdrv_can_snapshot(bs
))
975 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
978 QEMUSnapshotInfo
*sn_tab
, *sn
;
982 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
985 for(i
= 0; i
< nb_sns
; i
++) {
987 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
997 void do_savevm(const char *name
)
999 BlockDriverState
*bs
, *bs1
;
1000 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1001 int must_delete
, ret
, i
;
1002 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
1004 int saved_vm_running
;
1005 uint32_t vm_state_size
;
1012 bs
= get_bs_snapshots();
1014 term_printf("No block device can accept snapshots\n");
1018 /* ??? Should this occur after vm_stop? */
1021 saved_vm_running
= vm_running
;
1026 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1031 memset(sn
, 0, sizeof(*sn
));
1033 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1034 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1037 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1040 /* fill auxiliary fields */
1043 sn
->date_sec
= tb
.time
;
1044 sn
->date_nsec
= tb
.millitm
* 1000000;
1046 gettimeofday(&tv
, NULL
);
1047 sn
->date_sec
= tv
.tv_sec
;
1048 sn
->date_nsec
= tv
.tv_usec
* 1000;
1050 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
1052 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
1053 term_printf("Device %s does not support VM state snapshots\n",
1054 bdrv_get_device_name(bs
));
1058 /* save the VM state */
1059 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
1061 term_printf("Could not open VM state file\n");
1064 ret
= qemu_savevm_state(f
);
1065 vm_state_size
= qemu_ftell(f
);
1068 term_printf("Error %d while writing VM\n", ret
);
1072 /* create the snapshots */
1074 for(i
= 0; i
< nb_drives
; i
++) {
1075 bs1
= drives_table
[i
].bdrv
;
1076 if (bdrv_has_snapshot(bs1
)) {
1078 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
1080 term_printf("Error while deleting snapshot on '%s'\n",
1081 bdrv_get_device_name(bs1
));
1084 /* Write VM state size only to the image that contains the state */
1085 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1086 ret
= bdrv_snapshot_create(bs1
, sn
);
1088 term_printf("Error while creating snapshot on '%s'\n",
1089 bdrv_get_device_name(bs1
));
1095 if (saved_vm_running
)
1099 void do_loadvm(const char *name
)
1101 BlockDriverState
*bs
, *bs1
;
1102 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
1103 QEMUSnapshotInfo sn
;
1106 int saved_vm_running
;
1108 bs
= get_bs_snapshots();
1110 term_printf("No block device supports snapshots\n");
1114 /* Flush all IO requests so they don't interfere with the new state. */
1117 saved_vm_running
= vm_running
;
1120 for(i
= 0; i
<= nb_drives
; i
++) {
1121 bs1
= drives_table
[i
].bdrv
;
1122 if (bdrv_has_snapshot(bs1
)) {
1123 ret
= bdrv_snapshot_goto(bs1
, name
);
1126 term_printf("Warning: ");
1129 term_printf("Snapshots not supported on device '%s'\n",
1130 bdrv_get_device_name(bs1
));
1133 term_printf("Could not find snapshot '%s' on device '%s'\n",
1134 name
, bdrv_get_device_name(bs1
));
1137 term_printf("Error %d while activating snapshot on '%s'\n",
1138 ret
, bdrv_get_device_name(bs1
));
1141 /* fatal on snapshot block device */
1148 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
1149 term_printf("Device %s does not support VM state snapshots\n",
1150 bdrv_get_device_name(bs
));
1154 /* Don't even try to load empty VM states */
1155 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1156 if ((ret
>= 0) && (sn
.vm_state_size
== 0))
1159 /* restore the VM state */
1160 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
1162 term_printf("Could not open VM state file\n");
1165 ret
= qemu_loadvm_state(f
);
1168 term_printf("Error %d while loading VM state\n", ret
);
1171 if (saved_vm_running
)
1175 void do_delvm(const char *name
)
1177 BlockDriverState
*bs
, *bs1
;
1180 bs
= get_bs_snapshots();
1182 term_printf("No block device supports snapshots\n");
1186 for(i
= 0; i
<= nb_drives
; i
++) {
1187 bs1
= drives_table
[i
].bdrv
;
1188 if (bdrv_has_snapshot(bs1
)) {
1189 ret
= bdrv_snapshot_delete(bs1
, name
);
1191 if (ret
== -ENOTSUP
)
1192 term_printf("Snapshots not supported on device '%s'\n",
1193 bdrv_get_device_name(bs1
));
1195 term_printf("Error %d while deleting snapshot on '%s'\n",
1196 ret
, bdrv_get_device_name(bs1
));
1202 void do_info_snapshots(void)
1204 BlockDriverState
*bs
, *bs1
;
1205 QEMUSnapshotInfo
*sn_tab
, *sn
;
1209 bs
= get_bs_snapshots();
1211 term_printf("No available block device supports snapshots\n");
1214 term_printf("Snapshot devices:");
1215 for(i
= 0; i
<= nb_drives
; i
++) {
1216 bs1
= drives_table
[i
].bdrv
;
1217 if (bdrv_has_snapshot(bs1
)) {
1219 term_printf(" %s", bdrv_get_device_name(bs1
));
1224 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1226 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
1229 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
1230 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
1231 for(i
= 0; i
< nb_sns
; i
++) {
1233 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));