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