Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 315 lines (279 sloc) 6.99 KB
#!/bin/bash
# This script is used to deal with partition, blkid and disks label
# It is used to mount/umount FS based on this label
# One can list this partition and based on label mount/umount ...
# It is usefull as I have several distro and need to check them from another one.
# See blk -h
# Determine program name
PRGDIR=`type $0 | awk '{print $3}'`
PRGNAME=`basename $0`
PRGNAME=`basename $0 | awk -F '.' '{print $1}'`
# Get absolute path name of the script
PWD=`pwd`
DIRNAME=`dirname $0| sed 's/^\.//'`
LIB="$PWD/$DIRNAME"
# Debug variable
DEBUG=0
trap 'trapfunc' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
function trapfunc {
printf "%-10s %-10s %-22s %-30s \n" "Info :" "$PRGNAME : " "Program aborted"
end
exit
}
function usage {
cat << FIN
Manage local FS devices
Wihtout arg display dev + label and exit
usage : $PRGNAME
-C : As -s but then chroot the given partition
-d : run in debug mode
-l : Display linux type only (ext4)
-m : mount all devices ext4 to /media/\$LABEL (create if not exist)
-p : purge $MNT fs (remove all not mounted sub-directory
-s : mount a single ressource (based on LABEL)
-u : umount all FS not listed in /etc/fstab
-v : full verbose mode
FIN
}
function end {
if [ $# -ne 0 ];
then
RET=$1
else
RET=0
fi
if [ -f {$TMPF1:=DUMMY.$$} ] ;
then
rm $TMPF1
fi
exit $RET
}
amiroot() {
CMD=/usr/bin/whoami
if [ ! -x $CMD ];
then
printf "%-10s %-10s %-22s %-30s \n" "ERROR :" "check root : " "cant get root status"
return 1
else
__DUM=$(eval $CMD)
if [ ${__DUM:=NULL} != "root" ];
then
printf "%-10s %-10s %-22s %-30s \n" "ERROR :" "check root : " "Need to be root to run this"
return 1
fi
fi
return 0
}
function rundiag() {
echo > /dev/null
}
get_valid_devname() {
local __ARG1
__ARG1=$1 ; shift
__DEV=$(blkid -L $__ARG1)
if [ $? -ne 0 ] ;
then
printf "%-10s %-10s %-22s %-30s \n" "ERROR :" "mount_device : " "Arg not OK $#"
echo ""
exit 1
else
echo "$__DEV"
return 0
fi
}
# need to have DEV to mount from blkid and mount point
# optionnal -C to then chroot in the device
mount_device2() {
if ! amiroot ; then return 9
fi
OPTION=NONE
[[ ${ZDEBUG:=0} -eq 1 ]] && echo "A $# : Arg $* : I $OPTIND A $OPTARG"
while getopts C sarg
do
[[ ${ZDEBUG:=0} -eq 1 ]] && echo "arg $sarg"
case $sarg in
C) [[ ${ZDEBUG:=0} -eq 1 ]] && echo "Boucle chroot"
OPTION=CHROOT ;;
*) printf "%-10s %-10s %-22s %-30s \n" "ERROR :" "mount_device2 : " "Error in passing arguments"
usage
end ;;
esac
done
shift $(( $OPTIND - 1))
if [ $# -ne 2 ] ;
then
printf "%-10s %-10s %-22s %-30s \n" "ERROR :" "mount_device2 : " "Arg not OK $#"
return 1
fi
DEV=$1
LABEL=$2
DUMMY=$(df -t ext4 | grep -w $DEV | awk '{print $NF}')
NBCHAR=$(echo $DUMMY | wc -c)
if [ $NBCHAR -eq 1 ] ;
then
if [ ! -d /mnt/$LABEL ] ;
then
mkdir /mnt/$LABEL
if [ $? -ne 0 ] ;
then
printf "%-10s %-10s %-22s %-30s \n" "ERROR :" "mount_device2 : " "Error bulding /mnt/$LABEL"
return
fi
fi
printf "%-10s %-10s %-22s %-30s \n" "INFO :" "mount_device2 : " "Mounting $DEV on /mnt/$LABEL"
mount $DEV /mnt/$LABEL
else
printf "%-10s %-10s %-22s %-30s \n" "INFO :" "mount_device2 : " "$DEV already mounted on $DUMMY"
fi
case $OPTION in
CHROOT) [[ ${VERBOSE:=0} -eq 1 ]] && echo "mount chroot $*"
do_chroot /mnt/$LABEL;;
NONE) [[ ${VERBOSE:=0} -eq 1 ]] && echo "mount normal $*" ;;
*) printf "%-10s %-10s %-22s %-30s \n" "ERROR :" "mount_device2 : " "Error in running option arguments"
esac
}
do_chroot() {
local __MNT
local __DUMMY
__MNT=$1
__DUMMY=$(df -h $__MNT | grep $__MNT) > /dev/null 2>&1
if [ "${__DUMMY:=EMPTY}" == "EMPTY" ]
then
printf "%-10s %-10s %-22s %-30s \n" "WARNING" "do_chroot" "$__MNT suspisious, not a FS"
fi
printf "%-10s %-10s %-22s %-30s \n" "INFO" "do_chroot" "Mounting /proc /sys /dev, your original kernel is $(uname -r)"
mount -t proc none $__MNT/proc
mount -t sysfs none $__MNT/sys
mount -o bind /dev $__MNT/dev
printf "%-10s %-10s %-22s %-30s \n" "INFO" "do_chroot" "chrooting to $__MNT, quit with exit or ^D"
chroot $__MNT /bin/bash
umount $__MNT/proc $__MNT/sys $__MNT/dev
printf "%-10s %-10s %-22s %-30s \n" "INFO" "do_chroot" "exiting chroot $__MNT, umount sys devices"
exit 0
}
mount_device() {
if [ $# -ne 2 ] ;
then
printf "%-10s %-10s %-22s %-30s \n" "ERROR :" "mount_device : " "Arg not OK $#"
return
fi
DEV=$1
LABEL=$2
DUMMY=$(df -t ext4 | grep -w $DEV | awk '{print $NF}')
NBCHAR=$(echo $DUMMY | wc -c)
if [ $NBCHAR -eq 1 ] ;
then
if [ ! -d /mnt/$LABEL ] ;
then
mkdir /mnt/$LABEL
if [ $? -ne 0 ] ;
then
printf "%-10s %-10s %-22s %-30s \n" "ERROR :" "mount_device : " "Error bulding /mnt/$LABEL"
return
fi
fi
printf "%-10s %-10s %-22s %-30s \n" "INFO :" "mount_device : " "Mounting $DEV on /mnt/$LABEL"
mount $DEV /mnt/$LABEL
else
printf "%-10s %-10s %-22s %-30s \n" "INFO :" "mount_device : " "$DEV already mounted on $DUMMY"
fi
}
purge() {
printf "%-10s %-10s %-22s %-30s \n" "WARNING :" "mount_device : " "purge command not yet implemented"
}
umount_all() {
LIST=$(df | grep /mnt | awk '{print $NF }')
if [ "A$LIST" == "A" ];
then
printf "%-10s %-10s %-22s %-30s \n" "INFO :" "Nothing to unmount"
exit
fi
for i in $(echo $LIST) ;
do
printf "%-10s %-10s %-22s %-30s \n" "INFO :" "umounting filesystem" "$i"
umount $i
done
}
MNT=/mnt
DEBUG=0
TYPE="NONE"
MOUNT=0
SINGLE=__NONE__
LABEL=ext4
CHROOT=__NONE__
ZDEBUG=0
VERBOSE=0
while getopts C:dhlmps:uvZ sarg
do
case $sarg in
C) CHROOT=$OPTARG ;;
d) set -x
DEBUG=1 ;;
h) usage
end;;
l) TYPE=linux
;;
p) purge
;;
m) MOUNT=1
TYPE=linux
;;
s) SINGLE=$OPTARG
;;
u) umount_all
exit
;;
v) VERBOSE=1 ;;
Z) ZDEBUG=1 ;;
*) echo "ERROR : $PRGNAME : Bad option or misusage"
usage
end ;;
esac
done
OPTIND=1
COMMAND="blkid"
if [ ${CHROOT:=__NONE__} != "__NONE__" ];
then
DEV=$(get_valid_devname $CHROOT)
if [ $? -ne 0 ]
then
printf "%-10s %-10s %-22s %-30s \n" "ERROR :" "$PRGNAME : " "device for label $CHROOT not found"
exit 1
fi
#DEV=$(blkid -L $CHROOT)
DEV=$(get_valid_devname $CHROOT)
[[ $? -ne 0 ]] && end $?
mount_device2 -C $DEV $CHROOT
end
fi
if [ "A$SINGLE" != "A__NONE__" ] ;
then
DEV=$(get_valid_devname $SINGLE)
if [ $? -ne 0 ]
then
printf "%-10s %-10s %-22s %-30s \n" "ERROR :" "$PRGNAME : " "device for label $SINGLE not found"
exit 1
fi
#get_valid_devname $SINGLE
#DEV=$(blkid -L $SINGLE)
DEV=$(get_valid_devname $SINGLE)
mount_device2 $DEV $SINGLE
end
fi
if [ $TYPE == "linux" ] ;
then
COMMAND="$COMMAND -t TYPE=ext4 -s LABEL"
else
COMMAND="$COMMAND -s LABEL"
fi
eval $COMMAND | while read line ;
do
DEV=$(echo $line | cut -d : -f 1)
LAB=$(echo $line | cut -d = -f 2 | sed s/\"//g)
if [ $MOUNT -eq 0 ] ;
then
printf "%-20s %-20s \n" "$DEV" "$LAB"
else
mount_device $DEV $LAB
fi
done