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