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
32 /* Needed early for HOST_BSD etc. */
33 #include "config-host.h"
36 #include <sys/times.h>
40 #include <sys/ioctl.h>
41 #include <sys/resource.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
45 #if defined(__NetBSD__)
46 #include <net/if_tap.h>
49 #include <linux/if_tun.h>
51 #include <arpa/inet.h>
54 #include <sys/select.h>
57 #if defined(__FreeBSD__) || defined(__DragonFly__)
62 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
63 #include <freebsd/stdlib.h>
68 #include <linux/rtc.h>
76 #include <sys/timeb.h>
78 #define getopt_long_only getopt_long
79 #define memalign(align, size) malloc(size)
82 #include "qemu-common.h"
87 #include "qemu-timer.h"
88 #include "qemu-char.h"
90 #include "audio/audio.h"
91 #include "migration.h"
92 #include "qemu_socket.h"
94 /* point to the block driver where the snapshots are managed */
95 static BlockDriverState
*bs_snapshots
;
97 #define SELF_ANNOUNCE_ROUNDS 5
98 #define ETH_P_EXPERIMENTAL 0x01F1 /* just a number */
99 //#define ETH_P_EXPERIMENTAL 0x0012 /* make it the size of the packet */
100 #define EXPERIMENTAL_MAGIC 0xf1f23f4f
102 static int announce_self_create(uint8_t *buf
,
105 uint32_t magic
= EXPERIMENTAL_MAGIC
;
106 uint16_t proto
= htons(ETH_P_EXPERIMENTAL
);
108 /* FIXME: should we send a different packet (arp/rarp/ping)? */
110 memset(buf
, 0xff, 6); /* h_dst */
111 memcpy(buf
+ 6, mac_addr
, 6); /* h_src */
112 memcpy(buf
+ 12, &proto
, 2); /* h_proto */
113 memcpy(buf
+ 14, &magic
, 4); /* magic */
118 void qemu_announce_self(void)
125 for (i
= 0; i
< MAX_NICS
; i
++) {
126 if (!nd_table
[i
].used
)
128 len
= announce_self_create(buf
, nd_table
[i
].macaddr
);
129 vlan
= nd_table
[i
].vlan
;
130 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
131 for (j
=0; j
< SELF_ANNOUNCE_ROUNDS
; j
++)
132 vc
->fd_read(vc
->opaque
, buf
, len
);
137 /***********************************************************/
138 /* savevm/loadvm support */
140 #define IO_BUF_SIZE 32768
143 QEMUFilePutBufferFunc
*put_buffer
;
144 QEMUFileGetBufferFunc
*get_buffer
;
145 QEMUFileCloseFunc
*close
;
146 QEMUFileRateLimit
*rate_limit
;
150 int64_t buf_offset
; /* start of buffer when writing, end of buffer
153 int buf_size
; /* 0 when writing */
154 uint8_t buf
[IO_BUF_SIZE
];
159 typedef struct QEMUFilePopen
165 typedef struct QEMUFileSocket
171 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
173 QEMUFileSocket
*s
= opaque
;
177 len
= recv(s
->fd
, buf
, size
, 0);
178 } while (len
== -1 && socket_error() == EINTR
);
181 len
= -socket_error();
186 static int socket_close(void *opaque
)
188 QEMUFileSocket
*s
= opaque
;
193 static int popen_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
195 QEMUFilePopen
*s
= opaque
;
196 return fwrite(buf
, 1, size
, s
->popen_file
);
199 static int popen_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
201 QEMUFilePopen
*s
= opaque
;
202 return fread(buf
, 1, size
, s
->popen_file
);
205 static int popen_close(void *opaque
)
207 QEMUFilePopen
*s
= opaque
;
208 pclose(s
->popen_file
);
213 QEMUFile
*qemu_popen(FILE *popen_file
, const char *mode
)
217 if (popen_file
== NULL
|| mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
218 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
222 s
= qemu_mallocz(sizeof(QEMUFilePopen
));
224 s
->popen_file
= popen_file
;
227 s
->file
= qemu_fopen_ops(s
, NULL
, popen_get_buffer
, popen_close
, NULL
);
229 s
->file
= qemu_fopen_ops(s
, popen_put_buffer
, NULL
, popen_close
, NULL
);
231 fprintf(stderr
, "qemu_popen: returning result of qemu_fopen_ops\n");
235 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
239 popen_file
= popen(command
, mode
);
240 if(popen_file
== NULL
) {
244 return qemu_popen(popen_file
, mode
);
247 QEMUFile
*qemu_fopen_socket(int fd
)
249 QEMUFileSocket
*s
= qemu_mallocz(sizeof(QEMUFileSocket
));
252 s
->file
= qemu_fopen_ops(s
, NULL
, socket_get_buffer
, socket_close
, NULL
);
256 typedef struct QEMUFileStdio
261 static int file_put_buffer(void *opaque
, const uint8_t *buf
,
262 int64_t pos
, int size
)
264 QEMUFileStdio
*s
= opaque
;
265 fseek(s
->outfile
, pos
, SEEK_SET
);
266 fwrite(buf
, 1, size
, s
->outfile
);
270 static int file_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
272 QEMUFileStdio
*s
= opaque
;
273 fseek(s
->outfile
, pos
, SEEK_SET
);
274 return fread(buf
, 1, size
, s
->outfile
);
277 static int file_close(void *opaque
)
279 QEMUFileStdio
*s
= opaque
;
285 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
289 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
291 s
->outfile
= fopen(filename
, mode
);
295 if (!strcmp(mode
, "wb"))
296 return qemu_fopen_ops(s
, file_put_buffer
, NULL
, file_close
, NULL
);
297 else if (!strcmp(mode
, "rb"))
298 return qemu_fopen_ops(s
, NULL
, file_get_buffer
, file_close
, NULL
);
307 typedef struct QEMUFileBdrv
309 BlockDriverState
*bs
;
313 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
314 int64_t pos
, int size
)
316 QEMUFileBdrv
*s
= opaque
;
317 bdrv_put_buffer(s
->bs
, buf
, s
->base_offset
+ pos
, size
);
321 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
323 QEMUFileBdrv
*s
= opaque
;
324 return bdrv_get_buffer(s
->bs
, buf
, s
->base_offset
+ pos
, size
);
327 static int bdrv_fclose(void *opaque
)
329 QEMUFileBdrv
*s
= opaque
;
334 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
338 s
= qemu_mallocz(sizeof(QEMUFileBdrv
));
341 s
->base_offset
= offset
;
344 return qemu_fopen_ops(s
, block_put_buffer
, NULL
, bdrv_fclose
, NULL
);
346 return qemu_fopen_ops(s
, NULL
, block_get_buffer
, bdrv_fclose
, NULL
);
349 QEMUFile
*qemu_fopen_ops(void *opaque
, QEMUFilePutBufferFunc
*put_buffer
,
350 QEMUFileGetBufferFunc
*get_buffer
,
351 QEMUFileCloseFunc
*close
,
352 QEMUFileRateLimit
*rate_limit
)
356 f
= qemu_mallocz(sizeof(QEMUFile
));
359 f
->put_buffer
= put_buffer
;
360 f
->get_buffer
= get_buffer
;
362 f
->rate_limit
= rate_limit
;
368 int qemu_file_has_error(QEMUFile
*f
)
373 void qemu_file_set_error(QEMUFile
*f
)
378 void qemu_fflush(QEMUFile
*f
)
383 if (f
->is_write
&& f
->buf_index
> 0) {
386 len
= f
->put_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, f
->buf_index
);
388 f
->buf_offset
+= f
->buf_index
;
395 static void qemu_fill_buffer(QEMUFile
*f
)
405 len
= f
->get_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, IO_BUF_SIZE
);
409 f
->buf_offset
+= len
;
410 } else if (len
!= -EAGAIN
)
414 int qemu_fclose(QEMUFile
*f
)
419 ret
= f
->close(f
->opaque
);
424 void qemu_file_put_notify(QEMUFile
*f
)
426 f
->put_buffer(f
->opaque
, NULL
, 0, 0);
429 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
433 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
435 "Attempted to write to buffer while read buffer is not empty\n");
439 while (!f
->has_error
&& size
> 0) {
440 l
= IO_BUF_SIZE
- f
->buf_index
;
443 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
448 if (f
->buf_index
>= IO_BUF_SIZE
)
453 void qemu_put_byte(QEMUFile
*f
, int v
)
455 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
457 "Attempted to write to buffer while read buffer is not empty\n");
461 f
->buf
[f
->buf_index
++] = v
;
463 if (f
->buf_index
>= IO_BUF_SIZE
)
467 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
476 l
= f
->buf_size
- f
->buf_index
;
479 l
= f
->buf_size
- f
->buf_index
;
485 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
493 int qemu_get_byte(QEMUFile
*f
)
498 if (f
->buf_index
>= f
->buf_size
) {
500 if (f
->buf_index
>= f
->buf_size
)
503 return f
->buf
[f
->buf_index
++];
506 int64_t qemu_ftell(QEMUFile
*f
)
508 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
511 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
513 if (whence
== SEEK_SET
) {
515 } else if (whence
== SEEK_CUR
) {
516 pos
+= qemu_ftell(f
);
518 /* SEEK_END not supported */
532 int qemu_file_rate_limit(QEMUFile
*f
)
535 return f
->rate_limit(f
->opaque
);
540 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
542 qemu_put_byte(f
, v
>> 8);
546 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
548 qemu_put_byte(f
, v
>> 24);
549 qemu_put_byte(f
, v
>> 16);
550 qemu_put_byte(f
, v
>> 8);
554 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
556 qemu_put_be32(f
, v
>> 32);
560 unsigned int qemu_get_be16(QEMUFile
*f
)
563 v
= qemu_get_byte(f
) << 8;
564 v
|= qemu_get_byte(f
);
568 unsigned int qemu_get_be32(QEMUFile
*f
)
571 v
= qemu_get_byte(f
) << 24;
572 v
|= qemu_get_byte(f
) << 16;
573 v
|= qemu_get_byte(f
) << 8;
574 v
|= qemu_get_byte(f
);
578 uint64_t qemu_get_be64(QEMUFile
*f
)
581 v
= (uint64_t)qemu_get_be32(f
) << 32;
582 v
|= qemu_get_be32(f
);
586 typedef struct SaveStateEntry
{
591 SaveLiveStateHandler
*save_live_state
;
592 SaveStateHandler
*save_state
;
593 LoadStateHandler
*load_state
;
595 struct SaveStateEntry
*next
;
598 static SaveStateEntry
*first_se
;
600 /* TODO: Individual devices generally have very little idea about the rest
601 of the system, so instance_id should be removed/replaced.
602 Meanwhile pass -1 as instance_id if you do not already have a clearly
603 distinguishing id for all instances of your device class. */
604 int register_savevm_live(const char *idstr
,
607 SaveLiveStateHandler
*save_live_state
,
608 SaveStateHandler
*save_state
,
609 LoadStateHandler
*load_state
,
612 SaveStateEntry
*se
, **pse
;
613 static int global_section_id
;
615 se
= qemu_malloc(sizeof(SaveStateEntry
));
616 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
617 se
->instance_id
= (instance_id
== -1) ? 0 : instance_id
;
618 se
->version_id
= version_id
;
619 se
->section_id
= global_section_id
++;
620 se
->save_live_state
= save_live_state
;
621 se
->save_state
= save_state
;
622 se
->load_state
= load_state
;
626 /* add at the end of list */
628 while (*pse
!= NULL
) {
629 if (instance_id
== -1
630 && strcmp(se
->idstr
, (*pse
)->idstr
) == 0
631 && se
->instance_id
<= (*pse
)->instance_id
)
632 se
->instance_id
= (*pse
)->instance_id
+ 1;
639 int register_savevm(const char *idstr
,
642 SaveStateHandler
*save_state
,
643 LoadStateHandler
*load_state
,
646 return register_savevm_live(idstr
, instance_id
, version_id
,
647 NULL
, save_state
, load_state
, opaque
);
650 #define QEMU_VM_FILE_MAGIC 0x5145564d
651 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
652 #define QEMU_VM_FILE_VERSION 0x00000003
654 #define QEMU_VM_EOF 0x00
655 #define QEMU_VM_SECTION_START 0x01
656 #define QEMU_VM_SECTION_PART 0x02
657 #define QEMU_VM_SECTION_END 0x03
658 #define QEMU_VM_SECTION_FULL 0x04
660 int qemu_savevm_state_begin(QEMUFile
*f
)
664 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
665 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
667 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
670 if (se
->save_live_state
== NULL
)
674 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
675 qemu_put_be32(f
, se
->section_id
);
678 len
= strlen(se
->idstr
);
679 qemu_put_byte(f
, len
);
680 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
682 qemu_put_be32(f
, se
->instance_id
);
683 qemu_put_be32(f
, se
->version_id
);
685 se
->save_live_state(f
, QEMU_VM_SECTION_START
, se
->opaque
);
688 if (qemu_file_has_error(f
))
694 int qemu_savevm_state_iterate(QEMUFile
*f
)
699 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
700 if (se
->save_live_state
== NULL
)
704 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
705 qemu_put_be32(f
, se
->section_id
);
707 ret
&= !!se
->save_live_state(f
, QEMU_VM_SECTION_PART
, se
->opaque
);
713 if (qemu_file_has_error(f
))
719 int qemu_savevm_state_complete(QEMUFile
*f
)
723 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
724 if (se
->save_live_state
== NULL
)
728 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
729 qemu_put_be32(f
, se
->section_id
);
731 se
->save_live_state(f
, QEMU_VM_SECTION_END
, se
->opaque
);
734 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
737 if (se
->save_state
== NULL
)
741 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
742 qemu_put_be32(f
, se
->section_id
);
745 len
= strlen(se
->idstr
);
746 qemu_put_byte(f
, len
);
747 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
749 qemu_put_be32(f
, se
->instance_id
);
750 qemu_put_be32(f
, se
->version_id
);
752 se
->save_state(f
, se
->opaque
);
755 qemu_put_byte(f
, QEMU_VM_EOF
);
757 if (qemu_file_has_error(f
))
763 int qemu_savevm_state(QEMUFile
*f
)
765 int saved_vm_running
;
768 saved_vm_running
= vm_running
;
773 ret
= qemu_savevm_state_begin(f
);
778 ret
= qemu_savevm_state_iterate(f
);
783 ret
= qemu_savevm_state_complete(f
);
786 if (qemu_file_has_error(f
))
789 if (!ret
&& saved_vm_running
)
795 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
799 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
800 if (!strcmp(se
->idstr
, idstr
) &&
801 instance_id
== se
->instance_id
)
807 typedef struct LoadStateEntry
{
811 struct LoadStateEntry
*next
;
814 static int qemu_loadvm_state_v2(QEMUFile
*f
)
817 int len
, ret
, instance_id
, record_len
, version_id
;
818 int64_t total_len
, end_pos
, cur_pos
;
821 total_len
= qemu_get_be64(f
);
822 end_pos
= total_len
+ qemu_ftell(f
);
824 if (qemu_ftell(f
) >= end_pos
)
826 len
= qemu_get_byte(f
);
827 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
829 instance_id
= qemu_get_be32(f
);
830 version_id
= qemu_get_be32(f
);
831 record_len
= qemu_get_be32(f
);
832 cur_pos
= qemu_ftell(f
);
833 se
= find_se(idstr
, instance_id
);
835 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
838 ret
= se
->load_state(f
, se
->opaque
, version_id
);
840 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
845 /* always seek to exact end of record */
846 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
849 if (qemu_file_has_error(f
))
855 int qemu_loadvm_state(QEMUFile
*f
)
857 LoadStateEntry
*first_le
= NULL
;
858 uint8_t section_type
;
862 v
= qemu_get_be32(f
);
863 if (v
!= QEMU_VM_FILE_MAGIC
)
866 v
= qemu_get_be32(f
);
867 if (v
== QEMU_VM_FILE_VERSION_COMPAT
)
868 return qemu_loadvm_state_v2(f
);
869 if (v
!= QEMU_VM_FILE_VERSION
)
872 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
873 uint32_t instance_id
, version_id
, section_id
;
879 switch (section_type
) {
880 case QEMU_VM_SECTION_START
:
881 case QEMU_VM_SECTION_FULL
:
882 /* Read section start */
883 section_id
= qemu_get_be32(f
);
884 len
= qemu_get_byte(f
);
885 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
887 instance_id
= qemu_get_be32(f
);
888 version_id
= qemu_get_be32(f
);
890 /* Find savevm section */
891 se
= find_se(idstr
, instance_id
);
893 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
898 /* Validate version */
899 if (version_id
> se
->version_id
) {
900 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
901 version_id
, idstr
, se
->version_id
);
907 le
= qemu_mallocz(sizeof(*le
));
910 le
->section_id
= section_id
;
911 le
->version_id
= version_id
;
915 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
917 case QEMU_VM_SECTION_PART
:
918 case QEMU_VM_SECTION_END
:
919 section_id
= qemu_get_be32(f
);
921 for (le
= first_le
; le
&& le
->section_id
!= section_id
; le
= le
->next
);
923 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
928 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
931 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
941 LoadStateEntry
*le
= first_le
;
942 first_le
= first_le
->next
;
946 if (qemu_file_has_error(f
))
952 /* device can contain snapshots */
953 static int bdrv_can_snapshot(BlockDriverState
*bs
)
956 !bdrv_is_removable(bs
) &&
957 !bdrv_is_read_only(bs
));
960 /* device must be snapshots in order to have a reliable snapshot */
961 static int bdrv_has_snapshot(BlockDriverState
*bs
)
964 !bdrv_is_removable(bs
) &&
965 !bdrv_is_read_only(bs
));
968 static BlockDriverState
*get_bs_snapshots(void)
970 BlockDriverState
*bs
;
975 for(i
= 0; i
<= nb_drives
; i
++) {
976 bs
= drives_table
[i
].bdrv
;
977 if (bdrv_can_snapshot(bs
))
986 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
989 QEMUSnapshotInfo
*sn_tab
, *sn
;
993 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
996 for(i
= 0; i
< nb_sns
; i
++) {
998 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
1008 void do_savevm(Monitor
*mon
, const char *name
)
1010 BlockDriverState
*bs
, *bs1
;
1011 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1012 int must_delete
, ret
, i
;
1013 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
1015 int saved_vm_running
;
1016 uint32_t vm_state_size
;
1023 bs
= get_bs_snapshots();
1025 monitor_printf(mon
, "No block device can accept snapshots\n");
1029 /* ??? Should this occur after vm_stop? */
1032 saved_vm_running
= vm_running
;
1037 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1042 memset(sn
, 0, sizeof(*sn
));
1044 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1045 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1048 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1051 /* fill auxiliary fields */
1054 sn
->date_sec
= tb
.time
;
1055 sn
->date_nsec
= tb
.millitm
* 1000000;
1057 gettimeofday(&tv
, NULL
);
1058 sn
->date_sec
= tv
.tv_sec
;
1059 sn
->date_nsec
= tv
.tv_usec
* 1000;
1061 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
1063 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
1064 monitor_printf(mon
, "Device %s does not support VM state snapshots\n",
1065 bdrv_get_device_name(bs
));
1069 /* save the VM state */
1070 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
1072 monitor_printf(mon
, "Could not open VM state file\n");
1075 ret
= qemu_savevm_state(f
);
1076 vm_state_size
= qemu_ftell(f
);
1079 monitor_printf(mon
, "Error %d while writing VM\n", ret
);
1083 /* create the snapshots */
1085 for(i
= 0; i
< nb_drives
; i
++) {
1086 bs1
= drives_table
[i
].bdrv
;
1087 if (bdrv_has_snapshot(bs1
)) {
1089 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
1092 "Error while deleting snapshot on '%s'\n",
1093 bdrv_get_device_name(bs1
));
1096 /* Write VM state size only to the image that contains the state */
1097 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1098 ret
= bdrv_snapshot_create(bs1
, sn
);
1100 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
1101 bdrv_get_device_name(bs1
));
1107 if (saved_vm_running
)
1111 void do_loadvm(Monitor
*mon
, const char *name
)
1113 BlockDriverState
*bs
, *bs1
;
1114 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
1115 QEMUSnapshotInfo sn
;
1118 int saved_vm_running
;
1120 bs
= get_bs_snapshots();
1122 monitor_printf(mon
, "No block device supports snapshots\n");
1126 /* Flush all IO requests so they don't interfere with the new state. */
1129 saved_vm_running
= vm_running
;
1132 for(i
= 0; i
<= nb_drives
; i
++) {
1133 bs1
= drives_table
[i
].bdrv
;
1134 if (bdrv_has_snapshot(bs1
)) {
1135 ret
= bdrv_snapshot_goto(bs1
, name
);
1138 monitor_printf(mon
, "Warning: ");
1142 "Snapshots not supported on device '%s'\n",
1143 bdrv_get_device_name(bs1
));
1146 monitor_printf(mon
, "Could not find snapshot '%s' on "
1148 name
, bdrv_get_device_name(bs1
));
1151 monitor_printf(mon
, "Error %d while activating snapshot on"
1152 " '%s'\n", ret
, bdrv_get_device_name(bs1
));
1155 /* fatal on snapshot block device */
1162 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
1163 monitor_printf(mon
, "Device %s does not support VM state snapshots\n",
1164 bdrv_get_device_name(bs
));
1168 /* Don't even try to load empty VM states */
1169 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1170 if ((ret
>= 0) && (sn
.vm_state_size
== 0))
1173 /* restore the VM state */
1174 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
1176 monitor_printf(mon
, "Could not open VM state file\n");
1179 ret
= qemu_loadvm_state(f
);
1182 monitor_printf(mon
, "Error %d while loading VM state\n", ret
);
1185 if (saved_vm_running
)
1189 void do_delvm(Monitor
*mon
, const char *name
)
1191 BlockDriverState
*bs
, *bs1
;
1194 bs
= get_bs_snapshots();
1196 monitor_printf(mon
, "No block device supports snapshots\n");
1200 for(i
= 0; i
<= nb_drives
; i
++) {
1201 bs1
= drives_table
[i
].bdrv
;
1202 if (bdrv_has_snapshot(bs1
)) {
1203 ret
= bdrv_snapshot_delete(bs1
, name
);
1205 if (ret
== -ENOTSUP
)
1207 "Snapshots not supported on device '%s'\n",
1208 bdrv_get_device_name(bs1
));
1210 monitor_printf(mon
, "Error %d while deleting snapshot on "
1211 "'%s'\n", ret
, bdrv_get_device_name(bs1
));
1217 void do_info_snapshots(Monitor
*mon
)
1219 BlockDriverState
*bs
, *bs1
;
1220 QEMUSnapshotInfo
*sn_tab
, *sn
;
1224 bs
= get_bs_snapshots();
1226 monitor_printf(mon
, "No available block device supports snapshots\n");
1229 monitor_printf(mon
, "Snapshot devices:");
1230 for(i
= 0; i
<= nb_drives
; i
++) {
1231 bs1
= drives_table
[i
].bdrv
;
1232 if (bdrv_has_snapshot(bs1
)) {
1234 monitor_printf(mon
, " %s", bdrv_get_device_name(bs1
));
1237 monitor_printf(mon
, "\n");
1239 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1241 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
1244 monitor_printf(mon
, "Snapshot list (from %s):\n",
1245 bdrv_get_device_name(bs
));
1246 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
1247 for(i
= 0; i
< nb_sns
; i
++) {
1249 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));