| 1 |
/* |
| 2 |
* Copyright 2009-2017 Citrix Ltd and other contributors |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as published |
| 6 |
* by the Free Software Foundation; version 2.1 only. with the special |
| 7 |
* exception on linking described in file LICENSE. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU Lesser General Public License for more details. |
| 13 |
*/ |
| 14 |
|
| 15 |
#include <fcntl.h> |
| 16 |
#include <inttypes.h> |
| 17 |
#include <signal.h> |
| 18 |
#include <stdlib.h> |
| 19 |
#include <sys/stat.h> |
| 20 |
#include <sys/types.h> |
| 21 |
#include <sys/utsname.h> |
| 22 |
#include <time.h> |
| 23 |
#include <unistd.h> |
| 24 |
|
| 25 |
#include <libxl.h> |
| 26 |
#include <libxl_utils.h> |
| 27 |
#include <libxlutil.h> |
| 28 |
|
| 29 |
#include "xl.h" |
| 30 |
#include "xl_utils.h" |
| 31 |
#include "xl_parse.h" |
| 32 |
|
| 33 |
#ifndef LIBXL_HAVE_NO_SUSPEND_RESUME |
| 34 |
|
| 35 |
static pid_t create_migration_child(const char *rune, int *send_fd, |
| 36 |
int *recv_fd) |
| 37 |
{ |
| 38 |
int sendpipe[2], recvpipe[2]; |
| 39 |
pid_t child; |
| 40 |
|
| 41 |
if (!rune || !send_fd || !recv_fd) |
| 42 |
return -1; |
| 43 |
|
| 44 |
MUST( libxl_pipe(ctx, sendpipe) ); |
| 45 |
MUST( libxl_pipe(ctx, recvpipe) ); |
| 46 |
|
| 47 |
child = xl_fork(child_migration, "migration transport process"); |
| 48 |
|
| 49 |
if (!child) { |
| 50 |
dup2(sendpipe[0], 0); |
| 51 |
dup2(recvpipe[1], 1); |
| 52 |
close(sendpipe[0]); close(sendpipe[1]); |
| 53 |
close(recvpipe[0]); close(recvpipe[1]); |
| 54 |
execlp("sh","sh","-c",rune,(char*)0); |
| 55 |
perror("failed to exec sh"); |
| 56 |
exit(EXIT_FAILURE); |
| 57 |
} |
| 58 |
|
| 59 |
close(sendpipe[0]); |
| 60 |
close(recvpipe[1]); |
| 61 |
*send_fd = sendpipe[1]; |
| 62 |
*recv_fd = recvpipe[0]; |
| 63 |
|
| 64 |
/* if receiver dies, we get an error and can clean up |
| 65 |
rather than just dying */ |
| 66 |
signal(SIGPIPE, SIG_IGN); |
| 67 |
|
| 68 |
return child; |
| 69 |
} |
| 70 |
|
| 71 |
static int migrate_read_fixedmessage(int fd, const void *msg, int msgsz, |
| 72 |
const char *what, const char *rune) { |
| 73 |
char buf[msgsz]; |
| 74 |
const char *stream; |
| 75 |
int rc; |
| 76 |
|
| 77 |
stream = rune ? "migration receiver stream" : "migration stream"; |
| 78 |
rc = libxl_read_exactly(ctx, fd, buf, msgsz, stream, what); |
| 79 |
if (rc) return 1; |
| 80 |
|
| 81 |
if (memcmp(buf, msg, msgsz)) { |
| 82 |
fprintf(stderr, "%s contained unexpected data instead of %s\n", |
| 83 |
stream, what); |
| 84 |
if (rune) |
| 85 |
fprintf(stderr, "(command run was: %s )\n", rune); |
| 86 |
return 1; |
| 87 |
} |
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
static void migration_child_report(int recv_fd) { |
| 92 |
pid_t child; |
| 93 |
int status, sr; |
| 94 |
struct timeval now, waituntil, timeout; |
| 95 |
static const struct timeval pollinterval = { 0, 1000 }; /* 1ms */ |
| 96 |
|
| 97 |
if (!xl_child_pid(child_migration)) return; |
| 98 |
|
| 99 |
CHK_SYSCALL(gettimeofday(&waituntil, 0)); |
| 100 |
waituntil.tv_sec += 2; |
| 101 |
|
| 102 |
for (;;) { |
| 103 |
pid_t migration_child = xl_child_pid(child_migration); |
| 104 |
child = xl_waitpid(child_migration, &status, WNOHANG); |
| 105 |
|
| 106 |
if (child == migration_child) { |
| 107 |
if (status) |
| 108 |
xl_report_child_exitstatus(XTL_INFO, child_migration, |
| 109 |
migration_child, status); |
| 110 |
break; |
| 111 |
} |
| 112 |
if (child == -1) { |
| 113 |
fprintf(stderr, "wait for migration child [%ld] failed: %s\n", |
| 114 |
(long)migration_child, strerror(errno)); |
| 115 |
break; |
| 116 |
} |
| 117 |
assert(child == 0); |
| 118 |
|
| 119 |
CHK_SYSCALL(gettimeofday(&now, 0)); |
| 120 |
if (timercmp(&now, &waituntil, >)) { |
| 121 |
fprintf(stderr, "migration child [%ld] not exiting, no longer" |
| 122 |
" waiting (exit status will be unreported)\n", |
| 123 |
(long)migration_child); |
| 124 |
break; |
| 125 |
} |
| 126 |
timersub(&waituntil, &now, &timeout); |
| 127 |
|
| 128 |
if (recv_fd >= 0) { |
| 129 |
fd_set readfds, exceptfds; |
| 130 |
FD_ZERO(&readfds); |
| 131 |
FD_ZERO(&exceptfds); |
| 132 |
FD_SET(recv_fd, &readfds); |
| 133 |
FD_SET(recv_fd, &exceptfds); |
| 134 |
sr = select(recv_fd+1, &readfds,0,&exceptfds, &timeout); |
| 135 |
} else { |
| 136 |
if (timercmp(&timeout, &pollinterval, >)) |
| 137 |
timeout = pollinterval; |
| 138 |
sr = select(0,0,0,0, &timeout); |
| 139 |
} |
| 140 |
if (sr > 0) { |
| 141 |
recv_fd = -1; |
| 142 |
} else if (sr == 0) { |
| 143 |
} else if (sr == -1) { |
| 144 |
if (errno != EINTR) { |
| 145 |
fprintf(stderr, "migration child [%ld] exit wait select" |
| 146 |
" failed unexpectedly: %s\n", |
| 147 |
(long)migration_child, strerror(errno)); |
| 148 |
break; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
static void migrate_do_preamble(int send_fd, int recv_fd, pid_t child, |
| 155 |
uint8_t *config_data, int config_len, |
| 156 |
const char *rune) |
| 157 |
{ |
| 158 |
int rc = 0; |
| 159 |
|
| 160 |
if (send_fd < 0 || recv_fd < 0) { |
| 161 |
fprintf(stderr, "migrate_do_preamble: invalid file descriptors\n"); |
| 162 |
exit(EXIT_FAILURE); |
| 163 |
} |
| 164 |
|
| 165 |
rc = migrate_read_fixedmessage(recv_fd, migrate_receiver_banner, |
| 166 |
sizeof(migrate_receiver_banner)-1, |
| 167 |
"banner", rune); |
| 168 |
if (rc) { |
| 169 |
close(send_fd); |
| 170 |
migration_child_report(recv_fd); |
| 171 |
exit(EXIT_FAILURE); |
| 172 |
} |
| 173 |
|
| 174 |
save_domain_core_writeconfig(send_fd, "migration stream", |
| 175 |
config_data, config_len); |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
static void migrate_domain(uint32_t domid, int preserve_domid, |
| 180 |
const char *rune, int debug, |
| 181 |
const char *override_config_file) |
| 182 |
{ |
| 183 |
pid_t child = -1; |
| 184 |
int rc; |
| 185 |
int send_fd = -1, recv_fd = -1; |
| 186 |
char *away_domname; |
| 187 |
char rc_buf; |
| 188 |
uint8_t *config_data; |
| 189 |
int config_len, flags = LIBXL_SUSPEND_LIVE; |
| 190 |
|
| 191 |
save_domain_core_begin(domid, preserve_domid, override_config_file, |
| 192 |
&config_data, &config_len); |
| 193 |
|
| 194 |
if (!config_len) { |
| 195 |
fprintf(stderr, "No config file stored for running domain and " |
| 196 |
"none supplied - cannot migrate.\n"); |
| 197 |
exit(EXIT_FAILURE); |
| 198 |
} |
| 199 |
|
| 200 |
child = create_migration_child(rune, &send_fd, &recv_fd); |
| 201 |
|
| 202 |
migrate_do_preamble(send_fd, recv_fd, child, config_data, config_len, |
| 203 |
rune); |
| 204 |
|
| 205 |
xtl_stdiostream_adjust_flags(logger, XTL_STDIOSTREAM_HIDE_PROGRESS, 0); |
| 206 |
|
| 207 |
if (debug) |
| 208 |
flags |= LIBXL_SUSPEND_DEBUG; |
| 209 |
rc = libxl_domain_suspend(ctx, domid, send_fd, flags, NULL); |
| 210 |
if (rc) { |
| 211 |
fprintf(stderr, "migration sender: libxl_domain_suspend failed" |
| 212 |
" (rc=%d)\n", rc); |
| 213 |
if (rc == ERROR_GUEST_TIMEDOUT) |
| 214 |
goto failed_suspend; |
| 215 |
else |
| 216 |
goto failed_resume; |
| 217 |
} |
| 218 |
|
| 219 |
//fprintf(stderr, "migration sender: Transfer complete.\n"); |
| 220 |
// Should only be printed when debugging as it's a bit messy with |
| 221 |
// progress indication. |
| 222 |
|
| 223 |
rc = migrate_read_fixedmessage(recv_fd, migrate_receiver_ready, |
| 224 |
sizeof(migrate_receiver_ready), |
| 225 |
"ready message", rune); |
| 226 |
if (rc) goto failed_resume; |
| 227 |
|
| 228 |
xtl_stdiostream_adjust_flags(logger, 0, XTL_STDIOSTREAM_HIDE_PROGRESS); |
| 229 |
|
| 230 |
/* right, at this point we are about give the destination |
| 231 |
* permission to rename and resume, so we must first rename the |
| 232 |
* domain away ourselves */ |
| 233 |
|
| 234 |
fprintf(stderr, "migration sender: Target has acknowledged transfer.\n"); |
| 235 |
|
| 236 |
if (common_domname) { |
| 237 |
xasprintf(&away_domname, "%s--migratedaway", common_domname); |
| 238 |
rc = libxl_domain_rename(ctx, domid, common_domname, away_domname); |
| 239 |
if (rc) goto failed_resume; |
| 240 |
} |
| 241 |
|
| 242 |
/* point of no return - as soon as we have tried to say |
| 243 |
* "go" to the receiver, it's not safe to carry on. We leave |
| 244 |
* the domain renamed to %s--migratedaway in case that's helpful. |
| 245 |
*/ |
| 246 |
|
| 247 |
fprintf(stderr, "migration sender: Giving target permission to start.\n"); |
| 248 |
|
| 249 |
rc = libxl_write_exactly(ctx, send_fd, |
| 250 |
migrate_permission_to_go, |
| 251 |
sizeof(migrate_permission_to_go), |
| 252 |
"migration stream", "GO message"); |
| 253 |
if (rc) goto failed_badly; |
| 254 |
|
| 255 |
rc = migrate_read_fixedmessage(recv_fd, migrate_report, |
| 256 |
sizeof(migrate_report), |
| 257 |
"success/failure report message", rune); |
| 258 |
if (rc) goto failed_badly; |
| 259 |
|
| 260 |
rc = libxl_read_exactly(ctx, recv_fd, |
| 261 |
&rc_buf, 1, |
| 262 |
"migration ack stream", "success/failure status"); |
| 263 |
if (rc) goto failed_badly; |
| 264 |
|
| 265 |
if (rc_buf) { |
| 266 |
fprintf(stderr, "migration sender: Target reports startup failure" |
| 267 |
" (status code %d).\n", rc_buf); |
| 268 |
|
| 269 |
rc = migrate_read_fixedmessage(recv_fd, migrate_permission_to_go, |
| 270 |
sizeof(migrate_permission_to_go), |
| 271 |
"permission for sender to resume", |
| 272 |
rune); |
| 273 |
if (rc) goto failed_badly; |
| 274 |
|
| 275 |
fprintf(stderr, "migration sender: Trying to resume at our end.\n"); |
| 276 |
|
| 277 |
if (common_domname) { |
| 278 |
libxl_domain_rename(ctx, domid, away_domname, common_domname); |
| 279 |
} |
| 280 |
rc = libxl_domain_resume(ctx, domid, 1, 0); |
| 281 |
if (!rc) fprintf(stderr, "migration sender: Resumed OK.\n"); |
| 282 |
|
| 283 |
fprintf(stderr, "Migration failed due to problems at target.\n"); |
| 284 |
exit(EXIT_FAILURE); |
| 285 |
} |
| 286 |
|
| 287 |
fprintf(stderr, "migration sender: Target reports successful startup.\n"); |
| 288 |
libxl_domain_destroy(ctx, domid, 0); /* bang! */ |
| 289 |
fprintf(stderr, "Migration successful.\n"); |
| 290 |
exit(EXIT_SUCCESS); |
| 291 |
|
| 292 |
failed_suspend: |
| 293 |
close(send_fd); |
| 294 |
migration_child_report(recv_fd); |
| 295 |
fprintf(stderr, "Migration failed, failed to suspend at sender.\n"); |
| 296 |
exit(EXIT_FAILURE); |
| 297 |
|
| 298 |
failed_resume: |
| 299 |
close(send_fd); |
| 300 |
migration_child_report(recv_fd); |
| 301 |
fprintf(stderr, "Migration failed, resuming at sender.\n"); |
| 302 |
libxl_domain_resume(ctx, domid, 1, 0); |
| 303 |
exit(EXIT_FAILURE); |
| 304 |
|
| 305 |
failed_badly: |
| 306 |
fprintf(stderr, |
| 307 |
"** Migration failed during final handshake **\n" |
| 308 |
"Domain state is now undefined !\n" |
| 309 |
"Please CHECK AT BOTH ENDS for running instances, before renaming and\n" |
| 310 |
" resuming at most one instance. Two simultaneous instances of the domain\n" |
| 311 |
" would probably result in SEVERE DATA LOSS and it is now your\n" |
| 312 |
" responsibility to avoid that. Sorry.\n"); |
| 313 |
|
| 314 |
close(send_fd); |
| 315 |
migration_child_report(recv_fd); |
| 316 |
exit(EXIT_FAILURE); |
| 317 |
} |
| 318 |
|
| 319 |
static void migrate_receive(int debug, int daemonize, int monitor, |
| 320 |
int pause_after_migration, |
| 321 |
int send_fd, int recv_fd, |
| 322 |
libxl_checkpointed_stream checkpointed, |
| 323 |
char *colo_proxy_script, |
| 324 |
bool userspace_colo_proxy) |
| 325 |
{ |
| 326 |
uint32_t domid; |
| 327 |
int rc, rc2; |
| 328 |
char rc_buf; |
| 329 |
char *migration_domname; |
| 330 |
struct domain_create dom_info; |
| 331 |
|
| 332 |
signal(SIGPIPE, SIG_IGN); |
| 333 |
/* if we get SIGPIPE we'd rather just have it as an error */ |
| 334 |
|
| 335 |
fprintf(stderr, "migration target: Ready to receive domain.\n"); |
| 336 |
|
| 337 |
CHK_ERRNOVAL(libxl_write_exactly( |
| 338 |
ctx, send_fd, migrate_receiver_banner, |
| 339 |
sizeof(migrate_receiver_banner)-1, |
| 340 |
"migration ack stream", "banner") ); |
| 341 |
|
| 342 |
memset(&dom_info, 0, sizeof(dom_info)); |
| 343 |
dom_info.debug = debug; |
| 344 |
dom_info.daemonize = daemonize; |
| 345 |
dom_info.monitor = monitor; |
| 346 |
dom_info.paused = 1; |
| 347 |
dom_info.migrate_fd = recv_fd; |
| 348 |
dom_info.send_back_fd = send_fd; |
| 349 |
dom_info.migration_domname_r = &migration_domname; |
| 350 |
dom_info.checkpointed_stream = checkpointed; |
| 351 |
dom_info.colo_proxy_script = colo_proxy_script; |
| 352 |
dom_info.userspace_colo_proxy = userspace_colo_proxy; |
| 353 |
|
| 354 |
rc = create_domain(&dom_info); |
| 355 |
if (rc < 0) { |
| 356 |
fprintf(stderr, "migration target: Domain creation failed" |
| 357 |
" (code %d).\n", rc); |
| 358 |
exit(EXIT_FAILURE); |
| 359 |
} |
| 360 |
|
| 361 |
domid = rc; |
| 362 |
|
| 363 |
switch (checkpointed) { |
| 364 |
case LIBXL_CHECKPOINTED_STREAM_REMUS: |
| 365 |
case LIBXL_CHECKPOINTED_STREAM_COLO: |
| 366 |
{ |
| 367 |
const char *ha = checkpointed == LIBXL_CHECKPOINTED_STREAM_COLO ? |
| 368 |
"COLO" : "Remus"; |
| 369 |
/* If we are here, it means that the sender (primary) has crashed. |
| 370 |
* TODO: Split-Brain Check. |
| 371 |
*/ |
| 372 |
fprintf(stderr, "migration target: %s Failover for domain %u\n", |
| 373 |
ha, domid); |
| 374 |
|
| 375 |
/* |
| 376 |
* If domain renaming fails, lets just continue (as we need the domain |
| 377 |
* to be up & dom names may not matter much, as long as its reachable |
| 378 |
* over network). |
| 379 |
* |
| 380 |
* If domain unpausing fails, destroy domain ? Or is it better to have |
| 381 |
* a consistent copy of the domain (memory, cpu state, disk) |
| 382 |
* on atleast one physical host ? Right now, lets just leave the domain |
| 383 |
* as is and let the Administrator decide (or troubleshoot). |
| 384 |
*/ |
| 385 |
if (migration_domname) { |
| 386 |
rc = libxl_domain_rename(ctx, domid, migration_domname, |
| 387 |
common_domname); |
| 388 |
if (rc) |
| 389 |
fprintf(stderr, "migration target (%s): " |
| 390 |
"Failed to rename domain from %s to %s:%d\n", |
| 391 |
ha, migration_domname, common_domname, rc); |
| 392 |
} |
| 393 |
|
| 394 |
if (checkpointed == LIBXL_CHECKPOINTED_STREAM_COLO) |
| 395 |
/* The guest is running after failover in COLO mode */ |
| 396 |
exit(rc ? -ERROR_FAIL: 0); |
| 397 |
|
| 398 |
rc = libxl_domain_unpause(ctx, domid, NULL); |
| 399 |
if (rc) |
| 400 |
fprintf(stderr, "migration target (%s): " |
| 401 |
"Failed to unpause domain %s (id: %u):%d\n", |
| 402 |
ha, common_domname, domid, rc); |
| 403 |
|
| 404 |
exit(rc ? EXIT_FAILURE : EXIT_SUCCESS); |
| 405 |
} |
| 406 |
default: |
| 407 |
/* do nothing */ |
| 408 |
break; |
| 409 |
} |
| 410 |
|
| 411 |
fprintf(stderr, "migration target: Transfer complete," |
| 412 |
" requesting permission to start domain.\n"); |
| 413 |
|
| 414 |
rc = libxl_write_exactly(ctx, send_fd, |
| 415 |
migrate_receiver_ready, |
| 416 |
sizeof(migrate_receiver_ready), |
| 417 |
"migration ack stream", "ready message"); |
| 418 |
if (rc) exit(EXIT_FAILURE); |
| 419 |
|
| 420 |
rc = migrate_read_fixedmessage(recv_fd, migrate_permission_to_go, |
| 421 |
sizeof(migrate_permission_to_go), |
| 422 |
"GO message", 0); |
| 423 |
if (rc) goto perhaps_destroy_notify_rc; |
| 424 |
|
| 425 |
fprintf(stderr, "migration target: Got permission, starting domain.\n"); |
| 426 |
|
| 427 |
if (migration_domname) { |
| 428 |
rc = libxl_domain_rename(ctx, domid, migration_domname, common_domname); |
| 429 |
if (rc) goto perhaps_destroy_notify_rc; |
| 430 |
} |
| 431 |
|
| 432 |
if (!pause_after_migration) { |
| 433 |
rc = libxl_domain_unpause(ctx, domid, NULL); |
| 434 |
if (rc) goto perhaps_destroy_notify_rc; |
| 435 |
} |
| 436 |
|
| 437 |
fprintf(stderr, "migration target: Domain started successsfully.\n"); |
| 438 |
rc = 0; |
| 439 |
|
| 440 |
perhaps_destroy_notify_rc: |
| 441 |
rc2 = libxl_write_exactly(ctx, send_fd, |
| 442 |
migrate_report, sizeof(migrate_report), |
| 443 |
"migration ack stream", |
| 444 |
"success/failure report"); |
| 445 |
if (rc2) exit(EXIT_FAILURE); |
| 446 |
|
| 447 |
rc_buf = -rc; |
| 448 |
assert(!!rc_buf == !!rc); |
| 449 |
rc2 = libxl_write_exactly(ctx, send_fd, &rc_buf, 1, |
| 450 |
"migration ack stream", |
| 451 |
"success/failure code"); |
| 452 |
if (rc2) exit(EXIT_FAILURE); |
| 453 |
|
| 454 |
if (rc) { |
| 455 |
fprintf(stderr, "migration target: Failure, destroying our copy.\n"); |
| 456 |
|
| 457 |
rc2 = libxl_domain_destroy(ctx, domid, 0); |
| 458 |
if (rc2) { |
| 459 |
fprintf(stderr, "migration target: Failed to destroy our copy" |
| 460 |
" (code %d).\n", rc2); |
| 461 |
exit(EXIT_FAILURE); |
| 462 |
} |
| 463 |
|
| 464 |
fprintf(stderr, "migration target: Cleanup OK, granting sender" |
| 465 |
" permission to resume.\n"); |
| 466 |
|
| 467 |
rc2 = libxl_write_exactly(ctx, send_fd, |
| 468 |
migrate_permission_to_go, |
| 469 |
sizeof(migrate_permission_to_go), |
| 470 |
"migration ack stream", |
| 471 |
"permission to sender to have domain back"); |
| 472 |
if (rc2) exit(EXIT_FAILURE); |
| 473 |
} |
| 474 |
|
| 475 |
exit(EXIT_SUCCESS); |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
int main_migrate_receive(int argc, char **argv) |
| 480 |
{ |
| 481 |
int debug = 0, daemonize = 1, monitor = 1, pause_after_migration = 0; |
| 482 |
libxl_checkpointed_stream checkpointed = LIBXL_CHECKPOINTED_STREAM_NONE; |
| 483 |
int opt; |
| 484 |
bool userspace_colo_proxy = false; |
| 485 |
char *script = NULL; |
| 486 |
static struct option opts[] = { |
| 487 |
{"colo", 0, 0, 0x100}, |
| 488 |
/* It is a shame that the management code for disk is not here. */ |
| 489 |
{"coloft-script", 1, 0, 0x200}, |
| 490 |
{"userspace-colo-proxy", 0, 0, 0x300}, |
| 491 |
COMMON_LONG_OPTS |
| 492 |
}; |
| 493 |
|
| 494 |
SWITCH_FOREACH_OPT(opt, "Fedrp", opts, "migrate-receive", 0) { |
| 495 |
case 'F': |
| 496 |
daemonize = 0; |
| 497 |
break; |
| 498 |
case 'e': |
| 499 |
daemonize = 0; |
| 500 |
monitor = 0; |
| 501 |
break; |
| 502 |
case 'd': |
| 503 |
debug = 1; |
| 504 |
break; |
| 505 |
case 'r': |
| 506 |
checkpointed = LIBXL_CHECKPOINTED_STREAM_REMUS; |
| 507 |
break; |
| 508 |
case 0x100: |
| 509 |
checkpointed = LIBXL_CHECKPOINTED_STREAM_COLO; |
| 510 |
break; |
| 511 |
case 0x200: |
| 512 |
script = optarg; |
| 513 |
break; |
| 514 |
case 0x300: |
| 515 |
userspace_colo_proxy = true; |
| 516 |
break; |
| 517 |
case 'p': |
| 518 |
pause_after_migration = 1; |
| 519 |
break; |
| 520 |
} |
| 521 |
|
| 522 |
if (argc-optind != 0) { |
| 523 |
help("migrate-receive"); |
| 524 |
return EXIT_FAILURE; |
| 525 |
} |
| 526 |
migrate_receive(debug, daemonize, monitor, pause_after_migration, |
| 527 |
STDOUT_FILENO, STDIN_FILENO, |
| 528 |
checkpointed, script, userspace_colo_proxy); |
| 529 |
|
| 530 |
return EXIT_SUCCESS; |
| 531 |
} |
| 532 |
|
| 533 |
int main_migrate(int argc, char **argv) |
| 534 |
{ |
| 535 |
uint32_t domid; |
| 536 |
const char *config_filename = NULL; |
| 537 |
const char *ssh_command = "ssh"; |
| 538 |
char *rune = NULL; |
| 539 |
char *host; |
| 540 |
int opt, daemonize = 1, monitor = 1, debug = 0, pause_after_migration = 0; |
| 541 |
int preserve_domid = 0; |
| 542 |
static struct option opts[] = { |
| 543 |
{"debug", 0, 0, 0x100}, |
| 544 |
{"live", 0, 0, 0x200}, |
| 545 |
COMMON_LONG_OPTS |
| 546 |
}; |
| 547 |
|
| 548 |
SWITCH_FOREACH_OPT(opt, "FC:s:epD", opts, "migrate", 2) { |
| 549 |
case 'C': |
| 550 |
config_filename = optarg; |
| 551 |
break; |
| 552 |
case 's': |
| 553 |
ssh_command = optarg; |
| 554 |
break; |
| 555 |
case 'F': |
| 556 |
daemonize = 0; |
| 557 |
break; |
| 558 |
case 'e': |
| 559 |
daemonize = 0; |
| 560 |
monitor = 0; |
| 561 |
break; |
| 562 |
case 'p': |
| 563 |
pause_after_migration = 1; |
| 564 |
break; |
| 565 |
case 'D': |
| 566 |
preserve_domid = 1; |
| 567 |
break; |
| 568 |
case 0x100: /* --debug */ |
| 569 |
debug = 1; |
| 570 |
break; |
| 571 |
case 0x200: /* --live */ |
| 572 |
/* ignored for compatibility with xm */ |
| 573 |
break; |
| 574 |
} |
| 575 |
|
| 576 |
domid = find_domain(argv[optind]); |
| 577 |
host = argv[optind + 1]; |
| 578 |
|
| 579 |
bool pass_tty_arg = progress_use_cr || (isatty(2) > 0); |
| 580 |
|
| 581 |
if (!ssh_command[0]) { |
| 582 |
rune= host; |
| 583 |
} else { |
| 584 |
char verbose_buf[minmsglevel_default+3]; |
| 585 |
int verbose_len; |
| 586 |
verbose_buf[0] = ' '; |
| 587 |
verbose_buf[1] = '-'; |
| 588 |
memset(verbose_buf+2, 'v', minmsglevel_default); |
| 589 |
verbose_buf[sizeof(verbose_buf)-1] = 0; |
| 590 |
if (minmsglevel == minmsglevel_default) { |
| 591 |
verbose_len = 0; |
| 592 |
} else { |
| 593 |
verbose_len = (minmsglevel_default - minmsglevel) + 2; |
| 594 |
} |
| 595 |
xasprintf(&rune, "exec %s %s xl%s%s%.*s migrate-receive%s%s%s", |
| 596 |
ssh_command, host, |
| 597 |
pass_tty_arg ? " -t" : "", |
| 598 |
timestamps ? " -T" : "", |
| 599 |
verbose_len, verbose_buf, |
| 600 |
daemonize ? "" : " -e", |
| 601 |
debug ? " -d" : "", |
| 602 |
pause_after_migration ? " -p" : ""); |
| 603 |
} |
| 604 |
|
| 605 |
migrate_domain(domid, preserve_domid, rune, debug, config_filename); |
| 606 |
return EXIT_SUCCESS; |
| 607 |
} |
| 608 |
|
| 609 |
int main_remus(int argc, char **argv) |
| 610 |
{ |
| 611 |
uint32_t domid; |
| 612 |
int opt, rc, daemonize = 1; |
| 613 |
const char *ssh_command = "ssh"; |
| 614 |
char *host = NULL, *rune = NULL; |
| 615 |
libxl_domain_remus_info r_info; |
| 616 |
int send_fd = -1, recv_fd = -1; |
| 617 |
pid_t child = -1; |
| 618 |
uint8_t *config_data; |
| 619 |
int config_len; |
| 620 |
|
| 621 |
memset(&r_info, 0, sizeof(libxl_domain_remus_info)); |
| 622 |
|
| 623 |
SWITCH_FOREACH_OPT(opt, "Fbundi:s:N:ecp", NULL, "remus", 2) { |
| 624 |
case 'i': |
| 625 |
r_info.interval = atoi(optarg); |
| 626 |
break; |
| 627 |
case 'F': |
| 628 |
libxl_defbool_set(&r_info.allow_unsafe, true); |
| 629 |
break; |
| 630 |
case 'b': |
| 631 |
libxl_defbool_set(&r_info.blackhole, true); |
| 632 |
break; |
| 633 |
case 'u': |
| 634 |
libxl_defbool_set(&r_info.compression, false); |
| 635 |
break; |
| 636 |
case 'n': |
| 637 |
libxl_defbool_set(&r_info.netbuf, false); |
| 638 |
break; |
| 639 |
case 'N': |
| 640 |
r_info.netbufscript = optarg; |
| 641 |
break; |
| 642 |
case 'd': |
| 643 |
libxl_defbool_set(&r_info.diskbuf, false); |
| 644 |
break; |
| 645 |
case 's': |
| 646 |
ssh_command = optarg; |
| 647 |
break; |
| 648 |
case 'e': |
| 649 |
daemonize = 0; |
| 650 |
break; |
| 651 |
case 'c': |
| 652 |
libxl_defbool_set(&r_info.colo, true); |
| 653 |
break; |
| 654 |
case 'p': |
| 655 |
libxl_defbool_set(&r_info.userspace_colo_proxy, true); |
| 656 |
} |
| 657 |
|
| 658 |
domid = find_domain(argv[optind]); |
| 659 |
host = argv[optind + 1]; |
| 660 |
|
| 661 |
/* Defaults */ |
| 662 |
libxl_defbool_setdefault(&r_info.blackhole, false); |
| 663 |
libxl_defbool_setdefault(&r_info.colo, false); |
| 664 |
libxl_defbool_setdefault(&r_info.userspace_colo_proxy, false); |
| 665 |
|
| 666 |
if (!libxl_defbool_val(r_info.colo) && !r_info.interval) |
| 667 |
r_info.interval = 200; |
| 668 |
|
| 669 |
if (libxl_defbool_val(r_info.userspace_colo_proxy) && |
| 670 |
!libxl_defbool_val(r_info.colo)) { |
| 671 |
fprintf(stderr, "Option -p must be used in conjunction with -c"); |
| 672 |
exit(-1); |
| 673 |
} |
| 674 |
|
| 675 |
if (libxl_defbool_val(r_info.colo)) { |
| 676 |
if (r_info.interval || libxl_defbool_val(r_info.blackhole) || |
| 677 |
!libxl_defbool_is_default(r_info.netbuf) || |
| 678 |
!libxl_defbool_is_default(r_info.diskbuf)) { |
| 679 |
perror("option -c is conflict with -i, -d, -n or -b"); |
| 680 |
exit(-1); |
| 681 |
} |
| 682 |
|
| 683 |
if (libxl_defbool_is_default(r_info.compression)) { |
| 684 |
perror("COLO can't be used with memory compression. " |
| 685 |
"Disable memory checkpoint compression now..."); |
| 686 |
libxl_defbool_set(&r_info.compression, false); |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
if (!r_info.netbufscript) { |
| 691 |
if (libxl_defbool_val(r_info.colo)) |
| 692 |
r_info.netbufscript = default_colo_proxy_script; |
| 693 |
else |
| 694 |
r_info.netbufscript = default_remus_netbufscript; |
| 695 |
} |
| 696 |
|
| 697 |
if (libxl_defbool_val(r_info.blackhole)) { |
| 698 |
send_fd = open("/dev/null", O_RDWR, 0644); |
| 699 |
if (send_fd < 0) { |
| 700 |
perror("failed to open /dev/null"); |
| 701 |
exit(EXIT_FAILURE); |
| 702 |
} |
| 703 |
} else { |
| 704 |
|
| 705 |
if (!ssh_command[0]) { |
| 706 |
rune = host; |
| 707 |
} else { |
| 708 |
if (!libxl_defbool_val(r_info.colo)) { |
| 709 |
xasprintf(&rune, "exec %s %s xl migrate-receive %s %s", |
| 710 |
ssh_command, host, |
| 711 |
"-r", |
| 712 |
daemonize ? "" : " -e"); |
| 713 |
} else { |
| 714 |
xasprintf(&rune, "exec %s %s xl migrate-receive %s %s %s %s %s", |
| 715 |
ssh_command, host, |
| 716 |
"--colo", |
| 717 |
r_info.netbufscript ? "--coloft-script" : "", |
| 718 |
r_info.netbufscript ? r_info.netbufscript : "", |
| 719 |
libxl_defbool_val(r_info.userspace_colo_proxy) ? |
| 720 |
"--userspace-colo-proxy" : "", |
| 721 |
daemonize ? "" : " -e"); |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
save_domain_core_begin(domid, 0, NULL, &config_data, &config_len); |
| 726 |
|
| 727 |
if (!config_len) { |
| 728 |
fprintf(stderr, "No config file stored for running domain and " |
| 729 |
"none supplied - cannot start remus.\n"); |
| 730 |
exit(EXIT_FAILURE); |
| 731 |
} |
| 732 |
|
| 733 |
child = create_migration_child(rune, &send_fd, &recv_fd); |
| 734 |
|
| 735 |
migrate_do_preamble(send_fd, recv_fd, child, config_data, config_len, |
| 736 |
rune); |
| 737 |
|
| 738 |
if (ssh_command[0]) |
| 739 |
free(rune); |
| 740 |
} |
| 741 |
|
| 742 |
/* Point of no return */ |
| 743 |
rc = libxl_domain_remus_start(ctx, &r_info, domid, send_fd, recv_fd, 0); |
| 744 |
|
| 745 |
/* check if the domain exists. User may have xl destroyed the |
| 746 |
* domain to force failover |
| 747 |
*/ |
| 748 |
if (libxl_domain_info(ctx, 0, domid)) { |
| 749 |
fprintf(stderr, "%s: Primary domain has been destroyed.\n", |
| 750 |
libxl_defbool_val(r_info.colo) ? "COLO" : "Remus"); |
| 751 |
close(send_fd); |
| 752 |
return EXIT_SUCCESS; |
| 753 |
} |
| 754 |
|
| 755 |
/* If we are here, it means remus setup/domain suspend/backup has |
| 756 |
* failed. Try to resume the domain and exit gracefully. |
| 757 |
* TODO: Split-Brain check. |
| 758 |
*/ |
| 759 |
if (rc == ERROR_GUEST_TIMEDOUT) |
| 760 |
fprintf(stderr, "Failed to suspend domain at primary.\n"); |
| 761 |
else { |
| 762 |
fprintf(stderr, "%s: Backup failed? resuming domain at primary.\n", |
| 763 |
libxl_defbool_val(r_info.colo) ? "COLO" : "Remus"); |
| 764 |
libxl_domain_resume(ctx, domid, 1, 0); |
| 765 |
} |
| 766 |
|
| 767 |
close(send_fd); |
| 768 |
return EXIT_FAILURE; |
| 769 |
} |
| 770 |
#endif |
| 771 |
|
| 772 |
|
| 773 |
/* |
| 774 |
* Local variables: |
| 775 |
* mode: C |
| 776 |
* c-basic-offset: 4 |
| 777 |
* indent-tabs-mode: nil |
| 778 |
* End: |
| 779 |
*/ |