Soosck

GNU/Linux

Improved Udev Rule For Arch Linux

with 9 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
LABEL="my_media_automount_end"

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

9 Responses

Subscribe to comments with RSS.

  1. For this kind of job I use the “Desktop Privileges” package from AUR: http://aur.archlinux.org/packages.php?ID=42669
    It works very smooth without hiccups

    Vladi

    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.

      Woody

      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

        Vladi

        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! :-)

          Woody

          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!

    josiano

    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.

    Archi

    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.

    masux

    October 3, 2013 at 6:49 pm

  6. Seems wrong.

    “Udisks

    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. “

    Eugene

    December 22, 2013 at 1:39 pm


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: