Script "chroot-here.sh"
#!/bin/sh

# --------------------------
# chroot-here.sh
# version 1.5.3.8
# --------------------------

for arg in $@
do
  if [ $arg = "--help" ] || [ $arg = "--usage" ]; then
    echo "Usage:" ./`basename ${0}` "[ --nomount ] [ --noscreen ]"
    exit 0
  fi
done

if [ ! -d ./bin ]; then
  echo "ERROR: ./bin not found, can't chroot here"
  exit 1
fi

# check for .chroot_lock in root directory
if [ -f /.chroot_lock ]; then
  echo "ERROR: /.chroot_lock found, nesting is not allowed"
  exit 1
fi

# check for .chroot_lock in current directory
if [ -f ./.chroot_lock ]; then
  echo "ERROR: ./.chroot_lock found, can't chroot here twice"
  exit 1
fi

if [ ! `whoami` = "root" ]; then
  echo "ERROR: you must be root to chroot"
  exit 1
fi

current_wdir=`pwd`
date > ./.chroot_lock

NOMOUNT_OPT=""
NOSCREEN_OPT=""
for arg in $@
do
  [ $arg = "--nomount" ] && NOMOUNT_OPT="yes" && continue
  [ $arg = "--noscreen" ] && NOSCREEN_OPT="yes" && continue
  echo "${0}: unknown option '${arg}'"
done

[ ! -z ${NOMOUNT_OPT} ] && [ -z ${NOSCREEN_OPT} ] && echo "WARNING: screen will probably fail without /dev/pts; try to add --noscreen then"

if [ -f ./bin/busybox ]; then
  cd ./bin
  # link ash in /bin to busybox, link sh in /bin to busybox
  [ -f ./ash ] || ln -s --force busybox ash
  [ -f ./sh ] || ln -s --force busybox sh
  cd ..
fi

# make sure that essential directories are present as well as "root" in /etc/passwd
mkdir -p ./proc ./sys ./dev ./root ./tmp ./usr ./etc ./var ./var/run ./var/tmp
[ -f ./etc/passwd ] || echo "root::0:0:root:/root:/bin/sh" > ./etc/passwd
[ -f ./var/run/utmp ] || cp /var/run/utmp ./var/run/

if [ -z ${NOMOUNT_OPT} ]; then
  # bind mounts
  mount --bind /proc ./proc
  mount --bind /sys ./sys
  mount --bind /dev ./dev
  mount --bind /dev/pts ./dev/pts
  [ -f ./dev/MAKEDEV ] && rm -f ./dev/MAKEDEV
  mkdir -p ./lib/modules/`uname -r`
  mount --bind /lib/modules/`uname -r` ./lib/modules/`uname -r`
fi

# try to obtain /etc/resolv.conf from the host system
if [ -f /etc/resolv.conf ] && [ ! -f ./etc/resolv.conf ]; then
  cp /etc/resolv.conf ./etc/
fi

ORIG_SHELL="${SHELL}"
ORIG_TERM="${TERM}"
##ORIG_LESS="${LESS}"
unset SHELL ; export SHELL="/bin/sh"
unset TERM ; export TERM="linux"
##unset LESS ; export LESS="-R"

# chroot!
chroot_done=""
if [ -z ${NOSCREEN_OPT} ]; then
  if [ -f ./usr/bin/screen ] || [ -L ./usr/bin/screen ]; then
    chroot_done="yes"
    chroot . /usr/bin/screen
  elif [ -f ./bin/screen ] || [ -L ./bin/screen ]; then
    chroot_done="yes"
    chroot . /bin/screen
  fi
fi
if [ -z ${chroot_done} ]; then
  if [ -f ./bin/bash ]; then
    export SHELL="/bin/bash"
    chroot_done="yes"
    chroot . /bin/bash --login
  else
    chroot_done="yes"
    chroot . /bin/sh --login
  fi
fi

# ----------------------------------------

cd "${current_wdir}"
echo "cleaning up..."

##unset LESS ; export LESS="${ORIG_LESS}"
unset SHELL ; export SHELL="${ORIG_SHELL}"
unset TERM ; export TERM="${ORIG_TERM}"

if [ -z ${NOMOUNT_OPT} ]; then
  # unmount
  umount ./proc 1>/dev/null 2>&1 || echo "ERROR: unable to unmount /proc"
  umount ./sys 1>/dev/null 2>&1 || echo "ERROR: unable to unmount /sys"
  umount ./dev/pts 1>/dev/null 2>&1 || echo "ERROR: unable to unmount /dev/pts"
  umount ./dev 1>/dev/null 2>&1 || echo "ERROR: unable to unmount /dev"
  umount ./lib/modules/`uname -r` 1>/dev/null 2>&1 || echo "ERROR: unable to unmount" /lib/modules/`uname -r`
  rmdir ./lib/modules/`uname -r` 1>/dev/null 2>&1
fi

# see you later
rm -f ./.chroot_lock
rm -rf ./tmp

cowsay_present=""
which cowsay >/dev/null 2>&1 && cowsay_present="yes"
if [ -z ${cowsay_present} ]; then
  echo "Bye!"
else
  cowsay -f small "Bye!"
  echo ""
fi
Unless otherwise stated, the content of this page is licensed under GNU Free Documentation License.