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