]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/examples/bhyve/vmrun.sh
MFV r266993:
[FreeBSD/FreeBSD.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 [-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 "       -t: tap device for virtio-net (default is $DEFAULT_TAPDEV)"
66         echo ""
67         [ -n "$msg" ] && errmsg "$msg"
68         exit 1
69 }
70
71 if [ `id -u` -ne 0 ]; then
72         errmsg "This script must be executed with superuser privileges"
73         exit 1
74 fi
75
76 kldstat -n vmm > /dev/null 2>&1 
77 if [ $? -ne 0 ]; then
78         errmsg "vmm.ko is not loaded"
79         exit 1
80 fi
81
82 force_install=0
83 isofile=${DEFAULT_ISOFILE}
84 memsize=${DEFAULT_MEMSIZE}
85 console=${DEFAULT_CONSOLE}
86 cpus=${DEFAULT_CPUS}
87 tap_total=0
88 disk_total=0
89 apic_opt=""
90 gdbport=0
91 loader_opt=""
92
93 while getopts ac:C:d:e:g:hH:iI:m:t: c ; do
94         case $c in
95         a)
96                 apic_opt="-a"
97                 ;;
98         c)
99                 cpus=${OPTARG}
100                 ;;
101         C)
102                 console=${OPTARG}
103                 ;;
104         d)
105                 eval "disk_dev${disk_total}=\"${OPTARG}\""
106                 disk_total=$(($disk_total + 1))
107                 ;;
108         e)
109                 loader_opt="${loader_opt} -e ${OPTARG}"
110                 ;;
111         g)      
112                 gdbport=${OPTARG}
113                 ;;
114         H)
115                 host_base=`realpath ${OPTARG}`
116                 ;;
117         i)
118                 force_install=1
119                 ;;
120         I)
121                 isofile=${OPTARG}
122                 ;;
123         m)
124                 memsize=${OPTARG}
125                 ;;
126         t)
127                 eval "tap_dev${tap_total}=\"${OPTARG}\""
128                 tap_total=$(($tap_total + 1))
129                 ;;
130         *)
131                 usage
132                 ;;
133         esac
134 done
135
136 if [ $tap_total -eq 0 ] ; then
137     tap_total=1
138     tap_dev0="${DEFAULT_TAPDEV}"
139 fi
140 if [ $disk_total -eq 0 ] ; then
141     disk_total=1
142     disk_dev0="${DEFAULT_VIRTIO_DISK}"
143
144 fi
145
146 shift $((${OPTIND} - 1))
147
148 if [ $# -ne 1 ]; then
149         usage "virtual machine name not specified"
150 fi
151
152 vmname="$1"
153 if [ -n "${host_base}" ]; then
154         loader_opt="${loader_opt} -h ${host_base}"
155 fi
156
157 make_and_check_diskdev()
158 {
159     local virtio_diskdev="$1"
160     # Create the virtio diskdev file if needed
161     if [ ! -e ${virtio_diskdev} ]; then
162             echo "virtio disk device file \"${virtio_diskdev}\" does not exist."
163             echo "Creating it ..."
164             truncate -s 8G ${virtio_diskdev} > /dev/null
165     fi
166
167     if [ ! -r ${virtio_diskdev} ]; then
168             echo "virtio disk device file \"${virtio_diskdev}\" is not readable"
169             exit 1
170     fi
171
172     if [ ! -w ${virtio_diskdev} ]; then
173             echo "virtio disk device file \"${virtio_diskdev}\" is not writable"
174             exit 1
175     fi
176 }
177
178 echo "Launching virtual machine \"$vmname\" ..."
179
180 virtio_diskdev="$disk_dev0"
181
182 ${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
183
184 while [ 1 ]; do
185
186         file -s ${virtio_diskdev} | grep "boot sector" > /dev/null
187         rc=$?
188         if [ $rc -ne 0 ]; then
189                 file -s ${virtio_diskdev} | grep ": Unix Fast File sys" > /dev/null
190                 rc=$?
191         fi
192         if [ $rc -ne 0 ]; then
193                 need_install=1
194         else
195                 need_install=0
196         fi
197
198         if [ $force_install -eq 1 -o $need_install -eq 1 ]; then
199                 if [ ! -r ${isofile} ]; then
200                         echo -n "Installation CDROM image \"${isofile}\" "
201                         echo    "is not readable"
202                         exit 1
203                 fi
204                 BOOTDISK=${isofile}
205                 installer_opt="-s 31:0,ahci-cd,${BOOTDISK}"
206         else
207                 BOOTDISK=${virtio_diskdev}
208                 installer_opt=""
209         fi
210
211         ${LOADER} -c ${console} -m ${memsize} -d ${BOOTDISK} ${loader_opt} \
212                 ${vmname}
213         bhyve_exit=$?
214         if [ $bhyve_exit -ne 0 ]; then
215                 break
216         fi
217
218         #
219         # Build up args for additional tap and disk devices now.
220         #
221         nextslot=2  # slot 0 is hostbridge, slot 1 is lpc
222         devargs=""  # accumulate disk/tap args here
223         i=0
224         while [ $i -lt $tap_total ] ; do
225             eval "tapname=\$tap_dev${i}"
226             devargs="$devargs -s $nextslot:0,virtio-net,${tapname} "
227             nextslot=$(($nextslot + 1))
228             i=$(($i + 1))
229         done
230
231         i=0
232         while [ $i -lt $disk_total ] ; do
233             eval "disk=\$disk_dev${i}"
234             make_and_check_diskdev "${disk}"
235             devargs="$devargs -s $nextslot:0,virtio-blk,${disk} "
236             nextslot=$(($nextslot + 1))
237             i=$(($i + 1))
238         done
239
240         ${FBSDRUN} -c ${cpus} -m ${memsize} ${apic_opt} -A -H -P        \
241                 -g ${gdbport}                                           \
242                 -s 0:0,hostbridge                                       \
243                 -s 1:0,lpc                                              \
244                 ${devargs}                                              \
245                 -l com1,${console}                                      \
246                 ${installer_opt}                                        \
247                 ${vmname}
248
249         bhyve_exit=$?
250         # bhyve returns the following status codes:
251         #  0 - VM has been reset
252         #  1 - VM has been powered off
253         #  2 - VM has been halted
254         #  3 - VM generated a triple fault
255         #  all other non-zero status codes are errors
256         #
257         if [ $bhyve_exit -ne 0 ]; then
258                 break
259         fi
260 done
261
262
263 case $bhyve_exit in
264         0|1|2)
265                 # Cleanup /dev/vmm entry when bhyve did not exit
266                 # due to an error.
267                 ${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
268                 ;;
269 esac
270
271 exit $bhyve_exit