3 # Copyright (c) 2013 NetApp, Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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
30 LOADER=/usr/sbin/bhyveload
31 BHYVECTL=/usr/sbin/bhyvectl
32 FBSDRUN=/usr/sbin/bhyve
39 DEFAULT_VIRTIO_DISK="./diskdev"
40 DEFAULT_ISOFILE="./release.iso"
49 echo "Usage: vmrun.sh [-ahi] [-c <CPUs>] [-C <console>] [-d <disk file>]"
50 echo " [-e <name=value>] [-g <gdbport> ] [-H <directory>]"
51 echo " [-I <location of installation iso>] [-m <memsize>]"
52 echo " [-t <tapdev>] <vmname>"
54 echo " -h: display this help message"
55 echo " -a: force memory mapped local APIC access"
56 echo " -c: number of virtual cpus (default is ${DEFAULT_CPUS})"
57 echo " -C: console device (default is ${DEFAULT_CONSOLE})"
58 echo " -d: virtio diskdev file (default is ${DEFAULT_VIRTIO_DISK})"
59 echo " -e: set FreeBSD loader environment variable"
60 echo " -g: listen for connection from kgdb at <gdbport>"
61 echo " -H: host filesystem to export to the loader"
62 echo " -i: force boot of the Installation CDROM image"
63 echo " -I: Installation CDROM image location (default is ${DEFAULT_ISOFILE})"
64 echo " -m: memory size (default is ${DEFAULT_MEMSIZE})"
65 echo " -p: pass-through a host PCI device at bus/slot/func (e.g. 10/0/0)"
66 echo " -t: tap device for virtio-net (default is $DEFAULT_TAPDEV)"
68 [ -n "$msg" ] && errmsg "$msg"
72 if [ `id -u` -ne 0 ]; then
73 errmsg "This script must be executed with superuser privileges"
77 kldstat -n vmm > /dev/null 2>&1
79 errmsg "vmm.ko is not loaded"
84 isofile=${DEFAULT_ISOFILE}
85 memsize=${DEFAULT_MEMSIZE}
86 console=${DEFAULT_CONSOLE}
95 while getopts ac:C:d:e:g:hH:iI:m:p:t: c ; do
107 disk_dev=${OPTARG%%,*}
108 disk_opts=${OPTARG#${disk_dev}}
109 eval "disk_dev${disk_total}=\"${disk_dev}\""
110 eval "disk_opts${disk_total}=\"${disk_opts}\""
111 disk_total=$(($disk_total + 1))
114 loader_opt="${loader_opt} -e ${OPTARG}"
120 host_base=`realpath ${OPTARG}`
132 eval "pass_dev${pass_total}=\"${OPTARG}\""
133 pass_total=$(($pass_total + 1))
136 eval "tap_dev${tap_total}=\"${OPTARG}\""
137 tap_total=$(($tap_total + 1))
145 if [ $tap_total -eq 0 ] ; then
147 tap_dev0="${DEFAULT_TAPDEV}"
149 if [ $disk_total -eq 0 ] ; then
151 disk_dev0="${DEFAULT_VIRTIO_DISK}"
155 shift $((${OPTIND} - 1))
157 if [ $# -ne 1 ]; then
158 usage "virtual machine name not specified"
162 if [ -n "${host_base}" ]; then
163 loader_opt="${loader_opt} -h ${host_base}"
166 make_and_check_diskdev()
168 local virtio_diskdev="$1"
169 # Create the virtio diskdev file if needed
170 if [ ! -f ${virtio_diskdev} ]; then
171 echo "virtio disk device file \"${virtio_diskdev}\" does not exist."
172 echo "Creating it ..."
173 truncate -s 8G ${virtio_diskdev} > /dev/null
176 if [ ! -r ${virtio_diskdev} ]; then
177 echo "virtio disk device file \"${virtio_diskdev}\" is not readable"
181 if [ ! -w ${virtio_diskdev} ]; then
182 echo "virtio disk device file \"${virtio_diskdev}\" is not writable"
187 echo "Launching virtual machine \"$vmname\" ..."
189 first_diskdev="$disk_dev0"
191 ${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
195 file -s ${first_diskdev} | grep "boot sector" > /dev/null
197 if [ $rc -ne 0 ]; then
198 file -s ${first_diskdev} | grep ": Unix Fast File sys" > /dev/null
201 if [ $rc -ne 0 ]; then
207 if [ $force_install -eq 1 -o $need_install -eq 1 ]; then
208 if [ ! -r ${isofile} ]; then
209 echo -n "Installation CDROM image \"${isofile}\" "
210 echo "is not readable"
213 BOOTDISKS="-d ${isofile}"
214 installer_opt="-s 31:0,ahci-cd,${isofile}"
218 while [ $i -lt $disk_total ] ; do
219 eval "disk=\$disk_dev${i}"
220 if [ -r ${disk} ] ; then
221 BOOTDISKS="$BOOTDISKS -d ${disk} "
228 ${LOADER} -c ${console} -m ${memsize} ${BOOTDISKS} ${loader_opt} \
231 if [ $bhyve_exit -ne 0 ]; then
236 # Build up args for additional tap and disk devices now.
238 nextslot=2 # slot 0 is hostbridge, slot 1 is lpc
239 devargs="" # accumulate disk/tap args here
241 while [ $i -lt $tap_total ] ; do
242 eval "tapname=\$tap_dev${i}"
243 devargs="$devargs -s $nextslot:0,virtio-net,${tapname} "
244 nextslot=$(($nextslot + 1))
249 while [ $i -lt $disk_total ] ; do
250 eval "disk=\$disk_dev${i}"
251 eval "opts=\$disk_opts${i}"
252 make_and_check_diskdev "${disk}"
253 devargs="$devargs -s $nextslot:0,virtio-blk,${disk}${opts} "
254 nextslot=$(($nextslot + 1))
259 while [ $i -lt $pass_total ] ; do
260 eval "pass=\$pass_dev${i}"
261 devargs="$devargs -s $nextslot:0,passthru,${pass} "
262 nextslot=$(($nextslot + 1))
266 ${FBSDRUN} -c ${cpus} -m ${memsize} ${apic_opt} -A -H -P \
276 # bhyve returns the following status codes:
277 # 0 - VM has been reset
278 # 1 - VM has been powered off
279 # 2 - VM has been halted
280 # 3 - VM generated a triple fault
281 # all other non-zero status codes are errors
283 if [ $bhyve_exit -ne 0 ]; then
291 # Cleanup /dev/vmm entry when bhyve did not exit
293 ${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1