FreeBSD Bhyve Virtualization¶
pkg install -y \
vm-bhyve-devel \
uefi-edk2-bhyve-csm \
bhyve-firmware \
edk2-bhyve \
dnsmasq \
grub2-bhyve \
tigervnc-viewer \
rdesktop
First we need to add several vm_* settings into the main FreeBSD /etc/rc.conf file.
vm_enable=YES vm_dir="zfs:zroot/vm" vm_list="" vm_delay=3
Keep in mind that you will later use the vm_list="" for the list of VMs that you would like to be started at boot. Like vm_list="freebsd13 freebsd14uefi" for example. Then the vm list command would place [1] in at the freebsd13 name (as its first) and [2] in the freebsd14uefi name as this one is second on the list. See below.
host # vm list NAME DATASTORE LOADER CPU MEMORY VNC AUTO STATE almalinux8 default uefi 2 2G 0.0.0.0:5908 No Running (11819) freebsd13 default bhyveload 1 256M - Yes [1] Running (2342) freebsd14 default bhyveload 1 256M - No Stopped freebsd14uefi default uefi 2 8G - Yes [2] Running (35394) windows10 default uefi 2 2G - No Stopped windows7 default uefi 2 2G - No Stopped
We need to create a dedicated ZFS dataset for our VMs. You can also use directory on UFS – check vm-bhyve documentation.
zfs create -o mountpoint=/vm zroot/vm
We will also copy the available templates to our new /vm dir.
cp -a /usr/local/share/examples/vm-bhyve /vm/.templates
Remember to check /vm/.templates/config.sample as it has the documentation for all available options.
head -12 /vm/.templates/config.sample
# This is a sample configuration file containing all supported options # Please do not try and use this file itself for a guest # For any option that contains a number in the name, such as "network0_type", # you can add additional devices of that type by creating a new set of # variables using the next number in sequence, e.g "network1_type" # # Please make sure all option names are specified in lowercase and # at the beginning of the line. If there is any whitespace before # the option name, the line will be ignored. # The '#' character signifies the start of a comment, even within # double-quotes, and so cannot be used inside any values. We can now start initialize the vm-bhyve.
service vm start
Networking
There as many network setups as many FreeBSD has network capabilities – a lot! I this guide I will cover two most typical network setups for Bhyve. One would be the most server (or desktop) oriented – as it requires a LAN card to be used. The other one I would call a laptop one – that one would provide network connectivity using wlan0 WiFi interface.
No matter which one we will choose – we need to enable port forwarding on our FreeBSD host. Do that with these two commands.
sysrc gateway_enable=YES
sysctl net.inet.ip.forwarding=1
echo net.link.tap.up_on_open=1 >> /etc/sysctl.conf
sysctl net.link.tap.up_on_open=1
I assume that our FreeBSD host system would use 10.0.0.10/24 IP address and that 10.0.0.1 would be its default gateway.
Your host system main /etc/rc.conf file can looks as follows then.
cat /etc/rc.conf
# NETWORK hostname=host ifconfig_re0="inet 10.0.0.10/24 up" defaultrouter="10.0.0.1" gateway_enable=YES # DAEMONS sshd_enable=YES zfs_enable=YES # BHYVE vm_enable="YES" vm_dir="zfs:zroot/vm" vm_list="" vm_delay="3"
Server/Desktop LAN Bridge
We will use 10.0.0.0/24 network – the same that our host system uses. We will need one bridge/switch named vm-public with 10.0.0.100/24 address on it. Without that address later the dnsmasq will complain unknown interface vm-public about it. Information about the switches is kept in the /vm/.config/system.conf file. We will also need to add out LAN interface to the public switch. It will be re0 interface in my case.
vm switch create -a 10.0.0.100/24 public
vm switch add public re0
vm switch list NAME TYPE IFACE ADDRESS PRIVATE MTU VLAN PORTS public standard vm-public 10.0.0.100/24 no - - re0
cat /vm/.config/system.conf switch_list="public" type_public="standard" addr_public="10.0.0.100/24" ports_public="re0"
To be honest the networking part setup is complete.
When you will be setting up your Bhyve VMs you will either use static 10.0.0.0/24 IP address space or just use DHCP and the one that is already on your network will take care of the rest (assuming you have one).
Link: