How to install Linux VM on FreeBSD using bhyve and ZFS

bhyve is a hypervisor for FreeBSD that allows you to run virtual machines (VMs) on top of a FreeBSD host. You can use bhyve and ZFS to install a Linux VM on a FreeBSD host. Here are the general steps to do this:

  1. Create a ZFS pool on the host system:
zpool create -f mypool /dev/ada0
  1. Create a ZFS dataset for the Linux VM:
zfs create mypool/vm
  1. Download a Linux ISO image, for example, Ubuntu or Debian, and copy it to the dataset:
fetch -o /mypool/vm/ubuntu.iso http://releases.ubuntu.com/20.04/ubuntu-20.04-live-server-amd64.iso
  1. Create a virtual disk for the Linux VM:
bhyve-firmware -c /mypool/vm/disk0.img -s 8G -t virtio
  1. Create a configuration file for the Linux VM:
echo 'vm:
vcpu: 1
memory: 2048
disks:
- /mypool/vm/disk0.img
cdrom: /mypool/vm/ubuntu.iso
boot: cd
network:
- type: virtio
- dhcp: true
'
> /mypool/vm/config.yml
  1. Use the bhyve command to create and start the Linux VM:
bhyve -c 1 -m 2G -H -P -A -l com1,stdio -s 0:0,hostbridge -s 3,ahci-cd,/mypool/vm/ubuntu.iso -s 4,virtio-blk,/mypool/vm/disk0.img vm
  1. Once the Linux VM is running, you can use the bhyveload command to load the kernel and initrd, then use the bhyve command to boot the Linux VM:
bhyveload -m 2G -d /mypool/vm/ubuntu.iso vm
bhyve -c 1 -m 2G -H -P -A -l com1,stdio -s 0:0,hostbridge -s 3,ahci-cd,/mypool/vm/ubuntu.iso -s 4,virtio-blk,/mypool/vm/disk0.img vm
  1. You should now see the Linux installer starting and you can proceed with the installation as you would on a physical machine.
  2. Once the Linux VM is installed, you can use the bhyve command to start the Linux VM:
bhyve -c 1 -m 2G -H -P -A -l com1,stdio -s 0:0,hostbridge -s 4,virtio-blk,/mypool/vm/disk0.img vm
  1. After the Linux VM is started, you can use the bconsole command to connect to the serial console and access the Linux VM:
bconsole -s /dev/nmdm0B

It is important to note that the above steps are just an example and may require adjustments depending on the specific Linux distribution, version and architecture you want to use. Additionally, bhyve uses a lot of memory and CPU, so make sure your hardware is up to the task.

Leave a Comment