Thursday, February 15, 2007

Setting up USB and CD/DVD auto mount

You can use default FreeBSD automount daemon (amd) to automatically mount and unmount external storage devices as USB or CD-ROM.

Add all necessary devices to /etc/fstab with noauto option (which will prevent automounting them at startup). This 2 entries are for single USB mass storage device and CD-ROM.


/dev/acd0 /mnt/cdrom cd9660 ro,noauto 0 0
/dev/da0s1 /mnt/flash msdosfs rw,noauto 0 0


Create these directories


mkdir /mnt/cdrom
mkdir /mnt/flash



Now we should tell amd that it should mount these devices automatically as we access their folders.

Put those options in /etc/amd.conf
[ global ]
restart_mounts = yes
unmount_on_exit= yes


And those lines to /etc/amd.map
# $FreeBSD: src/etc/amd.map,v 1.9 2002/05/15 22:24:29 obrien Exp $
* opts:=rw,grpid,resvport,vers=3,proto=tcp,nfs_retransmit_counter=10,nosuid,nodev

localhost type:=auto;fs:=${map};pref:=${key}/

localhost/cdrom type:=program;fs:=/mnt/cdrom;\
mount:="/sbin/mount mount /mnt/cdrom";\
unmount:="/sbin/umount umount /mnt/cdrom"

localhost/flash type:=program;fs:=/mnt/flash;\
mount:="/sbin/mount mount /mnt/flash";\
unmount:="/sbin/umount umount /mnt/flash"


One more step -- enable amd startup at boot.

Add these lines to /etc/rc.conf

portmap_enable=YES
portmap_flags="-h 127.0.0.1"
amd_enable=YES
amd_flags="-a /.amd_mnt -c 10 -w 2 -l syslog /host /etc/amd.map"


Portmap will bind with localhost address - thus disallowing someone to connect from outside, which improves security.

amd_flags have interesting option -w, which specified how long keep device mounted after nobody access them. I prefer to keep this value very low (2 seconds) to avoid occasionally disconnecting device while it's mounted.


After restart you can check that amd is running -- run ps -ax | grep 'amd\|rpc' command. You should get a listing like this:

565 ?? Ss 0:00.01 /usr/sbin/rpcbind -h 127.0.0.1
607 ?? Ss 0:00.66 /usr/sbin/amd -p -a /.amd_mnt -c 10 -w 2 -l syslog /h


May be PIDs will be different, but this does not matter.

Ok, now if you change to /host/localhost/cdrom, it will try to mount cdrom under /mnt/cdrom, and create symbolic link in /host/localhost directory.

To make things much more comfortable, just create symlinks in root directory

ln -s /host/localhost/flash /flash
ln -s /host/localhost/cdrom /autocdrom


That's it. You've got a working automounter for your pluggable devices.

Unmounting: it will unmount all devices as far as there is no program using it. So if you've changed to /cdrom and automounter mounted cdrom, please step out of directory (like cd .. or cd) to allow automounter unmount it.

Feel fee to ask questions in comments.

UPDATE: This method, as noted by mailing list participants, have limitation of having only one active USB mass storage device a time -- but usually it's ok.

Gaspar Chilingarov

No comments: