vmrun.sh revision 279925
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 279925 2015-03-12 15:58:07Z glebius $
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
90248484Sneelapic_opt=""
91248840Sneelgdbport=0
92264837Sjhbloader_opt=""
93279925Sglebiuspass_total=0
94248484Sneel
95279925Sglebiuswhile getopts ac:C:d:e:g:hH:iI:m:p:t: c ; do
96248484Sneel	case $c in
97248484Sneel	a)
98248484Sneel		apic_opt="-a"
99248484Sneel		;;
100264837Sjhb	c)
101264837Sjhb		cpus=${OPTARG}
102264837Sjhb		;;
103264837Sjhb	C)
104264837Sjhb		console=${OPTARG}
105264837Sjhb		;;
106248484Sneel	d)
107267559Salfred		eval "disk_dev${disk_total}=\"${OPTARG}\""
108267559Salfred		disk_total=$(($disk_total + 1))
109248484Sneel		;;
110256657Sneel	e)
111264837Sjhb		loader_opt="${loader_opt} -e ${OPTARG}"
112256657Sneel		;;
113264837Sjhb	g)	
114264837Sjhb		gdbport=${OPTARG}
115248840Sneel		;;
116264837Sjhb	H)
117264837Sjhb		host_base=`realpath ${OPTARG}`
118264837Sjhb		;;
119248484Sneel	i)
120248484Sneel		force_install=1
121248484Sneel		;;
122248484Sneel	I)
123248484Sneel		isofile=${OPTARG}
124248484Sneel		;;
125248484Sneel	m)
126248484Sneel		memsize=${OPTARG}
127248484Sneel		;;
128279925Sglebius	p)
129279925Sglebius		eval "pass_dev${pass_total}=\"${OPTARG}\""
130279925Sglebius		pass_total=$(($pass_total + 1))
131279925Sglebius		;;
132248484Sneel	t)
133267559Salfred		eval "tap_dev${tap_total}=\"${OPTARG}\""
134267559Salfred		tap_total=$(($tap_total + 1))
135248484Sneel		;;
136264837Sjhb	*)
137248484Sneel		usage
138248484Sneel		;;
139248484Sneel	esac
140248484Sneeldone
141248484Sneel
142267559Salfredif [ $tap_total -eq 0 ] ; then
143267559Salfred    tap_total=1
144267559Salfred    tap_dev0="${DEFAULT_TAPDEV}"
145267559Salfredfi
146267559Salfredif [ $disk_total -eq 0 ] ; then
147267559Salfred    disk_total=1
148267559Salfred    disk_dev0="${DEFAULT_VIRTIO_DISK}"
149267559Salfred
150267559Salfredfi
151267559Salfred
152248484Sneelshift $((${OPTIND} - 1))
153248484Sneel
154248484Sneelif [ $# -ne 1 ]; then
155277309Sneel	usage "virtual machine name not specified"
156248484Sneelfi
157248484Sneel
158248484Sneelvmname="$1"
159264837Sjhbif [ -n "${host_base}" ]; then
160264837Sjhb	loader_opt="${loader_opt} -h ${host_base}"
161264837Sjhbfi
162248484Sneel
163267559Salfredmake_and_check_diskdev()
164267559Salfred{
165267559Salfred    local virtio_diskdev="$1"
166267559Salfred    # Create the virtio diskdev file if needed
167273102Sneel    if [ ! -e ${virtio_diskdev} ]; then
168267559Salfred	    echo "virtio disk device file \"${virtio_diskdev}\" does not exist."
169267559Salfred	    echo "Creating it ..."
170267559Salfred	    truncate -s 8G ${virtio_diskdev} > /dev/null
171267559Salfred    fi
172248484Sneel
173267559Salfred    if [ ! -r ${virtio_diskdev} ]; then
174267559Salfred	    echo "virtio disk device file \"${virtio_diskdev}\" is not readable"
175267559Salfred	    exit 1
176267559Salfred    fi
177248484Sneel
178267559Salfred    if [ ! -w ${virtio_diskdev} ]; then
179267559Salfred	    echo "virtio disk device file \"${virtio_diskdev}\" is not writable"
180267559Salfred	    exit 1
181267559Salfred    fi
182267559Salfred}
183248484Sneel
184248484Sneelecho "Launching virtual machine \"$vmname\" ..."
185248484Sneel
186267559Salfredvirtio_diskdev="$disk_dev0"
187267559Salfred
188270513Srodrigc${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
189270513Srodrigc
190248484Sneelwhile [ 1 ]; do
191248484Sneel
192270754Srodrigc	file -s ${virtio_diskdev} | grep "boot sector" > /dev/null
193248484Sneel	rc=$?
194248484Sneel	if [ $rc -ne 0 ]; then
195270754Srodrigc		file -s ${virtio_diskdev} | grep ": Unix Fast File sys" > /dev/null
196248484Sneel		rc=$?
197248484Sneel	fi
198248484Sneel	if [ $rc -ne 0 ]; then
199248484Sneel		need_install=1
200248484Sneel	else
201248484Sneel		need_install=0
202248484Sneel	fi
203248484Sneel
204248484Sneel	if [ $force_install -eq 1 -o $need_install -eq 1 ]; then
205248484Sneel		if [ ! -r ${isofile} ]; then
206248484Sneel			echo -n "Installation CDROM image \"${isofile}\" "
207248484Sneel			echo    "is not readable"
208248484Sneel			exit 1
209248484Sneel		fi
210248484Sneel		BOOTDISK=${isofile}
211271447Srodrigc		installer_opt="-s 31:0,ahci-cd,${BOOTDISK}"
212248484Sneel	else
213248484Sneel		BOOTDISK=${virtio_diskdev}
214248484Sneel		installer_opt=""
215248484Sneel	fi
216248484Sneel
217264837Sjhb	${LOADER} -c ${console} -m ${memsize} -d ${BOOTDISK} ${loader_opt} \
218264837Sjhb		${vmname}
219271496Srodrigc	bhyve_exit=$?
220271496Srodrigc	if [ $bhyve_exit -ne 0 ]; then
221248484Sneel		break
222248484Sneel	fi
223248484Sneel
224267559Salfred	#
225267559Salfred	# Build up args for additional tap and disk devices now.
226267559Salfred	#
227267559Salfred	nextslot=2  # slot 0 is hostbridge, slot 1 is lpc
228267559Salfred	devargs=""  # accumulate disk/tap args here
229267559Salfred	i=0
230267559Salfred	while [ $i -lt $tap_total ] ; do
231267559Salfred	    eval "tapname=\$tap_dev${i}"
232267559Salfred	    devargs="$devargs -s $nextslot:0,virtio-net,${tapname} "
233267559Salfred	    nextslot=$(($nextslot + 1))
234267559Salfred	    i=$(($i + 1))
235267559Salfred	done
236267559Salfred
237267559Salfred	i=0
238267559Salfred	while [ $i -lt $disk_total ] ; do
239267559Salfred	    eval "disk=\$disk_dev${i}"
240267559Salfred	    make_and_check_diskdev "${disk}"
241267559Salfred	    devargs="$devargs -s $nextslot:0,virtio-blk,${disk} "
242267559Salfred	    nextslot=$(($nextslot + 1))
243267559Salfred	    i=$(($i + 1))
244267559Salfred	done
245267559Salfred
246279925Sglebius	i=0
247279925Sglebius	while [ $i -lt $pass_total ] ; do
248279925Sglebius	    eval "pass=\$pass_dev${i}"
249279925Sglebius	    devargs="$devargs -s $nextslot:0,passthru,${pass} "
250279925Sglebius	    nextslot=$(($nextslot + 1))
251279925Sglebius	    i=$(($i + 1))
252279925Sglebius        done
253279925Sglebius
254257423Sneel	${FBSDRUN} -c ${cpus} -m ${memsize} ${apic_opt} -A -H -P	\
255248840Sneel		-g ${gdbport}						\
256248484Sneel		-s 0:0,hostbridge					\
257257293Sneel		-s 1:0,lpc						\
258267559Salfred		${devargs}						\
259264837Sjhb		-l com1,${console}					\
260248484Sneel		${installer_opt}					\
261248484Sneel		${vmname}
262270512Srodrigc
263271496Srodrigc	bhyve_exit=$?
264270512Srodrigc	# bhyve returns the following status codes:
265270512Srodrigc	#  0 - VM has been reset
266270512Srodrigc	#  1 - VM has been powered off
267270512Srodrigc	#  2 - VM has been halted
268270512Srodrigc	#  3 - VM generated a triple fault
269270512Srodrigc	#  all other non-zero status codes are errors
270270512Srodrigc	#
271271496Srodrigc	if [ $bhyve_exit -ne 0 ]; then
272248484Sneel		break
273248484Sneel	fi
274248484Sneeldone
275248484Sneel
276271496Srodrigc
277271496Srodrigccase $bhyve_exit in
278271496Srodrigc	0|1|2)
279271496Srodrigc		# Cleanup /dev/vmm entry when bhyve did not exit
280271496Srodrigc		# due to an error.
281271496Srodrigc		${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
282271496Srodrigc		;;
283271496Srodrigcesac
284271496Srodrigc
285271496Srodrigcexit $bhyve_exit
286