1. /etc/udev/rules.d/11-media-by-label-auto-mount.rules
  2. KERNEL!="sd[a-z]*", GOTO="media_by_label_auto_mount_end"
  3. ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="media_by_label_auto_mount_end"
  4. # Do not mount devices already mounted somewhere else to avoid entries for all your local partitions in /media
  5. ACTION=="add", PROGRAM=="/bin/grep -q ' /dev/%k ' /proc/self/mountinfo", GOTO="media_by_label_auto_mount_end"
  6. # Open LUKS partition if necessary
  7. PROGRAM=="/sbin/blkid -o value -s TYPE %N", RESULT=="crypto_LUKS", ENV{crypto}="mapper/", ENV{device}="/dev/mapper/%k"
  8. ENV{crypto}=="", ENV{device}="%N"
  9. ACTION=="add", ENV{crypto}!="", PROGRAM=="/usr/bin/xterm -display :0.0 -e 'echo Password for /dev/%k; /sbin/cryptsetup luksOpen %N %k'"
  10. ACTION=="add", ENV{crypto}!="", TEST!="/dev/mapper/%k", GOTO="media_by_label_auto_mount_end"
  11. # Global mount options
  12. ACTION=="add", ENV{mount_options}="noatime"
  13. # Filesystem-specific mount options
  14. ACTION=="add", PROGRAM=="/sbin/blkid -o value -s TYPE %E{device}", RESULT=="vfat|ntfs", ENV{mount_options}="%E{mount_options},utf8,gid=100,umask=002"
  15. # Get label if present, otherwise assign one
  16. PROGRAM=="/sbin/blkid -o value -s LABEL %E{device}", ENV{dir_name}="%c"
  17. # Use basename to correctly handle labels such as ../mnt/foo
  18. PROGRAM=="/usr/bin/basename '%E{dir_name}'", ENV{dir_name}="%c"
  19. ENV{dir_name}=="", ENV{dir_name}="usbhd-%k"
  20. # Mount the device
  21. ACTION=="add", ENV{dir_name}!="", RUN+="/bin/mkdir -p '/media/%E{dir_name}'", RUN+="/bin/mount -o %E{mount_options} /dev/%E{crypto}%k '/media/%E{dir_name}'"
  22. # Clean up after removal
  23. ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l '/media/%E{dir_name}'"
  24. ACTION=="remove", ENV{crypto}!="", RUN+="/sbin/cryptsetup luksClose %k"
  25. ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/rmdir '/media/%E{dir_name}'"
  26. # Exit
  27. LABEL="media_by_label_auto_mount_end"
Comments powered by Disqus