]>
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 <aliguori@us.ibm.com> | |
9 | * Charles Duffy <charles_duffy@messageone.com> | |
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" | |
065e2813 AL |
21 | #include "buffered_file.h" |
22 | #include "block.h" | |
23 | ||
24 | //#define DEBUG_MIGRATION_EXEC | |
25 | ||
26 | #ifdef DEBUG_MIGRATION_EXEC | |
d0f2c4c6 | 27 | #define DPRINTF(fmt, ...) \ |
065e2813 AL |
28 | do { printf("migration-exec: " fmt, ## __VA_ARGS__); } while (0) |
29 | #else | |
d0f2c4c6 | 30 | #define DPRINTF(fmt, ...) \ |
065e2813 AL |
31 | do { } while (0) |
32 | #endif | |
33 | ||
34 | static int file_errno(FdMigrationState *s) | |
35 | { | |
36 | return errno; | |
37 | } | |
38 | ||
39 | static int file_write(FdMigrationState *s, const void * buf, size_t size) | |
40 | { | |
41 | return write(s->fd, buf, size); | |
42 | } | |
43 | ||
44 | static int exec_close(FdMigrationState *s) | |
45 | { | |
41ef56e6 | 46 | int ret = 0; |
d0f2c4c6 | 47 | DPRINTF("exec_close\n"); |
065e2813 | 48 | if (s->opaque) { |
41ef56e6 | 49 | ret = qemu_fclose(s->opaque); |
065e2813 AL |
50 | s->opaque = NULL; |
51 | s->fd = -1; | |
41ef56e6 AL |
52 | if (ret != -1 && |
53 | WIFEXITED(ret) | |
54 | && WEXITSTATUS(ret) == 0) { | |
55 | ret = 0; | |
56 | } else { | |
57 | ret = -1; | |
58 | } | |
065e2813 | 59 | } |
41ef56e6 | 60 | return ret; |
065e2813 AL |
61 | } |
62 | ||
f327aa0c JK |
63 | MigrationState *exec_start_outgoing_migration(Monitor *mon, |
64 | const char *command, | |
c163b5ca | 65 | int64_t bandwidth_limit, |
66 | int detach, | |
67 | int blk, | |
68 | int inc) | |
065e2813 AL |
69 | { |
70 | FdMigrationState *s; | |
71 | FILE *f; | |
72 | ||
73 | s = qemu_mallocz(sizeof(*s)); | |
065e2813 AL |
74 | |
75 | f = popen(command, "w"); | |
76 | if (f == NULL) { | |
d0f2c4c6 | 77 | DPRINTF("Unable to popen exec target\n"); |
065e2813 AL |
78 | goto err_after_alloc; |
79 | } | |
80 | ||
81 | s->fd = fileno(f); | |
82 | if (s->fd == -1) { | |
d0f2c4c6 | 83 | DPRINTF("Unable to retrieve file descriptor for popen'd handle\n"); |
065e2813 AL |
84 | goto err_after_open; |
85 | } | |
86 | ||
90750009 | 87 | socket_set_nonblock(s->fd); |
065e2813 AL |
88 | |
89 | s->opaque = qemu_popen(f, "w"); | |
90 | ||
8ad9fa5d | 91 | s->close = exec_close; |
065e2813 AL |
92 | s->get_error = file_errno; |
93 | s->write = file_write; | |
94 | s->mig_state.cancel = migrate_fd_cancel; | |
95 | s->mig_state.get_status = migrate_fd_get_status; | |
96 | s->mig_state.release = migrate_fd_release; | |
97 | ||
c163b5ca | 98 | s->mig_state.blk = blk; |
99 | s->mig_state.shared = inc; | |
f327aa0c | 100 | |
065e2813 | 101 | s->state = MIG_STATE_ACTIVE; |
f327aa0c | 102 | s->mon = NULL; |
065e2813 AL |
103 | s->bandwidth_limit = bandwidth_limit; |
104 | ||
f327aa0c JK |
105 | if (!detach) { |
106 | migrate_fd_monitor_suspend(s, mon); | |
107 | } | |
065e2813 AL |
108 | |
109 | migrate_fd_connect(s); | |
110 | return &s->mig_state; | |
111 | ||
112 | err_after_open: | |
113 | pclose(f); | |
114 | err_after_alloc: | |
115 | qemu_free(s); | |
065e2813 AL |
116 | return NULL; |
117 | } | |
118 | ||
8a43b1ea | 119 | static void exec_accept_incoming_migration(void *opaque) |
065e2813 | 120 | { |
8a43b1ea | 121 | QEMUFile *f = opaque; |
065e2813 | 122 | int ret; |
065e2813 | 123 | |
065e2813 AL |
124 | ret = qemu_loadvm_state(f); |
125 | if (ret < 0) { | |
126 | fprintf(stderr, "load of migration failed\n"); | |
127 | goto err; | |
128 | } | |
129 | qemu_announce_self(); | |
d0f2c4c6 | 130 | DPRINTF("successfully loaded vm state\n"); |
cfaf6d36 | 131 | |
d399f677 PB |
132 | if (autostart) |
133 | vm_start(); | |
065e2813 AL |
134 | |
135 | err: | |
cfaf6d36 | 136 | qemu_set_fd_handler2(qemu_stdio_fd(f), NULL, NULL, NULL, NULL); |
065e2813 | 137 | qemu_fclose(f); |
8a43b1ea CL |
138 | } |
139 | ||
140 | int exec_start_incoming_migration(const char *command) | |
141 | { | |
142 | QEMUFile *f; | |
143 | ||
d0f2c4c6 | 144 | DPRINTF("Attempting to start an incoming migration\n"); |
8a43b1ea CL |
145 | f = qemu_popen_cmd(command, "r"); |
146 | if(f == NULL) { | |
d0f2c4c6 | 147 | DPRINTF("Unable to apply qemu wrapper to popen file\n"); |
8a43b1ea CL |
148 | return -errno; |
149 | } | |
150 | ||
7f79dd28 | 151 | qemu_set_fd_handler2(qemu_stdio_fd(f), NULL, |
1c39e2a2 | 152 | exec_accept_incoming_migration, NULL, f); |
8a43b1ea CL |
153 | |
154 | return 0; | |
065e2813 | 155 | } |