Virtualization with Bhyve

By Kenneth Bernholm

Bhyve (pronounced "beehive") is the BSD Hypervisor initially written for FreeBSD and later ported to other Unices. The bhyve page on the FreeBSD wiki answers practically all questions you may have, and the FreeBSD Handbook's bhyve section covers all the practical details. This page is merely a quick summary and guide to get going with vm-bhyve managing the Virtual Machines (VMs).

Set up bhyve

Install, enable, and initialize bhyve:

  1. In the BIOS, enable your processor's virtualization capabilities (Intel VT-x or AMD-V).
  2. Install bhyve with pkg install vm-bhyve.
  3. Create a bhyve folder somewhere appropriate in your file system.
  4. Enable bhyve in /etc/rc.conf by adding vmm_enable="YES" and vm_dir="{your bhyve folder}".
  5. Use kldstat to verify that your kernel has vmm.ko loaded. If not, add vmm_load="YES" to /boot/loader.conf.
  6. If you plan on hosting Linux guests, install bhyve grub2 support with pkg install grub2-bhyve.
  7. Initialize bhyve with vm init.

Enable networking

If needed, enable networking for your guests:

  1. Create a public virtual switch with vm switch create public.
  2. Look up the name of you host's network interface with ifconfig.
  3. Add your host's network interface to the switch with vm switch add public {interface}.

Create a guest virtual machine

Create a guest virtual machine (VM) configured for Debian (why not?) named vdeb1 from the Debian template in /usr/local/share/examples/vm-bhyve/:

  1. Copy the debian.conf template to {your bhyve folder}/.templates/.
  2. Create the vdeb1 VM from the Debian template with vm create -t debian vdeb1.

Install the guest operating system

Installing the guest operating system ought to be one procedure, but a couple of small pre- and post procedures are presently required:

  1. Pre-install procedure. When you launch the installation process and connect to your VM, the installer boot screen is unfortunately invisible. You can choose to press Enter and blindly accept whatever default options the boot screen offers but this can be undesirable. It will, however, make the succeeding installer screens visible. A better solution is to enable the terminal multiplexer during the installation process. This makes the boot screen visible:
    1. Install the terminal multiplexer with pkg install tmux.
    2. Add console="tmux" to {your bhyve folder}/.config/system.conf.
  2. Install procedure. Download and install an operating system on vdeb1:
    1. Download a Debian ISO image to {your bhyve folder}/.iso/ with vm iso https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-12.1.0-amd64-netinst.iso.
    2. Boot the ISO image on vdeb1 with vm install vdeb1 debian-12.1.0-amd64-netinst.iso.
    3. Connect to vdeb1 with vm console vdeb1.
    4. Complete the Debian install procedure and reboot vdeb1.
    5. Shut down vdeb1 with vm stop vdeb1.
  3. Post-install procedure. It can be very difficult exiting a VM with the terminal multiplexer enabled. Do not uninstall the tmux package as you will need it next time you install an operating system on a VM, but disable it for now:
    1. Remove console="tmux" from {your bhyve folder}/.config/system.conf.

Someday the pre- and post installation procedures will become unnecessary. Patience is a virtue.

Manage a virtual machine

Type vm help for a list of commands to managing your VMs:

Run X11 applications

To run X11 applications on a VM, you must set up forwarding of the VM X11 client to the host X11 server.

This procedure configures the host and a FreeBSD VM. For other VMs such as Debian, change the paths and commands as needed.

  1. On the host, allow the VM to access the host X11 server with xhost +inet:{VM ip address}.
  2. On the VM, add X11 with pkg install xorg.
  3. Also on the VM in /etc/ssh/sshd_config, add
    X11Forwarding yes
    X11UseLocalhost yes
  4. Reboot the VM.

To launch an X11 application on the VM:

  1. Establish an SSH connection with X11 forwarding to the VM with ssh {VM ip address} -X.
  2. On the VM, start the X11 application and watch it's window open on the host screen.