1#!/bin/sh 2# 3# Copyright (c) 2013 NetApp, Inc. 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions 8# are met: 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. 14# 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 25# SUCH DAMAGE. 26# 27# $FreeBSD$ 28# 29 30LOADER=/usr/sbin/bhyveload 31BHYVECTL=/usr/sbin/bhyvectl 32FBSDRUN=/usr/sbin/bhyve 33 34DEFAULT_MEMSIZE=512M 35DEFAULT_CPUS=2 36DEFAULT_TAPDEV=tap0 37DEFAULT_CONSOLE=stdio 38 39DEFAULT_VIRTIO_DISK="./diskdev" 40DEFAULT_ISOFILE="./release.iso" 41 42usage() { 43 echo "Usage: vmrun.sh [-ahi] [-c <CPUs>] [-C <console>] [-d <disk file>]" 44 echo " [-e <name=value>] [-g <gdbport> ] [-H <directory>]" 45 echo " [-I <location of installation iso>] [-m <memsize>]" 46 echo " [-t <tapdev>] <vmname>" 47 echo "" 48 echo " -h: display this help message" 49 echo " -a: force memory mapped local APIC access" 50 echo " -c: number of virtual cpus (default is ${DEFAULT_CPUS})" 51 echo " -C: console device (default is ${DEFAULT_CONSOLE})" 52 echo " -d: virtio diskdev file (default is ${DEFAULT_VIRTIO_DISK})" 53 echo " -e: set FreeBSD loader environment variable" 54 echo " -g: listen for connection from kgdb at <gdbport>" 55 echo " -H: host filesystem to export to the loader" 56 echo " -i: force boot of the Installation CDROM image" 57 echo " -I: Installation CDROM image location (default is ${DEFAULT_ISOFILE})" 58 echo " -m: memory size (default is ${DEFAULT_MEMSIZE})" 59 echo " -t: tap device for virtio-net (default is $DEFAULT_TAPDEV)" 60 echo "" 61 echo " This script needs to be executed with superuser privileges" 62 echo "" 63 exit 1 64} 65 66if [ `id -u` -ne 0 ]; then 67 usage 68fi 69 70kldstat -n vmm > /dev/null 2>&1 71if [ $? -ne 0 ]; then 72 echo "vmm.ko is not loaded!" 73 exit 1 74fi 75 76force_install=0 77isofile=${DEFAULT_ISOFILE} 78memsize=${DEFAULT_MEMSIZE} 79console=${DEFAULT_CONSOLE} 80cpus=${DEFAULT_CPUS} 81virtio_diskdev=${DEFAULT_VIRTIO_DISK} 82tapdev=${DEFAULT_TAPDEV} 83apic_opt="" 84gdbport=0 85loader_opt="" 86 87while getopts ac:C:d:e:g:hH:iI:m:t: c ; do 88 case $c in 89 a) 90 apic_opt="-a" 91 ;; 92 c) 93 cpus=${OPTARG} 94 ;; 95 C) 96 console=${OPTARG} 97 ;; 98 d) 99 virtio_diskdev=${OPTARG} 100 ;; 101 e) 102 loader_opt="${loader_opt} -e ${OPTARG}" 103 ;; 104 g) 105 gdbport=${OPTARG} 106 ;; 107 H) 108 host_base=`realpath ${OPTARG}` 109 ;; 110 i) 111 force_install=1 112 ;; 113 I) 114 isofile=${OPTARG} 115 ;; 116 m) 117 memsize=${OPTARG} 118 ;; 119 t) 120 tapdev=${OPTARG} 121 ;; 122 *) 123 usage 124 ;; 125 esac 126done 127 128shift $((${OPTIND} - 1)) 129 130if [ $# -ne 1 ]; then 131 usage 132fi 133 134vmname="$1" 135if [ -n "${host_base}" ]; then 136 loader_opt="${loader_opt} -h ${host_base}" 137fi 138 139# Create the virtio diskdev file if needed 140if [ ! -f ${virtio_diskdev} ]; then 141 echo "virtio disk device file \"${virtio_diskdev}\" does not exist." 142 echo "Creating it ..." 143 truncate -s 8G ${virtio_diskdev} > /dev/null 144fi 145 146if [ ! -r ${virtio_diskdev} ]; then 147 echo "virtio disk device file \"${virtio_diskdev}\" is not readable" 148 exit 1 149fi 150 151if [ ! -w ${virtio_diskdev} ]; then 152 echo "virtio disk device file \"${virtio_diskdev}\" is not writable" 153 exit 1 154fi 155 156echo "Launching virtual machine \"$vmname\" ..." 157 158while [ 1 ]; do 159 ${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1 160 161 file ${virtio_diskdev} | grep ": x86 boot sector" > /dev/null 162 rc=$? 163 if [ $rc -ne 0 ]; then 164 file ${virtio_diskdev} | grep ": Unix Fast File sys" > /dev/null 165 rc=$? 166 fi 167 if [ $rc -ne 0 ]; then 168 need_install=1 169 else 170 need_install=0 171 fi 172 173 if [ $force_install -eq 1 -o $need_install -eq 1 ]; then 174 if [ ! -r ${isofile} ]; then 175 echo -n "Installation CDROM image \"${isofile}\" " 176 echo "is not readable" 177 exit 1 178 fi 179 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