#!/bin/bash # # Copyright (c) 2012-2015 # # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU Library General Public License as published # by the Free Software Foundation; either version 2, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # This script is the simplified and portable version of arch-chroot # (https://wiki.archlinux.org/index.php/Change_root#Using_arch-chroot) set -e ################################ IMPORTS ################################## source "$(dirname $0)/../lib/util.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 <