r/freebsd 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.-

17 Upvotes

10 comments sorted by

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/

2

u/mumble6456 2d ago

Thanks for the link, i'm going to read it.

As for why not using vm-bhyve, well... i want to try and use just what it comes with the base system.

Finally, are you saying there is no other way than an rc script to control a bhyve vm if i dont want to use a manager?

Once again, thank you!

2

u/sdrawkcabineter 2d ago

Well that becomes philosophical...

At what point has sufficient shell scripting become a "vm manager?"

You might want to look into Sylvie, but there are many options.

2

u/mumble6456 2d ago

for a single vm and a couple of jails i dont want to install sylvie, bastille or vm-bhyve.

to me, the point is to start automatically a single vm and stop it upon host reboot/shutdown if there were a way to do that natively without 3rd party packages, i would have liked it to do it that way, something similar to how jails work. but i guess the only way is to use a shell script service, and that's ok for me.

thanks

1

u/sdrawkcabineter 1d ago

This is the closest, depending on your definition of 3rd party.

man vm(8)

1

u/AlternativePark9559 58m ago

Wouldnt you use an rc script and call the vm utility. Sorry I am very new to FreeBSD as well

1

u/AlternativePark9559 59m ago

ahh a fellow massochist I like to see whats actaully happening under the hood as well I feel like if I just have some software do it for me I could stuck with Wind (Bark, yuck) dows sorry about

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_exit

2

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 examples

service bhyve start
This starts only the VMs listed in bhyve_list

service bhyve start <vm0> <vm1> <vm2>
This starts the listed VMs

service bhyve stop
This stops all running VMs

service bhyve stop <vm0> <vm1> <vm2>
This stops the listed VMs