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