aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 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 | * |
Paolo Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 14 | * Contributions after 2012-01-13 are licensed under the terms of the |
| 15 | * GNU GPL, version 2 or (at your option) any later version. |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include "qemu-common.h" |
| 19 | #include "qemu_socket.h" |
| 20 | #include "migration.h" |
| 21 | #include "qemu-char.h" |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 22 | #include "buffered_file.h" |
| 23 | #include "block.h" |
Blue Swirl | 0ffbba3 | 2010-06-04 20:01:07 +0000 | [diff] [blame] | 24 | #include <sys/types.h> |
| 25 | #include <sys/wait.h> |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 26 | |
| 27 | //#define DEBUG_MIGRATION_EXEC |
| 28 | |
| 29 | #ifdef DEBUG_MIGRATION_EXEC |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 30 | #define DPRINTF(fmt, ...) \ |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 31 | do { printf("migration-exec: " fmt, ## __VA_ARGS__); } while (0) |
| 32 | #else |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 33 | #define DPRINTF(fmt, ...) \ |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 34 | do { } while (0) |
| 35 | #endif |
| 36 | |
Juan Quintela | 22f00a4 | 2010-05-11 15:56:35 +0200 | [diff] [blame] | 37 | static int file_errno(MigrationState *s) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 38 | { |
| 39 | return errno; |
| 40 | } |
| 41 | |
Juan Quintela | 22f00a4 | 2010-05-11 15:56:35 +0200 | [diff] [blame] | 42 | static int file_write(MigrationState *s, const void * buf, size_t size) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 43 | { |
| 44 | return write(s->fd, buf, size); |
| 45 | } |
| 46 | |
Juan Quintela | 22f00a4 | 2010-05-11 15:56:35 +0200 | [diff] [blame] | 47 | static int exec_close(MigrationState *s) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 48 | { |
Anthony Liguori | 41ef56e | 2010-06-02 14:55:25 -0500 | [diff] [blame] | 49 | int ret = 0; |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 50 | DPRINTF("exec_close\n"); |
Paolo Bonzini | 6c36013 | 2012-09-27 13:30:15 +0200 | [diff] [blame^] | 51 | ret = qemu_fclose(s->opaque); |
| 52 | s->opaque = NULL; |
| 53 | s->fd = -1; |
| 54 | if (ret >= 0 && !(WIFEXITED(ret) && WEXITSTATUS(ret) == 0)) { |
| 55 | /* close succeeded, but non-zero exit code: */ |
| 56 | ret = -EIO; /* fake errno value */ |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 57 | } |
Anthony Liguori | 41ef56e | 2010-06-02 14:55:25 -0500 | [diff] [blame] | 58 | return ret; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 61 | void exec_start_outgoing_migration(MigrationState *s, const char *command, Error **errp) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 62 | { |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 63 | FILE *f; |
| 64 | |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 65 | f = popen(command, "w"); |
| 66 | if (f == NULL) { |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 67 | error_setg_errno(errp, errno, "failed to popen the migration target"); |
| 68 | return; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | s->fd = fileno(f); |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 72 | assert(s->fd != -1); |
Chris Lalancette | 9075000 | 2009-08-05 17:07:35 +0200 | [diff] [blame] | 73 | socket_set_nonblock(s->fd); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 74 | |
| 75 | s->opaque = qemu_popen(f, "w"); |
| 76 | |
aliguori | 8ad9fa5 | 2008-11-12 22:29:11 +0000 | [diff] [blame] | 77 | s->close = exec_close; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 78 | s->get_error = file_errno; |
| 79 | s->write = file_write; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 80 | |
| 81 | migrate_fd_connect(s); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Chris Lalancette | 8a43b1e | 2009-05-25 16:38:23 +0200 | [diff] [blame] | 84 | static void exec_accept_incoming_migration(void *opaque) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 85 | { |
Chris Lalancette | 8a43b1e | 2009-05-25 16:38:23 +0200 | [diff] [blame] | 86 | QEMUFile *f = opaque; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 87 | |
Paolo Bonzini | d263a20 | 2012-08-08 10:21:26 +0200 | [diff] [blame] | 88 | qemu_set_fd_handler2(qemu_get_fd(f), NULL, NULL, NULL, NULL); |
Paolo Bonzini | a6ef290 | 2012-08-07 10:49:13 +0200 | [diff] [blame] | 89 | process_incoming_migration(f); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 90 | qemu_fclose(f); |
Chris Lalancette | 8a43b1e | 2009-05-25 16:38:23 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 93 | void exec_start_incoming_migration(const char *command, Error **errp) |
Chris Lalancette | 8a43b1e | 2009-05-25 16:38:23 +0200 | [diff] [blame] | 94 | { |
| 95 | QEMUFile *f; |
| 96 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 97 | DPRINTF("Attempting to start an incoming migration\n"); |
Chris Lalancette | 8a43b1e | 2009-05-25 16:38:23 +0200 | [diff] [blame] | 98 | f = qemu_popen_cmd(command, "r"); |
| 99 | if(f == NULL) { |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 100 | error_setg_errno(errp, errno, "failed to popen the migration source"); |
| 101 | return; |
Chris Lalancette | 8a43b1e | 2009-05-25 16:38:23 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Paolo Bonzini | d263a20 | 2012-08-08 10:21:26 +0200 | [diff] [blame] | 104 | qemu_set_fd_handler2(qemu_get_fd(f), NULL, |
Juan Quintela | 1c39e2a | 2010-03-11 17:55:38 +0100 | [diff] [blame] | 105 | exec_accept_incoming_migration, NULL, f); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 106 | } |