Setting Up a Virtual Machine with bhyve on FreeBSD
Categories:
6 minute read
FreeBSD’s native hypervisor, bhyve (pronounced “beehive”), provides a robust and efficient way to run virtual machines on FreeBSD systems. This comprehensive guide will walk you through the process of setting up and managing virtual machines using bhyve on FreeBSD.
Introduction to bhyve
bhyve is a type-2 hypervisor that was first introduced in FreeBSD 10.0. It allows you to run various guest operating systems, including FreeBSD, OpenBSD, NetBSD, Linux, and Windows. bhyve is designed to be lightweight and performant, making it an excellent choice for both development environments and production workloads.
Key features of bhyve include:
- Hardware-assisted virtualization
- Support for multiple guest operating systems
- Direct device passthrough capabilities
- Integration with ZFS for storage management
- Low overhead compared to other virtualization solutions
Prerequisites
Before setting up bhyve, ensure your system meets the following requirements:
- A FreeBSD system (version 10.0 or later)
- A CPU with hardware virtualization support (Intel VT-x or AMD-V)
- Sufficient RAM to allocate to your virtual machines
- Adequate disk space for VM images
- Root or sudo access to your FreeBSD system
Step 1: Verifying Hardware Support
First, verify that your CPU supports hardware virtualization:
# For Intel CPUs
sysctl -a | grep vmx
# For AMD CPUs
sysctl -a | grep svm
If either command returns results, your CPU supports virtualization. You should also ensure that virtualization is enabled in your BIOS/UEFI settings.
Step 2: Loading Required Kernel Modules
bhyve requires several kernel modules to function properly. Load them using the following commands:
kldload vmm
kldload nmdm
kldload if_bridge
kldload if_tap
To make these modules load automatically at boot time, add the following lines to /boot/loader.conf
:
vmm_load="YES"
nmdm_load="YES"
if_bridge_load="YES"
if_tap_load="YES"
Step 3: Installing Management Tools
While bhyve can be used directly with command-line tools, several management utilities make it easier to work with. The most popular is vm-bhyve
, which provides a user-friendly interface for creating and managing VMs.
Install vm-bhyve
using pkg:
pkg install vm-bhyve
Step 4: Setting Up the VM Environment
After installing vm-bhyve, you need to create a directory to store your VM files and initialize the environment:
mkdir /usr/local/vm
sysrc vm_enable="YES"
sysrc vm_dir="/usr/local/vm"
vm init
Step 5: Creating a Network Bridge
To provide network connectivity to your VMs, create a network bridge:
sysrc cloned_interfaces="bridge0"
sysrc ifconfig_bridge0="inet 192.168.100.1 netmask 255.255.255.0 description 'VM Network'"
sysrc vm_list=""
sysrc vm_delay="5"
Now configure vm-bhyve to use this bridge:
vm switch create public
vm switch add public bridge0
Step 6: Installing Guest OS Templates
vm-bhyve includes templates for various guest operating systems. Install them using:
vm iso
This will display a list of available ISO images. To download a specific ISO image:
vm iso https://download.freebsd.org/ftp/releases/ISO-IMAGES/13.2/FreeBSD-13.2-RELEASE-amd64-disc1.iso
You can also manually copy ISO images to the /usr/local/vm/.iso
directory.
Step 7: Creating a Virtual Machine
Now you can create a virtual machine. The following example creates a FreeBSD VM with 2GB of RAM and a 20GB disk:
vm create -t freebsd -s 20G freebsd_vm
Available templates include freebsd, netbsd, openbsd, debian, ubuntu, centos, and windows.
Step 8: Configuring the Virtual Machine
After creating the VM, you may want to customize its configuration. The VM configuration file is located at /usr/local/vm/freebsd_vm/freebsd_vm.conf
.
Here’s an example configuration for a FreeBSD VM:
loader="bhyveload"
cpu=2
memory=2G
network0_type="virtio-net"
network0_switch="public"
disk0_type="virtio-blk"
disk0_name="disk0.img"
graphics="yes"
graphics_port="5900"
graphics_res="1024x768"
graphics_wait="no"
You can edit this file to adjust CPU cores, memory, network settings, and other parameters according to your needs.
Step 9: Installing the Guest Operating System
To start the VM installation process:
vm install freebsd_vm FreeBSD-13.2-RELEASE-amd64-disc1.iso
This command will boot the VM from the specified ISO file. You can connect to the VM console to complete the installation:
vm console freebsd_vm
Follow the on-screen instructions to complete the OS installation. Once the installation is complete, exit the console by pressing ~
(tilde) followed by .
(period).
Step 10: Managing Virtual Machines
vm-bhyve provides several commands for managing your virtual machines:
- Start a VM:
vm start freebsd_vm
- Stop a VM:
vm stop freebsd_vm
- Force stop a VM:
vm poweroff freebsd_vm
- Connect to a VM console:
vm console freebsd_vm
- List all VMs:
vm list
- Show VM details:
vm info freebsd_vm
Step 11: Configuring Networking Inside the Guest
Once your VM is running, you’ll need to configure networking inside the guest OS. For FreeBSD guests, add the following to /etc/rc.conf
:
ifconfig_vtnet0="DHCP"
For Linux guests, the network interface will usually be detected automatically and can be configured through the distribution’s network management tools.
Step 12: Setting Up VNC for Graphical Access
For operating systems that require a graphical interface during installation (like Windows), you can use VNC to access the VM’s console:
vm configure freebsd_vm
vm set freebsd_vm graphics="yes"
vm set freebsd_vm graphics_port="5900"
vm set freebsd_vm graphics_res="1024x768"
vm set freebsd_vm graphics_wait="no"
You can then use a VNC client to connect to your FreeBSD host on port 5900.
Advanced Features and Configurations
PCI Passthrough
bhyve supports PCI passthrough, allowing you to give a VM direct access to physical hardware. To use PCI passthrough:
Identify the PCI device you want to pass through:
pciconf -lv
Modify the VM configuration to include the device:
vm set freebsd_vm passthru0="pci0:1:0"
ZFS Integration
bhyve works exceptionally well with ZFS. You can create a dedicated ZFS dataset for your VMs:
zfs create zroot/vm
sysrc vm_dir="/zroot/vm"
vm init
Using ZFS allows you to leverage features like snapshots and clones for your VMs:
# Create a snapshot
zfs snapshot zroot/vm/freebsd_vm@backup
# Restore from a snapshot
zfs rollback zroot/vm/freebsd_vm@backup
# Clone a VM
zfs clone zroot/vm/freebsd_vm@backup zroot/vm/freebsd_vm_clone
Auto-Starting VMs
To automatically start VMs when your FreeBSD system boots:
sysrc vm_list="freebsd_vm another_vm"
The VMs listed in vm_list will start automatically in the order specified.
Troubleshooting Common Issues
VM Fails to Start
If a VM fails to start, check the following:
- Ensure all required kernel modules are loaded:
kldstat | grep vmm
- Verify that you have sufficient memory available for the VM
- Check the VM’s log file:
cat /usr/local/vm/freebsd_vm/console.log
Network Connectivity Issues
If your VM can’t connect to the network:
- Verify that the bridge interface is correctly configured:
ifconfig bridge0
- Check if the tap interface for the VM was created:
ifconfig | grep tap
- Ensure the network configuration inside the guest OS is correct
Performance Optimization
To optimize VM performance:
- Use virtio drivers for disk and network when possible
- Allocate appropriate CPU cores and memory based on workload
- Consider using PCI passthrough for I/O-intensive applications
- Use ZFS with sufficient ARC cache for disk-intensive workloads
Conclusion
bhyve provides a powerful and native virtualization solution for FreeBSD systems. With the tools and techniques covered in this guide, you should be able to create, configure, and manage virtual machines efficiently. As you become more familiar with bhyve, you can explore its advanced features to build complex virtualized environments tailored to your specific needs.
Whether you’re using virtualization for development, testing, or production workloads, bhyve’s performance, stability, and integration with FreeBSD’s core technologies make it an excellent choice for your virtualization needs.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.