Advertisement
s243a

/sbin/probedisk (tahrpup)

Jan 14th, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.90 KB | None | 0 0
  1. #!/bin/sh
  2. # GPL www.puppylinux.com
  3. # DEVICE|TYPE|DESC
  4.  
  5. #PLUS_SIZE=yes # alternative format: DEVICE|TYPE|DESC|SIZE
  6. #HDPARM=yes
  7. SHOW_USBFLASH=yes
  8. SHOW_EXTENDED_USB=yes
  9.  
  10. device=${@##*/}
  11.  
  12. ###############################################################
  13.  
  14. if [ "$device" ] ; then
  15.     ALLDRVS=${device} #process cli argument
  16.     CLI_ARG=1
  17. else
  18.     if [ -e /proc/ide ] ; then
  19.         ALLDRVS="`ls -1 /sys/block | grep -E '^fd|^scd|^sd|^mmc|^sr' | tr '\n' ' '``ls -1 /proc/ide | grep '^hd' | tr '\n' ' '`"
  20.     else
  21.         ALLDRVS="`ls -1 /sys/block | grep -E '^fd|^scd|^sd|^mmc|^sr' | tr '\n' ' '`"
  22.     fi
  23. fi
  24.  
  25. ###############################################################
  26.  
  27. for DRV in ${ALLDRVS} ; do
  28.  
  29.     if [ ! -b "/dev/$DRV" ] ; then
  30.         echo "${device}: invalid device" 1>&2
  31.         continue
  32.     fi
  33.  
  34.     DRV=${DRV:0:3}
  35.  
  36.     vendor=""
  37.     model=""
  38.     size=""
  39.     removable=""
  40.     usb=""
  41.     [ -f /sys/block/${DRV}/device/vendor ] && read -r vendor < /sys/block/${DRV}/device/vendor
  42.     [ -f /sys/block/${DRV}/device/model ] && read -r model < /sys/block/${DRV}/device/model
  43.     [ -f /sys/block/${DRV}/size ] && read -r size < /sys/block/${DRV}/size
  44.     [ -f /sys/block/${DRV}/removable ] && read -r removable < /sys/block/${DRV}/removable
  45.     info="$vendor $model"
  46.  
  47.     type=drive
  48.     case $(readlink /sys/block/${DRV}) in *usb*) usb=yes ;; esac
  49.  
  50.     if [ -e /sys/block/${DRV}/device/type ] ; then
  51.         # http://lxr.free-electrons.com/source/include/scsi/scsi.h?v=3.12
  52.         case $(cat /sys/block/${DRV}/device/type) in
  53.             0) #define TYPE_DISK 0x00
  54.                 type='drive'
  55.                 [ "$usb" = "yes" ] && type="usbdrv"
  56.                 ;;
  57.             5) #define TYPE_ROM 0x05
  58.                 type='optical' #see below
  59.                 ;;
  60.         esac
  61.     fi
  62.  
  63.     case ${DRV} in
  64.         fd*)  type=floppy  ;;
  65.         scd*) type=optical ;; #old stuff
  66.         mmc*) type=card ; info="MMC/SD: $info" ;;
  67.     esac
  68.  
  69.     # -- legacy: /proc/ide - special case
  70.     if [ -e /proc/ide ] ; then #legacy
  71.         [ -f /proc/ide/${DRV}/model ] && read -r info < /proc/ide/${DRV}/model
  72.         [ -f /proc/ide/${DRV}/media ] && read -r media < /proc/ide/${DRV}/media
  73.         case ${DRV} in
  74.             sd*) # usb hd drives and usb flash drives appear as sdX
  75.                 usb=yes #will be used in the usbflash check
  76.                 type=usbdrv
  77.                 ;;
  78.             hd*) # somtimes optical drives appear as hdX... must check
  79.                 if [ "$media" = "cdrom" ] ; then
  80.                     type=optical # hard to tell what hdX drives are usb optical drives
  81.                 else
  82.                     [ "$removable" = "1" ] && usb=yes #not sure about this.. just in case
  83.                 fi
  84.                 ;;
  85.         esac
  86.     fi
  87.     # --
  88.  
  89.     ## -- legacy: properly identify a floppy
  90.     if [ "$type" = "usbdrv" ] ; then
  91.         # find out if a usb floppy drive...
  92.         [ "$size" = "2880" ] && type=floppy
  93.         # if the floppy diskette not inserted, try this fallback test...
  94.         # some examples: Vendor: NEC Model: USB UF000x Rev: 1.50, Sony USB Floppy Drive, rev 1.10/5.01,
  95.         # MITUMI USB FDD, VenDor: TEAC Model: FD-05PUB, Vendor: COMPAQ Model: USB EXT FLOPPY
  96.         case "$model" in *" FDD"*|*" UF000x"*|*"Floppy"*|*"USB-FDU"*|"FD-"*|*"FLOPPY"*) type=floppy ;; esac
  97.     else
  98.         # find out if it is a removable internal drive (zip, ls120, etc)...
  99.         case ${DRV} in sd*) [ "$removable" = "1" ] && type=floppy ;; esac
  100.     fi
  101.     # might be a "fake" floppy device. have to check..
  102.     [ "$type" = "floppy" -a "$model" = "" -a "$vendor" = "" ] && continue
  103.     # --
  104.  
  105.     case $type in drive|usbdrive) # -hdparm
  106.         if [ "$HDPARM" = "yes" -a "$removable" != "1" ] ; then
  107.             modelx="$(hdparm -i /dev/${DRV} 2>/dev/null | grep Model | cut -f 1 -d ',' | cut -f 2 -d '=' | tr -s ' ')"
  108.             [ "$modelx" != "" ] && info="$vendor $modelx"
  109.         fi
  110.     esac
  111.  
  112.     if [ "$usb" = "yes" ] ; then
  113.         [ "$SHOW_USBFLASH" = "yes" -a "$type" = "usbdrv" -a "$removable" = "1" ] && type='usbflash'
  114.         [ "$SHOW_EXTENDED_USB" = "yes" -a "$type" = "floppy" ] && type='usbfloppy'
  115.         [ "$SHOW_EXTENDED_USB" = "yes" -a "$type" = "optical" ] && type='usboptical'
  116.     fi
  117.  
  118.     # print results
  119.     OUTPUT_STR="/dev/${DRV}|$type|$info"
  120.     [ "$PLUS_SIZE" = "yes" ] && OUTPUT_STR="${OUTPUT_STR}|$size"
  121.     echo "$OUTPUT_STR"
  122. done
  123.  
  124. ### END ###
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement