FreeBSD Hypervisor: bhyve
FreeBSD Hypervisor: bhyve
Introduction
The bhyve BSD-licensed hypervisor became part of the base system with FreeBSD 10.0-RELEASE. This hypervisor supports a number of guests, including FreeBSD, OpenBSD, and many Linux® distributions.
Preparing the Host
The first step to creating a virtual machine in bhyve is configuring the host system. First, load the bhyve kernel module:
1 | 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:
1 | ifconfig tap0 create |
Creating a FreeBSD Guest
Download an installation image of FreeBSD to install:
1 | pkg install -y aria2 lzma vm-bhyve bhyve-rc |
Create a file to use as the virtual disk for the guest machine. Specify the size and name of the virtual disk:
1 | truncate -s 16G guest.img |
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
This example starts the virtual machine in installation mode:
1 | sh /usr/share/examples/bhyve/vmrun.sh -c 2 -m 1024M -t tap0 -d guest.img -i -I FreeBSD-13.1-RELEASE-amd64-disc1.iso guestname |
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.
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:
1 | sh /usr/share/examples/bhyve/vmrun.sh -c 2 -m 1024M -t tap0 -d guest.img guestname |
Managing Virtual Machines
A device node is created in /dev/vmm for each virtual machine. This allows the administrator to easily see a list of the running virtual machines:
1 | ls -al /dev/vmm |
A specified virtual machine can be destroyed using bhyvectl
:
1 | bhyvectl --destroy --vm=guestname |
Persistent Configuration
In order to configure the system to start bhyve guests at boot time, the following configurations must be made in the specified files:
/etc/sysctl.conf
1
net.link.tap.up_on_open=1
/etc/rc.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28hostname="freebsd-amd64-01"
ifconfig_DEFAULT="DHCP"
ipv6_enable="YES"
ipv6_activate_all_interfaces="YES"
sshd_enable="YES"
ntpdate_enable="YES"
sendmail_enable="NONE"
sendmail_submit_enable="NO"
sendmail_outbound_enable="NO"
sendmail_msp_queue_enable="NO"
growfs_enable="YES"
cloned_interfaces="tap0 tap1 bridge0"
ifconfig_bridge0="addm ue0 addm tap0 addm tap1"
kld_list="nmdm vmm"
bhyve_enable="YES"
bhyve_profiles="virt1 virt2"
bhyve_virt1_diskdev="/dev/zvol/tank/bhyve/virt1"
bhyve_virt2_tapdev="tap1"
bhyve_virt2_diskdev="/dev/zvol/tank/bhyve/virt2"
bhyve_virt2_memsize="8192"
bhyve_virt2_ncpu="4"
vm_enable="YES"
vm_dir="zfs:pool/bhyve/virt"
vm_dir="/var/bhyve/virt"