3 Jun 2014

how to automount usb drives and memory cards in linux

A recent discussion on Debian forum has inspired me to write this post. If you use a minimal window manager like openbox, fluxbox, i3 etc. you will find that USB drives and Memory cards won't automount. It can be very hard for new Linux users to figure out how to mount the USB drive or Memory card they have just inserted. Thankfully this can be achieved easily via udev rules.

Open your favorite text-editor as root ( sudo or su) and copy-paste the following,


#!/bin/sh

KERNEL!="sd[b-z]*", GOTO="exit"

ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="exit"

IMPORT{program}="/sbin/blkid -o udev -p %N"

ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"

ENV{ID_FS_LABEL}=="", ENV{dir_name}="flash_drive_%k"

ACTION=="add", RUN+="/bin/mkdir -p '/media/%E{dir_name}'"

ACTION=="add", ENV{mount_options_vfat}="rw,uid=1000,gid=1000,dmask=022,fmask=133,noatime"

ACTION=="add", ENV{ID_FS_TYPE}=="vfat", RUN+="/bin/mount -t vfat -o %E{mount_options_vfat} /dev/%k '/media/%E{dir_name}'"

ACTION=="add", ENV{mount_options_ntfs}="rw,uid=1000,gid=1000,dmask=022,fmask=133,noatime"

ACTION=="add", ENV{ID_FS_TYPE}=="ntfs", RUN+="/bin/mount -t ntfs-3g -o %E{mount_options_ntfs} /dev/%k '/media/%E{dir_name}'"

ACTION=="add", ENV{mount_options_ext2}="users,exec,noatime"

ACTION=="add", ENV{ID_FS_TYPE}=="ext2", RUN+="/bin/mount -t ext2 -o %E{mount_options_ext2} /dev/%k '/media/%E{dir_name}'"

ACTION=="add", ENV{mount_options_ext3}="users,exec,noatime"

ACTION=="add", ENV{ID_FS_TYPE}=="ext3", RUN+="/bin/mount -t ext3 -o %E{mount_options_ext3} /dev/%k '/media/%E{dir_name}'"

ACTION=="add", ENV{mount_options_ext4}="users,exec,noatime"

ACTION=="add", ENV{ID_FS_TYPE}=="ext4", RUN+="/bin/mount -t ext4 -o %E{mount_options_ext4} /dev/%k '/media/%E{dir_name}'"

ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount '/media/%E{dir_name}'", RUN+="/bin/rmdir '/media/%E{dir_name}'"

LABEL="exit"

And save this file in '/etc/udev/rules.d' directory with the name '10-usb-mount.rules'.

We'll need another rules file for Memory cards,


#!/bin/sh

KERNEL!="mmcblk[0-9]p[0-9]", GOTO="exit"

ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="exit"

IMPORT{program}="/sbin/blkid -o udev -p %N"

ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"

ENV{ID_FS_LABEL}=="", ENV{dir_name}="flash_drive_%k"

ACTION=="add", RUN+="/bin/mkdir -p '/media/%E{dir_name}'"

ACTION=="add", ENV{mount_options_vfat}="rw,uid=1000,gid=1000,dmask=022,fmask=133,noatime"

ACTION=="add", ENV{ID_FS_TYPE}=="vfat", RUN+="/bin/mount -t vfat -o %E{mount_options_vfat} /dev/%k '/media/%E{dir_name}'"

ACTION=="add", ENV{mount_options_ntfs}="rw,uid=1000,gid=1000,dmask=022,fmask=133,noatime"

ACTION=="add", ENV{ID_FS_TYPE}=="ntfs", RUN+="/bin/mount -t ntfs-3g -o %E{mount_options_ntfs} /dev/%k '/media/%E{dir_name}'"

ACTION=="add", ENV{mount_options_ext2}="users,exec,noatime"

ACTION=="add", ENV{ID_FS_TYPE}=="ext2", RUN+="/bin/mount -t ext2 -o %E{mount_options_ext2} /dev/%k '/media/%E{dir_name}'"

ACTION=="add", ENV{mount_options_ext3}="users,exec,noatime"

ACTION=="add", ENV{ID_FS_TYPE}=="ext3", RUN+="/bin/mount -t ext3 -o %E{mount_options_ext3} /dev/%k '/media/%E{dir_name}'"

ACTION=="add", ENV{mount_options_ext4}="users,exec,noatime"

ACTION=="add", ENV{ID_FS_TYPE}=="ext4", RUN+="/bin/mount -t ext4 -o %E{mount_options_ext4} /dev/%k '/media/%E{dir_name}'"

ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount '/media/%E{dir_name}'", RUN+="/bin/rmdir '/media/%E{dir_name}'"

LABEL="exit"

Save this file in '/etc/udev/rules.d' directory with the name '10-sdcard-mount.rules'.

Next open terminal and run the following command as root to reload the udev rules,


udevadm control --reload-rules

That's it! The next time you plug-in a USB drive or a Memory card it will get automounted and be ready for you to use. Cheers!!! 

6 comments:

Anonymous said...

Perfect solution for NewBees! Thanks: worked right out of the box.

vixidem said...

Hi Bhikhu,

I am using Arch Linux, with Awesome WM. After applying those udev rules you provided above, USB drive and memory card are able to be mounted (at least that is what I perceived, as the drives appear in /media). However, when I access the content of the 'mounted' drive, it appears as the drives have no content at all. All those drives that I have tested with are working if I mount them on Arch with KDE installed. I have no idea where to troubleshoot. Can you please point me to a direction? Thank you.

Unknown said...

vixidem,
Attach USB drive to computer and run the following commands,
'stat -c "%A %a %n" /media/'
'cat /etc/fstab'
'sudo blkid'
'mount'
Please post the output of the above commands here.
Cheers!!!

Anonymous said...

Thanks this helped me and will do the same for many others.

vlslv said...

Nice work. Thank you very much

Kate Harlington said...

Great article for those looking for Memory Cards forsale online! Thank you for sharing!