vmrun.sh revision 271496
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 271496 2014-09-13 05:08:35Z rodrigc $
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
42248484Sneelusage() {
43264837Sjhb	echo "Usage: vmrun.sh [-ahi] [-c <CPUs>] [-C <console>] [-d <disk file>]"
44264837Sjhb	echo "                [-e <name=value>] [-g <gdbport> ] [-H <directory>]"
45264837Sjhb	echo "                [-I <location of installation iso>] [-m <memsize>]"
46264837Sjhb	echo "                [-t <tapdev>] <vmname>"
47264837Sjhb	echo ""
48248484Sneel	echo "       -h: display this help message"
49264837Sjhb	echo "       -a: force memory mapped local APIC access"
50248484Sneel	echo "       -c: number of virtual cpus (default is ${DEFAULT_CPUS})"
51264837Sjhb	echo "       -C: console device (default is ${DEFAULT_CONSOLE})"
52248484Sneel	echo "       -d: virtio diskdev file (default is ${DEFAULT_VIRTIO_DISK})"
53256657Sneel	echo "       -e: set FreeBSD loader environment variable"
54248840Sneel	echo "       -g: listen for connection from kgdb at <gdbport>"
55264837Sjhb	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})"
58256176Sneel	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}
79264837Sjhbconsole=${DEFAULT_CONSOLE}
80248484Sneelcpus=${DEFAULT_CPUS}
81267559Salfredtap_total=0
82267559Salfreddisk_total=0
83248484Sneelapic_opt=""
84248840Sneelgdbport=0
85264837Sjhbloader_opt=""
86248484Sneel
87264837Sjhbwhile getopts ac:C:d:e:g:hH:iI:m:t: c ; do
88248484Sneel	case $c in
89248484Sneel	a)
90248484Sneel		apic_opt="-a"
91248484Sneel		;;
92264837Sjhb	c)
93264837Sjhb		cpus=${OPTARG}
94264837Sjhb		;;
95264837Sjhb	C)
96264837Sjhb		console=${OPTARG}
97264837Sjhb		;;
98248484Sneel	d)
99267559Salfred		eval "disk_dev${disk_total}=\"${OPTARG}\""
100267559Salfred		disk_total=$(($disk_total + 1))
101248484Sneel		;;
102256657Sneel	e)
103264837Sjhb		loader_opt="${loader_opt} -e ${OPTARG}"
104256657Sneel		;;
105264837Sjhb	g)	
106264837Sjhb		gdbport=${OPTARG}
107248840Sneel		;;
108264837Sjhb	H)
109264837Sjhb		host_base=`realpath ${OPTARG}`
110264837Sjhb		;;
111248484Sneel	i)
112248484Sneel		force_install=1
113248484Sneel		;;
114248484Sneel	I)
115248484Sneel		isofile=${OPTARG}
116248484Sneel		;;
117248484Sneel	m)
118248484Sneel		memsize=${OPTARG}
119248484Sneel		;;
120248484Sneel	t)
121267559Salfred		eval "tap_dev${tap_total}=\"${OPTARG}\""
122267559Salfred		tap_total=$(($tap_total + 1))
123248484Sneel		;;
124264837Sjhb	*)
125248484Sneel		usage
126248484Sneel		;;
127248484Sneel	esac
128248484Sneeldone
129248484Sneel
130267559Salfredif [ $tap_total -eq 0 ] ; then
131267559Salfred    tap_total=1
132267559Salfred    tap_dev0="${DEFAULT_TAPDEV}"
133267559Salfredfi
134267559Salfredif [ $disk_total -eq 0 ] ; then
135267559Salfred    disk_total=1
136267559Salfred    disk_dev0="${DEFAULT_VIRTIO_DISK}"
137267559Salfred
138267559Salfredfi
139267559Salfred
140248484Sneelshift $((${OPTIND} - 1))
141248484Sneel
142248484Sneelif [ $# -ne 1 ]; then
143248484Sneel	usage
144248484Sneelfi
145248484Sneel
146248484Sneelvmname="$1"
147264837Sjhbif [ -n "${host_base}" ]; then
148264837Sjhb	loader_opt="${loader_opt} -h ${host_base}"
149264837Sjhbfi
150248484Sneel
151267559Salfredmake_and_check_diskdev()
152267559Salfred{
153267559Salfred    local virtio_diskdev="$1"
154267559Salfred    # Create the virtio diskdev file if needed
155267559Salfred    if [ ! -f ${virtio_diskdev} ]; then
156267559Salfred	    echo "virtio disk device file \"${virtio_diskdev}\" does not exist."
157267559Salfred	    echo "Creating it ..."
158267559Salfred	    truncate -s 8G ${virtio_diskdev} > /dev/null
159267559Salfred    fi
160248484Sneel
161267559Salfred    if [ ! -r ${virtio_diskdev} ]; then
162267559Salfred	    echo "virtio disk device file \"${virtio_diskdev}\" is not readable"
163267559Salfred	    exit 1
164267559Salfred    fi
165248484Sneel
166267559Salfred    if [ ! -w ${virtio_diskdev} ]; then
167267559Salfred	    echo "virtio disk device file \"${virtio_diskdev}\" is not writable"
168267559Salfred	    exit 1
169267559Salfred    fi
170267559Salfred}
171248484Sneel
172248484Sneelecho "Launching virtual machine \"$vmname\" ..."
173248484Sneel
174267559Salfredvirtio_diskdev="$disk_dev0"
175267559Salfred
176270513Srodrigc${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
177270513Srodrigc
178248484Sneelwhile [ 1 ]; do
179248484Sneel
180270754Srodrigc	file -s ${virtio_diskdev} | grep "boot sector" > /dev/null
181248484Sneel	rc=$?
182248484Sneel	if [ $rc -ne 0 ]; then
183270754Srodrigc		file -s ${virtio_diskdev} | grep ": Unix Fast File sys" > /dev/null
184248484Sneel		rc=$?
185248484Sneel	fi
186248484Sneel	if [ $rc -ne 0 ]; then
187248484Sneel		need_install=1
188248484Sneel	else
189248484Sneel		need_install=0
190248484Sneel	fi
191248484Sneel
192248484Sneel	if [ $force_install -eq 1 -o $need_install -eq 1 ]; then
193248484Sneel		if [ ! -r ${isofile} ]; then
194248484Sneel			echo -n "Installation CDROM image \"${isofile}\" "
195248484Sneel			echo    "is not readable"
196248484Sneel			exit 1
197248484Sneel		fi
198248484Sneel		BOOTDISK=${isofile}
199271447Srodrigc		installer_opt="-s 31:0,ahci-cd,${BOOTDISK}"
200248484Sneel	else
201248484Sneel		BOOTDISK=${virtio_diskdev}
202248484Sneel		installer_opt=""
203248484Sneel	fi
204248484Sneel
205264837Sjhb	${LOADER} -c ${console} -m ${memsize} -d ${BOOTDISK} ${loader_opt} \
206264837Sjhb		${vmname}
207271496Srodrigc	bhyve_exit=$?
208271496Srodrigc	if [ $bhyve_exit -ne 0 ]; then
209248484Sneel		break
210248484Sneel	fi
211248484Sneel
212267559Salfred	#
213267559Salfred	# Build up args for additional tap and disk devices now.
214267559Salfred	#
215267559Salfred	nextslot=2  # slot 0 is hostbridge, slot 1 is lpc
216267559Salfred	devargs=""  # accumulate disk/tap args here
217267559Salfred	i=0
218267559Salfred	while [ $i -lt $tap_total ] ; do
219267559Salfred	    eval "tapname=\$tap_dev${i}"
220267559Salfred	    devargs="$devargs -s $nextslot:0,virtio-net,${tapname} "
221267559Salfred	    nextslot=$(($nextslot + 1))
222267559Salfred	    i=$(($i + 1))
223267559Salfred	done
224267559Salfred
225267559Salfred	i=0
226267559Salfred	while [ $i -lt $disk_total ] ; do
227267559Salfred	    eval "disk=\$disk_dev${i}"
228267559Salfred	    make_and_check_diskdev "${disk}"
229267559Salfred	    devargs="$devargs -s $nextslot:0,virtio-blk,${disk} "
230267559Salfred	    nextslot=$(($nextslot + 1))
231267559Salfred	    i=$(($i + 1))
232267559Salfred	done
233267559Salfred
234257423Sneel	${FBSDRUN} -c ${cpus} -m ${memsize} ${apic_opt} -A -H -P	\
235248840Sneel		-g ${gdbport}						\
236248484Sneel		-s 0:0,hostbridge					\
237257293Sneel		-s 1:0,lpc						\
238267559Salfred		${devargs}						\
239264837Sjhb		-l com1,${console}					\
240248484Sneel		${installer_opt}					\
241248484Sneel		${vmname}
242270512Srodrigc
243271496Srodrigc	bhyve_exit=$?
244270512Srodrigc	# bhyve returns the following status codes:
245270512Srodrigc	#  0 - VM has been reset
246270512Srodrigc	#  1 - VM has been powered off
247270512Srodrigc	#  2 - VM has been halted
248270512Srodrigc	#  3 - VM generated a triple fault
249270512Srodrigc	#  all other non-zero status codes are errors
250270512Srodrigc	#
251271496Srodrigc	if [ $bhyve_exit -ne 0 ]; then
252248484Sneel		break
253248484Sneel	fi
254248484Sneeldone
255248484Sneel
256271496Srodrigc
257271496Srodrigccase $bhyve_exit in
258271496Srodrigc	0|1|2)
259271496Srodrigc		# Cleanup /dev/vmm entry when bhyve did not exit
260271496Srodrigc		# due to an error.
261271496Srodrigc		${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
262271496Srodrigc		;;
263271496Srodrigcesac
264271496Srodrigc
265271496Srodrigcexit $bhyve_exit
266