vmrun.sh revision 264837
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 264837 2014-04-23 20:55:07Z jhb $ 28248484Sneel# 29248484Sneel 30248484SneelLOADER=/usr/sbin/bhyveload 31248484SneelBHYVECTL=/usr/sbin/bhyvectl 32248484SneelFBSDRUN=/usr/sbin/bhyve 33248484Sneel 34248484SneelDEFAULT_MEMSIZE=512M 35248484SneelDEFAULT_CPUS=2 36248484SneelDEFAULT_TAPDEV=tap0 37248484SneelDEFAULT_CONSOLE=stdio 38248484Sneel 39248484SneelDEFAULT_VIRTIO_DISK="./diskdev" 40248484SneelDEFAULT_ISOFILE="./release.iso" 41248484Sneel 42248484Sneelusage() { 43248484Sneel echo "Usage: vmrun.sh [-ahi] [-c <CPUs>] [-C <console>] [-d <disk file>]" 44248484Sneel echo " [-e <name=value>] [-g <gdbport> ] [-H <directory>]" 45248484Sneel echo " [-I <location of installation iso>] [-m <memsize>]" 46248484Sneel echo " [-t <tapdev>] <vmname>" 47248484Sneel echo "" 48248484Sneel echo " -h: display this help message" 49248484Sneel echo " -a: force memory mapped local APIC access" 50248484Sneel echo " -c: number of virtual cpus (default is ${DEFAULT_CPUS})" 51248484Sneel echo " -C: console device (default is ${DEFAULT_CONSOLE})" 52248484Sneel echo " -d: virtio diskdev file (default is ${DEFAULT_VIRTIO_DISK})" 53248484Sneel echo " -e: set FreeBSD loader environment variable" 54248484Sneel echo " -g: listen for connection from kgdb at <gdbport>" 55248484Sneel echo " -H: host filesystem to export to the loader" 56248484Sneel echo " -i: force boot of the Installation CDROM image" 57248484Sneel echo " -I: Installation CDROM image location (default is ${DEFAULT_ISOFILE})" 58248484Sneel echo " -m: memory size (default is ${DEFAULT_MEMSIZE})" 59248484Sneel echo " -t: tap device for virtio-net (default is $DEFAULT_TAPDEV)" 60248484Sneel echo "" 61248484Sneel echo " This script needs to be executed with superuser privileges" 62248484Sneel echo "" 63248484Sneel exit 1 64248484Sneel} 65248484Sneel 66248484Sneelif [ `id -u` -ne 0 ]; then 67248484Sneel usage 68248484Sneelfi 69248484Sneel 70248484Sneelkldstat -n vmm > /dev/null 2>&1 71248484Sneelif [ $? -ne 0 ]; then 72248484Sneel echo "vmm.ko is not loaded!" 73248484Sneel exit 1 74248484Sneelfi 75248484Sneel 76248484Sneelforce_install=0 77248484Sneelisofile=${DEFAULT_ISOFILE} 78248484Sneelmemsize=${DEFAULT_MEMSIZE} 79248484Sneelconsole=${DEFAULT_CONSOLE} 80248484Sneelcpus=${DEFAULT_CPUS} 81248484Sneelvirtio_diskdev=${DEFAULT_VIRTIO_DISK} 82248484Sneeltapdev=${DEFAULT_TAPDEV} 83248484Sneelapic_opt="" 84248484Sneelgdbport=0 85248484Sneelloader_opt="" 86248484Sneel 87248484Sneelwhile getopts ac:C:d:e:g:hH:iI:m:t: c ; do 88248484Sneel case $c in 89248484Sneel a) 90248484Sneel apic_opt="-a" 91248484Sneel ;; 92248484Sneel c) 93248484Sneel cpus=${OPTARG} 94248484Sneel ;; 95248484Sneel C) 96248484Sneel console=${OPTARG} 97248484Sneel ;; 98248484Sneel d) 99248484Sneel virtio_diskdev=${OPTARG} 100248484Sneel ;; 101248484Sneel e) 102248484Sneel loader_opt="${loader_opt} -e ${OPTARG}" 103248484Sneel ;; 104248484Sneel g) 105248484Sneel gdbport=${OPTARG} 106248484Sneel ;; 107248484Sneel H) 108248484Sneel host_base=`realpath ${OPTARG}` 109248484Sneel ;; 110248484Sneel i) 111248484Sneel force_install=1 112248484Sneel ;; 113248484Sneel I) 114248484Sneel isofile=${OPTARG} 115248484Sneel ;; 116248484Sneel m) 117248484Sneel memsize=${OPTARG} 118248484Sneel ;; 119248484Sneel t) 120248484Sneel tapdev=${OPTARG} 121248484Sneel ;; 122248484Sneel *) 123248484Sneel usage 124248484Sneel ;; 125248484Sneel esac 126248484Sneeldone 127248484Sneel 128248484Sneelshift $((${OPTIND} - 1)) 129248484Sneel 130248484Sneelif [ $# -ne 1 ]; then 131248484Sneel usage 132248484Sneelfi 133248484Sneel 134248484Sneelvmname="$1" 135248484Sneelif [ -n "${host_base}" ]; then 136248484Sneel loader_opt="${loader_opt} -h ${host_base}" 137248484Sneelfi 138248484Sneel 139248484Sneel# Create the virtio diskdev file if needed 140248484Sneelif [ ! -f ${virtio_diskdev} ]; then 141248484Sneel echo "virtio disk device file \"${virtio_diskdev}\" does not exist." 142248484Sneel echo "Creating it ..." 143248484Sneel truncate -s 8G ${virtio_diskdev} > /dev/null 144248484Sneelfi 145248484Sneel 146248484Sneelif [ ! -r ${virtio_diskdev} ]; then 147248484Sneel echo "virtio disk device file \"${virtio_diskdev}\" is not readable" 148248484Sneel exit 1 149248484Sneelfi 150248484Sneel 151248484Sneelif [ ! -w ${virtio_diskdev} ]; then 152248484Sneel echo "virtio disk device file \"${virtio_diskdev}\" is not writable" 153248484Sneel exit 1 154248484Sneelfi 155248484Sneel 156248484Sneelecho "Launching virtual machine \"$vmname\" ..." 157248484Sneel 158248484Sneelwhile [ 1 ]; do 159248484Sneel ${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1 160248484Sneel 161248484Sneel file ${virtio_diskdev} | grep ": x86 boot sector" > /dev/null 162248484Sneel rc=$? 163248484Sneel if [ $rc -ne 0 ]; then 164248484Sneel file ${virtio_diskdev} | grep ": Unix Fast File sys" > /dev/null 165248484Sneel rc=$? 166248484Sneel fi 167248484Sneel if [ $rc -ne 0 ]; then 168248484Sneel need_install=1 169248484Sneel else 170248484Sneel need_install=0 171248484Sneel fi 172248484Sneel 173248484Sneel if [ $force_install -eq 1 -o $need_install -eq 1 ]; then 174248484Sneel if [ ! -r ${isofile} ]; then 175248484Sneel echo -n "Installation CDROM image \"${isofile}\" " 176248484Sneel echo "is not readable" 177248484Sneel exit 1 178248484Sneel fi 179248484Sneel BOOTDISK=${isofile} 180 installer_opt="-s 31:0,virtio-blk,${BOOTDISK}" 181 else 182 BOOTDISK=${virtio_diskdev} 183 installer_opt="" 184 fi 185 186 ${LOADER} -c ${console} -m ${memsize} -d ${BOOTDISK} ${loader_opt} \ 187 ${vmname} 188 if [ $? -ne 0 ]; then 189 break 190 fi 191 192 ${FBSDRUN} -c ${cpus} -m ${memsize} ${apic_opt} -A -H -P \ 193 -g ${gdbport} \ 194 -s 0:0,hostbridge \ 195 -s 1:0,lpc \ 196 -s 2:0,virtio-net,${tapdev} \ 197 -s 3:0,virtio-blk,${virtio_diskdev} \ 198 -l com1,${console} \ 199 ${installer_opt} \ 200 ${vmname} 201 if [ $? -ne 0 ]; then 202 break 203 fi 204done 205 206exit 99 207