Disposable QEMU guests for cross-platform testing, driven by one POSIX shell script. Built for the case where a project needs to be built and tested on an OS that is not the one on your desk — FreeBSD, initially.
Guests are reproducible: nothing is ever installed by hand. A guest is an official upstream cloud image plus a cloud-init seed, so any guest can be deleted and recreated from its definition.
Everything except the guest images is expected to be on the host already:
qemu-system-x86_64,qemu-img, KVM access (/dev/kvm)genisoimage(builds the cloud-init seed),curl,xz,rsyncpython3(serves the response file and drives installers over the console)- an SSH key at
~/.ssh/vmctl_ed25519:
ssh-keygen -t ed25519 -f ~/.ssh/vmctl_ed25519 -N ''./vmctl bake freebsd-14.4 # one-time, slow: provision and keep the result
./vmctl up freebsd-14.4 # seconds, from the baked image
./vmctl ssh freebsd-14.4./vmctl with no arguments lists every command.
The reason bake exists is that a cold FreeBSD cloud image is expensive to
boot: its firstboot service downloads and applies the full patch set for the
release, then reboots, which takes minutes. Paying that per rebuild would make
the whole loop useless, so it is paid once:
| Tier | File | Cost | Made by |
|---|---|---|---|
| base | <guest>.base.qcow2 |
one download | fetch |
| golden | <guest>.golden.qcow2 |
one slow boot | bake |
| overlay | <guest>.qcow2 |
instant | create |
Guests run on the overlay, so the state you break is never the state you paid
for. rebuild throws the overlay away and makes a new one — a few seconds back
to a clean machine. purge drops everything including the download, for
verifying the whole flow from nothing.
One consequence is worth internalizing: a guest built on the golden image
boots already provisioned, so its seed is never applied again. Editing
cloud-init/ or a guest's PACKAGES therefore does nothing until you
bake again — create warns when it spots this.
Two paths, deliberately:
./vmctl clone freebsd-14.4 https://github.com/you/project # pristine
./vmctl sync freebsd-14.4 ~/source/project # working treeclone gets committed state only, which is exactly right for checking that
the documented build-from-source path works for a real user. sync rsyncs
your working tree including uncommitted changes, which is what you want while
iterating on a port. It excludes build*/ so host build trees — wrong
architecture — never reach the guest, and keeps .git so version stamping
still works.
Guests differ only in how they first come into existence, selected by
PROVISIONER in the guest file:
nocloud(default) — boot a vendor cloud image and configure it from a cloud-init seed. Used by the FreeBSD guests.autoinstall— no cloud image exists, so install once from official media by answering the installer over the serial console (provision/autoinstall.py), then run aPOST_INSTALLscript over SSH. Used by the OpenBSD guest.
Both converge on the same golden image, so everything after bake — create,
up, ssh, sync, rebuild — is identical.
Drop a file in guests/. It is plain shell, sourced by vmctl; see
guests/freebsd-14.4.conf. Give each guest a unique SSH_PORT, and a unique
HTTP_PORT too if it autoinstalls.
Pick the guest login with care. GUEST_USER must not collide with a system
account on the target OS — build, an obvious choice, is taken on OpenBSD
(uid 21, home /var/empty), and its installer silently declines to create a
user that already exists, which surfaces much later as a login that cannot
authenticate. The guests here use builder.
FreeBSD's cloud images are provisioned by nuageinit(8), the
base-system Lua implementation of cloud-init, not the Python one. It reads the
same NoCloud seed but supports a subset, and three details cost real debugging
time:
runcmdentries must be plain strings. Upstream's[sh, -c, "..."]exec form is accepted and silently ignored.- A user created without a password ends up
*LOCKED*, and FreeBSD'ssshdrefuses a locked account even for publickey auth. The seed runspw unlockto undo this. Do not addlocked: true— that is the same lock, and it makes the guest permanently unreachable. PermitRootLogin nois the default andnuageinitdoes not editsshd_config, so there is no root login. Thebuilduser has passwordlesssudo.- The login greeting has two independent sources:
login(1)prints the motd unless~/.hushloginexists, and the skel dotfiles runfortune(6)from both.profileand.login. The seed handles all of them; suppressing only the motd still leaves a tip above every prompt.
Unrelated to cloud-init, one QEMU detail matters: the seed volume must not win
the firmware's boot order, so both drives carry an explicit bootindex.
Without it SeaBIOS reports "No bootable device" to the VGA console, which the
serial log does not capture — the guest simply looks dead.
OpenBSD publishes no cloud image, so the guest is installed from
install79.iso via autoinstall(8). The response file answers
only what must differ from the defaults; anything it omits takes the
installer's default answer.
- The console is the hard part. QEMU's
-nographicsets a fw_cfg flag that moves the firmware and bootloader onto the serial line.-display nonedoes not, and neither does-vga none: with those the guest boots invisibly and the driver waits forever.-nographiccombined with an explicit-serial unix:...gets both the flag and a socket to script against. - The bootloader's
boot>prompt times out in about five seconds, so the reply must be driven by matching output rather than by sleeping. The socket is opened withwait=onso the machine cannot start before the driver has connected. Change the default console to com0matters for every later boot; without it the installed system talks to a screen nobody reads.- There is no working response-file question for a user's ssh key. Such a
line is accepted and ignored, so only root gets a key at install time and
the guest user is set up afterwards by
provision/openbsd-post.sh. - No
sudoin base: escalation isdoas, keyed onwheelmembership. pkg_addrejects an ambiguous name when a package ships flavours, soPACKAGESusesrsync--to select the unflavoured one.