Automounting card readers and USB keys using autofs

Posted by chris on Mon 25 Apr 2005 at 12:57

Following on to the previous article on Card Readers and USB keys using udev we can go one step further and get automounting working.

Most of the information here is taken from http://www.greenfly.org/tips/autofs.html but is modified to match the information from the udev article. The naming conventions of the config files etc are taken from the greenfly article - which includes many more things you can do with autofs :)

Getting autofs

Again - on sarge or sid it should be as simple as

apt-get install autofs

We want to replace the following /etc/fstab settings

/dev/card_sd1   /media/sd       vfat    rw,user,noauto  0       0
/dev/card_cf1   /media/cf       vfat    rw,user,noauto  0       0
/dev/card_sm1   /media/sm       vfat    rw,user,noauto  0       0
/dev/card_ms1   /media/ms       vfat    rw,user,noauto  0       0
/dev/mobiledisk   /media/mobiledisk       vfat    rw,user,noauto  0       0

Step 1 - configure autofs

Edit /etc/auto.master - add the line

/var/autofs/removable	/etc/auto.removable	--timeout=2

This means that autofs will watch the /var/autofs/removable directory, using the configure file /etc/auto.removable and with a 2 second timeout for disconnection (how long to wait before you can pull the device).

Step 2 - configure auto.removable

Remember from the udev article and the fstab listed above that we are interested in the /dev/card_xx devices, plus the /dev/mobiledisk USB key. So - in /etc/auto.removable (new file) add

card_cf         -fstype=vfat,rw,gid=100,umask=002       :/dev/card_cf1
card_sm         -fstype=vfat,rw,gid=100,umask=002       :/dev/card_sm1
card_ms         -fstype=vfat,rw,gid=100,umask=002       :/dev/card_ms1
card_sd         -fstype=vfat,rw,gid=100,umask=002       :/dev/card_sd1
mobiledisk      -fstype=vfat,rw,gid=100,umask=002       :/dev/mobiledisk

Step 3 - restart autofs and test

/etc/init.d/autofs restart

Stick a card in one of the slots (say the sd card) - and try

ls /var/autofs/removable/card_sd/

You should get the directory listing of the card - for my Nikon I get

dcim/  misc/  nikon001.dsc*

Step 4 - Linking in to the system

In the udev article we used the /media directory for mount points. Remove any cf, sd, ms, sm or mobiledisk directories and replace them with symlinks

ln -s /var/autofs/removable/card_cf cf
ln -s /var/autofs/removable/card_sd sd
ln -s /var/autofs/removable/card_ms ms
ln -s /var/autofs/removable/card_sm sm
ln -s /var/autofs/removable/mobiledisk mobiledisk

Now you should be able to e.g.

ls -l /media/sd/

and see the cards contents. 2 seconds later it's safe to pull the card/key from the machine.

Notes on the mount options

Using the gid of 100 and a umask of 002 sets the device to rwxrwxr-x root.users. You can add a uid=1000 (or whatever your uid is to set it so that only your user can access the cards contents. You may want to set noexec or similar - especially if keeping root.users

Once again - thanks to greenfly.org for the background info here - I would recommend folk to take a look at http://www.greenfly.org/tips/autofs.html for further ideas :)


This article can be found online at the Debian Administration website at the following bookmarkable URL:

This article is copyright 2005 chris - please ask for permission to republish or translate.