wait4usb_classic 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/bin/sh
  2. #this is called from 'init', runs as parallel process.
  3. #110711 return found usb drives.
  4. modprobe usb-storage & #run as separate process
  5. mount -t usbfs none /proc/bus/usb
  6. #110425 note, if 'usbhid.ko' builtin to kernel, might not need this...
  7. #v423 problem recent kernels: kernel configured to load hid-* extra drivers when
  8. #usbhid loads, but it doesn't work. Most unfortunate. Note, older kernels have the
  9. #hid-* (drivers for specific hardware, like Logitech wireless keyboard) builtin to
  10. #the usbhid driver. Now that they are separated out, it is a headache. Try this...
  11. /bin/hotplug2stdout_notimeout >/tmp/uevents.log &
  12. PIDHOT=$!
  13. modprobe usbhid #for a usb keyboard.
  14. #101127 very troublesome... bump up to 3...
  15. #v3.94 Classmate laptop, needs more delay here... no, further down...
  16. sleep 3 #2 v403 bumped it up to 3. v412 try 1sec again.
  17. USBSTORAGES=0 ; CNTUSB=0
  18. while [ $USBSTORAGES -eq 0 ];do
  19. echo -n "." > /dev/console
  20. sleep 1
  21. CNTUSB=$(($CNTUSB+1))
  22. [ $CNTUSB -gt 25 ] && break
  23. #v412 bug, ubuntu kernel, got duplicate 'device found at 2', need 'sort -u'...
  24. USBSTORAGES=`/bin/dmesg | grep "usb-storage: device found at" | sort -u | wc -l | sed -e 's/ //g'`
  25. #if booting from usb, USBSTORAGES must be non-zero...
  26. [ "`echo "$PMEDIA" | grep 'usb'`" != "" ] && [ $USBSTORAGES -eq 0 ] && continue
  27. [ $USBSTORAGES -eq 0 ] && break
  28. AVAILABLEUSBSTORAGES=`/bin/dmesg | grep "usb-storage: device scan complete" | wc -l | sed -e 's/ //g'`
  29. [ $USBSTORAGES -ne $AVAILABLEUSBSTORAGES ] && USBSTORAGES=0
  30. done
  31. [ $USBSTORAGES -ne 0 ] && sleep 1 #v412 was needed for classmate. was 2, try 1sec.
  32. #v423 load hid-* driver...
  33. for ONEHID in `grep -o 'MODALIAS=hid:[^ ]*' /tmp/uevents.log | cut -f 2 -d '=' | tr '\n' ' '`
  34. do
  35. modprobe $ONEHID
  36. done
  37. kill $PIDHOT
  38. #101127 note about extra sleeps:
  39. #testing quirky 1.4, booting from cd, save-file on usb (hd). i found even with initial 'sleep 3'
  40. #still got CNTUSB=3. it shows up in /sys/block, but the partitions /sys/block/*/ and /proc/partitions
  41. #take a very long time to show up... kernel: 2.6.31.14. i think more recent kernels have improved timing.
  42. #101127 wait for usb partitions to become available...
  43. CNTUSB2=$CNTUSB
  44. SDDRVS="`ls -1 /sys/block | grep '^sd' | tr '\n' ' '`"
  45. for ONEDRV in $SDDRVS
  46. do
  47. #[ "`echo -n "$ATADRIVES" | grep "$ONEDRV"`" != "" ] && continue
  48. [ "`readlink /sys/block/${ONEDRV} | grep '/usb[0-9]/'`" = "" ] && continue #not usb.
  49. while [ ! -e /sys/block/${ONEDRV}/${ONEDRV}1 ];do
  50. echo -en "\\033[1;31m.\\033[0;39m" >/dev/console #red dot
  51. sleep 1
  52. CNTUSB2=$(($CNTUSB2+1))
  53. [ $CNTUSB2 -gt 15 ] && break
  54. done
  55. #force update of /proc/partitions...
  56. dd if=/dev/${ONEDRV} of=/dev/null bs=512 count=1 >/dev/null 2>&1
  57. done
  58. echo "USBSTORAGES=$USBSTORAGES AVAILABLEUSBSTORAGES=$AVAILABLEUSBSTORAGES CNTUSB=$CNTUSB CNTUSB2=$CNTUSB2" > /tmp/usb-drives-probe #101127 for debugging.
  59. #110711 return found usb drives...
  60. ALLUSBDRVS="`find /sys/block -maxdepth 1 -name 'sd*' -o -name 'sr*' | xargs -l readlink 2>/dev/null | grep '/usb[0-9]' | rev | cut -f 1 -d '/' | rev | tr '\n' ' '`"
  61. [ "$ALLUSBDRVS" = " " ] && ALLUSBDRVS=""
  62. echo -n "$ALLUSBDRVS" > /tmp/flag-usb-ready
  63. ###end###