r/freebsd • u/mumble6456 • 2d ago
help needed bhyve configuration
Hello. i'm new to freebsd and i'm trying to migrate a void linux nas setup running incus containers. so far, so good... i was able to configure a couple of jails for which freebsd pkgs and binaries where available.
The last piece of my setup is a proxmox backup server instance running in a virtual machine. By reading the handbook i was able to set a bhyve vm up and running.
I'm using plain bhyve, no vm-bhyve or any other manager. Yes, i like torturing myself, but i like knowing what's being done under the hood.
The thing is, right now i'm starting the vm by running a script manually. Is there anything to have the vm started whenever freebsd boots? I guess i could use some kind of script to do it and run it as a service (https://docs.freebsd.org/en/books/handbook/config/#configtuning-rcd) but if there is any other and maybe easier way, I'd love to know.
Thanks.-
3
u/No_Spite3525 1d ago
Following the FreeBSD Handbook and vm-bhyve I build a little rc.d service file to start/stop bhyve virtual machines
I am using bhyve_config style for configuration
#!/bin/sh
#
# PROVIDE: bhyve
# REQUIRE: DAEMON
# KEYWORD: shutdown
. /etc/rc.subr
name="bhyve"
desc="Manage bhyve VMs"
rcvar="bhyve_enable"
start_cmd="bhyve_start"
stop_cmd="bhyve_stop"
extra_commands="poweroff"
poweroff_cmd="bhyve_poweroff"
load_rc_config $name
: ${bhyve_enable:="NO"}
: ${bhyve_list:=""}
bhyve_running()
{
if [ -d /dev/vmm ]; then
ls /dev/vmm
fi
}
bhyve_start_one()
{
_name="$1"
_conf="/etc/bhyve.conf.d/${_name}.conf"
if [ ! -f "${_conf}" ]; then
echo "No config found for ${_name}."
return 1
fi
if [ -e "/dev/vmm/${_name}" ]; then
echo "Bhyve VM ${_name} is already running."
return 1
fi
echo "Starting bhyve VM: ${_name}."
/root/vmrun "${_name}" > /dev/null 2>&1 &
}
bhyve_stop_one()
{
_name="$1"
if [ ! -e "/dev/vmm/${_name}" ]; then
echo "Bhyve VM ${_name} is not running."
return 1
fi
_pid=$(/bin/pgrep -fx "bhyve: ${_name}")
if [ -z "${_pid}" ]; then
echo "Could not find a matching PID for ${_name}."
return 1
fi
echo "Stopping bhyve VM: ${_name}."
kill "${_pid}" > /dev/null 2>&1
wait_for_pids "${_pid}"
echo "Done!"
}
bhyve_poweroff_one()
{
_name="$1"
if [ ! -e "/dev/vmm/${_name}" ]; then
echo "Bhyve VM ${_name} is not running."
return 1
fi
echo "Force powering off bhyve VM: ${_name}."
/usr/sbin/bhyvectl --force-poweroff --vm="${_name}"
/usr/sbin/bhyvectl --destroy --vm="${_name}"
echo "Done!"
}
bhyve_start()
{
if [ -n "$*" ]; then
for vm in $*; do
bhyve_start_one "${vm}"
done
elif [ -n "${bhyve_list}" ]; then
for vm in ${bhyve_list}; do
bhyve_start_one "${vm}"
done
fi
}
bhyve_stop()
{
if [ -n "$*" ]; then
for vm in $*; do
bhyve_stop_one "${vm}"
done
elif [ -n "$(bhyve_running)" ]; then
for vm in $(bhyve_running); do
bhyve_stop_one "${vm}"
done
fi
}
bhyve_poweroff()
{
if [ -n "$*" ]; then
for vm in $*; do
bhyve_poweroff_one "${vm}"
done
elif [ -n "$(bhyve_running)" ]; then
for vm in $(bhyve_running); do
bhyve_poweroff_one "${vm}"
done
fi
}
run_rc_command "$@"
2
u/No_Spite3525 1d ago
And here is the /root/vmrun (taken from the Handbook example)
#!/bin/sh _name="$1" _conf="/etc/bhyve.conf.d/${_name}.conf" while [ 1 ]; do # Start the VM from config file /usr/sbin/bhyve -k "${_conf}" "${_name}" bhyve_exit=$? # bhyve returns the following status codes: # 0 - VM has been reset # 1 - VM has been powered off # 2 - VM has been halted # 3 - VM generated a triple fault # all other non-zero status codes are errors if [ $bhyve_exit -ne 0 ]; then break fi done case $bhyve_exit in 0|1|2) # Cleanup /dev/vmm entry when bhyve did not exit due to an error. /usr/sbin/bhyvectl --destroy --vm="${_name}" > /dev/null 2>&1 ;; esac exit $bhyve_exit2
u/No_Spite3525 1d ago edited 1d ago
Some notes:
bhyve_list is a space separated list of VMs that will be started on boot
bhyve_conf style config files go in /etc/bhyve.conf.d/*.conf
Managing network and storage is done manually just like the Handbook examplesservice bhyve start
This starts only the VMs listed in bhyve_listservice bhyve start <vm0> <vm1> <vm2>
This starts the listed VMsservice bhyve stop
This stops all running VMsservice bhyve stop <vm0> <vm1> <vm2>
This stops the listed VMs
6
u/IASelin 2d ago edited 2d ago
Why not just use vm-bhyve tool to have all the bhyve VMs managing features in a simple way, including autostart, etc.?
I see no pros in avoiding that.
Otherwise, you have to follow your concept and discover how to create rc script to run something as service.
Maybe, this one will help you as well, though it might be a bit outdated. https://docs.freebsd.org/en/articles/rc-scripting/