vmrun.sh revision 284414
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: stable/10/share/examples/bhyve/vmrun.sh 284414 2015-06-15 13:18:30Z avg $
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}
81tap_total=0
82disk_total=0
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		disk_dev=${OPTARG%%,*}
100		disk_opts=${OPTARG#${disk_dev}}
101		eval "disk_dev${disk_total}=\"${disk_dev}\""
102		eval "disk_opts${disk_total}=\"${disk_opts}\""
103		disk_total=$(($disk_total + 1))
104		;;
105	e)
106		loader_opt="${loader_opt} -e ${OPTARG}"
107		;;
108	g)	
109		gdbport=${OPTARG}
110		;;
111	H)
112		host_base=`realpath ${OPTARG}`
113		;;
114	i)
115		force_install=1
116		;;
117	I)
118		isofile=${OPTARG}
119		;;
120	m)
121		memsize=${OPTARG}
122		;;
123	t)
124		eval "tap_dev${tap_total}=\"${OPTARG}\""
125		tap_total=$(($tap_total + 1))
126		;;
127	*)
128		usage
129		;;
130	esac
131done
132
133if [ $tap_total -eq 0 ] ; then
134    tap_total=1
135    tap_dev0="${DEFAULT_TAPDEV}"
136fi
137if [ $disk_total -eq 0 ] ; then
138    disk_total=1
139    disk_dev0="${DEFAULT_VIRTIO_DISK}"
140
141fi
142
143shift $((${OPTIND} - 1))
144
145if [ $# -ne 1 ]; then
146	usage
147fi
148
149vmname="$1"
150if [ -n "${host_base}" ]; then
151	loader_opt="${loader_opt} -h ${host_base}"
152fi
153
154make_and_check_diskdev()
155{
156    local virtio_diskdev="$1"
157    # Create the virtio diskdev file if needed
158    if [ ! -f ${virtio_diskdev} ]; then
159	    echo "virtio disk device file \"${virtio_diskdev}\" does not exist."
160	    echo "Creating it ..."
161	    truncate -s 8G ${virtio_diskdev} > /dev/null
162    fi
163
164    if [ ! -r ${virtio_diskdev} ]; then
165	    echo "virtio disk device file \"${virtio_diskdev}\" is not readable"
166	    exit 1
167    fi
168
169    if [ ! -w ${virtio_diskdev} ]; then
170	    echo "virtio disk device file \"${virtio_diskdev}\" is not writable"
171	    exit 1
172    fi
173}
174
175echo "Launching virtual machine \"$vmname\" ..."
176
177first_diskdev="$disk_dev0"
178
179${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
180
181while [ 1 ]; do
182
183	file -s ${first_diskdev} | grep "boot sector" > /dev/null
184	rc=$?
185	if [ $rc -ne 0 ]; then
186		file -s ${first_diskdev} | grep ": Unix Fast File sys" > /dev/null
187		rc=$?
188	fi
189	if [ $rc -ne 0 ]; then
190		need_install=1
191	else
192		need_install=0
193	fi
194
195	if [ $force_install -eq 1 -o $need_install -eq 1 ]; then
196		if [ ! -r ${isofile} ]; then
197			echo -n "Installation CDROM image \"${isofile}\" "
198			echo    "is not readable"
199			exit 1
200		fi
201		BOOTDISKS="-d ${isofile}"
202		installer_opt="-s 31:0,ahci-cd,${isofile}"
203	else
204		BOOTDISKS=""
205		i=0
206		while [ $i -lt $disk_total ] ; do
207			eval "disk=\$disk_dev${i}"
208			if [ -r ${disk} ] ; then
209				BOOTDISKS="$BOOTDISKS -d ${disk} "
210			fi
211			i=$(($i + 1))
212		done
213		installer_opt=""
214	fi
215
216	${LOADER} -c ${console} -m ${memsize} ${BOOTDISKS} ${loader_opt} \
217		${vmname}
218	bhyve_exit=$?
219	if [ $bhyve_exit -ne 0 ]; then
220		break
221	fi
222
223	#
224	# Build up args for additional tap and disk devices now.
225	#
226	nextslot=2  # slot 0 is hostbridge, slot 1 is lpc
227	devargs=""  # accumulate disk/tap args here
228	i=0
229	while [ $i -lt $tap_total ] ; do
230	    eval "tapname=\$tap_dev${i}"
231	    devargs="$devargs -s $nextslot:0,virtio-net,${tapname} "
232	    nextslot=$(($nextslot + 1))
233	    i=$(($i + 1))
234	done
235
236	i=0
237	while [ $i -lt $disk_total ] ; do
238	    eval "disk=\$disk_dev${i}"
239	    eval "opts=\$disk_opts${i}"
240	    make_and_check_diskdev "${disk}"
241	    devargs="$devargs -s $nextslot:0,virtio-blk,${disk}${opts} "
242	    nextslot=$(($nextslot + 1))
243	    i=$(($i + 1))
244	done
245
246	${FBSDRUN} -c ${cpus} -m ${memsize} ${apic_opt} -A -H -P	\
247		-g ${gdbport}						\
248		-s 0:0,hostbridge					\
249		-s 1:0,lpc						\
250		${devargs}						\
251		-l com1,${console}					\
252		${installer_opt}					\
253		${vmname}
254
255	bhyve_exit=$?
256	# bhyve returns the following status codes:
257	#  0 - VM has been reset
258	#  1 - VM has been powered off
259	#  2 - VM has been halted
260	#  3 - VM generated a triple fault
261	#  all other non-zero status codes are errors
262	#
263	if [ $bhyve_exit -ne 0 ]; then
264		break
265	fi
266done
267
268
269case $bhyve_exit in
270	0|1|2)
271		# Cleanup /dev/vmm entry when bhyve did not exit
272		# due to an error.
273		${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
274		;;
275esac
276
277exit $bhyve_exit
278