You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
611 B

  1. #!/bin/sh
  2. arg0=$(basename "$0")
  3. fatal() {
  4. echo "F: $arg0: $@"
  5. exit 1
  6. }
  7. if [ $# -gt 0 -a -d "$1" ]; then
  8. DIR="$1"
  9. shift
  10. else
  11. DIR="."
  12. fi
  13. cd "$DIR" || fatal "$DIR: invalid chroot dir"
  14. DIR=$(pwd -P)
  15. error=false
  16. for x in dev dev/pts proc sys tmp; do
  17. if ! grep -q " $DIR/$x " /proc/mounts; then
  18. mount --bind /$x $DIR/$x || error=true
  19. if $error; then
  20. [ "$x" != dev ] || fatal "can't do anything"
  21. break
  22. fi
  23. fi
  24. done
  25. $error || HOME=/root chroot "$DIR" "$@"
  26. while grep -q " $DIR/" /proc/mounts; do
  27. sleep 0.2
  28. grep " $DIR/" /proc/mounts | cut -d' ' -f2 | tac | xargs -r --verbose -- umount
  29. done