probepart 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/bin/sh
  2. #Barry Kauler www.puppylinux.com
  3. #LGPL 2007 Puppy Linux www.puppylinux.com
  4. #17 june 2007
  5. #21 June 2007 BK:
  6. #some usb adaptors have slots for cards, plugging/unplugging these cards
  7. #does not cause a hotplug event and the kernel does not update /proc.
  8. #'disktype' does a direct hardware probe which forces /proc update.
  9. #attempt here to run disktype when appropriate.
  10. #v3.93 10 dec 2007 BK: updated for 2.6.24 kernel, no /dev/hd*.
  11. #v3.97 31jan2008 BK: refinement for detecting kernel with /dev/hd support.
  12. #v3.97 25feb2008 BK: guess_fstype does not work when testing 'makebootfat', use fdisk.
  13. #v4.01 19may2008 BK: bugfix for 2.6.25.4, ram entries in /proc/partitions.
  14. #v403 fix for SIZE of drives without partitions.
  15. #v406 support for old kernel, /proc/ide, /dev/hd*
  16. #v407 improved filesystem probe for optical discs.
  17. #110126 remove code for SATADRIVES (not used in PUPSTATE anymore).
  18. #120601 jemimah: fallback to use 'blkid' to find f.s. type. 120601 revert use of blkid, too slow.
  19. #120602 kernel 3.2.18 major deviance from earlier kernels, /proc/partitions (and /sys/block) now shows /dev/sr0 when no disk.
  20. # new situation, getting duplicate /dev/sr0 so need 'sort -u'.
  21. #120516 raspi, guess_fstype fails for ext4 and swap f.s., did detect fat. fallback blkid.
  22. #120516 sort in old coreutils (as in wary/racy) does not have -V option
  23. #130127 early kernels do not have sr0/sr1 in /proc/partitions, 3.2+ do, which can upset things. (tested 2.6.32.29 and 3.2.29+, so not sure exact version this difference came in)
  24. #130128 improve detection of optical: distinguish iso9600, udf. no o/p if no media inserted.
  25. # previously, any optical media returned "iso9660", now have "udf" (also for video dvds), "audiocd" (audio cds), "none" (unknown f.s.).
  26. #130526 Karl Godt: fix blkid parsing.
  27. . /etc/rc.d/PUPSTATE
  28. SUNITS="$1" #allowed params are '-k' or '-m'.
  29. OUTPUT=""
  30. if [ -f /root/.usb-drive-log-probepart ];then #force /proc upate mechanism
  31. for ONEUSBDRV in `cat /root/.usb-drive-log-probepart | tr '\n' ' '`
  32. do
  33. #disktype /dev/$ONEUSBDRV > /dev/null 2>&1
  34. dd if=/dev/$ONEUSBDRV of=/dev/null bs=512 count=1 >/dev/null 2>&1 #v3.97 faster.
  35. done
  36. fi
  37. #devices that have partitions...
  38. #([^k] is to eliminate mmcblk0 device -- allow mmcblk0p1 etc) v4.01 bugfix eliminate ram...
  39. #130127 [^kr] screens out sr0-sr9. early kernels do not have these in /proc/partitions, 3.2+ do, which causes desktop icon to not appear when audio-cd inserted.
  40. PARTITIONS="`grep '^ .*[^kr][0-9]$' /proc/partitions | tr -s ' ' | cut -f 4-5 -d ' ' | grep -vE ' loop| ram'`" #each line ex: 16076800 sda5
  41. PARTNAMES="`echo "$PARTITIONS" | cut -f 2 -d ' '`" #120602
  42. #all disk devices...
  43. #note, /proc/diskstats only updated when a disk accessed, so devs may be missing...
  44. #NO, NO, NO, /sys is very flakey for hd devices...
  45. ALLDRVS="`ls -1 /sys/block | grep -E '^scd|^sd|^mmc|^sr'`"
  46. [ -e /proc/ide ] && ALLDRVS="${ALLDRVS}
  47. `ls -1 /proc/ide | grep '^hd'`"
  48. #all drives and partitions... 120602 sort...
  49. #120516 sort in old coreutils (as in wary/racy) does not have -V option...
  50. COREUTILSVER="$(sort --version | head -n 1 | rev | cut -f 1 -d ' ' | rev)"
  51. if vercmp $COREUTILSVER le 6.12;then
  52. ALLDEVS="`echo "${PARTNAMES}
  53. ${ALLDRVS}" | tr '\n' ' '`"
  54. else
  55. ALLDEVS="`echo "${PARTNAMES}
  56. ${ALLDRVS}" | sort -V -u | tr '\n' ' '`"
  57. fi
  58. for ONEDEV in $ALLDEVS
  59. do
  60. MEDIATYPE="" #130128
  61. FSTYPE="unknown"
  62. DPATTERN=' '${ONEDEV}'$'
  63. SIZE=`echo "$PARTITIONS" | grep "$DPATTERN" | cut -f 1 -d ' '`
  64. DEVICE="`echo "$PARTITIONS" | grep "$DPATTERN" | cut -f 2 -d ' '`"
  65. if [ ! $SIZE ];then
  66. [ "`echo "$PARTITIONS" | grep "$ONEDEV"`" != "" ] && continue
  67. #must be a device without partitions...
  68. SIZE=0
  69. DEVICE="$ONEDEV"
  70. case $DEVICE in
  71. hd*)
  72. [ "`cat /proc/ide/$DEVICE/media`" = "cdrom" ] && MEDIATYPE="optical" #130128
  73. ;;
  74. scd*|sr*) #usb,sata,scsi cd/dvd drive.
  75. MEDIATYPE="optical" #130128
  76. ;;
  77. esac
  78. if [ -e /sys/block/${DEVICE}/size ];then
  79. SIZE=`cat /sys/block/${DEVICE}/size`
  80. SIZE=$(($SIZE/2)) #get KB.
  81. fi
  82. #for hd* or sd* superfloppy, determine size (avoid probing optical drive)...
  83. if [ "$MEDIATYPE" = "" -a $SIZE -eq 0 ];then #130128
  84. BSIZE=`disktype /dev/$DEVICE 2>/dev/null | grep '^Block device' | cut -f 2 -d '(' | cut -f 1 -d ' '`
  85. [ $BSIZE ] && SIZE=$(($BSIZE/1024)) #KB
  86. fi
  87. if [ "$MEDIATYPE" = "optical" ];then #130128
  88. opticalPATTERN='^/dev/'"$DEVICE"
  89. if [ "`grep "$opticalPATTERN" /proc/mounts`" = "" ];then
  90. cddetect_quick -d/dev/${DEVICE} >/dev/null 2>&1 #very fast.
  91. #...returns 0 if disc inserted, else 255.
  92. [ $? -ne 0 ] && continue #130128 go no further, as no media inserted.
  93. fi
  94. fi
  95. fi
  96. FSTYPE="`guess_fstype /dev/$DEVICE 2>/dev/null`" #130128 note, audio-cd returns "unknown", as no f.s.
  97. if [ "$FSTYPE" = "unknown" -a "$MEDIATYPE" = "optical" ];then #130128 probe optical a bit more.
  98. cddetect -q -d/dev/${DEVICE} > /dev/null 2>&1
  99. case $? in
  100. 1) FSTYPE="audiocd" ;;
  101. esac
  102. fi
  103. #v3.97 guess_fstype fails sometimes...
  104. #(using makebootfat to setup a USB-FLOPPY/-HDD/-ZIP combined bootable FAT drive).
  105. xFSTYPE=''
  106. if [ "$FSTYPE" = "unknown" -a "$MEDIATYPE" = "" ];then #130128 not optical.
  107. fsPATTERN='^/dev/'"$DEVICE"' '
  108. xDEVICE="`echo -n "$DEVICE" | sed -e 's/[0-9]*$//' -e 's%p$%%'`" #"${DEVICE/[0-9]/}" #remove partition number. 120516 remove 'p' from mmcblk0p1
  109. xFSTYPE="`fdisk -l /dev/$xDEVICE 2>/dev/null | grep "$fsPATTERN" | head -n 1 | grep -o -E 'FAT|swap|Linux|NTFS'`" #120516 FAT12$|FAT16$|FAT32$
  110. case $xFSTYPE in #120516
  111. FAT) FSTYPE='vfat' ;;
  112. swap) FSTYPE='swap' ;;
  113. NTFS) FSTYPE='ntfs' ;;
  114. Linux)
  115. FSTYPE=$(blkid -c /dev/null /dev/${DEVICE} | grep -w "${DEVICE}" | grep -o ' TYPE=".*"' | cut -f2 -d'"')
  116. [ "$FSTYPE" = "" ] && FSTYPE="unknown"
  117. ;;
  118. esac
  119. fi
  120. [ "$FSTYPE" = "unknown" ] && FSTYPE="none"
  121. [ "$SUNITS" = "" ] && SIZE=$(($SIZE*2)) #want 512 byte blocks.
  122. [ "$SUNITS" = '-m' ] && SIZE=$(($SIZE/1024)) #want MB
  123. echo "/dev/$DEVICE|$FSTYPE|$SIZE"
  124. #keep record of usb sd*, for forced updating of /proc...
  125. case $DEVICE in
  126. sd*)
  127. #log if usb drive (not a ata drive)...
  128. DEVDRV="`echo -n "$DEVICE" | tr -d '[0-9]'`"
  129. if [ "`echo "$ATADRIVES" | grep "$DEVDRV"`" = "" ];then #110126
  130. echo "$DEVDRV" >> /root/.usb-drive-log-probepart
  131. sort -u /root/.usb-drive-log-probepart > /tmp/usb-drive-log-probepart-tmp
  132. mv -f /tmp/usb-drive-log-probepart-tmp /root/.usb-drive-log-probepart
  133. fi
  134. ;;
  135. esac
  136. done
  137. ###END###