In this guide I show how to install virtual machines using Bhyve on FreeBSD. Byhve is the virtualization technology built into FreeBSD. It allows you to run virtual machines, much like KVM on Linux, to turn your FreeBSD machine into a hypervisor. Easy virtualization is appealing on Linux, and while VirtualBox is available for FreeBSD, I decided to go with the community solution. This requires a command-line setup, but in the future I might explore some GUI management tools.
Note: This guide assumes you are using ZFS, although it’s possible to use UFS with only slight modifications. I recommend ZFS. If you still need to install FreeBSD, check out this guide.
There are several reasons why you might want to employ virtualization. They include:
- Try out new operating systems
- Test software on an operating system without dedicated a machine
- Provide VPS hosting
- Run software specific to an OS or not available on FreeBSD
In this example, I install Fedora with a GUI. In many cases you do not need a GUI. However, a lot of distros use a graphical installer, so I’m going to use a GUI system for this example. You may have to implement when installing a different OS. I tried to write this guide so that it should help you get most major Linux distros running.
Time needed: 20 minutes
Install Virtual Machines using Byhve on FreeBSD
- Enable Hardware Virtualization
Utilize your system’s BIOS to enable virtualization.
- Install Support Software (some optional)
vm-bhyve, bhyve-firmware, tigervnc-viewer, grub2-bhyve
- Modify rc.conf
Enable Bhyve and the necessary kernel module.
- Create ZFS Pools
Set up a your filesystem.
- Download ISO File for OS
Find the ISO file for your desired OS and download it using vm-bhyve.
- Create and Modify Config File
Create a configuration file for your virtual machine.
- Create VM
Use vm-bhyve to create a virtual machine.
- Run VM Installer (GUI OSes)
Install the operating system as you would on a normal computer.
Enable Hardware Virtualization
In most cases, you need to enable hardware virtualization through your BIOS (hit escape when you boot up the computer before anything can load). This will be specific to your hardware vendor or motherboard manufacturer if you built your own computer. Because this step is very system-specific, you’ll have to look this up.
Install Bhyve and Necessary Software
# pkg install vm-bhyve bhyve-firmware tigervnc-viewer grub2-bhyve
Note that grub2-bhyve might not be necessary if any system you plan on booting supports uefi. I don’t actually use it in the steps taken in this guide. bhyve-firmware is necessary for uefi support.
The other stuff: vm-bhyve is a basic manager to allow you to control vm creation, installation, removal, etc. using simple commands. Bhyve itself is built into FreeBSD, this just makes controlling it easier (there are more feature-rich alternatives, such as iohyve or CBSD, but it’s probably best to learn with vm-bhyve). tigervnc-viewer will allow you to interact with a VM that has a GUI. Obviously, if you prefer a different VNC viewer use that.
Add the following lines to /etc/rc.conf:
vm_enable="YES" vm_dir="zfs:zroot/bhyve" kld_list="vmm"
Note: An alternative is to add the following line to /boot/loader.conf:
To the best of my knowledge one is not better than the other, although I read in some places that it might load faster if you use rc.conf.
Create ZFS Pools
Some basic filesystem setup:
# zfs create zroot/bhyve # zfs set recordsize=64K zroot/bhyve # zfs create zroot/bhyve/.templates
Determine Network Interface
Using ifconfig, determine your network interface. Basically, this is your ethernet port or wifi module:
# ifconfig em0: flags=8863<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500 lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384
I just included the first lines for each device. Your output should be longer. Ignore lo0, that’s your loopback device. The device I’m looking for is em0. It may be called something different for you.
Configure VM Networking
These commands use the vm-bhyve tool to help automate our setup:
# vm init # vm switch create public # vm switch add public em0
Download Operating System
I am installing Fedora with this example, but you can also use pretty much any Unix-like system or Windows. Basically, you can install most operating systems other than macOS. Get the ISO file off the distro’s website. Typically it’s the link you get from the “Download” button and the file will end in .iso.
If you choose to use Fedora, as I did, I recommend against the default install. This guide does not cover passing through the GPU so unless you have a screaming fast processor and you dedicate a large portion of it running your VM, the GUI will run very slowly. Try something like XFCE or LXQT that are designed to run on minimal requirements.
# vm iso [URL of ISO file]
Create and Modify Config File
You can create a config file from scratch or use the templates available. If your OS has a template, I suggest you use that. Fedora didn’t have one, but CentOS did. They’re close enough so I figured I’d give it a shot, review the config file, and it worked.
# cp /usr/local/share/examples/vm-bhyve/centos7.conf /zroot/bhyve/.templates/
Here is what the config file looks like. Red lines are ones I modified:
loader="uefi" graphics="yes" xhci_mouse="yes" graphics_res="1280x720" cpu=4 memory=4G network0_type="virtio-net" network0_switch="public" disk0_type="virtio-blk" disk0_name="disk0.img"
Originally, I used 2 CPU cores and 2G of RAM, but Fedora ran pretty slow. This is on a 2012 Intel i7, which is no slouch, but also decently old. I’m pretty sure it’s so slow because 1) Gnome is the most resource-heavy GUI 2) I am currently not passing through my GPU (that may be the topic for another post). The graphics_res will depend on what you want or your monitor. The default is 800×600. There are limited options here (see this sample config to see the options for this and more).
# vm create -t fedora -s 25G fedora
Run VM Installer
# vm install -f fedora [name of ISO]
You can get the name of your ISO by typing the following (assuming the previous steps worked):
# vm iso DATASTORE FILENAME default Fedora-Workstation-Live-x86_64-37-1.7.iso
If you’re not using a GUI, you can enter your VM with the following command:
# vm console fedora
Using the following command, you can see the VNC login:
# vm list
It will be 0.0.0.0:5900. Open up your VNC viewer application, type in that address, and you should now be into your VM. Walk through the installer just as if you are installing the operating system on a computer and you now have a working VM.
Congratulations, you have now installed a VM using Bhyve on FreeBSD.