Last active
February 17, 2020 16:01
-
-
Save ramnes/67899785dbe985c48dca to your computer and use it in GitHub Desktop.
Automatically mount USB storages when plugged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just put that file in
/etc/udev/rules.d
, then executeudevadm 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 manuallyumount
before unplugging, as said in the comments.