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 " [-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 " -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" \
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"
98 [ -n "$msg" ] && errmsg "$msg"
102 if [ `id -u` -ne 0 ]; then
103 errmsg "This script must be executed with superuser privileges"
107 kldstat -n vmm > /dev/null 2>&1
108 if [ $? -ne 0 ]; then
109 errmsg "vmm.ko is not loaded"
114 isofile=${DEFAULT_ISOFILE}
115 memsize=${DEFAULT_MEMSIZE}
116 console=${DEFAULT_CONSOLE}
121 disk_emulation=${DEFAULT_DISK}
123 bhyverun_opt="-H -A -P"
126 # EFI-specific options
128 efi_firmware="/usr/local/share/uefi-firmware/BHYVE_UEFI.fd"
130 vnchost=${DEFAULT_VNCHOST}
131 vncport=${DEFAULT_VNCPORT}
132 vncsize=${DEFAULT_VNCSIZE}
135 while getopts aAc:C:d:e:Ef:F:g:hH:iI:l:L:m:n:p:P:t:Tuvw c ; do
138 bhyverun_opt="${bhyverun_opt} -a"
141 disk_emulation="ahci-hd"
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))
157 loader_opt="${loader_opt} -e ${OPTARG}"
163 efi_firmware="${OPTARG}"
169 host_base=`realpath ${OPTARG}`
178 loader_opt="${loader_opt} -l ${OPTARG}"
190 eval "pass_dev${pass_total}=\"${OPTARG}\""
191 pass_total=$(($pass_total + 1))
197 eval "tap_dev${tap_total}=\"${OPTARG}\""
198 tap_total=$(($tap_total + 1))
201 tablet="-s 30,xhci,tablet"
204 bhyverun_opt="${bhyverun_opt} -u"
210 bhyverun_opt="${bhyverun_opt} -w"
218 if [ $tap_total -eq 0 ] ; then
220 tap_dev0="${DEFAULT_TAPDEV}"
222 if [ $disk_total -eq 0 ] ; then
224 disk_dev0="${DEFAULT_VIRTIO_DISK}"
228 shift $((${OPTIND} - 1))
230 if [ $# -ne 1 ]; then
231 usage "virtual machine name not specified"
235 if [ -n "${host_base}" ]; then
236 loader_opt="${loader_opt} -h ${host_base}"
239 # If PCI passthru devices are configured then guest memory must be wired
240 if [ ${pass_total} -gt 0 ]; then
241 loader_opt="${loader_opt} -S"
242 bhyverun_opt="${bhyverun_opt} -S"
245 if [ ${efi_mode} -gt 0 ]; then
246 if [ ! -f ${efi_firmware} ]; then
247 echo "Error: EFI Firmware ${efi_firmware} doesn't exist." \
248 "Try: pkg install uefi-edk2-bhyve"
253 make_and_check_diskdev()
255 local virtio_diskdev="$1"
256 # Create the virtio diskdev file if needed
257 if [ ! -e ${virtio_diskdev} ]; then
258 echo "virtio disk device file \"${virtio_diskdev}\" does not exist."
259 echo "Creating it ..."
260 truncate -s 8G ${virtio_diskdev} > /dev/null
263 if [ ! -r ${virtio_diskdev} ]; then
264 echo "virtio disk device file \"${virtio_diskdev}\" is not readable"
268 if [ ! -w ${virtio_diskdev} ]; then
269 echo "virtio disk device file \"${virtio_diskdev}\" is not writable"
274 echo "Launching virtual machine \"$vmname\" ..."
276 first_diskdev="$disk_dev0"
278 ${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
282 file -s ${first_diskdev} | grep "boot sector" > /dev/null
284 if [ $rc -ne 0 ]; then
285 file -s ${first_diskdev} | \
286 grep ": Unix Fast File sys" > /dev/null
289 if [ $rc -ne 0 ]; then
295 if [ $force_install -eq 1 -o $need_install -eq 1 ]; then
296 if [ ! -r ${isofile} ]; then
297 echo -n "Installation CDROM image \"${isofile}\" "
298 echo "is not readable"
301 BOOTDISKS="-d ${isofile}"
302 installer_opt="-s 31:0,ahci-cd,${isofile}"
306 while [ $i -lt $disk_total ] ; do
307 eval "disk=\$disk_dev${i}"
308 if [ -r ${disk} ] ; then
309 BOOTDISKS="$BOOTDISKS -d ${disk} "
316 if [ ${efi_mode} -eq 0 ]; then
317 ${LOADER} -c ${console} -m ${memsize} ${BOOTDISKS} \
318 ${loader_opt} ${vmname}
320 if [ $bhyve_exit -ne 0 ]; then
326 # Build up args for additional tap and disk devices now.
328 nextslot=2 # slot 0 is hostbridge, slot 1 is lpc
329 devargs="" # accumulate disk/tap args here
331 while [ $i -lt $tap_total ] ; do
332 eval "tapname=\$tap_dev${i}"
333 devargs="$devargs -s $nextslot:0,${nic},${tapname} "
334 nextslot=$(($nextslot + 1))
339 while [ $i -lt $disk_total ] ; do
340 eval "disk=\$disk_dev${i}"
341 eval "opts=\$disk_opts${i}"
342 make_and_check_diskdev "${disk}"
343 devargs="$devargs -s $nextslot:0,$disk_emulation,${disk}${opts} "
344 nextslot=$(($nextslot + 1))
349 while [ $i -lt $pass_total ] ; do
350 eval "pass=\$pass_dev${i}"
351 devargs="$devargs -s $nextslot:0,passthru,${pass} "
352 nextslot=$(($nextslot + 1))
357 if [ ${efi_mode} -gt 0 ]; then
358 efiargs="-s 29,fbuf,tcp=${vnchost}:${vncport},"
359 efiargs="${efiargs}${vncsize}${vncwait}"
360 efiargs="${efiargs} -l bootrom,${efi_firmware}"
361 efiargs="${efiargs} ${tablet}"
364 ${FBSDRUN} -c ${cpus} -m ${memsize} ${bhyverun_opt} \
374 # bhyve returns the following status codes:
375 # 0 - VM has been reset
376 # 1 - VM has been powered off
377 # 2 - VM has been halted
378 # 3 - VM generated a triple fault
379 # all other non-zero status codes are errors
381 if [ $bhyve_exit -ne 0 ]; then
389 # Cleanup /dev/vmm entry when bhyve did not exit
391 ${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1