rc.d scripts for starting BHyve from rc.d
Craig Rodrigues
rodrigc at FreeBSD.org
Fri Jan 24 03:44:26 UTC 2014
Hi,
I put together some scripts for starting a BHyve VM
when a system boots. When the system boots, the console
of the VM uses /dev/nmdm. It is possible to connect
to the console of the VM with: cu -l /dev/nmdm0B
It is a bit rough, but works nicely.
Does someone out there have better scripts for doing this?
Can some of the existing rc.d scripts for jails be reused for BHyve?
I did the following:
== put following entries in /etc/sysctl.conf ==
# BHyve needs this for tap interfaces
net.link.tap.user_open=1
net.link.tap.up_on_open=1
=================================================================
== put following in /etc/rc.conf ==
#####################################################
# Create tap devices, one tap interface per BHyve VM.
# Add the tap interfaces to bridge0
####################################################
cloned_interfaces="bridge0 tap0"
autobridge_interfaces="bridge0"
# change igb0 to whatever NIC you are using
autobridge_bridge0="tap* igb0"
== put following file in /usr/local/etc/rc.d/bhyvevm ==
#!/bin/sh
#
# $FreeBSD$
#
# PROVIDE: bhyvevm
# REQUIRE: netif bgfsck sshd LOGIN localpkg
# KEYWORD: shutdown
#
. /etc/rc.subr
name="bhyvevm"
rcvar="bhyvevm_enable"
start_cmd="bhyvevm_start"
stop_cmd="bhyvevm_stop"
# read configuration and set defaults
load_rc_config "$name"
: ${bhyvevm_enable="YES"}
bhyvevm_start()
{
/vm/10.0/start_vm.sh
}
bhyvevm_stop()
{
/vm/10.0/stop_vm.sh vm1
}
run_rc_command "$1"
== put following script in /vm/10.0/start_vm.sh ==
#!/bin/sh
VM=vm1
CONS_A=/dev/nmdm0A
CONS_B=${CONS_A%%A}B
IMG=/vm/10.0/disk.img
TAP=tap0
BRIDGE=bridge0
touch ${CONS_A}
if [ -e /dev/vmm/${VM} ]; then
/usr/sbin/bhyvectl --vm=${VM} --destroy
fi
echo "Starting BHyve virtual machine named '${VM}'. Use 'cu -l ${CONS_B}'
to access console"
cmd="/usr/sbin/bhyveload -m 8G -d ${IMG} -c ${CONS_A} ${VM}"
$cmd
ret=$?
if [ $ret -ne 0 ]; then
echo "[FAILED]: $cmd"
exit $ret
fi
ifconfig bridge0 up
cmd="/usr/sbin/bhyve -c 16 -m 8G -A -H -P -g 0 -s 0:0,hostbridge -s 1:0,lpc
-s 2:0,virtio-net,${TAP} -s 3:0,virtio-blk,${IMG} -l com1,${CONS_A} ${VM}"
$cmd &
ifconfig bridge0 up
sleep 5
echo "~." | cu -l ${CONS_B}
=================================================================
==== put following script in /vm/10.0/stop_vm.sh ===
#!/bin/sh
usage()
{
echo $0 "[vm name]"
}
if [ $# -lt 1 ]; then
usage
exit 1
fi
VM=$1
echo "Stopping BHyve virtual machine named '$VM'"
PID=`pgrep bhyve`
if [ -n "$PID" ]; then
kill $PID
fi
COUNT=0
while [ $COUNT -lt 20 -a -n "$PID" ] ; do
PID2=`pgrep bhyve`
if [ "$PID" != "$PID2" ]; then
break
fi
sleep 5
done
if [ -e /dev/vmm/${VM} ]; then
/usr/sbin/bhyvectl --vm=${VM} --destroy
fi
exit 0
=================================================================
--
Craig
More information about the freebsd-virtualization
mailing list