2013-03-18 23:46:14 +00:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
|
|
|
# Copyright (c) 2013 NetApp, Inc.
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions
|
|
|
|
# are met:
|
|
|
|
# 1. Redistributions of source code must retain the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
# SUCH DAMAGE.
|
|
|
|
#
|
|
|
|
# $FreeBSD$
|
|
|
|
#
|
|
|
|
|
|
|
|
LOADER=/usr/sbin/bhyveload
|
|
|
|
BHYVECTL=/usr/sbin/bhyvectl
|
|
|
|
FBSDRUN=/usr/sbin/bhyve
|
|
|
|
|
2013-10-09 03:56:07 +00:00
|
|
|
DEFAULT_MEMSIZE=512M
|
2013-03-18 23:46:14 +00:00
|
|
|
DEFAULT_CPUS=2
|
|
|
|
DEFAULT_TAPDEV=tap0
|
2014-04-23 20:55:07 +00:00
|
|
|
DEFAULT_CONSOLE=stdio
|
2013-03-18 23:46:14 +00:00
|
|
|
|
|
|
|
DEFAULT_VIRTIO_DISK="./diskdev"
|
|
|
|
DEFAULT_ISOFILE="./release.iso"
|
|
|
|
|
2015-01-18 01:50:10 +00:00
|
|
|
errmsg() {
|
|
|
|
echo "*** $1"
|
|
|
|
}
|
|
|
|
|
2013-03-18 23:46:14 +00:00
|
|
|
usage() {
|
2015-01-18 01:50:10 +00:00
|
|
|
local msg=$1
|
|
|
|
|
2014-04-23 20:55:07 +00:00
|
|
|
echo "Usage: vmrun.sh [-ahi] [-c <CPUs>] [-C <console>] [-d <disk file>]"
|
|
|
|
echo " [-e <name=value>] [-g <gdbport> ] [-H <directory>]"
|
|
|
|
echo " [-I <location of installation iso>] [-m <memsize>]"
|
|
|
|
echo " [-t <tapdev>] <vmname>"
|
|
|
|
echo ""
|
2013-03-18 23:46:14 +00:00
|
|
|
echo " -h: display this help message"
|
2014-04-23 20:55:07 +00:00
|
|
|
echo " -a: force memory mapped local APIC access"
|
2013-03-18 23:46:14 +00:00
|
|
|
echo " -c: number of virtual cpus (default is ${DEFAULT_CPUS})"
|
2014-04-23 20:55:07 +00:00
|
|
|
echo " -C: console device (default is ${DEFAULT_CONSOLE})"
|
2013-03-18 23:46:14 +00:00
|
|
|
echo " -d: virtio diskdev file (default is ${DEFAULT_VIRTIO_DISK})"
|
2013-10-17 00:28:35 +00:00
|
|
|
echo " -e: set FreeBSD loader environment variable"
|
2013-03-28 18:03:39 +00:00
|
|
|
echo " -g: listen for connection from kgdb at <gdbport>"
|
2014-04-23 20:55:07 +00:00
|
|
|
echo " -H: host filesystem to export to the loader"
|
2013-03-18 23:46:14 +00:00
|
|
|
echo " -i: force boot of the Installation CDROM image"
|
|
|
|
echo " -I: Installation CDROM image location (default is ${DEFAULT_ISOFILE})"
|
2013-10-09 03:56:07 +00:00
|
|
|
echo " -m: memory size (default is ${DEFAULT_MEMSIZE})"
|
2015-03-12 15:58:07 +00:00
|
|
|
echo " -p: pass-through a host PCI device at bus/slot/func (e.g. 10/0/0)"
|
2013-03-18 23:46:14 +00:00
|
|
|
echo " -t: tap device for virtio-net (default is $DEFAULT_TAPDEV)"
|
|
|
|
echo ""
|
2015-01-18 01:50:10 +00:00
|
|
|
[ -n "$msg" ] && errmsg "$msg"
|
2013-03-18 23:46:14 +00:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if [ `id -u` -ne 0 ]; then
|
2015-01-18 01:50:10 +00:00
|
|
|
errmsg "This script must be executed with superuser privileges"
|
|
|
|
exit 1
|
2013-03-18 23:46:14 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
kldstat -n vmm > /dev/null 2>&1
|
|
|
|
if [ $? -ne 0 ]; then
|
2015-01-18 01:50:10 +00:00
|
|
|
errmsg "vmm.ko is not loaded"
|
2013-03-18 23:46:14 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
force_install=0
|
|
|
|
isofile=${DEFAULT_ISOFILE}
|
|
|
|
memsize=${DEFAULT_MEMSIZE}
|
2014-04-23 20:55:07 +00:00
|
|
|
console=${DEFAULT_CONSOLE}
|
2013-03-18 23:46:14 +00:00
|
|
|
cpus=${DEFAULT_CPUS}
|
2014-06-17 00:53:00 +00:00
|
|
|
tap_total=0
|
|
|
|
disk_total=0
|
2013-03-28 18:03:39 +00:00
|
|
|
gdbport=0
|
2014-04-23 20:55:07 +00:00
|
|
|
loader_opt=""
|
2015-06-18 06:00:17 +00:00
|
|
|
bhyverun_opt="-H -A -P"
|
2015-03-12 15:58:07 +00:00
|
|
|
pass_total=0
|
2013-03-18 23:46:14 +00:00
|
|
|
|
2015-03-12 15:58:07 +00:00
|
|
|
while getopts ac:C:d:e:g:hH:iI:m:p:t: c ; do
|
2013-03-18 23:46:14 +00:00
|
|
|
case $c in
|
|
|
|
a)
|
2015-06-18 06:00:17 +00:00
|
|
|
bhyverun_opt="${bhyverun_opt} -a"
|
2013-03-18 23:46:14 +00:00
|
|
|
;;
|
2014-04-23 20:55:07 +00:00
|
|
|
c)
|
|
|
|
cpus=${OPTARG}
|
|
|
|
;;
|
|
|
|
C)
|
|
|
|
console=${OPTARG}
|
|
|
|
;;
|
2013-03-18 23:46:14 +00:00
|
|
|
d)
|
2015-06-05 15:14:54 +00:00
|
|
|
disk_dev=${OPTARG%%,*}
|
|
|
|
disk_opts=${OPTARG#${disk_dev}}
|
|
|
|
eval "disk_dev${disk_total}=\"${disk_dev}\""
|
|
|
|
eval "disk_opts${disk_total}=\"${disk_opts}\""
|
2014-06-17 00:53:00 +00:00
|
|
|
disk_total=$(($disk_total + 1))
|
2013-03-18 23:46:14 +00:00
|
|
|
;;
|
2013-10-17 00:28:35 +00:00
|
|
|
e)
|
2014-04-23 20:55:07 +00:00
|
|
|
loader_opt="${loader_opt} -e ${OPTARG}"
|
2013-10-17 00:28:35 +00:00
|
|
|
;;
|
2014-04-23 20:55:07 +00:00
|
|
|
g)
|
|
|
|
gdbport=${OPTARG}
|
|
|
|
;;
|
|
|
|
H)
|
|
|
|
host_base=`realpath ${OPTARG}`
|
2013-03-28 18:03:39 +00:00
|
|
|
;;
|
2013-03-18 23:46:14 +00:00
|
|
|
i)
|
|
|
|
force_install=1
|
|
|
|
;;
|
|
|
|
I)
|
|
|
|
isofile=${OPTARG}
|
|
|
|
;;
|
|
|
|
m)
|
|
|
|
memsize=${OPTARG}
|
|
|
|
;;
|
2015-03-12 15:58:07 +00:00
|
|
|
p)
|
|
|
|
eval "pass_dev${pass_total}=\"${OPTARG}\""
|
|
|
|
pass_total=$(($pass_total + 1))
|
|
|
|
;;
|
2013-03-18 23:46:14 +00:00
|
|
|
t)
|
2014-06-17 00:53:00 +00:00
|
|
|
eval "tap_dev${tap_total}=\"${OPTARG}\""
|
|
|
|
tap_total=$(($tap_total + 1))
|
2013-03-18 23:46:14 +00:00
|
|
|
;;
|
2014-04-23 20:55:07 +00:00
|
|
|
*)
|
2013-03-18 23:46:14 +00:00
|
|
|
usage
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2014-06-17 00:53:00 +00:00
|
|
|
if [ $tap_total -eq 0 ] ; then
|
|
|
|
tap_total=1
|
|
|
|
tap_dev0="${DEFAULT_TAPDEV}"
|
|
|
|
fi
|
|
|
|
if [ $disk_total -eq 0 ] ; then
|
|
|
|
disk_total=1
|
|
|
|
disk_dev0="${DEFAULT_VIRTIO_DISK}"
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
2013-03-18 23:46:14 +00:00
|
|
|
shift $((${OPTIND} - 1))
|
|
|
|
|
|
|
|
if [ $# -ne 1 ]; then
|
2015-01-18 01:50:10 +00:00
|
|
|
usage "virtual machine name not specified"
|
2013-03-18 23:46:14 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
vmname="$1"
|
2014-04-23 20:55:07 +00:00
|
|
|
if [ -n "${host_base}" ]; then
|
|
|
|
loader_opt="${loader_opt} -h ${host_base}"
|
|
|
|
fi
|
2013-03-18 23:46:14 +00:00
|
|
|
|
2015-06-18 06:00:17 +00:00
|
|
|
# If PCI passthru devices are configured then guest memory must be wired
|
|
|
|
if [ ${pass_total} -gt 0 ]; then
|
|
|
|
loader_opt="${loader_opt} -S"
|
|
|
|
bhyverun_opt="${bhyverun_opt} -S"
|
|
|
|
fi
|
|
|
|
|
2014-06-17 00:53:00 +00:00
|
|
|
make_and_check_diskdev()
|
|
|
|
{
|
|
|
|
local virtio_diskdev="$1"
|
|
|
|
# Create the virtio diskdev file if needed
|
2014-10-14 18:34:15 +00:00
|
|
|
if [ ! -e ${virtio_diskdev} ]; then
|
2014-06-17 00:53:00 +00:00
|
|
|
echo "virtio disk device file \"${virtio_diskdev}\" does not exist."
|
|
|
|
echo "Creating it ..."
|
|
|
|
truncate -s 8G ${virtio_diskdev} > /dev/null
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -r ${virtio_diskdev} ]; then
|
|
|
|
echo "virtio disk device file \"${virtio_diskdev}\" is not readable"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -w ${virtio_diskdev} ]; then
|
|
|
|
echo "virtio disk device file \"${virtio_diskdev}\" is not writable"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
2013-03-18 23:46:14 +00:00
|
|
|
|
|
|
|
echo "Launching virtual machine \"$vmname\" ..."
|
|
|
|
|
2015-06-05 15:16:26 +00:00
|
|
|
first_diskdev="$disk_dev0"
|
2014-06-17 00:53:00 +00:00
|
|
|
|
2014-08-25 01:36:56 +00:00
|
|
|
${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
|
|
|
|
|
2013-03-18 23:46:14 +00:00
|
|
|
while [ 1 ]; do
|
|
|
|
|
2015-06-05 15:16:26 +00:00
|
|
|
file -s ${first_diskdev} | grep "boot sector" > /dev/null
|
2013-03-18 23:46:14 +00:00
|
|
|
rc=$?
|
|
|
|
if [ $rc -ne 0 ]; then
|
2015-06-05 15:16:26 +00:00
|
|
|
file -s ${first_diskdev} | grep ": Unix Fast File sys" > /dev/null
|
2013-03-18 23:46:14 +00:00
|
|
|
rc=$?
|
|
|
|
fi
|
|
|
|
if [ $rc -ne 0 ]; then
|
|
|
|
need_install=1
|
|
|
|
else
|
|
|
|
need_install=0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ $force_install -eq 1 -o $need_install -eq 1 ]; then
|
|
|
|
if [ ! -r ${isofile} ]; then
|
|
|
|
echo -n "Installation CDROM image \"${isofile}\" "
|
|
|
|
echo "is not readable"
|
|
|
|
exit 1
|
|
|
|
fi
|
2015-06-05 15:16:26 +00:00
|
|
|
BOOTDISKS="-d ${isofile}"
|
|
|
|
installer_opt="-s 31:0,ahci-cd,${isofile}"
|
2013-03-18 23:46:14 +00:00
|
|
|
else
|
2015-06-05 15:16:26 +00:00
|
|
|
BOOTDISKS=""
|
|
|
|
i=0
|
|
|
|
while [ $i -lt $disk_total ] ; do
|
|
|
|
eval "disk=\$disk_dev${i}"
|
|
|
|
if [ -r ${disk} ] ; then
|
|
|
|
BOOTDISKS="$BOOTDISKS -d ${disk} "
|
|
|
|
fi
|
|
|
|
i=$(($i + 1))
|
|
|
|
done
|
2013-03-18 23:46:14 +00:00
|
|
|
installer_opt=""
|
|
|
|
fi
|
|
|
|
|
2015-06-05 15:16:26 +00:00
|
|
|
${LOADER} -c ${console} -m ${memsize} ${BOOTDISKS} ${loader_opt} \
|
2014-04-23 20:55:07 +00:00
|
|
|
${vmname}
|
2014-09-13 05:08:35 +00:00
|
|
|
bhyve_exit=$?
|
|
|
|
if [ $bhyve_exit -ne 0 ]; then
|
2013-03-18 23:46:14 +00:00
|
|
|
break
|
|
|
|
fi
|
|
|
|
|
2014-06-17 00:53:00 +00:00
|
|
|
#
|
|
|
|
# Build up args for additional tap and disk devices now.
|
|
|
|
#
|
|
|
|
nextslot=2 # slot 0 is hostbridge, slot 1 is lpc
|
|
|
|
devargs="" # accumulate disk/tap args here
|
|
|
|
i=0
|
|
|
|
while [ $i -lt $tap_total ] ; do
|
|
|
|
eval "tapname=\$tap_dev${i}"
|
|
|
|
devargs="$devargs -s $nextslot:0,virtio-net,${tapname} "
|
|
|
|
nextslot=$(($nextslot + 1))
|
|
|
|
i=$(($i + 1))
|
|
|
|
done
|
|
|
|
|
|
|
|
i=0
|
|
|
|
while [ $i -lt $disk_total ] ; do
|
|
|
|
eval "disk=\$disk_dev${i}"
|
2015-06-05 15:14:54 +00:00
|
|
|
eval "opts=\$disk_opts${i}"
|
2014-06-17 00:53:00 +00:00
|
|
|
make_and_check_diskdev "${disk}"
|
2015-06-05 15:14:54 +00:00
|
|
|
devargs="$devargs -s $nextslot:0,virtio-blk,${disk}${opts} "
|
2014-06-17 00:53:00 +00:00
|
|
|
nextslot=$(($nextslot + 1))
|
|
|
|
i=$(($i + 1))
|
|
|
|
done
|
|
|
|
|
2015-03-12 15:58:07 +00:00
|
|
|
i=0
|
|
|
|
while [ $i -lt $pass_total ] ; do
|
|
|
|
eval "pass=\$pass_dev${i}"
|
|
|
|
devargs="$devargs -s $nextslot:0,passthru,${pass} "
|
|
|
|
nextslot=$(($nextslot + 1))
|
|
|
|
i=$(($i + 1))
|
|
|
|
done
|
|
|
|
|
2015-06-18 06:00:17 +00:00
|
|
|
${FBSDRUN} -c ${cpus} -m ${memsize} ${bhyverun_opt} \
|
2013-03-28 18:03:39 +00:00
|
|
|
-g ${gdbport} \
|
2013-03-18 23:46:14 +00:00
|
|
|
-s 0:0,hostbridge \
|
2013-10-29 00:18:11 +00:00
|
|
|
-s 1:0,lpc \
|
2014-06-17 00:53:00 +00:00
|
|
|
${devargs} \
|
2014-04-23 20:55:07 +00:00
|
|
|
-l com1,${console} \
|
2013-03-18 23:46:14 +00:00
|
|
|
${installer_opt} \
|
|
|
|
${vmname}
|
2014-08-25 01:04:07 +00:00
|
|
|
|
2014-09-13 05:08:35 +00:00
|
|
|
bhyve_exit=$?
|
2014-08-25 01:04:07 +00:00
|
|
|
# bhyve returns the following status codes:
|
|
|
|
# 0 - VM has been reset
|
|
|
|
# 1 - VM has been powered off
|
|
|
|
# 2 - VM has been halted
|
|
|
|
# 3 - VM generated a triple fault
|
|
|
|
# all other non-zero status codes are errors
|
|
|
|
#
|
2014-09-13 05:08:35 +00:00
|
|
|
if [ $bhyve_exit -ne 0 ]; then
|
2013-03-18 23:46:14 +00:00
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2014-09-13 05:08:35 +00:00
|
|
|
|
|
|
|
case $bhyve_exit in
|
|
|
|
0|1|2)
|
|
|
|
# Cleanup /dev/vmm entry when bhyve did not exit
|
|
|
|
# due to an error.
|
|
|
|
${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
exit $bhyve_exit
|