1#!/bin/sh
2#
3# Copyright (c) 2013 NetApp, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD: stable/11/share/examples/bhyve/vmrun.sh 335698 2018-06-27 07:18:54Z kib $
28#
29
30LOADER=/usr/sbin/bhyveload
31BHYVECTL=/usr/sbin/bhyvectl
32FBSDRUN=/usr/sbin/bhyve
33
34DEFAULT_MEMSIZE=512M
35DEFAULT_CPUS=2
36DEFAULT_TAPDEV=tap0
37DEFAULT_CONSOLE=stdio
38
39DEFAULT_NIC=virtio-net
40DEFAULT_DISK=virtio-blk
41DEFAULT_VIRTIO_DISK="./diskdev"
42DEFAULT_ISOFILE="./release.iso"
43
44DEFAULT_VNCHOST="127.0.0.1"
45DEFAULT_VNCPORT=5900
46DEFAULT_VNCSIZE="w=1024,h=768"
47
48errmsg() {
49	echo "*** $1"
50}
51
52usage() {
53	local msg=$1
54
55	echo "Usage: vmrun.sh [-aAEhiTv] [-c <CPUs>] [-C <console>]" \
56	    "[-d <disk file>]"
57	echo "                [-e <name=value>] [-f <path of firmware>]" \
58	    "[-F <size>]"
59	echo "                [-g <gdbport> ] [-H <directory>]"
60	echo "                [-I <location of installation iso>] [-l <loader>]"
61	echo "                [-L <VNC IP for UEFI framebuffer>]"
62	echo "                [-m <memsize>]" \
63	    "[-n <network adapter emulation type>]"
64	echo "                [-P <port>] [-t <tapdev>] <vmname>"
65	echo ""
66	echo "       -h: display this help message"
67	echo "       -a: force memory mapped local APIC access"
68	echo "       -A: use AHCI disk emulation instead of ${DEFAULT_DISK}"
69	echo "       -c: number of virtual cpus (default: ${DEFAULT_CPUS})"
70	echo "       -C: console device (default: ${DEFAULT_CONSOLE})"
71	echo "       -d: virtio diskdev file (default: ${DEFAULT_VIRTIO_DISK})"
72	echo "       -e: set FreeBSD loader environment variable"
73	echo "       -E: Use UEFI mode"
74	echo "       -f: Use a specific UEFI firmware"
75	echo "       -F: Use a custom UEFI GOP framebuffer size" \
76	    "(default: ${DEFAULT_VNCSIZE}"
77	echo "       -g: listen for connection from kgdb at <gdbport>"
78	echo "       -H: host filesystem to export to the loader"
79	echo "       -i: force boot of the Installation CDROM image"
80	echo "       -I: Installation CDROM image location" \
81	    "(default: ${DEFAULT_ISOFILE})"
82	echo "       -l: the OS loader to use (default: /boot/userboot.so)"
83	echo "       -L: IP address for UEFI GOP VNC server" \
84	    "(default: ${DEFAULT_VNCHOST}"
85	echo "       -m: memory size (default: ${DEFAULT_MEMSIZE})"
86	echo "       -n: network adapter emulation type" \
87	    "(default: ${DEFAULT_NIC})"
88	echo "       -p: pass-through a host PCI device at bus/slot/func" \
89	    "(e.g. 10/0/0)"
90	echo "       -P: UEFI GOP VNC port (default: ${DEFAULT_VNCPORT})"
91	echo "       -t: tap device for virtio-net (default: $DEFAULT_TAPDEV)"
92	echo "       -T: Enable tablet device (for UEFI GOP)"
93	echo "       -u: RTC keeps UTC time"
94	echo "       -v: Wait for VNC client connection before booting VM"
95	echo "       -w: ignore unimplemented MSRs"
96	echo ""
97	[ -n "$msg" ] && errmsg "$msg"
98	exit 1
99}
100
101if [ `id -u` -ne 0 ]; then
102	errmsg "This script must be executed with superuser privileges"
103	exit 1
104fi
105
106kldstat -n vmm > /dev/null 2>&1 
107if [ $? -ne 0 ]; then
108	errmsg "vmm.ko is not loaded"
109	exit 1
110fi
111
112force_install=0
113isofile=${DEFAULT_ISOFILE}
114memsize=${DEFAULT_MEMSIZE}
115console=${DEFAULT_CONSOLE}
116cpus=${DEFAULT_CPUS}
117nic=${DEFAULT_NIC}
118tap_total=0
119disk_total=0
120disk_emulation=${DEFAULT_DISK}
121gdbport=0
122loader_opt=""
123bhyverun_opt="-H -A -P"
124pass_total=0
125
126# EFI-specific options
127efi_mode=0
128efi_firmware="/usr/local/share/uefi-firmware/BHYVE_UEFI.fd"
129vncwait=""
130vnchost=${DEFAULT_VNCHOST}
131vncport=${DEFAULT_VNCPORT}
132vncsize=${DEFAULT_VNCSIZE}
133tablet=""
134
135while getopts aAc:C:d:e:Ef:F:g:hH:iI:l:L:m:n:p:P:t:Tuvw c ; do
136	case $c in
137	a)
138		bhyverun_opt="${bhyverun_opt} -a"
139		;;
140	A)
141		disk_emulation="ahci-hd"
142		;;
143	c)
144		cpus=${OPTARG}
145		;;
146	C)
147		console=${OPTARG}
148		;;
149	d)
150		disk_dev=${OPTARG%%,*}
151		disk_opts=${OPTARG#${disk_dev}}
152		eval "disk_dev${disk_total}=\"${disk_dev}\""
153		eval "disk_opts${disk_total}=\"${disk_opts}\""
154		disk_total=$(($disk_total + 1))
155		;;
156	e)
157		loader_opt="${loader_opt} -e ${OPTARG}"
158		;;
159	E)
160		efi_mode=1
161		;;
162	f)
163		efi_firmware="${OPTARG}"
164		;;
165	F)
166		vncsize="${OPTARG}"
167		;;
168	g)	
169		gdbport=${OPTARG}
170		;;
171	H)
172		host_base=`realpath ${OPTARG}`
173		;;
174	i)
175		force_install=1
176		;;
177	I)
178		isofile=${OPTARG}
179		;;
180	l)
181		loader_opt="${loader_opt} -l ${OPTARG}"
182		;;
183	L)
184		vnchost="${OPTARG}"
185		;;
186	m)
187		memsize=${OPTARG}
188		;;
189	n)
190		nic=${OPTARG}
191		;;
192	p)
193		eval "pass_dev${pass_total}=\"${OPTARG}\""
194		pass_total=$(($pass_total + 1))
195		;;
196	P)
197		vncport="${OPTARG}"
198		;;
199	t)
200		eval "tap_dev${tap_total}=\"${OPTARG}\""
201		tap_total=$(($tap_total + 1))
202		;;
203	T)
204		tablet="-s 30,xhci,tablet"
205		;;
206	u)	
207		bhyverun_opt="${bhyverun_opt} -u"
208		;;
209	v)
210		vncwait=",wait"
211		;;
212	w)
213		bhyverun_opt="${bhyverun_opt} -w"
214		;;
215	*)
216		usage
217		;;
218	esac
219done
220
221if [ $tap_total -eq 0 ] ; then
222    tap_total=1
223    tap_dev0="${DEFAULT_TAPDEV}"
224fi
225if [ $disk_total -eq 0 ] ; then
226    disk_total=1
227    disk_dev0="${DEFAULT_VIRTIO_DISK}"
228
229fi
230
231shift $((${OPTIND} - 1))
232
233if [ $# -ne 1 ]; then
234	usage "virtual machine name not specified"
235fi
236
237vmname="$1"
238if [ -n "${host_base}" ]; then
239	loader_opt="${loader_opt} -h ${host_base}"
240fi
241
242# If PCI passthru devices are configured then guest memory must be wired
243if [ ${pass_total} -gt 0 ]; then
244	loader_opt="${loader_opt} -S"
245	bhyverun_opt="${bhyverun_opt} -S"
246fi
247
248if [ ${efi_mode} -gt 0 ]; then
249	if [ ! -f ${efi_firmware} ]; then
250		echo "Error: EFI Firmware ${efi_firmware} doesn't exist." \
251		    "Try: pkg install uefi-edk2-bhyve"
252		exit 1
253	fi
254fi
255
256make_and_check_diskdev()
257{
258    local virtio_diskdev="$1"
259    # Create the virtio diskdev file if needed
260    if [ ! -e ${virtio_diskdev} ]; then
261	    echo "virtio disk device file \"${virtio_diskdev}\" does not exist."
262	    echo "Creating it ..."
263	    truncate -s 8G ${virtio_diskdev} > /dev/null
264    fi
265
266    if [ ! -r ${virtio_diskdev} ]; then
267	    echo "virtio disk device file \"${virtio_diskdev}\" is not readable"
268	    exit 1
269    fi
270
271    if [ ! -w ${virtio_diskdev} ]; then
272	    echo "virtio disk device file \"${virtio_diskdev}\" is not writable"
273	    exit 1
274    fi
275}
276
277echo "Launching virtual machine \"$vmname\" ..."
278
279first_diskdev="$disk_dev0"
280
281${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
282
283while [ 1 ]; do
284
285	file -s ${first_diskdev} | grep "boot sector" > /dev/null
286	rc=$?
287	if [ $rc -ne 0 ]; then
288		file -s ${first_diskdev} | \
289		    grep ": Unix Fast File sys" > /dev/null
290		rc=$?
291	fi
292	if [ $rc -ne 0 ]; then
293		need_install=1
294	else
295		need_install=0
296	fi
297
298	if [ $force_install -eq 1 -o $need_install -eq 1 ]; then
299		if [ ! -r ${isofile} ]; then
300			echo -n "Installation CDROM image \"${isofile}\" "
301			echo    "is not readable"
302			exit 1
303		fi
304		BOOTDISKS="-d ${isofile}"
305		installer_opt="-s 31:0,ahci-cd,${isofile}"
306	else
307		BOOTDISKS=""
308		i=0
309		while [ $i -lt $disk_total ] ; do
310			eval "disk=\$disk_dev${i}"
311			if [ -r ${disk} ] ; then
312				BOOTDISKS="$BOOTDISKS -d ${disk} "
313			fi
314			i=$(($i + 1))
315		done
316		installer_opt=""
317	fi
318
319	if [ ${efi_mode} -eq 0 ]; then
320		${LOADER} -c ${console} -m ${memsize} ${BOOTDISKS} \
321		    ${loader_opt} ${vmname}
322		bhyve_exit=$?
323		if [ $bhyve_exit -ne 0 ]; then
324			break
325		fi
326	fi
327
328	#
329	# Build up args for additional tap and disk devices now.
330	#
331	nextslot=2  # slot 0 is hostbridge, slot 1 is lpc
332	devargs=""  # accumulate disk/tap args here
333	i=0
334	while [ $i -lt $tap_total ] ; do
335	    eval "tapname=\$tap_dev${i}"
336	    devargs="$devargs -s $nextslot:0,${nic},${tapname} "
337	    nextslot=$(($nextslot + 1))
338	    i=$(($i + 1))
339	done
340
341	i=0
342	while [ $i -lt $disk_total ] ; do
343	    eval "disk=\$disk_dev${i}"
344	    eval "opts=\$disk_opts${i}"
345	    make_and_check_diskdev "${disk}"
346	    devargs="$devargs -s $nextslot:0,$disk_emulation,${disk}${opts} "
347	    nextslot=$(($nextslot + 1))
348	    i=$(($i + 1))
349	done
350
351	i=0
352	while [ $i -lt $pass_total ] ; do
353	    eval "pass=\$pass_dev${i}"
354	    devargs="$devargs -s $nextslot:0,passthru,${pass} "
355	    nextslot=$(($nextslot + 1))
356	    i=$(($i + 1))
357        done
358
359	efiargs=""
360	if [ ${efi_mode} -gt 0 ]; then
361		efiargs="-s 29,fbuf,tcp=${vnchost}:${vncport},"
362		efiargs="${efiargs}${vncsize}${vncwait}"
363		efiargs="${efiargs} -l bootrom,${efi_firmware}"
364		efiargs="${efiargs} ${tablet}"
365	fi
366
367	${FBSDRUN} -c ${cpus} -m ${memsize} ${bhyverun_opt}		\
368		-g ${gdbport}						\
369		-s 0:0,hostbridge					\
370		-s 1:0,lpc						\
371		${efiargs}						\
372		${devargs}						\
373		-l com1,${console}					\
374		${installer_opt}					\
375		${vmname}
376
377	bhyve_exit=$?
378	# bhyve returns the following status codes:
379	#  0 - VM has been reset
380	#  1 - VM has been powered off
381	#  2 - VM has been halted
382	#  3 - VM generated a triple fault
383	#  all other non-zero status codes are errors
384	#
385	if [ $bhyve_exit -ne 0 ]; then
386		break
387	fi
388done
389
390
391case $bhyve_exit in
392	0|1|2)
393		# Cleanup /dev/vmm entry when bhyve did not exit
394		# due to an error.
395		${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
396		;;
397esac
398
399exit $bhyve_exit
400