Prepare scripts, patches, and PID 1 before work starts
Before a sandbox starts doing real work, you can prepare it three ways. Scripts bundle reusable commands. Patches modify the rootfs before the VM boots. A custom init system (systemd, OpenRC, s6) can run as PID 1 instead of microsandbox’s minimal agent. All three are defined at creation time and keep the base image untouched.
Scripts are files mounted at /.msb/scripts/ inside the sandbox. The directory is on PATH, so each script is callable by name through exec() or shell().It provides a clean way to bundle setup procedures or entry points with a sandbox without baking them into the image.
Patches modify the rootfs before the VM boots. Write config files, copy directories from the host, create symlinks, append to existing files, remove things you don’t need. The base image stays untouched since patches are written to the writable layer on top.Patches are applied in order and work with OCI images and bind-mounted rootfs. They’re not supported with disk image roots (QCOW2, Raw).
By default, patching a path that already exists in the image will error. Pass replace: true on the operation to allow it. Mkdir and Remove are idempotent and won’t error either way.
use microsandbox::Sandbox;let sb = Sandbox::builder("worker") .image("alpine") .patch(|p| p .text("/etc/greeting.txt", "Hello from a patched rootfs!\n", None, false) .text("/etc/motd", "Custom message of the day.\n", None, true) // replace existing .mkdir("/app", Some(0o755)) .text("/app/config.json", r#"{"debug": true}"#, Some(0o644), false) .copy_file("./cert.pem", "/etc/ssl/cert.pem", None, false) .append("/etc/hosts", "127.0.0.1 myapp.local\n") ) .create() .await?;
The patch builder appends operations in the order you call them; calls are chainable. Available operations across SDKs: text, file, mkdir, append, copyFile / copy_file, copyDir / copy_dir, symlink, remove.For per-language signatures and option shapes, see the SDK references:
By default the microsandbox agent runs as PID 1 inside the guest: small, fast, minimal. For workloads that expect a real init (systemd, OpenRC, s6, runit, etc.), --init hands PID 1 over to the init binary of your choice.Common reasons to opt in: long-lived daemons, system service tests, anything that talks to dbus or expects systemctl to work.Use auto to pick a known init binary from the image, or pass an absolute path when you need to pin the entry point for reproducible CI. When an OCI image declares a known init path as the first ENTRYPOINT token, such as /init in s6-overlay images, auto hands PID 1 to that path. For attached msb run, microsandbox preserves the OCI launch contract by passing the remaining ENTRYPOINT plus CMD or trailing command to that init instead of direct-executing the wrapper through agentd. Otherwise, auto falls back to probing common distro paths: /sbin/init, /lib/systemd/systemd, and /usr/lib/systemd/systemd.
To verify the handoff worked, check /proc/1/comm inside the sandbox:
$ msb run ghcr.io/superradcompany/debian-systemd:12 --init auto -- cat /proc/1/commsystemd
If --init=auto cannot find an init binary from the image ENTRYPOINT or the guest-side probe list, boot fails with a clear error in kernel.log. Use an explicit path when you know where the init lives.
Most slim Docker base images do not include systemd or another full init. Use an image that ships the init you want, or build a small custom image that installs it.--init controls PID 1. --entrypoint and the trailing command normally control the workload you run after boot, so the two can be combined. An image-declared init entrypoint such as /init is the special case. With --init auto and attached msb run, microsandbox passes the trailing command to PID 1 as part of the image’s own launch contract.