]>
Commit | Line | Data |
---|---|---|
065e2813 AL |
1 | /* |
2 | * QEMU live migration | |
3 | * | |
4 | * Copyright IBM, Corp. 2008 | |
5 | * Copyright Dell MessageOne 2008 | |
6 | * | |
7 | * Authors: | |
8 | * Anthony Liguori <[email protected]> | |
9 | * Charles Duffy <[email protected]> | |
10 | * | |
11 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
12 | * the COPYING file in the top-level directory. | |
13 | * | |
14 | */ | |
15 | ||
16 | #include "qemu-common.h" | |
17 | #include "qemu_socket.h" | |
18 | #include "migration.h" | |
19 | #include "qemu-char.h" | |
20 | #include "sysemu.h" | |
21 | #include "console.h" | |
22 | #include "buffered_file.h" | |
23 | #include "block.h" | |
24 | ||
25 | //#define DEBUG_MIGRATION_EXEC | |
26 | ||
27 | #ifdef DEBUG_MIGRATION_EXEC | |
28 | #define dprintf(fmt, ...) \ | |
29 | do { printf("migration-exec: " fmt, ## __VA_ARGS__); } while (0) | |
30 | #else | |
31 | #define dprintf(fmt, ...) \ | |
32 | do { } while (0) | |
33 | #endif | |
34 | ||
35 | static int file_errno(FdMigrationState *s) | |
36 | { | |
37 | return errno; | |
38 | } | |
39 | ||
40 | static int file_write(FdMigrationState *s, const void * buf, size_t size) | |
41 | { | |
42 | return write(s->fd, buf, size); | |
43 | } | |
44 | ||
45 | static int exec_close(FdMigrationState *s) | |
46 | { | |
47 | dprintf("exec_close\n"); | |
48 | if (s->opaque) { | |
49 | qemu_fclose(s->opaque); | |
50 | s->opaque = NULL; | |
51 | s->fd = -1; | |
52 | } | |
53 | return 0; | |
54 | } | |
55 | ||
56 | MigrationState *exec_start_outgoing_migration(const char *command, | |
57 | int64_t bandwidth_limit, | |
58 | int async) | |
59 | { | |
60 | FdMigrationState *s; | |
61 | FILE *f; | |
62 | ||
63 | s = qemu_mallocz(sizeof(*s)); | |
64 | if (s == NULL) { | |
65 | dprintf("Unable to allocate FdMigrationState\n"); | |
66 | goto err; | |
67 | } | |
68 | ||
69 | f = popen(command, "w"); | |
70 | if (f == NULL) { | |
71 | dprintf("Unable to popen exec target\n"); | |
72 | goto err_after_alloc; | |
73 | } | |
74 | ||
75 | s->fd = fileno(f); | |
76 | if (s->fd == -1) { | |
77 | dprintf("Unable to retrieve file descriptor for popen'd handle\n"); | |
78 | goto err_after_open; | |
79 | } | |
80 | ||
81 | if (fcntl(s->fd, F_SETFD, O_NONBLOCK) == -1) { | |
82 | dprintf("Unable to set nonblocking mode on file descriptor\n"); | |
83 | goto err_after_open; | |
84 | } | |
85 | ||
86 | s->opaque = qemu_popen(f, "w"); | |
87 | ||
88 | s->get_error = file_errno; | |
89 | s->write = file_write; | |
90 | s->mig_state.cancel = migrate_fd_cancel; | |
91 | s->mig_state.get_status = migrate_fd_get_status; | |
92 | s->mig_state.release = migrate_fd_release; | |
93 | ||
94 | s->state = MIG_STATE_ACTIVE; | |
95 | s->detach = !async; | |
96 | s->bandwidth_limit = bandwidth_limit; | |
97 | ||
98 | if (s->detach == 1) { | |
99 | dprintf("detaching from monitor\n"); | |
100 | monitor_suspend(); | |
101 | s->detach = 2; | |
102 | } | |
103 | ||
104 | migrate_fd_connect(s); | |
105 | return &s->mig_state; | |
106 | ||
107 | err_after_open: | |
108 | pclose(f); | |
109 | err_after_alloc: | |
110 | qemu_free(s); | |
111 | err: | |
112 | return NULL; | |
113 | } | |
114 | ||
115 | int exec_start_incoming_migration(const char *command) | |
116 | { | |
117 | int ret; | |
118 | QEMUFile *f; | |
119 | ||
120 | dprintf("Attempting to start an incoming migration\n"); | |
121 | f = qemu_popen_cmd(command, "r"); | |
122 | if(f == NULL) { | |
123 | dprintf("Unable to apply qemu wrapper to popen file\n"); | |
124 | return -errno; | |
125 | } | |
126 | vm_stop(0); /* just in case */ | |
127 | ret = qemu_loadvm_state(f); | |
128 | if (ret < 0) { | |
129 | fprintf(stderr, "load of migration failed\n"); | |
130 | goto err; | |
131 | } | |
132 | qemu_announce_self(); | |
133 | dprintf("successfully loaded vm state\n"); | |
134 | vm_start(); | |
135 | qemu_fclose(f); | |
136 | return 0; | |
137 | ||
138 | err: | |
139 | qemu_fclose(f); | |
140 | return -errno; | |
141 | } |