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
18 trap bailout 1 2 3 3 6 9 14 15
29 if cut -d\ -f 2 /proc/mounts | grep -q "$dir"; then
39 Usage: "$PROG_NAME_" NEWROOT [COMMAND....]
41 $PROG_NAME_ is a chroot wrapper with proc/sys/pts/dev filesystem handling
49 if [[ $MOUNTED_ == "" ]]; then
50 MOUNTED_="$to_append_"
52 MOUNTED_="$MOUNTED_ $to_append_"
58 local type_="$1" # type _or_ src
64 if ! isMounted "${DEST_}/$dest_"; then
65 if [[ $options_ == "--bind" ]]; then
66 all_options_="--bind $type_"
68 all_options_="-t $type_ none"
70 mount $all_options_ "${DEST_}/$dest_" && storeMounts "$dest_"
76 for i in /proc /sys /dev; do
77 umount "${DEST_}/${i}"
80 rm -f "$DEST_"/etc/debian_chroot
88 while getopts "h" opt; do
90 h) printUsage; exit 0 ;;
91 ?) printUsage; exit 64 ;;
94 shift $(($OPTIND - 1))
98 die "Wrong number of arguments."
103 if [ ! -d "$DEST_" ]; then
104 die "Target chroot does not exist: $DEST_"
108 mountit "proc" "proc"
109 mountit "sysfs" "sys"
110 mountit "/dev" "dev" "--bind"
112 # do not write to /var/run of chroot if it's not present
113 if [ -d "$DEST_/tmp" ] ; then
114 STATEDIR="tmp/grml-chroot"
115 mkdir -p "$DEST_/$STATEDIR"
116 touch "$DEST_/$STATEDIR/$$"
119 if [ ! -e "$DEST_"/etc/debian_chroot ]; then
120 echo "Writing /etc/debian_chroot ..."
121 cat "$DEST_"/etc/hostname > "$DEST_"/etc/debian_chroot
124 if (( $# < 1 )); then
132 if [ -z "$STATEDIR" ] ; then
135 rm "$DEST_/$STATEDIR/$$"
137 if rmdir "$DEST_/$STATEDIR" 2>/dev/null; then