My chrooting script

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. :slight_smile:
Thank you.

I’ve put my script here, because on the french forum, there is only please_try_again who can do comments. And I know that here, there are good “scripters”
I’m looking for things that do not work, or that I forgot.

So, as we said in french: no news, good news! :wink:

On 2012-12-05 09:06, rengejao wrote:
>
> I’ve put my script here, because on the french forum, there is only
> please_try_again who can do comments. And I know that here, there are
> good “scripters”
> I’m looking for things that do not work, or that I forgot.

Yep. You forgot to sell us your script, ie, tell us in a paragraph what
can we use it for :slight_smile:


Cheers / Saludos,

Carlos E. R.
(from 12.1 x86_64 “Asparagus” at Telcontar)

Thank you.
With this script, you can chroot to others system you have. Should I explain what is chroot (this will be a little difficult to explain that in english)? I’ve found that: chroot - Wikipedia, the free encyclopedia

For myself I use (often) chroot to have a look, or change something in my others systems without rebooting. My principal system is openSUSE 12.1 with legacy grub, so when someone come on the french forum with questions about openSUSE 12.2 or grub2, I had: to run a virtual machine (which take about 5 minutes to start with the grub2 menu), or reboot on the os 12.2 I’ve installed on my computer. These two things are very annoying, and take lot’s of time.
So I use chroot. it’s like you have two system running at the same time, the chrooted one in the terminal, in text mode (for the moment I don’t arrive to do graphical chroot).

To chroot, you have to write several commands in the terminal. I wrote this script to avoid having to write the same things, again, and again. The first script I’ve write, just ask about the partitions to mount. This one can find the system you have, and directly chroot to it.
…I just thought of something, this script need os-prober to find which other system you have, so I have to check if os-prober is present or not :stuck_out_tongue:

On 2012-12-06 10:06, rengejao wrote:

> Thank you.
> With this script, you can chroot to others system you have. Should I
> explain what is chroot (this will be a little difficult to explain that
> in english)? I’ve found that: ‘chroot - Wikipedia, the free
> encyclopedia’ (http://en.wikipedia.org/wiki/Chroot)

I know what is chroot, but your script is different. :slight_smile:

> For myself I use (often) chroot to have a look, or change something in
> my others systems without rebooting. My principal system is openSUSE
> 12.1 with legacy grub, so when someone come on the french forum with
> questions about openSUSE 12.2 or grub2, I had: to run a virtual machine
> (which take about 5 minutes to start with the grub2 menu), or reboot on
> the os 12.2 I’ve installed on my computer. These two things are very
> annoying, and take lot’s of time.
> So I use chroot. it’s like you have two system running at the same
> time, the chrooted one in the terminal, in text mode (for the moment I
> don’t arrive to do graphical chroot).

Ah, yes, I have used this method often. I have my own script to set it
up correctly.

I’ll have another look at yours :slight_smile:


Cheers / Saludos,

Carlos E. R.
(from 12.1 x86_64 “Asparagus” at Telcontar)

Maybe the name I’ve choose is confusing. I’ve choose a short name wich look like the command chroot, but to much maybe.

Ah, yes, I have used this method often.

I’m happy to see that I’m not the only one!

I have my own script to set it up correctly.

Let me know, if I do something wrong to chroot.

And I’ve not tell that this script can find every separated partitions which are available in the opensuse installer, and /usr. The root partition is found with os-prober, then mounted, and after the script looks in /etc/fstab to find separated partitions.