Hello,
I come here to present my script of chroot.
This script has began on the french forum. https://forums.opensuse.org/franaais-french/aide-et-support/programmation-et-scripts/480488-mon-script-de-chroot.html
With all comments and suggestions from please_try_again, this script has grow up, and now I found it very cool! So big thanks to him!
I’ve named this script chr00t (with zero), but you can save it with the name you want.
Here are the options:
-h --help
-u --unmount
-i --interactive
With the option -i, the script show you the available systems, and ask you the one you want to chroot.
You can directly tell the system you want to chroot.
Example:
chr00t archlinux
Searching for archlinux ...
chroot to Arch Linux (rolling)
/ [ok]
/home [ok]
proc [ok]
sys [ok]
dev [ok]
---export PS1="(chroot) $PS1"---
[root@linux-xkcx /]#
If you have multiple version of the same system, you have to precise the n° of version (without space), like opensuse12.2. Without the precision, the script will chroot the first he can found.
The option -u, is to umount partitions and delete the chr00t directory. Don’t forget to do it after chrooting!
The line “export PS1=”(chroot) $PS1"" is to copie and past it in the chrooted system. So you have (chroot) at the begining of the prompt.
So, here is the code:
#! /bin/bash
#chr00t by rengejao
#options:
#-h --help : show this help
#-u --unmount : umount partitions
#-i --interactive : interactive mode
# colors
red='e[31;1m'; dred='e[31m'
gre='e[32;1m'; dgre='e[32m'
yel='e[33;1m'; dyel='e[33m'
blu='e[34;1m'; dblu='e[34m'
mag='e[35;1m'; dmag='e[35m'
cya='e[36;1m'; dcya='e[36m'
whi='e[37;1m'; noc='e[37;0m'
sgr=$(tput sgr0)
# Check to see if we are root
if [[ $UID -ne 0 ]]; then
id -Gn | grep -q wheel || echo "Root User Permissions are required, Please Enter the ..."
echo
sudo $0 $1
exit 0
fi
funct_help () {
echo '-h --help : show this help'
echo '-u --unmount : umount partitions'
echo '-i --interactive : interactive mode'
exit
}
Mount_uuid () {
mount -U ${pt[$i]} /mnt/chr00t/${mp[$i]} 2> /dev/null && echo "${mp[$i]} ["$gre"ok"$noc"]"
}
Mount_label () {
mount -L ${pt[$i]} /mnt/chr00t/${mp[$i]} 2> /dev/null && echo "${mp[$i]} ["$gre"ok"$noc"]"
}
Mount_dev () {
mount ${pt[$i]} /mnt/chr00t/${mp[$i]} 2> /dev/null && echo "${mp[$i]} ["$gre"ok"$noc"]"
}
Mount_part () {
eval $(awk '{if ( $2 == "/home" || $2 == "/usr/local" || $2 == "/var" || $2 == "/usr" || $2 == "/boot" || $2 == "/srv" || $2 == "/tmp" || $2 == "/opt") print $2, $1 }' /mnt/chr00t/etc/fstab | sort | awk 'BEGIN { i=-1}; { i++ ; printf "mp[%s]=\"%s\"; pt[%s]=\"%s\";
", i, $1, i, $2 }')
i=0
while [ $i -lt ${#pt[li]} ]; do
[/li] Mount_uuid
Mount_label
Mount_dev
let i++
done
}
funct_chroot () {
mount -t proc none /mnt/chr00t/proc && echo "proc ["$gre"ok"$noc"]"
mount -t sysfs sys /mnt/chr00t/sys && echo "sys ["$gre"ok"$noc"]"
mount -o bind /dev /mnt/chr00t/dev && echo "dev ["$gre"ok"$noc"]"
echo '---export PS1="(chroot) $PS1"---'
cp -L /etc/resolv.conf /mnt/chr00t/etc/resolv.conf 2> /dev/null
chroot /mnt/chr00t /bin/bash
}
#Host system variables
name=`cat /etc/os-release | grep -i pretty_name | awk -F '=' '{ print $2 }' | sed -n '1p' | sed 's/"//g'`
root=`df -hl | grep -vi rootfs | awk '/\/$/ { print $1 }'`
uflag=0
iflag=0
args=`getopt -q -u -o hui -l help,unmount,interactive -- "$@"`
set -- $args
for i; do
case "$i" in
-h|--help) funct_help ;;
-u|--unmount) uflag=1 ; shift ;;
-i|--interactive) iflag=1 ; shift ;;
esac
done
shift
chrootos=$@
#Interactive mode
if [ $iflag -eq 1 ]; then
mkdir /mnt/chr00t 2> /dev/null
#list systems
echo -e Available systems:"
"
echo Host system:
echo -e "0 $name"
echo Other systems:
eval $(os-prober 2>/dev/null | awk -F ":" 'BEGIN {i=-1} /linux/ {i++ ; printf "dev[%s]=\"%s\"; os[%s]=\"%s\";
", i, $1, i, $2}')
i=0
while [ $i -lt ${#os[li]} ]; do
[/li] echo -e "$(($i+1)) ${os[$i]}"
let i++
done
read -p "Which system do you want to chroot to? [0-$i]: " syst
while [[ $syst > $i ]]; do
read -p "Which system do you want to chroot to? [0-$i]: " syst
done
case $syst in
[0]) read -p "You selected: $name. Ok? [y/n] " yesno;;
*) read -p "You selected: ${os[(($syst-1))]}. Ok? [y/n] " yesno;;
esac
while [ "$yesno" = "no" ] || [ "$yesno" = "n" ]; do
read -p "Which system do you want to chroot to? [0-$i]: " syst
while [[ $syst > $i ]]; do
read -p "Which system do you want to chroot to? [0-$i]: " syst
done
case $syst in
[0]) read -p "You selected: $name. Ok? [y/n] " yesno;;
*) read -p "You selected: ${os[(($syst-1))]}. Ok? [y/n] " yesno;;
esac
done
#Mount host system
if [ $syst = 0 ]; then
mount $root /mnt/chr00t && echo "/ ["$gre"ok"$noc"]"
Mount_part
#Mount other systems
else
mount ${dev[(($syst-1))]} /mnt/chr00t && echo "/ ["$gre"ok"$noc"]"
Mount_part
fi
funct_chroot
#Umount partitions
elif [ $uflag -eq 1 ]; then
eval $(awk 2>/dev/null '{if ( $2 == "/home" || $2 == "/usr/local" || $2 == "/var" || $2 == "/usr" || $2 == "/boot" || $2 == "/srv" || $2 == "/tmp" || $2 == "/opt") print $2, $1 }' /mnt/chr00t/etc/fstab | sort -r | awk 'BEGIN { i=-1}; { i++ ; printf "mp[%s]=\"%s\"; pt[%s]=\"%s\";
", i, $1, i, $2 }')
i=0
while [ $i -lt ${#pt[li]} ]; do
[/li] umount /mnt/chr00t/${mp[$i]} /mnt/chr00t/dev /mnt/chr00t/sys /mnt/chr00t/proc /mnt/chr00t 2> /dev/null
let i++
done
rmdir /mnt/chr00t && echo "partitions umounted ["$gre"ok"$noc"]"
exit
#Chroot with os as argument
else
[ "$chrootos" ] || exec echo "missing argument."
mkdir /mnt/chr00t 2> /dev/null
hostsystem=$(printf %s $name | grep -i $chrootos)
case ${#hostsystem} in
[0]) echo Searching for $chrootos ...
eval $(os-prober 2>/dev/null | awk -F ":" 'BEGIN {i=-1} /linux/ {i++ ; printf "dev[%s]=\"%s\"; os[%s]=\"%s\";
", i, $1, i, $2}')
i=0
while [ $i -lt ${#os[li]} ]; do
[/li] system=$(printf %s ${os[$i]} $i | grep -i $chrootos)
case ${#system} in
[0]) echo 'system not found' 1> /dev/null ;;
*) system2=$system;;
esac
let i++
done
case ${#system2} in
[0]) echo 'system not found';;
*) index=$(printf %s $system2 | tail -c1)
echo "chroot to ${os[$index]} "
mount ${dev[$index]} /mnt/chr00t && echo "/ ["$gre"ok"$noc"]"
Mount_part; funct_chroot;;
esac ;;
*) echo "chroot to $name "
mount $root /mnt/chr00t && echo "/ ["$gre"ok"$noc"]"
Mount_part; funct_chroot;;
esac
fi
And now I’m waiting for your comments.
Thank you.