Improved Udev Rule For Arch Linux

with 10 comments

There are many time-saving features that you should have on your Arch Linux and one of sacred ones is automount. It’d be very time-consuming and monotonous to use mount/umount commands every time you connect and disconnect a usb removable device. Fortunately, Arch Wiki has a very clear explanation on this. However the original script lack the ability of automatically creating a symbolic link on your desktop. The sort of feature that you’ve seen in ubuntu or mint: Every time you connect a usb removable device an icon appears on your Desktop and it vanishes when you remove the usb.

automount udev arch linux

The USB storage icon has been added to Desktop automatically using a udev rule


I’ve made some modification to the original script to add this feature. It’s quite easy. Just create a rule (i.e. /etc/udev/rules.d/11-media-by-label-auto-mount.rules) using your favorite editor and paste the following code inside it:

# /etc/udev/rules.d/10-my-media-automount.rules

# start at sdb to ignore the system hard drive
KERNEL!="sd[b-z]*", GOTO="my_media_automount_end"
ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="my_media_automount_end"

# import some useful filesystem info as variables
IMPORT{program}="/sbin/blkid -o udev -p %N"

# get the label if present, otherwise assign one based on device/partition
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="usbhd-%k"

# create the dir in /media and symlink it to /mnt
ACTION=="add", RUN+="/bin/mkdir -p '/media/%E{dir_name}'"
# create a symbolic link to /media/{usb_folder} on desktop
ACTION=="add", RUN+="/bin/ln -s '/media/%E{dir_name}' '/home/aardvark/Desktop/%E{dir_name}'"

# global mount options
ACTION=="add", ENV{mount_options}="relatime"
# filesystem-specific mount options (777/666 dir/file perms for ntfs/vfat)
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},gid=100,dmask=000,fmask=111,utf8"

# automount ntfs filesystems using ntfs-3g driver
ACTION=="add", ENV{ID_FS_TYPE}=="ntfs", RUN+="/bin/mount -t ntfs-3g -o %E{mount_options} /dev/%k '/media/%E{dir_name}'"
# automount all other filesystems
ACTION=="add", ENV{ID_FS_TYPE}!="ntfs", RUN+="/bin/mount -t auto -o %E{mount_options} /dev/%k '/media/%E{dir_name}'"

#remove the symbolic link to ~/{usb_folder}
ACTION=="remove", RUN+="/bin/rm -f '/home/aardvark/Desktop/%E{dir_name}'"
# clean up after device removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l '/media/%E{dir_name}'", RUN+="/bin/rmdir '/media/%E{dir_name}'"

# exit

Note: Edit line 17 and 30 to reflect your Desktop path. In other words, change /home/aardvark/Desktop to /home/{your_user_name}/Desktop. The reason that I didn’t use a relative path is because there is a myth that such configs do not go well with relative paths.

Now save the file and you’re done. Just connect your usb removable device(s) to see if it works. 8-)

About these ads

Written by Woody

January 19, 2011 at 4:07 pm

Posted in ArchLinux

10 Responses

Subscribe to comments with RSS.

  1. For this kind of job I use the “Desktop Privileges” package from AUR:
    It works very smooth without hiccups


    February 2, 2011 at 9:41 am

    • Never heard of it before. What are the advatnages of using DP method instead of the traditional method(udev)? Since I’m quite happy with my own version of udev rule I just want to know whether DP worth the try or not.


      February 2, 2011 at 5:21 pm

      • I don’t know what are the differences, but one of the Arch developers recommended it and for me that’s enough :)
        In a more serious note: it’s waaay easier for someone new to Arch to install it than to modify that script


        February 2, 2011 at 5:44 pm

        • By taking a looksee at the PKGBUILD file, it seems that it uses udisk which expalins why the method is considered easier.
          Cheers! :-)


          February 2, 2011 at 7:54 pm

  2. awesome!…thanks a lot!

    Aash Dhariya

    March 5, 2013 at 1:15 pm

  3. deu certo! muito obrigado! procurei bastante por isso!


    April 3, 2013 at 11:09 pm

  4. This is the best solution ever …………….. I was consistently getting permission errors for all other solutions. But this worked like a charm. Thank you !!!!!
    I need to know how to auto mount my cd rom drivers. If you know a solution like this please let me know.


    August 14, 2013 at 6:05 am

  5. Thank you, but I must do little edit, I replaced gid=100,dmask=000,fmask=111 with default. For full work with awesome I also install desktop-privliges from AUR.


    October 3, 2013 at 6:49 pm

  6. Seems wrong.


    If you want to mount removable drives please do not call ‘mount’ from udev rule. In case of fuse filesystems (e.g. ntfs-3g) you’ll get “Transport endpoint not connected” error. Instead use udisks that handles automount correctly. “


    December 22, 2013 at 1:39 pm

  7. Hello,

    I did copy/past this code in the proper file as explained but nothing happen when I connect my phone (android with USB as Media Device (MTP) neither Camera (PTP)), does it work for these features?

    Or am I simply missing the other file (/etc/udev/rules.d/10-my-media-automount.rules) I could not find it anywhere in my system (/etc/udev/rules.d nor /usr/lib/udev/rules.d) ?



    July 31, 2014 at 5:33 am

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: