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