libvirt: Bhyve driver
Description: libvirt, virtualization, virtualization API

# Bhyve driver

- Connections to the Bhyve driver
- Example guest domain XML configurations
- Guest usage / management
  - Connecting to a guest console
  - Converting from domain XML to Bhyve args
  - Using ZFS volumes
  - Using grub2-bhyve or Alternative Bootloaders

Bhyve is a FreeBSD hypervisor. It first appeared in FreeBSD 10.0. However, it's recommended to keep tracking FreeBSD 10-STABLE to make sure all new features of bhyve are supported. In order to enable bhyve on your FreeBSD host, you'll need to load the `vmm` kernel module. Additionally, `if_tap` and `if_bridge` modules should be loaded for networking support.

Additional information on bhyve could be obtained on bhyve.org.

## Connections to the Bhyve driver

The libvirt bhyve driver is a single-instance privileged driver. Some sample connection URIs are:

    bhyve:///system                     (local access)
    bhyve+unix:///system                (local access)
    bhyve+ssh://root@example.com/system (remote access, SSH tunnelled)

## Example guest domain XML configurations

### Example config

The bhyve driver in libvirt is in its early stage and under active development. So it supports only limited number of features bhyve provides.

Note: in older libvirt versions, only a single network device and a single disk device were supported per-domain. However, since 1.2.6 the libvirt bhyve driver supports up to 31 PCI devices.

Note: the Bhyve driver in libvirt will boot whichever device is first. If you want to install from CD, put the CD device first. If not, put the root HDD first.

Note: Only the SATA bus is supported. Only `cdrom`- and `disk`-type disks are supported.

    <domain type='bhyve'>
        <name>bhyve</name>
        <uuid>df3be7e7-a104-11e3-aeb0-50e5492bd3dc</uuid>
        <memory>219136</memory>
        <currentMemory>219136</currentMemory>
        <vcpu>1</vcpu>
        <os>
           <type>hvm</type>
        </os>
        <features>
          <apic/>
          <acpi/>
        </features>
        <clock offset='utc'/>
        <on_poweroff>destroy</on_poweroff>
        <on_reboot>restart</on_reboot>
        <on_crash>destroy</on_crash>
        <devices>
          <disk type='file'>
            <driver name='file' type='raw'/>
            <source file='/path/to/bhyve_freebsd.img'/>
            <target dev='hda' bus='sata'/>
          </disk>
          <disk type='file' device='cdrom'>
            <driver name='file' type='raw'/>
            <source file='/path/to/cdrom.iso'/>
            <target dev='hdc' bus='sata'/>
            <readonly/>
          </disk>
          <interface type='bridge'>
            <model type='virtio'/>
            <source bridge="virbr0"/>
          </interface>
        </devices>
    </domain>

(The <disk> sections may be swapped in order to install from cdrom.iso.)

### Example config (Linux guest)

Note the addition of <bootloader>.

    <domain type='bhyve'>
        <name>linux_guest</name>
        <uuid>df3be7e7-a104-11e3-aeb0-50e5492bd3dc</uuid>
        <memory>131072</memory>
        <currentMemory>131072</currentMemory>
        <vcpu>1</vcpu>
        <bootloader>/usr/local/sbin/grub-bhyve</bootloader>
        <os>
           <type>hvm</type>
        </os>
        <features>
          <apic/>
          <acpi/>
        </features>
        <clock offset='utc'/>
        <on_poweroff>destroy</on_poweroff>
        <on_reboot>restart</on_reboot>
        <on_crash>destroy</on_crash>
        <devices>
          <disk type='file' device='disk'>
            <driver name='file' type='raw'/>
            <source file='/path/to/guest_hdd.img'/>
            <target dev='hda' bus='sata'/>
          </disk>
          <disk type='file' device='cdrom'>
            <driver name='file' type='raw'/>
            <source file='/path/to/cdrom.iso'/>
            <target dev='hdc' bus='sata'/>
            <readonly/>
          </disk>
          <interface type='bridge'>
            <model type='virtio'/>
            <source bridge="virbr0"/>
          </interface>
        </devices>
    </domain>

## Guest usage / management

### Connecting to a guest console

Guest console connection is supported through the `nmdm` device. It could be enabled by adding the following to the domain XML (Since 1.2.4):

      ...
      <devices>
        <serial type="nmdm">
          <source master="/dev/nmdm0A" slave="/dev/nmdm0B"/>
        </serial>
      </devices>
      ...

Make sure to load the `nmdm` kernel module if you plan to use that.

Then `virsh console` command can be used to connect to the text console of a guest.

NB: Some versions of bhyve have a bug that prevents guests from booting until the console is opened by a client. This bug was fixed in FreeBSD r262884. If an older version is used, one either has to open a console manually with `virsh console` to let a guest boot or start a guest using:

    start --console domname

NB: An bootloader configured to require user interaction will prevent the domain from starting (and thus `virsh console` or `start --console` from functioning) until the user interacts with it manually on the VM host. Because users typically do not have access to the VM host, interactive bootloaders are unsupported by libvirt. However, if you happen to run into this scenario and also happen to have access to the Bhyve host machine, you may select a boot option and allow the domain to finish starting by using an alternative terminal client on the VM host to connect to the domain-configured null modem device. One example (assuming `/dev/nmdm0B` is configured as the slave end of the domain serial device) is:

    cu -l /dev/nmdm0B

### Converting from domain XML to Bhyve args

The `virsh domxml-to-native` command can preview the actual `bhyve` commands that will be executed for a given domain. It outputs two lines, the first line is a `bhyveload` command and the second is a `bhyve` command.

Please note that the `virsh domxml-to-native` doesn't do any real actions other than printing the command, for example, it doesn't try to find a proper TAP interface and create it, like what is done when starting a domain; and always returns `tap0` for the network interface. So if you're going to run these commands manually, most likely you might want to tweak them.

    # virsh -c "bhyve:///system"  domxml-to-native --format bhyve-argv --xml /path/to/bhyve.xml
    /usr/sbin/bhyveload -m 214 -d /home/user/vm1.img vm1
    /usr/sbin/bhyve -c 2 -m 214 -A -I -H -P -s 0:0,hostbridge -s 3:0,virtio-net,tap0,mac=52:54:00:5d:74:e3 -s 2:0,virtio-blk,/home/user/vm1.img -s 1,lpc -l com1,/dev/nmdm0A vm1

### Using ZFS volumes

It's possible to use ZFS volumes as disk devices since 1.2.8. An example of domain XML device entry for that will look like:

      ...
      <disk type='volume' device='disk'>
        <source pool='zfspool' volume='vol1'/>
        <target dev='vdb' bus='virtio'/>
      </disk>
      ...

Please refer to the Storage documentation for more details on storage management.

### Using grub2-bhyve or Alternative Bootloaders

It's possible to boot non-FreeBSD guests by specifying an explicit bootloader, e.g. `grub-bhyve(1)`. Arguments to the bootloader may be specified as well. If the bootloader is `grub-bhyve` and arguments are omitted, libvirt will try and infer boot ordering from user-supplied <boot order='N'> configuration in the domain. Failing that, it will boot the first disk in the domain (either `cdrom`- or `disk`-type devices). If the disk type is `disk`, it will attempt to boot from the first partition in the disk image.

      ...
        <bootloader>/usr/local/sbin/grub-bhyve</bootloader>
        <bootloader_args>...</bootloader_args>
      ...

Caveat: `bootloader_args` does not support any quoting. Filenames, etc, must not have spaces or they will be tokenized incorrectly.