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 block_put_buffer(void *opaque
, const uint8_t *buf
,
310 int64_t pos
, int size
)
312 QEMUFileBdrv
*s
= opaque
;
313 bdrv_put_buffer(s
->bs
, buf
, s
->base_offset
+ pos
, size
);
317 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
319 QEMUFileBdrv
*s
= opaque
;
320 return bdrv_get_buffer(s
->bs
, buf
, s
->base_offset
+ pos
, 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
, block_put_buffer
, NULL
, bdrv_fclose
, NULL
);
342 return qemu_fopen_ops(s
, NULL
, block_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_file_set_error(QEMUFile
*f
)
374 void qemu_fflush(QEMUFile
*f
)
379 if (f
->is_write
&& f
->buf_index
> 0) {
382 len
= f
->put_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, f
->buf_index
);
384 f
->buf_offset
+= f
->buf_index
;
391 static void qemu_fill_buffer(QEMUFile
*f
)
401 len
= f
->get_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, IO_BUF_SIZE
);
405 f
->buf_offset
+= len
;
406 } else if (len
!= -EAGAIN
)
410 int qemu_fclose(QEMUFile
*f
)
415 ret
= f
->close(f
->opaque
);
420 void qemu_file_put_notify(QEMUFile
*f
)
422 f
->put_buffer(f
->opaque
, NULL
, 0, 0);
425 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
429 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
431 "Attempted to write to buffer while read buffer is not empty\n");
435 while (!f
->has_error
&& size
> 0) {
436 l
= IO_BUF_SIZE
- f
->buf_index
;
439 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
444 if (f
->buf_index
>= IO_BUF_SIZE
)
449 void qemu_put_byte(QEMUFile
*f
, int v
)
451 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
453 "Attempted to write to buffer while read buffer is not empty\n");
457 f
->buf
[f
->buf_index
++] = v
;
459 if (f
->buf_index
>= IO_BUF_SIZE
)
463 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
472 l
= f
->buf_size
- f
->buf_index
;
475 l
= f
->buf_size
- f
->buf_index
;
481 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
489 int qemu_get_byte(QEMUFile
*f
)
494 if (f
->buf_index
>= f
->buf_size
) {
496 if (f
->buf_index
>= f
->buf_size
)
499 return f
->buf
[f
->buf_index
++];
502 int64_t qemu_ftell(QEMUFile
*f
)
504 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
507 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
509 if (whence
== SEEK_SET
) {
511 } else if (whence
== SEEK_CUR
) {
512 pos
+= qemu_ftell(f
);
514 /* SEEK_END not supported */
528 int qemu_file_rate_limit(QEMUFile
*f
)
531 return f
->rate_limit(f
->opaque
);
536 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
538 qemu_put_byte(f
, v
>> 8);
542 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
544 qemu_put_byte(f
, v
>> 24);
545 qemu_put_byte(f
, v
>> 16);
546 qemu_put_byte(f
, v
>> 8);
550 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
552 qemu_put_be32(f
, v
>> 32);
556 unsigned int qemu_get_be16(QEMUFile
*f
)
559 v
= qemu_get_byte(f
) << 8;
560 v
|= qemu_get_byte(f
);
564 unsigned int qemu_get_be32(QEMUFile
*f
)
567 v
= qemu_get_byte(f
) << 24;
568 v
|= qemu_get_byte(f
) << 16;
569 v
|= qemu_get_byte(f
) << 8;
570 v
|= qemu_get_byte(f
);
574 uint64_t qemu_get_be64(QEMUFile
*f
)
577 v
= (uint64_t)qemu_get_be32(f
) << 32;
578 v
|= qemu_get_be32(f
);
582 typedef struct SaveStateEntry
{
587 SaveLiveStateHandler
*save_live_state
;
588 SaveStateHandler
*save_state
;
589 LoadStateHandler
*load_state
;
591 struct SaveStateEntry
*next
;
594 static SaveStateEntry
*first_se
;
596 /* TODO: Individual devices generally have very little idea about the rest
597 of the system, so instance_id should be removed/replaced.
598 Meanwhile pass -1 as instance_id if you do not already have a clearly
599 distinguishing id for all instances of your device class. */
600 int register_savevm_live(const char *idstr
,
603 SaveLiveStateHandler
*save_live_state
,
604 SaveStateHandler
*save_state
,
605 LoadStateHandler
*load_state
,
608 SaveStateEntry
*se
, **pse
;
609 static int global_section_id
;
611 se
= qemu_malloc(sizeof(SaveStateEntry
));
612 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
613 se
->instance_id
= (instance_id
== -1) ? 0 : instance_id
;
614 se
->version_id
= version_id
;
615 se
->section_id
= global_section_id
++;
616 se
->save_live_state
= save_live_state
;
617 se
->save_state
= save_state
;
618 se
->load_state
= load_state
;
622 /* add at the end of list */
624 while (*pse
!= NULL
) {
625 if (instance_id
== -1
626 && strcmp(se
->idstr
, (*pse
)->idstr
) == 0
627 && se
->instance_id
<= (*pse
)->instance_id
)
628 se
->instance_id
= (*pse
)->instance_id
+ 1;
635 int register_savevm(const char *idstr
,
638 SaveStateHandler
*save_state
,
639 LoadStateHandler
*load_state
,
642 return register_savevm_live(idstr
, instance_id
, version_id
,
643 NULL
, save_state
, load_state
, opaque
);
646 void unregister_savevm(const char *idstr
, void *opaque
)
648 SaveStateEntry
**pse
;
651 while (*pse
!= NULL
) {
652 if (strcmp((*pse
)->idstr
, idstr
) == 0 && (*pse
)->opaque
== opaque
) {
653 SaveStateEntry
*next
= (*pse
)->next
;
662 #define QEMU_VM_FILE_MAGIC 0x5145564d
663 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
664 #define QEMU_VM_FILE_VERSION 0x00000003
666 #define QEMU_VM_EOF 0x00
667 #define QEMU_VM_SECTION_START 0x01
668 #define QEMU_VM_SECTION_PART 0x02
669 #define QEMU_VM_SECTION_END 0x03
670 #define QEMU_VM_SECTION_FULL 0x04
672 int qemu_savevm_state_begin(QEMUFile
*f
)
676 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
677 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
679 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
682 if (se
->save_live_state
== NULL
)
686 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
687 qemu_put_be32(f
, se
->section_id
);
690 len
= strlen(se
->idstr
);
691 qemu_put_byte(f
, len
);
692 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
694 qemu_put_be32(f
, se
->instance_id
);
695 qemu_put_be32(f
, se
->version_id
);
697 se
->save_live_state(f
, QEMU_VM_SECTION_START
, se
->opaque
);
700 if (qemu_file_has_error(f
))
706 int qemu_savevm_state_iterate(QEMUFile
*f
)
711 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
712 if (se
->save_live_state
== NULL
)
716 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
717 qemu_put_be32(f
, se
->section_id
);
719 ret
&= !!se
->save_live_state(f
, QEMU_VM_SECTION_PART
, se
->opaque
);
725 if (qemu_file_has_error(f
))
731 int qemu_savevm_state_complete(QEMUFile
*f
)
735 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
736 if (se
->save_live_state
== NULL
)
740 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
741 qemu_put_be32(f
, se
->section_id
);
743 se
->save_live_state(f
, QEMU_VM_SECTION_END
, se
->opaque
);
746 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
749 if (se
->save_state
== NULL
)
753 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
754 qemu_put_be32(f
, se
->section_id
);
757 len
= strlen(se
->idstr
);
758 qemu_put_byte(f
, len
);
759 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
761 qemu_put_be32(f
, se
->instance_id
);
762 qemu_put_be32(f
, se
->version_id
);
764 se
->save_state(f
, se
->opaque
);
767 qemu_put_byte(f
, QEMU_VM_EOF
);
769 if (qemu_file_has_error(f
))
775 int qemu_savevm_state(QEMUFile
*f
)
777 int saved_vm_running
;
780 saved_vm_running
= vm_running
;
785 ret
= qemu_savevm_state_begin(f
);
790 ret
= qemu_savevm_state_iterate(f
);
795 ret
= qemu_savevm_state_complete(f
);
798 if (qemu_file_has_error(f
))
801 if (!ret
&& saved_vm_running
)
807 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
811 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
812 if (!strcmp(se
->idstr
, idstr
) &&
813 instance_id
== se
->instance_id
)
819 typedef struct LoadStateEntry
{
823 struct LoadStateEntry
*next
;
826 static int qemu_loadvm_state_v2(QEMUFile
*f
)
829 int len
, ret
, instance_id
, record_len
, version_id
;
830 int64_t total_len
, end_pos
, cur_pos
;
833 total_len
= qemu_get_be64(f
);
834 end_pos
= total_len
+ qemu_ftell(f
);
836 if (qemu_ftell(f
) >= end_pos
)
838 len
= qemu_get_byte(f
);
839 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
841 instance_id
= qemu_get_be32(f
);
842 version_id
= qemu_get_be32(f
);
843 record_len
= qemu_get_be32(f
);
844 cur_pos
= qemu_ftell(f
);
845 se
= find_se(idstr
, instance_id
);
847 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
850 ret
= se
->load_state(f
, se
->opaque
, version_id
);
852 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
856 /* always seek to exact end of record */
857 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
860 if (qemu_file_has_error(f
))
866 int qemu_loadvm_state(QEMUFile
*f
)
868 LoadStateEntry
*first_le
= NULL
;
869 uint8_t section_type
;
873 v
= qemu_get_be32(f
);
874 if (v
!= QEMU_VM_FILE_MAGIC
)
877 v
= qemu_get_be32(f
);
878 if (v
== QEMU_VM_FILE_VERSION_COMPAT
)
879 return qemu_loadvm_state_v2(f
);
880 if (v
!= QEMU_VM_FILE_VERSION
)
883 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
884 uint32_t instance_id
, version_id
, section_id
;
890 switch (section_type
) {
891 case QEMU_VM_SECTION_START
:
892 case QEMU_VM_SECTION_FULL
:
893 /* Read section start */
894 section_id
= qemu_get_be32(f
);
895 len
= qemu_get_byte(f
);
896 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
898 instance_id
= qemu_get_be32(f
);
899 version_id
= qemu_get_be32(f
);
901 /* Find savevm section */
902 se
= find_se(idstr
, instance_id
);
904 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
909 /* Validate version */
910 if (version_id
> se
->version_id
) {
911 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
912 version_id
, idstr
, se
->version_id
);
918 le
= qemu_mallocz(sizeof(*le
));
921 le
->section_id
= section_id
;
922 le
->version_id
= version_id
;
926 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
928 case QEMU_VM_SECTION_PART
:
929 case QEMU_VM_SECTION_END
:
930 section_id
= qemu_get_be32(f
);
932 for (le
= first_le
; le
&& le
->section_id
!= section_id
; le
= le
->next
);
934 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
939 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
942 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
952 LoadStateEntry
*le
= first_le
;
953 first_le
= first_le
->next
;
957 if (qemu_file_has_error(f
))
963 /* device can contain snapshots */
964 static int bdrv_can_snapshot(BlockDriverState
*bs
)
967 !bdrv_is_removable(bs
) &&
968 !bdrv_is_read_only(bs
));
971 /* device must be snapshots in order to have a reliable snapshot */
972 static int bdrv_has_snapshot(BlockDriverState
*bs
)
975 !bdrv_is_removable(bs
) &&
976 !bdrv_is_read_only(bs
));
979 static BlockDriverState
*get_bs_snapshots(void)
981 BlockDriverState
*bs
;
986 for(i
= 0; i
<= nb_drives
; i
++) {
987 bs
= drives_table
[i
].bdrv
;
988 if (bdrv_can_snapshot(bs
))
997 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
1000 QEMUSnapshotInfo
*sn_tab
, *sn
;
1004 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1007 for(i
= 0; i
< nb_sns
; i
++) {
1009 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
1019 void do_savevm(const char *name
)
1021 BlockDriverState
*bs
, *bs1
;
1022 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1023 int must_delete
, ret
, i
;
1024 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
1026 int saved_vm_running
;
1027 uint32_t vm_state_size
;
1034 bs
= get_bs_snapshots();
1036 term_printf("No block device can accept snapshots\n");
1040 /* ??? Should this occur after vm_stop? */
1043 saved_vm_running
= vm_running
;
1048 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1053 memset(sn
, 0, sizeof(*sn
));
1055 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1056 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1059 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1062 /* fill auxiliary fields */
1065 sn
->date_sec
= tb
.time
;
1066 sn
->date_nsec
= tb
.millitm
* 1000000;
1068 gettimeofday(&tv
, NULL
);
1069 sn
->date_sec
= tv
.tv_sec
;
1070 sn
->date_nsec
= tv
.tv_usec
* 1000;
1072 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
1074 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
1075 term_printf("Device %s does not support VM state snapshots\n",
1076 bdrv_get_device_name(bs
));
1080 /* save the VM state */
1081 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
1083 term_printf("Could not open VM state file\n");
1086 ret
= qemu_savevm_state(f
);
1087 vm_state_size
= qemu_ftell(f
);
1090 term_printf("Error %d while writing VM\n", ret
);
1094 /* create the snapshots */
1096 for(i
= 0; i
< nb_drives
; i
++) {
1097 bs1
= drives_table
[i
].bdrv
;
1098 if (bdrv_has_snapshot(bs1
)) {
1100 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
1102 term_printf("Error while deleting snapshot on '%s'\n",
1103 bdrv_get_device_name(bs1
));
1106 /* Write VM state size only to the image that contains the state */
1107 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1108 ret
= bdrv_snapshot_create(bs1
, sn
);
1110 term_printf("Error while creating snapshot on '%s'\n",
1111 bdrv_get_device_name(bs1
));
1117 if (saved_vm_running
)
1121 void do_loadvm(const char *name
)
1123 BlockDriverState
*bs
, *bs1
;
1124 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
1125 QEMUSnapshotInfo sn
;
1128 int saved_vm_running
;
1130 bs
= get_bs_snapshots();
1132 term_printf("No block device supports snapshots\n");
1136 /* Flush all IO requests so they don't interfere with the new state. */
1139 saved_vm_running
= vm_running
;
1142 for(i
= 0; i
<= nb_drives
; i
++) {
1143 bs1
= drives_table
[i
].bdrv
;
1144 if (bdrv_has_snapshot(bs1
)) {
1145 ret
= bdrv_snapshot_goto(bs1
, name
);
1148 term_printf("Warning: ");
1151 term_printf("Snapshots not supported on device '%s'\n",
1152 bdrv_get_device_name(bs1
));
1155 term_printf("Could not find snapshot '%s' on device '%s'\n",
1156 name
, bdrv_get_device_name(bs1
));
1159 term_printf("Error %d while activating snapshot on '%s'\n",
1160 ret
, bdrv_get_device_name(bs1
));
1163 /* fatal on snapshot block device */
1170 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
1171 term_printf("Device %s does not support VM state snapshots\n",
1172 bdrv_get_device_name(bs
));
1176 /* Don't even try to load empty VM states */
1177 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1178 if ((ret
>= 0) && (sn
.vm_state_size
== 0))
1181 /* restore the VM state */
1182 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
1184 term_printf("Could not open VM state file\n");
1187 ret
= qemu_loadvm_state(f
);
1190 term_printf("Error %d while loading VM state\n", ret
);
1193 if (saved_vm_running
)
1197 void do_delvm(const char *name
)
1199 BlockDriverState
*bs
, *bs1
;
1202 bs
= get_bs_snapshots();
1204 term_printf("No block device supports snapshots\n");
1208 for(i
= 0; i
<= nb_drives
; i
++) {
1209 bs1
= drives_table
[i
].bdrv
;
1210 if (bdrv_has_snapshot(bs1
)) {
1211 ret
= bdrv_snapshot_delete(bs1
, name
);
1213 if (ret
== -ENOTSUP
)
1214 term_printf("Snapshots not supported on device '%s'\n",
1215 bdrv_get_device_name(bs1
));
1217 term_printf("Error %d while deleting snapshot on '%s'\n",
1218 ret
, bdrv_get_device_name(bs1
));
1224 void do_info_snapshots(void)
1226 BlockDriverState
*bs
, *bs1
;
1227 QEMUSnapshotInfo
*sn_tab
, *sn
;
1231 bs
= get_bs_snapshots();
1233 term_printf("No available block device supports snapshots\n");
1236 term_printf("Snapshot devices:");
1237 for(i
= 0; i
<= nb_drives
; i
++) {
1238 bs1
= drives_table
[i
].bdrv
;
1239 if (bdrv_has_snapshot(bs1
)) {
1241 term_printf(" %s", bdrv_get_device_name(bs1
));
1246 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1248 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
1251 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
1252 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
1253 for(i
= 0; i
< nb_sns
; i
++) {
1255 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));