]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/examples/bhyve/vmrun.sh
ssh: update to OpenSSH v8.8p1
[FreeBSD/FreeBSD.git] / share / examples / bhyve / vmrun.sh
1 #!/bin/sh
2 #
3 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 #
5 # Copyright (c) 2013 NetApp, Inc.
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 # 1. Redistributions of source code must retain the above copyright
12 #    notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 #    notice, this list of conditions and the following disclaimer in the
15 #    documentation and/or other materials provided with the distribution.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 # SUCH DAMAGE.
28 #
29 # $FreeBSD$
30 #
31
32 LOADER=/usr/sbin/bhyveload
33 BHYVECTL=/usr/sbin/bhyvectl
34 FBSDRUN=/usr/sbin/bhyve
35
36 DEFAULT_MEMSIZE=512M
37 DEFAULT_CPUS=2
38 DEFAULT_TAPDEV=tap0
39 DEFAULT_CONSOLE=stdio
40
41 DEFAULT_NIC=virtio-net
42 DEFAULT_DISK=virtio-blk
43 DEFAULT_VIRTIO_DISK="./diskdev"
44 DEFAULT_ISOFILE="./release.iso"
45
46 DEFAULT_VNCHOST="127.0.0.1"
47 DEFAULT_VNCPORT=5900
48 DEFAULT_VNCSIZE="w=1024,h=768"
49
50 errmsg() {
51         echo "*** $1"
52 }
53
54 usage() {
55         local msg=$1
56
57         echo "Usage: vmrun.sh [-aAEhiTv] [-c <CPUs>] [-C <console>]" \
58             "[-d <disk file>]"
59         echo "                [-e <name=value>] [-f <path of firmware>]" \
60             "[-F <size>]"
61         echo "                [-H <directory>]"
62         echo "                [-I <location of installation iso>] [-l <loader>]"
63         echo "                [-L <VNC IP for UEFI framebuffer>]"
64         echo "                [-m <memsize>]" \
65             "[-n <network adapter emulation type>]"
66         echo "                [-P <port>] [-t <tapdev>] <vmname>"
67         echo ""
68         echo "       -h: display this help message"
69         echo "       -a: force memory mapped local APIC access"
70         echo "       -A: use AHCI disk emulation instead of ${DEFAULT_DISK}"
71         echo "       -c: number of virtual cpus (default: ${DEFAULT_CPUS})"
72         echo "       -C: console device (default: ${DEFAULT_CONSOLE})"
73         echo "       -d: virtio diskdev file (default: ${DEFAULT_VIRTIO_DISK})"
74         echo "       -e: set FreeBSD loader environment variable"
75         echo "       -E: Use UEFI mode"
76         echo "       -f: Use a specific UEFI firmware"
77         echo "       -F: Use a custom UEFI GOP framebuffer size" \
78             "(default: ${DEFAULT_VNCSIZE})"
79         echo "       -H: host filesystem to export to the loader"
80         echo "       -i: force boot of the Installation CDROM image"
81         echo "       -I: Installation CDROM image location" \
82             "(default: ${DEFAULT_ISOFILE})"
83         echo "       -l: the OS loader to use (default: /boot/userboot.so)"
84         echo "       -L: IP address for UEFI GOP VNC server" \
85             "(default: ${DEFAULT_VNCHOST}"
86         echo "       -m: memory size (default: ${DEFAULT_MEMSIZE})"
87         echo "       -n: network adapter emulation type" \
88             "(default: ${DEFAULT_NIC})"
89         echo "       -p: pass-through a host PCI device at bus/slot/func" \
90             "(e.g. 10/0/0)"
91         echo "       -P: UEFI GOP VNC port (default: ${DEFAULT_VNCPORT})"
92         echo "       -t: tap device for virtio-net (default: $DEFAULT_TAPDEV)"
93         echo "       -T: Enable tablet device (for UEFI GOP)"
94         echo "       -u: RTC keeps UTC time"
95         echo "       -v: Wait for VNC client connection before booting VM"
96         echo "       -w: ignore unimplemented MSRs"
97         echo ""
98         [ -n "$msg" ] && errmsg "$msg"
99         exit 1
100 }
101
102 if [ `id -u` -ne 0 ]; then
103         errmsg "This script must be executed with superuser privileges"
104         exit 1
105 fi
106
107 kldstat -n vmm > /dev/null 2>&1 
108 if [ $? -ne 0 ]; then
109         errmsg "vmm.ko is not loaded"
110         exit 1
111 fi
112
113 force_install=0
114 isofile=${DEFAULT_ISOFILE}
115 memsize=${DEFAULT_MEMSIZE}
116 console=${DEFAULT_CONSOLE}
117 cpus=${DEFAULT_CPUS}
118 nic=${DEFAULT_NIC}
119 tap_total=0
120 disk_total=0
121 disk_emulation=${DEFAULT_DISK}
122 loader_opt=""
123 bhyverun_opt="-H -A -P"
124 pass_total=0
125
126 # EFI-specific options
127 efi_mode=0
128 efi_firmware="/usr/local/share/uefi-firmware/BHYVE_UEFI.fd"
129 vncwait=""
130 vnchost=${DEFAULT_VNCHOST}
131 vncport=${DEFAULT_VNCPORT}
132 vncsize=${DEFAULT_VNCSIZE}
133 tablet=""
134
135 while getopts aAc:C:d:e:Ef:F:g:hH:iI:l:L:m:n:p:P:t:Tuvw c ; do
136         case $c in
137         a)
138                 bhyverun_opt="${bhyverun_opt} -a"
139                 ;;
140         A)
141                 disk_emulation="ahci-hd"
142                 ;;
143         c)
144                 cpus=${OPTARG}
145                 ;;
146         C)
147                 console=${OPTARG}
148                 ;;
149         d)
150                 disk_dev=${OPTARG%%,*}
151                 disk_opts=${OPTARG#${disk_dev}}
152                 eval "disk_dev${disk_total}=\"${disk_dev}\""
153                 eval "disk_opts${disk_total}=\"${disk_opts}\""
154                 disk_total=$(($disk_total + 1))
155                 ;;
156         e)
157                 loader_opt="${loader_opt} -e ${OPTARG}"
158                 ;;
159         E)
160                 efi_mode=1
161                 ;;
162         f)
163                 efi_firmware="${OPTARG}"
164                 ;;
165         F)
166                 vncsize="${OPTARG}"
167                 ;;
168         H)
169                 host_base=`realpath ${OPTARG}`
170                 ;;
171         i)
172                 force_install=1
173                 ;;
174         I)
175                 isofile=${OPTARG}
176                 ;;
177         l)
178                 loader_opt="${loader_opt} -l ${OPTARG}"
179                 ;;
180         L)
181                 vnchost="${OPTARG}"
182                 ;;
183         m)
184                 memsize=${OPTARG}
185                 ;;
186         n)
187                 nic=${OPTARG}
188                 ;;
189         p)
190                 eval "pass_dev${pass_total}=\"${OPTARG}\""
191                 pass_total=$(($pass_total + 1))
192                 ;;
193         P)
194                 vncport="${OPTARG}"
195                 ;;
196         t)
197                 eval "tap_dev${tap_total}=\"${OPTARG}\""
198                 tap_total=$(($tap_total + 1))
199                 ;;
200         T)
201                 tablet="-s 30,xhci,tablet"
202                 ;;
203         u)      
204                 bhyverun_opt="${bhyverun_opt} -u"
205                 ;;
206         v)
207                 vncwait=",wait"
208                 ;;
209         w)
210                 bhyverun_opt="${bhyverun_opt} -w"
211                 ;;
212         *)
213                 usage
214                 ;;
215         esac
216 done
217
218 if [ $tap_total -eq 0 ] ; then
219     tap_total=1
220     tap_dev0="${DEFAULT_TAPDEV}"
221 fi
222 if [ $disk_total -eq 0 ] ; then
223     disk_total=1
224     disk_dev0="${DEFAULT_VIRTIO_DISK}"
225
226 fi
227
228 shift $((${OPTIND} - 1))
229
230 if [ $# -ne 1 ]; then
231         usage "virtual machine name not specified"
232 fi
233
234 vmname="$1"
235 if [ -n "${host_base}" ]; then
236         loader_opt="${loader_opt} -h ${host_base}"
237 fi
238
239 # If PCI passthru devices are configured then guest memory must be wired
240 if [ ${pass_total} -gt 0 ]; then
241         loader_opt="${loader_opt} -S"
242         bhyverun_opt="${bhyverun_opt} -S"
243 fi
244
245 if [ ${efi_mode} -gt 0 ]; then
246         if [ ! -f ${efi_firmware} ]; then
247                 echo "Error: EFI Firmware ${efi_firmware} doesn't exist." \
248                     "Try: pkg install uefi-edk2-bhyve"
249                 exit 1
250         fi
251 fi
252
253 make_and_check_diskdev()
254 {
255     local virtio_diskdev="$1"
256     # Create the virtio diskdev file if needed
257     if [ ! -e ${virtio_diskdev} ]; then
258             echo "virtio disk device file \"${virtio_diskdev}\" does not exist."
259             echo "Creating it ..."
260             truncate -s 8G ${virtio_diskdev} > /dev/null
261     fi
262
263     if [ ! -r ${virtio_diskdev} ]; then
264             echo "virtio disk device file \"${virtio_diskdev}\" is not readable"
265             exit 1
266     fi
267
268     if [ ! -w ${virtio_diskdev} ]; then
269             echo "virtio disk device file \"${virtio_diskdev}\" is not writable"
270             exit 1
271     fi
272 }
273
274 echo "Launching virtual machine \"$vmname\" ..."
275
276 first_diskdev="$disk_dev0"
277
278 ${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
279
280 while [ 1 ]; do
281
282         file -s ${first_diskdev} | grep "boot sector" > /dev/null
283         rc=$?
284         if [ $rc -ne 0 ]; then
285                 file -s ${first_diskdev} | \
286                     grep ": Unix Fast File sys" > /dev/null
287                 rc=$?
288         fi
289         if [ $rc -ne 0 ]; then
290                 need_install=1
291         else
292                 need_install=0
293         fi
294
295         if [ $force_install -eq 1 -o $need_install -eq 1 ]; then
296                 if [ ! -r ${isofile} ]; then
297                         echo -n "Installation CDROM image \"${isofile}\" "
298                         echo    "is not readable"
299                         exit 1
300                 fi
301                 BOOTDISKS="-d ${isofile}"
302                 installer_opt="-s 31:0,ahci-cd,${isofile}"
303         else
304                 BOOTDISKS=""
305                 i=0
306                 while [ $i -lt $disk_total ] ; do
307                         eval "disk=\$disk_dev${i}"
308                         if [ -r ${disk} ] ; then
309                                 BOOTDISKS="$BOOTDISKS -d ${disk} "
310                         fi
311                         i=$(($i + 1))
312                 done
313                 installer_opt=""
314         fi
315
316         if [ ${efi_mode} -eq 0 ]; then
317                 ${LOADER} -c ${console} -m ${memsize} ${BOOTDISKS} \
318                     ${loader_opt} ${vmname}
319                 bhyve_exit=$?
320                 if [ $bhyve_exit -ne 0 ]; then
321                         break
322                 fi
323         fi
324
325         #
326         # Build up args for additional tap and disk devices now.
327         #
328         nextslot=2  # slot 0 is hostbridge, slot 1 is lpc
329         devargs=""  # accumulate disk/tap args here
330         i=0
331         while [ $i -lt $tap_total ] ; do
332             eval "tapname=\$tap_dev${i}"
333             devargs="$devargs -s $nextslot:0,${nic},${tapname} "
334             nextslot=$(($nextslot + 1))
335             i=$(($i + 1))
336         done
337
338         i=0
339         while [ $i -lt $disk_total ] ; do
340             eval "disk=\$disk_dev${i}"
341             eval "opts=\$disk_opts${i}"
342             make_and_check_diskdev "${disk}"
343             devargs="$devargs -s $nextslot:0,$disk_emulation,${disk}${opts} "
344             nextslot=$(($nextslot + 1))
345             i=$(($i + 1))
346         done
347
348         i=0
349         while [ $i -lt $pass_total ] ; do
350             eval "pass=\$pass_dev${i}"
351             devargs="$devargs -s $nextslot:0,passthru,${pass} "
352             nextslot=$(($nextslot + 1))
353             i=$(($i + 1))
354         done
355
356         efiargs=""
357         if [ ${efi_mode} -gt 0 ]; then
358                 efiargs="-s 29,fbuf,tcp=${vnchost}:${vncport},"
359                 efiargs="${efiargs}${vncsize}${vncwait}"
360                 efiargs="${efiargs} -l bootrom,${efi_firmware}"
361                 efiargs="${efiargs} ${tablet}"
362         fi
363
364         ${FBSDRUN} -c ${cpus} -m ${memsize} ${bhyverun_opt}             \
365                 -s 0:0,hostbridge                                       \
366                 -s 1:0,lpc                                              \
367                 ${efiargs}                                              \
368                 ${devargs}                                              \
369                 -l com1,${console}                                      \
370                 ${installer_opt}                                        \
371                 ${vmname}
372
373         bhyve_exit=$?
374         # bhyve returns the following status codes:
375         #  0 - VM has been reset
376         #  1 - VM has been powered off
377         #  2 - VM has been halted
378         #  3 - VM generated a triple fault
379         #  all other non-zero status codes are errors
380         #
381         if [ $bhyve_exit -ne 0 ]; then
382                 break
383         fi
384 done
385
386
387 case $bhyve_exit in
388         0|1|2)
389                 # Cleanup /dev/vmm entry when bhyve did not exit
390                 # due to an error.
391                 ${BHYVECTL} --vm=${vmname} --destroy > /dev/null 2>&1
392                 ;;
393 esac
394
395 exit $bhyve_exit