xref: /openbsd/usr.sbin/vmctl/main.c (revision 771fbea0)
1 /*	$OpenBSD: main.c,v 1.65 2021/05/12 20:13:00 dv 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 					errno = ret;
307 					err(1, "command failed");
308 				} else
309 					errx(1, "command failed");
310 			}
311 
312 			ret = 0;
313 			switch (action) {
314 			case CMD_START:
315 				done = vm_start_complete(&imsg, &ret,
316 				    tty_autoconnect);
317 				break;
318 			case CMD_WAITFOR:
319 				flags = VMOP_WAIT;
320 				/* FALLTHROUGH */
321 			case CMD_STOP:
322 				done = terminate_vm_complete(&imsg, &ret,
323 				    flags);
324 				break;
325 			case CMD_CONSOLE:
326 			case CMD_STATUS:
327 			case CMD_STOPALL:
328 				done = add_info(&imsg, &ret);
329 				break;
330 			case CMD_PAUSE:
331 				done = pause_vm_complete(&imsg, &ret);
332 				break;
333 			case CMD_RECEIVE:
334 				done = vm_start_complete(&imsg, &ret, 0);
335 				break;
336 			case CMD_UNPAUSE:
337 				done = unpause_vm_complete(&imsg, &ret);
338 				break;
339 			default:
340 				done = 1;
341 				break;
342 			}
343 
344 			imsg_free(&imsg);
345 		}
346 	}
347 
348 	if (ret)
349 		return (1);
350 	else
351 		return (0);
352 }
353 
354 void
355 parse_free(struct parse_result *res)
356 {
357 	size_t	 i;
358 
359 	free(res->name);
360 	free(res->path);
361 	free(res->isopath);
362 	free(res->instance);
363 	for (i = 0; i < res->ndisks; i++)
364 		free(res->disks[i]);
365 	free(res->disks);
366 	free(res->disktypes);
367 	memset(res, 0, sizeof(*res));
368 }
369 
370 int
371 parse_ifs(struct parse_result *res, char *word, int val)
372 {
373 	const char	*error;
374 
375 	if (word != NULL) {
376 		val = strtonum(word, 1, INT_MAX, &error);
377 		if (error != NULL)  {
378 			warnx("count is %s: %s", error, word);
379 			return (-1);
380 		}
381 	}
382 	res->nifs = val;
383 
384 	return (0);
385 }
386 
387 int
388 parse_network(struct parse_result *res, char *word)
389 {
390 	char		**nets;
391 	char		*s;
392 
393 	if ((nets = reallocarray(res->nets, res->nnets + 1,
394 	    sizeof(char *))) == NULL) {
395 		warn("reallocarray");
396 		return (-1);
397 	}
398 	if ((s = strdup(word)) == NULL) {
399 		warn("strdup");
400 		return (-1);
401 	}
402 	nets[res->nnets] = s;
403 	res->nets = nets;
404 	res->nnets++;
405 
406 	return (0);
407 }
408 
409 int
410 parse_size(struct parse_result *res, char *word)
411 {
412 	long long val = 0;
413 
414 	if (word != NULL) {
415 		if (scan_scaled(word, &val) != 0) {
416 			warn("invalid size: %s", word);
417 			return (-1);
418 		}
419 	}
420 
421 	if (val < (1024 * 1024)) {
422 		warnx("size must be at least one megabyte");
423 		return (-1);
424 	} else
425 		res->size = val / 1024 / 1024;
426 
427 	if ((res->size * 1024 * 1024) != val)
428 		warnx("size rounded to %lld megabytes", res->size);
429 
430 	return (0);
431 }
432 
433 int
434 parse_disktype(const char *s, const char **ret)
435 {
436 	char		 buf[BUFSIZ];
437 	const char	*ext;
438 	int		 fd;
439 	ssize_t		 len;
440 
441 	*ret = s;
442 
443 	/* Try to parse the explicit format (qcow2:disk.qc2) */
444 	if (strstr(s, RAW_FMT) == s && *(s + strlen(RAW_FMT)) == ':') {
445 		*ret = s + strlen(RAW_FMT) + 1;
446 		return (VMDF_RAW);
447 	}
448 	if (strstr(s, QCOW2_FMT) == s && *(s + strlen(QCOW2_FMT)) == ':') {
449 		*ret = s + strlen(QCOW2_FMT) + 1;
450 		return (VMDF_QCOW2);
451 	}
452 
453 	/* Or try to derive the format from the file signature */
454 	if ((fd = open(s, O_RDONLY)) != -1) {
455 		len = read(fd, buf, sizeof(buf));
456 		close(fd);
457 
458 		if (len >= (ssize_t)strlen(VM_MAGIC_QCOW) &&
459 		    strncmp(buf, VM_MAGIC_QCOW,
460 		    strlen(VM_MAGIC_QCOW)) == 0) {
461 			/* Return qcow2, the version will be checked later */
462 			return (VMDF_QCOW2);
463 		}
464 	}
465 
466 	/*
467 	 * Use the extension as a last option.  This is needed for
468 	 * 'vmctl create' as the file, and the signature, doesn't
469 	 * exist yet.
470 	 */
471 	if ((ext = strrchr(s, '.')) != NULL && *(++ext) != '\0') {
472 		if (strcasecmp(ext, RAW_FMT) == 0)
473 			return (VMDF_RAW);
474 		else if (strcasecmp(ext, QCOW2_FMT) == 0)
475 			return (VMDF_QCOW2);
476 	}
477 
478 	/* Fallback to raw */
479 	return (VMDF_RAW);
480 }
481 
482 int
483 parse_disk(struct parse_result *res, char *word, int type)
484 {
485 	char		**disks;
486 	int		*disktypes;
487 	char		*s;
488 
489 	if ((disks = reallocarray(res->disks, res->ndisks + 1,
490 	    sizeof(char *))) == NULL) {
491 		warn("reallocarray");
492 		return (-1);
493 	}
494 	if ((disktypes = reallocarray(res->disktypes, res->ndisks + 1,
495 	    sizeof(int))) == NULL) {
496 		warn("reallocarray");
497 		return -1;
498 	}
499 	if ((s = strdup(word)) == NULL) {
500 		warn("strdup");
501 		return (-1);
502 	}
503 	disks[res->ndisks] = s;
504 	disktypes[res->ndisks] = type;
505 	res->disks = disks;
506 	res->disktypes = disktypes;
507 	res->ndisks++;
508 
509 	return (0);
510 }
511 
512 int
513 parse_vmid(struct parse_result *res, char *word, int needname)
514 {
515 	const char	*error;
516 	uint32_t	 id;
517 
518 	if (word == NULL) {
519 		warnx("missing vmid argument");
520 		return (-1);
521 	}
522 	if (*word == '-') {
523 		/* don't print a warning to allow command line options */
524 		return (-1);
525 	}
526 	id = strtonum(word, 0, UINT32_MAX, &error);
527 	if (error == NULL) {
528 		if (needname) {
529 			warnx("invalid vm name");
530 			return (-1);
531 		} else {
532 			res->id = id;
533 			res->name = NULL;
534 		}
535 	} else {
536 		if (strlen(word) >= VMM_MAX_NAME_LEN) {
537 			warnx("name too long");
538 			return (-1);
539 		}
540 		res->id = 0;
541 		if ((res->name = strdup(word)) == NULL)
542 			errx(1, "strdup");
543 	}
544 
545 	return (0);
546 }
547 
548 int
549 parse_instance(struct parse_result *res, char *word)
550 {
551 	if (strlen(word) >= VMM_MAX_NAME_LEN) {
552 		warnx("instance vm name too long");
553 		return (-1);
554 	}
555 	res->id = 0;
556 	if ((res->instance = strdup(word)) == NULL)
557 		errx(1, "strdup");
558 
559 	return (0);
560 }
561 
562 int
563 ctl_create(struct parse_result *res, int argc, char *argv[])
564 {
565 	int		 ch, ret, type;
566 	const char	*disk, *format, *base = NULL, *input = NULL;
567 
568 	while ((ch = getopt(argc, argv, "b:i:s:")) != -1) {
569 		switch (ch) {
570 		case 'b':
571 			base = optarg;
572 			break;
573 		case 'i':
574 			input = optarg;
575 			break;
576 		case 's':
577 			if (parse_size(res, optarg) != 0)
578 				errx(1, "invalid size: %s", optarg);
579 			break;
580 		default:
581 			ctl_usage(res->ctl);
582 			/* NOTREACHED */
583 		}
584 	}
585 	argc -= optind;
586 	argv += optind;
587 
588 	if (argc < 1)
589 		ctl_usage(res->ctl);
590 
591 	type = parse_disktype(argv[0], &disk);
592 
593 	if (pledge("stdio rpath wpath cpath", NULL) == -1)
594 		err(1, "pledge");
595 
596 	if (input) {
597 		if (base && input)
598 			errx(1, "conflicting -b and -i arguments");
599 		return ctl_convert(input, disk, type, res->size);
600 	}
601 
602 	if (base && type != VMDF_QCOW2)
603 		errx(1, "base images require qcow2 disk format");
604 	if (res->size == 0 && !base) {
605 		fprintf(stderr, "could not create %s: missing size argument\n",
606 		    disk);
607 		ctl_usage(res->ctl);
608 	}
609 
610 	if ((ret = create_imagefile(type, disk, base, res->size, &format)) != 0) {
611 		errno = ret;
612 		err(1, "create imagefile operation failed");
613 	} else
614 		warnx("%s imagefile created", format);
615 
616 	return (0);
617 }
618 
619 int
620 ctl_convert(const char *srcfile, const char *dstfile, int dsttype, size_t dstsize)
621 {
622 	struct {
623 		int			 fd;
624 		int			 type;
625 		struct virtio_backing	 file;
626 		const char		*disk;
627 		off_t			 size;
628 	}	 src, dst;
629 	int		 ret;
630 	const char	*format, *errstr = NULL;
631 	uint8_t		*buf = NULL, *zerobuf = NULL;
632 	size_t		 buflen;
633 	ssize_t		 len, rlen;
634 	off_t		 off;
635 
636 	memset(&src, 0, sizeof(src));
637 	memset(&dst, 0, sizeof(dst));
638 
639 	src.type = parse_disktype(srcfile, &src.disk);
640 	dst.type = dsttype;
641 	dst.disk = dstfile;
642 
643 	if ((src.fd = open_imagefile(src.type, src.disk, O_RDONLY,
644 	    &src.file, &src.size)) == -1) {
645 		errstr = "failed to open source image file";
646 		goto done;
647 	}
648 
649 	if (dstsize == 0)
650 		dstsize = src.size;
651 	else
652 		dstsize *= 1048576;
653 	if (dstsize < (size_t)src.size) {
654 		errstr = "size cannot be smaller than input disk size";
655 		goto done;
656 	}
657 
658 	/* align to megabytes */
659 	dst.size = ALIGNSZ(dstsize, 1048576);
660 
661 	if ((ret = create_imagefile(dst.type, dst.disk, NULL,
662 	   dst.size / 1048576, &format)) != 0) {
663 		errno = ret;
664 		errstr = "failed to create destination image file";
665 		goto done;
666 	}
667 
668 	if ((dst.fd = open_imagefile(dst.type, dst.disk, O_RDWR,
669 	    &dst.file, &dst.size)) == -1) {
670 		errstr = "failed to open destination image file";
671 		goto done;
672 	}
673 
674 	if (pledge("stdio", NULL) == -1)
675 		err(1, "pledge");
676 
677 	/*
678 	 * Use 64k buffers by default.  This could also be adjusted to
679 	 * the backend cluster size.
680 	 */
681 	buflen = 1 << 16;
682 	if ((buf = calloc(1, buflen)) == NULL ||
683 	    (zerobuf = calloc(1, buflen)) == NULL) {
684 		errstr = "failed to allocated buffers";
685 		goto done;
686 	}
687 
688 	for (off = 0; off < dst.size; off += len) {
689 		/* Read input from the source image */
690 		if (off < src.size) {
691 			len = MIN((off_t)buflen, src.size - off);
692 			if ((rlen = src.file.pread(src.file.p,
693 			    buf, (size_t)len, off)) != len) {
694 				errno = EIO;
695 				errstr = "failed to read from source";
696 				goto done;
697 			}
698 		} else
699 			len = 0;
700 
701 		/* and pad the remaining bytes */
702 		if (len < (ssize_t)buflen) {
703 			log_debug("%s: padding %zd zero bytes at offset %lld",
704 			    format, buflen - len, off + len);
705 			memset(buf + len, 0, buflen - len);
706 			len = buflen;
707 		}
708 
709 		/*
710 		 * No need to copy empty buffers.  This allows the backend,
711 		 * sparse files or QCOW2 images, to save space in the
712 		 * destination file.
713 		 */
714 		if (memcmp(buf, zerobuf, buflen) == 0)
715 			continue;
716 
717 		log_debug("%s: writing %zd of %lld bytes at offset %lld",
718 		    format, len, dst.size, off);
719 
720 		if ((rlen = dst.file.pwrite(dst.file.p,
721 		    buf, (size_t)len, off)) != len) {
722 			errno = EIO;
723 			errstr = "failed to write to destination";
724 			goto done;
725 		}
726 	}
727 
728 	if (dstsize < (size_t)dst.size)
729 		warnx("destination size rounded to %lld megabytes",
730 		    dst.size / 1048576);
731 
732  done:
733 	free(buf);
734 	free(zerobuf);
735 	if (src.file.p != NULL)
736 		src.file.close(src.file.p, 0);
737 	if (dst.file.p != NULL)
738 		dst.file.close(dst.file.p, 0);
739 	if (errstr != NULL)
740 		errx(1, "%s", errstr);
741 	else
742 		warnx("%s imagefile created", format);
743 
744 	return (0);
745 }
746 
747 int
748 ctl_status(struct parse_result *res, int argc, char *argv[])
749 {
750 	if (argc == 2) {
751 		if (parse_vmid(res, argv[1], 0) == -1)
752 			errx(1, "invalid id: %s", argv[1]);
753 	} else if (argc > 2)
754 		ctl_usage(res->ctl);
755 
756 	return (vmmaction(res));
757 }
758 
759 int
760 ctl_load(struct parse_result *res, int argc, char *argv[])
761 {
762 	if (argc != 2)
763 		ctl_usage(res->ctl);
764 
765 	if ((res->path = strdup(argv[1])) == NULL)
766 		err(1, "strdup");
767 
768 	return (vmmaction(res));
769 }
770 
771 int
772 ctl_log(struct parse_result *res, int argc, char *argv[])
773 {
774 	if (argc != 2)
775 		ctl_usage(res->ctl);
776 
777 	if (strncasecmp("brief", argv[1], strlen(argv[1])) == 0)
778 		res->verbose = 0;
779 	else if (strncasecmp("verbose", argv[1], strlen(argv[1])) == 0)
780 		res->verbose = 2;
781 	else
782 		ctl_usage(res->ctl);
783 
784 	return (vmmaction(res));
785 }
786 
787 int
788 ctl_reload(struct parse_result *res, int argc, char *argv[])
789 {
790 	if (argc != 1)
791 		ctl_usage(res->ctl);
792 
793 	return (vmmaction(res));
794 }
795 
796 int
797 ctl_reset(struct parse_result *res, int argc, char *argv[])
798 {
799 	if (argc == 2) {
800 		if (strcasecmp("all", argv[1]) == 0)
801 			res->mode = CONFIG_ALL;
802 		else if (strcasecmp("vms", argv[1]) == 0)
803 			res->mode = CONFIG_VMS;
804 		else if (strcasecmp("switches", argv[1]) == 0)
805 			res->mode = CONFIG_SWITCHES;
806 		else
807 			ctl_usage(res->ctl);
808 	} else if (argc > 2)
809 		ctl_usage(res->ctl);
810 
811 	if (res->mode == 0)
812 		res->mode = CONFIG_ALL;
813 
814 	return (vmmaction(res));
815 }
816 
817 int
818 ctl_start(struct parse_result *res, int argc, char *argv[])
819 {
820 	int		 ch, i, type;
821 	char		 path[PATH_MAX];
822 	const char	*s;
823 
824 	while ((ch = getopt(argc, argv, "b:B:cd:i:Lm:n:r:t:")) != -1) {
825 		switch (ch) {
826 		case 'b':
827 			if (res->path)
828 				errx(1, "boot image specified multiple times");
829 			if (realpath(optarg, path) == NULL)
830 				err(1, "invalid boot image path");
831 			if ((res->path = strdup(path)) == NULL)
832 				errx(1, "strdup");
833 			break;
834 		case 'B':
835 			if (res->bootdevice)
836 				errx(1, "boot device specified multiple times");
837 			if (strcmp("disk", optarg) == 0)
838 				res->bootdevice = VMBOOTDEV_DISK;
839 			else if (strcmp("cdrom", optarg) == 0)
840 				res->bootdevice = VMBOOTDEV_CDROM;
841 			else if (strcmp("net", optarg) == 0)
842 				res->bootdevice = VMBOOTDEV_NET;
843 			else
844 				errx(1, "unknown boot device %s", optarg);
845 			break;
846 		case 'r':
847 			if (res->isopath)
848 				errx(1, "iso image specified multiple times");
849 			if (realpath(optarg, path) == NULL)
850 				err(1, "invalid iso image path");
851 			if ((res->isopath = strdup(path)) == NULL)
852 				errx(1, "strdup");
853 			break;
854 		case 'c':
855 			tty_autoconnect = 1;
856 			break;
857 		case 'L':
858 			if (parse_network(res, ".") != 0)
859 				errx(1, "invalid network: %s", optarg);
860 			break;
861 		case 'm':
862 			if (res->size)
863 				errx(1, "memory specified multiple times");
864 			if (parse_size(res, optarg) != 0)
865 				errx(1, "invalid memory size: %s", optarg);
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");
1032 	execl(VMCTL_CU, VMCTL_CU, "-r", "-l", name, "-s", "115200",
1033 	    (char *)NULL);
1034 	err(1, "failed to open the console");
1035 }
1036