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 fprintf(stderr
, "qemu_popen: malloc failed\n");
222 s
->popen_file
= popen_file
;
225 s
->file
= qemu_fopen_ops(s
, NULL
, popen_get_buffer
, popen_close
, NULL
);
227 s
->file
= qemu_fopen_ops(s
, popen_put_buffer
, NULL
, popen_close
, NULL
);
229 fprintf(stderr
, "qemu_popen: returning result of qemu_fopen_ops\n");
233 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
237 popen_file
= popen(command
, mode
);
238 if(popen_file
== NULL
) {
242 return qemu_popen(popen_file
, mode
);
245 QEMUFile
*qemu_fopen_socket(int fd
)
247 QEMUFileSocket
*s
= qemu_mallocz(sizeof(QEMUFileSocket
));
253 s
->file
= qemu_fopen_ops(s
, NULL
, socket_get_buffer
, socket_close
, NULL
);
257 typedef struct QEMUFileStdio
262 static int file_put_buffer(void *opaque
, const uint8_t *buf
,
263 int64_t pos
, int size
)
265 QEMUFileStdio
*s
= opaque
;
266 fseek(s
->outfile
, pos
, SEEK_SET
);
267 fwrite(buf
, 1, size
, s
->outfile
);
271 static int file_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
273 QEMUFileStdio
*s
= opaque
;
274 fseek(s
->outfile
, pos
, SEEK_SET
);
275 return fread(buf
, 1, size
, s
->outfile
);
278 static int file_close(void *opaque
)
280 QEMUFileStdio
*s
= opaque
;
286 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
290 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
294 s
->outfile
= fopen(filename
, mode
);
298 if (!strcmp(mode
, "wb"))
299 return qemu_fopen_ops(s
, file_put_buffer
, NULL
, file_close
, NULL
);
300 else if (!strcmp(mode
, "rb"))
301 return qemu_fopen_ops(s
, NULL
, file_get_buffer
, file_close
, NULL
);
310 typedef struct QEMUFileBdrv
312 BlockDriverState
*bs
;
316 static int bdrv_put_buffer(void *opaque
, const uint8_t *buf
,
317 int64_t pos
, int size
)
319 QEMUFileBdrv
*s
= opaque
;
320 bdrv_pwrite(s
->bs
, s
->base_offset
+ pos
, buf
, size
);
324 static int bdrv_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
326 QEMUFileBdrv
*s
= opaque
;
327 return bdrv_pread(s
->bs
, s
->base_offset
+ pos
, buf
, size
);
330 static int bdrv_fclose(void *opaque
)
332 QEMUFileBdrv
*s
= opaque
;
337 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
341 s
= qemu_mallocz(sizeof(QEMUFileBdrv
));
346 s
->base_offset
= offset
;
349 return qemu_fopen_ops(s
, bdrv_put_buffer
, NULL
, bdrv_fclose
, NULL
);
351 return qemu_fopen_ops(s
, NULL
, bdrv_get_buffer
, bdrv_fclose
, NULL
);
354 QEMUFile
*qemu_fopen_ops(void *opaque
, QEMUFilePutBufferFunc
*put_buffer
,
355 QEMUFileGetBufferFunc
*get_buffer
,
356 QEMUFileCloseFunc
*close
,
357 QEMUFileRateLimit
*rate_limit
)
361 f
= qemu_mallocz(sizeof(QEMUFile
));
366 f
->put_buffer
= put_buffer
;
367 f
->get_buffer
= get_buffer
;
369 f
->rate_limit
= rate_limit
;
375 int qemu_file_has_error(QEMUFile
*f
)
380 void qemu_fflush(QEMUFile
*f
)
385 if (f
->is_write
&& f
->buf_index
> 0) {
388 len
= f
->put_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, f
->buf_index
);
390 f
->buf_offset
+= f
->buf_index
;
397 static void qemu_fill_buffer(QEMUFile
*f
)
407 len
= f
->get_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, IO_BUF_SIZE
);
411 f
->buf_offset
+= len
;
412 } else if (len
!= -EAGAIN
)
416 int qemu_fclose(QEMUFile
*f
)
421 ret
= f
->close(f
->opaque
);
426 void qemu_file_put_notify(QEMUFile
*f
)
428 f
->put_buffer(f
->opaque
, NULL
, 0, 0);
431 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
435 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
437 "Attempted to write to buffer while read buffer is not empty\n");
441 while (!f
->has_error
&& size
> 0) {
442 l
= IO_BUF_SIZE
- f
->buf_index
;
445 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
450 if (f
->buf_index
>= IO_BUF_SIZE
)
455 void qemu_put_byte(QEMUFile
*f
, int v
)
457 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
459 "Attempted to write to buffer while read buffer is not empty\n");
463 f
->buf
[f
->buf_index
++] = v
;
465 if (f
->buf_index
>= IO_BUF_SIZE
)
469 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
478 l
= f
->buf_size
- f
->buf_index
;
481 l
= f
->buf_size
- f
->buf_index
;
487 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
495 int qemu_get_byte(QEMUFile
*f
)
500 if (f
->buf_index
>= f
->buf_size
) {
502 if (f
->buf_index
>= f
->buf_size
)
505 return f
->buf
[f
->buf_index
++];
508 int64_t qemu_ftell(QEMUFile
*f
)
510 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
513 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
515 if (whence
== SEEK_SET
) {
517 } else if (whence
== SEEK_CUR
) {
518 pos
+= qemu_ftell(f
);
520 /* SEEK_END not supported */
534 int qemu_file_rate_limit(QEMUFile
*f
)
537 return f
->rate_limit(f
->opaque
);
542 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
544 qemu_put_byte(f
, v
>> 8);
548 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
550 qemu_put_byte(f
, v
>> 24);
551 qemu_put_byte(f
, v
>> 16);
552 qemu_put_byte(f
, v
>> 8);
556 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
558 qemu_put_be32(f
, v
>> 32);
562 unsigned int qemu_get_be16(QEMUFile
*f
)
565 v
= qemu_get_byte(f
) << 8;
566 v
|= qemu_get_byte(f
);
570 unsigned int qemu_get_be32(QEMUFile
*f
)
573 v
= qemu_get_byte(f
) << 24;
574 v
|= qemu_get_byte(f
) << 16;
575 v
|= qemu_get_byte(f
) << 8;
576 v
|= qemu_get_byte(f
);
580 uint64_t qemu_get_be64(QEMUFile
*f
)
583 v
= (uint64_t)qemu_get_be32(f
) << 32;
584 v
|= qemu_get_be32(f
);
588 typedef struct SaveStateEntry
{
593 SaveLiveStateHandler
*save_live_state
;
594 SaveStateHandler
*save_state
;
595 LoadStateHandler
*load_state
;
597 struct SaveStateEntry
*next
;
600 static SaveStateEntry
*first_se
;
602 /* TODO: Individual devices generally have very little idea about the rest
603 of the system, so instance_id should be removed/replaced.
604 Meanwhile pass -1 as instance_id if you do not already have a clearly
605 distinguishing id for all instances of your device class. */
606 int register_savevm_live(const char *idstr
,
609 SaveLiveStateHandler
*save_live_state
,
610 SaveStateHandler
*save_state
,
611 LoadStateHandler
*load_state
,
614 SaveStateEntry
*se
, **pse
;
615 static int global_section_id
;
617 se
= qemu_malloc(sizeof(SaveStateEntry
));
620 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
621 se
->instance_id
= (instance_id
== -1) ? 0 : instance_id
;
622 se
->version_id
= version_id
;
623 se
->section_id
= global_section_id
++;
624 se
->save_live_state
= save_live_state
;
625 se
->save_state
= save_state
;
626 se
->load_state
= load_state
;
630 /* add at the end of list */
632 while (*pse
!= NULL
) {
633 if (instance_id
== -1
634 && strcmp(se
->idstr
, (*pse
)->idstr
) == 0
635 && se
->instance_id
<= (*pse
)->instance_id
)
636 se
->instance_id
= (*pse
)->instance_id
+ 1;
643 int register_savevm(const char *idstr
,
646 SaveStateHandler
*save_state
,
647 LoadStateHandler
*load_state
,
650 return register_savevm_live(idstr
, instance_id
, version_id
,
651 NULL
, save_state
, load_state
, opaque
);
654 #define QEMU_VM_FILE_MAGIC 0x5145564d
655 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
656 #define QEMU_VM_FILE_VERSION 0x00000003
658 #define QEMU_VM_EOF 0x00
659 #define QEMU_VM_SECTION_START 0x01
660 #define QEMU_VM_SECTION_PART 0x02
661 #define QEMU_VM_SECTION_END 0x03
662 #define QEMU_VM_SECTION_FULL 0x04
664 int qemu_savevm_state_begin(QEMUFile
*f
)
668 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
669 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
671 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
674 if (se
->save_live_state
== NULL
)
678 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
679 qemu_put_be32(f
, se
->section_id
);
682 len
= strlen(se
->idstr
);
683 qemu_put_byte(f
, len
);
684 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
686 qemu_put_be32(f
, se
->instance_id
);
687 qemu_put_be32(f
, se
->version_id
);
689 se
->save_live_state(f
, QEMU_VM_SECTION_START
, se
->opaque
);
692 if (qemu_file_has_error(f
))
698 int qemu_savevm_state_iterate(QEMUFile
*f
)
703 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
704 if (se
->save_live_state
== NULL
)
708 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
709 qemu_put_be32(f
, se
->section_id
);
711 ret
&= !!se
->save_live_state(f
, QEMU_VM_SECTION_PART
, se
->opaque
);
717 if (qemu_file_has_error(f
))
723 int qemu_savevm_state_complete(QEMUFile
*f
)
727 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
728 if (se
->save_live_state
== NULL
)
732 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
733 qemu_put_be32(f
, se
->section_id
);
735 se
->save_live_state(f
, QEMU_VM_SECTION_END
, se
->opaque
);
738 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
741 if (se
->save_state
== NULL
)
745 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
746 qemu_put_be32(f
, se
->section_id
);
749 len
= strlen(se
->idstr
);
750 qemu_put_byte(f
, len
);
751 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
753 qemu_put_be32(f
, se
->instance_id
);
754 qemu_put_be32(f
, se
->version_id
);
756 se
->save_state(f
, se
->opaque
);
759 qemu_put_byte(f
, QEMU_VM_EOF
);
761 if (qemu_file_has_error(f
))
767 int qemu_savevm_state(QEMUFile
*f
)
769 int saved_vm_running
;
772 saved_vm_running
= vm_running
;
777 ret
= qemu_savevm_state_begin(f
);
782 ret
= qemu_savevm_state_iterate(f
);
787 ret
= qemu_savevm_state_complete(f
);
790 if (qemu_file_has_error(f
))
793 if (!ret
&& saved_vm_running
)
799 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
803 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
804 if (!strcmp(se
->idstr
, idstr
) &&
805 instance_id
== se
->instance_id
)
811 typedef struct LoadStateEntry
{
815 struct LoadStateEntry
*next
;
818 static int qemu_loadvm_state_v2(QEMUFile
*f
)
821 int len
, ret
, instance_id
, record_len
, version_id
;
822 int64_t total_len
, end_pos
, cur_pos
;
825 total_len
= qemu_get_be64(f
);
826 end_pos
= total_len
+ qemu_ftell(f
);
828 if (qemu_ftell(f
) >= end_pos
)
830 len
= qemu_get_byte(f
);
831 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
833 instance_id
= qemu_get_be32(f
);
834 version_id
= qemu_get_be32(f
);
835 record_len
= qemu_get_be32(f
);
836 cur_pos
= qemu_ftell(f
);
837 se
= find_se(idstr
, instance_id
);
839 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
842 ret
= se
->load_state(f
, se
->opaque
, version_id
);
844 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
848 /* always seek to exact end of record */
849 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
852 if (qemu_file_has_error(f
))
858 int qemu_loadvm_state(QEMUFile
*f
)
860 LoadStateEntry
*first_le
= NULL
;
861 uint8_t section_type
;
865 v
= qemu_get_be32(f
);
866 if (v
!= QEMU_VM_FILE_MAGIC
)
869 v
= qemu_get_be32(f
);
870 if (v
== QEMU_VM_FILE_VERSION_COMPAT
)
871 return qemu_loadvm_state_v2(f
);
872 if (v
!= QEMU_VM_FILE_VERSION
)
875 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
876 uint32_t instance_id
, version_id
, section_id
;
882 switch (section_type
) {
883 case QEMU_VM_SECTION_START
:
884 case QEMU_VM_SECTION_FULL
:
885 /* Read section start */
886 section_id
= qemu_get_be32(f
);
887 len
= qemu_get_byte(f
);
888 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
890 instance_id
= qemu_get_be32(f
);
891 version_id
= qemu_get_be32(f
);
893 /* Find savevm section */
894 se
= find_se(idstr
, instance_id
);
896 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
901 /* Validate version */
902 if (version_id
> se
->version_id
) {
903 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
904 version_id
, idstr
, se
->version_id
);
910 le
= qemu_mallocz(sizeof(*le
));
917 le
->section_id
= section_id
;
918 le
->version_id
= version_id
;
922 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
924 case QEMU_VM_SECTION_PART
:
925 case QEMU_VM_SECTION_END
:
926 section_id
= qemu_get_be32(f
);
928 for (le
= first_le
; le
&& le
->section_id
!= section_id
; le
= le
->next
);
930 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
935 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
938 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
948 LoadStateEntry
*le
= first_le
;
949 first_le
= first_le
->next
;
953 if (qemu_file_has_error(f
))
959 /* device can contain snapshots */
960 static int bdrv_can_snapshot(BlockDriverState
*bs
)
963 !bdrv_is_removable(bs
) &&
964 !bdrv_is_read_only(bs
));
967 /* device must be snapshots in order to have a reliable snapshot */
968 static int bdrv_has_snapshot(BlockDriverState
*bs
)
971 !bdrv_is_removable(bs
) &&
972 !bdrv_is_read_only(bs
));
975 static BlockDriverState
*get_bs_snapshots(void)
977 BlockDriverState
*bs
;
982 for(i
= 0; i
<= nb_drives
; i
++) {
983 bs
= drives_table
[i
].bdrv
;
984 if (bdrv_can_snapshot(bs
))
993 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
996 QEMUSnapshotInfo
*sn_tab
, *sn
;
1000 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1003 for(i
= 0; i
< nb_sns
; i
++) {
1005 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
1015 void do_savevm(const char *name
)
1017 BlockDriverState
*bs
, *bs1
;
1018 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1019 int must_delete
, ret
, i
;
1020 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
1022 int saved_vm_running
;
1023 uint32_t vm_state_size
;
1030 bs
= get_bs_snapshots();
1032 term_printf("No block device can accept snapshots\n");
1036 /* ??? Should this occur after vm_stop? */
1039 saved_vm_running
= vm_running
;
1044 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1049 memset(sn
, 0, sizeof(*sn
));
1051 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1052 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1055 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1058 /* fill auxiliary fields */
1061 sn
->date_sec
= tb
.time
;
1062 sn
->date_nsec
= tb
.millitm
* 1000000;
1064 gettimeofday(&tv
, NULL
);
1065 sn
->date_sec
= tv
.tv_sec
;
1066 sn
->date_nsec
= tv
.tv_usec
* 1000;
1068 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
1070 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
1071 term_printf("Device %s does not support VM state snapshots\n",
1072 bdrv_get_device_name(bs
));
1076 /* save the VM state */
1077 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
1079 term_printf("Could not open VM state file\n");
1082 ret
= qemu_savevm_state(f
);
1083 vm_state_size
= qemu_ftell(f
);
1086 term_printf("Error %d while writing VM\n", ret
);
1090 /* create the snapshots */
1092 for(i
= 0; i
< nb_drives
; i
++) {
1093 bs1
= drives_table
[i
].bdrv
;
1094 if (bdrv_has_snapshot(bs1
)) {
1096 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
1098 term_printf("Error while deleting snapshot on '%s'\n",
1099 bdrv_get_device_name(bs1
));
1102 /* Write VM state size only to the image that contains the state */
1103 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1104 ret
= bdrv_snapshot_create(bs1
, sn
);
1106 term_printf("Error while creating snapshot on '%s'\n",
1107 bdrv_get_device_name(bs1
));
1113 if (saved_vm_running
)
1117 void do_loadvm(const char *name
)
1119 BlockDriverState
*bs
, *bs1
;
1120 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
1121 QEMUSnapshotInfo sn
;
1124 int saved_vm_running
;
1126 bs
= get_bs_snapshots();
1128 term_printf("No block device supports snapshots\n");
1132 /* Flush all IO requests so they don't interfere with the new state. */
1135 saved_vm_running
= vm_running
;
1138 for(i
= 0; i
<= nb_drives
; i
++) {
1139 bs1
= drives_table
[i
].bdrv
;
1140 if (bdrv_has_snapshot(bs1
)) {
1141 ret
= bdrv_snapshot_goto(bs1
, name
);
1144 term_printf("Warning: ");
1147 term_printf("Snapshots not supported on device '%s'\n",
1148 bdrv_get_device_name(bs1
));
1151 term_printf("Could not find snapshot '%s' on device '%s'\n",
1152 name
, bdrv_get_device_name(bs1
));
1155 term_printf("Error %d while activating snapshot on '%s'\n",
1156 ret
, bdrv_get_device_name(bs1
));
1159 /* fatal on snapshot block device */
1166 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
1167 term_printf("Device %s does not support VM state snapshots\n",
1168 bdrv_get_device_name(bs
));
1172 /* Don't even try to load empty VM states */
1173 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1174 if ((ret
>= 0) && (sn
.vm_state_size
== 0))
1177 /* restore the VM state */
1178 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
1180 term_printf("Could not open VM state file\n");
1183 ret
= qemu_loadvm_state(f
);
1186 term_printf("Error %d while loading VM state\n", ret
);
1189 if (saved_vm_running
)
1193 void do_delvm(const char *name
)
1195 BlockDriverState
*bs
, *bs1
;
1198 bs
= get_bs_snapshots();
1200 term_printf("No block device supports snapshots\n");
1204 for(i
= 0; i
<= nb_drives
; i
++) {
1205 bs1
= drives_table
[i
].bdrv
;
1206 if (bdrv_has_snapshot(bs1
)) {
1207 ret
= bdrv_snapshot_delete(bs1
, name
);
1209 if (ret
== -ENOTSUP
)
1210 term_printf("Snapshots not supported on device '%s'\n",
1211 bdrv_get_device_name(bs1
));
1213 term_printf("Error %d while deleting snapshot on '%s'\n",
1214 ret
, bdrv_get_device_name(bs1
));
1220 void do_info_snapshots(void)
1222 BlockDriverState
*bs
, *bs1
;
1223 QEMUSnapshotInfo
*sn_tab
, *sn
;
1227 bs
= get_bs_snapshots();
1229 term_printf("No available block device supports snapshots\n");
1232 term_printf("Snapshot devices:");
1233 for(i
= 0; i
<= nb_drives
; i
++) {
1234 bs1
= drives_table
[i
].bdrv
;
1235 if (bdrv_has_snapshot(bs1
)) {
1237 term_printf(" %s", bdrv_get_device_name(bs1
));
1242 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1244 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
1247 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
1248 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
1249 for(i
= 0; i
< nb_sns
; i
++) {
1251 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));