OpenBSD on Firecracker: from source to first boot, step by step

Last updated

I still plan to walk through the port step by step in other posts, but since the work is long and I split my time between the day job and a kid, this won't ship in weeks. What I can do now is show the current state and open the door for anyone who wants to help, so this post is the recipe: how to set up the environment, build, and test OpenBSD inside a Firecracker.

Firecracker logo (crystal with an orange flame) next to the OpenBSD blowfish mascot

Heads up. This port is my experimental fork, not official OpenBSD. Don't run it in production. Use it to study the direct boot path through PVH, to play with virtio-mmio in a microVM, or just to see a 1.4 MB kernel come up in ~30 ms.

Prerequisites

You'll need two machines (or one VM and the host):

MachineWhat for
OpenBSD/amd64Build the kernel, create the image with vnd(4) + newfs(8)
Linux/amd64Run firecracker and boot the microVM

Why not build on Linux? OpenBSD's config(8) and the Makefile under sys/arch/amd64/compile/ expect BSD make(1), the canonical tree layout, and tools like vnconfig/newfs -O 2 that Linux doesn't have. It's cheaper and faster to keep an OpenBSD VM as the build host and use Linux only for firecracker.

Tested versions:

Step 1: fetch the source

Two paths.

Path A: my mirror (patch already applied)

Faster. The fork lives on SourceHut and the consolidated patch is already in the history:

# inside the OpenBSD VM, as a regular user:
$ cd /usr/src
$ doas chown $USER /usr/src
$ git clone https://git.sr.ht/~ijanc/openbsd-src sys-fc
$ cd sys-fc

The base commit for the port is 2bf59553dc8f900a2098ab872eb9b79bd1e2659d. The mirror's HEAD may be ahead of that commit as I keep updating it.

Since the patch is already in, skip step 2 and jump to step 3.

Path B: official source + patch

If you'd rather compare with a vanilla tree, grab the source via CVS following the canonical procedure in FAQ 5: Building the system from source. Short version:

$ export CVSROOT=anoncvs@anoncvs.spacehopper.org:/cvs
$ cd /usr
$ doas cvs -q checkout -P src

Or use the official Git mirror (read-only):

$ git clone --depth 1 https://github.com/openbsd/src.git /usr/src

Now grab the patch:

$ ftp https://ijanc.org/downloads/firecracker-pvh.diff
$ cd /usr/src
$ patch -p1 < ~/firecracker-pvh.diff

Sanity check. If patch complains about hunk failed, your tree drifted relative to the snapshot I used as base. The least-painful way out is Path A.

What the patch brings in:

Step 2: write the FIRECRACKER config

The config is 31 useful lines (without comments), deliberately minimal — only the devices Firecracker actually exposes to the guest. Real contents of sys/arch/amd64/conf/FIRECRACKER on my build host:

#	$OpenBSD$
#

machine		amd64
maxusers	4

# Smaller output: -Oz, shorter retpolines, no -fstack-protector.
# option    SMALL_KERNEL # brynet: https://man.openbsd.org/options
option		NO_PROPOLICE

# MP-table tolerance for Firecracker's slightly Linux-flavoured Intel
# MP Specification emission, plus SMP support so multi-vcpu Firecracker
# instances can use all assigned cores.
option		MULTIPROCESSOR
option		MPTABLE_LINUX_BUG_COMPAT

# Filesystems.  FFS + FFS2 cover OpenBSD's native rootfs; UFS_DIRHASH
# is cheap and keeps dir lookups fast on rootfs images with many
# entries (e.g. /usr/bin, /usr/lib).  FIFO is required for pipe(2)
# and named pipes in any remotely usable userland.
option		FFS
option		FFS2
option		UFS_DIRHASH
option		FIFO

# Networking.  INET only; INET6/IPSEC/pf belong in a larger profile.
option		INET

# Root/swap are selected at runtime (PVH cmdline parser patches
# bootdev); `swap generic' still falls back to `root device:' prompt
# if parsing fails.
config		bsd swap generic

# Autoconf topology.
mainbus0	at root
cpu0		at mainbus?
cpu*		at mainbus?
ioapic*		at mainbus?
mpbios0		at mainbus?
isa0		at mainbus0
pvbus0		at mainbus0

pvclock0	at pvbus?

com0		at isa? port 0x3f8 irq 4

# PVH (Xen HVM) boot entry point.
pseudo-device	pvh	1

# Virtio-MMIO transport + drivers.
virtio*		at mainbus?
vioblk*		at virtio?		# block device, presents scsi target
vio*		at virtio?		# network interface (vio0, vio1, ...)
viornd*		at virtio?		# entropy device

scsibus*	at scsi?
sd*		at scsibus?

pseudo-device	loop		1
pseudo-device	bpfilter	1
pseudo-device	pty		2

pseudo-device pvh 1 is what pulls pvh_entry.S and pvh_early.c into the build. The GENERIC kernel doesn't declare it, so nothing changes there.

