Install a FreeBSD13 Virtual Machine (VM) via bhyve

Install a FreeBSD VM is relatively easier now in a FreeBSD host via the bhyve hypervisor. The following is my simple list of actions to create a new FreeBSD 13 VM. My FreeBSD host (running 13.2-RELEASE-p4) is called n1.

For installation of a Ubuntu22.04 VM via bhyve, please refer to my wiki Install a Ubuntu22.04 VM via bhyve.

necessary VM tools

cc@n1:/ % sudo pkg install vm-bhyve                     // simple vm(8) management
cc@n1:/ % sudo zfs create zroot/bhyve                   // parent data pool for VMs 
cc@n1:/ % sudo zfs create zroot/bhyve/.templates        // used to store VM configs

passthru device

Find the pci device (WiFi 6 wireless interface) on host to pass through the VM. The bus/slot/function is for this devices is 2/0/0.
cc@n1:~ % pciconf -lv | grep -B2 Wi-Fi
ppt0@pci0:2:0:0:        class=0x028000 rev=0x1a hdr=0x00 vendor=0x8086 device=0x2723 subvendor=0x8086 subdevice=0x0084
    vendor     = 'Intel Corporation'
    device     = 'Wi-Fi 6 AX200'

/boot/loader.conf

vmm_load="YES"
nmdm_load="YES"
if_bridge_load="YES"
if_tap_load="YES"
# the passthru pci device
pptdevs="2/0/0"
# default AMD-Vi passthrough support is disabled, enable it
hw.vmm.amdvi.enable="1"

/etc/sysctl.conf

net.link.tap.up_on_open=1

/etc/rc.conf

# needed for virtualization support 
vm_enable="YES"
vm_dir="zfs:zroot/bhyve"

reboot

my FreeBSD VM config file

This file is used as the config file for a new FreeBSD VM.
cc@n1:~ % cat /zroot/bhyve/.templates/fbsd.conf 
loader="bhyveload"

cpu=4 
memory=4G 

# put up to 8 disks on a single ahci controller. This avoids the creation of 
# a new “controller” on a new “PCIe slot” for each drive added to the guest. 
ahci_device_limit="8"

# e1000 works out-of-the-box, but virtio-net performs better. Virtio support 
# is built in on FreeBSD and Linux guests, but Windows guests will need 
# to have virtio drivers manually installed. 

#network0_type="e1000"
network0_type="virtio-net"
network0_switch="public"

# bhyve/nvme storage is considerably faster than bhyve/virtio-blk storage.
#disk0_type="virtio-blk"
disk0_type="nvme"
disk0_name="disk0"
disk0_dev="sparse-zvol"

# passing of host PCI devices to a virtual machine for its exclusive use of them
passthru0="2/0/0"

setup the net bridge and create a new VM

cc@n1:/zroot/bhyve % sudo vm init
cc@n1:/zroot/bhyve % ifconfig | grep RUNNING
re0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384
cc@n1:/zroot/bhyve % sudo vm switch create public
cc@n1:/zroot/bhyve % sudo vm switch add public re0
cc@n1:/zroot/bhyve % sudo vm create -t fbsd -s 100G n1_iwl_vm
cc@n1:/zroot/bhyve % sudo vm install n1_iwl_vm ./FreeBSD-13.2-RELEASE-amd64-disc1.iso
Starting n1_iwl_vm
  * found guest in /zroot/bhyve/n1_iwl_vm
  * booting...

Use the "vm console" command to access the VM and finish the installation process.
cc@n1:~ % sudo vm console n1_iwl_vm
...

snapshot

cc@n1:/zroot/bhyve % sudo vm snapshot n1_iwl_vm@snapshot1              // create a snapshot of n1_iwl_vm
cc@n1:/zroot/bhyve % sudo vm rollback n1_iwl_vm@snapshot1              // restore to the snapshot of n1_iwl_vm

references:
From 0 to Bhyve on FreeBSD 13.1
https://github.com/churchers/vm-bhyve


CategoryHowTo

chengcui/install_FreeBSD13_VM_via_bhyve (last edited 2023-12-20T15:12:33+0000 by chengcui)