bhyve freebsd

Load bhyve kernel module.

kldload vmm

Create tap interface for the network device.

ifconfig tap0 create
sysctl net.link.tap.up_on_open=1

Create bridge interface, add tap and physical interface to it.

ifconfig bridge0 create
ifconfig bridge0 addm tap0
ifconfig bridge0 addm re0
ifconfig bridge0 up

Create ZFS datasets.

zfs create -o mountpoint=/bhyve zroot/bhyve
zfs create zroot/bhyve/media
zfs create zroot/bhyve/freebsd

Download FreeBSD image.

cd /bhyve/media
fetch https://download.freebsd.org/releases/amd64/amd64/ISO-IMAGES/15.0/FreeBSD-15.0-RELEASE-amd64-bootonly.iso

Create virtual disk for the guest machine. It's a block device (ZFS volume), not directory.

zfs create -V 16G -o volmode=dev zroot/bhyve/freebsd/guest

Create and boot VM.

bhyve -AHP \
      -c 4 \
      -m 8G \
      -s 0:0,hostbridge \
      -s 1:0,lpc \
      -s 2:0,virtio-net,tap0 \
      -s 3:0,virtio-blk,/dev/zvol/zroot/bhyve/freebsd/guest \
      -s 4:0,ahci-cd,/bhyve/media/FreeBSD-15.0-RELEASE-amd64-bootonly.iso \
      -l bootrom,/usr/local/share/uefi-firmware/BHYVE_UEFI.fd \
      -l com1,stdio \
      freebsd

It's possible to use null modem device.

kldload nmdm

Replace stdio with /dev/nmdm0A.

You can connect with cu(1) (drop connection by pressing RET ~. RET).

cu -l /dev/nmdm0B

You can also connect with screen(4).

screen /dev/nmdm0B 115200

See currently running VMs.

ls -l /dev/vmm/

Use bhyvectl(8) to control instances.

Start previously created VM.

bhyve -AHP \
      -c 4 \
      -m 8G \
      -s 0:0,hostbridge \
      -s 1:0,lpc \
      -s 2:0,virtio-net,tap0 \
      -s 3:0,virtio-blk,/dev/zvol/zroot/bhyve/freebsd/guest \
      -l bootrom,/usr/local/share/uefi-firmware/BHYVE_UEFI.fd \
      -l com1,/dev/nmdm0A \
      freebsd &

There is an example script for running VM in FreeBSD.

sh /usr/share/examples/bhyve/vmrun.sh -h

Use VNC for GUI.

bhyve -AHP \
      -c 4 \
      -m 8G \
      -s 0:0,hostbridge \
      -s 1:0,lpc \
      -s 2:0,virtio-net,tap0 \
      -s 3:0,virtio-blk,/dev/zvol/zroot/bhyve/freebsd/guest \
      -s 4:0,fbuf,tcp=127.0.0.1:5900,w=800,h=600 \
      -l bootrom,/usr/local/share/uefi-firmware/BHYVE_UEFI.fd \
      -l com1,/dev/nmdm0A \
      freebsd &

Connect to 127.0.0.1:5900 with your VNC client.