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
< MAX_NICS
; i
++) {
122 if (!nd_table
[i
].used
)
124 len
= announce_self_create(buf
, nd_table
[i
].macaddr
);
125 vlan
= nd_table
[i
].vlan
;
126 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
127 for (j
=0; j
< SELF_ANNOUNCE_ROUNDS
; j
++)
128 vc
->fd_read(vc
->opaque
, buf
, len
);
133 /***********************************************************/
134 /* savevm/loadvm support */
136 #define IO_BUF_SIZE 32768
139 QEMUFilePutBufferFunc
*put_buffer
;
140 QEMUFileGetBufferFunc
*get_buffer
;
141 QEMUFileCloseFunc
*close
;
142 QEMUFileRateLimit
*rate_limit
;
146 int64_t buf_offset
; /* start of buffer when writing, end of buffer
149 int buf_size
; /* 0 when writing */
150 uint8_t buf
[IO_BUF_SIZE
];
155 typedef struct QEMUFilePopen
161 typedef struct QEMUFileSocket
167 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
169 QEMUFileSocket
*s
= opaque
;
173 len
= recv(s
->fd
, buf
, size
, 0);
174 } while (len
== -1 && socket_error() == EINTR
);
177 len
= -socket_error();
182 static int socket_close(void *opaque
)
184 QEMUFileSocket
*s
= opaque
;
189 static int popen_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
191 QEMUFilePopen
*s
= opaque
;
192 return fwrite(buf
, 1, size
, s
->popen_file
);
195 static int popen_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
197 QEMUFilePopen
*s
= opaque
;
198 return fread(buf
, 1, size
, s
->popen_file
);
201 static int popen_close(void *opaque
)
203 QEMUFilePopen
*s
= opaque
;
204 pclose(s
->popen_file
);
209 QEMUFile
*qemu_popen(FILE *popen_file
, const char *mode
)
213 if (popen_file
== NULL
|| mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
214 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
218 s
= qemu_mallocz(sizeof(QEMUFilePopen
));
220 s
->popen_file
= popen_file
;
223 s
->file
= qemu_fopen_ops(s
, NULL
, popen_get_buffer
, popen_close
, NULL
);
225 s
->file
= qemu_fopen_ops(s
, popen_put_buffer
, NULL
, popen_close
, NULL
);
227 fprintf(stderr
, "qemu_popen: returning result of qemu_fopen_ops\n");
231 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
235 popen_file
= popen(command
, mode
);
236 if(popen_file
== NULL
) {
240 return qemu_popen(popen_file
, mode
);
243 QEMUFile
*qemu_fopen_socket(int fd
)
245 QEMUFileSocket
*s
= qemu_mallocz(sizeof(QEMUFileSocket
));
248 s
->file
= qemu_fopen_ops(s
, NULL
, socket_get_buffer
, socket_close
, NULL
);
252 typedef struct QEMUFileStdio
257 static int file_put_buffer(void *opaque
, const uint8_t *buf
,
258 int64_t pos
, int size
)
260 QEMUFileStdio
*s
= opaque
;
261 fseek(s
->outfile
, pos
, SEEK_SET
);
262 fwrite(buf
, 1, size
, s
->outfile
);
266 static int file_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
268 QEMUFileStdio
*s
= opaque
;
269 fseek(s
->outfile
, pos
, SEEK_SET
);
270 return fread(buf
, 1, size
, s
->outfile
);
273 static int file_close(void *opaque
)
275 QEMUFileStdio
*s
= opaque
;
281 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
285 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
287 s
->outfile
= fopen(filename
, mode
);
291 if (!strcmp(mode
, "wb"))
292 return qemu_fopen_ops(s
, file_put_buffer
, NULL
, file_close
, NULL
);
293 else if (!strcmp(mode
, "rb"))
294 return qemu_fopen_ops(s
, NULL
, file_get_buffer
, file_close
, NULL
);
303 typedef struct QEMUFileBdrv
305 BlockDriverState
*bs
;
309 static int bdrv_put_buffer(void *opaque
, const uint8_t *buf
,
310 int64_t pos
, int size
)
312 QEMUFileBdrv
*s
= opaque
;
313 bdrv_pwrite(s
->bs
, s
->base_offset
+ pos
, buf
, size
);
317 static int bdrv_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
319 QEMUFileBdrv
*s
= opaque
;
320 return bdrv_pread(s
->bs
, s
->base_offset
+ pos
, buf
, size
);
323 static int bdrv_fclose(void *opaque
)
325 QEMUFileBdrv
*s
= opaque
;
330 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
334 s
= qemu_mallocz(sizeof(QEMUFileBdrv
));
337 s
->base_offset
= offset
;
340 return qemu_fopen_ops(s
, bdrv_put_buffer
, NULL
, bdrv_fclose
, NULL
);
342 return qemu_fopen_ops(s
, NULL
, bdrv_get_buffer
, bdrv_fclose
, NULL
);
345 QEMUFile
*qemu_fopen_ops(void *opaque
, QEMUFilePutBufferFunc
*put_buffer
,
346 QEMUFileGetBufferFunc
*get_buffer
,
347 QEMUFileCloseFunc
*close
,
348 QEMUFileRateLimit
*rate_limit
)
352 f
= qemu_mallocz(sizeof(QEMUFile
));
355 f
->put_buffer
= put_buffer
;
356 f
->get_buffer
= get_buffer
;
358 f
->rate_limit
= rate_limit
;
364 int qemu_file_has_error(QEMUFile
*f
)
369 void qemu_fflush(QEMUFile
*f
)
374 if (f
->is_write
&& f
->buf_index
> 0) {
377 len
= f
->put_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, f
->buf_index
);
379 f
->buf_offset
+= f
->buf_index
;
386 static void qemu_fill_buffer(QEMUFile
*f
)
396 len
= f
->get_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, IO_BUF_SIZE
);
400 f
->buf_offset
+= len
;
401 } else if (len
!= -EAGAIN
)
405 int qemu_fclose(QEMUFile
*f
)
410 ret
= f
->close(f
->opaque
);
415 void qemu_file_put_notify(QEMUFile
*f
)
417 f
->put_buffer(f
->opaque
, NULL
, 0, 0);
420 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
424 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
426 "Attempted to write to buffer while read buffer is not empty\n");
430 while (!f
->has_error
&& size
> 0) {
431 l
= IO_BUF_SIZE
- f
->buf_index
;
434 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
439 if (f
->buf_index
>= IO_BUF_SIZE
)
444 void qemu_put_byte(QEMUFile
*f
, int v
)
446 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
448 "Attempted to write to buffer while read buffer is not empty\n");
452 f
->buf
[f
->buf_index
++] = v
;
454 if (f
->buf_index
>= IO_BUF_SIZE
)
458 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
467 l
= f
->buf_size
- f
->buf_index
;
470 l
= f
->buf_size
- f
->buf_index
;
476 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
484 int qemu_get_byte(QEMUFile
*f
)
489 if (f
->buf_index
>= f
->buf_size
) {
491 if (f
->buf_index
>= f
->buf_size
)
494 return f
->buf
[f
->buf_index
++];
497 int64_t qemu_ftell(QEMUFile
*f
)
499 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
502 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
504 if (whence
== SEEK_SET
) {
506 } else if (whence
== SEEK_CUR
) {
507 pos
+= qemu_ftell(f
);
509 /* SEEK_END not supported */
523 int qemu_file_rate_limit(QEMUFile
*f
)
526 return f
->rate_limit(f
->opaque
);
531 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
533 qemu_put_byte(f
, v
>> 8);
537 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
539 qemu_put_byte(f
, v
>> 24);
540 qemu_put_byte(f
, v
>> 16);
541 qemu_put_byte(f
, v
>> 8);
545 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
547 qemu_put_be32(f
, v
>> 32);
551 unsigned int qemu_get_be16(QEMUFile
*f
)
554 v
= qemu_get_byte(f
) << 8;
555 v
|= qemu_get_byte(f
);
559 unsigned int qemu_get_be32(QEMUFile
*f
)
562 v
= qemu_get_byte(f
) << 24;
563 v
|= qemu_get_byte(f
) << 16;
564 v
|= qemu_get_byte(f
) << 8;
565 v
|= qemu_get_byte(f
);
569 uint64_t qemu_get_be64(QEMUFile
*f
)
572 v
= (uint64_t)qemu_get_be32(f
) << 32;
573 v
|= qemu_get_be32(f
);
577 typedef struct SaveStateEntry
{
582 SaveLiveStateHandler
*save_live_state
;
583 SaveStateHandler
*save_state
;
584 LoadStateHandler
*load_state
;
586 struct SaveStateEntry
*next
;
589 static SaveStateEntry
*first_se
;
591 /* TODO: Individual devices generally have very little idea about the rest
592 of the system, so instance_id should be removed/replaced.
593 Meanwhile pass -1 as instance_id if you do not already have a clearly
594 distinguishing id for all instances of your device class. */
595 int register_savevm_live(const char *idstr
,
598 SaveLiveStateHandler
*save_live_state
,
599 SaveStateHandler
*save_state
,
600 LoadStateHandler
*load_state
,
603 SaveStateEntry
*se
, **pse
;
604 static int global_section_id
;
606 se
= qemu_malloc(sizeof(SaveStateEntry
));
607 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
608 se
->instance_id
= (instance_id
== -1) ? 0 : instance_id
;
609 se
->version_id
= version_id
;
610 se
->section_id
= global_section_id
++;
611 se
->save_live_state
= save_live_state
;
612 se
->save_state
= save_state
;
613 se
->load_state
= load_state
;
617 /* add at the end of list */
619 while (*pse
!= NULL
) {
620 if (instance_id
== -1
621 && strcmp(se
->idstr
, (*pse
)->idstr
) == 0
622 && se
->instance_id
<= (*pse
)->instance_id
)
623 se
->instance_id
= (*pse
)->instance_id
+ 1;
630 int register_savevm(const char *idstr
,
633 SaveStateHandler
*save_state
,
634 LoadStateHandler
*load_state
,
637 return register_savevm_live(idstr
, instance_id
, version_id
,
638 NULL
, save_state
, load_state
, opaque
);
641 #define QEMU_VM_FILE_MAGIC 0x5145564d
642 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
643 #define QEMU_VM_FILE_VERSION 0x00000003
645 #define QEMU_VM_EOF 0x00
646 #define QEMU_VM_SECTION_START 0x01
647 #define QEMU_VM_SECTION_PART 0x02
648 #define QEMU_VM_SECTION_END 0x03
649 #define QEMU_VM_SECTION_FULL 0x04
651 int qemu_savevm_state_begin(QEMUFile
*f
)
655 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
656 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
658 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
661 if (se
->save_live_state
== NULL
)
665 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
666 qemu_put_be32(f
, se
->section_id
);
669 len
= strlen(se
->idstr
);
670 qemu_put_byte(f
, len
);
671 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
673 qemu_put_be32(f
, se
->instance_id
);
674 qemu_put_be32(f
, se
->version_id
);
676 se
->save_live_state(f
, QEMU_VM_SECTION_START
, se
->opaque
);
679 if (qemu_file_has_error(f
))
685 int qemu_savevm_state_iterate(QEMUFile
*f
)
690 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
691 if (se
->save_live_state
== NULL
)
695 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
696 qemu_put_be32(f
, se
->section_id
);
698 ret
&= !!se
->save_live_state(f
, QEMU_VM_SECTION_PART
, se
->opaque
);
704 if (qemu_file_has_error(f
))
710 int qemu_savevm_state_complete(QEMUFile
*f
)
714 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
715 if (se
->save_live_state
== NULL
)
719 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
720 qemu_put_be32(f
, se
->section_id
);
722 se
->save_live_state(f
, QEMU_VM_SECTION_END
, se
->opaque
);
725 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
728 if (se
->save_state
== NULL
)
732 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
733 qemu_put_be32(f
, se
->section_id
);
736 len
= strlen(se
->idstr
);
737 qemu_put_byte(f
, len
);
738 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
740 qemu_put_be32(f
, se
->instance_id
);
741 qemu_put_be32(f
, se
->version_id
);
743 se
->save_state(f
, se
->opaque
);
746 qemu_put_byte(f
, QEMU_VM_EOF
);
748 if (qemu_file_has_error(f
))
754 int qemu_savevm_state(QEMUFile
*f
)
756 int saved_vm_running
;
759 saved_vm_running
= vm_running
;
764 ret
= qemu_savevm_state_begin(f
);
769 ret
= qemu_savevm_state_iterate(f
);
774 ret
= qemu_savevm_state_complete(f
);
777 if (qemu_file_has_error(f
))
780 if (!ret
&& saved_vm_running
)
786 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
790 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
791 if (!strcmp(se
->idstr
, idstr
) &&
792 instance_id
== se
->instance_id
)
798 typedef struct LoadStateEntry
{
802 struct LoadStateEntry
*next
;
805 static int qemu_loadvm_state_v2(QEMUFile
*f
)
808 int len
, ret
, instance_id
, record_len
, version_id
;
809 int64_t total_len
, end_pos
, cur_pos
;
812 total_len
= qemu_get_be64(f
);
813 end_pos
= total_len
+ qemu_ftell(f
);
815 if (qemu_ftell(f
) >= end_pos
)
817 len
= qemu_get_byte(f
);
818 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
820 instance_id
= qemu_get_be32(f
);
821 version_id
= qemu_get_be32(f
);
822 record_len
= qemu_get_be32(f
);
823 cur_pos
= qemu_ftell(f
);
824 se
= find_se(idstr
, instance_id
);
826 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
829 ret
= se
->load_state(f
, se
->opaque
, version_id
);
831 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
835 /* always seek to exact end of record */
836 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
839 if (qemu_file_has_error(f
))
845 int qemu_loadvm_state(QEMUFile
*f
)
847 LoadStateEntry
*first_le
= NULL
;
848 uint8_t section_type
;
852 v
= qemu_get_be32(f
);
853 if (v
!= QEMU_VM_FILE_MAGIC
)
856 v
= qemu_get_be32(f
);
857 if (v
== QEMU_VM_FILE_VERSION_COMPAT
)
858 return qemu_loadvm_state_v2(f
);
859 if (v
!= QEMU_VM_FILE_VERSION
)
862 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
863 uint32_t instance_id
, version_id
, section_id
;
869 switch (section_type
) {
870 case QEMU_VM_SECTION_START
:
871 case QEMU_VM_SECTION_FULL
:
872 /* Read section start */
873 section_id
= qemu_get_be32(f
);
874 len
= qemu_get_byte(f
);
875 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
877 instance_id
= qemu_get_be32(f
);
878 version_id
= qemu_get_be32(f
);
880 /* Find savevm section */
881 se
= find_se(idstr
, instance_id
);
883 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
888 /* Validate version */
889 if (version_id
> se
->version_id
) {
890 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
891 version_id
, idstr
, se
->version_id
);
897 le
= qemu_mallocz(sizeof(*le
));
900 le
->section_id
= section_id
;
901 le
->version_id
= version_id
;
905 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
907 case QEMU_VM_SECTION_PART
:
908 case QEMU_VM_SECTION_END
:
909 section_id
= qemu_get_be32(f
);
911 for (le
= first_le
; le
&& le
->section_id
!= section_id
; le
= le
->next
);
913 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
918 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
921 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
931 LoadStateEntry
*le
= first_le
;
932 first_le
= first_le
->next
;
936 if (qemu_file_has_error(f
))
942 /* device can contain snapshots */
943 static int bdrv_can_snapshot(BlockDriverState
*bs
)
946 !bdrv_is_removable(bs
) &&
947 !bdrv_is_read_only(bs
));
950 /* device must be snapshots in order to have a reliable snapshot */
951 static int bdrv_has_snapshot(BlockDriverState
*bs
)
954 !bdrv_is_removable(bs
) &&
955 !bdrv_is_read_only(bs
));
958 static BlockDriverState
*get_bs_snapshots(void)
960 BlockDriverState
*bs
;
965 for(i
= 0; i
<= nb_drives
; i
++) {
966 bs
= drives_table
[i
].bdrv
;
967 if (bdrv_can_snapshot(bs
))
976 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
979 QEMUSnapshotInfo
*sn_tab
, *sn
;
983 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
986 for(i
= 0; i
< nb_sns
; i
++) {
988 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
998 void do_savevm(const char *name
)
1000 BlockDriverState
*bs
, *bs1
;
1001 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1002 int must_delete
, ret
, i
;
1003 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
1005 int saved_vm_running
;
1006 uint32_t vm_state_size
;
1013 bs
= get_bs_snapshots();
1015 term_printf("No block device can accept snapshots\n");
1019 /* ??? Should this occur after vm_stop? */
1022 saved_vm_running
= vm_running
;
1027 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1032 memset(sn
, 0, sizeof(*sn
));
1034 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1035 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1038 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1041 /* fill auxiliary fields */
1044 sn
->date_sec
= tb
.time
;
1045 sn
->date_nsec
= tb
.millitm
* 1000000;
1047 gettimeofday(&tv
, NULL
);
1048 sn
->date_sec
= tv
.tv_sec
;
1049 sn
->date_nsec
= tv
.tv_usec
* 1000;
1051 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
1053 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
1054 term_printf("Device %s does not support VM state snapshots\n",
1055 bdrv_get_device_name(bs
));
1059 /* save the VM state */
1060 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
1062 term_printf("Could not open VM state file\n");
1065 ret
= qemu_savevm_state(f
);
1066 vm_state_size
= qemu_ftell(f
);
1069 term_printf("Error %d while writing VM\n", ret
);
1073 /* create the snapshots */
1075 for(i
= 0; i
< nb_drives
; i
++) {
1076 bs1
= drives_table
[i
].bdrv
;
1077 if (bdrv_has_snapshot(bs1
)) {
1079 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
1081 term_printf("Error while deleting snapshot on '%s'\n",
1082 bdrv_get_device_name(bs1
));
1085 /* Write VM state size only to the image that contains the state */
1086 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1087 ret
= bdrv_snapshot_create(bs1
, sn
);
1089 term_printf("Error while creating snapshot on '%s'\n",
1090 bdrv_get_device_name(bs1
));
1096 if (saved_vm_running
)
1100 void do_loadvm(const char *name
)
1102 BlockDriverState
*bs
, *bs1
;
1103 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
1104 QEMUSnapshotInfo sn
;
1107 int saved_vm_running
;
1109 bs
= get_bs_snapshots();
1111 term_printf("No block device supports snapshots\n");
1115 /* Flush all IO requests so they don't interfere with the new state. */
1118 saved_vm_running
= vm_running
;
1121 for(i
= 0; i
<= nb_drives
; i
++) {
1122 bs1
= drives_table
[i
].bdrv
;
1123 if (bdrv_has_snapshot(bs1
)) {
1124 ret
= bdrv_snapshot_goto(bs1
, name
);
1127 term_printf("Warning: ");
1130 term_printf("Snapshots not supported on device '%s'\n",
1131 bdrv_get_device_name(bs1
));
1134 term_printf("Could not find snapshot '%s' on device '%s'\n",
1135 name
, bdrv_get_device_name(bs1
));
1138 term_printf("Error %d while activating snapshot on '%s'\n",
1139 ret
, bdrv_get_device_name(bs1
));
1142 /* fatal on snapshot block device */
1149 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
1150 term_printf("Device %s does not support VM state snapshots\n",
1151 bdrv_get_device_name(bs
));
1155 /* Don't even try to load empty VM states */
1156 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1157 if ((ret
>= 0) && (sn
.vm_state_size
== 0))
1160 /* restore the VM state */
1161 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
1163 term_printf("Could not open VM state file\n");
1166 ret
= qemu_loadvm_state(f
);
1169 term_printf("Error %d while loading VM state\n", ret
);
1172 if (saved_vm_running
)
1176 void do_delvm(const char *name
)
1178 BlockDriverState
*bs
, *bs1
;
1181 bs
= get_bs_snapshots();
1183 term_printf("No block device supports snapshots\n");
1187 for(i
= 0; i
<= nb_drives
; i
++) {
1188 bs1
= drives_table
[i
].bdrv
;
1189 if (bdrv_has_snapshot(bs1
)) {
1190 ret
= bdrv_snapshot_delete(bs1
, name
);
1192 if (ret
== -ENOTSUP
)
1193 term_printf("Snapshots not supported on device '%s'\n",
1194 bdrv_get_device_name(bs1
));
1196 term_printf("Error %d while deleting snapshot on '%s'\n",
1197 ret
, bdrv_get_device_name(bs1
));
1203 void do_info_snapshots(void)
1205 BlockDriverState
*bs
, *bs1
;
1206 QEMUSnapshotInfo
*sn_tab
, *sn
;
1210 bs
= get_bs_snapshots();
1212 term_printf("No available block device supports snapshots\n");
1215 term_printf("Snapshot devices:");
1216 for(i
= 0; i
<= nb_drives
; i
++) {
1217 bs1
= drives_table
[i
].bdrv
;
1218 if (bdrv_has_snapshot(bs1
)) {
1220 term_printf(" %s", bdrv_get_device_name(bs1
));
1225 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1227 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
1230 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
1231 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
1232 for(i
= 0; i
< nb_sns
; i
++) {
1234 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));