22.4.Â FreeBSD as a Host with
bhyve

Starting with FreeBSD 10.0-RELEASE, the
bhyveBSD-licensed
hypervisor is part of the base system. This hypervisor supports
a number of guests, including FreeBSD, OpenBSD, and many LinuxÂ®
distributions. Currently, bhyve only
supports a serial console and does not emulate a graphical
console. As a legacy-free hypervisor, it relies on the
virtualization offload features of newer
CPUs, instead of translating instructions and
manually managing memory mappings.

Due to the design of bhyve, it
requires a computer with a newer processor that supports IntelÂ®
Extended Page Tables (EPT) or AMDÂ® Rapid
Virtualization Indexing (RVI), also known as
Nested Page Tables (NPT). In addition, to
host LinuxÂ® guests, or FreeBSD guests with more than one
vCPU, VMX unrestricted
mode support (UG) is also required. Most
newer processors, specifically the IntelÂ®Â Core™
i3/i5/i7 and IntelÂ®Â Xeon™ E3/E5/E7, support these
features. UG support was introduced with
Intel's Westmere micro-architecture. For a complete list of
IntelÂ® processors that support EPT, refer
to http://ark.intel.com/search/advanced?s=t&ExtendedPageTables=true.
RVI is found on the third generation and
later of the AMDÂ Opteron™ (Barcelona) processors. The easiest
way to tell if a processor will support
bhyve is to run
dmesg or look in
/var/run/dmesg.boot for the
POPCNT processor feature flag on the
Features2 line and EPT and
UG on the VT-x
line.

22.4.1.Â Preparing the Host

The first step to creating a virtual machine in
bhyve is configuring the host
system. First, load the bhyve
kernel module:

#kldload vmm

Then, create a tap interface for the
network device in the virtual machine to attach to. In order
for the network device to participate in the network, also
create a bridge interface containing the
tap interface and the physical interface
as members. In this example, the physical interface is
igb0:

FreeBSD comes with an example script for running a virtual
machine in bhyve. The script will
start the virtual machine and run it in a loop, so it will
automatically restart if it crashes. The script takes a
number of options to control the configuration of the machine:
-c controls the number of virtual CPUs,
-m limits the amount of memory available to
the guest, -t defines which
tap device to use, -d
indicates which disk image to use, -i tells
bhyve to boot from the
CD image instead of the disk, and
-I defines which CD image
to use. The last parameter is the name of the virtual
machine, used to track the running machines. This example
starts the virtual machine in installation mode:

The virtual machine will boot and start the installer.
After installing a system in the virtual machine, when the
system asks about dropping in to a shell at the end of the
installation, choose Yes. A small
change needs to be made to make the system start with a serial
console. Edit /etc/ttys and replace the
existing ttyu0 line with:

ttyu0 "/usr/libexec/getty 3wire" xterm on secure

Note:

Beginning with FreeBSDÂ 9.3-RELEASE and
10.1-RELEASE the console is configured
automatically.

Reboot the virtual machine. While rebooting the virtual
machine causes bhyve to exit, the
vmrun.sh script runs
bhyve in a loop and will automatically
restart it. When this happens, choose the reboot option from
the boot loader menu in order to escape the loop. Now the
guest can be started from the virtual disk:

22.4.3.Â Creating a LinuxÂ® Guest

In order to boot operating systems other than FreeBSD, the
sysutils/grub2-bhyve port must be first
installed.

Next, create a file to use as the virtual disk for the
guest machine:

#truncate -s 16Glinux.img

Starting a virtual machine with
bhyve is a two step process. First
a kernel must be loaded, then the guest can be started. The
LinuxÂ® kernel is loaded with
sysutils/grub2-bhyve. Create a
device.map that
grub will use to map the virtual
devices to the files on the host system:

The system will boot and start the installer. After
installing a system in the virtual machine, reboot the virtual
machine. This will cause bhyve to
exit. The instance of the virtual machine needs to be
destroyed before it can be started again:

#bhyvectl --destroy --vm=linuxguest

Now the guest can be started directly from the virtual
disk. Load the kernel:

LinuxÂ® will now boot in the virtual machine and
eventually present you with the login prompt. Login and use
the virtual machine. When you are finished, reboot the
virtual machine to exit bhyve.
Destroy the virtual machine instance:

#bhyvectl --destroy --vm=linuxguest

22.4.4.Â Using ZFS with
bhyve Guests

If ZFS is available on the host
machine, using ZFS volumes
instead of disk image files can provide significant
performance benefits for the guest VMs. A
ZFS volume can be created by:

22.4.5.Â Virtual Machine Consoles

It is advantageous to wrap the
bhyve console in a session
management tool such as sysutils/tmux or
sysutils/screen in order to detach and
reattach to the console. It is also possible to have the
console of bhyve be a null modem
device that can be accessed with cu. To do
this, load the nmdm kernel module and
replace -l com1,stdio with
-l com1,/dev/nmdm0A. The
/dev/nmdm devices are created
automatically as needed, where each is a pair, corresponding
to the two ends of the null modem cable
(/dev/nmdm1A and
/dev/nmdm1B). See nmdm(4) for more
information.