vmrun.sh revision 284539
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 284539 2015-06-18 06:00:17Z 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
37DEFAULT_CONSOLE=stdio
38
39DEFAULT_VIRTIO_DISK="./diskdev"
40DEFAULT_ISOFILE="./release.iso"
41
42errmsg() {
43	echo "*** $1"
44}
45
46usage() {
47	local msg=$1
48
49	echo "Usage: vmrun.sh [-ahi] [-c <CPUs>] [-C <console>] [-d <disk file>]"
50	echo "                [-e <name=value>] [-g <gdbport> ] [-H <directory>]"
51	echo "                [-I <location of installation iso>] [-m <memsize>]"
52	echo "                [-t <tapdev>] <vmname>"
53	echo ""
54	echo "       -h: display this help message"
55	echo "       -a: force memory mapped local APIC access"
56	echo "       -c: number of virtual cpus (default is ${DEFAULT_CPUS})"
57	echo "       -C: console device (default is ${DEFAULT_CONSOLE})"
58	echo "       -d: virtio diskdev file (default is ${DEFAULT_VIRTIO_DISK})"
59	echo "       -e: set FreeBSD loader environment variable"
60	echo "       -g: listen for connection from kgdb at <gdbport>"
61	echo "       -H: host filesystem to export to the loader"
62	echo "       -i: force boot of the Installation CDROM image"
63	echo "       -I: Installation CDROM image location (default is ${DEFAULT_ISOFILE})"
64	echo "       -m: memory size (default is ${DEFAULT_MEMSIZE})"
65	echo "       -p: pass-through a host PCI device at bus/slot/func (e.g. 10/0/0)"
66	echo "       -t: tap device for virtio-net (default is $DEFAULT_TAPDEV)"
67	echo ""
68	[ -n "$msg" ] && errmsg "$msg"
69	exit 1
70}
71
72if [ `id -u` -ne 0 ]; then
73	errmsg "This script must be executed with superuser privileges"
74	exit 1
75fi
76
77kldstat -n vmm > /dev/null 2>&1 
78if [ $? -ne 0 ]; then
79	errmsg "vmm.ko is not loaded"
80	exit 1
81fi
82
83force_install=0
84isofile=${DEFAULT_ISOFILE}
85memsize=${DEFAULT_MEMSIZE}
86console=${DEFAULT_CONSOLE}
87cpus=${DEFAULT_CPUS}
88tap_total=0
89disk_total=0
90gdbport=0
91loader_opt=""
92bhyverun_opt="-H -A -P"
93pass_total=0
94
95while getopts ac:C:d:e:g:hH:iI:m:p:t: c ; do
96	case $c in
97	a)
98		bhyverun_opt="${bhyverun_opt} -a"
99		;;
100	c)
101		cpus=${OPTARG}
102		;;
103	C)
104		console=${OPTARG}
105		;;
106	d)
107		disk_dev=${OPTARG%%,*}
108		disk_opts=${OPTARG#${disk_dev}}
109		eval "disk_dev${disk_total}=\"${disk_dev}\""
110		eval "disk_opts${disk_total}=\"${disk_opts}\""
111		disk_total=$(($disk_total + 1))
112		;;
113	e)
114		loader_opt="${loader_opt} -e ${OPTARG}"
115		;;
116	g)	
117		gdbport=${OPTARG}
118		;;
119	H)
120		host_base=`realpath ${OPTARG}`
121		;;
122	i)
123		force_install=1
124		;;
125	I)
126		isofile=${OPTARG}
127		;;
128	m)
129		memsize=${OPTARG}
130		;;
131	p)
132		eval "pass_dev${pass_total}=\"${OPTARG}\""
133		pass_total=$(($pass_total + 1))
134		;;
135	t)
136		eval "tap_dev${tap_total}=\"${OPTARG}\""
137		tap_total=$(($tap_total + 1))
138		;;
139	*)
140		usage
141		;;
142	esac
143done
144
145if [ $tap_total -eq 0 ] ; then
146    tap_total=1
147    tap_dev0="${DEFAULT_TAPDEV}"
148fi
149if [ $disk_total -eq 0 ] ; then
150    disk_total=1
151    disk_dev0="${DEFAULT_VIRTIO_DISK}"
152
153fi
154
155shift $((${OPTIND} - 1))
156
157if [ $# -ne 1 ]; then
158	usage "virtual machine name not specified"
159fi
160
161vmname="$1"
162if [ -n "${host_base}" ]; then
163	loader_opt="${loader_opt} -h ${host_base}"
164fi
165
166# If PCI passthru devices are configured then guest memory must be wired
167if [ ${pass_total} -gt 0 ]; then
168	loader_opt="${loader_opt} -S"
169	bhyverun_opt="${bhyverun_opt} -S"
170fi
171
172make_and_check_diskdev()
173{
174    local virtio_diskdev="$1"
175    # Create the virtio diskdev file if needed
176    if [ ! -e ${virtio_diskdev} ]; then
177	    echo "virtio disk device file \"${virtio_diskdev}\" does not exist."
178	    echo "Creating it ..."
179	    truncate -s 8G ${virtio_diskdev} > /dev/null
180    fi
181
182    if [ ! -r ${virtio_diskdev} ]; then
183	    echo "virtio disk device file \"${virtio_diskdev}\" is not readable"
184	    exit 1
185    fi
186
187    if [ ! -w ${virtio_diskdev} ]; then
188	    echo "virtio disk device file \"${virtio_diskdev}\" is not writable"
189	    exit 1
190    fi
191}
192
193echo "Launching virtual machine \"$vmname\" ..."
194
195first_diskdev="$disk_dev0"
196
197${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
198
199while [ 1 ]; do
200
201	file -s ${first_diskdev} | grep "boot sector" > /dev/null
202	rc=$?
203	if [ $rc -ne 0 ]; then
204		file -s ${first_diskdev} | grep ": Unix Fast File sys" > /dev/null
205		rc=$?
206	fi
207	if [ $rc -ne 0 ]; then
208		need_install=1
209	else
210		need_install=0
211	fi
212
213	if [ $force_install -eq 1 -o $need_install -eq 1 ]; then
214		if [ ! -r ${isofile} ]; then
215			echo -n "Installation CDROM image \"${isofile}\" "
216			echo    "is not readable"
217			exit 1
218		fi
219		BOOTDISKS="-d ${isofile}"
220		installer_opt="-s 31:0,ahci-cd,${isofile}"
221	else
222		BOOTDISKS=""
223		i=0
224		while [ $i -lt $disk_total ] ; do
225			eval "disk=\$disk_dev${i}"
226			if [ -r ${disk} ] ; then
227				BOOTDISKS="$BOOTDISKS -d ${disk} "
228			fi
229			i=$(($i + 1))
230		done
231		installer_opt=""
232	fi
233
234	${LOADER} -c ${console} -m ${memsize} ${BOOTDISKS} ${loader_opt} \
235		${vmname}
236	bhyve_exit=$?
237	if [ $bhyve_exit -ne 0 ]; then
238		break
239	fi
240
241	#
242	# Build up args for additional tap and disk devices now.
243	#
244	nextslot=2  # slot 0 is hostbridge, slot 1 is lpc
245	devargs=""  # accumulate disk/tap args here
246	i=0
247	while [ $i -lt $tap_total ] ; do
248	    eval "tapname=\$tap_dev${i}"
249	    devargs="$devargs -s $nextslot:0,virtio-net,${tapname} "
250	    nextslot=$(($nextslot + 1))
251	    i=$(($i + 1))
252	done
253
254	i=0
255	while [ $i -lt $disk_total ] ; do
256	    eval "disk=\$disk_dev${i}"
257	    eval "opts=\$disk_opts${i}"
258	    make_and_check_diskdev "${disk}"
259	    devargs="$devargs -s $nextslot:0,virtio-blk,${disk}${opts} "
260	    nextslot=$(($nextslot + 1))
261	    i=$(($i + 1))
262	done
263
264	i=0
265	while [ $i -lt $pass_total ] ; do
266	    eval "pass=\$pass_dev${i}"
267	    devargs="$devargs -s $nextslot:0,passthru,${pass} "
268	    nextslot=$(($nextslot + 1))
269	    i=$(($i + 1))
270        done
271
272	${FBSDRUN} -c ${cpus} -m ${memsize} ${bhyverun_opt}		\
273		-g ${gdbport}						\
274		-s 0:0,hostbridge					\
275		-s 1:0,lpc						\
276		${devargs}						\
277		-l com1,${console}					\
278		${installer_opt}					\
279		${vmname}
280
281	bhyve_exit=$?
282	# bhyve returns the following status codes:
283	#  0 - VM has been reset
284	#  1 - VM has been powered off
285	#  2 - VM has been halted
286	#  3 - VM generated a triple fault
287	#  all other non-zero status codes are errors
288	#
289	if [ $bhyve_exit -ne 0 ]; then
290		break
291	fi
292done
293
294
295case $bhyve_exit in
296	0|1|2)
297		# Cleanup /dev/vmm entry when bhyve did not exit
298		# due to an error.
299		${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
300		;;
301esac
302
303exit $bhyve_exit
304