probepart 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. # guess_fstype really sucks for extended partitions - use fdisk but not in the main loop - too slow #140714
  59. EXTD=''
  60. printf "$ALLDRVS"|while read DISK
  61. do
  62. FDISK="`fdisk -l /dev/"$DISK"`"
  63. EXTD="`echo "$FDISK"|grep -w 'Extended'|grep "$DISK"|awk '{print $1}'`"
  64. EXTD=${EXTD##*/}
  65. [ "$EXTD" ] && echo -n "$EXTD " >> /tmp/extended_parts
  66. done
  67. [ -f /tmp/extended_parts ] && EXTD="`cat /tmp/extended_parts`"
  68. rm /tmp/extended_parts 2>/dev/null
  69. for ONEDEV in $ALLDEVS
  70. do
  71. MEDIATYPE="" #130128
  72. FSTYPE="unknown"
  73. DPATTERN=' '${ONEDEV}'$'
  74. SIZE=`echo "$PARTITIONS" | grep "$DPATTERN" | cut -f 1 -d ' '`
  75. DEVICE="`echo "$PARTITIONS" | grep "$DPATTERN" | cut -f 2 -d ' '`"
  76. if [ ! $SIZE ];then
  77. [ "`echo "$PARTITIONS" | grep "$ONEDEV"`" != "" ] && continue
  78. #must be a device without partitions...
  79. SIZE=0
  80. DEVICE="$ONEDEV"
  81. case $DEVICE in
  82. hd*)
  83. [ "`cat /proc/ide/$DEVICE/media`" = "cdrom" ] && MEDIATYPE="optical" #130128
  84. ;;
  85. scd*|sr*) #usb,sata,scsi cd/dvd drive.
  86. MEDIATYPE="optical" #130128
  87. ;;
  88. esac
  89. if [ -e /sys/block/${DEVICE}/size ];then
  90. SIZE=`cat /sys/block/${DEVICE}/size`
  91. SIZE=$(($SIZE/2)) #get KB.
  92. fi
  93. #for hd* or sd* superfloppy, determine size (avoid probing optical drive)...
  94. if [ "$MEDIATYPE" = "" -a $SIZE -eq 0 ];then #130128
  95. BSIZE=`disktype /dev/$DEVICE 2>/dev/null | grep '^Block device' | cut -f 2 -d '(' | cut -f 1 -d ' '`
  96. [ $BSIZE ] && SIZE=$(($BSIZE/1024)) #KB
  97. fi
  98. if [ "$MEDIATYPE" = "optical" ];then #130128
  99. opticalPATTERN='^/dev/'"$DEVICE"
  100. if [ "`grep "$opticalPATTERN" /proc/mounts`" = "" ];then
  101. cddetect_quick -d/dev/${DEVICE} >/dev/null 2>&1 #very fast.
  102. #...returns 0 if disc inserted, else 255.
  103. [ $? -ne 0 ] && continue #130128 go no further, as no media inserted.
  104. fi
  105. fi
  106. fi
  107. FSTYPE="`guess_fstype /dev/$DEVICE 2>/dev/null`" #130128 note, audio-cd returns "unknown", as no f.s.
  108. echo "$EXTD"|grep -q "$DEVICE" && FSTYPE='unknown' #140714 'Extended' partition, gets changed to 'none'
  109. if [ "$FSTYPE" = "unknown" -a "$MEDIATYPE" = "optical" ];then #130128 probe optical a bit more.
  110. cddetect -q -d/dev/${DEVICE} > /dev/null 2>&1
  111. case $? in
  112. 1) FSTYPE="audiocd" ;;
  113. esac
  114. fi
  115. #v3.97 guess_fstype fails sometimes...
  116. #(using makebootfat to setup a USB-FLOPPY/-HDD/-ZIP combined bootable FAT drive).
  117. xFSTYPE=''
  118. if [ "$FSTYPE" = "unknown" -a "$MEDIATYPE" = "" ];then #130128 not optical.
  119. fsPATTERN='^/dev/'"$DEVICE"' '
  120. xDEVICE="`echo -n "$DEVICE" | sed -e 's/[0-9]*$//' -e 's%p$%%'`" #"${DEVICE/[0-9]/}" #remove partition number. 120516 remove 'p' from mmcblk0p1
  121. xFSTYPE="`fdisk -l /dev/$xDEVICE 2>/dev/null | grep "$fsPATTERN" | head -n 1 | grep -o -E 'FAT|swap|Linux|NTFS'`" #120516 FAT12$|FAT16$|FAT32$
  122. case $xFSTYPE in #120516
  123. FAT) FSTYPE='vfat' ;;
  124. swap) FSTYPE='swap' ;;
  125. NTFS) FSTYPE='ntfs' ;;
  126. Linux)
  127. FSTYPE=$(blkid -c /dev/null /dev/${DEVICE} | grep -w "${DEVICE}" | grep -o ' TYPE=".*"' | cut -f2 -d'"')
  128. [ "$FSTYPE" = "" ] && FSTYPE="unknown"
  129. ;;
  130. esac
  131. fi
  132. [ "$FSTYPE" = "unknown" ] && FSTYPE="none"
  133. [ "$SUNITS" = "" ] && SIZE=$(($SIZE*2)) #want 512 byte blocks.
  134. [ "$SUNITS" = '-m' ] && SIZE=$(($SIZE/1024)) #want MB
  135. echo "/dev/$DEVICE|$FSTYPE|$SIZE"
  136. #keep record of usb sd*, for forced updating of /proc...
  137. case $DEVICE in
  138. sd*)
  139. #log if usb drive (not a ata drive)...
  140. DEVDRV="`echo -n "$DEVICE" | tr -d '[0-9]'`"
  141. if [ "`echo "$ATADRIVES" | grep "$DEVDRV"`" = "" ];then #110126
  142. echo "$DEVDRV" >> /root/.usb-drive-log-probepart
  143. sort -u /root/.usb-drive-log-probepart > /tmp/usb-drive-log-probepart-tmp
  144. mv -f /tmp/usb-drive-log-probepart-tmp /root/.usb-drive-log-probepart
  145. fi
  146. ;;
  147. esac
  148. done
  149. ###END###