A Go binary on a read-only rootfs
The target we wrote our agent for looks like a Buildroot or Yocto image: root filesystem mounted read-only, BusyBox init instead of systemd, a small data partition that is writable, and a few megabytes of tmpfs that vanish at reboot. Almost everything a server developer takes for granted about where a daemon keeps its files, how it restarts and how it gets updated stops being true there.
This post covers a Go binary in that setting: what we changed, one check that gave us the wrong answer, and how a device that cannot rewrite its own binary still gets new versions. On 22 November we shipped agent 0.2.4, which closed the last gap on that last point.
A caveat: our installer's BusyBox path is tested under BusyBox and dash in a test folder, and the armv7 build runs in QEMU. We have no complete Buildroot image in our test matrix, and the [ -w ] surprise below came from a test rig.
Build it static, without cgo
Set CGO_ENABLED=0 and Go links everything into one file. There is no libc to match against the image, no musl-versus-glibc question, and nothing to copy into the rootfs besides the binary. Our release script builds for amd64, arm64, armv7 and armv6, reads the build settings back out of each binary, and fails the release if cgo was on or the architecture is not the one it asked for.
It costs little: no cgo means the pure-Go resolver and no C libraries, and a daemon with one outbound TLS connection and a local SSH server has not missed them.
What the agent still needs from the image is small. The installer wants sh, curl or wget, tar, sha256sum, and OpenSSL 3 to check a signature, or a hash you pass in by hand. The device needs an OpenSSH server, since Dropbear is not supported. No kernel module, no tun device.
State goes where you point it
On a read-only root there may be no /var/lib to write to. Our agent keeps one small state directory, mainly its device credential, and the installer takes --state-dir so you can point it at the data partition. The binary goes where --prefix says, which also has to be writable at install time.
Two details came from running it:
- If the state directory already exists on a read-only mount, the agent starts anyway. It only has to write when the credential rotates.
- The installer refuses a state directory on tmpfs or ramfs unless you pass
--allow-volatile-state. The credential would be gone after a reboot and the device would need a new token. Better to refuse at install time than find a silent device a month later.
Writes go to a temporary file in the same directory, then fsync, then a rename, then an fsync of the directory itself. A rename is atomic only inside one filesystem, which is why the temp file lives next to its destination. After a power cut the old value or the new one is there, and on the next start a complete leftover temp file is promoted rather than thrown away.
[ -w ] told us yes
Credentials rotate. The relay hands over a new one and the old one is spent. If the agent cannot store the new one, the device holds neither, and a person has to visit it. So before it presents a credential, the agent creates a file in its state directory, writes to it, syncs it and removes it. If any step fails it stops and says so, and the old credential is still good. It repeats the test each time it is about to present one, because a mount can turn read-only long after start-up.
The obvious test is [ -w "$dir" ]. On a test rig, BusyBox running as root answered yes for a directory where a real write then failed. The shell check looks at who you are and at permission bits, and root is allowed to write almost anything, so whether the filesystem will take a write never enters into it. Permission bits can mislead in the other direction too: a directory with write permission but no search permission passes a mode check, yet nothing can be created in it. Our tests build exactly that directory and confirm the write test rejects it.
So the installer and the agent both try. In shell:
probe=$(mktemp "$dir/.writable-probe-XXXXXX" 2>/dev/null) || exit 1
rm -f "$probe"
and in Go, simplified:
f, err := os.CreateTemp(dir, ".probe.")
if err != nil {
return err // read-only, full, or not searchable
}
_, err = f.Write([]byte{0})
if err == nil {
err = f.Sync()
}
f.Close()
os.Remove(f.Name())
return err
mktemp also means a planted symlink is never written through, which matters for a root installer. If --prefix fails the test on a read-only root, the installer says so and tells you to pass a path on a writable mount.
Init without systemd
There are no unit files and no Restart=always. With no systemd or OpenRC the installer writes a plain S95rescueshell-agent script into the init directory and a small wrapper that restarts the agent when it exits, with a delay that doubles up to 30 minutes. If the init directory is read-only, the installer keeps a script from an earlier install; if it finds none, it prints the start command for you to add to your image.
The wrapper stops for one exit code only: 2 means the relay rejected the device's credential, and looping would help nobody. Everything else, including the code the agent uses to ask for a restart after an update, brings it back up.
A binary that updates itself, and one that cannot
An agent you cannot update is a liability, so since 0.2.3 it updates itself.
The release carries a checksum list and a signature over it. The public key that checks the signature is compiled into the agent, so the server that hands out files is only a courier. The agent fetches the list and signature, verifies the signature against its baked-in key, checks that the list names one version newer than its own, downloads the tarball for its CPU, and compares the hash with the signed list. The tarball must hold a single rescueshell-agent file. The new binary then has to run --version and report the version it was signed as. Any failure, at any step, leaves the running agent untouched.
The swap itself is two renames of complete files. The running binary is copied to rescueshell-agent.prev first, the receipt goes into the state directory, and then the new file is renamed over the old path. If the power goes at any point there is a runnable binary at the usual path. The agent restarts only when no shell is open.
A new binary that dies three times before it has held a relay connection for 60 seconds is replaced by .prev on its next start, and that version is skipped on that device. The previous binary is also what install.sh --rollback restores by hand.
On control, there are two switches. A team setting turns automatic updates on or off for every device in the team; it is on by default and only an admin or owner can change it. Separately, installing with --no-auto-update locks one device off. The agent reads that lock from its own state directory before it does anything else, so it wins over whatever the server says, and only someone with root on the device can lift it. Checks run about once a day, spread out so a release does not hit every device in the same minute.
Now the read-only part. On a read-only root none of this can work: there is nowhere to put the staged file, the .prev copy or the rename. Before 0.2.4 the Devices page marked such a device outdated and suggested running the install command again, which cannot succeed there either.
In 0.2.4 the agent runs the same write test as above, in the directory its own binary lives in, before it asks the relay anything and before it downloads a byte. If it fails, it logs one line, saying that updates come through the device image, and from then on does one tiny write test an hour: no relay call, no download, no log line. If the directory becomes writable again, it resumes. It tells the relay this as a fact about itself, and the relay shows it as "Updates via your image". The write test never asks the relay, so the server cannot push an update onto a read-only install, and nothing it says can make the agent skip the signature or hash checks.
How you update an image-built device
You build a new image with the newer agent in it and roll it out like any other image. Keep the state directory on the data partition and a reflash of the root filesystem leaves the credential alone, so the device comes back as itself and not as a new one.
One snag we have to be plain about: a read-only device still on 0.2.3 cannot update itself to 0.2.4, and 0.2.3 cannot report that it is read-only, so the Devices page shows it as outdated until the new image lands. A device that can write to its install directory moves to 0.2.4 on its own.
What we would tell another team
- Build static and verify it with
go version -mon the output. - Make the state directory a setting, and test writability with a write at the moment it matters, not once at start-up.
- Write atomically and sync the directory, not only the file.
- Decide early what an update means on a device that cannot rewrite itself. "A new image" is a fine answer, but the software should know that and say it, not retry forever.
If you ship this kind of device, the Buildroot guide covers the install and the changelog has the 0.2.3 and 0.2.9 entries for updates. For why the agent is a shell and not a network, see why our agent is not a VPN.
RescueShell: a shell on any machine you shipped. Try it