Page 1 of 1

chroot-mnt

Posted: Tue Jun 24, 2014 3:18 am
by pidsley
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.

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
Thanks to Xaos for the post here: http://crunchbang.org/forums/viewtopic. ... 94#p150294

Re: chroot-mnt

Posted: Tue Jun 24, 2014 3:22 am
by machinebacon
right on, i like these, without bling~bling - and i won't leave an annoying comment like 'could have also been done like this or that'. this script simply works... thanks, this is super useful.

Re: chroot-mnt

Posted: Tue Jun 24, 2014 3:51 am
by dkeg
yes, I have the cookbook saved off for reference, b/c, as you say, remembering all of these isn't always the case, so this script is helpful.

Re: chroot-mnt

Posted: Tue Jun 24, 2014 9:16 am
by GekkoP
To be honest, I had already grabbed this script from your github Pidsley and it helped me a great deal the first time I did something wrong with Gentoo, so thanks. :)

Re: chroot-mnt

Posted: Mon Jul 06, 2015 9:36 am
by wuxmedia
Fucking love you guys.
Bacon, thanks for putting decent VGA modes in the boot menu.
nice one pids, always thought this would come in handy.
now you can say used in production environment, if that makes any difference to you :)

knoppix wouldn't boot VGA right, so BORK! to the rescue, and I don't think knoppix has a nice script like this, so nice work!