10-flash-mounts.rules 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #! /bin/sh
  2. #######################################
  3. # USB Flash Drives automounting #
  4. #######################################
  5. # start at sdb to ignore the system hard drive
  6. KERNEL!="sd[b-z]*", GOTO="exit"
  7. ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="exit"
  8. # import some useful filesystem info as variables
  9. IMPORT{program}="/sbin/blkid -o udev -p %N"
  10. # get the label if present, otherwise assign one based on device/partition
  11. ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
  12. ENV{ID_FS_LABEL}=="", ENV{dir_name}="flash_drive_%k"
  13. # create the dir in /media and symlink it to /mnt
  14. ACTION=="add", RUN+="/bin/mkdir -p '/media/%E{dir_name}'"
  15. # global mount options
  16. #ACTION=="add", ENV{mount_options}="relatime"
  17. # filesystem-specific mount options (777/666 dir/file perms for ntfs/vfat)
  18. ########
  19. # vfat #
  20. ########
  21. # vfat specific mount options
  22. ACTION=="add", ENV{mount_options_vfat}="gid=100,dmask=000,fmask=111,utf8,flush,rw,noatime,users"
  23. # add device to /etc/fstab
  24. ACTION=="add", ENV{ID_FS_TYPE}=="vfat", RUN+="/bin/sed -i '$a\/dev/%k /media/%E{dir_name} vfat %E{mount_options_vfat} 0 0' /etc/fstab"
  25. # mount device
  26. ACTION=="add", ENV{ID_FS_TYPE}=="vfat", RUN+="/bin/mount -t auto -o %E{mount_options_vfat} /dev/%k '/media/%E{dir_name}'"
  27. ########
  28. # ntfs #
  29. ########
  30. # ntfs specific mount options
  31. ACTION=="add", ENV{mount_options_ntfs}="gid=100,dmask=000,fmask=111,utf8,flush,rw,noatime,users"
  32. # add device to /etc/fstab
  33. ACTION=="add", ENV{ID_FS_TYPE}=="ntfs", RUN+="/bin/sed -i '$a\/dev/%k /media/%E{dir_name} ntfs-3g %E{mount_options_ntfs} 0 0' /etc/fstab"
  34. # mount device
  35. ACTION=="add", ENV{ID_FS_TYPE}=="ntfs", RUN+="/bin/mount -t ntfs-3g -o %E{mount_options_ntfs} /dev/%k '/media/%E{dir_name}'"
  36. #####################
  37. # other filesystems #
  38. #####################
  39. # ntfs specific mount options
  40. ACTION=="add", ENV{mount_options_other}="gid=100,dmask=000,fmask=111,utf8,flush,rw,noatime,users"
  41. # automount all other filesystems
  42. ACTION=="add", ENV{ID_FS_TYPE}!="(ntfs|vfat)", RUN+="/bin/mount -t auto -o %E{mount_options_other} /dev/%k '/media/%E{dir_name}'"
  43. #################################
  44. # clean up after device removal #
  45. #################################
  46. ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l '/media/%E{dir_name}'", RUN+="/bin/rmdir '/media/%E{dir_name}'"
  47. ACTION=="remove", ENV{ID_FS_TYPE}!="", RUN+="/bin/sed -i '/\/dev\/%k /d' /etc/fstab"
  48. # exit
  49. LABEL="exit"