A single shell script that builds an Alpine Linux sysroot tarball from a JSON
config. Relies only on bash, curl, jq, tar, and unshare — all of
which ship by default (or via a one-line install) on essentially every
mainstream Linux distribution.
./bootstrap.sh <output.tar> <config.json>Example:
./bootstrap.sh ./alpine.tar example.jsonThen inspect:
tar -tvf alpine.tar | head{
"arch": "x86_64",
"repos": [
"https://dl-cdn.alpinelinux.org/alpine/v3.21/main",
"https://dl-cdn.alpinelinux.org/alpine/v3.21/community"
],
"packages": ["bash", "clang", "openrc"]
}- Scrapes Alpine's
latest-stable/main/x86_64/directory listing to find the currentapk-tools-static-*.apkand downloads it. (latest-stableis an Alpine-maintained symlink, so this stays working as Alpine releases.) - Extracts
sbin/apk.staticfrom the apk (an apk is just a tarball). - Lays out a minimal sysroot skeleton in a temp directory.
- Enters an unprivileged user namespace via
unshare --user --map-root-user, then inside the namespace runsapk.static add ...followed bytar --numeric-owner -cf <output> -C <sysroot> ..
unshare --map-root-user only fakes UID 0; from the host's view, every other
UID created inside the namespace (mail, daemon, wheel, …) collapses to
the overflow UID 65534. A directory left on disk would have wrong
ownership. Tarring inside the same namespace records the in-namespace
UIDs/GIDs into the archive, and a later tar -xf as real root reconstitutes
the correct attributes.
- Linux host with unprivileged user namespaces enabled.
bash,curl,jq,tar,unshare(util-linux),coreutils.- Network access to
dl-cdn.alpinelinux.org.
- Host-side
apk-tools-staticis fetched forx86_64only. Thearchfield in the config controls the target sysroot arch (passed toapk.static --arch), but if you build from a non-x86_64 host you'll need to extend the listing URL. - Not bit-for-bit reproducible: both
apk-tools-staticand the package versions resolve against the live Alpine mirror at run time. --allow-untrustedis passed toapk.static addbecause the sysroot starts with no signing keys. Addalpine-keysfirst (or vendor the keys) to drop the flag.