forked from gardenlinux/gardenlinux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgarden-chroot
executable file
·45 lines (39 loc) · 1.38 KB
/
garden-chroot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env bash
# Contains sources from https://github.com/debuerreotype/debuerreotype
set -Eeuo pipefail
thisDir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
source "$thisDir/.constants.sh" \
'<target-dir> <command> [args...]' \
'rootfs apt-get update'
eval "$dgetopt"
while true; do
flag="$1"; shift
dgetopt-case "$flag"
case "$flag" in
--) break ;;
*) eusage "unknown flag '$flag'" ;;
esac
done
targetDir="${1:-}"; shift || eusage 'missing target-dir'
cmd="${1:-}"; shift || eusage 'missing command'
[ -n "$targetDir" ]
epoch="$(< "$targetDir/garden-epoch")"
[ -n "$epoch" ]
export targetDir epoch
unshare -f --mount bash -Eeuo pipefail -c '
[ -n "$targetDir" ] # just to be safe
for dir in dev proc sys; do
if [ -e "$targetDir/$dir" ]; then
mount --rbind "/$dir" "$targetDir/$dir"
fi
done
if [[ ! -e "$targetDir/etc/resolv.conf" && -L "$targetDir/etc/resolv.conf" ]]; then
mv "$targetDir/etc/resolv.conf" "$targetDir/etc/resolv.conf.orig"
touch $targetDir/etc/resolv.conf
fi
cp /etc/resolv.conf "$targetDir/etc/resolv.conf"
exec chroot "$targetDir" /usr/bin/env -i PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" TZ="$TZ" LC_ALL="$LC_ALL" SOURCE_DATE_EPOCH="$epoch" "$@"
' -- "$cmd" "$@"
if [[ -e "$targetDir/etc/resolv.conf.orig" || -L "$targetDir/etc/resolv.conf.orig" ]]; then
mv "$targetDir/etc/resolv.conf.orig" "$targetDir/etc/resolv.conf"
fi