User interface for the bash commands 'mount' and 'chroot"

Post there if you plan to contribute to this SystemCd
Salvat
Posts: 2
Joined: 27 Aug 2011, 16:30
Location: Paris, France
Contact:

User interface for the bash commands 'mount' and 'chroot"

Postby Salvat » 27 Aug 2011, 16:59

Hello

[sorry for my English]

These 2 scripts (see below) are an user interface (Xterminal and text console) for the bash commands "Mount" and "Chroot".
They are written in "Bash", with "Awk" and "Cdialog" ; "FsArchiver" is used for scanning the devices and partitions.

1-) First script "Monter" :
The user has a menu with the partitions found by 'FsArchiver' (except for swap partition). the chosen partitions are mounted by the script with the right file system type

2-) Second script "Chrooter" :
The script makes a chroot to the chosen partition and leaves the user in a "chroot console".

[These 2 scripts are running with a third script ('montreFST.sh')]


R. Salvat


First script "Monter"

Code: Select all

#!/bin/bash
#----------------------------------------------------------------------------------------------------------
#
#    MOUNT one or several partitions scanned, with the right file system type
#
#   The script provides a menu with the partitions found by 'FsArchiver' (except for swap partition) ;
#   the chosen partitions are mounted by the script with the right file system type (of course..)
#
#----------------------------------------------------------------------------------------------------------
#
#
# Require a complementary script --> check :
   if test ! -x /usr/bin/montreFST.sh
      then echo; echo "___ The file '/usr/bin/montreFST.sh' does not exist or is not executable___"; exit 1
   fi
#
# Retrieve the remainder of the command line
   AppelChroot=$1
   if [[ "$AppelChroot" == "--help" ]]
      then
      echo
      echo " monter [--chroot] "
      echo " The script provides a menu with the partitions found by 'FsArchiver' (except for swap partition) ;"
      echo " the chosen partitions are mounted by the script with the right file system type (of course..)"
      echo " The option --chroot calls the script 'chrooter'"
      echo
      exit 0
   else
   echo $AppelChroot
   fi
#
   rm /tmp/finalType.txt 2> /dev/nul
   touch /tmp/intermediaire.txt
   touch /tmp/final.txt
   touch /tmp/finalType.txt
#
# Retrieve the partitions of the scanned list (except for swap partition)
   fsarchiver probe 2> /tmp/intermediaire.txt
#
   awk 'substr($1,5,1) <= 9 && substr($1,5,1) >= 1 && substr($3,2,4) != "swap" \
   {print substr($1,2,5) "        " substr($3,2,5) " off "}' /tmp/intermediaire.txt > /tmp/final.txt
   Resultat=`cat /tmp/final.txt`
#
# Show the partitions with 'montreFST.sh' (2 scenarios: either in Xconsole [tty --> pts], or TextConsole [tty --> tty] :
   tty | awk '$1~/pts/ { system("terminal  --hide-toolbars --hide-menubar --geometry=90x30 --execute /usr/bin/montreFST.sh --graph &") }'
   tty | awk '$1 ~/tty/ { system("/usr/bin/montreFST.sh") }'
#
# "checklist box"  to choose one or several partitions :
#
   touch /tmp/dialoghdatmp && FICHTMP=/tmp/dialoghdatmp
   trap "rm -f $FICHTMP" 0 1 2 3 15
#
   dialog \
   --backtitle "Well, i mount what partitions ?" --title "Menu " \
   --ok-label "Ok" --cancel-label "Quit" \
   --checklist "Check the partitions (mount on /mnt)" 16 38 7 \
   $Resultat 2> $FICHTMP
if [ "$?" != "0" ]; then
   echo $?
   exit $?
fi
#
# Format : Remove the inverted commas
Rien=""
Guillemet='"'
   sed "s/$Guillemet/$Rien/g" < $FICHTMP > /tmp/brouillon.txt
#
# Unite the files 'Chosen partitions' and 'File system Type'
ListePart=`cat /tmp/brouillon.txt`
   for index in $ListePart
      do
      grep -w $index /tmp/final.txt >> /tmp/finalType.txt
   done
echo
echo "+++++++++++"
echo
#
# Ensure the mount-point /mnt/XXX exist ; otherwise create ; $1 : first field : 'partition' ; $2 : second field : 'type'
   awk '{print("Partition="$1"; Type="$2"; \
   if test ! -d /mnt/$Partition; \
   then mkdir -v /mnt/$Partition; fi; ") | "sh"}' /tmp/finalType.txt
