]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - share/examples/bhyve/vmrun.sh
MFC 256657,257423,264837,267559:
[FreeBSD/stable/10.git] / share / examples / bhyve / vmrun.sh
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$
28 #
29
30 LOADER=/usr/sbin/bhyveload
31 BHYVECTL=/usr/sbin/bhyvectl
32 FBSDRUN=/usr/sbin/bhyve
33
34 DEFAULT_MEMSIZE=512M
35 DEFAULT_CPUS=2
36 DEFAULT_TAPDEV=tap0
37 DEFAULT_CONSOLE=stdio
38
39 DEFAULT_VIRTIO_DISK="./diskdev"
40 DEFAULT_ISOFILE="./release.iso"
41
42 usage() {
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
66 if [ `id -u` -ne 0 ]; then
67         usage
68 fi
69
70 kldstat -n vmm > /dev/null 2>&1 
71 if [ $? -ne 0 ]; then
72         echo "vmm.ko is not loaded!"
73         exit 1
74 fi
75
76 force_install=0
77 isofile=${DEFAULT_ISOFILE}
78 memsize=${DEFAULT_MEMSIZE}
79 console=${DEFAULT_CONSOLE}
80 cpus=${DEFAULT_CPUS}
81 tap_total=0
82 disk_total=0
83 apic_opt=""
84 gdbport=0
85 loader_opt=""
86
87 while 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                 eval "disk_dev${disk_total}=\"${OPTARG}\""
100                 disk_total=$(($disk_total + 1))
101                 ;;
102         e)
103                 loader_opt="${loader_opt} -e ${OPTARG}"
104                 ;;
105         g)      
106                 gdbport=${OPTARG}
107                 ;;
108         H)
109                 host_base=`realpath ${OPTARG}`
110                 ;;
111         i)
112                 force_install=1
113                 ;;
114         I)
115                 isofile=${OPTARG}
116                 ;;
117         m)
118                 memsize=${OPTARG}
119                 ;;
120         t)
121                 eval "tap_dev${tap_total}=\"${OPTARG}\""
122                 tap_total=$(($tap_total + 1))
123                 ;;
124         *)
125                 usage
126                 ;;
127         esac
128 done
129
130 if [ $tap_total -eq 0 ] ; then
131     tap_total=1
132     tap_dev0="${DEFAULT_TAPDEV}"
133 fi
134 if [ $disk_total -eq 0 ] ; then
135     disk_total=1
136     disk_dev0="${DEFAULT_VIRTIO_DISK}"
137
138 fi
139
140 shift $((${OPTIND} - 1))
141
142 if [ $# -ne 1 ]; then
143         usage
144 fi
145
146 vmname="$1"
147 if [ -n "${host_base}" ]; then
148         loader_opt="${loader_opt} -h ${host_base}"
149 fi
150
151 make_and_check_diskdev()
152 {
153     local virtio_diskdev="$1"
154     # Create the virtio diskdev file if needed
155     if [ ! -f ${virtio_diskdev} ]; then
156             echo "virtio disk device file \"${virtio_diskdev}\" does not exist."
157             echo "Creating it ..."
158             truncate -s 8G ${virtio_diskdev} > /dev/null
159     fi
160
161     if [ ! -r ${virtio_diskdev} ]; then
162             echo "virtio disk device file \"${virtio_diskdev}\" is not readable"
163             exit 1
164     fi
165
166     if [ ! -w ${virtio_diskdev} ]; then
167             echo "virtio disk device file \"${virtio_diskdev}\" is not writable"
168             exit 1
169     fi
170 }
171
172 echo "Launching virtual machine \"$vmname\" ..."
173
174 virtio_diskdev="$disk_dev0"
175
176 while [ 1 ]; do
177         ${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
178
179         file ${virtio_diskdev} | grep "boot sector" > /dev/null
180         rc=$?
181         if [ $rc -ne 0 ]; then
182                 file ${virtio_diskdev} | grep ": Unix Fast File sys" > /dev/null
183                 rc=$?
184         fi
185         if [ $rc -ne 0 ]; then
186                 need_install=1
187         else
188                 need_install=0
189         fi
190
191         if [ $force_install -eq 1 -o $need_install -eq 1 ]; then
192                 if [ ! -r ${isofile} ]; then
193                         echo -n "Installation CDROM image \"${isofile}\" "
194                         echo    "is not readable"
195                         exit 1
196                 fi
197                 BOOTDISK=${isofile}
198                 installer_opt="-s 31:0,virtio-blk,${BOOTDISK}"
199         else
200                 BOOTDISK=${virtio_diskdev}
201                 installer_opt=""
202         fi
203
204         ${LOADER} -c ${console} -m ${memsize} -d ${BOOTDISK} ${loader_opt} \
205                 ${vmname}
206         if [ $? -ne 0 ]; then
207                 break
208         fi
209
210         #
211         # Build up args for additional tap and disk devices now.
212         #
213         nextslot=2  # slot 0 is hostbridge, slot 1 is lpc
214         devargs=""  # accumulate disk/tap args here
215         i=0
216         while [ $i -lt $tap_total ] ; do
217             eval "tapname=\$tap_dev${i}"
218             devargs="$devargs -s $nextslot:0,virtio-net,${tapname} "
219             nextslot=$(($nextslot + 1))
220             i=$(($i + 1))
221         done
222
223         i=0
224         while [ $i -lt $disk_total ] ; do
225             eval "disk=\$disk_dev${i}"
226             make_and_check_diskdev "${disk}"
227             devargs="$devargs -s $nextslot:0,virtio-blk,${disk} "
228             nextslot=$(($nextslot + 1))
229             i=$(($i + 1))
230         done
231
232         ${FBSDRUN} -c ${cpus} -m ${memsize} ${apic_opt} -A -H -P        \
233                 -g ${gdbport}                                           \
234                 -s 0:0,hostbridge                                       \
235                 -s 1:0,lpc                                              \
236                 ${devargs}                                              \
237                 -l com1,${console}                                      \
238                 ${installer_opt}                                        \
239                 ${vmname}
240         if [ $? -ne 0 ]; then
241                 break
242         fi
243 done
244
245 exit 99