Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-ci
Path: blob/main/vm/scripts/start_vm.sh
1130 views
1
#!/bin/sh
2
#
3
# Copyright (c) 2014 Craig Rodrigues
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
28
usage()
29
{
30
echo ""
31
echo "$0 CONF=[configuration file]"
32
echo ""
33
}
34
35
# Allow users to pass in from the command line
36
# $0 A=VAL1 B=VAL2
37
#
38
# which will set the A and B variables
39
for f in $@;
40
do
41
if [ "$f" != ${f%%=*} ]; then
42
eval $f
43
shift
44
fi
45
done
46
47
if [ -z "${CONF}" -o ! -f "${CONF}" ]; then
48
usage
49
exit 1
50
fi
51
52
. ${CONF}
53
54
PIDFILE=/var/run/vmm/${VM}.pid
55
if [ -f ${PIDFILE} ]; then
56
PID=$(pgrep -F $PIDFILE bhyve 2> /dev/null)
57
if [ -n "$PID" ]; then
58
echo ""
59
echo "${PIDFILE} exists. Not starting ${VM}"
60
echo ""
61
exit 1
62
else
63
rm -f ${PIDFILE}
64
fi
65
fi
66
67
if [ -e /dev/vmm/${VM} ]; then
68
/usr/sbin/bhyvectl --vm=${VM} --destroy
69
fi
70
71
mkdir -p /var/run/vmm
72
(
73
while true
74
do
75
CONS_A=/dev/nmdm${VM}A
76
CONS_B=${CONS_A%%A}B
77
touch ${CONS_A}
78
echo "Starting BHyve virtual machine named '${VM}'. Use 'cu -l ${CONS_B}' to access console"
79
cmd="/usr/sbin/bhyveload -m ${MEM} -d ${IMG} -c ${CONS_A} ${VM}"
80
$cmd
81
ret=$?
82
if [ $ret -ne 0 ]; then
83
echo "[FAILED]: $cmd"
84
exit $ret
85
fi
86
ifconfig ${BRIDGE} up
87
touch ${CONS_A}
88
pidfile="/var/run/vmm/${VM}.pid"
89
cmd="/usr/sbin/bhyve -c ${CPU} -m ${MEM} -A -H -P -g 0 -s 0:0,hostbridge -s 1:0,lpc -s 2:0,virtio-net,${TAP},mac=${MAC} -s 3:0,virtio-blk,${IMG} -l com1,${CONS_A} ${VM}"
90
$cmd &
91
cmd_pid="$!"
92
echo -n $cmd_pid > ${pidfile}
93
wait %1
94
bhyve_status=$?
95
rm -f ${pidfile}
96
if [ $bhyve_status -ne 0 ]; then
97
break
98
fi
99
done
100
) &
101
102