This added the -A --all option to show everything like ram and unused loop disks.
Code: Select all
#!/bin/bash
###INTRO
# Karl Reimer Godt, March 2013
# /sbin/probepart originally written by
#
# Barry Kauler www.puppylinux.com
# LGPL 2007 Puppy Linux www.puppylinux.com
#
# Different code for /sbin/probepart
# that is used by
# /sbin/pup_event_frontend_d
# ,
# /usr/sbin/pmount
# ,
# /usr/local/bin/drive_all
# and
# /usr/sbin/shutdownconfig
# and probably some more .
#
# May output fat as Fat[16|32] by disktype, not as vfat
# so take this into account if these scripts don't
# show your fat partitions.
# Also omitting the /root/.usb-drive-log-probepart file
# , the forcing of USB by
# dd if=/dev/$ONEUSBDRV of=/dev/null bs=512 count=1 >/dev/null 2>&1 #v3.97 faster.
# and unfortunately
# /usr/lib/mut/bin/guess_fstype
# since the developer jesse had not ported it to support ext4.
#
# Thanks very much to Keef for patience while testing and
# helping me detecting some wrong formatted code, confirmations
# and to keep pmount happier.
# Thanks muchly to wjaguar to remind/teach me to use the read command more effectively,
# which solved problems posted by Keef and also encountered by me, and did
# increase the speed reasonably.
# Also thanks to Ted Dog and headfound for company.
###ENDINTRO
##HEAD
trap "exit 1" HUP INT QUIT KILL TERM
OUT=/dev/null;ERR=$OUT
[ "$DEBUG" ] || DEBUG=`sed 's% %\n%g' /proc/cmdline | grep -w -i '^debug'`
[ "$DEBUG" ] && { OUT=/dev/stdout;ERR=/dev/stderr; }
Version='2.0'
usage(){
USAGE_MSG="
$0 [ PARAMETERS ]
example usage : DEBUG=1 probepart -d/dev/sdc1 /dev/sdd1 -m
PARAMETERS:
-V|--version : showing version information
-H|--help : show this usage information
-A|--all : show all disks noted in /proc/diskstats like ram|loop|mtd|md
##edit1: added following size formats:
-b : show size in bits (default)
-B : Bytes
-kb : 1000-kilo bits
-mb : 1000-mega bits
-gb : 1000-giga bits
-kib : 1024-kilo bits
-mib : 1024-mega bits
-gib : 1024-giga bits
-KB : 1000-kilo Bytes
-MB : 1000-mega Bytes
-GB : 1000-giga Bytes
-KIB : 1024-kilo Bytes
-MIB : 1024-mega Bytes
-GIB : 1024-giga Bytes
-k : show sizes in kilobytes (KiB) #compat.
-m : show sizes in megabytes (MiB) #compat.
Without one of the above parameters it is the value found in
/sys/class/block/drive/size
; usually as blocksize=512 formatted to bits .
##edit2: added the forgotten -d parameter to check only given partitions
-d/dev/sdaX [/dev/sdaY ..] : output only for given partitions
$2
"
echo "$USAGE_MSG"
exit $1
}
[ "`echo "$1" | grep -wiE "help|\-H"`" ] && usage 0
[ "`echo "$1" | grep -wiE "version|\-V"`" ] && { echo "$0 -version $Version";exit 0; }
(mount |grep -o "/proc " >$OUT 2>$ERR && mount |grep -o "/sys " >$OUT 2>$ERR) || usage 1 '/proc or /sys not mounted!' ##+++edit4 sanity check
##
SIZE_FACTOR=1
SIZE_PARAM=`echo "$@" | grep -o -m1 -w -i -E '\-k|\-m|\-b|\-[kmg]b|\-[kmg]ib' | head -n1` ##-+edit2
[ "$SIZE_PARAM" = '-b' ] && SIZE_FACTOR=1
[ "$SIZE_PARAM" = '-B' ] && SIZE_FACTOR=8
[ "$SIZE_PARAM" = '-kb' ] && SIZE_FACTOR=$((1*1000))
[ "$SIZE_PARAM" = '-mb' ] && SIZE_FACTOR=$((1*1000*1000))
[ "$SIZE_PARAM" = '-gb' ] && SIZE_FACTOR=$((1*1000*1000*1000))
[ "$SIZE_PARAM" = '-KB' ] && SIZE_FACTOR=$((8*1000))
[ "$SIZE_PARAM" = '-MB' ] && SIZE_FACTOR=$((8*1000*1000))
[ "$SIZE_PARAM" = '-GB' ] && SIZE_FACTOR=$((8*1000*1000*1000))
[ "$SIZE_PARAM" = '-kib' ] && SIZE_FACTOR=$((1*1024))
[ "$SIZE_PARAM" = '-mib' ] && SIZE_FACTOR=$((1*1024*1024))
[ "$SIZE_PARAM" = '-gib' ] && SIZE_FACTOR=$((1*1024*1024*1024))
[ "$SIZE_PARAM" = '-KIB' ] && SIZE_FACTOR=$((8*1024))
[ "$SIZE_PARAM" = '-k' ] && SIZE_FACTOR=$((8*1024)) ##compat.
[ "$SIZE_PARAM" = '-MIB' ] && SIZE_FACTOR=$((8*1024*1024))
[ "$SIZE_PARAM" = '-m' ] && SIZE_FACTOR=$((8*1024*1024)) ##compat.
[ "$SIZE_PARAM" = '-GIB' ] && SIZE_FACTOR=$((8*1024*1024*1024))
DEVICES=`echo "$@" | grep -o '\-d.*' |sed 's%-[[:alnum:]]*%%g;s%/dev/%%g'` ##+++edit2
SHOWALL=`echo "$@" | grep -oE '\-A|\-\-all' |tr -d '-'` ##+++edit3
##ENDHEAD
disktype_func(){
disktype /dev/"$1" |grep -iE -o '.* file system|swap|squashfs' |tail -n1 |awk '{print $1}' |tr '[A-Z]' '[a-z]' ##+edit2 support detection of squashfs
}
filesystem_func(){ ##+++edit1
[ "$FSTYPE" ] || FSTYPE=`guess_fstype /dev/"$1" 2>$ERR`
[ "$FSTYPE" = 'unknown' ] && FSTYPE=`disktype_func "$1"` ##-+edit2
[ "$FSTYPE" ] || FSTYPE='none or unknown' ##-+edit2 added none to please pmount thanks to Keef
}
cddetect_func(){ ##+++edit1
cddetect_quick -d/dev/"$1" >$OUT && FSTYPE=`disktype_func "$1"` || FSTYPE='none or not inserted' ##+edit3 none to please pmount thanks to Keef
}
present_removable(){
cddetect_func "$drive"
filesystem_func "$drive"
echo "/dev/$drive|$FSTYPE|$SIZE"
}
FDISK=`fdisk -l | grep '^/dev' |tr -s ' ' |sed 's%\(/dev/[[:alpha:]]*[0-9]*\) \([[:digit:]]*\) \(.*\)%\1 N \2 \3%'` ##=+-edit2 : sed + bash and the # sign ..
##MAIN
while read maj min drive rest; do
[ "$maj" -a " $min" -a "$drive" -a "$rest" ] || continue ##+++edit4
#[ "`echo "$line" | grep '^[[:digit:]]'`" ] || continue ##---edit4
#drive=`echo $line |awk '{print $3}'` ##---edit4 out again, thanks to wjaguar
[ "$drive" ] || continue ##+++edit3
[ "$SHOWALL" ] || { [ "`echo $drive | grep -vE 'ram|loop|mtd|md'`" ] || continue; } ##+++edit3 added --all option
[ -b /dev/$drive ] || {
#maj="${line%% *}" ; min=`echo $line |awk '{print $2}'`; ##---edit4 thanks to wjaguar, no need for this anymore
[ "$DEBUG" ] && echo "'$maj' '$min' '$drive'" >&2;
[ "$maj" -a "$min" -a "$drive" ] && mknod /dev/$drive b $maj $min 2>$ERR || continue; } ##+++edit2 : create nodes if not exists, older Puppy's static /dev/ directory supported only up to 15 partitions for each drive, newer only for sda up to 23. ##+edit3 check for maj min not NULL
[ "$DEVICES" ] && { [ "`echo "$DEVICES" | grep -w "$drive"`" ] || continue; } ##+++edit2 forgot to implement it
SIZE='';FSTYPE='';
[ -f /sys/class/block/$drive/size ] || continue
SIZE=$((`cat /sys/class/block/$drive/size`*512*8)) ##want bits
[ "$SIZE" = $((2*8*512)) ] && { SIZE=$((`echo "$FDISK" |grep -w "^/dev/$drive" | awk '{print $5}' |sed 's%[^[:digit:]]%%g'`*2*512*8));FSTYPE=`echo "$FDISK" |grep -w "^/dev/$drive" |cut -f7- -d ' '`'(none)'; } ##+ edit2 added (none) to please pmount
SIZE=$((SIZE/SIZE_FACTOR))
REMOVEABLE=0
if [[ "${drive}" =~ [0-9] ]]; then ## was '[0-9]' : bash-3.00 accepts it, bash-3.2 not
if [ -d /sys/class/block/$drive/device ]; then
REMOVEABLE=`cat /sys/class/block/$drive/removable`
fi
[ "$REMOVEABLE" = 0 ] || cddetect_func "$drive"
filesystem_func "$drive"
echo "/dev/$drive|$FSTYPE|$SIZE" ##edit1 wrong order for pmount
else #whole drive
if [ -d /sys/class/block/$drive/device ]; then
REMOVEABLE=`cat /sys/class/block/$drive/removable`
if [ "$REMOVEABLE" = 1 ]; then
if [ -f /sys/class/block/$drive/device/type ]; then ##+++edit3
if [ "`cat /sys/class/block/$drive/device/type`" = 5 ]; then
present_removable
fi
else ##+++edit3
present_removable
fi
fi;fi
fi
#done</proc/partitions
done</proc/diskstats ##-+edit3
*TODO : Look into the kernel source about which drives get which hex ( usb, cd, ..)
EDIT1 :
* Changed output order from DEVICE|SIZE|FSTYPE to DEVICE|FSTYPE|SIZE thanks to Keef.
* If no CDDRIVE attached, ls output had been empty and the code tried to find files for /sys/class/block//dev/ which don't exist.
* Put repeatedly used code into functions.
* Added more code to give more size formatting output possibilities, ie: -MB and -MIB .
EDIT2 :
* added note that the #110919 implemented /usr/sbin/shutdownconfig uses probepart too.
* sed on bash-3.2 failed using #numbersign as delimiter, treated it as comment, resulting in screwing the script. I used to use # because it is easy to type, and it is seldom that sed should edit a commented line.
* added the forgotten -d option to present only given partitions.
* added support to detect squashfs.
* fixed an error in edit1, which passed a too long argument from cddetect_func to disktype_func .
* force the creation of device nodes, if their number exceeds the supported by the static /dev directory (important for attached drives with more than 15/23 partitions).
* added to append the term 'none' to extended partitions so they don't show up in pmount.
* some minor changes to use shell builtins than external commands.
* minor security fix in case of aliases in /etc/profile or .bashrc : had ls -F alias on one partition, making mknod choke.
EDIT3 :
* Complete rewrite to use /proc/diskstats for handling hdb IDE-Kernel CD -drives.
* Added --all option to show everything in /proc/diskstats .
* Better handling of the mknod part.
* Add a second time none to cd-drives to please pmount.
* Obsoleted the second drives loop now using diskstats file.
EDIT4 :
* Use read val1 val2 val3 rest instead of read line to avoid usage of awk. This seems to fix crashes at first usages.
* extra check if /proc and /sys are mounted
* extra check for /sys/class/block/drive/size is there