About

This post documents how to build a hardenedBSD virtual machine image that can be used with popular virtual machine software. So far I have only tested with virt-manager but a working VM image should theoretically set you up to deploy on a VPS as well.

Background

Overview

The hardenedBSD project does not currently provide official VM images but it is possible to build them yourself from the source tree. We're going to focus on the 16-CURRENT branch since the 15-STABLE branch lacks a commit we need (at least at the time of writing).

Beyond that there is known bug on the 15-STABLE branch that prevents hardenedBSD from booting on Microsoft's Hyper-V hypervisor. This might be a dealbreaker in certain environments (eg GitHub actions), and the 16-CURRENT branch is not known to have this bug.

Phase #1

Overview

Before we start the process of producing virtual machine images we have to buildworld, and buildkernel. We're interested in the artifacts that are produced, so there's no need to run installworld or installkernel here:

git clone https://git.hardenedbsd.org/hardenedbsd/hardenedbsd
mdo -u root mv hardenedbsd /usr/src
cd /usr/src
mdo -u root make -j $(sysctl -n hw.ncpu) buildworld
mdo -u root make -j $(sysctl -n hw.ncpu) buildkernel

Explanation

Phase #2

Overview

This is the interesting part where we build virtual machine images. We're going to choose the 'raw' image format, and we're going to choose the ufs filesystem, alongside some other options that I will explain one by one:

cd release/
export VMSIZE=80g
export VMFORMATS=raw
export VMFSLIST=ufs
export TARGET_ARCH=amd64
export NOPKGBASE=YES
export WITH_VMIMAGES=YES
export NO_ROOT=YES
export WITHOUT_QEMU=YES
make vm-image

Explanation

Conclusion

As long as the last step was successful, an image should be available at /usr/obj/usr/src/amd64.amd64/release/vm.ufs.raw or a similar path. The image will provide a stock installation of hardenedBSD. The image can be modified with the help of tools like mdconfig(8) and mount(8) but that's out of scope for this post. See hardenedbsd-builder for an example of a project that modifies an image in that way.