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