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 :)
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
-fstype=smbfs \
/share1 ://server/share1
/share2 ://server/share2
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
In order to avoid opening the hotplug box, I just hacked up this autofs script. It meets my needs so someone else may find it of use too. It will automount an encrypted block device at /dev/sdb using whatever name you choose. The key files with a corresponding name in /etc is used to decrypt the device.
I have a set of removable hard drives that are used for backup (RDX QuikStor). With the following configuration I can insert a cartridge and the backup software (Bacula) can just mount it, making the encryption transparent to it.
The mapping for the 'cd' key also appears in this script. That's there because I'm mounting this at /media and hijacking the original, static /etc/auto.media. In /etc/auto.master:
/media /etc/auto.mediaIn /etc/auto.media:
#!/bin/bash # This is the path beneath this map's root that autofs is looking for key="$1" # A static mapping for the key 'cd' # This is what /etc/auto.media used to do statically if [ "$key" == "cd" ]; then echo -fstype=iso9660,ro,nosuid,nodev / :/dev/cdrom exit 0 fi # The cryptsetup tool from the package of the same name CRYPTSETUP=/sbin/cryptsetup # This is the raw device that we will mount mount_device=/dev/sdb # This is the encryption key file key_file=/etc/quikstor.key # Options to pass to the cryptsetup tool luks_opts="--key-file $key_file" # Mount options for the encrypted fileystem mount_opts="-fstype=xfs,defaults" # The mapped block device crypt_device=/dev/mapper/$key # Give up if there is no key or setup tool [ -r $key_file ] || exit 0 [ -x $CRYPTSETUP ] || exit 0 # If there is an encrypted device mapped in already, it must be from a # previous mount. It may be out-of-date so remove it now. [ -b $crypt_device ] && $CRYPTSETUP remove $key # Give up if the raw device doesn't have a LUKS header $CRYPTSETUP isLuks $mount_device || exit 0 # Open the encrypted block device $CRYPTSETUP luksOpen $mount_device $key $luks_opts >& /dev/null || exit 1 # If we ended up with a block device, echo a mount line for autofs to use if [ -b $crypt_device ]; then echo $mount_opts / $crypt_device fi
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
/dev/external_hd1 /media/dragon vfat rw,user,noauto 0 0
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
Thanks again!
[ Parent ]