distro_rootfs.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 the calling user 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. # whether no component of $2 below $1 is a symlink
  14. plain_path() {
  15. local root="$1" path="$2"
  16. while [ "$path" != "$root" ] && [ "$path" != . ] && [ "$path" != / ]; do
  17. [ -L "$path" ] && return 1
  18. path="$(dirname "$path")"
  19. done
  20. return 0
  21. }
  22. fetch() {
  23. local image="$1" dir="$2" repo tag token manifest digest registry=registry-1.docker.io
  24. repo="${image%%:*}"
  25. tag="${image#*:}"
  26. [ "$tag" = "$image" ] && tag=latest
  27. if [[ "$repo" == ghcr.io/* ]]; then
  28. registry=ghcr.io
  29. repo="${repo#ghcr.io/}"
  30. token=$(curl -fsSL "https://ghcr.io/token?scope=repository:$repo:pull" | jq -r .token)
  31. else
  32. [[ "$repo" == */* ]] || repo="library/$repo"
  33. token=$(curl -fsSL "https://auth.docker.io/token?service=registry.docker.io&scope=repository:$repo:pull" |
  34. jq -r .token)
  35. fi
  36. local accept="application/vnd.oci.image.index.v1+json,application/vnd.docker.distribution.manifest.list.v2+json"
  37. accept+=",application/vnd.oci.image.manifest.v1+json,application/vnd.docker.distribution.manifest.v2+json"
  38. manifest=$(curl -fsSL -H "Authorization: Bearer $token" -H "Accept: $accept" \
  39. "https://$registry/v2/$repo/manifests/$tag")
  40. if jq -e '.manifests' <<<"$manifest" >/dev/null; then
  41. digest=$(jq -r '[.manifests[] | select(.platform.architecture == "amd64" and .platform.os == "linux")][0].digest' <<<"$manifest")
  42. manifest=$(curl -fsSL -H "Authorization: Bearer $token" -H "Accept: $accept" \
  43. "https://$registry/v2/$repo/manifests/$digest")
  44. fi
  45. mkdir -p "$dir"
  46. local layer
  47. for layer in $(jq -r '.layers[].digest' <<<"$manifest"); do
  48. echo "layer $layer" >&2
  49. curl -fsSL -H "Authorization: Bearer $token" "https://$registry/v2/$repo/blobs/$layer" -o "$dir/.layer"
  50. if [[ "$layer" == sha256:* ]] && [ "$(sha256sum "$dir/.layer" | cut -d' ' -f1)" != "${layer#sha256:}" ]; then
  51. echo "layer $layer: digest mismatch, the download is corrupt" >&2
  52. exit 1
  53. fi
  54. # whiteouts: .wh.name deletes name from the lower layers, .wh..wh..opq empties the directory
  55. local wh
  56. while IFS= read -r wh; do
  57. wh="${wh#./}"
  58. case "/$wh/" in */../*) continue ;; esac
  59. local base="${wh##*/}" parent target="$dir"
  60. parent="$(dirname "$wh")"
  61. if [ "$parent" != . ]; then target="$dir/$parent"; fi
  62. # a symlink (var/run -> /run) would point rm at the host's files
  63. if ! plain_path "$dir" "$target"; then
  64. echo "skipping whiteout $wh, its directory goes through a symlink" >&2
  65. continue
  66. fi
  67. if [ "$base" = ".wh..wh..opq" ]; then
  68. find "$target" -mindepth 1 ! -path "$dir/.layer" -delete 2>/dev/null || true
  69. else
  70. rm -rf "${target:?}/${base#.wh.}"
  71. fi
  72. done < <(tar -tf "$dir/.layer" 2>/dev/null | grep -E '(^|/)\.wh\.' || true)
  73. # mknod fails without root, hide that noise
  74. tar -xf "$dir/.layer" -C "$dir" --exclude='*.wh.*' --no-same-owner 2>&1 |
  75. { grep -v -e 'Cannot mknod' -e 'Exiting with failure status' || true; } >&2 || true
  76. rm -f "$dir/.layer"
  77. done
  78. chmod -R u+w "$dir"
  79. cp /etc/resolv.conf "$dir/etc/resolv.conf" 2>/dev/null || true
  80. echo "$image" >"$dir/.image"
  81. }
  82. run() {
  83. local dir="$1" user=(-0) home=/root
  84. shift
  85. if [ "${1:-}" = -u ]; then
  86. user=(-i "$(id -u):$(id -g)")
  87. home=/tmp
  88. shift
  89. fi
  90. # shellcheck disable=SC2086
  91. exec "$PROOT" "${user[@]}" -r "$dir" -b /dev -b /proc -b /sys -b /etc/resolv.conf ${BINDS:-} -w / \
  92. /usr/bin/env -i HOME="$home" PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
  93. TERM=xterm LANG=C.UTF-8 "$@"
  94. }
  95. case "${1:-}" in
  96. fetch) fetch "$2" "$3" ;;
  97. run) shift; run "$@" ;;
  98. *) sed -n '2,12p' "$0"; exit 1 ;;
  99. esac