vmrun.sh revision 284539
1248484Sneel#!/bin/sh 2248484Sneel# 3248484Sneel# Copyright (c) 2013 NetApp, Inc. 4248484Sneel# All rights reserved. 5248484Sneel# 6248484Sneel# Redistribution and use in source and binary forms, with or without 7248484Sneel# modification, are permitted provided that the following conditions 8248484Sneel# are met: 9248484Sneel# 1. Redistributions of source code must retain the above copyright 10248484Sneel# notice, this list of conditions and the following disclaimer. 11248484Sneel# 2. Redistributions in binary form must reproduce the above copyright 12248484Sneel# notice, this list of conditions and the following disclaimer in the 13248484Sneel# documentation and/or other materials provided with the distribution. 14248484Sneel# 15248484Sneel# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16248484Sneel# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17248484Sneel# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18248484Sneel# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19248484Sneel# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20248484Sneel# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21248484Sneel# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22248484Sneel# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23248484Sneel# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24248484Sneel# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25248484Sneel# SUCH DAMAGE. 26248484Sneel# 27248484Sneel# $FreeBSD: head/share/examples/bhyve/vmrun.sh 284539 2015-06-18 06:00:17Z neel $ 28248484Sneel# 29248484Sneel 30248484SneelLOADER=/usr/sbin/bhyveload 31248484SneelBHYVECTL=/usr/sbin/bhyvectl 32248484SneelFBSDRUN=/usr/sbin/bhyve 33248484Sneel 34256176SneelDEFAULT_MEMSIZE=512M 35248484SneelDEFAULT_CPUS=2 36248484SneelDEFAULT_TAPDEV=tap0 37264837SjhbDEFAULT_CONSOLE=stdio 38248484Sneel 39248484SneelDEFAULT_VIRTIO_DISK="./diskdev" 40248484SneelDEFAULT_ISOFILE="./release.iso" 41248484Sneel 42277309Sneelerrmsg() { 43277309Sneel echo "*** $1" 44277309Sneel} 45277309Sneel 46248484Sneelusage() { 47277309Sneel local msg=$1 48277309Sneel 49264837Sjhb echo "Usage: vmrun.sh [-ahi] [-c <CPUs>] [-C <console>] [-d <disk file>]" 50264837Sjhb echo " [-e <name=value>] [-g <gdbport> ] [-H <directory>]" 51264837Sjhb echo " [-I <location of installation iso>] [-m <memsize>]" 52264837Sjhb echo " [-t <tapdev>] <vmname>" 53264837Sjhb echo "" 54248484Sneel echo " -h: display this help message" 55264837Sjhb echo " -a: force memory mapped local APIC access" 56248484Sneel echo " -c: number of virtual cpus (default is ${DEFAULT_CPUS})" 57264837Sjhb echo " -C: console device (default is ${DEFAULT_CONSOLE})" 58248484Sneel echo " -d: virtio diskdev file (default is ${DEFAULT_VIRTIO_DISK})" 59256657Sneel echo " -e: set FreeBSD loader environment variable" 60248840Sneel echo " -g: listen for connection from kgdb at <gdbport>" 61264837Sjhb echo " -H: host filesystem to export to the loader" 62248484Sneel echo " -i: force boot of the Installation CDROM image" 63248484Sneel echo " -I: Installation CDROM image location (default is ${DEFAULT_ISOFILE})" 64256176Sneel echo " -m: memory size (default is ${DEFAULT_MEMSIZE})" 65279925Sglebius echo " -p: pass-through a host PCI device at bus/slot/func (e.g. 10/0/0)" 66248484Sneel echo " -t: tap device for virtio-net (default is $DEFAULT_TAPDEV)" 67248484Sneel echo "" 68277309Sneel [ -n "$msg" ] && errmsg "$msg" 69248484Sneel exit 1 70248484Sneel} 71248484Sneel 72248484Sneelif [ `id -u` -ne 0 ]; then 73277309Sneel errmsg "This script must be executed with superuser privileges" 74277309Sneel exit 1 75248484Sneelfi 76248484Sneel 77248484Sneelkldstat -n vmm > /dev/null 2>&1 78248484Sneelif [ $? -ne 0 ]; then 79277309Sneel errmsg "vmm.ko is not loaded" 80248484Sneel exit 1 81248484Sneelfi 82248484Sneel 83248484Sneelforce_install=0 84248484Sneelisofile=${DEFAULT_ISOFILE} 85248484Sneelmemsize=${DEFAULT_MEMSIZE} 86264837Sjhbconsole=${DEFAULT_CONSOLE} 87248484Sneelcpus=${DEFAULT_CPUS} 88267559Salfredtap_total=0 89267559Salfreddisk_total=0 90248840Sneelgdbport=0 91264837Sjhbloader_opt="" 92284539Sneelbhyverun_opt="-H -A -P" 93279925Sglebiuspass_total=0 94248484Sneel 95279925Sglebiuswhile getopts ac:C:d:e:g:hH:iI:m:p:t: c ; do 96248484Sneel case $c in 97248484Sneel a) 98284539Sneel bhyverun_opt="${bhyverun_opt} -a" 99248484Sneel ;; 100264837Sjhb c) 101264837Sjhb cpus=${OPTARG} 102264837Sjhb ;; 103264837Sjhb C) 104264837Sjhb console=${OPTARG} 105264837Sjhb ;; 106248484Sneel d) 107284023Savg disk_dev=${OPTARG%%,*} 108284023Savg disk_opts=${OPTARG#${disk_dev}} 109284023Savg eval "disk_dev${disk_total}=\"${disk_dev}\"" 110284023Savg eval "disk_opts${disk_total}=\"${disk_opts}\"" 111267559Salfred disk_total=$(($disk_total + 1)) 112248484Sneel ;; 113256657Sneel e) 114264837Sjhb loader_opt="${loader_opt} -e ${OPTARG}" 115256657Sneel ;; 116264837Sjhb g) 117264837Sjhb gdbport=${OPTARG} 118248840Sneel ;; 119264837Sjhb H) 120264837Sjhb host_base=`realpath ${OPTARG}` 121264837Sjhb ;; 122248484Sneel i) 123248484Sneel force_install=1 124248484Sneel ;; 125248484Sneel I) 126248484Sneel isofile=${OPTARG} 127248484Sneel ;; 128248484Sneel m) 129248484Sneel memsize=${OPTARG} 130248484Sneel ;; 131279925Sglebius p) 132279925Sglebius eval "pass_dev${pass_total}=\"${OPTARG}\"" 133279925Sglebius pass_total=$(($pass_total + 1)) 134279925Sglebius ;; 135248484Sneel t) 136267559Salfred eval "tap_dev${tap_total}=\"${OPTARG}\"" 137267559Salfred tap_total=$(($tap_total + 1)) 138248484Sneel ;; 139264837Sjhb *) 140248484Sneel usage 141248484Sneel ;; 142248484Sneel esac 143248484Sneeldone 144248484Sneel 145267559Salfredif [ $tap_total -eq 0 ] ; then 146267559Salfred tap_total=1 147267559Salfred tap_dev0="${DEFAULT_TAPDEV}" 148267559Salfredfi 149267559Salfredif [ $disk_total -eq 0 ] ; then 150267559Salfred disk_total=1 151267559Salfred disk_dev0="${DEFAULT_VIRTIO_DISK}" 152267559Salfred 153267559Salfredfi 154267559Salfred 155248484Sneelshift $((${OPTIND} - 1)) 156248484Sneel 157248484Sneelif [ $# -ne 1 ]; then 158277309Sneel usage "virtual machine name not specified" 159248484Sneelfi 160248484Sneel 161248484Sneelvmname="$1" 162264837Sjhbif [ -n "${host_base}" ]; then 163264837Sjhb loader_opt="${loader_opt} -h ${host_base}" 164264837Sjhbfi 165248484Sneel 166284539Sneel# If PCI passthru devices are configured then guest memory must be wired 167284539Sneelif [ ${pass_total} -gt 0 ]; then 168284539Sneel loader_opt="${loader_opt} -S" 169284539Sneel bhyverun_opt="${bhyverun_opt} -S" 170284539Sneelfi 171284539Sneel 172267559Salfredmake_and_check_diskdev() 173267559Salfred{ 174267559Salfred local virtio_diskdev="$1" 175267559Salfred # Create the virtio diskdev file if needed 176273102Sneel if [ ! -e ${virtio_diskdev} ]; then 177267559Salfred echo "virtio disk device file \"${virtio_diskdev}\" does not exist." 178267559Salfred echo "Creating it ..." 179267559Salfred truncate -s 8G ${virtio_diskdev} > /dev/null 180267559Salfred fi 181248484Sneel 182267559Salfred if [ ! -r ${virtio_diskdev} ]; then 183267559Salfred echo "virtio disk device file \"${virtio_diskdev}\" is not readable" 184267559Salfred exit 1 185267559Salfred fi 186248484Sneel 187267559Salfred if [ ! -w ${virtio_diskdev} ]; then 188267559Salfred echo "virtio disk device file \"${virtio_diskdev}\" is not writable" 189267559Salfred exit 1 190267559Salfred fi 191267559Salfred} 192248484Sneel 193248484Sneelecho "Launching virtual machine \"$vmname\" ..." 194248484Sneel 195284024Savgfirst_diskdev="$disk_dev0" 196267559Salfred 197270513Srodrigc${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1 198270513Srodrigc 199248484Sneelwhile [ 1 ]; do 200248484Sneel 201284024Savg file -s ${first_diskdev} | grep "boot sector" > /dev/null 202248484Sneel rc=$? 203248484Sneel if [ $rc -ne 0 ]; then 204284024Savg file -s ${first_diskdev} | grep ": Unix Fast File sys" > /dev/null 205248484Sneel rc=$? 206248484Sneel fi 207248484Sneel if [ $rc -ne 0 ]; then 208248484Sneel need_install=1 209248484Sneel else 210248484Sneel need_install=0 211248484Sneel fi 212248484Sneel 213248484Sneel if [ $force_install -eq 1 -o $need_install -eq 1 ]; then 214248484Sneel if [ ! -r ${isofile} ]; then 215248484Sneel echo -n "Installation CDROM image \"${isofile}\" " 216248484Sneel echo "is not readable" 217248484Sneel exit 1 218248484Sneel fi 219284024Savg BOOTDISKS="-d ${isofile}" 220284024Savg installer_opt="-s 31:0,ahci-cd,${isofile}" 221248484Sneel else 222284024Savg BOOTDISKS="" 223284024Savg i=0 224284024Savg while [ $i -lt $disk_total ] ; do 225284024Savg eval "disk=\$disk_dev${i}" 226284024Savg if [ -r ${disk} ] ; then 227284024Savg BOOTDISKS="$BOOTDISKS -d ${disk} " 228284024Savg fi 229284024Savg i=$(($i + 1)) 230284024Savg done 231248484Sneel installer_opt="" 232248484Sneel fi 233248484Sneel 234284024Savg ${LOADER} -c ${console} -m ${memsize} ${BOOTDISKS} ${loader_opt} \ 235264837Sjhb ${vmname} 236271496Srodrigc bhyve_exit=$? 237271496Srodrigc if [ $bhyve_exit -ne 0 ]; then 238248484Sneel break 239248484Sneel fi 240248484Sneel 241267559Salfred # 242267559Salfred # Build up args for additional tap and disk devices now. 243267559Salfred # 244267559Salfred nextslot=2 # slot 0 is hostbridge, slot 1 is lpc 245267559Salfred devargs="" # accumulate disk/tap args here 246267559Salfred i=0 247267559Salfred while [ $i -lt $tap_total ] ; do 248267559Salfred eval "tapname=\$tap_dev${i}" 249267559Salfred devargs="$devargs -s $nextslot:0,virtio-net,${tapname} " 250267559Salfred nextslot=$(($nextslot + 1)) 251267559Salfred i=$(($i + 1)) 252267559Salfred done 253267559Salfred 254267559Salfred i=0 255267559Salfred while [ $i -lt $disk_total ] ; do 256267559Salfred eval "disk=\$disk_dev${i}" 257284023Savg eval "opts=\$disk_opts${i}" 258267559Salfred make_and_check_diskdev "${disk}" 259284023Savg devargs="$devargs -s $nextslot:0,virtio-blk,${disk}${opts} " 260267559Salfred nextslot=$(($nextslot + 1)) 261267559Salfred i=$(($i + 1)) 262267559Salfred done 263267559Salfred 264279925Sglebius i=0 265279925Sglebius while [ $i -lt $pass_total ] ; do 266279925Sglebius eval "pass=\$pass_dev${i}" 267279925Sglebius devargs="$devargs -s $nextslot:0,passthru,${pass} " 268279925Sglebius nextslot=$(($nextslot + 1)) 269279925Sglebius i=$(($i + 1)) 270279925Sglebius done 271279925Sglebius 272284539Sneel ${FBSDRUN} -c ${cpus} -m ${memsize} ${bhyverun_opt} \ 273248840Sneel -g ${gdbport} \ 274248484Sneel -s 0:0,hostbridge \ 275257293Sneel -s 1:0,lpc \ 276267559Salfred ${devargs} \ 277264837Sjhb -l com1,${console} \ 278248484Sneel ${installer_opt} \ 279248484Sneel ${vmname} 280270512Srodrigc 281271496Srodrigc bhyve_exit=$? 282270512Srodrigc # bhyve returns the following status codes: 283270512Srodrigc # 0 - VM has been reset 284270512Srodrigc # 1 - VM has been powered off 285270512Srodrigc # 2 - VM has been halted 286270512Srodrigc # 3 - VM generated a triple fault 287270512Srodrigc # all other non-zero status codes are errors 288270512Srodrigc # 289271496Srodrigc if [ $bhyve_exit -ne 0 ]; then 290248484Sneel break 291248484Sneel fi 292248484Sneeldone 293248484Sneel 294271496Srodrigc 295271496Srodrigccase $bhyve_exit in 296271496Srodrigc 0|1|2) 297271496Srodrigc # Cleanup /dev/vmm entry when bhyve did not exit 298271496Srodrigc # due to an error. 299271496Srodrigc ${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1 300271496Srodrigc ;; 301271496Srodrigcesac 302271496Srodrigc 303271496Srodrigcexit $bhyve_exit 304