host_device_remove: remove incorrect check for device name (Eduardo Habkost)
[qemu-kvm/fedora.git] / savevm.c
blob3b4941448bfb135a6b11313cea410c94f9125dd0
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 bdrv_put_buffer(void *opaque, const uint8_t *buf,
310 int64_t pos, int size)
312 QEMUFileBdrv *s = opaque;
313 bdrv_pwrite(s->bs, s->base_offset + pos, buf, size);
314 return size;
317 static int bdrv_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
319 QEMUFileBdrv *s = opaque;
320 return bdrv_pread(s->bs, s->base_offset + pos, buf, 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, bdrv_put_buffer, NULL, bdrv_fclose, NULL);
342 return qemu_fopen_ops(s, NULL, bdrv_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_fflush(QEMUFile *f)
371 if (!f->put_buffer)
372 return;
374 if (f->is_write && f->buf_index > 0) {
375 int len;
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;
386 static void qemu_fill_buffer(QEMUFile *f)
388 int len;
390 if (!f->get_buffer)
391 return;
393 if (f->is_write)
394 abort();
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;
405 int qemu_fclose(QEMUFile *f)
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;
415 void qemu_file_put_notify(QEMUFile *f)
417 f->put_buffer(f->opaque, NULL, 0, 0);
420 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
422 int l;
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();
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);
444 void qemu_put_byte(QEMUFile *f, int v)
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();
452 f->buf[f->buf_index++] = v;
453 f->is_write = 1;
454 if (f->buf_index >= IO_BUF_SIZE)
455 qemu_fflush(f);
458 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
460 int size, l;
462 if (f->is_write)
463 abort();
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;
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;
481 return size1 - size;
484 int qemu_get_byte(QEMUFile *f)
486 if (f->is_write)
487 abort();
489 if (f->buf_index >= f->buf_size) {
490 qemu_fill_buffer(f);
491 if (f->buf_index >= f->buf_size)
492 return 0;
494 return f->buf[f->buf_index++];
497 int64_t qemu_ftell(QEMUFile *f)
499 return f->buf_offset - f->buf_size + f->buf_index;
502 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
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;
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;
520 return pos;
523 int qemu_file_rate_limit(QEMUFile *f)
525 if (f->rate_limit)
526 return f->rate_limit(f->opaque);
528 return 0;
531 void qemu_put_be16(QEMUFile *f, unsigned int v)
533 qemu_put_byte(f, v >> 8);
534 qemu_put_byte(f, v);
537 void qemu_put_be32(QEMUFile *f, unsigned int v)
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);
545 void qemu_put_be64(QEMUFile *f, uint64_t v)
547 qemu_put_be32(f, v >> 32);
548 qemu_put_be32(f, v);
551 unsigned int qemu_get_be16(QEMUFile *f)
553 unsigned int v;
554 v = qemu_get_byte(f) << 8;
555 v |= qemu_get_byte(f);
556 return v;
559 unsigned int qemu_get_be32(QEMUFile *f)
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;
569 uint64_t qemu_get_be64(QEMUFile *f)
571 uint64_t v;
572 v = (uint64_t)qemu_get_be32(f) << 32;
573 v |= qemu_get_be32(f);
574 return v;
577 typedef 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;
589 static SaveStateEntry *first_se;
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. */
595 int 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)
603 SaveStateEntry *se, **pse;
604 static int global_section_id;
606 se = qemu_malloc(sizeof(SaveStateEntry));
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;
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;
626 *pse = se;
627 return 0;
630 int register_savevm(const char *idstr,
631 int instance_id,
632 int version_id,
633 SaveStateHandler *save_state,
634 LoadStateHandler *load_state,
635 void *opaque)
637 return register_savevm_live(idstr, instance_id, version_id,
638 NULL, save_state, load_state, opaque);
641 #define QEMU_VM_FILE_MAGIC 0x5145564d
642 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
643 #define QEMU_VM_FILE_VERSION 0x00000003
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
651 int qemu_savevm_state_begin(QEMUFile *f)
653 SaveStateEntry *se;
655 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
656 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
658 for (se = first_se; se != NULL; se = se->next) {
659 int len;
661 if (se->save_live_state == NULL)
662 continue;
664 /* Section type */
665 qemu_put_byte(f, QEMU_VM_SECTION_START);
666 qemu_put_be32(f, se->section_id);
668 /* ID string */
669 len = strlen(se->idstr);
670 qemu_put_byte(f, len);
671 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
673 qemu_put_be32(f, se->instance_id);
674 qemu_put_be32(f, se->version_id);
676 se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
679 if (qemu_file_has_error(f))
680 return -EIO;
682 return 0;
685 int qemu_savevm_state_iterate(QEMUFile *f)
687 SaveStateEntry *se;
688 int ret = 1;
690 for (se = first_se; se != NULL; se = se->next) {
691 if (se->save_live_state == NULL)
692 continue;
694 /* Section type */
695 qemu_put_byte(f, QEMU_VM_SECTION_PART);
696 qemu_put_be32(f, se->section_id);
698 ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
701 if (ret)
702 return 1;
704 if (qemu_file_has_error(f))
705 return -EIO;
707 return 0;
710 int qemu_savevm_state_complete(QEMUFile *f)
712 SaveStateEntry *se;
714 for (se = first_se; se != NULL; se = se->next) {
715 if (se->save_live_state == NULL)
716 continue;
718 /* Section type */
719 qemu_put_byte(f, QEMU_VM_SECTION_END);
720 qemu_put_be32(f, se->section_id);
722 se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
725 for(se = first_se; se != NULL; se = se->next) {
726 int len;
728 if (se->save_state == NULL)
729 continue;
731 /* Section type */
732 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
733 qemu_put_be32(f, se->section_id);
735 /* ID string */
736 len = strlen(se->idstr);
737 qemu_put_byte(f, len);
738 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
740 qemu_put_be32(f, se->instance_id);
741 qemu_put_be32(f, se->version_id);
743 se->save_state(f, se->opaque);
746 qemu_put_byte(f, QEMU_VM_EOF);
748 if (qemu_file_has_error(f))
749 return -EIO;
751 return 0;
754 int qemu_savevm_state(QEMUFile *f)
756 int saved_vm_running;
757 int ret;
759 saved_vm_running = vm_running;
760 vm_stop(0);
762 bdrv_flush_all();
764 ret = qemu_savevm_state_begin(f);
765 if (ret < 0)
766 goto out;
768 do {
769 ret = qemu_savevm_state_iterate(f);
770 if (ret < 0)
771 goto out;
772 } while (ret == 0);
774 ret = qemu_savevm_state_complete(f);
776 out:
777 if (qemu_file_has_error(f))
778 ret = -EIO;
780 if (!ret && saved_vm_running)
781 vm_start();
783 return ret;
786 static SaveStateEntry *find_se(const char *idstr, int instance_id)
788 SaveStateEntry *se;
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;
795 return NULL;
798 typedef struct LoadStateEntry {
799 SaveStateEntry *se;
800 int section_id;
801 int version_id;
802 struct LoadStateEntry *next;
803 } LoadStateEntry;
805 static int qemu_loadvm_state_v2(QEMUFile *f)
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];
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);
835 /* always seek to exact end of record */
836 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
839 if (qemu_file_has_error(f))
840 return -EIO;
842 return 0;
845 int qemu_loadvm_state(QEMUFile *f)
847 LoadStateEntry *first_le = NULL;
848 uint8_t section_type;
849 unsigned int v;
850 int ret;
852 v = qemu_get_be32(f);
853 if (v != QEMU_VM_FILE_MAGIC)
854 return -EINVAL;
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;
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;
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);
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;
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;
896 /* Add entry */
897 le = qemu_mallocz(sizeof(*le));
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;
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);
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;
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;
927 ret = 0;
929 out:
930 while (first_le) {
931 LoadStateEntry *le = first_le;
932 first_le = first_le->next;
933 qemu_free(le);
936 if (qemu_file_has_error(f))
937 ret = -EIO;
939 return ret;
942 /* device can contain snapshots */
943 static int bdrv_can_snapshot(BlockDriverState *bs)
945 return (bs &&
946 !bdrv_is_removable(bs) &&
947 !bdrv_is_read_only(bs));
950 /* device must be snapshots in order to have a reliable snapshot */
951 static int bdrv_has_snapshot(BlockDriverState *bs)
953 return (bs &&
954 !bdrv_is_removable(bs) &&
955 !bdrv_is_read_only(bs));
958 static BlockDriverState *get_bs_snapshots(void)
960 BlockDriverState *bs;
961 int i;
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;
970 return NULL;
972 bs_snapshots = bs;
973 return bs;
976 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
977 const char *name)
979 QEMUSnapshotInfo *sn_tab, *sn;
980 int nb_sns, i, ret;
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;
994 qemu_free(sn_tab);
995 return ret;
998 void do_savevm(const char *name)
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;
1006 uint32_t vm_state_size;
1007 #ifdef _WIN32
1008 struct _timeb tb;
1009 #else
1010 struct timeval tv;
1011 #endif
1013 bs = get_bs_snapshots();
1014 if (!bs) {
1015 term_printf("No block device can accept snapshots\n");
1016 return;
1019 /* ??? Should this occur after vm_stop? */
1020 qemu_aio_flush();
1022 saved_vm_running = vm_running;
1023 vm_stop(0);
1025 must_delete = 0;
1026 if (name) {
1027 ret = bdrv_snapshot_find(bs, old_sn, name);
1028 if (ret >= 0) {
1029 must_delete = 1;
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);
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);
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;
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;
1065 ret = qemu_savevm_state(f);
1066 vm_state_size = qemu_ftell(f);
1067 qemu_fclose(f);
1068 if (ret < 0) {
1069 term_printf("Error %d while writing VM\n", ret);
1070 goto the_end;
1073 /* create the snapshots */
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));
1085 /* Write VM state size only to the image that contains the state */
1086 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
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));
1095 the_end:
1096 if (saved_vm_running)
1097 vm_start();
1100 void do_loadvm(const char *name)
1102 BlockDriverState *bs, *bs1;
1103 BlockDriverInfo bdi1, *bdi = &bdi1;
1104 QEMUSnapshotInfo sn;
1105 QEMUFile *f;
1106 int i, ret;
1107 int saved_vm_running;
1109 bs = get_bs_snapshots();
1110 if (!bs) {
1111 term_printf("No block device supports snapshots\n");
1112 return;
1115 /* Flush all IO requests so they don't interfere with the new state. */
1116 qemu_aio_flush();
1118 saved_vm_running = vm_running;
1119 vm_stop(0);
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;
1142 /* fatal on snapshot block device */
1143 if (bs == bs1)
1144 goto the_end;
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;
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;
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;
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);
1171 the_end:
1172 if (saved_vm_running)
1173 vm_start();
1176 void do_delvm(const char *name)
1178 BlockDriverState *bs, *bs1;
1179 int i, ret;
1181 bs = get_bs_snapshots();
1182 if (!bs) {
1183 term_printf("No block device supports snapshots\n");
1184 return;
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));
1203 void do_info_snapshots(void)
1205 BlockDriverState *bs, *bs1;
1206 QEMUSnapshotInfo *sn_tab, *sn;
1207 int nb_sns, i;
1208 char buf[256];
1210 bs = get_bs_snapshots();
1211 if (!bs) {
1212 term_printf("No available block device supports snapshots\n");
1213 return;
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));
1223 term_printf("\n");
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;
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));
1236 qemu_free(sn_tab);