share 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/bin/sh
  2. # Uncomment the following line to get debug info
  3. #set -x
  4. SELF=${SELF:-$(basename $0)}
  5. LOCK_FILE=/var/lock/share.lock
  6. source /usr/local/lib/utils
  7. source usb_gadget
  8. # The USB network interface file
  9. USB_IF=/etc/network/interfaces.d/usb0
  10. # The USB mass storage backing store file or partition
  11. BACKING_STORE_FILE=/sys/kernel/config/usb_gadget/FunKey/functions/mass_storage.mmcblk0p3/lun.0/file
  12. # Unmount a local share
  13. unmount_share () {
  14. umount /mnt || die 1 "cannot unmount the backing store partition"
  15. return 0
  16. }
  17. # Mount a share locally
  18. mount_share () {
  19. mount | grep -q /dev/mmcblk0p3
  20. if [ $? -ne 0 ]; then
  21. # Check if the FAT32 partition has been unmounted cleanly
  22. fsck.fat -n /dev/mmcblk0p3 2>/dev/null | egrep -q "Dirty bit"
  23. if [ $? -eq 0 ]; then
  24. # The FAT32 partition was not cleanly unmounted, try to
  25. # clean it
  26. warn "the backing store partition was not properly unmounted"
  27. #warn "the backing store partition was not properly
  28. #unmounted, clean it"
  29. # fsck.fat -a -t -w /dev/loop0 >/dev/null 2>&1
  30. # if [ $? -gt 1 ]; then
  31. # die 3 "cannot clean backing store file"
  32. # fi
  33. fi
  34. # Mount the backing store file locally
  35. mount /mnt/ || die 4 "cannot mount backing store partition"
  36. fi
  37. # Create the directory structure if required
  38. (cd /mnt; mkdir -p "Atari lynx" "Game Boy" "Game Boy Color" "Game Boy Advance" "Game Gear" "Neo Geo Pocket" "NES" "PS1" "Sega Genesis" "Sega Master System" "SNES" "WonderSwan")
  39. return 0
  40. }
  41. # Initialize the backing store if needed and mount it locally
  42. init_share () {
  43. if [ -f ${LOCK_FILE} ]; then
  44. return 0
  45. fi
  46. touch ${LOCK_FILE}
  47. # Initialize the USB gadget
  48. init_usb_gadget
  49. # Mount the backing store partition locally
  50. mount_share
  51. return 0
  52. }
  53. # Stop sharing the backing store partition
  54. stop_share () {
  55. # Stop sharing the backing store partition
  56. echo > $BACKING_STORE_FILE || die 11 "the backing store partition is still mounted on host"
  57. # 3) Mount the backing store partition locally
  58. mount_share
  59. info "stopped sharing the backing store partition"
  60. return 0
  61. }
  62. # Start sharing the backing store partition
  63. start_share () {
  64. # 1) Check if the backing store partition is already shared
  65. cat $BACKING_STORE_FILE | egrep -q ^/dev/mmcblk0p3 && die 12 "the backing store partition is already shared"
  66. # 2) Check if USB data is connected
  67. is_usb_data_connected > /dev/null 2>&1 || die 13 "USB sharing impossible, not connected to a host"
  68. # 3) Unmount the backing store partition if it is locally mounted
  69. unmount_share
  70. # 4) Everything is now clear to start sharing the backing store partition
  71. info "start sharing the backing store partition"
  72. echo /dev/mmcblk0p3 > $BACKING_STORE_FILE || die 14 "cannot share the backing store partition"
  73. }
  74. # Return if currently sharing
  75. is_share_started () {
  76. cat $BACKING_STORE_FILE | egrep -q ^/dev/mmcblk0p3
  77. local res=$?
  78. if [ "$res" == "0" ]; then
  79. echo "yes"
  80. else
  81. echo "no"
  82. fi
  83. return $res
  84. }
  85. case "$1" in
  86. init)
  87. init_share
  88. ;;
  89. start)
  90. start_share
  91. ;;
  92. stop)
  93. stop_share
  94. ;;
  95. is_sharing)
  96. is_share_started
  97. ;;
  98. *)
  99. die 15 "Usage $0 {init|start|stop|is_sharing}"
  100. ;;
  101. esac
  102. exit $?