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