chroot-mnt

Submitted scripts and programs
Forum rules
Your own work only.
pidsley
Hermit
Posts: 2539
Joined: Wed Oct 17, 2012 12:31 pm

chroot-mnt

Unread post by pidsley » 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.

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

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: chroot-mnt

Unread post by machinebacon » Tue Jun 24, 2014 3:22 am

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.
..gnutella..

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: chroot-mnt

Unread post by dkeg » Tue Jun 24, 2014 3:51 am

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.

Work hard; Complain less

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5877
Joined: Tue Sep 03, 2013 7:05 am

Re: chroot-mnt

Unread post by GekkoP » Tue Jun 24, 2014 9:16 am

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. :)

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: chroot-mnt

Unread post by wuxmedia » Mon Jul 06, 2015 9:36 am

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!
"Seek, and Ye shall find"
"Github | Chooons | Site"

Post Reply