| 1 |
#!/hint/bash |
| 2 |
# SPDX-License-Identifier: GPL-2.0-only |
| 3 |
# shellcheck disable=SC2059 # $1 and $2 can contain the printf modifiers |
| 4 |
|
| 5 |
out() { printf "$1 $2\n" "${@:3}"; } |
| 6 |
error() { out "==> ERROR:" "$@"; } >&2 |
| 7 |
warning() { out "==> WARNING:" "$@"; } >&2 |
| 8 |
msg() { out "==>" "$@"; } |
| 9 |
die() { error "$@"; exit 1; } |
| 10 |
|
| 11 |
ignore_error() { |
| 12 |
"$@" 2>/dev/null |
| 13 |
return 0 |
| 14 |
} |
| 15 |
|
| 16 |
chroot_add_mount() { |
| 17 |
mount "$@" && CHROOT_ACTIVE_MOUNTS=("$2" "${CHROOT_ACTIVE_MOUNTS[@]}") |
| 18 |
} |
| 19 |
|
| 20 |
chroot_maybe_add_mount() { |
| 21 |
local cond=$1; shift |
| 22 |
if eval "$cond"; then |
| 23 |
chroot_add_mount "$@" |
| 24 |
fi |
| 25 |
} |
| 26 |
|
| 27 |
chroot_init() { |
| 28 |
CHROOT_ACTIVE_MOUNTS=() |
| 29 |
CHROOT_ACTIVE_LAZY=() |
| 30 |
CHROOT_ACTIVE_FILES=() |
| 31 |
[[ $(trap -p EXIT) ]] && die '(BUG): attempting to overwrite existing EXIT trap' |
| 32 |
trap 'chroot_teardown' EXIT |
| 33 |
} |
| 34 |
|
| 35 |
chroot_teardown() { |
| 36 |
if (( ${#CHROOT_ACTIVE_MOUNTS[@]} )); then |
| 37 |
umount "${CHROOT_ACTIVE_MOUNTS[@]}" |
| 38 |
fi |
| 39 |
unset CHROOT_ACTIVE_MOUNTS |
| 40 |
|
| 41 |
if (( ${#CHROOT_ACTIVE_LAZY[@]} )); then |
| 42 |
umount --lazy "${CHROOT_ACTIVE_LAZY[@]}" |
| 43 |
fi |
| 44 |
unset CHROOT_ACTIVE_LAZY |
| 45 |
|
| 46 |
if (( ${#CHROOT_ACTIVE_FILES[@]} )); then |
| 47 |
rm "${CHROOT_ACTIVE_FILES[@]}" |
| 48 |
fi |
| 49 |
unset CHROOT_ACTIVE_FILES |
| 50 |
} |
| 51 |
|
| 52 |
chroot_setup() { |
| 53 |
chroot_add_mount proc "$1/proc" -t proc -o nosuid,noexec,nodev && |
| 54 |
chroot_add_mount sys "$1/sys" -t sysfs -o nosuid,noexec,nodev,ro && |
| 55 |
ignore_error chroot_maybe_add_mount "[[ -d '$1/sys/firmware/efi/efivars' ]]" \ |
| 56 |
efivarfs "$1/sys/firmware/efi/efivars" -t efivarfs -o nosuid,noexec,nodev && |
| 57 |
chroot_add_mount udev "$1/dev" -t devtmpfs -o mode=0755,nosuid && |
| 58 |
chroot_add_mount devpts "$1/dev/pts" -t devpts -o mode=0620,gid=5,nosuid,noexec && |
| 59 |
chroot_add_mount shm "$1/dev/shm" -t tmpfs -o mode=1777,nosuid,nodev && |
| 60 |
chroot_add_mount /run "$1/run" --bind --make-private && |
| 61 |
chroot_add_mount tmp "$1/tmp" -t tmpfs -o mode=1777,strictatime,nodev,nosuid |
| 62 |
} |
| 63 |
|
| 64 |
chroot_add_mount_lazy() { |
| 65 |
mount "$@" && CHROOT_ACTIVE_LAZY=("$2" "${CHROOT_ACTIVE_LAZY[@]}") |
| 66 |
} |
| 67 |
|
| 68 |
chroot_bind_device() { |
| 69 |
touch "$2" && CHROOT_ACTIVE_FILES=("$2" "${CHROOT_ACTIVE_FILES[@]}") |
| 70 |
chroot_add_mount "$1" "$2" --bind |
| 71 |
} |
| 72 |
|
| 73 |
chroot_add_link() { |
| 74 |
ln -sf "$1" "$2" && CHROOT_ACTIVE_FILES=("$2" "${CHROOT_ACTIVE_FILES[@]}") |
| 75 |
} |
| 76 |
|
| 77 |
unshare_setup() { |
| 78 |
chroot_add_mount_lazy "$1" "$1" --bind && |
| 79 |
chroot_add_mount proc "$1/proc" -t proc -o nosuid,noexec,nodev && |
| 80 |
chroot_add_mount_lazy /sys "$1/sys" --rbind && |
| 81 |
chroot_add_link /proc/self/fd "$1/dev/fd" && |
| 82 |
chroot_add_link /proc/self/fd/0 "$1/dev/stdin" && |
| 83 |
chroot_add_link /proc/self/fd/1 "$1/dev/stdout" && |
| 84 |
chroot_add_link /proc/self/fd/2 "$1/dev/stderr" && |
| 85 |
chroot_bind_device /dev/full "$1/dev/full" && |
| 86 |
chroot_bind_device /dev/null "$1/dev/null" && |
| 87 |
chroot_bind_device /dev/random "$1/dev/random" && |
| 88 |
chroot_bind_device /dev/tty "$1/dev/tty" && |
| 89 |
chroot_bind_device /dev/urandom "$1/dev/urandom" && |
| 90 |
chroot_bind_device /dev/zero "$1/dev/zero" && |
| 91 |
chroot_add_mount run "$1/run" -t tmpfs -o nosuid,nodev,mode=0755 && |
| 92 |
chroot_add_mount tmp "$1/tmp" -t tmpfs -o mode=1777,strictatime,nodev,nosuid |
| 93 |
} |
| 94 |
|
| 95 |
pid_unshare="unshare --fork --pid" |
| 96 |
mount_unshare="$pid_unshare --mount --map-auto --map-root-user --setuid 0 --setgid 0" |
| 97 |
|
| 98 |
# This outputs code for declaring all variables to stdout. For example, if |
| 99 |
# FOO=BAR, then running |
| 100 |
# declare -p FOO |
| 101 |
# will result in the output |
| 102 |
# declare -- FOO="bar" |
| 103 |
# This function may be used to re-declare all currently used variables and |
| 104 |
# functions in a new shell. |
| 105 |
declare_all() { |
| 106 |
# Remove read-only variables to avoid warnings. Unfortunately, declare +r -p |
| 107 |
# doesn't work like it looks like it should (declaring only read-write |
| 108 |
# variables). However, declare -rp will print out read-only variables, which |
| 109 |
# we can then use to remove those definitions. |
| 110 |
declare -p | grep -Fvf <(declare -rp) |
| 111 |
# Then declare functions |
| 112 |
declare -pf |
| 113 |
} |