usbmount.sh 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/bin/bash
  2. MOUNTDIR="/home/user";
  3. [ ! -f /usr/sbin/usbmount.sh ] && echo "Stellen Sie sicher, dass dieses Script unter /usr/sbin/usbmount.sh liegt." && exit 1;
  4. [ "$(id -u)" -ne "0" ] && echo "Dieses Script muss unter root laufen." && exit 1;
  5. _add() {
  6. [ "$DEVNAME" == "" ] && exit 1;
  7. TYPE=$(blkid $DEVNAME | sed 's/^.* TYPE="\([^"]*\)".*$/\1/');
  8. NAME=$( echo $DEVNAME | sed 's/\/dev\///');
  9. case "$TYPE" in
  10. exfat|vfat|ntfs)
  11. OPT="uid=1000,gid=1000,noatime,nodiratime,async";
  12. ;;
  13. ext2|ext3|ext4)
  14. OPT="noatime,nodiratime,async";
  15. ;;
  16. *)
  17. echo "Unbekannter Partitionstyp oder Partitionstabelle.";
  18. exit 0;
  19. ;;
  20. esac
  21. rm -R -f $MOUNTDIR/$NAME; mkdir -p $MOUNTDIR/$NAME;
  22. sleep 1 && mount -o $OPT $DEVNAME $MOUNTDIR/$NAME &&
  23. sleep 1 && chown 1000:1000 $MOUNTDIR/$NAME;
  24. [ -f /etc/hook.mount.sh ] && /bin/bash /etc/hook.mount.sh $NAME $TYPE $MOUNTDIR/$NAME;
  25. }
  26. _del() {
  27. umount $DEVNAME;
  28. NAME=$( echo $DEVNAME | sed 's/\/dev\///');
  29. rm -R -f $MOUNTDIR/$NAME;
  30. [ -f /etc/hook.umount.sh ] && /bin/bash /etc/hook.umount.sh $NAME $TYPE $MOUNTDIR/$NAME;
  31. }
  32. _install() {
  33. cat <<EOF > /lib/udev/rules.d/05-usbmount.rules
  34. KERNEL=="sd*", DRIVERS=="sbp2", ACTION=="add", RUN+="/usr/sbin/usbmount.sh add"
  35. KERNEL=="sd*", SUBSYSTEMS=="usb", ACTION=="add", RUN+="/usr/sbin/usbmount.sh add"
  36. KERNEL=="ub*", SUBSYSTEMS=="usb", ACTION=="add", RUN+="/usr/sbin/usbmount.sh add"
  37. KERNEL=="sd*", ACTION=="remove", RUN+="/usr/sbin/usbmount.sh del"
  38. KERNEL=="ub*", ACTION=="remove", RUN+="/usr/sbin/usbmount.sh del"
  39. EOF
  40. /etc/init.d/udev restart
  41. }
  42. case "$1" in
  43. add)
  44. _add;
  45. ;;
  46. del)
  47. _del;
  48. ;;
  49. install)
  50. _install;
  51. ;;
  52. esac