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