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