#
# Mount :
   awk '{print("Partition="$1"; Type="$2"; \
   mount -v -t $Type /dev/$Partition /mnt/$Partition") | "sh"}' /tmp/finalType.txt
#
echo
echo "+++++++++++"
echo
#
# Option :
   if [[ "$AppelChroot" == "--chroot" ]]
      then
      sleep 2
      /usr/bin/chrooter
   fi
#
exit 0
#
#-----------------------------End------------------------------------
#


Second script
#!/bin/bash
#---------------------------------------------------------------------------------------------------
#
# CHROOT in a chosen partition
#
# This script is an user interface (Xterminal and text console) for the bash command "chroot".
# The script makes a chroot to the chosen partition and leaves the user in a "chroot console".
#
#---------------------------------------------------------------------------------------------------
#
#
# Require a complementary script --> check :
if test ! -x /usr/bin/montreFST.sh
then echo; echo "___ The file '/usr/bin/montreFST.sh' does not exist or is not executable___"; exit 1
fi
#
# Retrieve the remainder of the command line
Appel=$1
if [[ "$Appel" == "--help" ]]
then
echo
echo " chrooter "
echo " This script is an user interface (Xterminal and text console) for the bash command "chroot"."
echo " The script makes a chroot to the chosen partition and leaves the user in a "chroot console"."
echo
exit 0
fi
#
touch /tmp/final2.txt
#
# Show the partitions with 'montreFST.sh' (2 scenarios: either in Xconsole [tty --> pts], or TextConsole [tty --> tty] :
tty | awk '$1~/pts/ { system("terminal --hide-toolbars --hide-menubar --geometry=90x30 --execute /usr/bin/montreFST.sh --graph &") }'
tty | awk '$1 ~/tty/ { system("/usr/bin/montreFST.sh") }'
#
# "checklist box" To choose one or several partitions :
#
touch /tmp/dialoghdatmp && FICHTMP=/tmp/dialoghdatmp
trap "rm -f $FICHTMP" 0 1 2 3 15
#
dialog \
--backtitle "What partition ?" --title "CHROOT " \
--ok-label "Ok" --cancel-label "Quit" \
--inputbox "Type the partition (for example : sda1) " 12 28 sda1 2> $FICHTMP
if [ "$?" != "0" ]; then
echo $?
exit $?
fi
#
# Remove the tabulations and spaces :
sed 's/\s\{1,\}//' < $FICHTMP | sed 's/\s\{1,\}//' | sed 's/\s\{1,\}//' | sed 's/\s\{1,\}//' > /tmp/final2.txt
Partition=`cat /tmp/final2.txt`
#
echo "==========="
echo $Partition
echo "==========="
#
# Check if '$Partition' is mounted and mounted on '/mnt/'
if ! grep -wq "/dev/"$Partition /etc/mtab
then
echo ; echo "Fatal error : "$Partition" is not mounted ; I exit."
exit 1
fi
if ! grep -wq "/mnt/"$Partition /etc/mtab
then
echo ; echo "Fatal error : "$Partition" must be mounted on '/mnt' with his device-name : /mnt"$Partition" ; I exit)."
exit 1
fi
# CHROOT with a console
echo "Bind the system files :"
mount -v -o bind /proc /mnt/$Partition/proc
mount -v -o bind /dev /mnt/$Partition/dev
mount -v -o bind /sys /mnt/$Partition/sys
echo "==========="
echo "CHROOT console : (type 'exit' for exit)"
chroot /mnt/$Partition ``bash -i''
#
exit 0
#
#-----------------------------End------------------------------------
#


Third script

Code: Select all

#!/bin/bash
#
# Script called by the scripts "MONTER" and "CHROOTER" 
#
 VERT="\\033[1;32m"
  NORMAL="\\033[0;39m"
   ROUGE="\\033[1;31m"
    ROSE="\\033[1;35m"
     BLEU="\\033[1;34m"
      BLANC="\\033[0;02m"
       BLANCLAIR="\\033[1;08m"
        JAUNE="\\033[1;33m"
         CYAN="\\033[1;36m"
#
# Retrieve the remainder of the command line
Graphique=$1
   if [[ "$Graphique" == "--help" ]]
      then
      echo
      echo " Script called by the scripts 'MONTER' and 'CHROOTER'."
      echo
      exit 0
   fi
