Intro
Here's a short guide for automounting external USB mass storage devices with udev. I've only mounted USB sticks and a USB-plugged memory card reader, but you can probably adjust the settings to suit other kinds of devices, too.
My USB sticks are VFAT-formatted, and thus the example fstab entry below doesn't work for e.g. ext3-formatted devices. Some mount options are filesystem-specific, so one line in fstab can't serve 'em all.
(WARNING: Although automounting is neat, it's not idiot proof. You should be careful not to remove the device before all data has been written. The 'sync' mount option forces writing to happen as soon as possible, but that doesn't prevent you from yanking a USB stick out prematurely. If you want to play it safe, unmount manually, then remove.)
Setup
System Requirements
You will need these in your system:
A working udev.
A group in /etc/group for accessing the device. I use floppy.
An existing directory to use as mount point. I use /media/stick.
Kernel Requirements
In addition to USB support in general, your kernel will also need:
SCSI disk support (CONFIG_BLK_DEV_SD)
USB mass storage support (CONFIG_USB_STORAGE).
udev rules
You'll need udev rules for running the proper mount and umount commands whenever the device is inserted or removed. The rules should exist relatively early in the rule hierarchy, which means that the file they're in needs to be alphabetically before most other *.rules files. So, if you don't already have a file called /etc/udev/rules.d/10-local.rules, now's your time to create it.
Let's suppose your USB stick appears as /dev/sdb1 like mine does. You will then need these two lines in 10-local.rules:
ACTION=="add", KERNEL=="sdb*", SUBSYSTEM=="block", RUN+="/bin/mount /dev/%k" ACTION=="remove", KERNEL=="sdb*", SUBSYSTEM=="block", RUN+="/bin/umount /dev/%k"
Notice, that KERNEL=="sdb*" means that not only sdb1, but also sdb2, sdb3, etc. will be mounted. If you want to be more specific, you can change it to KERNEL=="sdb1" or whatever. You will also need to add more lines if you want to specify other devices, like e.g. sdc*.
fstab entry
To be able to mount and read the device, you need an entry in /etc/fstab to define where and how to do it.
I'll again use my system as an example. My device name and mount point are the ones I already mentioned, and floppy gid is 15. Here's what I have in /etc/fstab:
/dev/sdb1 /media/stick vfat defaults,noauto,sync,group,gid=15,umask=0002 0 0
You can probably figure out how to customize that to suit your needs. Notice that everything is literal, so no wildcards are allowed. You will also need to add separate lines for other devices if you have more than one.
User permissions
Now it's up to you to decide who has permissions to write to the device. Just add the desired users to the floppy group in /etc/group.
Enabling changes
Recent udev versions should use inotify to automatically reread rules changes. But if automounting doesn't work right away, restart the udevd init script:
telinit run udevd restart
You should now be able to automount.
iuso
