distro_rootfs.sh 4.4 KB

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