3 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 # Copyright (c) 2013 NetApp, Inc.
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
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.
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
32 LOADER=/usr/sbin/bhyveload
33 BHYVECTL=/usr/sbin/bhyvectl
34 FBSDRUN=/usr/sbin/bhyve
41 DEFAULT_NIC=virtio-net
42 DEFAULT_DISK=virtio-blk
43 DEFAULT_VIRTIO_DISK="./diskdev"
44 DEFAULT_ISOFILE="./release.iso"
46 DEFAULT_VNCHOST="127.0.0.1"
48 DEFAULT_VNCSIZE="w=1024,h=768"
57 echo "Usage: vmrun.sh [-aAEhiTv] [-c <CPUs>] [-C <console>]" \
59 echo " [-e <name=value>] [-f <path of firmware>]" \
61 echo " [-g <gdbport> ] [-H <directory>]"
62 echo " [-I <location of installation iso>] [-l <loader>]"
63 echo " [-L <VNC IP for UEFI framebuffer>]"
64 echo " [-m <memsize>]" \
65 "[-n <network adapter emulation type>]"
66 echo " [-P <port>] [-t <tapdev>] <vmname>"
68 echo " -h: display this help message"
69 echo " -a: force memory mapped local APIC access"
70 echo " -A: use AHCI disk emulation instead of ${DEFAULT_DISK}"
71 echo " -c: number of virtual cpus (default: ${DEFAULT_CPUS})"
72 echo " -C: console device (default: ${DEFAULT_CONSOLE})"
73 echo " -d: virtio diskdev file (default: ${DEFAULT_VIRTIO_DISK})"
74 echo " -e: set FreeBSD loader environment variable"
75 echo " -E: Use UEFI mode"
76 echo " -f: Use a specific UEFI firmware"
77 echo " -F: Use a custom UEFI GOP framebuffer size" \
78 "(default: ${DEFAULT_VNCSIZE})"
79 echo " -g: listen for connection from kgdb at <gdbport>"
80 echo " -H: host filesystem to export to the loader"
81 echo " -i: force boot of the Installation CDROM image"
82 echo " -I: Installation CDROM image location" \
83 "(default: ${DEFAULT_ISOFILE})"
84 echo " -l: the OS loader to use (default: /boot/userboot.so)"
85 echo " -L: IP address for UEFI GOP VNC server" \
86 "(default: ${DEFAULT_VNCHOST}"
87 echo " -m: memory size (default: ${DEFAULT_MEMSIZE})"
88 echo " -n: network adapter emulation type" \
89 "(default: ${DEFAULT_NIC})"
90 echo " -p: pass-through a host PCI device at bus/slot/func" \
92 echo " -P: UEFI GOP VNC port (default: ${DEFAULT_VNCPORT})"
93 echo " -t: tap device for virtio-net (default: $DEFAULT_TAPDEV)"
94 echo " -T: Enable tablet device (for UEFI GOP)"
95 echo " -u: RTC keeps UTC time"
96 echo " -v: Wait for VNC client connection before booting VM"
97 echo " -w: ignore unimplemented MSRs"
99 [ -n "$msg" ] && errmsg "$msg"
103 if [ `id -u` -ne 0 ]; then
104 errmsg "This script must be executed with superuser privileges"
108 kldstat -n vmm > /dev/null 2>&1
109 if [ $? -ne 0 ]; then
110 errmsg "vmm.ko is not loaded"
115 isofile=${DEFAULT_ISOFILE}
116 memsize=${DEFAULT_MEMSIZE}
117 console=${DEFAULT_CONSOLE}
122 disk_emulation=${DEFAULT_DISK}
125 bhyverun_opt="-H -A -P"
128 # EFI-specific options
130 efi_firmware="/usr/local/share/uefi-firmware/BHYVE_UEFI.fd"
132 vnchost=${DEFAULT_VNCHOST}
133 vncport=${DEFAULT_VNCPORT}
134 vncsize=${DEFAULT_VNCSIZE}
137 while getopts aAc:C:d:e:Ef:F:g:hH:iI:l:L:m:n:p:P:t:Tuvw c ; do
140 bhyverun_opt="${bhyverun_opt} -a"
143 disk_emulation="ahci-hd"
152 disk_dev=${OPTARG%%,*}
153 disk_opts=${OPTARG#${disk_dev}}
154 eval "disk_dev${disk_total}=\"${disk_dev}\""
155 eval "disk_opts${disk_total}=\"${disk_opts}\""
156 disk_total=$(($disk_total + 1))
159 loader_opt="${loader_opt} -e ${OPTARG}"
165 efi_firmware="${OPTARG}"
174 host_base=`realpath ${OPTARG}`
183 loader_opt="${loader_opt} -l ${OPTARG}"
195 eval "pass_dev${pass_total}=\"${OPTARG}\""
196 pass_total=$(($pass_total + 1))
202 eval "tap_dev${tap_total}=\"${OPTARG}\""
203 tap_total=$(($tap_total + 1))
206 tablet="-s 30,xhci,tablet"
209 bhyverun_opt="${bhyverun_opt} -u"
215 bhyverun_opt="${bhyverun_opt} -w"
223 if [ $tap_total -eq 0 ] ; then
225 tap_dev0="${DEFAULT_TAPDEV}"
227 if [ $disk_total -eq 0 ] ; then
229 disk_dev0="${DEFAULT_VIRTIO_DISK}"
233 shift $((${OPTIND} - 1))
235 if [ $# -ne 1 ]; then
236 usage "virtual machine name not specified"
240 if [ -n "${host_base}" ]; then
241 loader_opt="${loader_opt} -h ${host_base}"
244 # If PCI passthru devices are configured then guest memory must be wired
245 if [ ${pass_total} -gt 0 ]; then
246 loader_opt="${loader_opt} -S"
247 bhyverun_opt="${bhyverun_opt} -S"
250 if [ ${efi_mode} -gt 0 ]; then
251 if [ ! -f ${efi_firmware} ]; then
252 echo "Error: EFI Firmware ${efi_firmware} doesn't exist." \
253 "Try: pkg install uefi-edk2-bhyve"
258 make_and_check_diskdev()
260 local virtio_diskdev="$1"
261 # Create the virtio diskdev file if needed
262 if [ ! -e ${virtio_diskdev} ]; then
263 echo "virtio disk device file \"${virtio_diskdev}\" does not exist."
264 echo "Creating it ..."
265 truncate -s 8G ${virtio_diskdev} > /dev/null
268 if [ ! -r ${virtio_diskdev} ]; then
269 echo "virtio disk device file \"${virtio_diskdev}\" is not readable"
273 if [ ! -w ${virtio_diskdev} ]; then
274 echo "virtio disk device file \"${virtio_diskdev}\" is not writable"
279 echo "Launching virtual machine \"$vmname\" ..."
281 first_diskdev="$disk_dev0"
283 ${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
287 file -s ${first_diskdev} | grep "boot sector" > /dev/null
289 if [ $rc -ne 0 ]; then
290 file -s ${first_diskdev} | \
291 grep ": Unix Fast File sys" > /dev/null
294 if [ $rc -ne 0 ]; then
300 if [ $force_install -eq 1 -o $need_install -eq 1 ]; then
301 if [ ! -r ${isofile} ]; then
302 echo -n "Installation CDROM image \"${isofile}\" "
303 echo "is not readable"
306 BOOTDISKS="-d ${isofile}"
307 installer_opt="-s 31:0,ahci-cd,${isofile}"
311 while [ $i -lt $disk_total ] ; do
312 eval "disk=\$disk_dev${i}"
313 if [ -r ${disk} ] ; then
314 BOOTDISKS="$BOOTDISKS -d ${disk} "
321 if [ ${efi_mode} -eq 0 ]; then
322 ${LOADER} -c ${console} -m ${memsize} ${BOOTDISKS} \
323 ${loader_opt} ${vmname}
325 if [ $bhyve_exit -ne 0 ]; then
331 # Build up args for additional tap and disk devices now.
333 nextslot=2 # slot 0 is hostbridge, slot 1 is lpc
334 devargs="" # accumulate disk/tap args here
336 while [ $i -lt $tap_total ] ; do
337 eval "tapname=\$tap_dev${i}"
338 devargs="$devargs -s $nextslot:0,${nic},${tapname} "
339 nextslot=$(($nextslot + 1))
344 while [ $i -lt $disk_total ] ; do
345 eval "disk=\$disk_dev${i}"
346 eval "opts=\$disk_opts${i}"
347 make_and_check_diskdev "${disk}"
348 devargs="$devargs -s $nextslot:0,$disk_emulation,${disk}${opts} "
349 nextslot=$(($nextslot + 1))
354 while [ $i -lt $pass_total ] ; do
355 eval "pass=\$pass_dev${i}"
356 devargs="$devargs -s $nextslot:0,passthru,${pass} "
357 nextslot=$(($nextslot + 1))
362 if [ ${efi_mode} -gt 0 ]; then
363 efiargs="-s 29,fbuf,tcp=${vnchost}:${vncport},"
364 efiargs="${efiargs}${vncsize}${vncwait}"
365 efiargs="${efiargs} -l bootrom,${efi_firmware}"
366 efiargs="${efiargs} ${tablet}"
369 ${FBSDRUN} -c ${cpus} -m ${memsize} ${bhyverun_opt} \
380 # bhyve returns the following status codes:
381 # 0 - VM has been reset
382 # 1 - VM has been powered off
383 # 2 - VM has been halted
384 # 3 - VM generated a triple fault
385 # all other non-zero status codes are errors
387 if [ $bhyve_exit -ne 0 ]; then
395 # Cleanup /dev/vmm entry when bhyve did not exit
397 ${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1