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