vm-bhyve on FreeBSD
Figure 1. vm-bhyve on FreeBSD

Install and Configure vm-bhyve

Install vm-bhyve on FreeBSD

$ doas pkg update -f
$ doas pkg install -y -U vm-bhyve uefi-edk2-bhyve tightvnc

Init vm-bhyve

# su -
# sysrc kld_list+=vmm
# kldload vmm
# zfs create -o mountpoint=/vm zroot/vm
# sysrc vm_enable="YES"
# sysrc vm_dir="zfs:zroot/vm"
# ls /vm
# vm init
# ls /vm
# cp /usr/local/share/examples/vm-bhyve/* /vm/.templates/

Setup network for vm-bhyve

Create switch

# vm switch create bhnet
# vm switch list   #--- Watch created switch   (bhnet => vm-bhnet)
# ifconfig   #--- Watch created interface   (vm-bhnet)
# vm switch add bhnet wlan0
# vm switch list   #--- Watch what changed
# ifconfig vm-bhnet   #--- Watch what changed

Assign IP to switch interface

# echo "ifconfig vm-bhnet inet 2.2.2.1/24" >> /etc/rc.local
# ifconfig vm-bhnet inet 2.2.2.1/24   #--- Assign an ip to vm-bhnet
# ifconfig vm-bhnet   #---  Watch what changed

vm-bhnet is treated as all guests' gateway and the gateway ip is 2.2.2.1/24

Enable IP forwarding using pf

# echo "net.inet.ip.forwarding=1" >> /etc/sysctl.conf
# sysctl net.inet.ip.forwarding=1
net.inet.ip.forwarding: 0 -> 1
# sysrc pf_enable="YES"
# echo "nat on wlan0 from {2.2.2.0/24} to any -> (wlan0)" >> /etc/pf.conf
# service pf start

Check your configures

You’d better restart your FreeBSD Operating Sytem and watch if your vm switch and ip forwarding are OK now.

Test Your vm-bhyve by Installing a Guest VM on it

Install FreeBSD on vm-bhyve

# vm iso path/or/url/to/FreeBSD-13.0-RELEASE-amd64-disc1.iso
# vm create -t windows freebsd01
# vm config freebsd01   #--- Change the network switch from public to bhnet
# vm iso   #--- List iso
# vm install freebsd01 FreeBSD-13.0-RELEASE-amd64-disc1.iso
# vm list   #--- Watch the vnc port
# exit
$ vncviewer

Just follow the FreeBSD installer to install FreeBSD.

Setup the FreeBSD Guest OS Network

Setup IP when installing FreeBSD

Set static ip: 2.2.2.2

Set netmask: 255.255.255.0

Set defaultrouter: 2.2.2.1

Set nameserver: 8.8.8.8 8.8.4.4

Setup IP when os installing is finished

# sysrc ifconfig_vtnet0="inet 2.2.2.2/24"
# sysrc defaultrouter="2.2.2.1"
# echo "nameserver 8.8.8.8" >> /etc/resolv.conf
# echo "nameserver 8.8.4.4" >> /etc/resolv.conf
# reboot

FAQ. (Last Questions)

Q: My operating system iso is too large, I don’t want to copy it again by vm iso command.

A: Do not use any "vm iso" command. When starting installing, point the iso file path to an absolute path:

# vm install path/to/your-os.iso

Q: I can’t connet to Internet from guest os.

A: Check and test your configure again and again, especially the nameserver and pf setup.

COPYING

Copyright (c) 2021 Fas Xmut (fasxmut at protonmail dot com)

Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)