Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 3 Nov 2013 03:01:27 GMT
From:      Michael Gmelin <freebsd@grem.de>
To:        freebsd-gnats-submit@FreeBSD.org
Subject:   ports/183604: New port: sysutils/bhyve-rc
Message-ID:  <201311030301.rA331Rnm020932@oldred.freebsd.org>
Resent-Message-ID: <201311030310.rA33A0gs088342@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         183604
>Category:       ports
>Synopsis:       New port: sysutils/bhyve-rc
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-ports-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Sun Nov 03 03:10:00 UTC 2013
>Closed-Date:
>Last-Modified:
>Originator:     Michael Gmelin
>Release:        10.0-BETA2
>Organization:
Grem Equity GmbH
>Environment:
FreeBSD srv15 10.0-BETA2 FreeBSD 10.0-BETA2 #0: Mon Oct 28 20:44:29 UTC 2013     root@:/usr/obj/usr/src/sys/GENERIC  amd64
>Description:
Allows starting of one or multiple bhyve instances within tmux on boot.
See also:
http://lists.freebsd.org/pipermail/freebsd-virtualization/2013-November/001694.html.

pkg-message:

Configuration is done completely though rc.conf.
The rc script won't touch any devices for you (neither disk, nor tap)
so you need to make sure all of those have been initialized properly.

General setup:
kldload vmm
net.link.tap.up_on_open=1

Make it persistent:
echo "net.link.tap.up_on_open=1" >> /etc/sysctl.conf
cat >> /boot/loader.conf << EOF
vmm_load="YES"
EOF

Minimal example:
cat >> /etc/rc.conf << EOF
cloned_interfaces="tap0 bridge0"
bhyve_enable="YES"
bhyve_diskdev="/dev/zvol/tank/bhyve/virt"
EOF

ifconfig tap0 create
ifconfig bridge0 create

service bhyve start
tmux list-sessions
tmux attach -t bhyve
service bhyve status
service bhyve stop

Multi profile configuration example:
cat >> /etc/rc.conf << EOF
cloned_interfaces="tap0 tap1 bridge0"
bhyve_enable="YES"
bhyve_profiles="virt1 virt2"
bhyve_virt1_diskdev="/dev/zvol/tank/bhyve/virt1"

bhyve_virt2_tapdev="tap1"
bhyve_virt2_diskdev="/dev/zvol/tank/bhyve/virt2"
bhyve_virt2_memsize="8192"
bhyve_virt2_ncpu="4"
EOF

ifconfig tap0 create
ifconfig tap1 create
ifconfig bridge0 create

service bhyve start # start all
service bhyve start virt2 # start individual
tmux attach -t bhyve_virt1
tmux attach -t bhyve_virt1
service bhyve stop virt2 # stop individual
service bhyve stop # stop all

(by default ctrl-b d detaches from tmux).

>How-To-Repeat:

>Fix:


Patch attached with submission follows:

# This is a shell archive.  Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file".  Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
#	bhyve-rc
#	bhyve-rc/files
#	bhyve-rc/files/bhyve.in
#	bhyve-rc/pkg-message
#	bhyve-rc/Makefile
#	bhyve-rc/pkg-descr
#
echo c - bhyve-rc
mkdir -p bhyve-rc > /dev/null 2>&1
echo c - bhyve-rc/files
mkdir -p bhyve-rc/files > /dev/null 2>&1
echo x - bhyve-rc/files/bhyve.in
sed 's/^X//' >bhyve-rc/files/bhyve.in << '15204ded0697d56ea8130cce111027fe'
X#!/bin/sh
X#
X# $FreeBSD: mgmelin
X#
X
X# PROVIDE: bhyve
X# REQUIRE: LOGIN
X# KEYWORD: nojail
X#
X
X#
X# Add the following lines to /etc/rc.conf to enable bhyve:
X# bhyve_enable (bool):  Set to "NO" by default.
X#                       Set it to "YES" to enable bhyve
X# bhyve_profiles (str): Set to "" by default.
X#                       Define your profiles here.
X# bhyve_tapdev (str):   Set to "tap0" by default.
X#                       Set to the tap(4) device to use.
X# bhyve_diskdev (str):  Must be set, no default.
X#                       Set to the disk device to use.
X# bhyve_ncpu (int):     Set to 1 by default.
X#                       Set to the number of CPUs for the VM.
X# bhyve_memsize (int):  Set to 512 by default.
X#                       Set to the number of MB of memory for the VM.
X
X. /etc/rc.subr
X
Xname="bhyve"
Xrcvar=bhyve_enable
X
Xstart_precmd="bhyve_prestart"
Xstatus_cmd="bhyve_status"
Xpoll_cmd="bhyve_poll"
Xstop_cmd="bhyve_stop"
X_session=$name
Xcommand="/usr/local/bin/tmux"
Xprocname="-sh"
X
X[ -z "$bhyve_enable" ]  && bhyve_enable="NO"
X[ -z "$bhyve_tapdev" ]  && bhyve_tapdev="tap0"
X[ -z "$bhyve_diskdev" ] && bhyve_diskdev="none"
X[ -z "$bhyve_ncpu" ]    && bhyve_ncpu="1"
X[ -z "$bhyve_memsize" ] && bhyve_memsize="512"
X
Xload_rc_config $name
X
Xif [ -n "$2" ]; then
X	profile="$2"
X	_session="${_session}_${profile}"
X	if [ "x${bhyve_profiles}" != "x" ]; then
X		eval bhyve_enable="\${${_session}_enable:-${bhyve_enable}}"
X		eval bhyve_tapdev="\${${_session}_tapdev:-${bhyve_tapdev}}"
X		eval bhyve_diskdev="\${${_session}_diskdev:-${bhyve_diskdev}}"
X		eval bhyve_ncpu="\${${_session}_ncpu:-${bhyve_ncpu}}"
X		eval bhyve_memsize="\${${_session}_memsize:-${bhyve_memsize}}"
X	else
X		echo "$0: extra argument ignored"
X	fi
Xelse
X	if [ "x${bhyve_profiles}" != "x" -a "x$1" != "x" ]; then
X		for profile in ${bhyve_profiles}; do
X			eval _enable="\${bhyve_${profile}_enable}"
X			case "x${_enable:-${bhyve_enable}}" in
X			x|x[Nn][Oo]|x[Nn][Oo][Nn][Ee])
X				continue
X				;;
X			x[Yy][Ee][Ss])
X				;;
X			*)
X				if test -z "$_enable"; then
X					_var=bhyve_enable
X				else
X					_var=bhyve_"${profile}"_enable
X				fi
X				echo "Bad value" \
X				    "'${_enable:-${bhyve_enable}}'" \
X				    "for ${_var}. " \
X				    "Profile ${profile} skipped."
X				continue
X				;;
X			esac
X			echo "===> bhyve profile: ${profile}"
X			/usr/local/etc/rc.d/bhyve $1 ${profile}
X			retcode="$?"
X			if [ "0${retcode}" -ne 0 ]; then
X				failed="${profile} (${retcode}) ${failed:-}"
X			else
X				success="${profile} ${success:-}"
X			fi
X		done
X		exit 0
X	fi
X	profile=$name
Xfi
X
Xpidfile="/var/run/${_session}.pid"
X
X
Xbhyve_prestart()
X{
X	case ${bhyve_diskdev} in
X	[Nn][Oo][Nn][Ee] | '')
X		echo "No ${_session}_diskdev set. Quitting." 1>&2
X		return 1;
X		;;
X	esac
X	if [ ! -c "${bhyve_diskdev}" -a ! -f "${bhyve_diskdev}" ]; then
X		echo "${bhyve_diskdev} doesn't exist or is not suitable as a diskdev" 1>&2
X		return 1;
X	fi
X}
X
Xbhyve_status()
X{
X	if ${command} has-session -t ${_session} 2>/dev/null; then
X		echo "${_session} is running."
X	else
X		echo "${_session} is not running."
X		return 1
X	fi
X}
X
Xbhyve_poll()
X{
X	echo -n "Waiting for session: ${_session}"
X	while ${command} has-session -t ${_session} 2>dev/null; do
X		sleep 1
X	done
X	echo
X}
X
Xbhyve_stop()
X{
X	if ${command} has-session -t ${_session} 2>/dev/null; then
X		echo "Stopping ${_session}."
X		${command} kill-session -t ${_session}
X		while ${command} has-session -t ${_session} 2>dev/null; do
X			sleep 1
X		done
X	fi
X	rm -f ${pidfile}
X}
X
Xcommand_args="new-session -ds ${_session} \"sh -c 'echo \\\$PPID >${pidfile}; while true; do /usr/sbin/bhyvectl --vm=${_session} --destroy; /usr/sbin/bhyveload -m ${bhyve_memsize} -d ${bhyve_diskdev} ${_session} && /usr/sbin/bhyve -c ${bhyve_ncpu} -m ${bhyve_memsize} -AI -H -P -g 0 -s 0:0,hostbridge -s 1:0,virtio-net,${bhyve_tapdev} -s 2:0,virtio-blk,${bhyve_diskdev} -S 31,uart,stdio ${_session} || break; done'\""
X
Xrun_rc_command "$1"
15204ded0697d56ea8130cce111027fe
echo x - bhyve-rc/pkg-message
sed 's/^X//' >bhyve-rc/pkg-message << '08f317d2efdcbca70cd87748f6d0df00'
XConfiguration is done completely though rc.conf.
XThe rc script won't touch any devices for you (neither disk, nor tap)
Xso you need to make sure all of those have been initialized properly.
X
XGeneral setup:
Xkldload vmm
Xnet.link.tap.up_on_open=1
X
XMake it persistent:
Xecho "net.link.tap.up_on_open=1" >> /etc/sysctl.conf
Xcat >> /boot/loader.conf << EOF
Xvmm_load="YES"
XEOF
X
XMinimal example:
Xcat >> /etc/rc.conf << EOF
Xcloned_interfaces="tap0 bridge0"
Xbhyve_enable="YES"
Xbhyve_diskdev="/dev/zvol/tank/bhyve/virt"
XEOF
X
Xifconfig tap0 create
Xifconfig bridge0 create
X
Xservice bhyve start
Xtmux list-sessions
Xtmux attach -t bhyve
Xservice bhyve status
Xservice bhyve stop
X
XMulti profile configuration example:
Xcat >> /etc/rc.conf << EOF
Xcloned_interfaces="tap0 tap1 bridge0"
Xbhyve_enable="YES"
Xbhyve_profiles="virt1 virt2"
Xbhyve_virt1_diskdev="/dev/zvol/tank/bhyve/virt1"
X
Xbhyve_virt2_tapdev="tap1"
Xbhyve_virt2_diskdev="/dev/zvol/tank/bhyve/virt2"
Xbhyve_virt2_memsize="8192"
Xbhyve_virt2_ncpu="4"
XEOF
X
Xifconfig tap0 create
Xifconfig tap1 create
Xifconfig bridge0 create
X
Xservice bhyve start # start all
Xservice bhyve start virt2 # start individual
Xtmux attach -t bhyve_virt1
Xtmux attach -t bhyve_virt1
Xservice bhyve stop virt2 # stop individual
Xservice bhyve stop # stop all
X
X(by default ctrl-b d detaches from tmux).
08f317d2efdcbca70cd87748f6d0df00
echo x - bhyve-rc/Makefile
sed 's/^X//' >bhyve-rc/Makefile << 'fd453146a6e0f48a938d410ed13f9cb7'
X# Created by: Michael Gmelin <freebsd@grem.de>
X# $FreeBSD$
X
XPORTNAME=	bhyve-rc
XPORTVERSION=	1
XCATEGORIES=	sysutils
XMASTER_SITES=	# none
XDISTFILES=	# none
X
XMAINTAINER=	freebsd@grem.de
XCOMMENT=	FreeBSD RC script for starting bhyve guests in tmux
X
XRUN_DEPENDS=	tmux:${PORTSDIR}/sysutils/tmux
X
XNO_WRKSUBDIR=	yes
XNO_BUILD=	yes
X
XUSE_RC_SUBR=	bhyve
X
X.include <bsd.port.pre.mk>
X
X.if ${OSVERSION} < 1000000
XIGNORE=	bhyve first appeared in FreeBSD 10.0
X.endif
X
Xdo-fetch do-install:
X	@${DO_NADA}
X
X.include <bsd.port.post.mk>
fd453146a6e0f48a938d410ed13f9cb7
echo x - bhyve-rc/pkg-descr
sed 's/^X//' >bhyve-rc/pkg-descr << '123826cc3a27c1c38f22c520ea39dac1'
XFreeBSD RC script to start bhyve guests in tmux
123826cc3a27c1c38f22c520ea39dac1
exit



>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201311030301.rA331Rnm020932>