2 # Filename: grml-chroot
3 # Purpose: Program to chroot into another system
4 # Authors: grml-team (grml.org), (c) Michael Gebetsroither <gebi@grml.org>
5 # Bug-Reports: see http://grml.org/bugs/
6 # License: This file is licensed under the GPL v2.
7 ################################################################################
9 PROG_NAME_=$(basename $0)
11 MOUNTED_="" # all mounted destinations
23 Usage: "$PROG_NAME_" NEWROOT [COMMAND....]
25 $PROG_NAME_ is a chroot wrapper with proc/sys/pts/dev filesystem handling
33 if [[ $MOUNTED_ == "" ]]; then
34 MOUNTED_="$to_append_"
36 MOUNTED_="$MOUNTED_ $to_append_"
42 local type_="$1" # type _or_ src
48 if [[ $options_ == "--bind" ]]; then
49 all_options_="--bind $type_"
51 all_options_="-t $type_ none"
53 mount $all_options_ "${DEST_}/$dest_" && storeMounts "$dest_"
58 local reverse=$(echo $MOUNTED_ | awk '{ for (i=NF; i>1; i--) printf("%s ",$i); print $1; }')
69 while getopts "h" opt; do
71 h) printUsage; exit 0 ;;
72 ?) printUsage; exit 64 ;;
75 shift $(($OPTIND - 1))
79 die "Wrong number of arguments."
82 if ! which awk >/dev/null 2>&1 ; then
83 die "No awk binary found, required for execution."
88 if [ ! -d "$DEST_" ]; then
89 die "Target chroot does not exist: $DEST_"
95 mountit "/dev" "dev" "--bind"
96 mountit "devpts" "dev/pts"
98 WROTE_DEBIAN_CHROOT=""
99 if [ ! -f "$DEST_"/etc/debian_chroot ]; then
100 WROTE_DEBIAN_CHROOT="yes"
101 echo "Writing /etc/debian_chroot ..."
102 cat "$DEST_"/etc/hostname > "$DEST_"/etc/debian_chroot
105 if (( $# < 1 )); then
114 if [ ! -z "$WROTE_DEBIAN_CHROOT" ]; then
115 rm "$DEST_"/etc/debian_chroot