MPTABLE_LINUX_BUG_COMPAT is required because Firecracker emits an MP Table that's slightly off-spec (Linux-flavoured). Without that option, mpbios0 refuses to attach. More on the spec in MultiProcessor Specification (Wikipedia).

Step 3: build the kernel

All on the OpenBSD VM, as root:

$ cd /usr/src/sys/arch/amd64/conf
$ doas config FIRECRACKER
$ cd ../compile/FIRECRACKER
$ doas make obj
$ doas make config
$ doas make -j$(sysctl -n hw.ncpuonline)

The kernel ends up in obj/bsd (obj is a symlink to /usr/obj/sys/arch/amd64/compile/FIRECRACKER/). Real output from my build host:

$ du -h obj/bsd
1.4M	obj/bsd
$ ls -lh obj/bsd
-rwxrwx---  1 root  wobj   1.3M Apr 22 18:03 obj/bsd
$ file obj/bsd
obj/bsd: ELF 64-bit LSB executable, x86-64, version 1

Section breakdown (so you know where the bytes go):

$ size obj/bsd
text	data	bss	dec	hex
1009695	65408	495616	1570719	17f79f

~1 MB of .text, ~64 KB of .data, ~480 KB of .bss. The .bss doesn't go on disk; what counts for kernel download size is text+data. That's why the ELF fits in 1.3 MB on disk while the kernel takes almost 1.6 MB once loaded.

Rename to bsd.fc and keep it around — we'll copy this over to Linux:

$ cp obj/bsd /tmp/bsd.fc

Verifying the ELF note PVH. If you want to be sure the note is in the right format before trying to boot. Real output:

$ readelf -n /tmp/bsd.fc

Notes at offset 0x000f86a0 with length 0x00000014:
  Owner		Data size	Description
  Xen		0x00000004	Unknown note type: (0x00000012)

$ objdump -s -j .note.Xen /tmp/bsd.fc
Contents of section .note.Xen:
 ffffffff810f86a0 04000000 04000000 12000000 58656e00  ............Xen.
 ffffffff810f86b0 88070d01                             ....

0x12 = 18 = XEN_ELFNOTE_PHYS32_ENTRY. The four bytes 88 07 0d 01 are the physical address of start_pvh in little-endian (0x010d0788). If the note isn't there, Firecracker rejects the kernel without a useful error.

Step 4: build the disk image with vnd(4)

Still on the OpenBSD VM. Firecracker expects a raw image with FFS2 inside; the cleanest path is vnconfig(8) + disklabel(8) + newfs(8).

$ doas su -
# IMG=/tmp/openbsd-fc-rootfs.img
# VND=vnd0
# MNT=/mnt/fc

# 64 MiB is enough for single-user with /bin/ksh, /sbin/init and a
# few base binaries.
# dd if=/dev/zero of=$IMG bs=1m count=64

# vnconfig $VND $IMG
# fdisk -iy $VND
# disklabel -E $VND <<'LABEL'
a a




w
q
LABEL
# newfs -O 2 /dev/rvnd0a
# mkdir -p $MNT
# mount /dev/vnd0a $MNT

The disklabel heredoc is literal. The four blank lines are the default answers for offset, size, fstype and mount point. If you run disklabel -E vnd0 interactively, just hit ENTER four times then w/q.

Now populate the rootfs. Bare minimum for single-user is /sbin/init, /bin/sh, a few /dev nodes and the standard directories:

# install -d $MNT/{bin,sbin,etc,dev,tmp,var,usr/bin,usr/sbin}
# install -m 0555 /sbin/init  $MNT/sbin/init
# install -m 0555 /bin/sh     $MNT/bin/sh
# install -m 0555 /bin/ksh    $MNT/bin/ksh
# install -m 0555 /sbin/halt  $MNT/sbin/halt
# install -m 0555 /sbin/mount $MNT/sbin/mount
# install -m 0555 /bin/ls     $MNT/bin/ls

# mknod $MNT/dev/console c 0 0
# mknod $MNT/dev/null    c 2 2
# mknod $MNT/dev/tty     c 1 0

# cat > $MNT/etc/fstab << 'EOF'
/dev/sd0a / ffs rw 1 1
EOF

# cat > $MNT/etc/rc << 'EOF'
#!/bin/sh
mount -a
echo
echo "OpenBSD/firecracker ready."
exec /bin/ksh -l
EOF
# chmod 0555 $MNT/etc/rc

Unmount and tear down the vnd:

# umount $MNT
# vnconfig -u $VND
# ls -lh $IMG
-rw-r--r--  1 root  wheel   64M Apr 26 12:41 /tmp/openbsd-fc-rootfs.img

Why sd0a if I mounted vnd0a? Inside the microVM the virtio-block device is discovered as vioblk0 → scsibus0 → sd0. The image is the same; only the name changes. The boot_args we pass in the Firecracker JSON is exactly root=sd0a.

Step 5: copy to the Linux host

From Linux, pull both files from the VM:

$ scp openbsd-vm:/tmp/bsd.fc                ./bsd.fc
$ scp openbsd-vm:/tmp/openbsd-fc-rootfs.img ./openbsd-fc-rootfs.img