#
#                           ----If we are in a "X console"----
if [[ "$Graphique" == "--graph" ]]
then
   echo -e "$VERT" '=====================>>> fsarchiver probe simple <<<=====================' "$NORMAL"
   fsarchiver probe simple
   echo ' '
   echo -e "$VERT" '=====================>>> file /etc/mtab <<<===========================' "$NORMAL"
   cat /etc/mtab
   echo -e "$VERT" '_________________________________________________________________________' "$NORMAL"
   echo ' '
   echo -e "$BLEU" 'Press any key to exit' "$NORMAL"
   read key
#
else
#                           ----If we are in a "Text console"----
   clavier=""
   echo -e "$VERT" "-------------------------------------------------------------------------" "$NORMAL" > /tmp/MontreFS.txt
   echo -e "$BLEU" " Scanned partitions and mounted partitions (file /etc/mtab)" >> /tmp/MontreFS.txt
   echo -e "$BLEU" " Then, menu to choose one or several partitions to mount (after exiting)" >> /tmp/MontreFS.txt
   echo >> /tmp/MontreFS.txt
   echo -e "$VERT" '=====================>>> fsarchiver probe simple <<<=====================' "$NORMAL" >> /tmp/MontreFS.txt
   fsarchiver probe 2>> /tmp/MontreFS.txt
   echo >> /tmp/MontreFS.txt
   echo -e "$VERT" '=====================>>> file /etc/mtab <<<===========================' "$NORMAL" >> /tmp/MontreFS.txt
   cat /etc/mtab >> /tmp/MontreFS.txt
   echo -e "$VERT" '_________________________________________________________________________' "$NORMAL" >> /tmp/MontreFS.txt
   echo >> /tmp/MontreFS.txt
   #sleep 2
   cat /tmp/MontreFS.txt | more -d
   sleep 1
   echo -e "$BLEU" 'Press CTRL + c to choose one or several partitions to mount' "$NORMAL"
   while [ ! $clavier ]
   do
   read clavier
   done
#
fi
#
#--------------------------------------------End------------------------------------------------------

Salvat
Posts: 2
Joined: 27 Aug 2011, 16:30
Location: Paris, France
Contact:

Re: User interface for the bash commands 'mount' and 'chroot

Postby Salvat » 18 May 2013, 12:48

Hello

