Features added :
Support for /dev/fd* floppy drives .
usage message if called with -h parameter
shell builtin getopts
Supported Parameters :
- -V : Shows version information.
-H|-h : Shows this usage.
-E : Use dc external binary if option -S applies.
-M : Shorter machine readable output for to be used in scripts.
-P : Show partition numbers.
-p : Pretty and long output for terminal usage.
-S : Show drive sizes in 1000/1024 format.
-s : Shorter output.
Example output :
bash-3.00# probedisk2E -M
- fd0|floppy|Generic 1440kb FD|4/4 K
sda|drive|ATA ST500DM002-1BD14|500/465 G
sdb|drive|ATA ST1000DM003-9YN1|1000/931 G
sdc|usbdrv|HUAWEI MMC Storage|1/1 G
sdd|usbdrv|HitachiG ST|500/465 G
sr0|optical|PLDS DVD-ROM DH-16D3S|1073/1023 M
sr1|optical|HL-DT-ST DVD-RAM GH10L|1073/1023 M
sr2|optical|HUAWEI Mass Storage|78/74 M
Code: Select all
#!/bin/sh
#LGPL 2007 Puppy Linux www.puppyos.com
#based on probedisk3 written by Dougal.
#small mods by BK 16 june 2007
# 21 Jun 2007 BK: force /proc update for usb drives.
#v3.93 10 dec 2007 BK: updated for 2.6.24 kernel, no /dev/hd*.
#v3.97 31jan2008 BK: refinement for detecting kernels with /dev/hd* drives.
#v4.01 10may2008 BK: bugfix for detecting if usb drive.
#v4.01 10may2008 BK: new 2nd-param categories. named 'probedisk2'.
#v4.01 21may2008 BK: zip and ls120 now categorized as 'floppy'.
#v4.02 9jun08 BK: correct detection of usb floppy.
#v403 fixed excessive spaces in description field.
#v406 support for old kernel, /proc/ide, /dev/hd*.
###KRG Fr 31. Aug 23:34:58 GMT+1 2012
trap "exit 1" HUP INT QUIT KILL TERM
OUT=/dev/null;ERR=$OUT
[ "$DEBUG" ] && { OUT=/dev/stdout;ERR=/dev/stderr; }
[ "$DEBUG" = "2" ] && set -x
Version='1.1' ##added partitions
Version='1.2E' ##added function_size, moved code
usage(){
USAGE_MSG="
$0 [ PARAMETERS ]
-V : Shows version information.
-H|-h : Shows this usage.
-E : Use dc external binary if option -S applies.
-M : Shorter machine readable output for to be used in scripts.
-P : Show partition numbers.
-p : Pretty and long output for terminal usage.
-S : Show drive sizes in 1000/1024 format.
-s : Shorter output.
******* ******* ******* ******* ******* ******* ******* ******* *******
$2"
echo "$USAGE_MSG"
exit $1
}
###KRG Fr 31. Aug 23:34:58 GMT+1 2012
###KRG Mon Jul 8 22:35:19 GMT+1 2013
function_format_size(){
if [ "$USE_DC" ] ; then
SIZE_I=`dc $blocks_1024 $F1024 \/ p`
SIZE_K=`dc $blocks_1000 $F1000 \/ p`
else
SIZE_I=$(($blocks_1024 / $F1024))
SIZE_K=$(($blocks_1000 / $F1000))
fi
SIZES="|${SIZE_K}/${SIZE_I} $USE_C"
}
function_size(){
if [ -f /sys/block/$ONEDRV/size ] ; then
read blocks_512 </sys/block/$ONEDRV/size
blocks_1024=$(($blocks_512/2))
blocks_1000=$((($blocks_512 * 512) / 1000 ))
if [ "$blocks_1024" -gt $((1024*1024)) ] ; then
F1024=$((1024*1024)) F1000=$((1000*1000)) USE_C=G function_format_size
elif [ "$blocks_1024" -gt 1024 ] ; then
F1024=1024 F1000=1000 USE_C=M function_format_size
else
F1024=1 F1000=1 USE_C=K function_format_size
fi
else
SIZES='|not available'
fi
}
function_partitions(){
PARTITIONS='|'`ls -1 /sys/class/block/$ONEDRV |grep -o -e '[[:digit:]]*' |sort -n |tr '\n' ' '`
}
PRETTY=YES;DICE=/dev/
while getopts hEMsSpPV option ; do
case $option in
E) USE_DC=YES ;; ## Exact count
h|H) usage 0 ;;
M) MACHINE=YES;PRETTY='';NO_CATEG='';USE_SIZES=YES;USE_PARTS='' ;; # pup_event OUPUT style
s) NO_CATEG=YES ;; ## omit $CATEGORY
S) USE_SIZES=YES ;; ## show sizes of drives
p) PRETTY=YES;MACHINE='';USE_DC=YES;USE_SIZES=YES;USE_PARTS=yes ;; # Pretty OUTPUT style
P) USE_PARTS=YES ;; ## show partitions
V) echo "$0 -version $Version";exit 0 ;;
esac
done
[ "$PRETTY" ] || DICE='';
function_output(){
[ "$NO_CATEG" ] && INFO="$MODEL" || INFO="$VENDOR $MODEL"
INFO=`echo $INFO | sed '/^$/d'`
[ "$INFO" ] && INFO="|$INFO" || INFO='|?'
[ "$NO_CATEG" ] && MEDIA=''
[ "$USE_SIZES" ] && function_size
[ "$USE_PARTS" ] && function_partitions
echo ${DICE}${ONEDRV}${MEDIA}${INFO}${SIZES}${PARTITIONS}
}
###KRGMon Jul 8 22:35:19 GMT+1 2013
. /etc/rc.d/PUPSTATE
#ATADRIVES is all ide/sata drives (not usb) (which all have /dev/sd notation).
#if old kernel supporting /dev/hd* then using SATADRIVES variable...
INTERNAL_SD_DRIVES="$SATADRIVES"
[ "$ATADRIVES" != "" ] && INTERNAL_SD_DRIVES="$ATADRIVES"
if [ -f /root/.usb-drive-log-probedisk ];then #force /proc upate mechanism
for ONEUSBDRV in `cat /root/.usb-drive-log-probedisk`;
do
#disktype /dev/$ONEUSBDRV > /dev/null 2>&1
[ -b /dev/$ONEUSBDRV ] && dd if=/dev/$ONEUSBDRV of=/dev/null bs=512 count=1 1>$OUT 2>$ERR #v4.01 faster.
done
fi
if [ ! "$MACHINE" ] ; then
#mounted drives/partitions...
MNTDDEVS=`awk '{print $1}' /proc/mounts | grep -o -E '[fhs]d.*|sr.*|scd.*|mmc.*'`
fi
#crap, right now, /sys/block does not show my hdb cd/dvd drive, but it is in
#/proc/ide. pathetic kernel! oh well...
if [ ! -e /proc/ide ];then #v3.97
ALLDRVS=`ls -1 /sys/block | grep -E '^scd|^sd|^mmc|^sr|^fd'`
else
ALLDRVS=`ls -1 /sys/block | grep -E '^scd|^sd|^mmc|^sr|^fd' | tr '\n' ' '``ls -1 /proc/ide | grep '^hd'`
fi
#note: after further testing, the 2.6.21.5 kernel exhibits inconsistent behaviour for hd devices, best to avoid old CONFIG_IDE kernel driver.
for ONEDRV in $ALLDRVS
do
case $ONEDRV in
hd*) # ide device, look in /proc/ide for info
read MEDIA </proc/ide/$ONEDRV/media
[ "$MEDIA" = "disk" ] && MEDIA="drive"
[ "$MEDIA" = "cdrom" ] && MEDIA="optical"
read INFO </proc/ide/$ONEDRV/model
;;
sd*) # scsi devices, look in /sys/block (all appear as Direct-Access)
MEDIA="|drive"
read VENDOR </sys/block/$ONEDRV/device/vendor
read MODEL </sys/block/$ONEDRV/device/model
DRVNAMES="$DRVNAMES `echo -n "$ONEDRV" | cut -b 1-3` "
#log if usb drive (not a sata drive)... v4.01...
#if [ "`echo "$INTERNAL_SD_DRIVES" | grep "$ONEDRV"`" = "" ];then
if [ "`find /sys -name $ONEDRV | grep 'usb' | grep 'block'`" ] ; then
MEDIA="|usbdrv" #v4.01
echo "$ONEDRV" >> /root/.usb-drive-log-probedisk
sort -u /root/.usb-drive-log-probedisk > /tmp/usb-drive-log-probedisk-tmp
mv -f /tmp/usb-drive-log-probedisk-tmp /root/.usb-drive-log-probedisk
#find out if a usb floppy drive...
if [ -e /sys/block/${ONEDRV}/size ];then
[ "`cat /sys/block/${ONEDRV}/size`" = "2880" ] && MEDIA="|floppy"
fi
if [ -e /sys/block/${ONEDRV}/device/model ];then
[ "`grep -E ' FDD| UF000x|Floppy|USB\-FDU|^FD\-|FLOPPY' /sys/block/${ONEDRV}/device/model`" ] && MEDIA="|floppy"
fi
#find out if it is a removable internal drive (zip, ls120, etc)...
elif [ -e /sys/block/${ONEDRV}/removable ];then
[ "`cat /sys/block/${ONEDRV}/removable`" = "1" ] && MEDIA="|floppy"
fi
;;
scd*|sr*) # scsi cdroms
MEDIA="|optical"
read VENDOR </sys/block/$ONEDRV/device/vendor
read MODEL </sys/block/$ONEDRV/device/model
;;
mmc*) #/dev/mmcblk0
MEDIA="|card"
VENDOR='MMC/SD:'
read MODEL </sys/block/$ONEDRV/device/name
;;
fd*)
MEDIA='|floppy'
VENDOR='Generic'
MODEL='1440kb FD'
;;
*)
MEDIA='|unknown'
VENDOR='?'
;;
esac
function_output
done
if [ ! "$MACHINE" ] ; then
#find out if a mounted device has been unplugged...
#for hotplug drives, remove it and it will disappear from /sys/block, however
#still shows up in 'mount' if hasn't been unmounted.
for ONEMNTD in $MNTDDEVS
do
case $ONEMNTD in
hd*|sd*|sr*|fd*)
MNTDDRV="`echo -n "$ONEMNTD" | cut -b 1-3` "
;;
scd*)
MNTDDRV="`echo -n "$ONEMNTD" | cut -b 1-4` "
;;
mmc*)
MNTDDRV="`echo -n "$ONEMNTD" | cut -b 1-7` "
;;
esac
#prints to system log and to stderr...
[ "`echo "$ALLDRVS" | grep "$MNTDDRV"`" ] || logger -s "PROBEDISK WARNING: $ONEMNTD UNPLUGGED WITHOUT UNMOUNTING it FOREHAND !"
done
fi
###END###
exit 0 #force exitcode for klibc K.Almquist dash/sh
There might be some code changes due to my main installations are pre - #110126 no longer using SATADRIVES variable in PUPSTATE.
Dropping the hidden /root/.usb*log file , thought dd is really fast .
Dropping the logger part , because nothing uses it, and nothing would inform a user about that, if not run from CLI .
Probably merging probedisk with probepart - so scripts like pmount or *_frontend_d will only need to call one script and not two .