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",
849 /* always seek to exact end of record */
850 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
853 if (qemu_file_has_error(f
))
859 int qemu_loadvm_state(QEMUFile
*f
)
861 LoadStateEntry
*first_le
= NULL
;
862 uint8_t section_type
;
866 v
= qemu_get_be32(f
);
867 if (v
!= QEMU_VM_FILE_MAGIC
)
870 v
= qemu_get_be32(f
);
871 if (v
== QEMU_VM_FILE_VERSION_COMPAT
)
872 return qemu_loadvm_state_v2(f
);
873 if (v
!= QEMU_VM_FILE_VERSION
)
876 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
877 uint32_t instance_id
, version_id
, section_id
;
883 switch (section_type
) {
884 case QEMU_VM_SECTION_START
:
885 case QEMU_VM_SECTION_FULL
:
886 /* Read section start */
887 section_id
= qemu_get_be32(f
);
888 len
= qemu_get_byte(f
);
889 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
891 instance_id
= qemu_get_be32(f
);
892 version_id
= qemu_get_be32(f
);
894 /* Find savevm section */
895 se
= find_se(idstr
, instance_id
);
897 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
902 /* Validate version */
903 if (version_id
> se
->version_id
) {
904 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
905 version_id
, idstr
, se
->version_id
);
911 le
= qemu_mallocz(sizeof(*le
));
918 le
->section_id
= section_id
;
919 le
->version_id
= version_id
;
923 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
925 case QEMU_VM_SECTION_PART
:
926 case QEMU_VM_SECTION_END
:
927 section_id
= qemu_get_be32(f
);
929 for (le
= first_le
; le
&& le
->section_id
!= section_id
; le
= le
->next
);
931 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
936 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
939 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
949 LoadStateEntry
*le
= first_le
;
950 first_le
= first_le
->next
;
954 if (qemu_file_has_error(f
))
960 /* device can contain snapshots */
961 static int bdrv_can_snapshot(BlockDriverState
*bs
)
964 !bdrv_is_removable(bs
) &&
965 !bdrv_is_read_only(bs
));
968 /* device must be snapshots in order to have a reliable snapshot */
969 static int bdrv_has_snapshot(BlockDriverState
*bs
)
972 !bdrv_is_removable(bs
) &&
973 !bdrv_is_read_only(bs
));
976 static BlockDriverState
*get_bs_snapshots(void)
978 BlockDriverState
*bs
;
983 for(i
= 0; i
<= nb_drives
; i
++) {
984 bs
= drives_table
[i
].bdrv
;
985 if (bdrv_can_snapshot(bs
))
994 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
997 QEMUSnapshotInfo
*sn_tab
, *sn
;
1001 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1004 for(i
= 0; i
< nb_sns
; i
++) {
1006 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
1016 void do_savevm(const char *name
)
1018 BlockDriverState
*bs
, *bs1
;
1019 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1020 int must_delete
, ret
, i
;
1021 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
1023 int saved_vm_running
;
1024 uint32_t vm_state_size
;
1031 bs
= get_bs_snapshots();
1033 term_printf("No block device can accept snapshots\n");
1037 /* ??? Should this occur after vm_stop? */
1040 saved_vm_running
= vm_running
;
1045 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1050 memset(sn
, 0, sizeof(*sn
));
1052 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1053 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1056 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1059 /* fill auxiliary fields */
1062 sn
->date_sec
= tb
.time
;
1063 sn
->date_nsec
= tb
.millitm
* 1000000;
1065 gettimeofday(&tv
, NULL
);
1066 sn
->date_sec
= tv
.tv_sec
;
1067 sn
->date_nsec
= tv
.tv_usec
* 1000;
1069 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
1071 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
1072 term_printf("Device %s does not support VM state snapshots\n",
1073 bdrv_get_device_name(bs
));
1077 /* save the VM state */
1078 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
1080 term_printf("Could not open VM state file\n");
1083 ret
= qemu_savevm_state(f
);
1084 vm_state_size
= qemu_ftell(f
);
1087 term_printf("Error %d while writing VM\n", ret
);
1091 /* create the snapshots */
1093 for(i
= 0; i
< nb_drives
; i
++) {
1094 bs1
= drives_table
[i
].bdrv
;
1095 if (bdrv_has_snapshot(bs1
)) {
1097 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
1099 term_printf("Error while deleting snapshot on '%s'\n",
1100 bdrv_get_device_name(bs1
));
1103 /* Write VM state size only to the image that contains the state */
1104 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1105 ret
= bdrv_snapshot_create(bs1
, sn
);
1107 term_printf("Error while creating snapshot on '%s'\n",
1108 bdrv_get_device_name(bs1
));
1114 if (saved_vm_running
)
1118 void do_loadvm(const char *name
)
1120 BlockDriverState
*bs
, *bs1
;
1121 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
1122 QEMUSnapshotInfo sn
;
1125 int saved_vm_running
;
1127 bs
= get_bs_snapshots();
1129 term_printf("No block device supports snapshots\n");
1133 /* Flush all IO requests so they don't interfere with the new state. */
1136 saved_vm_running
= vm_running
;
1139 for(i
= 0; i
<= nb_drives
; i
++) {
1140 bs1
= drives_table
[i
].bdrv
;
1141 if (bdrv_has_snapshot(bs1
)) {
1142 ret
= bdrv_snapshot_goto(bs1
, name
);
1145 term_printf("Warning: ");
1148 term_printf("Snapshots not supported on device '%s'\n",
1149 bdrv_get_device_name(bs1
));
1152 term_printf("Could not find snapshot '%s' on device '%s'\n",
1153 name
, bdrv_get_device_name(bs1
));
1156 term_printf("Error %d while activating snapshot on '%s'\n",
1157 ret
, bdrv_get_device_name(bs1
));
1160 /* fatal on snapshot block device */
1167 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
1168 term_printf("Device %s does not support VM state snapshots\n",
1169 bdrv_get_device_name(bs
));
1173 /* Don't even try to load empty VM states */
1174 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1175 if ((ret
>= 0) && (sn
.vm_state_size
== 0))
1178 /* restore the VM state */
1179 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
1181 term_printf("Could not open VM state file\n");
1184 ret
= qemu_loadvm_state(f
);
1187 term_printf("Error %d while loading VM state\n", ret
);
1190 if (saved_vm_running
)
1194 void do_delvm(const char *name
)
1196 BlockDriverState
*bs
, *bs1
;
1199 bs
= get_bs_snapshots();
1201 term_printf("No block device supports snapshots\n");
1205 for(i
= 0; i
<= nb_drives
; i
++) {
1206 bs1
= drives_table
[i
].bdrv
;
1207 if (bdrv_has_snapshot(bs1
)) {
1208 ret
= bdrv_snapshot_delete(bs1
, name
);
1210 if (ret
== -ENOTSUP
)
1211 term_printf("Snapshots not supported on device '%s'\n",
1212 bdrv_get_device_name(bs1
));
1214 term_printf("Error %d while deleting snapshot on '%s'\n",
1215 ret
, bdrv_get_device_name(bs1
));
1221 void do_info_snapshots(void)
1223 BlockDriverState
*bs
, *bs1
;
1224 QEMUSnapshotInfo
*sn_tab
, *sn
;
1228 bs
= get_bs_snapshots();
1230 term_printf("No available block device supports snapshots\n");
1233 term_printf("Snapshot devices:");
1234 for(i
= 0; i
<= nb_drives
; i
++) {
1235 bs1
= drives_table
[i
].bdrv
;
1236 if (bdrv_has_snapshot(bs1
)) {
1238 term_printf(" %s", bdrv_get_device_name(bs1
));
1243 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1245 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
1248 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
1249 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
1250 for(i
= 0; i
< nb_sns
; i
++) {
1252 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));