#!/bin/bash # # This script is the simplified and portable version of arch-chroot # (https://wiki.archlinux.org/index.php/Change_root#Using_arch-chroot) # set -e JUNEST_BASE="$(readlink -f $(dirname $(readlink -f "$0"))/..)" source "${JUNEST_BASE}/lib/utils.sh" ################################ MAIN FUNCTIONS ########################### chroot_add_mount() { mount "$@" && CHROOT_ACTIVE_MOUNTS=("$2" "${CHROOT_ACTIVE_MOUNTS[@]}") } chroot_maybe_add_mount() { local cond=$1; shift if eval "$cond"; then chroot_add_mount "$@" fi } chroot_setup() { CHROOT_ACTIVE_MOUNTS=() [[ $(trap -p EXIT) ]] && die '(BUG): attempting to overwrite existing EXIT trap' trap 'chroot_teardown' EXIT chroot_maybe_add_mount "! mountpoint -q '$1'" "$1" "$1" --bind && chroot_add_mount proc "$1/proc" -t proc -o nosuid,noexec,nodev && chroot_add_mount sys "$1/sys" -t sysfs -o nosuid,noexec,nodev,ro && chroot_add_mount udev "$1/dev" -t devtmpfs -o mode=0755,nosuid && chroot_add_mount devpts "$1/dev/pts" -t devpts -o mode=0620,gid=5,nosuid,noexec && chroot_add_mount shm "$1/dev/shm" -t tmpfs -o mode=1777,nosuid,nodev && chroot_add_mount run "$1/run" -t tmpfs -o nosuid,nodev,mode=0755 && chroot_add_mount tmp "$1/tmp" -t tmpfs -o mode=1777,atime,nodev,nosuid && mkdir -p "$1/$HOME" && chroot_add_mount $HOME "$1/$HOME" --bind mkdir -p "$1/run/lock" } chroot_teardown() { umount "${CHROOT_ACTIVE_MOUNTS[@]}" unset CHROOT_ACTIVE_MOUNTS } usage() { cat <