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
);
230 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
234 popen_file
= popen(command
, mode
);
235 if(popen_file
== NULL
) {
239 return qemu_popen(popen_file
, mode
);
242 int qemu_popen_fd(QEMUFile
*f
)
247 p
= (QEMUFilePopen
*)f
->opaque
;
248 fd
= fileno(p
->popen_file
);
253 QEMUFile
*qemu_fopen_socket(int fd
)
255 QEMUFileSocket
*s
= qemu_mallocz(sizeof(QEMUFileSocket
));
258 s
->file
= qemu_fopen_ops(s
, NULL
, socket_get_buffer
, socket_close
, NULL
);
262 typedef struct QEMUFileStdio
267 static int file_put_buffer(void *opaque
, const uint8_t *buf
,
268 int64_t pos
, int size
)
270 QEMUFileStdio
*s
= opaque
;
271 fseek(s
->outfile
, pos
, SEEK_SET
);
272 fwrite(buf
, 1, size
, s
->outfile
);
276 static int file_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
278 QEMUFileStdio
*s
= opaque
;
279 fseek(s
->outfile
, pos
, SEEK_SET
);
280 return fread(buf
, 1, size
, s
->outfile
);
283 static int file_close(void *opaque
)
285 QEMUFileStdio
*s
= opaque
;
291 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
295 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
297 s
->outfile
= fopen(filename
, mode
);
301 if (!strcmp(mode
, "wb"))
302 return qemu_fopen_ops(s
, file_put_buffer
, NULL
, file_close
, NULL
);
303 else if (!strcmp(mode
, "rb"))
304 return qemu_fopen_ops(s
, NULL
, file_get_buffer
, file_close
, NULL
);
313 typedef struct QEMUFileBdrv
315 BlockDriverState
*bs
;
319 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
320 int64_t pos
, int size
)
322 QEMUFileBdrv
*s
= opaque
;
323 bdrv_put_buffer(s
->bs
, buf
, s
->base_offset
+ pos
, size
);
327 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
329 QEMUFileBdrv
*s
= opaque
;
330 return bdrv_get_buffer(s
->bs
, buf
, s
->base_offset
+ pos
, size
);
333 static int bdrv_fclose(void *opaque
)
335 QEMUFileBdrv
*s
= opaque
;
340 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
344 s
= qemu_mallocz(sizeof(QEMUFileBdrv
));
347 s
->base_offset
= offset
;
350 return qemu_fopen_ops(s
, block_put_buffer
, NULL
, bdrv_fclose
, NULL
);
352 return qemu_fopen_ops(s
, NULL
, block_get_buffer
, bdrv_fclose
, NULL
);
355 QEMUFile
*qemu_fopen_ops(void *opaque
, QEMUFilePutBufferFunc
*put_buffer
,
356 QEMUFileGetBufferFunc
*get_buffer
,
357 QEMUFileCloseFunc
*close
,
358 QEMUFileRateLimit
*rate_limit
)
362 f
= qemu_mallocz(sizeof(QEMUFile
));
365 f
->put_buffer
= put_buffer
;
366 f
->get_buffer
= get_buffer
;
368 f
->rate_limit
= rate_limit
;
374 int qemu_file_has_error(QEMUFile
*f
)
379 void qemu_file_set_error(QEMUFile
*f
)
384 void qemu_fflush(QEMUFile
*f
)
389 if (f
->is_write
&& f
->buf_index
> 0) {
392 len
= f
->put_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, f
->buf_index
);
394 f
->buf_offset
+= f
->buf_index
;
401 static void qemu_fill_buffer(QEMUFile
*f
)
411 len
= f
->get_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, IO_BUF_SIZE
);
415 f
->buf_offset
+= len
;
416 } else if (len
!= -EAGAIN
)
420 int qemu_fclose(QEMUFile
*f
)
425 ret
= f
->close(f
->opaque
);
430 void qemu_file_put_notify(QEMUFile
*f
)
432 f
->put_buffer(f
->opaque
, NULL
, 0, 0);
435 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
439 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
441 "Attempted to write to buffer while read buffer is not empty\n");
445 while (!f
->has_error
&& size
> 0) {
446 l
= IO_BUF_SIZE
- f
->buf_index
;
449 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
454 if (f
->buf_index
>= IO_BUF_SIZE
)
459 void qemu_put_byte(QEMUFile
*f
, int v
)
461 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
463 "Attempted to write to buffer while read buffer is not empty\n");
467 f
->buf
[f
->buf_index
++] = v
;
469 if (f
->buf_index
>= IO_BUF_SIZE
)
473 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
482 l
= f
->buf_size
- f
->buf_index
;
485 l
= f
->buf_size
- f
->buf_index
;
491 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
499 int qemu_get_byte(QEMUFile
*f
)
504 if (f
->buf_index
>= f
->buf_size
) {
506 if (f
->buf_index
>= f
->buf_size
)
509 return f
->buf
[f
->buf_index
++];
512 int64_t qemu_ftell(QEMUFile
*f
)
514 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
517 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
519 if (whence
== SEEK_SET
) {
521 } else if (whence
== SEEK_CUR
) {
522 pos
+= qemu_ftell(f
);
524 /* SEEK_END not supported */
538 int qemu_file_rate_limit(QEMUFile
*f
)
541 return f
->rate_limit(f
->opaque
);
546 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
548 qemu_put_byte(f
, v
>> 8);
552 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
554 qemu_put_byte(f
, v
>> 24);
555 qemu_put_byte(f
, v
>> 16);
556 qemu_put_byte(f
, v
>> 8);
560 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
562 qemu_put_be32(f
, v
>> 32);
566 unsigned int qemu_get_be16(QEMUFile
*f
)
569 v
= qemu_get_byte(f
) << 8;
570 v
|= qemu_get_byte(f
);
574 unsigned int qemu_get_be32(QEMUFile
*f
)
577 v
= qemu_get_byte(f
) << 24;
578 v
|= qemu_get_byte(f
) << 16;
579 v
|= qemu_get_byte(f
) << 8;
580 v
|= qemu_get_byte(f
);
584 uint64_t qemu_get_be64(QEMUFile
*f
)
587 v
= (uint64_t)qemu_get_be32(f
) << 32;
588 v
|= qemu_get_be32(f
);
592 typedef struct SaveStateEntry
{
597 SaveLiveStateHandler
*save_live_state
;
598 SaveStateHandler
*save_state
;
599 LoadStateHandler
*load_state
;
601 struct SaveStateEntry
*next
;
604 static SaveStateEntry
*first_se
;
606 /* TODO: Individual devices generally have very little idea about the rest
607 of the system, so instance_id should be removed/replaced.
608 Meanwhile pass -1 as instance_id if you do not already have a clearly
609 distinguishing id for all instances of your device class. */
610 int register_savevm_live(const char *idstr
,
613 SaveLiveStateHandler
*save_live_state
,
614 SaveStateHandler
*save_state
,
615 LoadStateHandler
*load_state
,
618 SaveStateEntry
*se
, **pse
;
619 static int global_section_id
;
621 se
= qemu_malloc(sizeof(SaveStateEntry
));
622 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
623 se
->instance_id
= (instance_id
== -1) ? 0 : instance_id
;
624 se
->version_id
= version_id
;
625 se
->section_id
= global_section_id
++;
626 se
->save_live_state
= save_live_state
;
627 se
->save_state
= save_state
;
628 se
->load_state
= load_state
;
632 /* add at the end of list */
634 while (*pse
!= NULL
) {
635 if (instance_id
== -1
636 && strcmp(se
->idstr
, (*pse
)->idstr
) == 0
637 && se
->instance_id
<= (*pse
)->instance_id
)
638 se
->instance_id
= (*pse
)->instance_id
+ 1;
645 int register_savevm(const char *idstr
,
648 SaveStateHandler
*save_state
,
649 LoadStateHandler
*load_state
,
652 return register_savevm_live(idstr
, instance_id
, version_id
,
653 NULL
, save_state
, load_state
, opaque
);
656 void unregister_savevm(const char *idstr
, void *opaque
)
658 SaveStateEntry
**pse
;
661 while (*pse
!= NULL
) {
662 if (strcmp((*pse
)->idstr
, idstr
) == 0 && (*pse
)->opaque
== opaque
) {
663 SaveStateEntry
*next
= (*pse
)->next
;
672 #define QEMU_VM_FILE_MAGIC 0x5145564d
673 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
674 #define QEMU_VM_FILE_VERSION 0x00000003
676 #define QEMU_VM_EOF 0x00
677 #define QEMU_VM_SECTION_START 0x01
678 #define QEMU_VM_SECTION_PART 0x02
679 #define QEMU_VM_SECTION_END 0x03
680 #define QEMU_VM_SECTION_FULL 0x04
682 int qemu_savevm_state_begin(QEMUFile
*f
)
686 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
687 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
689 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
692 if (se
->save_live_state
== NULL
)
696 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
697 qemu_put_be32(f
, se
->section_id
);
700 len
= strlen(se
->idstr
);
701 qemu_put_byte(f
, len
);
702 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
704 qemu_put_be32(f
, se
->instance_id
);
705 qemu_put_be32(f
, se
->version_id
);
707 se
->save_live_state(f
, QEMU_VM_SECTION_START
, se
->opaque
);
710 if (qemu_file_has_error(f
))
716 int qemu_savevm_state_iterate(QEMUFile
*f
)
721 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
722 if (se
->save_live_state
== NULL
)
726 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
727 qemu_put_be32(f
, se
->section_id
);
729 ret
&= !!se
->save_live_state(f
, QEMU_VM_SECTION_PART
, se
->opaque
);
735 if (qemu_file_has_error(f
))
741 int qemu_savevm_state_complete(QEMUFile
*f
)
745 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
746 if (se
->save_live_state
== NULL
)
750 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
751 qemu_put_be32(f
, se
->section_id
);
753 se
->save_live_state(f
, QEMU_VM_SECTION_END
, se
->opaque
);
756 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
759 if (se
->save_state
== NULL
)
763 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
764 qemu_put_be32(f
, se
->section_id
);
767 len
= strlen(se
->idstr
);
768 qemu_put_byte(f
, len
);
769 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
771 qemu_put_be32(f
, se
->instance_id
);
772 qemu_put_be32(f
, se
->version_id
);
774 se
->save_state(f
, se
->opaque
);
777 qemu_put_byte(f
, QEMU_VM_EOF
);
779 if (qemu_file_has_error(f
))
785 int qemu_savevm_state(QEMUFile
*f
)
787 int saved_vm_running
;
790 saved_vm_running
= vm_running
;
795 ret
= qemu_savevm_state_begin(f
);
800 ret
= qemu_savevm_state_iterate(f
);
805 ret
= qemu_savevm_state_complete(f
);
808 if (qemu_file_has_error(f
))
811 if (!ret
&& saved_vm_running
)
817 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
821 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
822 if (!strcmp(se
->idstr
, idstr
) &&
823 instance_id
== se
->instance_id
)
829 typedef struct LoadStateEntry
{
833 struct LoadStateEntry
*next
;
836 static int qemu_loadvm_state_v2(QEMUFile
*f
)
839 int len
, ret
, instance_id
, record_len
, version_id
;
840 int64_t total_len
, end_pos
, cur_pos
;
843 total_len
= qemu_get_be64(f
);
844 end_pos
= total_len
+ qemu_ftell(f
);
846 if (qemu_ftell(f
) >= end_pos
)
848 len
= qemu_get_byte(f
);
849 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
851 instance_id
= qemu_get_be32(f
);
852 version_id
= qemu_get_be32(f
);
853 record_len
= qemu_get_be32(f
);
854 cur_pos
= qemu_ftell(f
);
855 se
= find_se(idstr
, instance_id
);
857 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
860 ret
= se
->load_state(f
, se
->opaque
, version_id
);
862 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
867 /* always seek to exact end of record */
868 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
871 if (qemu_file_has_error(f
))
877 int qemu_loadvm_state(QEMUFile
*f
)
879 LoadStateEntry
*first_le
= NULL
;
880 uint8_t section_type
;
884 v
= qemu_get_be32(f
);
885 if (v
!= QEMU_VM_FILE_MAGIC
)
888 v
= qemu_get_be32(f
);
889 if (v
== QEMU_VM_FILE_VERSION_COMPAT
)
890 return qemu_loadvm_state_v2(f
);
891 if (v
!= QEMU_VM_FILE_VERSION
)
894 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
895 uint32_t instance_id
, version_id
, section_id
;
901 switch (section_type
) {
902 case QEMU_VM_SECTION_START
:
903 case QEMU_VM_SECTION_FULL
:
904 /* Read section start */
905 section_id
= qemu_get_be32(f
);
906 len
= qemu_get_byte(f
);
907 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
909 instance_id
= qemu_get_be32(f
);
910 version_id
= qemu_get_be32(f
);
912 /* Find savevm section */
913 se
= find_se(idstr
, instance_id
);
915 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
920 /* Validate version */
921 if (version_id
> se
->version_id
) {
922 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
923 version_id
, idstr
, se
->version_id
);
929 le
= qemu_mallocz(sizeof(*le
));
932 le
->section_id
= section_id
;
933 le
->version_id
= version_id
;
937 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
939 case QEMU_VM_SECTION_PART
:
940 case QEMU_VM_SECTION_END
:
941 section_id
= qemu_get_be32(f
);
943 for (le
= first_le
; le
&& le
->section_id
!= section_id
; le
= le
->next
);
945 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
950 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
953 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
963 LoadStateEntry
*le
= first_le
;
964 first_le
= first_le
->next
;
968 if (qemu_file_has_error(f
))
974 /* device can contain snapshots */
975 static int bdrv_can_snapshot(BlockDriverState
*bs
)
978 !bdrv_is_removable(bs
) &&
979 !bdrv_is_read_only(bs
));
982 /* device must be snapshots in order to have a reliable snapshot */
983 static int bdrv_has_snapshot(BlockDriverState
*bs
)
986 !bdrv_is_removable(bs
) &&
987 !bdrv_is_read_only(bs
));
990 static BlockDriverState
*get_bs_snapshots(void)
992 BlockDriverState
*bs
;
997 for(i
= 0; i
<= nb_drives
; i
++) {
998 bs
= drives_table
[i
].bdrv
;
999 if (bdrv_can_snapshot(bs
))
1008 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
1011 QEMUSnapshotInfo
*sn_tab
, *sn
;
1015 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1018 for(i
= 0; i
< nb_sns
; i
++) {
1020 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
1030 void do_savevm(const char *name
)
1032 BlockDriverState
*bs
, *bs1
;
1033 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1034 int must_delete
, ret
, i
;
1035 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
1037 int saved_vm_running
;
1038 uint32_t vm_state_size
;
1045 bs
= get_bs_snapshots();
1047 term_printf("No block device can accept snapshots\n");
1051 /* ??? Should this occur after vm_stop? */
1054 saved_vm_running
= vm_running
;
1059 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1064 memset(sn
, 0, sizeof(*sn
));
1066 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1067 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1070 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1073 /* fill auxiliary fields */
1076 sn
->date_sec
= tb
.time
;
1077 sn
->date_nsec
= tb
.millitm
* 1000000;
1079 gettimeofday(&tv
, NULL
);
1080 sn
->date_sec
= tv
.tv_sec
;
1081 sn
->date_nsec
= tv
.tv_usec
* 1000;
1083 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
1085 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
1086 term_printf("Device %s does not support VM state snapshots\n",
1087 bdrv_get_device_name(bs
));
1091 /* save the VM state */
1092 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
1094 term_printf("Could not open VM state file\n");
1097 ret
= qemu_savevm_state(f
);
1098 vm_state_size
= qemu_ftell(f
);
1101 term_printf("Error %d while writing VM\n", ret
);
1105 /* create the snapshots */
1107 for(i
= 0; i
< nb_drives
; i
++) {
1108 bs1
= drives_table
[i
].bdrv
;
1109 if (bdrv_has_snapshot(bs1
)) {
1111 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
1113 term_printf("Error while deleting snapshot on '%s'\n",
1114 bdrv_get_device_name(bs1
));
1117 /* Write VM state size only to the image that contains the state */
1118 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1119 ret
= bdrv_snapshot_create(bs1
, sn
);
1121 term_printf("Error while creating snapshot on '%s'\n",
1122 bdrv_get_device_name(bs1
));
1128 if (saved_vm_running
)
1132 void do_loadvm(const char *name
)
1134 BlockDriverState
*bs
, *bs1
;
1135 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
1136 QEMUSnapshotInfo sn
;
1139 int saved_vm_running
;
1141 bs
= get_bs_snapshots();
1143 term_printf("No block device supports snapshots\n");
1147 /* Flush all IO requests so they don't interfere with the new state. */
1150 saved_vm_running
= vm_running
;
1153 for(i
= 0; i
<= nb_drives
; i
++) {
1154 bs1
= drives_table
[i
].bdrv
;
1155 if (bdrv_has_snapshot(bs1
)) {
1156 ret
= bdrv_snapshot_goto(bs1
, name
);
1159 term_printf("Warning: ");
1162 term_printf("Snapshots not supported on device '%s'\n",
1163 bdrv_get_device_name(bs1
));
1166 term_printf("Could not find snapshot '%s' on device '%s'\n",
1167 name
, bdrv_get_device_name(bs1
));
1170 term_printf("Error %d while activating snapshot on '%s'\n",
1171 ret
, bdrv_get_device_name(bs1
));
1174 /* fatal on snapshot block device */
1181 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
1182 term_printf("Device %s does not support VM state snapshots\n",
1183 bdrv_get_device_name(bs
));
1187 /* Don't even try to load empty VM states */
1188 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1189 if ((ret
>= 0) && (sn
.vm_state_size
== 0))
1192 /* restore the VM state */
1193 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
1195 term_printf("Could not open VM state file\n");
1198 ret
= qemu_loadvm_state(f
);
1201 term_printf("Error %d while loading VM state\n", ret
);
1204 if (saved_vm_running
)
1208 void do_delvm(const char *name
)
1210 BlockDriverState
*bs
, *bs1
;
1213 bs
= get_bs_snapshots();
1215 term_printf("No block device supports snapshots\n");
1219 for(i
= 0; i
<= nb_drives
; i
++) {
1220 bs1
= drives_table
[i
].bdrv
;
1221 if (bdrv_has_snapshot(bs1
)) {
1222 ret
= bdrv_snapshot_delete(bs1
, name
);
1224 if (ret
== -ENOTSUP
)
1225 term_printf("Snapshots not supported on device '%s'\n",
1226 bdrv_get_device_name(bs1
));
1228 term_printf("Error %d while deleting snapshot on '%s'\n",
1229 ret
, bdrv_get_device_name(bs1
));
1235 void do_info_snapshots(void)
1237 BlockDriverState
*bs
, *bs1
;
1238 QEMUSnapshotInfo
*sn_tab
, *sn
;
1242 bs
= get_bs_snapshots();
1244 term_printf("No available block device supports snapshots\n");
1247 term_printf("Snapshot devices:");
1248 for(i
= 0; i
<= nb_drives
; i
++) {
1249 bs1
= drives_table
[i
].bdrv
;
1250 if (bdrv_has_snapshot(bs1
)) {
1252 term_printf(" %s", bdrv_get_device_name(bs1
));
1257 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1259 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
1262 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
1263 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
1264 for(i
= 0; i
< nb_sns
; i
++) {
1266 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));