kvm: libkvm: adjust Makefile to new layout
[qemu-kvm/fedora.git] / savevm.c
blob5293dd551d3f5cdd3b17e272368c324d95bc8beb
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
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"
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>
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
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
90 /* point to the block driver where the snapshots are managed */
91 static BlockDriverState *bs_snapshots;
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
98 static int announce_self_create(uint8_t *buf,
99 uint8_t *mac_addr)
101 uint32_t magic = EXPERIMENTAL_MAGIC;
102 uint16_t proto = htons(ETH_P_EXPERIMENTAL);
104 /* FIXME: should we send a different packet (arp/rarp/ping)? */
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 */
111 return 18; /* len */
114 void qemu_announce_self(void)
116 int i, j, len;
117 VLANState *vlan;
118 VLANClientState *vc;
119 uint8_t buf[256];
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);
133 /***********************************************************/
134 /* savevm/loadvm support */
136 #define IO_BUF_SIZE 32768
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;
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];
152 int has_error;
155 typedef struct QEMUFilePopen
157 FILE *popen_file;
158 QEMUFile *file;
159 } QEMUFilePopen;
161 typedef struct QEMUFileSocket
163 int fd;
164 QEMUFile *file;
165 } QEMUFileSocket;
167 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
169 QEMUFileSocket *s = opaque;
170 ssize_t len;
172 do {
173 len = recv(s->fd, buf, size, 0);
174 } while (len == -1 && socket_error() == EINTR);
176 if (len == -1)
177 len = -socket_error();
179 return len;
182 static int socket_close(void *opaque)
184 QEMUFileSocket *s = opaque;
185 qemu_free(s);
186 return 0;
189 static int popen_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
191 QEMUFilePopen *s = opaque;
192 return fwrite(buf, 1, size, s->popen_file);
195 static int popen_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
197 QEMUFilePopen *s = opaque;
198 return fread(buf, 1, size, s->popen_file);
201 static int popen_close(void *opaque)
203 QEMUFilePopen *s = opaque;
204 pclose(s->popen_file);
205 qemu_free(s);
206 return 0;
209 QEMUFile *qemu_popen(FILE *popen_file, const char *mode)
211 QEMUFilePopen *s;
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;
218 s = qemu_mallocz(sizeof(QEMUFilePopen));
220 s->popen_file = popen_file;
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);
227 fprintf(stderr, "qemu_popen: returning result of qemu_fopen_ops\n");
228 return s->file;
231 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
233 FILE *popen_file;
235 popen_file = popen(command, mode);
236 if(popen_file == NULL) {
237 return NULL;
240 return qemu_popen(popen_file, mode);
243 QEMUFile *qemu_fopen_socket(int fd)
245 QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
247 s->fd = fd;
248 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, NULL);
249 return s->file;
252 typedef struct QEMUFileStdio
254 FILE *outfile;
255 } QEMUFileStdio;
257 static int file_put_buffer(void *opaque, const uint8_t *buf,
258 int64_t pos, int size)
260 QEMUFileStdio *s = opaque;
261 fseek(s->outfile, pos, SEEK_SET);
262 fwrite(buf, 1, size, s->outfile);
263 return size;
266 static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
268 QEMUFileStdio *s = opaque;
269 fseek(s->outfile, pos, SEEK_SET);
270 return fread(buf, 1, size, s->outfile);
273 static int file_close(void *opaque)
275 QEMUFileStdio *s = opaque;
276 fclose(s->outfile);
277 qemu_free(s);
278 return 0;
281 QEMUFile *qemu_fopen(const char *filename, const char *mode)
283 QEMUFileStdio *s;
285 s = qemu_mallocz(sizeof(QEMUFileStdio));
287 s->outfile = fopen(filename, mode);
288 if (!s->outfile)
289 goto fail;
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);
296 fail:
297 if (s->outfile)
298 fclose(s->outfile);
299 qemu_free(s);
300 return NULL;
303 typedef struct QEMUFileBdrv
305 BlockDriverState *bs;
306 int64_t base_offset;
307 } QEMUFileBdrv;
309 static int block_put_buffer(void *opaque, const uint8_t *buf,
310 int64_t pos, int size)
312 QEMUFileBdrv *s = opaque;
313 bdrv_put_buffer(s->bs, buf, s->base_offset + pos, size);
314 return size;
317 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
319 QEMUFileBdrv *s = opaque;
320 return bdrv_get_buffer(s->bs, buf, s->base_offset + pos, size);
323 static int bdrv_fclose(void *opaque)
325 QEMUFileBdrv *s = opaque;
326 qemu_free(s);
327 return 0;
330 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable)
332 QEMUFileBdrv *s;
334 s = qemu_mallocz(sizeof(QEMUFileBdrv));
336 s->bs = bs;
337 s->base_offset = offset;
339 if (is_writable)
340 return qemu_fopen_ops(s, block_put_buffer, NULL, bdrv_fclose, NULL);
342 return qemu_fopen_ops(s, NULL, block_get_buffer, bdrv_fclose, NULL);
345 QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
346 QEMUFileGetBufferFunc *get_buffer,
347 QEMUFileCloseFunc *close,
348 QEMUFileRateLimit *rate_limit)
350 QEMUFile *f;
352 f = qemu_mallocz(sizeof(QEMUFile));
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;
361 return f;
364 int qemu_file_has_error(QEMUFile *f)
366 return f->has_error;
369 void qemu_file_set_error(QEMUFile *f)
371 f->has_error = 1;
374 void qemu_fflush(QEMUFile *f)
376 if (!f->put_buffer)
377 return;
379 if (f->is_write && f->buf_index > 0) {
380 int len;
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;
391 static void qemu_fill_buffer(QEMUFile *f)
393 int len;
395 if (!f->get_buffer)
396 return;
398 if (f->is_write)
399 abort();
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;
410 int qemu_fclose(QEMUFile *f)
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;
420 void qemu_file_put_notify(QEMUFile *f)
422 f->put_buffer(f->opaque, NULL, 0, 0);
425 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
427 int l;
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();
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);
449 void qemu_put_byte(QEMUFile *f, int v)
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();
457 f->buf[f->buf_index++] = v;
458 f->is_write = 1;
459 if (f->buf_index >= IO_BUF_SIZE)
460 qemu_fflush(f);
463 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
465 int size, l;
467 if (f->is_write)
468 abort();
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;
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;
486 return size1 - size;
489 int qemu_get_byte(QEMUFile *f)
491 if (f->is_write)
492 abort();
494 if (f->buf_index >= f->buf_size) {
495 qemu_fill_buffer(f);
496 if (f->buf_index >= f->buf_size)
497 return 0;
499 return f->buf[f->buf_index++];
502 int64_t qemu_ftell(QEMUFile *f)
504 return f->buf_offset - f->buf_size + f->buf_index;
507 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
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;
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;
525 return pos;
528 int qemu_file_rate_limit(QEMUFile *f)
530 if (f->rate_limit)
531 return f->rate_limit(f->opaque);
533 return 0;
536 void qemu_put_be16(QEMUFile *f, unsigned int v)
538 qemu_put_byte(f, v >> 8);
539 qemu_put_byte(f, v);
542 void qemu_put_be32(QEMUFile *f, unsigned int v)
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);
550 void qemu_put_be64(QEMUFile *f, uint64_t v)
552 qemu_put_be32(f, v >> 32);
553 qemu_put_be32(f, v);
556 unsigned int qemu_get_be16(QEMUFile *f)
558 unsigned int v;
559 v = qemu_get_byte(f) << 8;
560 v |= qemu_get_byte(f);
561 return v;
564 unsigned int qemu_get_be32(QEMUFile *f)
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;
574 uint64_t qemu_get_be64(QEMUFile *f)
576 uint64_t v;
577 v = (uint64_t)qemu_get_be32(f) << 32;
578 v |= qemu_get_be32(f);
579 return v;
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;
594 static SaveStateEntry *first_se;
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)
608 SaveStateEntry *se, **pse;
609 static int global_section_id;
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;
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;
631 *pse = se;
632 return 0;
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)
642 return register_savevm_live(idstr, instance_id, version_id,
643 NULL, save_state, load_state, opaque);
646 #define QEMU_VM_FILE_MAGIC 0x5145564d
647 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
648 #define QEMU_VM_FILE_VERSION 0x00000003
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
656 int qemu_savevm_state_begin(QEMUFile *f)
658 SaveStateEntry *se;
660 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
661 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
663 for (se = first_se; se != NULL; se = se->next) {
664 int len;
666 if (se->save_live_state == NULL)
667 continue;
669 /* Section type */
670 qemu_put_byte(f, QEMU_VM_SECTION_START);
671 qemu_put_be32(f, se->section_id);
673 /* ID string */
674 len = strlen(se->idstr);
675 qemu_put_byte(f, len);
676 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
678 qemu_put_be32(f, se->instance_id);
679 qemu_put_be32(f, se->version_id);
681 se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
684 if (qemu_file_has_error(f))
685 return -EIO;
687 return 0;
690 int qemu_savevm_state_iterate(QEMUFile *f)
692 SaveStateEntry *se;
693 int ret = 1;
695 for (se = first_se; se != NULL; se = se->next) {
696 if (se->save_live_state == NULL)
697 continue;
699 /* Section type */
700 qemu_put_byte(f, QEMU_VM_SECTION_PART);
701 qemu_put_be32(f, se->section_id);
703 ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
706 if (ret)
707 return 1;
709 if (qemu_file_has_error(f))
710 return -EIO;
712 return 0;
715 int qemu_savevm_state_complete(QEMUFile *f)
717 SaveStateEntry *se;
719 for (se = first_se; se != NULL; se = se->next) {
720 if (se->save_live_state == NULL)
721 continue;
723 /* Section type */
724 qemu_put_byte(f, QEMU_VM_SECTION_END);
725 qemu_put_be32(f, se->section_id);
727 se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
730 for(se = first_se; se != NULL; se = se->next) {
731 int len;
733 if (se->save_state == NULL)
734 continue;
736 /* Section type */
737 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
738 qemu_put_be32(f, se->section_id);
740 /* ID string */
741 len = strlen(se->idstr);
742 qemu_put_byte(f, len);
743 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
745 qemu_put_be32(f, se->instance_id);
746 qemu_put_be32(f, se->version_id);
748 se->save_state(f, se->opaque);
751 qemu_put_byte(f, QEMU_VM_EOF);
753 if (qemu_file_has_error(f))
754 return -EIO;
756 return 0;
759 int qemu_savevm_state(QEMUFile *f)
761 int saved_vm_running;
762 int ret;
764 saved_vm_running = vm_running;
765 vm_stop(0);
767 bdrv_flush_all();
769 ret = qemu_savevm_state_begin(f);
770 if (ret < 0)
771 goto out;
773 do {
774 ret = qemu_savevm_state_iterate(f);
775 if (ret < 0)
776 goto out;
777 } while (ret == 0);
779 ret = qemu_savevm_state_complete(f);
781 out:
782 if (qemu_file_has_error(f))
783 ret = -EIO;
785 if (!ret && saved_vm_running)
786 vm_start();
788 return ret;
791 static SaveStateEntry *find_se(const char *idstr, int instance_id)
793 SaveStateEntry *se;
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;
800 return NULL;
803 typedef struct LoadStateEntry {
804 SaveStateEntry *se;
805 int section_id;
806 int version_id;
807 struct LoadStateEntry *next;
808 } LoadStateEntry;
810 static int qemu_loadvm_state_v2(QEMUFile *f)
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];
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 return ret;
841 /* always seek to exact end of record */
842 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
845 if (qemu_file_has_error(f))
846 return -EIO;
848 return 0;
851 int qemu_loadvm_state(QEMUFile *f)
853 LoadStateEntry *first_le = NULL;
854 uint8_t section_type;
855 unsigned int v;
856 int ret;
858 v = qemu_get_be32(f);
859 if (v != QEMU_VM_FILE_MAGIC)
860 return -EINVAL;
862 v = qemu_get_be32(f);
863 if (v == QEMU_VM_FILE_VERSION_COMPAT)
864 return qemu_loadvm_state_v2(f);
865 if (v != QEMU_VM_FILE_VERSION)
866 return -ENOTSUP;
868 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
869 uint32_t instance_id, version_id, section_id;
870 LoadStateEntry *le;
871 SaveStateEntry *se;
872 char idstr[257];
873 int len;
875 switch (section_type) {
876 case QEMU_VM_SECTION_START:
877 case QEMU_VM_SECTION_FULL:
878 /* Read section start */
879 section_id = qemu_get_be32(f);
880 len = qemu_get_byte(f);
881 qemu_get_buffer(f, (uint8_t *)idstr, len);
882 idstr[len] = 0;
883 instance_id = qemu_get_be32(f);
884 version_id = qemu_get_be32(f);
886 /* Find savevm section */
887 se = find_se(idstr, instance_id);
888 if (se == NULL) {
889 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
890 ret = -EINVAL;
891 goto out;
894 /* Validate version */
895 if (version_id > se->version_id) {
896 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
897 version_id, idstr, se->version_id);
898 ret = -EINVAL;
899 goto out;
902 /* Add entry */
903 le = qemu_mallocz(sizeof(*le));
905 le->se = se;
906 le->section_id = section_id;
907 le->version_id = version_id;
908 le->next = first_le;
909 first_le = le;
911 le->se->load_state(f, le->se->opaque, le->version_id);
912 break;
913 case QEMU_VM_SECTION_PART:
914 case QEMU_VM_SECTION_END:
915 section_id = qemu_get_be32(f);
917 for (le = first_le; le && le->section_id != section_id; le = le->next);
918 if (le == NULL) {
919 fprintf(stderr, "Unknown savevm section %d\n", section_id);
920 ret = -EINVAL;
921 goto out;
924 le->se->load_state(f, le->se->opaque, le->version_id);
925 break;
926 default:
927 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
928 ret = -EINVAL;
929 goto out;
933 ret = 0;
935 out:
936 while (first_le) {
937 LoadStateEntry *le = first_le;
938 first_le = first_le->next;
939 qemu_free(le);
942 if (qemu_file_has_error(f))
943 ret = -EIO;
945 return ret;
948 /* device can contain snapshots */
949 static int bdrv_can_snapshot(BlockDriverState *bs)
951 return (bs &&
952 !bdrv_is_removable(bs) &&
953 !bdrv_is_read_only(bs));
956 /* device must be snapshots in order to have a reliable snapshot */
957 static int bdrv_has_snapshot(BlockDriverState *bs)
959 return (bs &&
960 !bdrv_is_removable(bs) &&
961 !bdrv_is_read_only(bs));
964 static BlockDriverState *get_bs_snapshots(void)
966 BlockDriverState *bs;
967 int i;
969 if (bs_snapshots)
970 return bs_snapshots;
971 for(i = 0; i <= nb_drives; i++) {
972 bs = drives_table[i].bdrv;
973 if (bdrv_can_snapshot(bs))
974 goto ok;
976 return NULL;
978 bs_snapshots = bs;
979 return bs;
982 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
983 const char *name)
985 QEMUSnapshotInfo *sn_tab, *sn;
986 int nb_sns, i, ret;
988 ret = -ENOENT;
989 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
990 if (nb_sns < 0)
991 return ret;
992 for(i = 0; i < nb_sns; i++) {
993 sn = &sn_tab[i];
994 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
995 *sn_info = *sn;
996 ret = 0;
997 break;
1000 qemu_free(sn_tab);
1001 return ret;
1004 void do_savevm(const char *name)
1006 BlockDriverState *bs, *bs1;
1007 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
1008 int must_delete, ret, i;
1009 BlockDriverInfo bdi1, *bdi = &bdi1;
1010 QEMUFile *f;
1011 int saved_vm_running;
1012 uint32_t vm_state_size;
1013 #ifdef _WIN32
1014 struct _timeb tb;
1015 #else
1016 struct timeval tv;
1017 #endif
1019 bs = get_bs_snapshots();
1020 if (!bs) {
1021 term_printf("No block device can accept snapshots\n");
1022 return;
1025 /* ??? Should this occur after vm_stop? */
1026 qemu_aio_flush();
1028 saved_vm_running = vm_running;
1029 vm_stop(0);
1031 must_delete = 0;
1032 if (name) {
1033 ret = bdrv_snapshot_find(bs, old_sn, name);
1034 if (ret >= 0) {
1035 must_delete = 1;
1038 memset(sn, 0, sizeof(*sn));
1039 if (must_delete) {
1040 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1041 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1042 } else {
1043 if (name)
1044 pstrcpy(sn->name, sizeof(sn->name), name);
1047 /* fill auxiliary fields */
1048 #ifdef _WIN32
1049 _ftime(&tb);
1050 sn->date_sec = tb.time;
1051 sn->date_nsec = tb.millitm * 1000000;
1052 #else
1053 gettimeofday(&tv, NULL);
1054 sn->date_sec = tv.tv_sec;
1055 sn->date_nsec = tv.tv_usec * 1000;
1056 #endif
1057 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1059 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
1060 term_printf("Device %s does not support VM state snapshots\n",
1061 bdrv_get_device_name(bs));
1062 goto the_end;
1065 /* save the VM state */
1066 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
1067 if (!f) {
1068 term_printf("Could not open VM state file\n");
1069 goto the_end;
1071 ret = qemu_savevm_state(f);
1072 vm_state_size = qemu_ftell(f);
1073 qemu_fclose(f);
1074 if (ret < 0) {
1075 term_printf("Error %d while writing VM\n", ret);
1076 goto the_end;
1079 /* create the snapshots */
1081 for(i = 0; i < nb_drives; i++) {
1082 bs1 = drives_table[i].bdrv;
1083 if (bdrv_has_snapshot(bs1)) {
1084 if (must_delete) {
1085 ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
1086 if (ret < 0) {
1087 term_printf("Error while deleting snapshot on '%s'\n",
1088 bdrv_get_device_name(bs1));
1091 /* Write VM state size only to the image that contains the state */
1092 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
1093 ret = bdrv_snapshot_create(bs1, sn);
1094 if (ret < 0) {
1095 term_printf("Error while creating snapshot on '%s'\n",
1096 bdrv_get_device_name(bs1));
1101 the_end:
1102 if (saved_vm_running)
1103 vm_start();
1106 void do_loadvm(const char *name)
1108 BlockDriverState *bs, *bs1;
1109 BlockDriverInfo bdi1, *bdi = &bdi1;
1110 QEMUSnapshotInfo sn;
1111 QEMUFile *f;
1112 int i, ret;
1113 int saved_vm_running;
1115 bs = get_bs_snapshots();
1116 if (!bs) {
1117 term_printf("No block device supports snapshots\n");
1118 return;
1121 /* Flush all IO requests so they don't interfere with the new state. */
1122 qemu_aio_flush();
1124 saved_vm_running = vm_running;
1125 vm_stop(0);
1127 for(i = 0; i <= nb_drives; i++) {
1128 bs1 = drives_table[i].bdrv;
1129 if (bdrv_has_snapshot(bs1)) {
1130 ret = bdrv_snapshot_goto(bs1, name);
1131 if (ret < 0) {
1132 if (bs != bs1)
1133 term_printf("Warning: ");
1134 switch(ret) {
1135 case -ENOTSUP:
1136 term_printf("Snapshots not supported on device '%s'\n",
1137 bdrv_get_device_name(bs1));
1138 break;
1139 case -ENOENT:
1140 term_printf("Could not find snapshot '%s' on device '%s'\n",
1141 name, bdrv_get_device_name(bs1));
1142 break;
1143 default:
1144 term_printf("Error %d while activating snapshot on '%s'\n",
1145 ret, bdrv_get_device_name(bs1));
1146 break;
1148 /* fatal on snapshot block device */
1149 if (bs == bs1)
1150 goto the_end;
1155 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
1156 term_printf("Device %s does not support VM state snapshots\n",
1157 bdrv_get_device_name(bs));
1158 return;
1161 /* Don't even try to load empty VM states */
1162 ret = bdrv_snapshot_find(bs, &sn, name);
1163 if ((ret >= 0) && (sn.vm_state_size == 0))
1164 goto the_end;
1166 /* restore the VM state */
1167 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
1168 if (!f) {
1169 term_printf("Could not open VM state file\n");
1170 goto the_end;
1172 ret = qemu_loadvm_state(f);
1173 qemu_fclose(f);
1174 if (ret < 0) {
1175 term_printf("Error %d while loading VM state\n", ret);
1177 the_end:
1178 if (saved_vm_running)
1179 vm_start();
1182 void do_delvm(const char *name)
1184 BlockDriverState *bs, *bs1;
1185 int i, ret;
1187 bs = get_bs_snapshots();
1188 if (!bs) {
1189 term_printf("No block device supports snapshots\n");
1190 return;
1193 for(i = 0; i <= nb_drives; i++) {
1194 bs1 = drives_table[i].bdrv;
1195 if (bdrv_has_snapshot(bs1)) {
1196 ret = bdrv_snapshot_delete(bs1, name);
1197 if (ret < 0) {
1198 if (ret == -ENOTSUP)
1199 term_printf("Snapshots not supported on device '%s'\n",
1200 bdrv_get_device_name(bs1));
1201 else
1202 term_printf("Error %d while deleting snapshot on '%s'\n",
1203 ret, bdrv_get_device_name(bs1));
1209 void do_info_snapshots(void)
1211 BlockDriverState *bs, *bs1;
1212 QEMUSnapshotInfo *sn_tab, *sn;
1213 int nb_sns, i;
1214 char buf[256];
1216 bs = get_bs_snapshots();
1217 if (!bs) {
1218 term_printf("No available block device supports snapshots\n");
1219 return;
1221 term_printf("Snapshot devices:");
1222 for(i = 0; i <= nb_drives; i++) {
1223 bs1 = drives_table[i].bdrv;
1224 if (bdrv_has_snapshot(bs1)) {
1225 if (bs == bs1)
1226 term_printf(" %s", bdrv_get_device_name(bs1));
1229 term_printf("\n");
1231 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1232 if (nb_sns < 0) {
1233 term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
1234 return;
1236 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
1237 term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1238 for(i = 0; i < nb_sns; i++) {
1239 sn = &sn_tab[i];
1240 term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1242 qemu_free(sn_tab);