root / savevm.c @ 3c4cf535
History | View | Annotate | Download (30.4 kB)
1 | a672b469 | aliguori | /*
|
---|---|---|---|
2 | a672b469 | aliguori | * QEMU System Emulator
|
3 | a672b469 | aliguori | *
|
4 | a672b469 | aliguori | * Copyright (c) 2003-2008 Fabrice Bellard
|
5 | a672b469 | aliguori | *
|
6 | a672b469 | aliguori | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | a672b469 | aliguori | * of this software and associated documentation files (the "Software"), to deal
|
8 | a672b469 | aliguori | * in the Software without restriction, including without limitation the rights
|
9 | a672b469 | aliguori | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | a672b469 | aliguori | * copies of the Software, and to permit persons to whom the Software is
|
11 | a672b469 | aliguori | * furnished to do so, subject to the following conditions:
|
12 | a672b469 | aliguori | *
|
13 | a672b469 | aliguori | * The above copyright notice and this permission notice shall be included in
|
14 | a672b469 | aliguori | * all copies or substantial portions of the Software.
|
15 | a672b469 | aliguori | *
|
16 | a672b469 | aliguori | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | a672b469 | aliguori | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | a672b469 | aliguori | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | a672b469 | aliguori | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | a672b469 | aliguori | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | a672b469 | aliguori | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | a672b469 | aliguori | * THE SOFTWARE.
|
23 | a672b469 | aliguori | */
|
24 | a672b469 | aliguori | #include "qemu-common.h" |
25 | a672b469 | aliguori | #include "hw/hw.h" |
26 | a672b469 | aliguori | #include "net.h" |
27 | a672b469 | aliguori | #include "console.h" |
28 | a672b469 | aliguori | #include "sysemu.h" |
29 | a672b469 | aliguori | #include "qemu-timer.h" |
30 | a672b469 | aliguori | #include "qemu-char.h" |
31 | a672b469 | aliguori | #include "block.h" |
32 | a672b469 | aliguori | #include "audio/audio.h" |
33 | a672b469 | aliguori | #include "migration.h" |
34 | a672b469 | aliguori | #include "qemu_socket.h" |
35 | a672b469 | aliguori | |
36 | a672b469 | aliguori | #include <unistd.h> |
37 | a672b469 | aliguori | #include <fcntl.h> |
38 | a672b469 | aliguori | #include <signal.h> |
39 | a672b469 | aliguori | #include <time.h> |
40 | a672b469 | aliguori | #include <errno.h> |
41 | a672b469 | aliguori | #include <sys/time.h> |
42 | a672b469 | aliguori | #include <zlib.h> |
43 | a672b469 | aliguori | |
44 | a672b469 | aliguori | #ifndef _WIN32
|
45 | a672b469 | aliguori | #include <sys/times.h> |
46 | a672b469 | aliguori | #include <sys/wait.h> |
47 | a672b469 | aliguori | #include <termios.h> |
48 | a672b469 | aliguori | #include <sys/mman.h> |
49 | a672b469 | aliguori | #include <sys/ioctl.h> |
50 | a672b469 | aliguori | #include <sys/resource.h> |
51 | a672b469 | aliguori | #include <sys/socket.h> |
52 | a672b469 | aliguori | #include <netinet/in.h> |
53 | a672b469 | aliguori | #include <net/if.h> |
54 | a672b469 | aliguori | #if defined(__NetBSD__)
|
55 | a672b469 | aliguori | #include <net/if_tap.h> |
56 | a672b469 | aliguori | #endif
|
57 | a672b469 | aliguori | #ifdef __linux__
|
58 | a672b469 | aliguori | #include <linux/if_tun.h> |
59 | a672b469 | aliguori | #endif
|
60 | a672b469 | aliguori | #include <arpa/inet.h> |
61 | a672b469 | aliguori | #include <dirent.h> |
62 | a672b469 | aliguori | #include <netdb.h> |
63 | a672b469 | aliguori | #include <sys/select.h> |
64 | a672b469 | aliguori | #ifdef _BSD
|
65 | a672b469 | aliguori | #include <sys/stat.h> |
66 | a672b469 | aliguori | #ifdef __FreeBSD__
|
67 | a672b469 | aliguori | #include <libutil.h> |
68 | a672b469 | aliguori | #else
|
69 | a672b469 | aliguori | #include <util.h> |
70 | a672b469 | aliguori | #endif
|
71 | a672b469 | aliguori | #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
|
72 | a672b469 | aliguori | #include <freebsd/stdlib.h> |
73 | a672b469 | aliguori | #else
|
74 | a672b469 | aliguori | #ifdef __linux__
|
75 | a672b469 | aliguori | #include <pty.h> |
76 | a672b469 | aliguori | #include <malloc.h> |
77 | a672b469 | aliguori | #include <linux/rtc.h> |
78 | a672b469 | aliguori | #endif
|
79 | a672b469 | aliguori | #endif
|
80 | a672b469 | aliguori | #endif
|
81 | a672b469 | aliguori | |
82 | a672b469 | aliguori | #ifdef _WIN32
|
83 | a672b469 | aliguori | #include <malloc.h> |
84 | a672b469 | aliguori | #include <sys/timeb.h> |
85 | a672b469 | aliguori | #include <mmsystem.h> |
86 | a672b469 | aliguori | #define getopt_long_only getopt_long
|
87 | a672b469 | aliguori | #define memalign(align, size) malloc(size)
|
88 | a672b469 | aliguori | #endif
|
89 | a672b469 | aliguori | |
90 | a672b469 | aliguori | /* point to the block driver where the snapshots are managed */
|
91 | a672b469 | aliguori | static BlockDriverState *bs_snapshots;
|
92 | a672b469 | aliguori | |
93 | a672b469 | aliguori | #define SELF_ANNOUNCE_ROUNDS 5 |
94 | a672b469 | aliguori | #define ETH_P_EXPERIMENTAL 0x01F1 /* just a number */ |
95 | a672b469 | aliguori | //#define ETH_P_EXPERIMENTAL 0x0012 /* make it the size of the packet */
|
96 | a672b469 | aliguori | #define EXPERIMENTAL_MAGIC 0xf1f23f4f |
97 | a672b469 | aliguori | |
98 | a672b469 | aliguori | static int announce_self_create(uint8_t *buf, |
99 | a672b469 | aliguori | uint8_t *mac_addr) |
100 | a672b469 | aliguori | { |
101 | a672b469 | aliguori | uint32_t magic = EXPERIMENTAL_MAGIC; |
102 | a672b469 | aliguori | uint16_t proto = htons(ETH_P_EXPERIMENTAL); |
103 | a672b469 | aliguori | |
104 | a672b469 | aliguori | /* FIXME: should we send a different packet (arp/rarp/ping)? */
|
105 | a672b469 | aliguori | |
106 | a672b469 | aliguori | memset(buf, 0xff, 6); /* h_dst */ |
107 | a672b469 | aliguori | memcpy(buf + 6, mac_addr, 6); /* h_src */ |
108 | a672b469 | aliguori | memcpy(buf + 12, &proto, 2); /* h_proto */ |
109 | a672b469 | aliguori | memcpy(buf + 14, &magic, 4); /* magic */ |
110 | a672b469 | aliguori | |
111 | a672b469 | aliguori | return 18; /* len */ |
112 | a672b469 | aliguori | } |
113 | a672b469 | aliguori | |
114 | a672b469 | aliguori | void qemu_announce_self(void) |
115 | a672b469 | aliguori | { |
116 | a672b469 | aliguori | int i, j, len;
|
117 | a672b469 | aliguori | VLANState *vlan; |
118 | a672b469 | aliguori | VLANClientState *vc; |
119 | a672b469 | aliguori | uint8_t buf[256];
|
120 | a672b469 | aliguori | |
121 | a672b469 | aliguori | for (i = 0; i < nb_nics; i++) { |
122 | a672b469 | aliguori | len = announce_self_create(buf, nd_table[i].macaddr); |
123 | a672b469 | aliguori | vlan = nd_table[i].vlan; |
124 | a672b469 | aliguori | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
125 | a672b469 | aliguori | for (j=0; j < SELF_ANNOUNCE_ROUNDS; j++) |
126 | a672b469 | aliguori | vc->fd_read(vc->opaque, buf, len); |
127 | a672b469 | aliguori | } |
128 | a672b469 | aliguori | } |
129 | a672b469 | aliguori | } |
130 | a672b469 | aliguori | |
131 | a672b469 | aliguori | /***********************************************************/
|
132 | a672b469 | aliguori | /* savevm/loadvm support */
|
133 | a672b469 | aliguori | |
134 | a672b469 | aliguori | #define IO_BUF_SIZE 32768 |
135 | a672b469 | aliguori | |
136 | a672b469 | aliguori | struct QEMUFile {
|
137 | a672b469 | aliguori | QEMUFilePutBufferFunc *put_buffer; |
138 | a672b469 | aliguori | QEMUFileGetBufferFunc *get_buffer; |
139 | a672b469 | aliguori | QEMUFileCloseFunc *close; |
140 | a672b469 | aliguori | QEMUFileRateLimit *rate_limit; |
141 | a672b469 | aliguori | void *opaque;
|
142 | a672b469 | aliguori | int is_write;
|
143 | a672b469 | aliguori | |
144 | a672b469 | aliguori | int64_t buf_offset; /* start of buffer when writing, end of buffer
|
145 | a672b469 | aliguori | when reading */
|
146 | a672b469 | aliguori | int buf_index;
|
147 | a672b469 | aliguori | int buf_size; /* 0 when writing */ |
148 | a672b469 | aliguori | uint8_t buf[IO_BUF_SIZE]; |
149 | a672b469 | aliguori | |
150 | a672b469 | aliguori | int has_error;
|
151 | a672b469 | aliguori | }; |
152 | a672b469 | aliguori | |
153 | a672b469 | aliguori | typedef struct QEMUFilePopen |
154 | a672b469 | aliguori | { |
155 | a672b469 | aliguori | FILE *popen_file; |
156 | a672b469 | aliguori | QEMUFile *file; |
157 | a672b469 | aliguori | } QEMUFilePopen; |
158 | a672b469 | aliguori | |
159 | a672b469 | aliguori | typedef struct QEMUFileSocket |
160 | a672b469 | aliguori | { |
161 | a672b469 | aliguori | int fd;
|
162 | a672b469 | aliguori | QEMUFile *file; |
163 | a672b469 | aliguori | } QEMUFileSocket; |
164 | a672b469 | aliguori | |
165 | a672b469 | aliguori | static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) |
166 | a672b469 | aliguori | { |
167 | a672b469 | aliguori | QEMUFileSocket *s = opaque; |
168 | a672b469 | aliguori | ssize_t len; |
169 | a672b469 | aliguori | |
170 | a672b469 | aliguori | do {
|
171 | a672b469 | aliguori | len = recv(s->fd, buf, size, 0);
|
172 | a672b469 | aliguori | } while (len == -1 && socket_error() == EINTR); |
173 | a672b469 | aliguori | |
174 | a672b469 | aliguori | if (len == -1) |
175 | a672b469 | aliguori | len = -socket_error(); |
176 | a672b469 | aliguori | |
177 | a672b469 | aliguori | return len;
|
178 | a672b469 | aliguori | } |
179 | a672b469 | aliguori | |
180 | a672b469 | aliguori | static int socket_close(void *opaque) |
181 | a672b469 | aliguori | { |
182 | a672b469 | aliguori | QEMUFileSocket *s = opaque; |
183 | a672b469 | aliguori | qemu_free(s); |
184 | a672b469 | aliguori | return 0; |
185 | a672b469 | aliguori | } |
186 | a672b469 | aliguori | |
187 | a672b469 | aliguori | static int popen_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size) |
188 | a672b469 | aliguori | { |
189 | a672b469 | aliguori | QEMUFilePopen *s = opaque; |
190 | a672b469 | aliguori | return fwrite(buf, 1, size, s->popen_file); |
191 | a672b469 | aliguori | } |
192 | a672b469 | aliguori | |
193 | a672b469 | aliguori | static int popen_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) |
194 | a672b469 | aliguori | { |
195 | a672b469 | aliguori | QEMUFilePopen *s = opaque; |
196 | a672b469 | aliguori | return fread(buf, 1, size, s->popen_file); |
197 | a672b469 | aliguori | } |
198 | a672b469 | aliguori | |
199 | a672b469 | aliguori | static int popen_close(void *opaque) |
200 | a672b469 | aliguori | { |
201 | a672b469 | aliguori | QEMUFilePopen *s = opaque; |
202 | a672b469 | aliguori | pclose(s->popen_file); |
203 | a672b469 | aliguori | qemu_free(s); |
204 | a672b469 | aliguori | return 0; |
205 | a672b469 | aliguori | } |
206 | a672b469 | aliguori | |
207 | a672b469 | aliguori | QEMUFile *qemu_popen(FILE *popen_file, const char *mode) |
208 | a672b469 | aliguori | { |
209 | a672b469 | aliguori | QEMUFilePopen *s; |
210 | a672b469 | aliguori | |
211 | a672b469 | aliguori | if (popen_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) { |
212 | a672b469 | aliguori | fprintf(stderr, "qemu_popen: Argument validity check failed\n");
|
213 | a672b469 | aliguori | return NULL; |
214 | a672b469 | aliguori | } |
215 | a672b469 | aliguori | |
216 | a672b469 | aliguori | s = qemu_mallocz(sizeof(QEMUFilePopen));
|
217 | a672b469 | aliguori | |
218 | a672b469 | aliguori | s->popen_file = popen_file; |
219 | a672b469 | aliguori | |
220 | a672b469 | aliguori | if(mode[0] == 'r') { |
221 | a672b469 | aliguori | s->file = qemu_fopen_ops(s, NULL, popen_get_buffer, popen_close, NULL); |
222 | a672b469 | aliguori | } else {
|
223 | a672b469 | aliguori | s->file = qemu_fopen_ops(s, popen_put_buffer, NULL, popen_close, NULL); |
224 | a672b469 | aliguori | } |
225 | a672b469 | aliguori | fprintf(stderr, "qemu_popen: returning result of qemu_fopen_ops\n");
|
226 | a672b469 | aliguori | return s->file;
|
227 | a672b469 | aliguori | } |
228 | a672b469 | aliguori | |
229 | a672b469 | aliguori | QEMUFile *qemu_popen_cmd(const char *command, const char *mode) |
230 | a672b469 | aliguori | { |
231 | a672b469 | aliguori | FILE *popen_file; |
232 | a672b469 | aliguori | |
233 | a672b469 | aliguori | popen_file = popen(command, mode); |
234 | a672b469 | aliguori | if(popen_file == NULL) { |
235 | a672b469 | aliguori | return NULL; |
236 | a672b469 | aliguori | } |
237 | a672b469 | aliguori | |
238 | a672b469 | aliguori | return qemu_popen(popen_file, mode);
|
239 | a672b469 | aliguori | } |
240 | a672b469 | aliguori | |
241 | a672b469 | aliguori | QEMUFile *qemu_fopen_socket(int fd)
|
242 | a672b469 | aliguori | { |
243 | a672b469 | aliguori | QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
|
244 | a672b469 | aliguori | |
245 | a672b469 | aliguori | s->fd = fd; |
246 | a672b469 | aliguori | s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, NULL); |
247 | a672b469 | aliguori | return s->file;
|
248 | a672b469 | aliguori | } |
249 | a672b469 | aliguori | |
250 | a672b469 | aliguori | typedef struct QEMUFileStdio |
251 | a672b469 | aliguori | { |
252 | a672b469 | aliguori | FILE *outfile; |
253 | a672b469 | aliguori | } QEMUFileStdio; |
254 | a672b469 | aliguori | |
255 | a672b469 | aliguori | static int file_put_buffer(void *opaque, const uint8_t *buf, |
256 | a672b469 | aliguori | int64_t pos, int size)
|
257 | a672b469 | aliguori | { |
258 | a672b469 | aliguori | QEMUFileStdio *s = opaque; |
259 | a672b469 | aliguori | fseek(s->outfile, pos, SEEK_SET); |
260 | a672b469 | aliguori | fwrite(buf, 1, size, s->outfile);
|
261 | a672b469 | aliguori | return size;
|
262 | a672b469 | aliguori | } |
263 | a672b469 | aliguori | |
264 | a672b469 | aliguori | static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) |
265 | a672b469 | aliguori | { |
266 | a672b469 | aliguori | QEMUFileStdio *s = opaque; |
267 | a672b469 | aliguori | fseek(s->outfile, pos, SEEK_SET); |
268 | a672b469 | aliguori | return fread(buf, 1, size, s->outfile); |
269 | a672b469 | aliguori | } |
270 | a672b469 | aliguori | |
271 | a672b469 | aliguori | static int file_close(void *opaque) |
272 | a672b469 | aliguori | { |
273 | a672b469 | aliguori | QEMUFileStdio *s = opaque; |
274 | a672b469 | aliguori | fclose(s->outfile); |
275 | a672b469 | aliguori | qemu_free(s); |
276 | a672b469 | aliguori | return 0; |
277 | a672b469 | aliguori | } |
278 | a672b469 | aliguori | |
279 | a672b469 | aliguori | QEMUFile *qemu_fopen(const char *filename, const char *mode) |
280 | a672b469 | aliguori | { |
281 | a672b469 | aliguori | QEMUFileStdio *s; |
282 | a672b469 | aliguori | |
283 | a672b469 | aliguori | s = qemu_mallocz(sizeof(QEMUFileStdio));
|
284 | a672b469 | aliguori | |
285 | a672b469 | aliguori | s->outfile = fopen(filename, mode); |
286 | a672b469 | aliguori | if (!s->outfile)
|
287 | a672b469 | aliguori | goto fail;
|
288 | a672b469 | aliguori | |
289 | a672b469 | aliguori | if (!strcmp(mode, "wb")) |
290 | a672b469 | aliguori | return qemu_fopen_ops(s, file_put_buffer, NULL, file_close, NULL); |
291 | a672b469 | aliguori | else if (!strcmp(mode, "rb")) |
292 | a672b469 | aliguori | return qemu_fopen_ops(s, NULL, file_get_buffer, file_close, NULL); |
293 | a672b469 | aliguori | |
294 | a672b469 | aliguori | fail:
|
295 | a672b469 | aliguori | if (s->outfile)
|
296 | a672b469 | aliguori | fclose(s->outfile); |
297 | a672b469 | aliguori | qemu_free(s); |
298 | a672b469 | aliguori | return NULL; |
299 | a672b469 | aliguori | } |
300 | a672b469 | aliguori | |
301 | a672b469 | aliguori | typedef struct QEMUFileBdrv |
302 | a672b469 | aliguori | { |
303 | a672b469 | aliguori | BlockDriverState *bs; |
304 | a672b469 | aliguori | int64_t base_offset; |
305 | a672b469 | aliguori | } QEMUFileBdrv; |
306 | a672b469 | aliguori | |
307 | a672b469 | aliguori | static int bdrv_put_buffer(void *opaque, const uint8_t *buf, |
308 | a672b469 | aliguori | int64_t pos, int size)
|
309 | a672b469 | aliguori | { |
310 | a672b469 | aliguori | QEMUFileBdrv *s = opaque; |
311 | a672b469 | aliguori | bdrv_pwrite(s->bs, s->base_offset + pos, buf, size); |
312 | a672b469 | aliguori | return size;
|
313 | a672b469 | aliguori | } |
314 | a672b469 | aliguori | |
315 | a672b469 | aliguori | static int bdrv_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) |
316 | a672b469 | aliguori | { |
317 | a672b469 | aliguori | QEMUFileBdrv *s = opaque; |
318 | a672b469 | aliguori | return bdrv_pread(s->bs, s->base_offset + pos, buf, size);
|
319 | a672b469 | aliguori | } |
320 | a672b469 | aliguori | |
321 | a672b469 | aliguori | static int bdrv_fclose(void *opaque) |
322 | a672b469 | aliguori | { |
323 | a672b469 | aliguori | QEMUFileBdrv *s = opaque; |
324 | a672b469 | aliguori | qemu_free(s); |
325 | a672b469 | aliguori | return 0; |
326 | a672b469 | aliguori | } |
327 | a672b469 | aliguori | |
328 | a672b469 | aliguori | static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable) |
329 | a672b469 | aliguori | { |
330 | a672b469 | aliguori | QEMUFileBdrv *s; |
331 | a672b469 | aliguori | |
332 | a672b469 | aliguori | s = qemu_mallocz(sizeof(QEMUFileBdrv));
|
333 | a672b469 | aliguori | |
334 | a672b469 | aliguori | s->bs = bs; |
335 | a672b469 | aliguori | s->base_offset = offset; |
336 | a672b469 | aliguori | |
337 | a672b469 | aliguori | if (is_writable)
|
338 | a672b469 | aliguori | return qemu_fopen_ops(s, bdrv_put_buffer, NULL, bdrv_fclose, NULL); |
339 | a672b469 | aliguori | |
340 | a672b469 | aliguori | return qemu_fopen_ops(s, NULL, bdrv_get_buffer, bdrv_fclose, NULL); |
341 | a672b469 | aliguori | } |
342 | a672b469 | aliguori | |
343 | a672b469 | aliguori | QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
|
344 | a672b469 | aliguori | QEMUFileGetBufferFunc *get_buffer, |
345 | a672b469 | aliguori | QEMUFileCloseFunc *close, |
346 | a672b469 | aliguori | QEMUFileRateLimit *rate_limit) |
347 | a672b469 | aliguori | { |
348 | a672b469 | aliguori | QEMUFile *f; |
349 | a672b469 | aliguori | |
350 | a672b469 | aliguori | f = qemu_mallocz(sizeof(QEMUFile));
|
351 | a672b469 | aliguori | |
352 | a672b469 | aliguori | f->opaque = opaque; |
353 | a672b469 | aliguori | f->put_buffer = put_buffer; |
354 | a672b469 | aliguori | f->get_buffer = get_buffer; |
355 | a672b469 | aliguori | f->close = close; |
356 | a672b469 | aliguori | f->rate_limit = rate_limit; |
357 | a672b469 | aliguori | f->is_write = 0;
|
358 | a672b469 | aliguori | |
359 | a672b469 | aliguori | return f;
|
360 | a672b469 | aliguori | } |
361 | a672b469 | aliguori | |
362 | a672b469 | aliguori | int qemu_file_has_error(QEMUFile *f)
|
363 | a672b469 | aliguori | { |
364 | a672b469 | aliguori | return f->has_error;
|
365 | a672b469 | aliguori | } |
366 | a672b469 | aliguori | |
367 | a672b469 | aliguori | void qemu_fflush(QEMUFile *f)
|
368 | a672b469 | aliguori | { |
369 | a672b469 | aliguori | if (!f->put_buffer)
|
370 | a672b469 | aliguori | return;
|
371 | a672b469 | aliguori | |
372 | a672b469 | aliguori | if (f->is_write && f->buf_index > 0) { |
373 | a672b469 | aliguori | int len;
|
374 | a672b469 | aliguori | |
375 | a672b469 | aliguori | len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index); |
376 | a672b469 | aliguori | if (len > 0) |
377 | a672b469 | aliguori | f->buf_offset += f->buf_index; |
378 | a672b469 | aliguori | else
|
379 | a672b469 | aliguori | f->has_error = 1;
|
380 | a672b469 | aliguori | f->buf_index = 0;
|
381 | a672b469 | aliguori | } |
382 | a672b469 | aliguori | } |
383 | a672b469 | aliguori | |
384 | a672b469 | aliguori | static void qemu_fill_buffer(QEMUFile *f) |
385 | a672b469 | aliguori | { |
386 | a672b469 | aliguori | int len;
|
387 | a672b469 | aliguori | |
388 | a672b469 | aliguori | if (!f->get_buffer)
|
389 | a672b469 | aliguori | return;
|
390 | a672b469 | aliguori | |
391 | a672b469 | aliguori | if (f->is_write)
|
392 | a672b469 | aliguori | abort(); |
393 | a672b469 | aliguori | |
394 | a672b469 | aliguori | len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE); |
395 | a672b469 | aliguori | if (len > 0) { |
396 | a672b469 | aliguori | f->buf_index = 0;
|
397 | a672b469 | aliguori | f->buf_size = len; |
398 | a672b469 | aliguori | f->buf_offset += len; |
399 | a672b469 | aliguori | } else if (len != -EAGAIN) |
400 | a672b469 | aliguori | f->has_error = 1;
|
401 | a672b469 | aliguori | } |
402 | a672b469 | aliguori | |
403 | a672b469 | aliguori | int qemu_fclose(QEMUFile *f)
|
404 | a672b469 | aliguori | { |
405 | a672b469 | aliguori | int ret = 0; |
406 | a672b469 | aliguori | qemu_fflush(f); |
407 | a672b469 | aliguori | if (f->close)
|
408 | a672b469 | aliguori | ret = f->close(f->opaque); |
409 | a672b469 | aliguori | qemu_free(f); |
410 | a672b469 | aliguori | return ret;
|
411 | a672b469 | aliguori | } |
412 | a672b469 | aliguori | |
413 | a672b469 | aliguori | void qemu_file_put_notify(QEMUFile *f)
|
414 | a672b469 | aliguori | { |
415 | a672b469 | aliguori | f->put_buffer(f->opaque, NULL, 0, 0); |
416 | a672b469 | aliguori | } |
417 | a672b469 | aliguori | |
418 | a672b469 | aliguori | void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) |
419 | a672b469 | aliguori | { |
420 | a672b469 | aliguori | int l;
|
421 | a672b469 | aliguori | |
422 | a672b469 | aliguori | if (!f->has_error && f->is_write == 0 && f->buf_index > 0) { |
423 | a672b469 | aliguori | fprintf(stderr, |
424 | a672b469 | aliguori | "Attempted to write to buffer while read buffer is not empty\n");
|
425 | a672b469 | aliguori | abort(); |
426 | a672b469 | aliguori | } |
427 | a672b469 | aliguori | |
428 | a672b469 | aliguori | while (!f->has_error && size > 0) { |
429 | a672b469 | aliguori | l = IO_BUF_SIZE - f->buf_index; |
430 | a672b469 | aliguori | if (l > size)
|
431 | a672b469 | aliguori | l = size; |
432 | a672b469 | aliguori | memcpy(f->buf + f->buf_index, buf, l); |
433 | a672b469 | aliguori | f->is_write = 1;
|
434 | a672b469 | aliguori | f->buf_index += l; |
435 | a672b469 | aliguori | buf += l; |
436 | a672b469 | aliguori | size -= l; |
437 | a672b469 | aliguori | if (f->buf_index >= IO_BUF_SIZE)
|
438 | a672b469 | aliguori | qemu_fflush(f); |
439 | a672b469 | aliguori | } |
440 | a672b469 | aliguori | } |
441 | a672b469 | aliguori | |
442 | a672b469 | aliguori | void qemu_put_byte(QEMUFile *f, int v) |
443 | a672b469 | aliguori | { |
444 | a672b469 | aliguori | if (!f->has_error && f->is_write == 0 && f->buf_index > 0) { |
445 | a672b469 | aliguori | fprintf(stderr, |
446 | a672b469 | aliguori | "Attempted to write to buffer while read buffer is not empty\n");
|
447 | a672b469 | aliguori | abort(); |
448 | a672b469 | aliguori | } |
449 | a672b469 | aliguori | |
450 | a672b469 | aliguori | f->buf[f->buf_index++] = v; |
451 | a672b469 | aliguori | f->is_write = 1;
|
452 | a672b469 | aliguori | if (f->buf_index >= IO_BUF_SIZE)
|
453 | a672b469 | aliguori | qemu_fflush(f); |
454 | a672b469 | aliguori | } |
455 | a672b469 | aliguori | |
456 | a672b469 | aliguori | int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1) |
457 | a672b469 | aliguori | { |
458 | a672b469 | aliguori | int size, l;
|
459 | a672b469 | aliguori | |
460 | a672b469 | aliguori | if (f->is_write)
|
461 | a672b469 | aliguori | abort(); |
462 | a672b469 | aliguori | |
463 | a672b469 | aliguori | size = size1; |
464 | a672b469 | aliguori | while (size > 0) { |
465 | a672b469 | aliguori | l = f->buf_size - f->buf_index; |
466 | a672b469 | aliguori | if (l == 0) { |
467 | a672b469 | aliguori | qemu_fill_buffer(f); |
468 | a672b469 | aliguori | l = f->buf_size - f->buf_index; |
469 | a672b469 | aliguori | if (l == 0) |
470 | a672b469 | aliguori | break;
|
471 | a672b469 | aliguori | } |
472 | a672b469 | aliguori | if (l > size)
|
473 | a672b469 | aliguori | l = size; |
474 | a672b469 | aliguori | memcpy(buf, f->buf + f->buf_index, l); |
475 | a672b469 | aliguori | f->buf_index += l; |
476 | a672b469 | aliguori | buf += l; |
477 | a672b469 | aliguori | size -= l; |
478 | a672b469 | aliguori | } |
479 | a672b469 | aliguori | return size1 - size;
|
480 | a672b469 | aliguori | } |
481 | a672b469 | aliguori | |
482 | a672b469 | aliguori | int qemu_get_byte(QEMUFile *f)
|
483 | a672b469 | aliguori | { |
484 | a672b469 | aliguori | if (f->is_write)
|
485 | a672b469 | aliguori | abort(); |
486 | a672b469 | aliguori | |
487 | a672b469 | aliguori | if (f->buf_index >= f->buf_size) {
|
488 | a672b469 | aliguori | qemu_fill_buffer(f); |
489 | a672b469 | aliguori | if (f->buf_index >= f->buf_size)
|
490 | a672b469 | aliguori | return 0; |
491 | a672b469 | aliguori | } |
492 | a672b469 | aliguori | return f->buf[f->buf_index++];
|
493 | a672b469 | aliguori | } |
494 | a672b469 | aliguori | |
495 | a672b469 | aliguori | int64_t qemu_ftell(QEMUFile *f) |
496 | a672b469 | aliguori | { |
497 | a672b469 | aliguori | return f->buf_offset - f->buf_size + f->buf_index;
|
498 | a672b469 | aliguori | } |
499 | a672b469 | aliguori | |
500 | a672b469 | aliguori | int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
|
501 | a672b469 | aliguori | { |
502 | a672b469 | aliguori | if (whence == SEEK_SET) {
|
503 | a672b469 | aliguori | /* nothing to do */
|
504 | a672b469 | aliguori | } else if (whence == SEEK_CUR) { |
505 | a672b469 | aliguori | pos += qemu_ftell(f); |
506 | a672b469 | aliguori | } else {
|
507 | a672b469 | aliguori | /* SEEK_END not supported */
|
508 | a672b469 | aliguori | return -1; |
509 | a672b469 | aliguori | } |
510 | a672b469 | aliguori | if (f->put_buffer) {
|
511 | a672b469 | aliguori | qemu_fflush(f); |
512 | a672b469 | aliguori | f->buf_offset = pos; |
513 | a672b469 | aliguori | } else {
|
514 | a672b469 | aliguori | f->buf_offset = pos; |
515 | a672b469 | aliguori | f->buf_index = 0;
|
516 | a672b469 | aliguori | f->buf_size = 0;
|
517 | a672b469 | aliguori | } |
518 | a672b469 | aliguori | return pos;
|
519 | a672b469 | aliguori | } |
520 | a672b469 | aliguori | |
521 | a672b469 | aliguori | int qemu_file_rate_limit(QEMUFile *f)
|
522 | a672b469 | aliguori | { |
523 | a672b469 | aliguori | if (f->rate_limit)
|
524 | a672b469 | aliguori | return f->rate_limit(f->opaque);
|
525 | a672b469 | aliguori | |
526 | a672b469 | aliguori | return 0; |
527 | a672b469 | aliguori | } |
528 | a672b469 | aliguori | |
529 | a672b469 | aliguori | void qemu_put_be16(QEMUFile *f, unsigned int v) |
530 | a672b469 | aliguori | { |
531 | a672b469 | aliguori | qemu_put_byte(f, v >> 8);
|
532 | a672b469 | aliguori | qemu_put_byte(f, v); |
533 | a672b469 | aliguori | } |
534 | a672b469 | aliguori | |
535 | a672b469 | aliguori | void qemu_put_be32(QEMUFile *f, unsigned int v) |
536 | a672b469 | aliguori | { |
537 | a672b469 | aliguori | qemu_put_byte(f, v >> 24);
|
538 | a672b469 | aliguori | qemu_put_byte(f, v >> 16);
|
539 | a672b469 | aliguori | qemu_put_byte(f, v >> 8);
|
540 | a672b469 | aliguori | qemu_put_byte(f, v); |
541 | a672b469 | aliguori | } |
542 | a672b469 | aliguori | |
543 | a672b469 | aliguori | void qemu_put_be64(QEMUFile *f, uint64_t v)
|
544 | a672b469 | aliguori | { |
545 | a672b469 | aliguori | qemu_put_be32(f, v >> 32);
|
546 | a672b469 | aliguori | qemu_put_be32(f, v); |
547 | a672b469 | aliguori | } |
548 | a672b469 | aliguori | |
549 | a672b469 | aliguori | unsigned int qemu_get_be16(QEMUFile *f) |
550 | a672b469 | aliguori | { |
551 | a672b469 | aliguori | unsigned int v; |
552 | a672b469 | aliguori | v = qemu_get_byte(f) << 8;
|
553 | a672b469 | aliguori | v |= qemu_get_byte(f); |
554 | a672b469 | aliguori | return v;
|
555 | a672b469 | aliguori | } |
556 | a672b469 | aliguori | |
557 | a672b469 | aliguori | unsigned int qemu_get_be32(QEMUFile *f) |
558 | a672b469 | aliguori | { |
559 | a672b469 | aliguori | unsigned int v; |
560 | a672b469 | aliguori | v = qemu_get_byte(f) << 24;
|
561 | a672b469 | aliguori | v |= qemu_get_byte(f) << 16;
|
562 | a672b469 | aliguori | v |= qemu_get_byte(f) << 8;
|
563 | a672b469 | aliguori | v |= qemu_get_byte(f); |
564 | a672b469 | aliguori | return v;
|
565 | a672b469 | aliguori | } |
566 | a672b469 | aliguori | |
567 | a672b469 | aliguori | uint64_t qemu_get_be64(QEMUFile *f) |
568 | a672b469 | aliguori | { |
569 | a672b469 | aliguori | uint64_t v; |
570 | a672b469 | aliguori | v = (uint64_t)qemu_get_be32(f) << 32;
|
571 | a672b469 | aliguori | v |= qemu_get_be32(f); |
572 | a672b469 | aliguori | return v;
|
573 | a672b469 | aliguori | } |
574 | a672b469 | aliguori | |
575 | a672b469 | aliguori | typedef struct SaveStateEntry { |
576 | a672b469 | aliguori | char idstr[256]; |
577 | a672b469 | aliguori | int instance_id;
|
578 | a672b469 | aliguori | int version_id;
|
579 | a672b469 | aliguori | int section_id;
|
580 | a672b469 | aliguori | SaveLiveStateHandler *save_live_state; |
581 | a672b469 | aliguori | SaveStateHandler *save_state; |
582 | a672b469 | aliguori | LoadStateHandler *load_state; |
583 | a672b469 | aliguori | void *opaque;
|
584 | a672b469 | aliguori | struct SaveStateEntry *next;
|
585 | a672b469 | aliguori | } SaveStateEntry; |
586 | a672b469 | aliguori | |
587 | a672b469 | aliguori | static SaveStateEntry *first_se;
|
588 | a672b469 | aliguori | |
589 | a672b469 | aliguori | /* TODO: Individual devices generally have very little idea about the rest
|
590 | a672b469 | aliguori | of the system, so instance_id should be removed/replaced.
|
591 | a672b469 | aliguori | Meanwhile pass -1 as instance_id if you do not already have a clearly
|
592 | a672b469 | aliguori | distinguishing id for all instances of your device class. */
|
593 | a672b469 | aliguori | int register_savevm_live(const char *idstr, |
594 | a672b469 | aliguori | int instance_id,
|
595 | a672b469 | aliguori | int version_id,
|
596 | a672b469 | aliguori | SaveLiveStateHandler *save_live_state, |
597 | a672b469 | aliguori | SaveStateHandler *save_state, |
598 | a672b469 | aliguori | LoadStateHandler *load_state, |
599 | a672b469 | aliguori | void *opaque)
|
600 | a672b469 | aliguori | { |
601 | a672b469 | aliguori | SaveStateEntry *se, **pse; |
602 | a672b469 | aliguori | static int global_section_id; |
603 | a672b469 | aliguori | |
604 | a672b469 | aliguori | se = qemu_malloc(sizeof(SaveStateEntry));
|
605 | a672b469 | aliguori | pstrcpy(se->idstr, sizeof(se->idstr), idstr);
|
606 | a672b469 | aliguori | se->instance_id = (instance_id == -1) ? 0 : instance_id; |
607 | a672b469 | aliguori | se->version_id = version_id; |
608 | a672b469 | aliguori | se->section_id = global_section_id++; |
609 | a672b469 | aliguori | se->save_live_state = save_live_state; |
610 | a672b469 | aliguori | se->save_state = save_state; |
611 | a672b469 | aliguori | se->load_state = load_state; |
612 | a672b469 | aliguori | se->opaque = opaque; |
613 | a672b469 | aliguori | se->next = NULL;
|
614 | a672b469 | aliguori | |
615 | a672b469 | aliguori | /* add at the end of list */
|
616 | a672b469 | aliguori | pse = &first_se; |
617 | a672b469 | aliguori | while (*pse != NULL) { |
618 | a672b469 | aliguori | if (instance_id == -1 |
619 | a672b469 | aliguori | && strcmp(se->idstr, (*pse)->idstr) == 0
|
620 | a672b469 | aliguori | && se->instance_id <= (*pse)->instance_id) |
621 | a672b469 | aliguori | se->instance_id = (*pse)->instance_id + 1;
|
622 | a672b469 | aliguori | pse = &(*pse)->next; |
623 | a672b469 | aliguori | } |
624 | a672b469 | aliguori | *pse = se; |
625 | a672b469 | aliguori | return 0; |
626 | a672b469 | aliguori | } |
627 | a672b469 | aliguori | |
628 | a672b469 | aliguori | int register_savevm(const char *idstr, |
629 | a672b469 | aliguori | int instance_id,
|
630 | a672b469 | aliguori | int version_id,
|
631 | a672b469 | aliguori | SaveStateHandler *save_state, |
632 | a672b469 | aliguori | LoadStateHandler *load_state, |
633 | a672b469 | aliguori | void *opaque)
|
634 | a672b469 | aliguori | { |
635 | a672b469 | aliguori | return register_savevm_live(idstr, instance_id, version_id,
|
636 | a672b469 | aliguori | NULL, save_state, load_state, opaque);
|
637 | a672b469 | aliguori | } |
638 | a672b469 | aliguori | |
639 | a672b469 | aliguori | #define QEMU_VM_FILE_MAGIC 0x5145564d |
640 | a672b469 | aliguori | #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002 |
641 | a672b469 | aliguori | #define QEMU_VM_FILE_VERSION 0x00000003 |
642 | a672b469 | aliguori | |
643 | a672b469 | aliguori | #define QEMU_VM_EOF 0x00 |
644 | a672b469 | aliguori | #define QEMU_VM_SECTION_START 0x01 |
645 | a672b469 | aliguori | #define QEMU_VM_SECTION_PART 0x02 |
646 | a672b469 | aliguori | #define QEMU_VM_SECTION_END 0x03 |
647 | a672b469 | aliguori | #define QEMU_VM_SECTION_FULL 0x04 |
648 | a672b469 | aliguori | |
649 | a672b469 | aliguori | int qemu_savevm_state_begin(QEMUFile *f)
|
650 | a672b469 | aliguori | { |
651 | a672b469 | aliguori | SaveStateEntry *se; |
652 | a672b469 | aliguori | |
653 | a672b469 | aliguori | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
654 | a672b469 | aliguori | qemu_put_be32(f, QEMU_VM_FILE_VERSION); |
655 | a672b469 | aliguori | |
656 | a672b469 | aliguori | for (se = first_se; se != NULL; se = se->next) { |
657 | a672b469 | aliguori | int len;
|
658 | a672b469 | aliguori | |
659 | a672b469 | aliguori | if (se->save_live_state == NULL) |
660 | a672b469 | aliguori | continue;
|
661 | a672b469 | aliguori | |
662 | a672b469 | aliguori | /* Section type */
|
663 | a672b469 | aliguori | qemu_put_byte(f, QEMU_VM_SECTION_START); |
664 | a672b469 | aliguori | qemu_put_be32(f, se->section_id); |
665 | a672b469 | aliguori | |
666 | a672b469 | aliguori | /* ID string */
|
667 | a672b469 | aliguori | len = strlen(se->idstr); |
668 | a672b469 | aliguori | qemu_put_byte(f, len); |
669 | a672b469 | aliguori | qemu_put_buffer(f, (uint8_t *)se->idstr, len); |
670 | a672b469 | aliguori | |
671 | a672b469 | aliguori | qemu_put_be32(f, se->instance_id); |
672 | a672b469 | aliguori | qemu_put_be32(f, se->version_id); |
673 | a672b469 | aliguori | |
674 | a672b469 | aliguori | se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque); |
675 | a672b469 | aliguori | } |
676 | a672b469 | aliguori | |
677 | a672b469 | aliguori | if (qemu_file_has_error(f))
|
678 | a672b469 | aliguori | return -EIO;
|
679 | a672b469 | aliguori | |
680 | a672b469 | aliguori | return 0; |
681 | a672b469 | aliguori | } |
682 | a672b469 | aliguori | |
683 | a672b469 | aliguori | int qemu_savevm_state_iterate(QEMUFile *f)
|
684 | a672b469 | aliguori | { |
685 | a672b469 | aliguori | SaveStateEntry *se; |
686 | a672b469 | aliguori | int ret = 1; |
687 | a672b469 | aliguori | |
688 | a672b469 | aliguori | for (se = first_se; se != NULL; se = se->next) { |
689 | a672b469 | aliguori | if (se->save_live_state == NULL) |
690 | a672b469 | aliguori | continue;
|
691 | a672b469 | aliguori | |
692 | a672b469 | aliguori | /* Section type */
|
693 | a672b469 | aliguori | qemu_put_byte(f, QEMU_VM_SECTION_PART); |
694 | a672b469 | aliguori | qemu_put_be32(f, se->section_id); |
695 | a672b469 | aliguori | |
696 | a672b469 | aliguori | ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque); |
697 | a672b469 | aliguori | } |
698 | a672b469 | aliguori | |
699 | a672b469 | aliguori | if (ret)
|
700 | a672b469 | aliguori | return 1; |
701 | a672b469 | aliguori | |
702 | a672b469 | aliguori | if (qemu_file_has_error(f))
|
703 | a672b469 | aliguori | return -EIO;
|
704 | a672b469 | aliguori | |
705 | a672b469 | aliguori | return 0; |
706 | a672b469 | aliguori | } |
707 | a672b469 | aliguori | |
708 | a672b469 | aliguori | int qemu_savevm_state_complete(QEMUFile *f)
|
709 | a672b469 | aliguori | { |
710 | a672b469 | aliguori | SaveStateEntry *se; |
711 | a672b469 | aliguori | |
712 | a672b469 | aliguori | for (se = first_se; se != NULL; se = se->next) { |
713 | a672b469 | aliguori | if (se->save_live_state == NULL) |
714 | a672b469 | aliguori | continue;
|
715 | a672b469 | aliguori | |
716 | a672b469 | aliguori | /* Section type */
|
717 | a672b469 | aliguori | qemu_put_byte(f, QEMU_VM_SECTION_END); |
718 | a672b469 | aliguori | qemu_put_be32(f, se->section_id); |
719 | a672b469 | aliguori | |
720 | a672b469 | aliguori | se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque); |
721 | a672b469 | aliguori | } |
722 | a672b469 | aliguori | |
723 | a672b469 | aliguori | for(se = first_se; se != NULL; se = se->next) { |
724 | a672b469 | aliguori | int len;
|
725 | a672b469 | aliguori | |
726 | a672b469 | aliguori | if (se->save_state == NULL) |
727 | a672b469 | aliguori | continue;
|
728 | a672b469 | aliguori | |
729 | a672b469 | aliguori | /* Section type */
|
730 | a672b469 | aliguori | qemu_put_byte(f, QEMU_VM_SECTION_FULL); |
731 | a672b469 | aliguori | qemu_put_be32(f, se->section_id); |
732 | a672b469 | aliguori | |
733 | a672b469 | aliguori | /* ID string */
|
734 | a672b469 | aliguori | len = strlen(se->idstr); |
735 | a672b469 | aliguori | qemu_put_byte(f, len); |
736 | a672b469 | aliguori | qemu_put_buffer(f, (uint8_t *)se->idstr, len); |
737 | a672b469 | aliguori | |
738 | a672b469 | aliguori | qemu_put_be32(f, se->instance_id); |
739 | a672b469 | aliguori | qemu_put_be32(f, se->version_id); |
740 | a672b469 | aliguori | |
741 | a672b469 | aliguori | se->save_state(f, se->opaque); |
742 | a672b469 | aliguori | } |
743 | a672b469 | aliguori | |
744 | a672b469 | aliguori | qemu_put_byte(f, QEMU_VM_EOF); |
745 | a672b469 | aliguori | |
746 | a672b469 | aliguori | if (qemu_file_has_error(f))
|
747 | a672b469 | aliguori | return -EIO;
|
748 | a672b469 | aliguori | |
749 | a672b469 | aliguori | return 0; |
750 | a672b469 | aliguori | } |
751 | a672b469 | aliguori | |
752 | a672b469 | aliguori | int qemu_savevm_state(QEMUFile *f)
|
753 | a672b469 | aliguori | { |
754 | a672b469 | aliguori | int saved_vm_running;
|
755 | a672b469 | aliguori | int ret;
|
756 | a672b469 | aliguori | |
757 | a672b469 | aliguori | saved_vm_running = vm_running; |
758 | a672b469 | aliguori | vm_stop(0);
|
759 | a672b469 | aliguori | |
760 | a672b469 | aliguori | bdrv_flush_all(); |
761 | a672b469 | aliguori | |
762 | a672b469 | aliguori | ret = qemu_savevm_state_begin(f); |
763 | a672b469 | aliguori | if (ret < 0) |
764 | a672b469 | aliguori | goto out;
|
765 | a672b469 | aliguori | |
766 | a672b469 | aliguori | do {
|
767 | a672b469 | aliguori | ret = qemu_savevm_state_iterate(f); |
768 | a672b469 | aliguori | if (ret < 0) |
769 | a672b469 | aliguori | goto out;
|
770 | a672b469 | aliguori | } while (ret == 0); |
771 | a672b469 | aliguori | |
772 | a672b469 | aliguori | ret = qemu_savevm_state_complete(f); |
773 | a672b469 | aliguori | |
774 | a672b469 | aliguori | out:
|
775 | a672b469 | aliguori | if (qemu_file_has_error(f))
|
776 | a672b469 | aliguori | ret = -EIO; |
777 | a672b469 | aliguori | |
778 | a672b469 | aliguori | if (!ret && saved_vm_running)
|
779 | a672b469 | aliguori | vm_start(); |
780 | a672b469 | aliguori | |
781 | a672b469 | aliguori | return ret;
|
782 | a672b469 | aliguori | } |
783 | a672b469 | aliguori | |
784 | a672b469 | aliguori | static SaveStateEntry *find_se(const char *idstr, int instance_id) |
785 | a672b469 | aliguori | { |
786 | a672b469 | aliguori | SaveStateEntry *se; |
787 | a672b469 | aliguori | |
788 | a672b469 | aliguori | for(se = first_se; se != NULL; se = se->next) { |
789 | a672b469 | aliguori | if (!strcmp(se->idstr, idstr) &&
|
790 | a672b469 | aliguori | instance_id == se->instance_id) |
791 | a672b469 | aliguori | return se;
|
792 | a672b469 | aliguori | } |
793 | a672b469 | aliguori | return NULL; |
794 | a672b469 | aliguori | } |
795 | a672b469 | aliguori | |
796 | a672b469 | aliguori | typedef struct LoadStateEntry { |
797 | a672b469 | aliguori | SaveStateEntry *se; |
798 | a672b469 | aliguori | int section_id;
|
799 | a672b469 | aliguori | int version_id;
|
800 | a672b469 | aliguori | struct LoadStateEntry *next;
|
801 | a672b469 | aliguori | } LoadStateEntry; |
802 | a672b469 | aliguori | |
803 | a672b469 | aliguori | static int qemu_loadvm_state_v2(QEMUFile *f) |
804 | a672b469 | aliguori | { |
805 | a672b469 | aliguori | SaveStateEntry *se; |
806 | a672b469 | aliguori | int len, ret, instance_id, record_len, version_id;
|
807 | a672b469 | aliguori | int64_t total_len, end_pos, cur_pos; |
808 | a672b469 | aliguori | char idstr[256]; |
809 | a672b469 | aliguori | |
810 | a672b469 | aliguori | total_len = qemu_get_be64(f); |
811 | a672b469 | aliguori | end_pos = total_len + qemu_ftell(f); |
812 | a672b469 | aliguori | for(;;) {
|
813 | a672b469 | aliguori | if (qemu_ftell(f) >= end_pos)
|
814 | a672b469 | aliguori | break;
|
815 | a672b469 | aliguori | len = qemu_get_byte(f); |
816 | a672b469 | aliguori | qemu_get_buffer(f, (uint8_t *)idstr, len); |
817 | a672b469 | aliguori | idstr[len] = '\0';
|
818 | a672b469 | aliguori | instance_id = qemu_get_be32(f); |
819 | a672b469 | aliguori | version_id = qemu_get_be32(f); |
820 | a672b469 | aliguori | record_len = qemu_get_be32(f); |
821 | a672b469 | aliguori | cur_pos = qemu_ftell(f); |
822 | a672b469 | aliguori | se = find_se(idstr, instance_id); |
823 | a672b469 | aliguori | if (!se) {
|
824 | a672b469 | aliguori | fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
|
825 | a672b469 | aliguori | instance_id, idstr); |
826 | a672b469 | aliguori | } else {
|
827 | a672b469 | aliguori | ret = se->load_state(f, se->opaque, version_id); |
828 | a672b469 | aliguori | if (ret < 0) { |
829 | a672b469 | aliguori | fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
|
830 | a672b469 | aliguori | instance_id, idstr); |
831 | a672b469 | aliguori | } |
832 | a672b469 | aliguori | } |
833 | a672b469 | aliguori | /* always seek to exact end of record */
|
834 | a672b469 | aliguori | qemu_fseek(f, cur_pos + record_len, SEEK_SET); |
835 | a672b469 | aliguori | } |
836 | a672b469 | aliguori | |
837 | a672b469 | aliguori | if (qemu_file_has_error(f))
|
838 | a672b469 | aliguori | return -EIO;
|
839 | a672b469 | aliguori | |
840 | a672b469 | aliguori | return 0; |
841 | a672b469 | aliguori | } |
842 | a672b469 | aliguori | |
843 | a672b469 | aliguori | int qemu_loadvm_state(QEMUFile *f)
|
844 | a672b469 | aliguori | { |
845 | a672b469 | aliguori | LoadStateEntry *first_le = NULL;
|
846 | a672b469 | aliguori | uint8_t section_type; |
847 | a672b469 | aliguori | unsigned int v; |
848 | a672b469 | aliguori | int ret;
|
849 | a672b469 | aliguori | |
850 | a672b469 | aliguori | v = qemu_get_be32(f); |
851 | a672b469 | aliguori | if (v != QEMU_VM_FILE_MAGIC)
|
852 | a672b469 | aliguori | return -EINVAL;
|
853 | a672b469 | aliguori | |
854 | a672b469 | aliguori | v = qemu_get_be32(f); |
855 | a672b469 | aliguori | if (v == QEMU_VM_FILE_VERSION_COMPAT)
|
856 | a672b469 | aliguori | return qemu_loadvm_state_v2(f);
|
857 | a672b469 | aliguori | if (v != QEMU_VM_FILE_VERSION)
|
858 | a672b469 | aliguori | return -ENOTSUP;
|
859 | a672b469 | aliguori | |
860 | a672b469 | aliguori | while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
|
861 | a672b469 | aliguori | uint32_t instance_id, version_id, section_id; |
862 | a672b469 | aliguori | LoadStateEntry *le; |
863 | a672b469 | aliguori | SaveStateEntry *se; |
864 | a672b469 | aliguori | char idstr[257]; |
865 | a672b469 | aliguori | int len;
|
866 | a672b469 | aliguori | |
867 | a672b469 | aliguori | switch (section_type) {
|
868 | a672b469 | aliguori | case QEMU_VM_SECTION_START:
|
869 | a672b469 | aliguori | case QEMU_VM_SECTION_FULL:
|
870 | a672b469 | aliguori | /* Read section start */
|
871 | a672b469 | aliguori | section_id = qemu_get_be32(f); |
872 | a672b469 | aliguori | len = qemu_get_byte(f); |
873 | a672b469 | aliguori | qemu_get_buffer(f, (uint8_t *)idstr, len); |
874 | a672b469 | aliguori | idstr[len] = 0;
|
875 | a672b469 | aliguori | instance_id = qemu_get_be32(f); |
876 | a672b469 | aliguori | version_id = qemu_get_be32(f); |
877 | a672b469 | aliguori | |
878 | a672b469 | aliguori | /* Find savevm section */
|
879 | a672b469 | aliguori | se = find_se(idstr, instance_id); |
880 | a672b469 | aliguori | if (se == NULL) { |
881 | a672b469 | aliguori | fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
|
882 | a672b469 | aliguori | ret = -EINVAL; |
883 | a672b469 | aliguori | goto out;
|
884 | a672b469 | aliguori | } |
885 | a672b469 | aliguori | |
886 | a672b469 | aliguori | /* Validate version */
|
887 | a672b469 | aliguori | if (version_id > se->version_id) {
|
888 | a672b469 | aliguori | fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
|
889 | a672b469 | aliguori | version_id, idstr, se->version_id); |
890 | a672b469 | aliguori | ret = -EINVAL; |
891 | a672b469 | aliguori | goto out;
|
892 | a672b469 | aliguori | } |
893 | a672b469 | aliguori | |
894 | a672b469 | aliguori | /* Add entry */
|
895 | a672b469 | aliguori | le = qemu_mallocz(sizeof(*le));
|
896 | a672b469 | aliguori | |
897 | a672b469 | aliguori | le->se = se; |
898 | a672b469 | aliguori | le->section_id = section_id; |
899 | a672b469 | aliguori | le->version_id = version_id; |
900 | a672b469 | aliguori | le->next = first_le; |
901 | a672b469 | aliguori | first_le = le; |
902 | a672b469 | aliguori | |
903 | a672b469 | aliguori | le->se->load_state(f, le->se->opaque, le->version_id); |
904 | a672b469 | aliguori | break;
|
905 | a672b469 | aliguori | case QEMU_VM_SECTION_PART:
|
906 | a672b469 | aliguori | case QEMU_VM_SECTION_END:
|
907 | a672b469 | aliguori | section_id = qemu_get_be32(f); |
908 | a672b469 | aliguori | |
909 | a672b469 | aliguori | for (le = first_le; le && le->section_id != section_id; le = le->next);
|
910 | a672b469 | aliguori | if (le == NULL) { |
911 | a672b469 | aliguori | fprintf(stderr, "Unknown savevm section %d\n", section_id);
|
912 | a672b469 | aliguori | ret = -EINVAL; |
913 | a672b469 | aliguori | goto out;
|
914 | a672b469 | aliguori | } |
915 | a672b469 | aliguori | |
916 | a672b469 | aliguori | le->se->load_state(f, le->se->opaque, le->version_id); |
917 | a672b469 | aliguori | break;
|
918 | a672b469 | aliguori | default:
|
919 | a672b469 | aliguori | fprintf(stderr, "Unknown savevm section type %d\n", section_type);
|
920 | a672b469 | aliguori | ret = -EINVAL; |
921 | a672b469 | aliguori | goto out;
|
922 | a672b469 | aliguori | } |
923 | a672b469 | aliguori | } |
924 | a672b469 | aliguori | |
925 | a672b469 | aliguori | ret = 0;
|
926 | a672b469 | aliguori | |
927 | a672b469 | aliguori | out:
|
928 | a672b469 | aliguori | while (first_le) {
|
929 | a672b469 | aliguori | LoadStateEntry *le = first_le; |
930 | a672b469 | aliguori | first_le = first_le->next; |
931 | a672b469 | aliguori | qemu_free(le); |
932 | a672b469 | aliguori | } |
933 | a672b469 | aliguori | |
934 | a672b469 | aliguori | if (qemu_file_has_error(f))
|
935 | a672b469 | aliguori | ret = -EIO; |
936 | a672b469 | aliguori | |
937 | a672b469 | aliguori | return ret;
|
938 | a672b469 | aliguori | } |
939 | a672b469 | aliguori | |
940 | a672b469 | aliguori | /* device can contain snapshots */
|
941 | a672b469 | aliguori | static int bdrv_can_snapshot(BlockDriverState *bs) |
942 | a672b469 | aliguori | { |
943 | a672b469 | aliguori | return (bs &&
|
944 | a672b469 | aliguori | !bdrv_is_removable(bs) && |
945 | a672b469 | aliguori | !bdrv_is_read_only(bs)); |
946 | a672b469 | aliguori | } |
947 | a672b469 | aliguori | |
948 | a672b469 | aliguori | /* device must be snapshots in order to have a reliable snapshot */
|
949 | a672b469 | aliguori | static int bdrv_has_snapshot(BlockDriverState *bs) |
950 | a672b469 | aliguori | { |
951 | a672b469 | aliguori | return (bs &&
|
952 | a672b469 | aliguori | !bdrv_is_removable(bs) && |
953 | a672b469 | aliguori | !bdrv_is_read_only(bs)); |
954 | a672b469 | aliguori | } |
955 | a672b469 | aliguori | |
956 | a672b469 | aliguori | static BlockDriverState *get_bs_snapshots(void) |
957 | a672b469 | aliguori | { |
958 | a672b469 | aliguori | BlockDriverState *bs; |
959 | a672b469 | aliguori | int i;
|
960 | a672b469 | aliguori | |
961 | a672b469 | aliguori | if (bs_snapshots)
|
962 | a672b469 | aliguori | return bs_snapshots;
|
963 | a672b469 | aliguori | for(i = 0; i <= nb_drives; i++) { |
964 | a672b469 | aliguori | bs = drives_table[i].bdrv; |
965 | a672b469 | aliguori | if (bdrv_can_snapshot(bs))
|
966 | a672b469 | aliguori | goto ok;
|
967 | a672b469 | aliguori | } |
968 | a672b469 | aliguori | return NULL; |
969 | a672b469 | aliguori | ok:
|
970 | a672b469 | aliguori | bs_snapshots = bs; |
971 | a672b469 | aliguori | return bs;
|
972 | a672b469 | aliguori | } |
973 | a672b469 | aliguori | |
974 | a672b469 | aliguori | static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, |
975 | a672b469 | aliguori | const char *name) |
976 | a672b469 | aliguori | { |
977 | a672b469 | aliguori | QEMUSnapshotInfo *sn_tab, *sn; |
978 | a672b469 | aliguori | int nb_sns, i, ret;
|
979 | a672b469 | aliguori | |
980 | a672b469 | aliguori | ret = -ENOENT; |
981 | a672b469 | aliguori | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
982 | a672b469 | aliguori | if (nb_sns < 0) |
983 | a672b469 | aliguori | return ret;
|
984 | a672b469 | aliguori | for(i = 0; i < nb_sns; i++) { |
985 | a672b469 | aliguori | sn = &sn_tab[i]; |
986 | a672b469 | aliguori | if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
|
987 | a672b469 | aliguori | *sn_info = *sn; |
988 | a672b469 | aliguori | ret = 0;
|
989 | a672b469 | aliguori | break;
|
990 | a672b469 | aliguori | } |
991 | a672b469 | aliguori | } |
992 | a672b469 | aliguori | qemu_free(sn_tab); |
993 | a672b469 | aliguori | return ret;
|
994 | a672b469 | aliguori | } |
995 | a672b469 | aliguori | |
996 | a672b469 | aliguori | void do_savevm(const char *name) |
997 | a672b469 | aliguori | { |
998 | a672b469 | aliguori | BlockDriverState *bs, *bs1; |
999 | a672b469 | aliguori | QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; |
1000 | a672b469 | aliguori | int must_delete, ret, i;
|
1001 | a672b469 | aliguori | BlockDriverInfo bdi1, *bdi = &bdi1; |
1002 | a672b469 | aliguori | QEMUFile *f; |
1003 | a672b469 | aliguori | int saved_vm_running;
|
1004 | 2d22b18f | aliguori | uint32_t vm_state_size; |
1005 | a672b469 | aliguori | #ifdef _WIN32
|
1006 | a672b469 | aliguori | struct _timeb tb;
|
1007 | a672b469 | aliguori | #else
|
1008 | a672b469 | aliguori | struct timeval tv;
|
1009 | a672b469 | aliguori | #endif
|
1010 | a672b469 | aliguori | |
1011 | a672b469 | aliguori | bs = get_bs_snapshots(); |
1012 | a672b469 | aliguori | if (!bs) {
|
1013 | a672b469 | aliguori | term_printf("No block device can accept snapshots\n");
|
1014 | a672b469 | aliguori | return;
|
1015 | a672b469 | aliguori | } |
1016 | a672b469 | aliguori | |
1017 | a672b469 | aliguori | /* ??? Should this occur after vm_stop? */
|
1018 | a672b469 | aliguori | qemu_aio_flush(); |
1019 | a672b469 | aliguori | |
1020 | a672b469 | aliguori | saved_vm_running = vm_running; |
1021 | a672b469 | aliguori | vm_stop(0);
|
1022 | a672b469 | aliguori | |
1023 | a672b469 | aliguori | must_delete = 0;
|
1024 | a672b469 | aliguori | if (name) {
|
1025 | a672b469 | aliguori | ret = bdrv_snapshot_find(bs, old_sn, name); |
1026 | a672b469 | aliguori | if (ret >= 0) { |
1027 | a672b469 | aliguori | must_delete = 1;
|
1028 | a672b469 | aliguori | } |
1029 | a672b469 | aliguori | } |
1030 | a672b469 | aliguori | memset(sn, 0, sizeof(*sn)); |
1031 | a672b469 | aliguori | if (must_delete) {
|
1032 | a672b469 | aliguori | pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
|
1033 | a672b469 | aliguori | pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
|
1034 | a672b469 | aliguori | } else {
|
1035 | a672b469 | aliguori | if (name)
|
1036 | a672b469 | aliguori | pstrcpy(sn->name, sizeof(sn->name), name);
|
1037 | a672b469 | aliguori | } |
1038 | a672b469 | aliguori | |
1039 | a672b469 | aliguori | /* fill auxiliary fields */
|
1040 | a672b469 | aliguori | #ifdef _WIN32
|
1041 | a672b469 | aliguori | _ftime(&tb); |
1042 | a672b469 | aliguori | sn->date_sec = tb.time; |
1043 | a672b469 | aliguori | sn->date_nsec = tb.millitm * 1000000;
|
1044 | a672b469 | aliguori | #else
|
1045 | a672b469 | aliguori | gettimeofday(&tv, NULL);
|
1046 | a672b469 | aliguori | sn->date_sec = tv.tv_sec; |
1047 | a672b469 | aliguori | sn->date_nsec = tv.tv_usec * 1000;
|
1048 | a672b469 | aliguori | #endif
|
1049 | a672b469 | aliguori | sn->vm_clock_nsec = qemu_get_clock(vm_clock); |
1050 | a672b469 | aliguori | |
1051 | a672b469 | aliguori | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
1052 | a672b469 | aliguori | term_printf("Device %s does not support VM state snapshots\n",
|
1053 | a672b469 | aliguori | bdrv_get_device_name(bs)); |
1054 | a672b469 | aliguori | goto the_end;
|
1055 | a672b469 | aliguori | } |
1056 | a672b469 | aliguori | |
1057 | a672b469 | aliguori | /* save the VM state */
|
1058 | a672b469 | aliguori | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
|
1059 | a672b469 | aliguori | if (!f) {
|
1060 | a672b469 | aliguori | term_printf("Could not open VM state file\n");
|
1061 | a672b469 | aliguori | goto the_end;
|
1062 | a672b469 | aliguori | } |
1063 | a672b469 | aliguori | ret = qemu_savevm_state(f); |
1064 | 2d22b18f | aliguori | vm_state_size = qemu_ftell(f); |
1065 | a672b469 | aliguori | qemu_fclose(f); |
1066 | a672b469 | aliguori | if (ret < 0) { |
1067 | a672b469 | aliguori | term_printf("Error %d while writing VM\n", ret);
|
1068 | a672b469 | aliguori | goto the_end;
|
1069 | a672b469 | aliguori | } |
1070 | a672b469 | aliguori | |
1071 | a672b469 | aliguori | /* create the snapshots */
|
1072 | a672b469 | aliguori | |
1073 | a672b469 | aliguori | for(i = 0; i < nb_drives; i++) { |
1074 | a672b469 | aliguori | bs1 = drives_table[i].bdrv; |
1075 | a672b469 | aliguori | if (bdrv_has_snapshot(bs1)) {
|
1076 | a672b469 | aliguori | if (must_delete) {
|
1077 | a672b469 | aliguori | ret = bdrv_snapshot_delete(bs1, old_sn->id_str); |
1078 | a672b469 | aliguori | if (ret < 0) { |
1079 | a672b469 | aliguori | term_printf("Error while deleting snapshot on '%s'\n",
|
1080 | a672b469 | aliguori | bdrv_get_device_name(bs1)); |
1081 | a672b469 | aliguori | } |
1082 | a672b469 | aliguori | } |
1083 | 2d22b18f | aliguori | /* Write VM state size only to the image that contains the state */
|
1084 | 2d22b18f | aliguori | sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
|
1085 | a672b469 | aliguori | ret = bdrv_snapshot_create(bs1, sn); |
1086 | a672b469 | aliguori | if (ret < 0) { |
1087 | a672b469 | aliguori | term_printf("Error while creating snapshot on '%s'\n",
|
1088 | a672b469 | aliguori | bdrv_get_device_name(bs1)); |
1089 | a672b469 | aliguori | } |
1090 | a672b469 | aliguori | } |
1091 | a672b469 | aliguori | } |
1092 | a672b469 | aliguori | |
1093 | a672b469 | aliguori | the_end:
|
1094 | a672b469 | aliguori | if (saved_vm_running)
|
1095 | a672b469 | aliguori | vm_start(); |
1096 | a672b469 | aliguori | } |
1097 | a672b469 | aliguori | |
1098 | a672b469 | aliguori | void do_loadvm(const char *name) |
1099 | a672b469 | aliguori | { |
1100 | a672b469 | aliguori | BlockDriverState *bs, *bs1; |
1101 | a672b469 | aliguori | BlockDriverInfo bdi1, *bdi = &bdi1; |
1102 | 2d22b18f | aliguori | QEMUSnapshotInfo sn; |
1103 | a672b469 | aliguori | QEMUFile *f; |
1104 | a672b469 | aliguori | int i, ret;
|
1105 | a672b469 | aliguori | int saved_vm_running;
|
1106 | a672b469 | aliguori | |
1107 | a672b469 | aliguori | bs = get_bs_snapshots(); |
1108 | a672b469 | aliguori | if (!bs) {
|
1109 | a672b469 | aliguori | term_printf("No block device supports snapshots\n");
|
1110 | a672b469 | aliguori | return;
|
1111 | a672b469 | aliguori | } |
1112 | a672b469 | aliguori | |
1113 | a672b469 | aliguori | /* Flush all IO requests so they don't interfere with the new state. */
|
1114 | a672b469 | aliguori | qemu_aio_flush(); |
1115 | a672b469 | aliguori | |
1116 | a672b469 | aliguori | saved_vm_running = vm_running; |
1117 | a672b469 | aliguori | vm_stop(0);
|
1118 | a672b469 | aliguori | |
1119 | a672b469 | aliguori | for(i = 0; i <= nb_drives; i++) { |
1120 | a672b469 | aliguori | bs1 = drives_table[i].bdrv; |
1121 | a672b469 | aliguori | if (bdrv_has_snapshot(bs1)) {
|
1122 | a672b469 | aliguori | ret = bdrv_snapshot_goto(bs1, name); |
1123 | a672b469 | aliguori | if (ret < 0) { |
1124 | a672b469 | aliguori | if (bs != bs1)
|
1125 | a672b469 | aliguori | term_printf("Warning: ");
|
1126 | a672b469 | aliguori | switch(ret) {
|
1127 | a672b469 | aliguori | case -ENOTSUP:
|
1128 | a672b469 | aliguori | term_printf("Snapshots not supported on device '%s'\n",
|
1129 | a672b469 | aliguori | bdrv_get_device_name(bs1)); |
1130 | a672b469 | aliguori | break;
|
1131 | a672b469 | aliguori | case -ENOENT:
|
1132 | a672b469 | aliguori | term_printf("Could not find snapshot '%s' on device '%s'\n",
|
1133 | a672b469 | aliguori | name, bdrv_get_device_name(bs1)); |
1134 | a672b469 | aliguori | break;
|
1135 | a672b469 | aliguori | default:
|
1136 | a672b469 | aliguori | term_printf("Error %d while activating snapshot on '%s'\n",
|
1137 | a672b469 | aliguori | ret, bdrv_get_device_name(bs1)); |
1138 | a672b469 | aliguori | break;
|
1139 | a672b469 | aliguori | } |
1140 | a672b469 | aliguori | /* fatal on snapshot block device */
|
1141 | a672b469 | aliguori | if (bs == bs1)
|
1142 | a672b469 | aliguori | goto the_end;
|
1143 | a672b469 | aliguori | } |
1144 | a672b469 | aliguori | } |
1145 | a672b469 | aliguori | } |
1146 | a672b469 | aliguori | |
1147 | a672b469 | aliguori | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
1148 | a672b469 | aliguori | term_printf("Device %s does not support VM state snapshots\n",
|
1149 | a672b469 | aliguori | bdrv_get_device_name(bs)); |
1150 | a672b469 | aliguori | return;
|
1151 | a672b469 | aliguori | } |
1152 | a672b469 | aliguori | |
1153 | 2d22b18f | aliguori | /* Don't even try to load empty VM states */
|
1154 | 2d22b18f | aliguori | ret = bdrv_snapshot_find(bs, &sn, name); |
1155 | 2d22b18f | aliguori | if ((ret >= 0) && (sn.vm_state_size == 0)) |
1156 | 2d22b18f | aliguori | goto the_end;
|
1157 | 2d22b18f | aliguori | |
1158 | a672b469 | aliguori | /* restore the VM state */
|
1159 | a672b469 | aliguori | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
|
1160 | a672b469 | aliguori | if (!f) {
|
1161 | a672b469 | aliguori | term_printf("Could not open VM state file\n");
|
1162 | a672b469 | aliguori | goto the_end;
|
1163 | a672b469 | aliguori | } |
1164 | a672b469 | aliguori | ret = qemu_loadvm_state(f); |
1165 | a672b469 | aliguori | qemu_fclose(f); |
1166 | a672b469 | aliguori | if (ret < 0) { |
1167 | a672b469 | aliguori | term_printf("Error %d while loading VM state\n", ret);
|
1168 | a672b469 | aliguori | } |
1169 | a672b469 | aliguori | the_end:
|
1170 | a672b469 | aliguori | if (saved_vm_running)
|
1171 | a672b469 | aliguori | vm_start(); |
1172 | a672b469 | aliguori | } |
1173 | a672b469 | aliguori | |
1174 | a672b469 | aliguori | void do_delvm(const char *name) |
1175 | a672b469 | aliguori | { |
1176 | a672b469 | aliguori | BlockDriverState *bs, *bs1; |
1177 | a672b469 | aliguori | int i, ret;
|
1178 | a672b469 | aliguori | |
1179 | a672b469 | aliguori | bs = get_bs_snapshots(); |
1180 | a672b469 | aliguori | if (!bs) {
|
1181 | a672b469 | aliguori | term_printf("No block device supports snapshots\n");
|
1182 | a672b469 | aliguori | return;
|
1183 | a672b469 | aliguori | } |
1184 | a672b469 | aliguori | |
1185 | a672b469 | aliguori | for(i = 0; i <= nb_drives; i++) { |
1186 | a672b469 | aliguori | bs1 = drives_table[i].bdrv; |
1187 | a672b469 | aliguori | if (bdrv_has_snapshot(bs1)) {
|
1188 | a672b469 | aliguori | ret = bdrv_snapshot_delete(bs1, name); |
1189 | a672b469 | aliguori | if (ret < 0) { |
1190 | a672b469 | aliguori | if (ret == -ENOTSUP)
|
1191 | a672b469 | aliguori | term_printf("Snapshots not supported on device '%s'\n",
|
1192 | a672b469 | aliguori | bdrv_get_device_name(bs1)); |
1193 | a672b469 | aliguori | else
|
1194 | a672b469 | aliguori | term_printf("Error %d while deleting snapshot on '%s'\n",
|
1195 | a672b469 | aliguori | ret, bdrv_get_device_name(bs1)); |
1196 | a672b469 | aliguori | } |
1197 | a672b469 | aliguori | } |
1198 | a672b469 | aliguori | } |
1199 | a672b469 | aliguori | } |
1200 | a672b469 | aliguori | |
1201 | a672b469 | aliguori | void do_info_snapshots(void) |
1202 | a672b469 | aliguori | { |
1203 | a672b469 | aliguori | BlockDriverState *bs, *bs1; |
1204 | a672b469 | aliguori | QEMUSnapshotInfo *sn_tab, *sn; |
1205 | a672b469 | aliguori | int nb_sns, i;
|
1206 | a672b469 | aliguori | char buf[256]; |
1207 | a672b469 | aliguori | |
1208 | a672b469 | aliguori | bs = get_bs_snapshots(); |
1209 | a672b469 | aliguori | if (!bs) {
|
1210 | a672b469 | aliguori | term_printf("No available block device supports snapshots\n");
|
1211 | a672b469 | aliguori | return;
|
1212 | a672b469 | aliguori | } |
1213 | a672b469 | aliguori | term_printf("Snapshot devices:");
|
1214 | a672b469 | aliguori | for(i = 0; i <= nb_drives; i++) { |
1215 | a672b469 | aliguori | bs1 = drives_table[i].bdrv; |
1216 | a672b469 | aliguori | if (bdrv_has_snapshot(bs1)) {
|
1217 | a672b469 | aliguori | if (bs == bs1)
|
1218 | a672b469 | aliguori | term_printf(" %s", bdrv_get_device_name(bs1));
|
1219 | a672b469 | aliguori | } |
1220 | a672b469 | aliguori | } |
1221 | a672b469 | aliguori | term_printf("\n");
|
1222 | a672b469 | aliguori | |
1223 | a672b469 | aliguori | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
1224 | a672b469 | aliguori | if (nb_sns < 0) { |
1225 | a672b469 | aliguori | term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
|
1226 | a672b469 | aliguori | return;
|
1227 | a672b469 | aliguori | } |
1228 | a672b469 | aliguori | term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
|
1229 | a672b469 | aliguori | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); |
1230 | a672b469 | aliguori | for(i = 0; i < nb_sns; i++) { |
1231 | a672b469 | aliguori | sn = &sn_tab[i]; |
1232 | a672b469 | aliguori | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); |
1233 | a672b469 | aliguori | } |
1234 | a672b469 | aliguori | qemu_free(sn_tab); |
1235 | a672b469 | aliguori | } |