Debian Codegraph @

scripts/debuerreotype-chroot

raw
1 #!/usr/bin/env bash
2 set -Eeuo pipefail
3
4 thisDir="$(dirname "$(readlink -vf "$BASH_SOURCE")")"
5 source "$thisDir/.constants.sh" \
6 '<target-dir> <command> [args...]' \
7 'rootfs apt-get update'
8
9 eval "$dgetopt"
10 while true; do
11 flag="$1"; shift
12 dgetopt-case "$flag"
13 case "$flag" in
14 --) break ;;
15 *) eusage "unknown flag '$flag'" ;;
16 esac
17 done
18
19 targetDir="${1:-}"; shift || eusage 'missing target-dir'
20 cmd="${1:-}"; shift || eusage 'missing command'
21 [ -n "$targetDir" ]
22 epoch="$(< "$targetDir/debuerreotype-epoch")"
23 [ -n "$epoch" ]
24
25 export targetDir epoch
26 unshare --mount bash -Eeuo pipefail -c '
27 [ -n "$targetDir" ] # just to be safe
28 for dir in dev proc sys; do
29 if [ -d "$targetDir/$dir" ]; then
30 # --debian-eol woody and below have no /sys
31 mount --rbind "/$dir" "$targetDir/$dir"
32 fi
33 done
34 if [ -f "$targetDir/etc/resolv.conf" ]; then
35 mount --rbind --read-only /etc/resolv.conf "$targetDir/etc/resolv.conf"
36 fi
37 exec chroot "$targetDir" /usr/bin/env -i \
38 PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" \
39 TZ="$TZ" \
40 LC_ALL="$LC_ALL" \
41 ${http_proxy:+http_proxy="$http_proxy"} \
42 ${DEBIAN_FRONTEND:+DEBIAN_FRONTEND="$DEBIAN_FRONTEND"} \
43 SOURCE_DATE_EPOCH="$epoch" \
44 "$@"
45 ' -- "$cmd" "$@"