xref: /openbsd/usr.sbin/vmctl/vmctl.c (revision 55cc5ba3)
1 /*	$OpenBSD: vmctl.c,v 1.76 2021/01/27 07:21:12 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 2014 Mike Larkin <mlarkin@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/queue.h>
20 #include <sys/uio.h>
21 #include <sys/stat.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 
25 #include <machine/vmmvar.h>
26 
27 #include <ctype.h>
28 #include <err.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <imsg.h>
32 #include <limits.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <util.h>
38 #include <pwd.h>
39 #include <grp.h>
40 
41 #include "vmd.h"
42 #include "virtio.h"
43 #include "vmctl.h"
44 #include "atomicio.h"
45 
46 extern char *__progname;
47 uint32_t info_id;
48 char info_name[VMM_MAX_NAME_LEN];
49 enum actions info_action;
50 unsigned int info_flags;
51 
52 struct imsgbuf *ibuf;
53 
54 /*
55  * vm_start
56  *
57  * Request vmd to start the VM defined by the supplied parameters
58  *
59  * Parameters:
60  *  start_id: optional ID of the VM
61  *  name: optional name of the VM
62  *  memsize: memory size (MB) of the VM to create
63  *  nnics: number of vionet network interfaces to create
64  *  nics: switch names of the network interfaces to create
65  *  ndisks: number of disk images
66  *  disks: disk image file names
67  *  kernel: kernel image to load
68  *  iso: iso image file
69  *  instance: create instance from vm
70  *
71  * Return:
72  *  0 if the request to start the VM was sent successfully.
73  *  ENOMEM if a memory allocation failure occurred.
74  */
75 int
76 vm_start(uint32_t start_id, const char *name, int memsize, int nnics,
77     char **nics, int ndisks, char **disks, int *disktypes, char *kernel,
78     char *iso, char *instance, unsigned int bootdevice)
79 {
80 	struct vmop_create_params *vmc;
81 	struct vm_create_params *vcp;
82 	unsigned int flags = 0;
83 	int i;
84 	const char *s;
85 
86 	if (memsize)
87 		flags |= VMOP_CREATE_MEMORY;
88 	if (nnics)
89 		flags |= VMOP_CREATE_NETWORK;
90 	if (ndisks)
91 		flags |= VMOP_CREATE_DISK;
92 	if (kernel)
93 		flags |= VMOP_CREATE_KERNEL;
94 	if (iso)
95 		flags |= VMOP_CREATE_CDROM;
96 	if (instance)
97 		flags |= VMOP_CREATE_INSTANCE;
98 	else if (flags != 0) {
99 		if (memsize < 1)
100 			memsize = VM_DEFAULT_MEMORY;
101 		if (ndisks > VMM_MAX_DISKS_PER_VM)
102 			errx(1, "too many disks");
103 		else if (ndisks == 0)
104 			warnx("starting without disks");
105 		if (kernel == NULL && ndisks == 0 && !iso)
106 			errx(1, "no kernel or disk/cdrom specified");
107 		if (nnics == -1)
108 			nnics = 0;
109 		if (nnics > VMM_MAX_NICS_PER_VM)
110 			errx(1, "too many network interfaces");
111 		if (nnics == 0)
112 			warnx("starting without network interfaces");
113 	}
114 
115 	if ((vmc = calloc(1, sizeof(struct vmop_create_params))) == NULL)
116 		return (ENOMEM);
117 
118 	vmc->vmc_flags = flags;
119 
120 	/* vcp includes configuration that is shared with the kernel */
121 	vcp = &vmc->vmc_params;
122 
123 	/*
124 	 * XXX: vmd(8) fills in the actual memory ranges. vmctl(8)
125 	 * just passes in the actual memory size in MB here.
126 	 */
127 	vcp->vcp_nmemranges = 1;
128 	vcp->vcp_memranges[0].vmr_size = memsize;
129 
130 	vcp->vcp_ncpus = 1;
131 	vcp->vcp_ndisks = ndisks;
132 	vcp->vcp_nnics = nnics;
133 	vcp->vcp_id = start_id;
134 
135 	for (i = 0 ; i < ndisks; i++) {
136 		if (strlcpy(vcp->vcp_disks[i], disks[i],
137 		    sizeof(vcp->vcp_disks[i])) >=
138 		    sizeof(vcp->vcp_disks[i]))
139 			errx(1, "disk path too long");
140 		vmc->vmc_disktypes[i] = disktypes[i];
141 	}
142 	for (i = 0 ; i < nnics; i++) {
143 		vmc->vmc_ifflags[i] = VMIFF_UP;
144 
145 		if (strcmp(".", nics[i]) == 0) {
146 			/* Add a "local" interface */
147 			(void)strlcpy(vmc->vmc_ifswitch[i], "",
148 			    sizeof(vmc->vmc_ifswitch[i]));
149 			vmc->vmc_ifflags[i] |= VMIFF_LOCAL;
150 		} else {
151 			/* Add an interface to a switch */
152 			if (strlcpy(vmc->vmc_ifswitch[i], nics[i],
153 			    sizeof(vmc->vmc_ifswitch[i])) >=
154 			    sizeof(vmc->vmc_ifswitch[i]))
155 				errx(1, "interface name too long");
156 		}
157 	}
158 	if (name != NULL) {
159 		/*
160 		 * Allow VMs names with alphanumeric characters, dot, hyphen
161 		 * and underscore. But disallow dot, hyphen and underscore at
162 		 * the start.
163 		 */
164 		if (*name == '-' || *name == '.' || *name == '_')
165 			errx(1, "invalid VM name");
166 
167 		for (s = name; *s != '\0'; ++s) {
168 			if (!(isalnum(*s) || *s == '.' || *s == '-' ||
169 			    *s == '_'))
170 				errx(1, "invalid VM name");
171 		}
172 
173 		if (strlcpy(vcp->vcp_name, name,
174 		    sizeof(vcp->vcp_name)) >= sizeof(vcp->vcp_name))
175 			errx(1, "vm name too long");
176 	}
177 	if (kernel != NULL)
178 		if (strlcpy(vcp->vcp_kernel, kernel,
179 		    sizeof(vcp->vcp_kernel)) >= sizeof(vcp->vcp_kernel))
180 			errx(1, "kernel name too long");
181 	if (iso != NULL)
182 		if (strlcpy(vcp->vcp_cdrom, iso,
183 		    sizeof(vcp->vcp_cdrom)) >= sizeof(vcp->vcp_cdrom))
184 			errx(1, "cdrom name too long");
185 	if (instance != NULL)
186 		if (strlcpy(vmc->vmc_instance, instance,
187 		    sizeof(vmc->vmc_instance)) >= sizeof(vmc->vmc_instance))
188 			errx(1, "instance vm name too long");
189 	vmc->vmc_bootdevice = bootdevice;
190 
191 	imsg_compose(ibuf, IMSG_VMDOP_START_VM_REQUEST, 0, 0, -1,
192 	    vmc, sizeof(struct vmop_create_params));
193 
194 	free(vcp);
195 	return (0);
196 }
197 
198 /*
199  * vm_start_complete
200  *
201  * Callback function invoked when we are expecting an
202  * IMSG_VMDOP_START_VMM_RESPONSE message indicating the completion of
203  * a start vm operation.
204  *
205  * Parameters:
206  *  imsg : response imsg received from vmd
207  *  ret  : return value
208  *  autoconnect : open the console after startup
209  *
210  * Return:
211  *  Always 1 to indicate we have processed the return message (even if it
212  *  was an incorrect/failure message)
213  *
214  *  The function also sets 'ret' to the error code as follows:
215  *   0     : Message successfully processed
216  *   EINVAL: Invalid or unexpected response from vmd
217  *   EIO   : vm_start command failed
218  *   ENOENT: a specified component of the VM could not be found (disk image,
219  *    BIOS firmware image, etc)
220  */
221 int
222 vm_start_complete(struct imsg *imsg, int *ret, int autoconnect)
223 {
224 	struct vmop_result *vmr;
225 	int res;
226 
227 	if (imsg->hdr.type == IMSG_VMDOP_START_VM_RESPONSE) {
228 		vmr = (struct vmop_result *)imsg->data;
229 		res = vmr->vmr_result;
230 		if (res) {
231 			switch (res) {
232 			case VMD_BIOS_MISSING:
233 				warnx("vmm bios firmware file not found.");
234 				*ret = ENOENT;
235 				break;
236 			case VMD_DISK_MISSING:
237 				warnx("could not open disk image(s)");
238 				*ret = ENOENT;
239 				break;
240 			case VMD_CDROM_MISSING:
241 				warnx("could not find specified iso image");
242 				*ret = ENOENT;
243 				break;
244 			case VMD_CDROM_INVALID:
245 				warnx("specified iso image is not a regular "
246 				    "file");
247 				*ret = ENOENT;
248 				break;
249 			case VMD_PARENT_INVALID:
250 				warnx("invalid template");
251 				*ret = EINVAL;
252 				break;
253 			default:
254 				errno = res;
255 				warn("start vm command failed");
256 				*ret = EIO;
257 			}
258 		} else if (autoconnect) {
259 			/* does not return */
260 			ctl_openconsole(vmr->vmr_ttyname);
261 		} else {
262 			warnx("started vm %d successfully, tty %s",
263 			    vmr->vmr_id, vmr->vmr_ttyname);
264 			*ret = 0;
265 		}
266 	} else {
267 		warnx("unexpected response received from vmd");
268 		*ret = EINVAL;
269 	}
270 
271 	return (1);
272 }
273 
274 void
275 send_vm(uint32_t id, const char *name)
276 {
277 	struct vmop_id vid;
278 	int fds[2], readn, writen;
279 	long pagesz;
280 	char *buf;
281 
282 	pagesz = getpagesize();
283 	buf = malloc(pagesz);
284 	if (buf == NULL)
285 		errx(1, "%s: memory allocation failure", __func__);
286 
287 	memset(&vid, 0, sizeof(vid));
288 	vid.vid_id = id;
289 	if (name != NULL)
290 		strlcpy(vid.vid_name, name, sizeof(vid.vid_name));
291 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fds) == -1) {
292 		warnx("%s: socketpair creation failed", __func__);
293 	} else {
294 		imsg_compose(ibuf, IMSG_VMDOP_SEND_VM_REQUEST, 0, 0, fds[0],
295 				&vid, sizeof(vid));
296 		imsg_flush(ibuf);
297 		while (1) {
298 			readn = atomicio(read, fds[1], buf, pagesz);
299 			if (!readn)
300 				break;
301 			writen = atomicio(vwrite, STDOUT_FILENO, buf,
302 					readn);
303 			if (writen != readn)
304 				break;
305 		}
306 		if (vid.vid_id)
307 			warnx("sent vm %d successfully", vid.vid_id);
308 		else
309 			warnx("sent vm %s successfully", vid.vid_name);
310 	}
311 
312 	free(buf);
313 }
314 
315 void
316 vm_receive(uint32_t id, const char *name)
317 {
318 	struct vmop_id vid;
319 	int fds[2], readn, writen;
320 	long pagesz;
321 	char *buf;
322 
323 	pagesz = getpagesize();
324 	buf = malloc(pagesz);
325 	if (buf == NULL)
326 		errx(1, "%s: memory allocation failure", __func__);
327 
328 	memset(&vid, 0, sizeof(vid));
329 	if (name != NULL)
330 		strlcpy(vid.vid_name, name, sizeof(vid.vid_name));
331 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fds) == -1) {
332 		warnx("%s: socketpair creation failed", __func__);
333 	} else {
334 		imsg_compose(ibuf, IMSG_VMDOP_RECEIVE_VM_REQUEST, 0, 0, fds[0],
335 		    &vid, sizeof(vid));
336 		imsg_flush(ibuf);
337 		while (1) {
338 			readn = atomicio(read, STDIN_FILENO, buf, pagesz);
339 			if (!readn) {
340 				close(fds[1]);
341 				break;
342 			}
343 			writen = atomicio(vwrite, fds[1], buf, readn);
344 			if (writen != readn)
345 				break;
346 		}
347 	}
348 
349 	free(buf);
350 }
351 
352 void
353 pause_vm(uint32_t pause_id, const char *name)
354 {
355 	struct vmop_id vid;
356 
357 	memset(&vid, 0, sizeof(vid));
358 	vid.vid_id = pause_id;
359 	if (name != NULL)
360 		(void)strlcpy(vid.vid_name, name, sizeof(vid.vid_name));
361 
362 	imsg_compose(ibuf, IMSG_VMDOP_PAUSE_VM, 0, 0, -1,
363 	    &vid, sizeof(vid));
364 }
365 
366 int
367 pause_vm_complete(struct imsg *imsg, int *ret)
368 {
369 	struct vmop_result *vmr;
370 	int res;
371 
372 	if (imsg->hdr.type == IMSG_VMDOP_PAUSE_VM_RESPONSE) {
373 		vmr = (struct vmop_result *)imsg->data;
374 		res = vmr->vmr_result;
375 		if (res) {
376 			errno = res;
377 			warn("pause vm command failed");
378 			*ret = EIO;
379 		} else {
380 			warnx("paused vm %d successfully", vmr->vmr_id);
381 			*ret = 0;
382 		}
383 	} else {
384 		warnx("unexpected response received from vmd");
385 		*ret = EINVAL;
386 	}
387 
388 	return (1);
389 }
390 
391 void
392 unpause_vm(uint32_t pause_id, const char *name)
393 {
394 	struct vmop_id vid;
395 
396 	memset(&vid, 0, sizeof(vid));
397 	vid.vid_id = pause_id;
398 	if (name != NULL)
399 		(void)strlcpy(vid.vid_name, name, sizeof(vid.vid_name));
400 
401 	imsg_compose(ibuf, IMSG_VMDOP_UNPAUSE_VM, 0, 0, -1,
402 	    &vid, sizeof(vid));
403 }
404 
405 int
406 unpause_vm_complete(struct imsg *imsg, int *ret)
407 {
408 	struct vmop_result *vmr;
409 	int res;
410 
411 	if (imsg->hdr.type == IMSG_VMDOP_UNPAUSE_VM_RESPONSE) {
412 		vmr = (struct vmop_result *)imsg->data;
413 		res = vmr->vmr_result;
414 		if (res) {
415 			errno = res;
416 			warn("unpause vm command failed");
417 			*ret = EIO;
418 		} else {
419 			warnx("unpaused vm %d successfully", vmr->vmr_id);
420 			*ret = 0;
421 		}
422 	} else {
423 		warnx("unexpected response received from vmd");
424 		*ret = EINVAL;
425 	}
426 
427 	return (1);
428 }
429 
430 /*
431  * terminate_vm
432  *
433  * Request vmd to stop the VM indicated
434  *
435  * Parameters:
436  *  terminate_id: ID of the vm to be terminated
437  *  name: optional name of the VM to be terminated
438  *  flags: VMOP_FORCE or VMOP_WAIT flags
439  */
440 void
441 terminate_vm(uint32_t terminate_id, const char *name, unsigned int flags)
442 {
443 	struct vmop_id vid;
444 
445 	memset(&vid, 0, sizeof(vid));
446 	vid.vid_id = terminate_id;
447 	if (name != NULL) {
448 		(void)strlcpy(vid.vid_name, name, sizeof(vid.vid_name));
449 		fprintf(stderr, "stopping vm %s: ", name);
450 	} else {
451 		fprintf(stderr, "stopping vm: ");
452 	}
453 
454 	vid.vid_flags = flags & (VMOP_FORCE|VMOP_WAIT);
455 
456 	imsg_compose(ibuf, IMSG_VMDOP_TERMINATE_VM_REQUEST,
457 	    0, 0, -1, &vid, sizeof(vid));
458 }
459 
460 /*
461  * terminate_vm_complete
462  *
463  * Callback function invoked when we are expecting an
464  * IMSG_VMDOP_TERMINATE_VM_RESPONSE message indicating the completion of
465  * a terminate vm operation.
466  *
467  * Parameters:
468  *  imsg : response imsg received from vmd
469  *  ret  : return value
470  *  flags: VMOP_FORCE or VMOP_WAIT flags
471  *
472  * Return:
473  *  Always 1 to indicate we have processed the return message (even if it
474  *  was an incorrect/failure message)
475  *
476  *  The function also sets 'ret' to the error code as follows:
477  *   0     : Message successfully processed
478  *   EINVAL: Invalid or unexpected response from vmd
479  *   EIO   : terminate_vm command failed
480  */
481 int
482 terminate_vm_complete(struct imsg *imsg, int *ret, unsigned int flags)
483 {
484 	struct vmop_result *vmr;
485 	int res;
486 
487 	if (imsg->hdr.type == IMSG_VMDOP_TERMINATE_VM_RESPONSE) {
488 		vmr = (struct vmop_result *)imsg->data;
489 		res = vmr->vmr_result;
490 		if (res) {
491 			switch (res) {
492 			case VMD_VM_STOP_INVALID:
493 				fprintf(stderr,
494 				    "cannot stop vm that is not running\n");
495 				*ret = EINVAL;
496 				break;
497 			case ENOENT:
498 				fprintf(stderr, "vm not found\n");
499 				*ret = EIO;
500 				break;
501 			case EINTR:
502 				fprintf(stderr, "interrupted call\n");
503 				*ret = EIO;
504 				break;
505 			default:
506 				errno = res;
507 				fprintf(stderr, "failed: %s\n",
508 				    strerror(res));
509 				*ret = EIO;
510 			}
511 		} else if (flags & VMOP_WAIT) {
512 			fprintf(stderr, "terminated vm %d\n", vmr->vmr_id);
513 		} else if (flags & VMOP_FORCE) {
514 			fprintf(stderr, "forced to terminate vm %d\n",
515 			    vmr->vmr_id);
516 		} else {
517 			fprintf(stderr, "requested to shutdown vm %d\n",
518 			    vmr->vmr_id);
519 			*ret = 0;
520 		}
521 	} else {
522 		fprintf(stderr, "unexpected response received from vmd\n");
523 		*ret = EINVAL;
524 	}
525 	errno = *ret;
526 
527 	return (1);
528 }
529 
530 /*
531  * terminate_all
532  *
533  * Request to stop all VMs gracefully
534  *
535  * Parameters
536  *  list: the vm information (consolidated) returned from vmd via imsg
537  *  ct  : the size (number of elements in 'list') of the result
538  *  flags: VMOP_FORCE or VMOP_WAIT flags
539  */
540 void
541 terminate_all(struct vmop_info_result *list, size_t ct, unsigned int flags)
542 {
543 	struct vm_info_result *vir;
544 	struct vmop_info_result *vmi;
545 	struct parse_result res;
546 	size_t i;
547 
548 	for (i = 0; i < ct; i++) {
549 		vmi = &list[i];
550 		vir = &vmi->vir_info;
551 
552 		/* The VM is already stopped */
553 		if (vir->vir_creator_pid == 0 || vir->vir_id == 0)
554 			continue;
555 
556 		memset(&res, 0, sizeof(res));
557 		res.action = CMD_STOP;
558 		res.id = 0;
559 		res.flags = info_flags;
560 
561 		if ((res.name = strdup(vir->vir_name)) == NULL)
562 			errx(1, "strdup");
563 
564 		vmmaction(&res);
565 	}
566 }
567 
568 /*
569  * waitfor_vm
570  *
571  * Wait until vmd stopped the indicated VM
572  *
573  * Parameters:
574  *  terminate_id: ID of the vm to be terminated
575  *  name: optional name of the VM to be terminated
576  */
577 void
578 waitfor_vm(uint32_t terminate_id, const char *name)
579 {
580 	struct vmop_id vid;
581 
582 	memset(&vid, 0, sizeof(vid));
583 	vid.vid_id = terminate_id;
584 	if (name != NULL) {
585 		(void)strlcpy(vid.vid_name, name, sizeof(vid.vid_name));
586 		fprintf(stderr, "waiting for vm %s: ", name);
587 	} else {
588 		fprintf(stderr, "waiting for vm: ");
589 	}
590 
591 	imsg_compose(ibuf, IMSG_VMDOP_WAIT_VM_REQUEST,
592 	    0, 0, -1, &vid, sizeof(vid));
593 }
594 
595 /*
596  * get_info_vm
597  *
598  * Return the list of all running VMs or find a specific VM by ID or name.
599  *
600  * Parameters:
601  *  id: optional ID of a VM to list
602  *  name: optional name of a VM to list
603  *  action: if CMD_CONSOLE or CMD_STOP open a console or terminate the VM.
604  *  flags: optional flags used by the CMD_STOP action.
605  *
606  * Request a list of running VMs from vmd
607  */
608 void
609 get_info_vm(uint32_t id, const char *name, enum actions action,
610     unsigned int flags)
611 {
612 	info_id = id;
613 	if (name != NULL)
614 		(void)strlcpy(info_name, name, sizeof(info_name));
615 	info_action = action;
616 	info_flags = flags;
617 	imsg_compose(ibuf, IMSG_VMDOP_GET_INFO_VM_REQUEST, 0, 0, -1, NULL, 0);
618 }
619 
620 /*
621  * check_info_id
622  *
623  * Check if requested name or ID of a VM matches specified arguments
624  *
625  * Parameters:
626  *  name: name of the VM
627  *  id: ID of the VM
628  */
629 int
630 check_info_id(const char *name, uint32_t id)
631 {
632 	if (info_id == 0 && *info_name == '\0')
633 		return (-1);
634 	if (info_id != 0 && info_id == id)
635 		return (1);
636 	if (*info_name != '\0' && name && strcmp(info_name, name) == 0)
637 		return (1);
638 	return (0);
639 }
640 
641 /*
642  * add_info
643  *
644  * Callback function invoked when we are expecting an
645  * IMSG_VMDOP_GET_INFO_VM_DATA message indicating the receipt of additional
646  * "list vm" data, or an IMSG_VMDOP_GET_INFO_VM_END_DATA message indicating
647  * that no additional "list vm" data will be forthcoming.
648  *
649  * Parameters:
650  *  imsg : response imsg received from vmd
651  *  ret  : return value
652  *
653  * Return:
654  *  0     : the returned data was successfully added to the "list vm" data.
655  *          The caller can expect more data.
656  *  1     : IMSG_VMDOP_GET_INFO_VM_END_DATA received (caller should not call
657  *          add_info again), or an error occurred adding the returned data
658  *          to the "list vm" data. The caller should check the value of
659  *          'ret' to determine which case occurred.
660  *
661  * This function does not return if a VM is found and info_action is CMD_CONSOLE
662  *
663  *  The function also sets 'ret' to the error code as follows:
664  *   0     : Message successfully processed
665  *   EINVAL: Invalid or unexpected response from vmd
666  *   ENOMEM: memory allocation failure
667  */
668 int
669 add_info(struct imsg *imsg, int *ret)
670 {
671 	static size_t ct = 0;
672 	static struct vmop_info_result *vir = NULL;
673 
674 	if (imsg->hdr.type == IMSG_VMDOP_GET_INFO_VM_DATA) {
675 		vir = reallocarray(vir, ct + 1,
676 		    sizeof(struct vmop_info_result));
677 		if (vir == NULL) {
678 			*ret = ENOMEM;
679 			return (1);
680 		}
681 		memcpy(&vir[ct], imsg->data, sizeof(struct vmop_info_result));
682 		ct++;
683 		*ret = 0;
684 		return (0);
685 	} else if (imsg->hdr.type == IMSG_VMDOP_GET_INFO_VM_END_DATA) {
686 		switch (info_action) {
687 		case CMD_CONSOLE:
688 			vm_console(vir, ct);
689 			break;
690 		case CMD_STOPALL:
691 			terminate_all(vir, ct, info_flags);
692 			break;
693 		default:
694 			print_vm_info(vir, ct);
695 			break;
696 		}
697 		free(vir);
698 		*ret = 0;
699 		return (1);
700 	} else {
701 		*ret = EINVAL;
702 		return (1);
703 	}
704 }
705 
706 /*
707  * vm_state
708  *
709  * Returns a string representing the current VM state, note that the order
710  * matters. A paused VM does have the VM_STATE_RUNNING bit set, but
711  * VM_STATE_PAUSED is more significant to report.
712  *
713  * Parameters
714  *  vm_state: mask indicating the vm state
715  */
716 const char *
717 vm_state(unsigned int mask)
718 {
719 	if (mask & VM_STATE_PAUSED)
720 		return "paused";
721 	else if (mask & VM_STATE_WAITING)
722 		return "waiting";
723 	else if (mask & VM_STATE_RUNNING)
724 		return "running";
725 	else if (mask & VM_STATE_SHUTDOWN)
726 		return "stopping";
727 	/* Presence of absence of other flags */
728 	else if (!mask || (mask & VM_STATE_DISABLED))
729 		return "stopped";
730 
731 	return "unknown";
732 }
733 
734 /*
735  * print_vm_info
736  *
737  * Prints the vm information returned from vmd in 'list' to stdout.
738  *
739  * Parameters
740  *  list: the vm information (consolidated) returned from vmd via imsg
741  *  ct  : the size (number of elements in 'list') of the result
742  */
743 void
744 print_vm_info(struct vmop_info_result *list, size_t ct)
745 {
746 	struct vm_info_result *vir;
747 	struct vmop_info_result *vmi;
748 	size_t i;
749 	char *tty;
750 	char curmem[FMT_SCALED_STRSIZE];
751 	char maxmem[FMT_SCALED_STRSIZE];
752 	char user[16], group[16];
753 	const char *name;
754 	int running;
755 
756 	printf("%5s %5s %5s %7s %7s %7s %12s %8s %s\n", "ID", "PID", "VCPUS",
757 	    "MAXMEM", "CURMEM", "TTY", "OWNER", "STATE", "NAME");
758 
759 	for (i = 0; i < ct; i++) {
760 		vmi = &list[i];
761 		vir = &vmi->vir_info;
762 		running = (vir->vir_creator_pid != 0 && vir->vir_id != 0);
763 		if (check_info_id(vir->vir_name, vir->vir_id)) {
764 			/* get user name */
765 			name = user_from_uid(vmi->vir_uid, 1);
766 			if (name == NULL)
767 				(void)snprintf(user, sizeof(user),
768 				    "%d", vmi->vir_uid);
769 			else
770 				(void)strlcpy(user, name, sizeof(user));
771 			/* get group name */
772 			if (vmi->vir_gid != -1) {
773 				name = group_from_gid(vmi->vir_gid, 1);
774 				if (name == NULL)
775 					(void)snprintf(group, sizeof(group),
776 					    ":%lld", vmi->vir_gid);
777 				else
778 					(void)snprintf(group, sizeof(group),
779 					    ":%s", name);
780 				(void)strlcat(user, group, sizeof(user));
781 			}
782 
783 			(void)strlcpy(curmem, "-", sizeof(curmem));
784 			(void)strlcpy(maxmem, "-", sizeof(maxmem));
785 
786 			(void)fmt_scaled(vir->vir_memory_size * 1024 * 1024,
787 			    maxmem);
788 
789 			if (running) {
790 				if (*vmi->vir_ttyname == '\0')
791 					tty = "-";
792 				/* get tty - skip /dev/ path */
793 				else if ((tty = strrchr(vmi->vir_ttyname,
794 				    '/')) == NULL || *++tty == '\0')
795 					tty = list[i].vir_ttyname;
796 
797 				(void)fmt_scaled(vir->vir_used_size, curmem);
798 
799 				/* running vm */
800 				printf("%5u %5u %5zd %7s %7s %7s %12s %8s %s\n",
801 				    vir->vir_id, vir->vir_creator_pid,
802 				    vir->vir_ncpus, maxmem, curmem,
803 				    tty, user, vm_state(vmi->vir_state),
804 				    vir->vir_name);
805 			} else {
806 				/* disabled vm */
807 				printf("%5u %5s %5zd %7s %7s %7s %12s %8s %s\n",
808 				    vir->vir_id, "-",
809 				    vir->vir_ncpus, maxmem, curmem,
810 				    "-", user, vm_state(vmi->vir_state),
811 				    vir->vir_name);
812 			}
813 		}
814 	}
815 }
816 
817 /*
818  * vm_console
819  *
820  * Connects to the vm console returned from vmd in 'list'.
821  *
822  * Parameters
823  *  list: the vm information (consolidated) returned from vmd via imsg
824  *  ct  : the size (number of elements in 'list') of the result
825  */
826 __dead void
827 vm_console(struct vmop_info_result *list, size_t ct)
828 {
829 	struct vmop_info_result *vir;
830 	size_t i;
831 
832 	for (i = 0; i < ct; i++) {
833 		vir = &list[i];
834 		if ((check_info_id(vir->vir_info.vir_name,
835 		    vir->vir_info.vir_id) > 0) &&
836 			(vir->vir_ttyname[0] != '\0')) {
837 			/* does not return */
838 			ctl_openconsole(vir->vir_ttyname);
839 		}
840 	}
841 
842 	errx(1, "console not found");
843 }
844 
845 /*
846  * open_imagefile
847  *
848  * Open an imagefile with the specified type, path and size.
849  *
850  * Parameters:
851  *  type        : format of the image file
852  *  imgfile_path: path to the image file to create
853  *  flags       : flags for open(2), e.g. O_RDONLY
854  *  file        : file structure
855  *  sz		: size of the image file
856  *
857  * Return:
858  *  fd          : Returns file descriptor of the new image file
859  *  -1          : Operation failed.  errno is set.
860  */
861 int
862 open_imagefile(int type, const char *imgfile_path, int flags,
863     struct virtio_backing *file, off_t *sz)
864 {
865 	int	 fd, ret, basefd[VM_MAX_BASE_PER_DISK], nfd, i;
866 	char	 path[PATH_MAX];
867 
868 	*sz = 0;
869 	if ((fd = open(imgfile_path, flags)) == -1)
870 		return (-1);
871 
872 	basefd[0] = fd;
873 	nfd = 1;
874 
875 	errno = 0;
876 	switch (type) {
877 	case VMDF_QCOW2:
878 		if (strlcpy(path, imgfile_path, sizeof(path)) >= sizeof(path))
879 			return (-1);
880 		for (i = 0; i < VM_MAX_BASE_PER_DISK - 1; i++, nfd++) {
881 			if ((ret = virtio_qcow2_get_base(basefd[i],
882 			    path, sizeof(path), imgfile_path)) == -1) {
883 				log_debug("%s: failed to get base %d", __func__, i);
884 				return -1;
885 			} else if (ret == 0)
886 				break;
887 
888 			/*
889 			 * This might be called after unveil is already
890 			 * locked but it is save to ignore the EPERM error
891 			 * here as the subsequent open would fail as well.
892 			 */
893 			if ((ret = unveil(path, "r")) != 0 &&
894 			    (ret != EPERM))
895 				err(1, "unveil");
896 			if ((basefd[i + 1] = open(path, O_RDONLY)) == -1) {
897 				log_warn("%s: failed to open base %s",
898 				    __func__, path);
899 				return (-1);
900 			}
901 		}
902 		ret = virtio_qcow2_init(file, sz, basefd, nfd);
903 		break;
904 	default:
905 		ret = virtio_raw_init(file, sz, &fd, 1);
906 		break;
907 	}
908 
909 	if (ret == -1) {
910 		for (i = 0; i < nfd; i++)
911 			close(basefd[i]);
912 		return (-1);
913 	}
914 
915 	return (fd);
916 }
917 
918 /*
919  * create_imagefile
920  *
921  * Create an empty imagefile with the specified type, path and size.
922  *
923  * Parameters:
924  *  type        : format of the image file
925  *  imgfile_path: path to the image file to create
926  *  base_path   : path to the qcow2 base image
927  *  imgsize     : size of the image file to create (in MB)
928  *  format      : string identifying the format
929  *
930  * Return:
931  *  EEXIST: The requested image file already exists
932  *  0     : Image file successfully created
933  *  Exxxx : Various other Exxxx errno codes due to other I/O errors
934  */
935 int
936 create_imagefile(int type, const char *imgfile_path, const char *base_path,
937     long imgsize, const char **format)
938 {
939 	int	 ret;
940 
941 	switch (type) {
942 	case VMDF_QCOW2:
943 		*format = "qcow2";
944 		ret = virtio_qcow2_create(imgfile_path, base_path, imgsize);
945 		break;
946 	default:
947 		*format = "raw";
948 		ret = virtio_raw_create(imgfile_path, imgsize);
949 		break;
950 	}
951 
952 	return (ret);
953 }
954 
955