root / migration-exec.c @ a15fdf86
History | View | Annotate | Download (3.1 kB)
1 | 065e2813 | aliguori | /*
|
---|---|---|---|
2 | 065e2813 | aliguori | * QEMU live migration
|
3 | 065e2813 | aliguori | *
|
4 | 065e2813 | aliguori | * Copyright IBM, Corp. 2008
|
5 | 065e2813 | aliguori | * Copyright Dell MessageOne 2008
|
6 | 065e2813 | aliguori | *
|
7 | 065e2813 | aliguori | * Authors:
|
8 | 065e2813 | aliguori | * Anthony Liguori <aliguori@us.ibm.com>
|
9 | 065e2813 | aliguori | * Charles Duffy <charles_duffy@messageone.com>
|
10 | 065e2813 | aliguori | *
|
11 | 065e2813 | aliguori | * This work is licensed under the terms of the GNU GPL, version 2. See
|
12 | 065e2813 | aliguori | * the COPYING file in the top-level directory.
|
13 | 065e2813 | aliguori | *
|
14 | 065e2813 | aliguori | */
|
15 | 065e2813 | aliguori | |
16 | 065e2813 | aliguori | #include "qemu-common.h" |
17 | 065e2813 | aliguori | #include "qemu_socket.h" |
18 | 065e2813 | aliguori | #include "migration.h" |
19 | 065e2813 | aliguori | #include "qemu-char.h" |
20 | 065e2813 | aliguori | #include "sysemu.h" |
21 | 065e2813 | aliguori | #include "buffered_file.h" |
22 | 065e2813 | aliguori | #include "block.h" |
23 | 065e2813 | aliguori | |
24 | 065e2813 | aliguori | //#define DEBUG_MIGRATION_EXEC
|
25 | 065e2813 | aliguori | |
26 | 065e2813 | aliguori | #ifdef DEBUG_MIGRATION_EXEC
|
27 | 065e2813 | aliguori | #define dprintf(fmt, ...) \
|
28 | 065e2813 | aliguori | do { printf("migration-exec: " fmt, ## __VA_ARGS__); } while (0) |
29 | 065e2813 | aliguori | #else
|
30 | 065e2813 | aliguori | #define dprintf(fmt, ...) \
|
31 | 065e2813 | aliguori | do { } while (0) |
32 | 065e2813 | aliguori | #endif
|
33 | 065e2813 | aliguori | |
34 | 065e2813 | aliguori | static int file_errno(FdMigrationState *s) |
35 | 065e2813 | aliguori | { |
36 | 065e2813 | aliguori | return errno;
|
37 | 065e2813 | aliguori | } |
38 | 065e2813 | aliguori | |
39 | 065e2813 | aliguori | static int file_write(FdMigrationState *s, const void * buf, size_t size) |
40 | 065e2813 | aliguori | { |
41 | 065e2813 | aliguori | return write(s->fd, buf, size);
|
42 | 065e2813 | aliguori | } |
43 | 065e2813 | aliguori | |
44 | 065e2813 | aliguori | static int exec_close(FdMigrationState *s) |
45 | 065e2813 | aliguori | { |
46 | 065e2813 | aliguori | dprintf("exec_close\n");
|
47 | 065e2813 | aliguori | if (s->opaque) {
|
48 | 065e2813 | aliguori | qemu_fclose(s->opaque); |
49 | 065e2813 | aliguori | s->opaque = NULL;
|
50 | 065e2813 | aliguori | s->fd = -1;
|
51 | 065e2813 | aliguori | } |
52 | 065e2813 | aliguori | return 0; |
53 | 065e2813 | aliguori | } |
54 | 065e2813 | aliguori | |
55 | 065e2813 | aliguori | MigrationState *exec_start_outgoing_migration(const char *command, |
56 | c163b5ca | lirans@il.ibm.com | int64_t bandwidth_limit, |
57 | c163b5ca | lirans@il.ibm.com | int detach,
|
58 | c163b5ca | lirans@il.ibm.com | int blk,
|
59 | c163b5ca | lirans@il.ibm.com | int inc)
|
60 | 065e2813 | aliguori | { |
61 | 065e2813 | aliguori | FdMigrationState *s; |
62 | 065e2813 | aliguori | FILE *f; |
63 | 065e2813 | aliguori | |
64 | 065e2813 | aliguori | s = qemu_mallocz(sizeof(*s));
|
65 | 065e2813 | aliguori | |
66 | 065e2813 | aliguori | f = popen(command, "w");
|
67 | 065e2813 | aliguori | if (f == NULL) { |
68 | 065e2813 | aliguori | dprintf("Unable to popen exec target\n");
|
69 | 065e2813 | aliguori | goto err_after_alloc;
|
70 | 065e2813 | aliguori | } |
71 | 065e2813 | aliguori | |
72 | 065e2813 | aliguori | s->fd = fileno(f); |
73 | 065e2813 | aliguori | if (s->fd == -1) { |
74 | 065e2813 | aliguori | dprintf("Unable to retrieve file descriptor for popen'd handle\n");
|
75 | 065e2813 | aliguori | goto err_after_open;
|
76 | 065e2813 | aliguori | } |
77 | 065e2813 | aliguori | |
78 | 90750009 | Chris Lalancette | socket_set_nonblock(s->fd); |
79 | 065e2813 | aliguori | |
80 | 065e2813 | aliguori | s->opaque = qemu_popen(f, "w");
|
81 | 065e2813 | aliguori | |
82 | 8ad9fa5d | aliguori | s->close = exec_close; |
83 | 065e2813 | aliguori | s->get_error = file_errno; |
84 | 065e2813 | aliguori | s->write = file_write; |
85 | 065e2813 | aliguori | s->mig_state.cancel = migrate_fd_cancel; |
86 | 065e2813 | aliguori | s->mig_state.get_status = migrate_fd_get_status; |
87 | 065e2813 | aliguori | s->mig_state.release = migrate_fd_release; |
88 | 065e2813 | aliguori | |
89 | c163b5ca | lirans@il.ibm.com | s->mig_state.blk = blk; |
90 | c163b5ca | lirans@il.ibm.com | s->mig_state.shared = inc; |
91 | c163b5ca | lirans@il.ibm.com | |
92 | 065e2813 | aliguori | s->state = MIG_STATE_ACTIVE; |
93 | 731b0364 | aliguori | s->mon_resume = NULL;
|
94 | 065e2813 | aliguori | s->bandwidth_limit = bandwidth_limit; |
95 | 065e2813 | aliguori | |
96 | 731b0364 | aliguori | if (!detach)
|
97 | 731b0364 | aliguori | migrate_fd_monitor_suspend(s); |
98 | 065e2813 | aliguori | |
99 | 065e2813 | aliguori | migrate_fd_connect(s); |
100 | 065e2813 | aliguori | return &s->mig_state;
|
101 | 065e2813 | aliguori | |
102 | 065e2813 | aliguori | err_after_open:
|
103 | 065e2813 | aliguori | pclose(f); |
104 | 065e2813 | aliguori | err_after_alloc:
|
105 | 065e2813 | aliguori | qemu_free(s); |
106 | 065e2813 | aliguori | return NULL; |
107 | 065e2813 | aliguori | } |
108 | 065e2813 | aliguori | |
109 | 8a43b1ea | Chris Lalancette | static void exec_accept_incoming_migration(void *opaque) |
110 | 065e2813 | aliguori | { |
111 | 8a43b1ea | Chris Lalancette | QEMUFile *f = opaque; |
112 | 065e2813 | aliguori | int ret;
|
113 | 065e2813 | aliguori | |
114 | 065e2813 | aliguori | ret = qemu_loadvm_state(f); |
115 | 065e2813 | aliguori | if (ret < 0) { |
116 | 065e2813 | aliguori | fprintf(stderr, "load of migration failed\n");
|
117 | 065e2813 | aliguori | goto err;
|
118 | 065e2813 | aliguori | } |
119 | 065e2813 | aliguori | qemu_announce_self(); |
120 | 065e2813 | aliguori | dprintf("successfully loaded vm state\n");
|
121 | 8a43b1ea | Chris Lalancette | /* we've successfully migrated, close the fd */
|
122 | 7f79dd28 | Paolo Bonzini | qemu_set_fd_handler2(qemu_stdio_fd(f), NULL, NULL, NULL, NULL); |
123 | d399f677 | Paolo Bonzini | if (autostart)
|
124 | d399f677 | Paolo Bonzini | vm_start(); |
125 | 065e2813 | aliguori | |
126 | 065e2813 | aliguori | err:
|
127 | 065e2813 | aliguori | qemu_fclose(f); |
128 | 8a43b1ea | Chris Lalancette | } |
129 | 8a43b1ea | Chris Lalancette | |
130 | 8a43b1ea | Chris Lalancette | int exec_start_incoming_migration(const char *command) |
131 | 8a43b1ea | Chris Lalancette | { |
132 | 8a43b1ea | Chris Lalancette | QEMUFile *f; |
133 | 8a43b1ea | Chris Lalancette | |
134 | 8a43b1ea | Chris Lalancette | dprintf("Attempting to start an incoming migration\n");
|
135 | 8a43b1ea | Chris Lalancette | f = qemu_popen_cmd(command, "r");
|
136 | 8a43b1ea | Chris Lalancette | if(f == NULL) { |
137 | 8a43b1ea | Chris Lalancette | dprintf("Unable to apply qemu wrapper to popen file\n");
|
138 | 8a43b1ea | Chris Lalancette | return -errno;
|
139 | 8a43b1ea | Chris Lalancette | } |
140 | 8a43b1ea | Chris Lalancette | |
141 | 7f79dd28 | Paolo Bonzini | qemu_set_fd_handler2(qemu_stdio_fd(f), NULL,
|
142 | 8a43b1ea | Chris Lalancette | exec_accept_incoming_migration, NULL,
|
143 | 8a43b1ea | Chris Lalancette | (void *)(unsigned long)f); |
144 | 8a43b1ea | Chris Lalancette | |
145 | 8a43b1ea | Chris Lalancette | return 0; |
146 | 065e2813 | aliguori | } |