xref: /freebsd/share/examples/bhyve/vmrun.sh (revision 06c3fb27)
1#!/bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2013 NetApp, Inc.
6# All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29#
30
31LOADER=/usr/sbin/bhyveload
32BHYVECTL=/usr/sbin/bhyvectl
33FBSDRUN=/usr/sbin/bhyve
34
35DEFAULT_MEMSIZE=512M
36DEFAULT_CPUS=2
37DEFAULT_TAPDEV=tap0
38DEFAULT_CONSOLE=stdio
39
40DEFAULT_NIC=virtio-net
41DEFAULT_DISK=virtio-blk
42DEFAULT_VIRTIO_DISK="./diskdev"
43DEFAULT_ISOFILE="./release.iso"
44
45DEFAULT_VNCHOST="127.0.0.1"
46DEFAULT_VNCPORT=5900
47DEFAULT_VNCSIZE="w=1024,h=768"
48
49errmsg() {
50	echo "*** $1"
51}
52
53usage() {
54	local msg=$1
55
56	echo "Usage: vmrun.sh [-aAEhiTv] [-c <CPUs>] [-C <console>]" \
57	    "[-d <disk file>]"
58	echo "                [-e <name=value>] [-f <path of firmware>]" \
59	    "[-F <size>]"
60	echo "                [-G [w][address:]port] [-H <directory>]"
61	echo "                [-I <location of installation iso>] [-l <loader>]"
62	echo "                [-L <VNC IP for UEFI framebuffer>]"
63	echo "                [-m <memsize>]" \
64	    "[-n <network adapter emulation type>]"
65	echo "                [-P <port>] [-t <tapdev>] <vmname>"
66	echo ""
67	echo "       -h: display this help message"
68	echo "       -a: force memory mapped local APIC access"
69	echo "       -A: use AHCI disk emulation instead of ${DEFAULT_DISK}"
70	echo "       -c: number of virtual cpus (default: ${DEFAULT_CPUS})"
71	echo "       -C: console device (default: ${DEFAULT_CONSOLE})"
72	echo "       -d: virtio diskdev file (default: ${DEFAULT_VIRTIO_DISK})"
73	echo "       -e: set FreeBSD loader environment variable"
74	echo "       -E: Use UEFI mode"
75	echo "       -f: Use a specific UEFI firmware"
76	echo "       -F: Use a custom UEFI GOP framebuffer size" \
77	    "(default: ${DEFAULT_VNCSIZE})"
78	echo "       -G: bind the GDB stub to the specified address"
79	echo "       -H: host filesystem to export to the loader"
80	echo "       -i: force boot of the Installation CDROM image"
81	echo "       -I: Installation CDROM image location" \
82	    "(default: ${DEFAULT_ISOFILE})"
83	echo "       -l: the OS loader to use (default: /boot/userboot.so)"
84	echo "       -L: IP address for UEFI GOP VNC server" \
85	    "(default: ${DEFAULT_VNCHOST})"
86	echo "       -m: memory size (default: ${DEFAULT_MEMSIZE})"
87	echo "       -n: network adapter emulation type" \
88	    "(default: ${DEFAULT_NIC})"
89	echo "       -p: pass-through a host PCI device at bus/slot/func" \
90	    "(e.g. 10/0/0)"
91	echo "       -P: UEFI GOP VNC port (default: ${DEFAULT_VNCPORT})"
92	echo "       -t: tap device for virtio-net (default: $DEFAULT_TAPDEV)"
93	echo "       -T: Enable tablet device (for UEFI GOP)"
94	echo "       -u: RTC keeps UTC time"
95	echo "       -v: Wait for VNC client connection before booting VM"
96	echo "       -w: ignore unimplemented MSRs"
97	echo ""
98	[ -n "$msg" ] && errmsg "$msg"
99	exit 1
100}
101
102if [ `id -u` -ne 0 ]; then
103	errmsg "This script must be executed with superuser privileges"
104	exit 1
105fi
106
107kldstat -n vmm > /dev/null 2>&1
108if [ $? -ne 0 ]; then
109	errmsg "vmm.ko is not loaded"
110	exit 1
111fi
112
113force_install=0
114isofile=${DEFAULT_ISOFILE}
115memsize=${DEFAULT_MEMSIZE}
116console=${DEFAULT_CONSOLE}
117cpus=${DEFAULT_CPUS}
118nic=${DEFAULT_NIC}
119tap_total=0
120disk_total=0
121disk_emulation=${DEFAULT_DISK}
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		bhyverun_opt="${bhyverun_opt} -G ${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 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		-s 0:0,hostbridge					\
369		-s 1:0,lpc						\
370		${efiargs}						\
371		${devargs}						\
372		-l com1,${console}					\
373		${installer_opt}					\
374		${vmname}
375
376	bhyve_exit=$?
377	# bhyve returns the following status codes:
378	#  0 - VM has been reset
379	#  1 - VM has been powered off
380	#  2 - VM has been halted
381	#  3 - VM generated a triple fault
382	#  all other non-zero status codes are errors
383	#
384	if [ $bhyve_exit -ne 0 ]; then
385		break
386	fi
387done
388
389
390case $bhyve_exit in
391	0|1|2)
392		# Cleanup /dev/vmm entry when bhyve did not exit
393		# due to an error.
394		${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
395		;;
396esac
397
398exit $bhyve_exit
399