chroot-mnt
Posted: Tue Jun 24, 2014 3:18 am
We have a grub-repair script that lets you chroot in and fix grub, and an even fancier version by Xaos52 that traps all kinds of errors and other useful shit, but I am lazy and just wanted to automate the few commands I always forget and have to look up. So I scripted it. The script expects to find a valid root partition already mounted at /mnt (or wherever you specify as $TARGET), and will complain if it can't find $TARGET/bin/bash or if you are not root when you run the script. Otherwise, it just mounts stuff, chroots, and unmounts when you exit the chroot.
Thanks to Xaos for the post here: http://crunchbang.org/forums/viewtopic. ... 94#p150294
Code: Select all
#!/bin/bash
# script to automate chroot commands
# mount the target root partition at $TARGET before running this script
#
# this script is provided with NO SUPPORT AND NO GUARANTEES
# if it breaks or does not do what you want, FIX IT YOURSELF
TARGET="/mnt" # root partition must be mounted here
[[ $(id -u) != 0 ]] && echo "you must be root to run this script" && exit
[[ ! -f $TARGET/bin/bash ]] && echo "a valid root partition does not appear to be mounted at $TARGET" && exit
part=$(mount | awk '/ on \'$TARGET' / {print $1}')
echo "chroot to partition mounted at $TARGET ($part)"
echo mounting
mount --bind /dev $TARGET/dev
mount --bind /dev/pts $TARGET/dev/pts
mount --bind /proc $TARGET/proc
mount --bind /sys $TARGET/sys
mount --bind /run $TARGET/run
chroot $TARGET /bin/bash
echo unmounting
umount -l $TARGET/dev/pts
umount -l $TARGET/dev
umount -l $TARGET/proc
umount -l $TARGET/sys
umount -l $TARGET/run