Tuesday 25 October 2011

USB automount

I looked for a solution for simply (preferably automatically) mounting any USB storages (pendrives, external hard drives, etc.) on my Arch Linux notebook. Gnome and other desktop environments do it for you automatically but if you keep things simple with a small window manager (like Awesome or wmfs) you need to set it up by yourself.

There are several solutions for doing so:

1. mounting USB storages manually or with an fstab entry:
You can do it with some basic Linux knowledge, the method is well documented here. It is simple to set up but not flexible enough (i.e. you have to mount the devices manually, it is hard to handle several storages at the same time, fstab can automount only at boot time).

2. using an external application to handle your usb devices: 
pmountskvm, udiskie etc. can more or less handle your USB volumes but I  found none of them satisfactory. One of them have dependency problems (i.e. using hal that I did not want to install), others need consolekit or to be run as root, and all of them needs an extra daemon to be run in the background.

3. using udev rules to recognise and automount your volumes:
The udev daemon natively runs in the background, you do not have to install and/or place it in your /etc/rc.conf. You only need to tell it to mount your USB storages when they are plugged in. You can find nice examples for such rules in the udev Arch Wiki.

I use the following udev rule that mounts automatically the hot-plugged device under /media/<label> and works seamlessly with vfat, ntfs and other formats of USB storages. Simply create this file and fill it up with the contents as follows:

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

# vim:enc=utf-8:nu:ai:si:et:ts=4:sw=4:ft=udevrules:
#
# /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}'"

# 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}'"

# 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"

3 comments:

  1. Hi,
    So I tried out the udev rule and it worked great. It's usb 3.0 hdd mounted under /media/. However, it won't let me touch the files on the drive because it says it's a read-only filesystem. It's hfs+ journaled. Does archlinux not handle hfs+? If it does, how do I get it to be rwx? Also, how do you get the drive lable name to be used when mounted? The label is Kelly, and it came up as usbhd-sdb2. I'm trying to use bittorrent sync and open vpn to handle offsite backups. Bittorrent sync is able to see the drive but I'm assuming it won't read from it considering I can't make a simple test directory from terminal. Perhaps I'm going about it the wrong way. Thanks for your help!

    ReplyDelete
  2. Hi Partick,
    I do not have experiences with hfs+ but I suppose it has limited support on linux. See the official Arch Linux forum:
    https://bbs.archlinux.org/viewtopic.php?id=89830
    Either you can switch off journaling or if it is not necessary to be hfs+ then you may consider repartitioning it for a supported file system.

    ReplyDelete
  3. jeje, old topic

    mount: only can use root "--option" option.

    What do you think? How did I can resolve?

    ReplyDelete