distro_rootfs.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env bash
  2. # Distribution userlands without docker or root: pulls an image's layers from a registry and runs commands in it
  3. # under proot. Used to build the portable release on an old glibc and to smoke test tarballs locally.
  4. #
  5. # Usage:
  6. # tools/distro_rootfs.sh fetch <image[:tag]> <dir> e.g. debian:12, fedora:44, opensuse/tumbleweed, ghcr.io/...
  7. # tools/distro_rootfs.sh run <dir> [-u] <cmd...> -u runs as uid 1000 instead of fake root
  8. #
  9. # PROOT points at the proot binary (default: proot from PATH, a static build works best), BINDS adds "-b src:dst" pairs.
  10. # Whiteout files of later layers are applied, other OCI features (hardlinks across layers etc.) are not.
  11. set -euo pipefail
  12. PROOT="${PROOT:-proot}"
  13. fetch() {
  14. local image="$1" dir="$2" repo tag token manifest digest registry=registry-1.docker.io
  15. repo="${image%%:*}"
  16. tag="${image#*:}"
  17. [ "$tag" = "$image" ] && tag=latest
  18. if [[ "$repo" == ghcr.io/* ]]; then
  19. registry=ghcr.io
  20. repo="${repo#ghcr.io/}"
  21. token=$(curl -fsSL "https://ghcr.io/token?scope=repository:$repo:pull" | jq -r .token)
  22. else
  23. [[ "$repo" == */* ]] || repo="library/$repo"
  24. token=$(curl -fsSL "https://auth.docker.io/token?service=registry.docker.io&scope=repository:$repo:pull" |
  25. jq -r .token)
  26. fi
  27. local accept="application/vnd.oci.image.index.v1+json,application/vnd.docker.distribution.manifest.list.v2+json"
  28. accept+=",application/vnd.oci.image.manifest.v1+json,application/vnd.docker.distribution.manifest.v2+json"
  29. manifest=$(curl -fsSL -H "Authorization: Bearer $token" -H "Accept: $accept" \
  30. "https://$registry/v2/$repo/manifests/$tag")
  31. if jq -e '.manifests' <<<"$manifest" >/dev/null; then
  32. digest=$(jq -r '[.manifests[] | select(.platform.architecture == "amd64" and .platform.os == "linux")][0].digest' <<<"$manifest")
  33. manifest=$(curl -fsSL -H "Authorization: Bearer $token" -H "Accept: $accept" \
  34. "https://$registry/v2/$repo/manifests/$digest")
  35. fi
  36. mkdir -p "$dir"
  37. local layer
  38. for layer in $(jq -r '.layers[].digest' <<<"$manifest"); do
  39. echo "layer $layer" >&2
  40. curl -fsSL -H "Authorization: Bearer $token" "https://$registry/v2/$repo/blobs/$layer" -o "$dir/.layer"
  41. # whiteouts: .wh.name deletes name from the lower layers, .wh..wh..opq empties the directory
  42. local wh
  43. while IFS= read -r wh; do
  44. local base="${wh##*/}" parent
  45. parent="$(dirname "$wh")"
  46. if [ "$base" = ".wh..wh..opq" ]; then
  47. find "$dir/$parent" -mindepth 1 -delete 2>/dev/null || true
  48. else
  49. rm -rf "${dir:?}/$parent/${base#.wh.}"
  50. fi
  51. done < <(tar -tf "$dir/.layer" 2>/dev/null | grep '\.wh\.' || true)
  52. tar -xf "$dir/.layer" -C "$dir" --exclude='*.wh.*' --no-same-owner 2>/dev/null || true
  53. rm -f "$dir/.layer"
  54. done
  55. chmod -R u+w "$dir"
  56. cp /etc/resolv.conf "$dir/etc/resolv.conf" 2>/dev/null || true
  57. echo "$image" >"$dir/.image"
  58. }
  59. run() {
  60. local dir="$1" user=(-0) home=/root
  61. shift
  62. if [ "${1:-}" = -u ]; then
  63. user=(-i 1000:1000)
  64. home=/tmp
  65. shift
  66. fi
  67. # shellcheck disable=SC2086
  68. exec "$PROOT" "${user[@]}" -r "$dir" -b /dev -b /proc -b /sys -b /etc/resolv.conf ${BINDS:-} -w / \
  69. /usr/bin/env -i HOME="$home" PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
  70. TERM=xterm LANG=C.UTF-8 "$@"
  71. }
  72. case "${1:-}" in
  73. fetch) fetch "$2" "$3" ;;
  74. run) shift; run "$@" ;;
  75. *) sed -n '2,12p' "$0"; exit 1 ;;
  76. esac