/ website / blog

run freebsd in qemu on linux

November 25, 2023

This blog post serves as a basic guide on how to run a FreeBSD 14 virtual machine in QEMU using a headless Ubuntu 22.04 linux host. The assumption here is both the host and guest operating systems are amd64 (a.k.a. x86_64).

Install QEMU packages

Install the qemu-system-x86 package. Do not install recommended packages as those will include a huge number of graphics packages we are not interested in for a headless server.

sudo apt-get install -y --no-install-recommends qemu-system-x86

Also install the qemu-utils package. This package provides the qemu-img tool which will be necessary for resizing the FreeBSD image.

sudo apt-get install -y qemu-utils

Lastly install the ovmf package. This package provides a UEFI bios capable of booting modern operating systems like the FreeBSD VM images.

sudo apt-get install -y ovmf

Download FreeBSD VM image

FreeBSD offers pre-made images specifically for use in VMs. These images do not need to be installed like a typical .iso image made for booting from CD-ROM or USB stick. The FreeBSD download page can be found here. Look under the VM column for VM images.

The image we want for QEMU is the .qcow2 format image for the amd64 architecture, without the ZFS overlay. For this guide we’ll download FreeBSD-14.0-RELEASE-amd64.qcow2.xz

wget https://download.freebsd.org/releases/VM-IMAGES/14.0-RELEASE/amd64/Latest/FreeBSD-14.0-RELEASE-amd64.qcow2.xz

Now extract the image.

unxz FreeBSD-14.0-RELEASE-amd64.qcow2.xz

Resize the image

Next, resize the image. By default you get just enough disk space for a minimal bootable OS. Once you install a few packages there will be insufficient disk space. The ideal size of the disk depends on your use case. In this example we expand the disk by 16 gigabytes.

qemu-img resize FreeBSD-14.0-RELEASE-amd64.qcow2 +16G

Boot the FreeBSD VM

We can finally boot the VM! QEMU needs a number of non-default options for booting a headless FreeBSD VM.

note: you must still resize the partition inside the VM after this step.

 sudo qemu-system-x86_64 -m 4096 -smp 4 -bios /usr/share/ovmf/OVMF.fd -serial mon:stdio -nographic -drive file=FreeBSD-14.0-RELEASE-amd64.qcow2 -enable-kvm

The -m 4096 option indicates the VM should have 4 gigabytes of system memory.

The -smp 4 option indicates the VM should have 4 CPU cores.

The -bios /usr/share/ovmf/OVMF.fd option indicates the VM should boot with UEFI.

The -serial mon:stdio option indicates the VM should use your pseudo terminal as the serial terminal.

The -nographic option indicates the VM should not have a display.

The -drive <image> option indicates our VM image to boot.

The -enable-kvm option indicates the VM should use Linux’s hardware acceleration.

Resize the OS partition

Once FreeBSD is booted, you can login with username root (there is no password).

Then we must run the growfs command inside the VM to resize the OS partition scheme to fill the expanded image we created.

growfs /

And that’s it! It’s probably a good idea to set a password on root, create service users, etc. The packer tool from HashiCorp can help automate such steps but that’s a blog post for another day.


➡️ related posts in the virtualization series ...