xref: /freebsd/share/examples/bhyve/vmrun.sh (revision 525d00dcf45f6f83b188cf1ad7bfcfcc8ea39eca)
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 [-aAEhiTuvw] [-9 <9p share>=<path>[,<opts>]]"
57	echo "                [-c <CPUs>] [-C <console>] [-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 <pcidev|bus/slot/func>]"
66	echo "                [-P <port>] [-t <tapdev>] <vmname>"
67	echo ""
68	echo "       -h: display this help message"
69	echo "       -9: virtio 9p (VirtFS) device to share directory"
70	echo "       -a: force memory mapped local APIC access"
71	echo "       -A: use AHCI disk emulation instead of ${DEFAULT_DISK}"
72	echo "       -c: number of virtual cpus (default: ${DEFAULT_CPUS})"
73	echo "       -C: console device (default: ${DEFAULT_CONSOLE})"
74	echo "       -d: virtio diskdev file (default: ${DEFAULT_VIRTIO_DISK})"
75	echo "       -e: set FreeBSD loader environment variable"
76	echo "       -E: Use UEFI mode (amd64 only)"
77	echo "       -f: Use a specific boot firmware (e.g., EDK2, U-Boot)"
78	echo "       -F: Use a custom UEFI GOP framebuffer size" \
79	    "(default: ${DEFAULT_VNCSIZE}) (amd64 only)"
80	echo "       -G: bind the GDB stub to the specified address"
81	echo "       -H: host filesystem to export to the loader"
82	echo "       -i: force boot of the Installation CDROM image"
83	echo "       -I: Installation CDROM image location" \
84	    "(default: ${DEFAULT_ISOFILE})"
85	echo "       -l: the OS loader to use (default: /boot/userboot.so) (amd64 only)"
86	echo "       -L: IP address for UEFI GOP VNC server" \
87	    "(default: ${DEFAULT_VNCHOST})"
88	echo "       -m: memory size (default: ${DEFAULT_MEMSIZE})"
89	echo "       -n: network adapter emulation type" \
90	    "(default: ${DEFAULT_NIC})"
91	echo "       -p: pass-through a host PCI device (e.g ppt0 or" \
92	    "bus/slot/func) (amd64 only)"
93	echo "       -P: UEFI GOP VNC port (default: ${DEFAULT_VNCPORT})"
94	echo "       -t: tap device for virtio-net (default: $DEFAULT_TAPDEV)"
95	echo "       -T: Enable tablet device (for UEFI GOP) (amd64 only)"
96	echo "       -u: RTC keeps UTC time"
97	echo "       -v: Wait for VNC client connection before booting VM"
98	echo "       -w: ignore unimplemented MSRs (amd64 only)"
99	echo ""
100	[ -n "$msg" ] && errmsg "$msg"
101	exit 1
102}
103
104if [ `id -u` -ne 0 ]; then
105	errmsg "This script must be executed with superuser privileges"
106	exit 1
107fi
108
109kldstat -n vmm > /dev/null 2>&1
110if [ $? -ne 0 ]; then
111	errmsg "vmm.ko is not loaded"
112	exit 1
113fi
114
115platform=$(uname -m)
116if [ "${platform}" != amd64 -a "${platform}" != arm64 ]; then
117	errmsg "This script is only supported on amd64 and arm64 platforms"
118	exit 1
119fi
120
121force_install=0
122isofile=${DEFAULT_ISOFILE}
123memsize=${DEFAULT_MEMSIZE}
124console=${DEFAULT_CONSOLE}
125cpus=${DEFAULT_CPUS}
126nic=${DEFAULT_NIC}
127tap_total=0
128disk_total=0
129disk_emulation=${DEFAULT_DISK}
130loader_opt=""
131pass_total=0
132plan9_total=0
133
134# EFI-specific options
135efi_mode=0
136efi_firmware="/usr/local/share/uefi-firmware/BHYVE_UEFI.fd"
137vncwait=""
138vnchost=${DEFAULT_VNCHOST}
139vncport=${DEFAULT_VNCPORT}
140vncsize=${DEFAULT_VNCSIZE}
141tablet=""
142
143# arm64 only
144uboot_firmware="/usr/local/share/u-boot/u-boot-bhyve-arm64/u-boot.bin"
145
146case ${platform} in
147amd64)
148	bhyverun_opt="-H -P"
149	opts="9:aAc:C:d:e:Ef:F:G:hH:iI:l:L:m:n:p:P:t:Tuvw"
150	;;
151arm64)
152	bhyverun_opt=""
153	opts="9:aAc:C:d:e:f:F:G:hH:iI:L:m:n:P:t:uv"
154	;;
155esac
156
157while getopts $opts c ; do
158	case $c in
159	9)
160		plan9_share=${OPTARG%%=*}
161		plan9_rest=${OPTARG#${plan9_share}=}
162		plan9_path=${plan9_rest%%,*}
163		plan9_opts=${plan9_rest#${plan9_path}}
164		eval "plan9_share${plan9_total}=\"${plan9_share}\""
165		eval "plan9_path${plan9_total}=\"${plan9_path}\""
166		eval "plan9_opts${plan9_total}=\"${plan9_opts}\""
167		plan9_total=$(($plan9_total + 1))
168		;;
169	a)
170		bhyverun_opt="${bhyverun_opt} -a"
171		;;
172	A)
173		disk_emulation="ahci-hd"
174		;;
175	c)
176		cpus=${OPTARG}
177		;;
178	C)
179		console=${OPTARG}
180		;;
181	d)
182		disk_dev=${OPTARG%%,*}
183		disk_opts=${OPTARG#${disk_dev}}
184		eval "disk_dev${disk_total}=\"${disk_dev}\""
185		eval "disk_opts${disk_total}=\"${disk_opts}\""
186		disk_total=$(($disk_total + 1))
187		;;
188	e)
189		loader_opt="${loader_opt} -e ${OPTARG}"
190		;;
191	E)
192		efi_mode=1
193		;;
194	f)
195		firmware="${OPTARG}"
196		;;
197	F)
198		vncsize="${OPTARG}"
199		;;
200	G)
201		bhyverun_opt="${bhyverun_opt} -G ${OPTARG}"
202		;;
203	H)
204		host_base=`realpath ${OPTARG}`
205		;;
206	i)
207		force_install=1
208		;;
209	I)
210		isofile=${OPTARG}
211		;;
212	l)
213		loader_opt="${loader_opt} -l ${OPTARG}"
214		;;
215	L)
216		vnchost="${OPTARG}"
217		;;
218	m)
219		memsize=${OPTARG}
220		;;
221	n)
222		nic=${OPTARG}
223		;;
224	p)
225		eval "pass_dev${pass_total}=\"${OPTARG}\""
226		pass_total=$(($pass_total + 1))
227		;;
228	P)
229		vncport="${OPTARG}"
230		;;
231	t)
232		eval "tap_dev${tap_total}=\"${OPTARG}\""
233		tap_total=$(($tap_total + 1))
234		;;
235	T)
236		tablet="-s 30,xhci,tablet"
237		;;
238	u)
239		bhyverun_opt="${bhyverun_opt} -u"
240		;;
241	v)
242		vncwait=",wait"
243		;;
244	w)
245		bhyverun_opt="${bhyverun_opt} -w"
246		;;
247	*)
248		usage
249		;;
250	esac
251done
252
253if [ $tap_total -eq 0 ] ; then
254    tap_total=1
255    tap_dev0="${DEFAULT_TAPDEV}"
256fi
257if [ $disk_total -eq 0 ] ; then
258    disk_total=1
259    disk_dev0="${DEFAULT_VIRTIO_DISK}"
260
261fi
262
263shift $((${OPTIND} - 1))
264
265if [ $# -ne 1 ]; then
266	usage "virtual machine name not specified"
267fi
268
269vmname="$1"
270if [ -n "${host_base}" ]; then
271	loader_opt="${loader_opt} -h ${host_base}"
272fi
273
274# If PCI passthru devices are configured then guest memory must be wired
275if [ ${pass_total} -gt 0 ]; then
276	loader_opt="${loader_opt} -S"
277	bhyverun_opt="${bhyverun_opt} -S"
278fi
279
280if [ -z "$firmware" ]; then
281	case ${platform} in
282	amd64)
283		if [ ${efi_mode} -ne 0 ]; then
284			firmware="${efi_firmware}"
285			firmware_pkg="edk2-bhyve"
286		fi
287		;;
288	arm64)
289		firmware="${uboot_firmware}"
290		firmware_pkg="u-boot-bhyve-arm64"
291		;;
292	esac
293fi
294
295if [ -n "${firmware}" -a ! -f "${firmware}" ]; then
296	echo "Error: Firmware file ${firmware} doesn't exist."
297	if [ -n "${firmware_pkg}" ]; then
298		echo "       Try: pkg install ${firmware_pkg}"
299	fi
300	exit 1
301fi
302
303make_and_check_diskdev()
304{
305    local virtio_diskdev="$1"
306    # Create the virtio diskdev file if needed
307    if [ ! -e ${virtio_diskdev} ]; then
308	    echo "virtio disk device file \"${virtio_diskdev}\" does not exist."
309	    echo "Creating it ..."
310	    truncate -s 8G ${virtio_diskdev} > /dev/null
311    fi
312
313    if [ ! -r ${virtio_diskdev} ]; then
314	    echo "virtio disk device file \"${virtio_diskdev}\" is not readable"
315	    exit 1
316    fi
317
318    if [ ! -w ${virtio_diskdev} ]; then
319	    echo "virtio disk device file \"${virtio_diskdev}\" is not writable"
320	    exit 1
321    fi
322}
323
324echo "Launching virtual machine \"$vmname\" ..."
325
326first_diskdev="$disk_dev0"
327
328${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
329
330while [ 1 ]; do
331
332	file -s ${first_diskdev} | grep "boot sector" > /dev/null
333	rc=$?
334	if [ $rc -ne 0 ]; then
335		file -s ${first_diskdev} | \
336		    grep ": Unix Fast File sys" > /dev/null
337		rc=$?
338	fi
339	if [ $rc -ne 0 ]; then
340		need_install=1
341	else
342		need_install=0
343	fi
344
345	if [ $force_install -eq 1 -o $need_install -eq 1 ]; then
346		if [ ! -r ${isofile} ]; then
347			echo -n "Installation CDROM image \"${isofile}\" "
348			echo    "is not readable"
349			exit 1
350		fi
351		BOOTDISKS="-d ${isofile}"
352		installer_opt="-s 31:0,ahci-cd,${isofile}"
353	else
354		BOOTDISKS=""
355		i=0
356		while [ $i -lt $disk_total ] ; do
357			eval "disk=\$disk_dev${i}"
358			if [ -r ${disk} ] ; then
359				BOOTDISKS="$BOOTDISKS -d ${disk} "
360			fi
361			i=$(($i + 1))
362		done
363		installer_opt=""
364	fi
365
366	if [ ${platform} = amd64 -a ${efi_mode} -eq 0 ]; then
367		${LOADER} -c ${console} -m ${memsize} ${BOOTDISKS} \
368		    ${loader_opt} ${vmname}
369		bhyve_exit=$?
370		if [ $bhyve_exit -ne 0 ]; then
371			break
372		fi
373	fi
374
375	#
376	# Build up args for additional tap and disk devices now.
377	#
378	devargs="-s 0:0,hostbridge"  # accumulate disk/tap args here
379	case ${platform} in
380	amd64)
381		console_opt="-l com1,${console}"
382		devargs="$devargs -s 1:0,lpc "
383		nextslot=2  # slot 0 is hostbridge, slot 1 is lpc
384		;;
385	arm64)
386		console_opt="-o console=${console}"
387		devargs="$devargs -o bootrom=${firmware} "
388		nextslot=1  # slot 0 is hostbridge
389		;;
390	esac
391
392	i=0
393	while [ $i -lt $disk_total ] ; do
394	    eval "disk=\$disk_dev${i}"
395	    eval "opts=\$disk_opts${i}"
396	    make_and_check_diskdev "${disk}"
397	    devargs="$devargs -s $nextslot:0,$disk_emulation,${disk}${opts} "
398	    nextslot=$(($nextslot + 1))
399	    i=$(($i + 1))
400	done
401
402	i=0
403	while [ $i -lt $plan9_total ] ; do
404	    eval "share=\$plan9_share${i}"
405	    eval "path=\$plan9_path${i}"
406	    eval "opts=\$plan9_opts${i}"
407	    if [ ! -d ${path} ]; then
408		echo "virtio-9p \"${path}\" is not a directory"
409		exit 1
410	    fi
411	    devargs="$devargs -s $nextslot,virtio-9p,${share}=${path}${opts} "
412	    nextslot=$(($nextslot + 1))
413	    i=$(($i + 1))
414	done
415
416	i=0
417	while [ $i -lt $tap_total ] ; do
418	    eval "tapname=\$tap_dev${i}"
419	    devargs="$devargs -s $nextslot:0,${nic},${tapname} "
420	    nextslot=$(($nextslot + 1))
421	    i=$(($i + 1))
422	done
423
424	i=0
425	while [ $i -lt $pass_total ] ; do
426		eval "pass=\$pass_dev${i}"
427		bsfform="$(echo "${pass}" | grep "^[0-9]\+/[0-9]\+/[0-9]\+$")"
428		if [ -z "${bsfform}" ]; then
429			bsf="$(pciconf -l "${pass}" 2>/dev/null)"
430			if [ $? -ne 0 ]; then
431				errmsg "${pass} is not a host PCI device"
432				exit 1
433			fi
434			bsf="$(echo "${bsf}" | awk -F: '{print $2"/"$3"/"$4}')"
435		else
436			bsf="${pass}"
437		fi
438		devargs="$devargs -s $nextslot:0,passthru,${bsf} "
439		nextslot=$(($nextslot + 1))
440		i=$(($i + 1))
441        done
442
443	efiargs=""
444	if [ ${efi_mode} -gt 0 ]; then
445		efiargs="-s 29,fbuf,tcp=${vnchost}:${vncport},"
446		efiargs="${efiargs}${vncsize}${vncwait}"
447		efiargs="${efiargs} -l bootrom,${firmware}"
448		efiargs="${efiargs} ${tablet}"
449	fi
450
451	${FBSDRUN} -c ${cpus} -m ${memsize} ${bhyverun_opt}		\
452		${efiargs}						\
453		${devargs}						\
454		${console_opt}						\
455		${installer_opt}					\
456		${vmname}
457
458	bhyve_exit=$?
459	# bhyve returns the following status codes:
460	#  0 - VM has been reset
461	#  1 - VM has been powered off
462	#  2 - VM has been halted
463	#  3 - VM generated a triple fault
464	#  all other non-zero status codes are errors
465	#
466	if [ $bhyve_exit -ne 0 ]; then
467		break
468	fi
469done
470
471
472case $bhyve_exit in
473	0|1|2)
474		# Cleanup /dev/vmm entry when bhyve did not exit
475		# due to an error.
476		${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
477		;;
478esac
479
480exit $bhyve_exit
481