Advertisement
jcomeau_ictx

chroot.sh, for a typical modern GNU/Linux system

Jan 4th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. username=$1
  2. newroot=$2
  3. if [ -z "$newroot" ]; then
  4. echo Usage: $0 USERNAME NEW_ROOT >&2
  5. exit 1
  6. fi
  7. echo Will chroot to $newroot then become user $username >&2
  8. if [ "$newroot" != "/" -a -x $newroot/bin/bash ]; then
  9. cd $newroot
  10. sudo mount -t proc proc proc/
  11. sudo mount -t sysfs sys sys/
  12. sudo mount -o bind /dev dev/
  13. sudo mount -t devpts devpts dev/pts
  14. sudo mount -o bind /media/sda3 media/sda3
  15. sudo chroot . /bin/bash -c "su - $username"
  16. # upon exit from chroot, undo the mounts
  17. sudo umount media/sda3
  18. sudo umount dev/pts
  19. sudo umount dev
  20. sudo umount sys
  21. sudo umount proc
  22. else
  23. echo Unexpected root directory $newroot, nothing done >&2
  24. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement