1 /* $OpenBSD: main.c,v 1.62 2020/01/03 05:32:00 pd Exp $ */ 2 3 /* 4 * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 #include <sys/socket.h> 21 #include <sys/queue.h> 22 #include <sys/un.h> 23 24 #include <machine/vmmvar.h> 25 26 #include <err.h> 27 #include <errno.h> 28 #include <stdio.h> 29 #include <stdlib.h> 30 #include <stdint.h> 31 #include <limits.h> 32 #include <string.h> 33 #include <syslog.h> 34 #include <unistd.h> 35 #include <fcntl.h> 36 #include <util.h> 37 #include <imsg.h> 38 39 #include "vmd.h" 40 #include "virtio.h" 41 #include "proc.h" 42 #include "vmctl.h" 43 44 #define RAW_FMT "raw" 45 #define QCOW2_FMT "qcow2" 46 47 static const char *socket_name = SOCKET_NAME; 48 static int ctl_sock = -1; 49 static int tty_autoconnect = 0; 50 51 __dead void usage(void); 52 __dead void ctl_usage(struct ctl_command *); 53 54 int vmm_action(struct parse_result *); 55 56 int ctl_console(struct parse_result *, int, char *[]); 57 int ctl_convert(const char *, const char *, int, size_t); 58 int ctl_create(struct parse_result *, int, char *[]); 59 int ctl_load(struct parse_result *, int, char *[]); 60 int ctl_log(struct parse_result *, int, char *[]); 61 int ctl_reload(struct parse_result *, int, char *[]); 62 int ctl_reset(struct parse_result *, int, char *[]); 63 int ctl_start(struct parse_result *, int, char *[]); 64 int ctl_status(struct parse_result *, int, char *[]); 65 int ctl_stop(struct parse_result *, int, char *[]); 66 int ctl_waitfor(struct parse_result *, int, char *[]); 67 int ctl_pause(struct parse_result *, int, char *[]); 68 int ctl_unpause(struct parse_result *, int, char *[]); 69 int ctl_send(struct parse_result *, int, char *[]); 70 int ctl_receive(struct parse_result *, int, char *[]); 71 72 struct ctl_command ctl_commands[] = { 73 { "console", CMD_CONSOLE, ctl_console, "id" }, 74 { "create", CMD_CREATE, ctl_create, 75 "[-b base | -i disk] [-s size] disk", 1 }, 76 { "load", CMD_LOAD, ctl_load, "filename" }, 77 { "log", CMD_LOG, ctl_log, "[brief | verbose]" }, 78 { "pause", CMD_PAUSE, ctl_pause, "id" }, 79 { "receive", CMD_RECEIVE, ctl_receive, "name" , 1}, 80 { "reload", CMD_RELOAD, ctl_reload, "" }, 81 { "reset", CMD_RESET, ctl_reset, "[all | switches | vms]" }, 82 { "send", CMD_SEND, ctl_send, "id", 1}, 83 { "show", CMD_STATUS, ctl_status, "[id]" }, 84 { "start", CMD_START, ctl_start, 85 "[-cL] [-B device] [-b path] [-d disk] [-i count]\n" 86 "\t\t[-m size] [-n switch] [-r path] [-t name] id | name" }, 87 { "status", CMD_STATUS, ctl_status, "[id]" }, 88 { "stop", CMD_STOP, ctl_stop, "[-fw] [id | -a]" }, 89 { "unpause", CMD_UNPAUSE, ctl_unpause, "id" }, 90 { "wait", CMD_WAITFOR, ctl_waitfor, "id" }, 91 { NULL } 92 }; 93 94 __dead void 95 usage(void) 96 { 97 extern char *__progname; 98 int i; 99 100 fprintf(stderr, "usage:\t%s [-v] command [arg ...]\n", 101 __progname); 102 for (i = 0; ctl_commands[i].name != NULL; i++) { 103 fprintf(stderr, "\t%s %s %s\n", __progname, 104 ctl_commands[i].name, ctl_commands[i].usage); 105 } 106 exit(1); 107 } 108 109 __dead void 110 ctl_usage(struct ctl_command *ctl) 111 { 112 extern char *__progname; 113 114 fprintf(stderr, "usage:\t%s [-v] %s %s\n", __progname, 115 ctl->name, ctl->usage); 116 exit(1); 117 } 118 119 int 120 main(int argc, char *argv[]) 121 { 122 int ch, verbose = 1; 123 124 while ((ch = getopt(argc, argv, "v")) != -1) { 125 switch (ch) { 126 case 'v': 127 verbose = 2; 128 break; 129 default: 130 usage(); 131 /* NOTREACHED */ 132 } 133 } 134 argc -= optind; 135 argv += optind; 136 optreset = 1; 137 optind = 1; 138 139 if (argc < 1) 140 usage(); 141 142 log_init(verbose, LOG_DAEMON); 143 144 return (parse(argc, argv)); 145 } 146 147 int 148 parse(int argc, char *argv[]) 149 { 150 struct ctl_command *ctl = NULL; 151 struct parse_result res; 152 int i; 153 154 memset(&res, 0, sizeof(res)); 155 res.nifs = -1; 156 157 for (i = 0; ctl_commands[i].name != NULL; i++) { 158 if (strncmp(ctl_commands[i].name, 159 argv[0], strlen(argv[0])) == 0) { 160 if (ctl != NULL) { 161 fprintf(stderr, 162 "ambiguous argument: %s\n", argv[0]); 163 usage(); 164 } 165 ctl = &ctl_commands[i]; 166 } 167 } 168 169 if (ctl == NULL) { 170 fprintf(stderr, "unknown argument: %s\n", argv[0]); 171 usage(); 172 } 173 174 res.action = ctl->action; 175 res.ctl = ctl; 176 177 if (!ctl->has_pledge) { 178 /* pledge(2) default if command doesn't have its own pledge */ 179 if (pledge("stdio rpath exec unix getpw unveil", NULL) == -1) 180 err(1, "pledge"); 181 } 182 if (ctl->main(&res, argc, argv) != 0) 183 exit(1); 184 185 if (ctl_sock != -1) { 186 close(ibuf->fd); 187 free(ibuf); 188 } 189 190 return (0); 191 } 192 193 int 194 vmmaction(struct parse_result *res) 195 { 196 struct sockaddr_un sun; 197 struct imsg imsg; 198 int done = 0; 199 int n; 200 int ret, action; 201 unsigned int flags; 202 203 if (ctl_sock == -1) { 204 if (unveil(SOCKET_NAME, "r") == -1) 205 err(1, "unveil"); 206 if ((ctl_sock = socket(AF_UNIX, 207 SOCK_STREAM|SOCK_CLOEXEC, 0)) == -1) 208 err(1, "socket"); 209 210 memset(&sun, 0, sizeof(sun)); 211 sun.sun_family = AF_UNIX; 212 strlcpy(sun.sun_path, socket_name, sizeof(sun.sun_path)); 213 214 if (connect(ctl_sock, 215 (struct sockaddr *)&sun, sizeof(sun)) == -1) 216 err(1, "connect: %s", socket_name); 217 218 if ((ibuf = malloc(sizeof(struct imsgbuf))) == NULL) 219 err(1, "malloc"); 220 imsg_init(ibuf, ctl_sock); 221 } 222 223 switch (res->action) { 224 case CMD_START: 225 ret = vm_start(res->id, res->name, res->size, res->nifs, 226 res->nets, res->ndisks, res->disks, res->disktypes, 227 res->path, res->isopath, res->instance, res->bootdevice); 228 if (ret) { 229 errno = ret; 230 err(1, "start VM operation failed"); 231 } 232 break; 233 case CMD_STOP: 234 terminate_vm(res->id, res->name, res->flags); 235 break; 236 case CMD_STATUS: 237 case CMD_CONSOLE: 238 case CMD_STOPALL: 239 get_info_vm(res->id, res->name, res->action, res->flags); 240 break; 241 case CMD_LOAD: 242 imsg_compose(ibuf, IMSG_VMDOP_LOAD, 0, 0, -1, 243 res->path, strlen(res->path) + 1); 244 break; 245 case CMD_LOG: 246 imsg_compose(ibuf, IMSG_CTL_VERBOSE, 0, 0, -1, 247 &res->verbose, sizeof(res->verbose)); 248 break; 249 case CMD_RELOAD: 250 imsg_compose(ibuf, IMSG_VMDOP_RELOAD, 0, 0, -1, NULL, 0); 251 break; 252 case CMD_RESET: 253 imsg_compose(ibuf, IMSG_CTL_RESET, 0, 0, -1, 254 &res->mode, sizeof(res->mode)); 255 break; 256 case CMD_WAITFOR: 257 waitfor_vm(res->id, res->name); 258 break; 259 case CMD_PAUSE: 260 pause_vm(res->id, res->name); 261 break; 262 case CMD_UNPAUSE: 263 unpause_vm(res->id, res->name); 264 break; 265 case CMD_SEND: 266 send_vm(res->id, res->name); 267 done = 1; 268 ret = 0; 269 break; 270 case CMD_RECEIVE: 271 vm_receive(res->id, res->name); 272 break; 273 case CMD_CREATE: 274 case NONE: 275 /* The action is not expected here */ 276 errx(1, "invalid action %u", res->action); 277 break; 278 } 279 280 action = res->action; 281 flags = res->flags; 282 parse_free(res); 283 284 while (ibuf->w.queued) 285 if (msgbuf_write(&ibuf->w) <= 0 && errno != EAGAIN) 286 err(1, "write error"); 287 288 while (!done) { 289 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) 290 errx(1, "imsg_read error"); 291 if (n == 0) 292 errx(1, "pipe closed"); 293 294 while (!done) { 295 if ((n = imsg_get(ibuf, &imsg)) == -1) 296 errx(1, "imsg_get error"); 297 if (n == 0) 298 break; 299 300 if (imsg.hdr.type == IMSG_CTL_FAIL) { 301 if (IMSG_DATA_SIZE(&imsg) == sizeof(ret)) 302 memcpy(&ret, imsg.data, sizeof(ret)); 303 else 304 ret = 0; 305 if (ret != 0) { 306 memcpy(&ret, imsg.data, sizeof(ret)); 307 errno = ret; 308 err(1, "command failed"); 309 } else 310 errx(1, "command failed"); 311 } 312 313 ret = 0; 314 switch (action) { 315 case CMD_START: 316 done = vm_start_complete(&imsg, &ret, 317 tty_autoconnect); 318 break; 319 case CMD_WAITFOR: 320 flags = VMOP_WAIT; 321 /* FALLTHROUGH */ 322 case CMD_STOP: 323 done = terminate_vm_complete(&imsg, &ret, 324 flags); 325 break; 326 case CMD_CONSOLE: 327 case CMD_STATUS: 328 case CMD_STOPALL: 329 done = add_info(&imsg, &ret); 330 break; 331 case CMD_PAUSE: 332 done = pause_vm_complete(&imsg, &ret); 333 break; 334 case CMD_RECEIVE: 335 done = vm_start_complete(&imsg, &ret, 0); 336 break; 337 case CMD_UNPAUSE: 338 done = unpause_vm_complete(&imsg, &ret); 339 break; 340 default: 341 done = 1; 342 break; 343 } 344 345 imsg_free(&imsg); 346 } 347 } 348 349 if (ret) 350 return (1); 351 else 352 return (0); 353 } 354 355 void 356 parse_free(struct parse_result *res) 357 { 358 size_t i; 359 360 free(res->name); 361 free(res->path); 362 free(res->isopath); 363 free(res->instance); 364 for (i = 0; i < res->ndisks; i++) 365 free(res->disks[i]); 366 free(res->disks); 367 free(res->disktypes); 368 memset(res, 0, sizeof(*res)); 369 } 370 371 int 372 parse_ifs(struct parse_result *res, char *word, int val) 373 { 374 const char *error; 375 376 if (word != NULL) { 377 val = strtonum(word, 1, INT_MAX, &error); 378 if (error != NULL) { 379 warnx("count is %s: %s", error, word); 380 return (-1); 381 } 382 } 383 res->nifs = val; 384 385 return (0); 386 } 387 388 int 389 parse_network(struct parse_result *res, char *word) 390 { 391 char **nets; 392 char *s; 393 394 if ((nets = reallocarray(res->nets, res->nnets + 1, 395 sizeof(char *))) == NULL) { 396 warn("reallocarray"); 397 return (-1); 398 } 399 if ((s = strdup(word)) == NULL) { 400 warn("strdup"); 401 return (-1); 402 } 403 nets[res->nnets] = s; 404 res->nets = nets; 405 res->nnets++; 406 407 return (0); 408 } 409 410 int 411 parse_size(struct parse_result *res, char *word) 412 { 413 long long val = 0; 414 415 if (word != NULL) { 416 if (scan_scaled(word, &val) != 0) { 417 warn("invalid size: %s", word); 418 return (-1); 419 } 420 } 421 422 if (val < (1024 * 1024)) { 423 warnx("size must be at least one megabyte"); 424 return (-1); 425 } else 426 res->size = val / 1024 / 1024; 427 428 if ((res->size * 1024 * 1024) != val) 429 warnx("size rounded to %lld megabytes", res->size); 430 431 return (0); 432 } 433 434 int 435 parse_disktype(const char *s, const char **ret) 436 { 437 char buf[BUFSIZ]; 438 const char *ext; 439 int fd; 440 ssize_t len; 441 442 *ret = s; 443 444 /* Try to parse the explicit format (qcow2:disk.qc2) */ 445 if (strstr(s, RAW_FMT) == s && *(s + strlen(RAW_FMT)) == ':') { 446 *ret = s + strlen(RAW_FMT) + 1; 447 return (VMDF_RAW); 448 } 449 if (strstr(s, QCOW2_FMT) == s && *(s + strlen(QCOW2_FMT)) == ':') { 450 *ret = s + strlen(QCOW2_FMT) + 1; 451 return (VMDF_QCOW2); 452 } 453 454 /* Or try to derive the format from the file signature */ 455 if ((fd = open(s, O_RDONLY)) != -1) { 456 len = read(fd, buf, sizeof(buf)); 457 close(fd); 458 459 if (len >= (ssize_t)strlen(VM_MAGIC_QCOW) && 460 strncmp(buf, VM_MAGIC_QCOW, 461 strlen(VM_MAGIC_QCOW)) == 0) { 462 /* Return qcow2, the version will be checked later */ 463 return (VMDF_QCOW2); 464 } 465 } 466 467 /* 468 * Use the extension as a last option. This is needed for 469 * 'vmctl create' as the file, and the signature, doesn't 470 * exist yet. 471 */ 472 if ((ext = strrchr(s, '.')) != NULL && *(++ext) != '\0') { 473 if (strcasecmp(ext, RAW_FMT) == 0) 474 return (VMDF_RAW); 475 else if (strcasecmp(ext, QCOW2_FMT) == 0) 476 return (VMDF_QCOW2); 477 } 478 479 /* Fallback to raw */ 480 return (VMDF_RAW); 481 } 482 483 int 484 parse_disk(struct parse_result *res, char *word, int type) 485 { 486 char **disks; 487 int *disktypes; 488 char *s; 489 490 if ((disks = reallocarray(res->disks, res->ndisks + 1, 491 sizeof(char *))) == NULL) { 492 warn("reallocarray"); 493 return (-1); 494 } 495 if ((disktypes = reallocarray(res->disktypes, res->ndisks + 1, 496 sizeof(int))) == NULL) { 497 warn("reallocarray"); 498 return -1; 499 } 500 if ((s = strdup(word)) == NULL) { 501 warn("strdup"); 502 return (-1); 503 } 504 disks[res->ndisks] = s; 505 disktypes[res->ndisks] = type; 506 res->disks = disks; 507 res->disktypes = disktypes; 508 res->ndisks++; 509 510 return (0); 511 } 512 513 int 514 parse_vmid(struct parse_result *res, char *word, int needname) 515 { 516 const char *error; 517 uint32_t id; 518 519 if (word == NULL) { 520 warnx("missing vmid argument"); 521 return (-1); 522 } 523 if (*word == '-') { 524 /* don't print a warning to allow command line options */ 525 return (-1); 526 } 527 id = strtonum(word, 0, UINT32_MAX, &error); 528 if (error == NULL) { 529 if (needname) { 530 warnx("invalid vm name"); 531 return (-1); 532 } else { 533 res->id = id; 534 res->name = NULL; 535 } 536 } else { 537 if (strlen(word) >= VMM_MAX_NAME_LEN) { 538 warnx("name too long"); 539 return (-1); 540 } 541 res->id = 0; 542 if ((res->name = strdup(word)) == NULL) 543 errx(1, "strdup"); 544 } 545 546 return (0); 547 } 548 549 int 550 parse_instance(struct parse_result *res, char *word) 551 { 552 if (strlen(word) >= VMM_MAX_NAME_LEN) { 553 warnx("instance vm name too long"); 554 return (-1); 555 } 556 res->id = 0; 557 if ((res->instance = strdup(word)) == NULL) 558 errx(1, "strdup"); 559 560 return (0); 561 } 562 563 int 564 ctl_create(struct parse_result *res, int argc, char *argv[]) 565 { 566 int ch, ret, type; 567 const char *disk, *format, *base = NULL, *input = NULL; 568 569 while ((ch = getopt(argc, argv, "b:i:s:")) != -1) { 570 switch (ch) { 571 case 'b': 572 base = optarg; 573 if (unveil(base, "r") == -1) 574 err(1, "unveil"); 575 break; 576 case 'i': 577 input = optarg; 578 if (unveil(input, "r") == -1) 579 err(1, "unveil"); 580 break; 581 case 's': 582 if (parse_size(res, optarg) != 0) 583 errx(1, "invalid size: %s", optarg); 584 break; 585 default: 586 ctl_usage(res->ctl); 587 /* NOTREACHED */ 588 } 589 } 590 argc -= optind; 591 argv += optind; 592 593 if (argc < 1) 594 ctl_usage(res->ctl); 595 596 type = parse_disktype(argv[0], &disk); 597 598 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1) 599 err(1, "pledge"); 600 if (unveil(disk, "rwc") == -1) 601 err(1, "unveil"); 602 603 if (input) { 604 if (base && input) 605 errx(1, "conflicting -b and -i arguments"); 606 return ctl_convert(input, disk, type, res->size); 607 } 608 609 if (unveil(NULL, NULL)) 610 err(1, "unveil"); 611 612 if (base && type != VMDF_QCOW2) 613 errx(1, "base images require qcow2 disk format"); 614 if (res->size == 0 && !base) { 615 fprintf(stderr, "could not create %s: missing size argument\n", 616 disk); 617 ctl_usage(res->ctl); 618 } 619 620 if ((ret = create_imagefile(type, disk, base, res->size, &format)) != 0) { 621 errno = ret; 622 err(1, "create imagefile operation failed"); 623 } else 624 warnx("%s imagefile created", format); 625 626 return (0); 627 } 628 629 int 630 ctl_convert(const char *srcfile, const char *dstfile, int dsttype, size_t dstsize) 631 { 632 struct { 633 int fd; 634 int type; 635 struct virtio_backing file; 636 const char *disk; 637 off_t size; 638 } src, dst; 639 int ret; 640 const char *format, *errstr = NULL; 641 uint8_t *buf = NULL, *zerobuf = NULL; 642 size_t buflen; 643 ssize_t len, rlen; 644 off_t off; 645 646 memset(&src, 0, sizeof(src)); 647 memset(&dst, 0, sizeof(dst)); 648 649 src.type = parse_disktype(srcfile, &src.disk); 650 dst.type = dsttype; 651 dst.disk = dstfile; 652 653 if ((src.fd = open_imagefile(src.type, src.disk, O_RDONLY, 654 &src.file, &src.size)) == -1) { 655 errstr = "failed to open source image file"; 656 goto done; 657 } 658 659 /* We can only lock unveil after opening the disk and all base images */ 660 if (unveil(NULL, NULL)) 661 err(1, "unveil"); 662 663 if (dstsize == 0) 664 dstsize = src.size; 665 else 666 dstsize *= 1048576; 667 if (dstsize < (size_t)src.size) { 668 errstr = "size cannot be smaller than input disk size"; 669 goto done; 670 } 671 672 /* align to megabytes */ 673 dst.size = ALIGNSZ(dstsize, 1048576); 674 675 if ((ret = create_imagefile(dst.type, dst.disk, NULL, 676 dst.size / 1048576, &format)) != 0) { 677 errno = ret; 678 errstr = "failed to create destination image file"; 679 goto done; 680 } 681 682 if ((dst.fd = open_imagefile(dst.type, dst.disk, O_RDWR, 683 &dst.file, &dst.size)) == -1) { 684 errstr = "failed to open destination image file"; 685 goto done; 686 } 687 688 if (pledge("stdio", NULL) == -1) 689 err(1, "pledge"); 690 691 /* 692 * Use 64k buffers by default. This could also be adjusted to 693 * the backend cluster size. 694 */ 695 buflen = 1 << 16; 696 if ((buf = calloc(1, buflen)) == NULL || 697 (zerobuf = calloc(1, buflen)) == NULL) { 698 errstr = "failed to allocated buffers"; 699 goto done; 700 } 701 702 for (off = 0; off < dst.size; off += len) { 703 /* Read input from the source image */ 704 if (off < src.size) { 705 len = MIN((off_t)buflen, src.size - off); 706 if ((rlen = src.file.pread(src.file.p, 707 buf, (size_t)len, off)) != len) { 708 errno = EIO; 709 errstr = "failed to read from source"; 710 goto done; 711 } 712 } else 713 len = 0; 714 715 /* and pad the remaining bytes */ 716 if (len < (ssize_t)buflen) { 717 log_debug("%s: padding %zd zero bytes at offset %lld", 718 format, buflen - len, off + len); 719 memset(buf + len, 0, buflen - len); 720 len = buflen; 721 } 722 723 /* 724 * No need to copy empty buffers. This allows the backend, 725 * sparse files or QCOW2 images, to save space in the 726 * destination file. 727 */ 728 if (memcmp(buf, zerobuf, buflen) == 0) 729 continue; 730 731 log_debug("%s: writing %zd of %lld bytes at offset %lld", 732 format, len, dst.size, off); 733 734 if ((rlen = dst.file.pwrite(dst.file.p, 735 buf, (size_t)len, off)) != len) { 736 errno = EIO; 737 errstr = "failed to write to destination"; 738 goto done; 739 } 740 } 741 742 if (dstsize < (size_t)dst.size) 743 warnx("destination size rounded to %lld megabytes", 744 dst.size / 1048576); 745 746 done: 747 free(buf); 748 free(zerobuf); 749 if (src.file.p != NULL) 750 src.file.close(src.file.p, 0); 751 if (dst.file.p != NULL) 752 dst.file.close(dst.file.p, 0); 753 if (errstr != NULL) 754 errx(1, "%s", errstr); 755 else 756 warnx("%s imagefile created", format); 757 758 return (0); 759 } 760 761 int 762 ctl_status(struct parse_result *res, int argc, char *argv[]) 763 { 764 if (argc == 2) { 765 if (parse_vmid(res, argv[1], 0) == -1) 766 errx(1, "invalid id: %s", argv[1]); 767 } else if (argc > 2) 768 ctl_usage(res->ctl); 769 770 return (vmmaction(res)); 771 } 772 773 int 774 ctl_load(struct parse_result *res, int argc, char *argv[]) 775 { 776 if (argc != 2) 777 ctl_usage(res->ctl); 778 779 if ((res->path = strdup(argv[1])) == NULL) 780 err(1, "strdup"); 781 782 return (vmmaction(res)); 783 } 784 785 int 786 ctl_log(struct parse_result *res, int argc, char *argv[]) 787 { 788 if (argc != 2) 789 ctl_usage(res->ctl); 790 791 if (strncasecmp("brief", argv[1], strlen(argv[1])) == 0) 792 res->verbose = 0; 793 else if (strncasecmp("verbose", argv[1], strlen(argv[1])) == 0) 794 res->verbose = 2; 795 else 796 ctl_usage(res->ctl); 797 798 return (vmmaction(res)); 799 } 800 801 int 802 ctl_reload(struct parse_result *res, int argc, char *argv[]) 803 { 804 if (argc != 1) 805 ctl_usage(res->ctl); 806 807 return (vmmaction(res)); 808 } 809 810 int 811 ctl_reset(struct parse_result *res, int argc, char *argv[]) 812 { 813 if (argc == 2) { 814 if (strcasecmp("all", argv[1]) == 0) 815 res->mode = CONFIG_ALL; 816 else if (strcasecmp("vms", argv[1]) == 0) 817 res->mode = CONFIG_VMS; 818 else if (strcasecmp("switches", argv[1]) == 0) 819 res->mode = CONFIG_SWITCHES; 820 else 821 ctl_usage(res->ctl); 822 } else if (argc > 2) 823 ctl_usage(res->ctl); 824 825 if (res->mode == 0) 826 res->mode = CONFIG_ALL; 827 828 return (vmmaction(res)); 829 } 830 831 int 832 ctl_start(struct parse_result *res, int argc, char *argv[]) 833 { 834 int ch, i, type; 835 char path[PATH_MAX]; 836 const char *s; 837 838 while ((ch = getopt(argc, argv, "b:B:cd:i:Lm:n:r:t:")) != -1) { 839 switch (ch) { 840 case 'b': 841 if (res->path) 842 errx(1, "boot image specified multiple times"); 843 if (realpath(optarg, path) == NULL) 844 err(1, "invalid boot image path"); 845 if ((res->path = strdup(path)) == NULL) 846 errx(1, "strdup"); 847 break; 848 case 'B': 849 if (res->bootdevice) 850 errx(1, "boot device specified multiple times"); 851 if (strcmp("disk", optarg) == 0) 852 res->bootdevice = VMBOOTDEV_DISK; 853 else if (strcmp("cdrom", optarg) == 0) 854 res->bootdevice = VMBOOTDEV_CDROM; 855 else if (strcmp("net", optarg) == 0) 856 res->bootdevice = VMBOOTDEV_NET; 857 else 858 errx(1, "unknown boot device %s", optarg); 859 break; 860 case 'r': 861 if (res->isopath) 862 errx(1, "iso image specified multiple times"); 863 if (realpath(optarg, path) == NULL) 864 err(1, "invalid iso image path"); 865 if ((res->isopath = strdup(path)) == NULL) 866 errx(1, "strdup"); 867 break; 868 case 'c': 869 tty_autoconnect = 1; 870 break; 871 case 'L': 872 if (parse_network(res, ".") != 0) 873 errx(1, "invalid network: %s", optarg); 874 break; 875 case 'm': 876 if (res->size) 877 errx(1, "memory specified multiple times"); 878 if (parse_size(res, optarg) != 0) 879 errx(1, "invalid memory size: %s", optarg); 880 break; 881 case 'n': 882 if (parse_network(res, optarg) != 0) 883 errx(1, "invalid network: %s", optarg); 884 break; 885 case 'd': 886 type = parse_disktype(optarg, &s); 887 if (realpath(s, path) == NULL) 888 err(1, "invalid disk path"); 889 if (parse_disk(res, path, type) != 0) 890 errx(1, "invalid disk: %s", optarg); 891 break; 892 case 'i': 893 if (res->nifs != -1) 894 errx(1, "interfaces specified multiple times"); 895 if (parse_ifs(res, optarg, 0) != 0) 896 errx(1, "invalid interface count: %s", optarg); 897 break; 898 case 't': 899 if (parse_instance(res, optarg) == -1) 900 errx(1, "invalid name: %s", optarg); 901 break; 902 default: 903 ctl_usage(res->ctl); 904 /* NOTREACHED */ 905 } 906 } 907 argc -= optind; 908 argv += optind; 909 910 if (argc != 1) 911 ctl_usage(res->ctl); 912 913 if (parse_vmid(res, argv[0], 0) == -1) 914 errx(1, "invalid id: %s", argv[0]); 915 916 for (i = res->nnets; i < res->nifs; i++) { 917 /* Add interface that is not attached to a switch */ 918 if (parse_network(res, "") == -1) 919 return (-1); 920 } 921 if (res->nnets > res->nifs) 922 res->nifs = res->nnets; 923 924 return (vmmaction(res)); 925 } 926 927 int 928 ctl_stop(struct parse_result *res, int argc, char *argv[]) 929 { 930 int ch, ret; 931 932 while ((ch = getopt(argc, argv, "afw")) != -1) { 933 switch (ch) { 934 case 'f': 935 res->flags |= VMOP_FORCE; 936 break; 937 case 'w': 938 res->flags |= VMOP_WAIT; 939 break; 940 case 'a': 941 res->action = CMD_STOPALL; 942 break; 943 default: 944 ctl_usage(res->ctl); 945 /* NOTREACHED */ 946 } 947 } 948 argc -= optind; 949 argv += optind; 950 951 if (argc == 0) { 952 if (res->action != CMD_STOPALL) 953 ctl_usage(res->ctl); 954 } else if (argc > 1) 955 ctl_usage(res->ctl); 956 else if (argc == 1) 957 ret = parse_vmid(res, argv[0], 0); 958 else 959 ret = -1; 960 961 /* VM id is only expected without the -a flag */ 962 if ((res->action != CMD_STOPALL && ret == -1) || 963 (res->action == CMD_STOPALL && ret != -1)) 964 errx(1, "invalid id: %s", argv[1]); 965 966 return (vmmaction(res)); 967 } 968 969 int 970 ctl_console(struct parse_result *res, int argc, char *argv[]) 971 { 972 if (argc == 2) { 973 if (parse_vmid(res, argv[1], 0) == -1) 974 errx(1, "invalid id: %s", argv[1]); 975 } else if (argc != 2) 976 ctl_usage(res->ctl); 977 978 return (vmmaction(res)); 979 } 980 981 int 982 ctl_waitfor(struct parse_result *res, int argc, char *argv[]) 983 { 984 if (argc == 2) { 985 if (parse_vmid(res, argv[1], 0) == -1) 986 errx(1, "invalid id: %s", argv[1]); 987 } else if (argc != 2) 988 ctl_usage(res->ctl); 989 990 return (vmmaction(res)); 991 } 992 993 int 994 ctl_pause(struct parse_result *res, int argc, char *argv[]) 995 { 996 if (argc == 2) { 997 if (parse_vmid(res, argv[1], 0) == -1) 998 errx(1, "invalid id: %s", argv[1]); 999 } else if (argc != 2) 1000 ctl_usage(res->ctl); 1001 1002 return (vmmaction(res)); 1003 } 1004 1005 int 1006 ctl_unpause(struct parse_result *res, int argc, char *argv[]) 1007 { 1008 if (argc == 2) { 1009 if (parse_vmid(res, argv[1], 0) == -1) 1010 errx(1, "invalid id: %s", argv[1]); 1011 } else if (argc != 2) 1012 ctl_usage(res->ctl); 1013 1014 return (vmmaction(res)); 1015 } 1016 1017 int 1018 ctl_send(struct parse_result *res, int argc, char *argv[]) 1019 { 1020 if (pledge("stdio unix sendfd unveil", NULL) == -1) 1021 err(1, "pledge"); 1022 if (argc == 2) { 1023 if (parse_vmid(res, argv[1], 0) == -1) 1024 errx(1, "invalid id: %s", argv[1]); 1025 } else if (argc != 2) 1026 ctl_usage(res->ctl); 1027 1028 return (vmmaction(res)); 1029 } 1030 1031 int 1032 ctl_receive(struct parse_result *res, int argc, char *argv[]) 1033 { 1034 if (pledge("stdio unix sendfd unveil", NULL) == -1) 1035 err(1, "pledge"); 1036 if (argc == 2) { 1037 if (parse_vmid(res, argv[1], 1) == -1) 1038 errx(1, "invalid id: %s", argv[1]); 1039 } else if (argc != 2) 1040 ctl_usage(res->ctl); 1041 1042 return (vmmaction(res)); 1043 } 1044 1045 __dead void 1046 ctl_openconsole(const char *name) 1047 { 1048 closefrom(STDERR_FILENO + 1); 1049 if (unveil(VMCTL_CU, "x") == -1) 1050 err(1, "unveil"); 1051 execl(VMCTL_CU, VMCTL_CU, "-r", "-l", name, "-s", "115200", 1052 (char *)NULL); 1053 err(1, "failed to open the console"); 1054 } 1055