Or whatever pipeline you prefer: rsync, sneakernet, carrier pigeon.

Step 6: install Firecracker

Each distro has its way. Three common ones:

Arch Linux (AUR):

$ paru -S firecracker
$ firecracker --version
Firecracker v1.15.1

Debian/Ubuntu (official binary):

$ ARCH=$(uname -m)
$ V=v1.15.1
$ curl -L \
    https://github.com/firecracker-microvm/firecracker/releases/download/$V/firecracker-$V-$ARCH.tgz \
    | sudo tar xz -C /usr/local/bin --strip-components=1 release-$V-$ARCH/firecracker-$V-$ARCH
$ sudo mv /usr/local/bin/firecracker-$V-$ARCH /usr/local/bin/firecracker

KVM has to be available. Check with ls -l /dev/kvm. If the file isn't there, enable virtualization in BIOS/UEFI and load the kvm_intel (or kvm_amd) module. Add your user to the kvm group so you don't need sudo every time: sudo usermod -aG kvm $USER and re-login.

More details in the official getting started.

Step 7: the Firecracker JSON

Create firecracker.json in the same directory as bsd.fc:

{
  "boot-source": {
    "kernel_image_path": "./bsd.fc",
    "boot_args": "root=sd0a"
  },
  "drives": [
    {
      "drive_id": "rootfs",
      "path_on_host": "./openbsd-fc-rootfs.img",
      "is_root_device": true,
      "is_read_only": false
    }
  ],
  "machine-config": {
    "vcpu_count": 1,
    "mem_size_mib": 256,
    "smt": false
  }
}

boot_args. My port's pvh_early.c does a simple parse of root= and virtio_mmio.device= from the PVH cmdline. Other tokens are ignored. You won't be able to pass panic=on-error or any of the Linux equivalents.

Why no network-interfaces? Networking goes in the next post. Here the goal is just boot and shell; vio0 requires a tap on the host, nft/pf doing NAT, and hostname.if(5) on the guest. That looks like a separate tutorial.

Step 8: boot

$ firecracker --no-api \
              --log-path /tmp/fc.log \
              --level Error \
              --config-file ./firecracker.json

If everything went right:

Copyright (c) 1982, 1986, 1989, 1991, 1993
        The Regents of the University of California.  All rights reserved.
Copyright (c) 1995-2026 OpenBSD. All rights reserved.  https://www.OpenBSD.org

OpenBSD 7.9-current (FIRECRACKER) #88
real mem = 251658240 (240MB)
avail mem = 248250368 (236MB)
mainbus0 at root
mpbios0 at mainbus0: Intel MP Specification 1.4
cpu0 at mainbus0: apid 0 (boot processor)
cpu0: AMD EPYC, 1696.81 MHz, 17-60-01
ioapic0 at mainbus0: apid 2 pa 0xfec00000, version 11, 24 pins
pvbus0 at mainbus0: KVM
pvclock0 at pvbus0
virtio0 at mainbus0 addr 0xc0001000 irq 5: Virtio Block Device
vioblk0 at virtio0
scsibus0 at vioblk0: 1 targets
sd0 at scsibus0 targ 0 lun 0: <VirtIO, Block Device, >
sd0: 64MB, 512 bytes/sector, 131072 sectors
isa0 at mainbus0
com0 at isa0 port 0x3f8/8 irq 4: ns16550a, 16 byte fifo
com0: console
root on sd0a swap on sd0b dump on sd0b

OpenBSD/firecracker

# uname -a
OpenBSD  7.9 FIRECRACKER#88 amd64

From firecracker fork to the #, around 30 ms. To exit: CTRL-A x (the Firecracker terminal stays in raw mode; halt(8) from inside also works).

If it hangs at mountroot. Almost always either boot_args or the disklabel. Make sure the JSON has "boot_args": "root=sd0a" exactly (no /dev/) and that disklabel -E created an a partition covering the whole disk.

If Firecracker complains about "invalid kernel format". The PVH ELF note wasn't generated. readelf -n bsd.fc should show .note.Xen with type 0x12. If it doesn't, pseudo-device pvh 1 wasn't in the config when you ran config FIRECRACKER.

What's next

This post closes the minimum path: kernel + disk + boot. Two steps still missing for a guest that's actually useful:

  1. Networking. Bringing tap0 up on Linux, vio0 on the guest, NAT via nftables or pf, sshd(8) accepting login. I'll open all of that in the next post. Three virtio-mmio handshake bugs are worth a closer look, so the post will be part tutorial, part Phase 6.
  2. Rootfs with real programs. The 64 MiB disk we just put together has init, ksh, halt and not much else. I'll open a separate repo with scripts to generate ready-made volumes seeded with base79.tgz, httpd(8), sshd(8), plus a parameterized prepare-rootfs.sh. I'll post a heads-up here once the repo is live.

If you followed this far and hit a wall anywhere, ping me through the contact page or open an issue on the mirror. Pretty likely I've already bumped into the same error somewhere along the way.