Temporary workaround for ppc on ppc
[qemu-kvm/fedora.git] / savevm.c
blobcd833504e2f6055b893d722ce7b2af68f6378e3c
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 void unregister_savevm(const char *idstr, void *opaque)
648 SaveStateEntry **pse;
650 pse = &first_se;
651 while (*pse != NULL) {
652 if (strcmp((*pse)->idstr, idstr) == 0 && (*pse)->opaque == opaque) {
653 SaveStateEntry *next = (*pse)->next;
654 qemu_free(*pse);
655 *pse = next;
656 continue;
658 pse = &(*pse)->next;
662 #define QEMU_VM_FILE_MAGIC 0x5145564d
663 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
664 #define QEMU_VM_FILE_VERSION 0x00000003
666 #define QEMU_VM_EOF 0x00
667 #define QEMU_VM_SECTION_START 0x01
668 #define QEMU_VM_SECTION_PART 0x02
669 #define QEMU_VM_SECTION_END 0x03
670 #define QEMU_VM_SECTION_FULL 0x04
672 int qemu_savevm_state_begin(QEMUFile *f)
674 SaveStateEntry *se;
676 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
677 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
679 for (se = first_se; se != NULL; se = se->next) {
680 int len;
682 if (se->save_live_state == NULL)
683 continue;
685 /* Section type */
686 qemu_put_byte(f, QEMU_VM_SECTION_START);
687 qemu_put_be32(f, se->section_id);
689 /* ID string */
690 len = strlen(se->idstr);
691 qemu_put_byte(f, len);
692 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
694 qemu_put_be32(f, se->instance_id);
695 qemu_put_be32(f, se->version_id);
697 se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
700 if (qemu_file_has_error(f))
701 return -EIO;
703 return 0;
706 int qemu_savevm_state_iterate(QEMUFile *f)
708 SaveStateEntry *se;
709 int ret = 1;
711 for (se = first_se; se != NULL; se = se->next) {
712 if (se->save_live_state == NULL)
713 continue;
715 /* Section type */
716 qemu_put_byte(f, QEMU_VM_SECTION_PART);
717 qemu_put_be32(f, se->section_id);
719 ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
722 if (ret)
723 return 1;
725 if (qemu_file_has_error(f))
726 return -EIO;
728 return 0;
731 int qemu_savevm_state_complete(QEMUFile *f)
733 SaveStateEntry *se;
735 for (se = first_se; se != NULL; se = se->next) {
736 if (se->save_live_state == NULL)
737 continue;
739 /* Section type */
740 qemu_put_byte(f, QEMU_VM_SECTION_END);
741 qemu_put_be32(f, se->section_id);
743 se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
746 for(se = first_se; se != NULL; se = se->next) {
747 int len;
749 if (se->save_state == NULL)
750 continue;
752 /* Section type */
753 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
754 qemu_put_be32(f, se->section_id);
756 /* ID string */
757 len = strlen(se->idstr);
758 qemu_put_byte(f, len);
759 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
761 qemu_put_be32(f, se->instance_id);
762 qemu_put_be32(f, se->version_id);
764 se->save_state(f, se->opaque);
767 qemu_put_byte(f, QEMU_VM_EOF);
769 if (qemu_file_has_error(f))
770 return -EIO;
772 return 0;
775 int qemu_savevm_state(QEMUFile *f)
777 int saved_vm_running;
778 int ret;
780 saved_vm_running = vm_running;
781 vm_stop(0);
783 bdrv_flush_all();
785 ret = qemu_savevm_state_begin(f);
786 if (ret < 0)
787 goto out;
789 do {
790 ret = qemu_savevm_state_iterate(f);
791 if (ret < 0)
792 goto out;
793 } while (ret == 0);
795 ret = qemu_savevm_state_complete(f);
797 out:
798 if (qemu_file_has_error(f))
799 ret = -EIO;
801 if (!ret && saved_vm_running)
802 vm_start();
804 return ret;
807 static SaveStateEntry *find_se(const char *idstr, int instance_id)
809 SaveStateEntry *se;
811 for(se = first_se; se != NULL; se = se->next) {
812 if (!strcmp(se->idstr, idstr) &&
813 instance_id == se->instance_id)
814 return se;
816 return NULL;
819 typedef struct LoadStateEntry {
820 SaveStateEntry *se;
821 int section_id;
822 int version_id;
823 struct LoadStateEntry *next;
824 } LoadStateEntry;
826 static int qemu_loadvm_state_v2(QEMUFile *f)
828 SaveStateEntry *se;
829 int len, ret, instance_id, record_len, version_id;
830 int64_t total_len, end_pos, cur_pos;
831 char idstr[256];
833 total_len = qemu_get_be64(f);
834 end_pos = total_len + qemu_ftell(f);
835 for(;;) {
836 if (qemu_ftell(f) >= end_pos)
837 break;
838 len = qemu_get_byte(f);
839 qemu_get_buffer(f, (uint8_t *)idstr, len);
840 idstr[len] = '\0';
841 instance_id = qemu_get_be32(f);
842 version_id = qemu_get_be32(f);
843 record_len = qemu_get_be32(f);
844 cur_pos = qemu_ftell(f);
845 se = find_se(idstr, instance_id);
846 if (!se) {
847 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
848 instance_id, idstr);
849 } else {
850 ret = se->load_state(f, se->opaque, version_id);
851 if (ret < 0) {
852 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
853 instance_id, idstr);
854 return ret;
857 /* always seek to exact end of record */
858 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
861 if (qemu_file_has_error(f))
862 return -EIO;
864 return 0;
867 int qemu_loadvm_state(QEMUFile *f)
869 LoadStateEntry *first_le = NULL;
870 uint8_t section_type;
871 unsigned int v;
872 int ret;
874 v = qemu_get_be32(f);
875 if (v != QEMU_VM_FILE_MAGIC)
876 return -EINVAL;
878 v = qemu_get_be32(f);
879 if (v == QEMU_VM_FILE_VERSION_COMPAT)
880 return qemu_loadvm_state_v2(f);
881 if (v != QEMU_VM_FILE_VERSION)
882 return -ENOTSUP;
884 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
885 uint32_t instance_id, version_id, section_id;
886 LoadStateEntry *le;
887 SaveStateEntry *se;
888 char idstr[257];
889 int len;
891 switch (section_type) {
892 case QEMU_VM_SECTION_START:
893 case QEMU_VM_SECTION_FULL:
894 /* Read section start */
895 section_id = qemu_get_be32(f);
896 len = qemu_get_byte(f);
897 qemu_get_buffer(f, (uint8_t *)idstr, len);
898 idstr[len] = 0;
899 instance_id = qemu_get_be32(f);
900 version_id = qemu_get_be32(f);
902 /* Find savevm section */
903 se = find_se(idstr, instance_id);
904 if (se == NULL) {
905 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
906 ret = -EINVAL;
907 goto out;
910 /* Validate version */
911 if (version_id > se->version_id) {
912 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
913 version_id, idstr, se->version_id);
914 ret = -EINVAL;
915 goto out;
918 /* Add entry */
919 le = qemu_mallocz(sizeof(*le));
921 le->se = se;
922 le->section_id = section_id;
923 le->version_id = version_id;
924 le->next = first_le;
925 first_le = le;
927 le->se->load_state(f, le->se->opaque, le->version_id);
928 break;
929 case QEMU_VM_SECTION_PART:
930 case QEMU_VM_SECTION_END:
931 section_id = qemu_get_be32(f);
933 for (le = first_le; le && le->section_id != section_id; le = le->next);
934 if (le == NULL) {
935 fprintf(stderr, "Unknown savevm section %d\n", section_id);
936 ret = -EINVAL;
937 goto out;
940 le->se->load_state(f, le->se->opaque, le->version_id);
941 break;
942 default:
943 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
944 ret = -EINVAL;
945 goto out;
949 ret = 0;
951 out:
952 while (first_le) {
953 LoadStateEntry *le = first_le;
954 first_le = first_le->next;
955 qemu_free(le);
958 if (qemu_file_has_error(f))
959 ret = -EIO;
961 return ret;
964 /* device can contain snapshots */
965 static int bdrv_can_snapshot(BlockDriverState *bs)
967 return (bs &&
968 !bdrv_is_removable(bs) &&
969 !bdrv_is_read_only(bs));
972 /* device must be snapshots in order to have a reliable snapshot */
973 static int bdrv_has_snapshot(BlockDriverState *bs)
975 return (bs &&
976 !bdrv_is_removable(bs) &&
977 !bdrv_is_read_only(bs));
980 static BlockDriverState *get_bs_snapshots(void)
982 BlockDriverState *bs;
983 int i;
985 if (bs_snapshots)
986 return bs_snapshots;
987 for(i = 0; i <= nb_drives; i++) {
988 bs = drives_table[i].bdrv;
989 if (bdrv_can_snapshot(bs))
990 goto ok;
992 return NULL;
994 bs_snapshots = bs;
995 return bs;
998 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
999 const char *name)
1001 QEMUSnapshotInfo *sn_tab, *sn;
1002 int nb_sns, i, ret;
1004 ret = -ENOENT;
1005 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1006 if (nb_sns < 0)
1007 return ret;
1008 for(i = 0; i < nb_sns; i++) {
1009 sn = &sn_tab[i];
1010 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1011 *sn_info = *sn;
1012 ret = 0;
1013 break;
1016 qemu_free(sn_tab);
1017 return ret;
1020 void do_savevm(const char *name)
1022 BlockDriverState *bs, *bs1;
1023 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
1024 int must_delete, ret, i;
1025 BlockDriverInfo bdi1, *bdi = &bdi1;
1026 QEMUFile *f;
1027 int saved_vm_running;
1028 uint32_t vm_state_size;
1029 #ifdef _WIN32
1030 struct _timeb tb;
1031 #else
1032 struct timeval tv;
1033 #endif
1035 bs = get_bs_snapshots();
1036 if (!bs) {
1037 term_printf("No block device can accept snapshots\n");
1038 return;
1041 /* ??? Should this occur after vm_stop? */
1042 qemu_aio_flush();
1044 saved_vm_running = vm_running;
1045 vm_stop(0);
1047 must_delete = 0;
1048 if (name) {
1049 ret = bdrv_snapshot_find(bs, old_sn, name);
1050 if (ret >= 0) {
1051 must_delete = 1;
1054 memset(sn, 0, sizeof(*sn));
1055 if (must_delete) {
1056 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1057 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1058 } else {
1059 if (name)
1060 pstrcpy(sn->name, sizeof(sn->name), name);
1063 /* fill auxiliary fields */
1064 #ifdef _WIN32
1065 _ftime(&tb);
1066 sn->date_sec = tb.time;
1067 sn->date_nsec = tb.millitm * 1000000;
1068 #else
1069 gettimeofday(&tv, NULL);
1070 sn->date_sec = tv.tv_sec;
1071 sn->date_nsec = tv.tv_usec * 1000;
1072 #endif
1073 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1075 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
1076 term_printf("Device %s does not support VM state snapshots\n",
1077 bdrv_get_device_name(bs));
1078 goto the_end;
1081 /* save the VM state */
1082 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
1083 if (!f) {
1084 term_printf("Could not open VM state file\n");
1085 goto the_end;
1087 ret = qemu_savevm_state(f);
1088 vm_state_size = qemu_ftell(f);
1089 qemu_fclose(f);
1090 if (ret < 0) {
1091 term_printf("Error %d while writing VM\n", ret);
1092 goto the_end;
1095 /* create the snapshots */
1097 for(i = 0; i < nb_drives; i++) {
1098 bs1 = drives_table[i].bdrv;
1099 if (bdrv_has_snapshot(bs1)) {
1100 if (must_delete) {
1101 ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
1102 if (ret < 0) {
1103 term_printf("Error while deleting snapshot on '%s'\n",
1104 bdrv_get_device_name(bs1));
1107 /* Write VM state size only to the image that contains the state */
1108 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
1109 ret = bdrv_snapshot_create(bs1, sn);
1110 if (ret < 0) {
1111 term_printf("Error while creating snapshot on '%s'\n",
1112 bdrv_get_device_name(bs1));
1117 the_end:
1118 if (saved_vm_running)
1119 vm_start();
1122 void do_loadvm(const char *name)
1124 BlockDriverState *bs, *bs1;
1125 BlockDriverInfo bdi1, *bdi = &bdi1;
1126 QEMUSnapshotInfo sn;
1127 QEMUFile *f;
1128 int i, ret;
1129 int saved_vm_running;
1131 bs = get_bs_snapshots();
1132 if (!bs) {
1133 term_printf("No block device supports snapshots\n");
1134 return;
1137 /* Flush all IO requests so they don't interfere with the new state. */
1138 qemu_aio_flush();
1140 saved_vm_running = vm_running;
1141 vm_stop(0);
1143 for(i = 0; i <= nb_drives; i++) {
1144 bs1 = drives_table[i].bdrv;
1145 if (bdrv_has_snapshot(bs1)) {
1146 ret = bdrv_snapshot_goto(bs1, name);
1147 if (ret < 0) {
1148 if (bs != bs1)
1149 term_printf("Warning: ");
1150 switch(ret) {
1151 case -ENOTSUP:
1152 term_printf("Snapshots not supported on device '%s'\n",
1153 bdrv_get_device_name(bs1));
1154 break;
1155 case -ENOENT:
1156 term_printf("Could not find snapshot '%s' on device '%s'\n",
1157 name, bdrv_get_device_name(bs1));
1158 break;
1159 default:
1160 term_printf("Error %d while activating snapshot on '%s'\n",
1161 ret, bdrv_get_device_name(bs1));
1162 break;
1164 /* fatal on snapshot block device */
1165 if (bs == bs1)
1166 goto the_end;
1171 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
1172 term_printf("Device %s does not support VM state snapshots\n",
1173 bdrv_get_device_name(bs));
1174 return;
1177 /* Don't even try to load empty VM states */
1178 ret = bdrv_snapshot_find(bs, &sn, name);
1179 if ((ret >= 0) && (sn.vm_state_size == 0))
1180 goto the_end;
1182 /* restore the VM state */
1183 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
1184 if (!f) {
1185 term_printf("Could not open VM state file\n");
1186 goto the_end;
1188 ret = qemu_loadvm_state(f);
1189 qemu_fclose(f);
1190 if (ret < 0) {
1191 term_printf("Error %d while loading VM state\n", ret);
1193 the_end:
1194 if (saved_vm_running)
1195 vm_start();
1198 void do_delvm(const char *name)
1200 BlockDriverState *bs, *bs1;
1201 int i, ret;
1203 bs = get_bs_snapshots();
1204 if (!bs) {
1205 term_printf("No block device supports snapshots\n");
1206 return;
1209 for(i = 0; i <= nb_drives; i++) {
1210 bs1 = drives_table[i].bdrv;
1211 if (bdrv_has_snapshot(bs1)) {
1212 ret = bdrv_snapshot_delete(bs1, name);
1213 if (ret < 0) {
1214 if (ret == -ENOTSUP)
1215 term_printf("Snapshots not supported on device '%s'\n",
1216 bdrv_get_device_name(bs1));
1217 else
1218 term_printf("Error %d while deleting snapshot on '%s'\n",
1219 ret, bdrv_get_device_name(bs1));
1225 void do_info_snapshots(void)
1227 BlockDriverState *bs, *bs1;
1228 QEMUSnapshotInfo *sn_tab, *sn;
1229 int nb_sns, i;
1230 char buf[256];
1232 bs = get_bs_snapshots();
1233 if (!bs) {
1234 term_printf("No available block device supports snapshots\n");
1235 return;
1237 term_printf("Snapshot devices:");
1238 for(i = 0; i <= nb_drives; i++) {
1239 bs1 = drives_table[i].bdrv;
1240 if (bdrv_has_snapshot(bs1)) {
1241 if (bs == bs1)
1242 term_printf(" %s", bdrv_get_device_name(bs1));
1245 term_printf("\n");
1247 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1248 if (nb_sns < 0) {
1249 term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
1250 return;
1252 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
1253 term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1254 for(i = 0; i < nb_sns; i++) {
1255 sn = &sn_tab[i];
1256 term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1258 qemu_free(sn_tab);