I update the first script "Monter" :
- minor bug fix (in line 66 : "substr($3,2,5)" changed to "substr($3,2,8)", because 'reiserfs" = 8 letters, not 5 (!) )
- new option [--read-only]
- ntfs files in 'read/write' (with "ntfs-3g")
- menu with LABEL

[sorry for my English : i don't update..]


R. Salvat


First script "Monter", updated :

Code: Select all

#!/bin/bash
#----------------------------------------------------------------------------------------------------------
#
#    MOUNT one or several partitions scanned, with the right file system type
#
#   The script provides a menu with the partitions found by 'FsArchiver' (except for swap partition) ;
#   the chosen partitions are mounted by the script with the right file system type (of course..)
#   'read-only' or 'read/write'
#
#----------------------------------------------------------------------------------------------------------
#
#
# Require a complementary script --> check :
   if test ! -x /usr/bin/montreFST.sh
      then echo; echo "___ The file '/usr/bin/montreFST.sh' does not exist or is not executable___"; exit 1
   fi
#
   rm /tmp/brouillon.txt 2> /dev/nul
   rm /tmp/intermediaire.txt 2> /dev/nul
   rm /tmp/final.txt 2> /dev/nul
   rm /tmp/finalTypeN.txt 2> /dev/nul
   rm /tmp/finalTypePN.txt 2> /dev/nul
   rm /tmp/finalType.txt 2> /dev/nul
   touch /tmp/brouillon.txt
   touch /tmp/intermediaire.txt
   touch /tmp/final.txt
   touch /tmp/finalTypeN.txt
   touch /tmp/finalTypePN.txt
   touch /tmp/finalType.txt
   Alphabetiser=""
   AppelChroot=""
#
# Retrieve the remainder of the command line
      if echo $* | grep --ignore-case --no-messages --quiet --regexp=--help --regexp=-h
      then
      echo
      echo
      echo " monter [--chroot] [--read-only] "
      echo
      echo " The script provides a menu with the partitions found by 'FsArchiver' (except for swap partition) ;"
      echo " the chosen partitions are mounted by the script with the right file system type (of course..), read/write or read-only"
      echo " The option [--chroot] calls the script 'chrooter'"
      echo " The option [--read-only] (or [-r], or [-ro]), mounts the filesystem 'read-only' (default is 'read/write')"
      echo
      echo " Version : 2.00 "
      echo
      exit 0
   else
   echo
   if echo $* | grep --ignore-case --no-messages --quiet --regexp=--read-only --regexp=-r
   then Alphabetiser="--read-only"
   fi
   if echo $* | grep --ignore-case --no-messages --quiet --regexp=--chroot
   then AppelChroot="--chroot"
   fi
      fi
   echo
   echo $Alphabetiser
   echo $AppelChroot
   echo
#
# Retrieve the partitions of the scanned list (except for swap partition)
   fsarchiver probe 2> /tmp/intermediaire.txt
#
   awk 'substr($1,5,1) <= 9 && substr($1,5,1) >= 1 && substr($3,2,4) != "swap" \
   {print substr($1,2,5) "     " substr($3,2,8) "________" substr($5,2,10) " off "}' /tmp/intermediaire.txt > /tmp/final.txt
   Resultat=`cat /tmp/final.txt`
#
# Show the partitions with 'montreFST.sh' (2 scenarios: either in Xconsole [tty --> pts], or TextConsole [tty --> tty] :
   tty | awk '$1~/pts/ { system("terminal  --hide-toolbars --hide-menubar --geometry=90x30 --execute /usr/bin/montreFST.sh --graph &") }'
   tty | awk '$1 ~/tty/ { system("/usr/bin/montreFST.sh") }'
#
# "checklist box" to choose one or several partitions :
#
   touch /tmp/dialoghdatmp && FICHTMP=/tmp/dialoghdatmp
   trap "rm -f $FICHTMP" 0 1 2 3 15
#
   dialog \
   --backtitle "Well, i mount what partitions ?" --title "Menu " \
   --ok-label "Ok" --cancel-label "Quit" \
   --checklist "Check the partitions (mount on /mnt)" 16 62 7 \
   $Resultat 2> $FICHTMP
if [ "$?" != "0" ]; then
   echo $?
   exit $?
fi
#
# Format : Remove the inverted commas
Rien=""
Guillemet='"'
   sed "s/$Guillemet/$Rien/g" < $FICHTMP > /tmp/brouillon.txt
#
# Unite the files 'Chosen partitions' and 'File system Type'
ListePart=`cat /tmp/brouillon.txt`
   for index in $ListePart
      do
      grep -w $index /tmp/final.txt >> /tmp/finalTypePN.txt
   done
#
# Format : Remove the "________"
Espace=" "
Tiret="________"
   sed "s/$Tiret/$Espace/g" < /tmp/finalTypePN.txt > /tmp/finalTypeN.txt
#
echo
echo "+++++++++++"
echo
#
# Ensure the mount-point /mnt/XXX exist ; otherwise create ; $1 : first field : 'partition' ; $2 : second field : 'type'
   awk '{print("Partition="$1"; Type="$2"; \
   if test ! -d /mnt/$Partition; \
   then mkdir -v /mnt/$Partition; fi; ") | "sh"}' /tmp/finalTypeN.txt
#
# Mount :
if [[ $Alphabetiser == "--read-only" ]]
then 
# read-only
cat /tmp/finalTypeN.txt > /tmp/finalType.txt
   awk '{print("Partition="$1"; Type="$2"; \
   mount -v -r -t $Type /dev/$Partition /mnt/$Partition") | "sh"}' /tmp/finalType.txt
else 
# read/write
# Format : Replace "ntfs" by "ntfs-3g" for ntfs read/write
   sed "s/ntfs/ntfs-3g/g" < /tmp/finalTypeN.txt > /tmp/finalType.txt
   awk '{print("Partition="$1"; Type="$2"; \
   mount -v -t $Type /dev/$Partition /mnt/$Partition") | "sh"}' /tmp/finalType.txt
fi
#
echo
echo "+++++++++++"
echo
#
# Option :
   if [[ "$AppelChroot" == "--chroot" ]]
      then
      sleep 2
      /usr/bin/chrooter
   fi
#
exit 0
#
#-----------------------------End------------------------------------
#


Return to “Contributions”

Who is online

Users browsing this forum: No registered users and 2 guests