Last active
February 2, 2017 21:40
-
-
Save StefanoBelli/99f10cb2dcabaf770f74a820a47543a2 to your computer and use it in GitHub Desktop.
Easy chrooting, mount and run this script :P
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
show_help() { | |
echo " ** Usage: $0 <chroot_target> [shell]" | |
echo " ** Where [shell] is not strictly required (will use /bin/sh)" | |
} | |
failure() { | |
echo -n " " | |
echo $@ | |
exit 3 | |
} | |
check_mounted_eof() { #eof means exit on failure | |
MNTPOINT=$1 | |
for mnts in $(mount | awk '{ print $3 }'); | |
do | |
[[ $mnts == $MNTPOINT ]] && return | |
done | |
failure !! FATAL: target $MNTPOINT - no device mounted | |
} | |
check_shell_eof() { #eof means exit on failure | |
NROOT_SHELL=$1 | |
if [ ! -f $NROOT_SHELL ]; | |
then | |
failure !! FATAL: cannot find shell - $NROOT_SHELL - no such file | |
fi | |
} | |
TARGETS_BIND="/dev /sys /proc" #basic | |
TARGET=$1 | |
SHELL="/bin/sh" | |
if [[ $# < 1 ]]; | |
then | |
echo " !! Not enough arguments. Invoking help" | |
show_help | |
exit 1 | |
fi | |
if [[ $# == 2 ]] || [[ $# > 2 ]]; | |
then | |
SHELL=$2 | |
fi | |
if [ $UID -ne 0 ]; | |
then | |
echo " !! You must be root. Exiting" | |
exit 2 | |
fi | |
check_mounted_eof $TARGET | |
check_shell_eof $SHELL | |
for i in $TARGETS_BIND; | |
do | |
echo " ** BIND: $i" | |
mount --rbind $i ${TARGET}${i} 2>/dev/null || failure !! FATAL: Cannot bind $i to destination | |
mount --make-rslave ${TARGET}${i} 2>/dev/null || failure !! FATAL: Cannot bind $i to destination | |
done | |
echo " ** CHROOT: $TARGET (shell: $SHELL)" | |
chroot $TARGET $SHELL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment