Skip to content

Instantly share code, notes, and snippets.

@ramnes
Last active February 17, 2020 16:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ramnes/67899785dbe985c48dca to your computer and use it in GitHub Desktop.
Save ramnes/67899785dbe985c48dca to your computer and use it in GitHub Desktop.
Automatically mount USB storages when plugged
KERNEL!="sd[b-z][0-9]", GOTO="automount_end"
# Import FS infos
IMPORT{program}="/sbin/blkid -o udev -p %N"
# Get a label if present, otherwise specify one
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="usbhd-%k"
# Global mount options
ACTION=="add", ENV{mount_options}="relatime"
# Filesystem-specific mount options
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002"
# Mount the device
ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}"
# Clean up after removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}"
# Exit
LABEL="automount_end"
@ramnes
Copy link
Author

ramnes commented Oct 21, 2015

Just put that file in /etc/udev/rules.d , then execute udevadm control --reload-rules to reload udev. This script is shamelessly stolen from http://www.axllent.org/docs/view/auto-mounting-usb-storage/. Also note that you should always manually umount before unplugging, as said in the comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment