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