migration-exec.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. *
  14. */
  15. #include "qemu-common.h"
  16. #include "qemu_socket.h"
  17. #include "migration.h"
  18. #include "qemu-char.h"
  19. #include "sysemu.h"
  20. #include "console.h"
  21. #include "buffered_file.h"
  22. #include "block.h"
  23. //#define DEBUG_MIGRATION_EXEC
  24. #ifdef DEBUG_MIGRATION_EXEC
  25. #define dprintf(fmt, ...) \
  26. do { printf("migration-exec: " fmt, ## __VA_ARGS__); } while (0)
  27. #else
  28. #define dprintf(fmt, ...) \
  29. do { } while (0)
  30. #endif
  31. static int file_errno(FdMigrationState *s)
  32. {
  33. return errno;
  34. }
  35. static int file_write(FdMigrationState *s, const void * buf, size_t size)
  36. {
  37. return write(s->fd, buf, size);
  38. }
  39. static int exec_close(FdMigrationState *s)
  40. {
  41. dprintf("exec_close\n");
  42. if (s->opaque) {
  43. qemu_fclose(s->opaque);
  44. s->opaque = NULL;
  45. s->fd = -1;
  46. }
  47. return 0;
  48. }
  49. MigrationState *exec_start_outgoing_migration(const char *command,
  50. int64_t bandwidth_limit,
  51. int async)
  52. {
  53. FdMigrationState *s;
  54. FILE *f;
  55. s = qemu_mallocz(sizeof(*s));
  56. f = popen(command, "w");
  57. if (f == NULL) {
  58. dprintf("Unable to popen exec target\n");
  59. goto err_after_alloc;
  60. }
  61. s->fd = fileno(f);
  62. if (s->fd == -1) {
  63. dprintf("Unable to retrieve file descriptor for popen'd handle\n");
  64. goto err_after_open;
  65. }
  66. socket_set_nonblock(s->fd);
  67. s->opaque = qemu_popen(f, "w");
  68. s->close = exec_close;
  69. s->get_error = file_errno;
  70. s->write = file_write;
  71. s->mig_state.cancel = migrate_fd_cancel;
  72. s->mig_state.get_status = migrate_fd_get_status;
  73. s->mig_state.release = migrate_fd_release;
  74. s->state = MIG_STATE_ACTIVE;
  75. s->detach = !async;
  76. s->bandwidth_limit = bandwidth_limit;
  77. if (s->detach == 1) {
  78. dprintf("detaching from monitor\n");
  79. monitor_suspend();
  80. s->detach = 2;
  81. }
  82. migrate_fd_connect(s);
  83. return &s->mig_state;
  84. err_after_open:
  85. pclose(f);
  86. err_after_alloc:
  87. qemu_free(s);
  88. return NULL;
  89. }
  90. static void exec_accept_incoming_migration(void *opaque)
  91. {
  92. QEMUFile *f = opaque;
  93. int ret;
  94. ret = qemu_loadvm_state(f);
  95. if (ret < 0) {
  96. fprintf(stderr, "load of migration failed\n");
  97. goto err;
  98. }
  99. qemu_announce_self();
  100. dprintf("successfully loaded vm state\n");
  101. /* we've successfully migrated, close the fd */
  102. qemu_set_fd_handler2(qemu_popen_fd(f), NULL, NULL, NULL, NULL);
  103. if (autostart)
  104. vm_start();
  105. err:
  106. qemu_fclose(f);
  107. }
  108. int exec_start_incoming_migration(const char *command)
  109. {
  110. QEMUFile *f;
  111. dprintf("Attempting to start an incoming migration\n");
  112. f = qemu_popen_cmd(command, "r");
  113. if(f == NULL) {
  114. dprintf("Unable to apply qemu wrapper to popen file\n");
  115. return -errno;
  116. }
  117. qemu_set_fd_handler2(qemu_popen_fd(f), NULL,
  118. exec_accept_incoming_migration, NULL,
  119. (void *)(unsigned long)f);
  120. return 0;
  121. }