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