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_"
59 reverse=$(echo "$MOUNTED_" | awk '{ for (i=NF; i>1; i--) printf("%s ",$i); print $1; }')
70 while getopts "h" opt; do
72 h) printUsage; exit 0 ;;
73 ?) printUsage; exit 64 ;;
80 die "Wrong number of arguments."
83 if ! command -v awk >/dev/null 2>&1 ; then
84 die "No awk binary found, required for execution."
89 if [ ! -d "$DEST_" ]; then
90 die "Target chroot does not exist: $DEST_"
95 if [ -f "$DEST_"/proc/cmdline ] ; then
96 echo "Looks like $DEST_ already has filesystems mounted, skipping."
100 mountit "/dev" "dev" "--bind"
101 mountit "devpts" "dev/pts"
102 if [ -d /sys/firmware/efi/efivars ] ; then
103 mountit "efivarfs" "sys/firmware/efi/efivars"
105 if [ -d "$DEST_"/run/udev ] && [ -d /run/udev ] ; then
106 mountit "/run/udev" "/run/udev" "--bind"
110 WROTE_DEBIAN_CHROOT=""
111 if [ ! -f "$DEST_"/etc/debian_chroot ]; then
112 WROTE_DEBIAN_CHROOT="yes"
113 echo "Writing /etc/debian_chroot ..."
114 cat "$DEST_"/etc/hostname > "$DEST_"/etc/debian_chroot
117 if (( $# < 1 )); then
126 if [ -n "$WROTE_DEBIAN_CHROOT" ]; then
127 rm "$DEST_"/etc/debian_chroot