This article explains how to automount an USB stick when inserted and unmount it automatically when unplugged
Install the required packages
The automounter facility is managed by autofs. Autofs controls the operation of the automount daemons. The automount daemons automatically mount filesystems when they are used and unmount them after a period of inactivity. This is done based on a set of pre-configured maps.
To install autofs type:
~# apt-get update
~# apt-get install autofs
Configure autofs
Insert an USB stick and check on which device it appears by typing:
~# dmesg
...
usb 1-2: new full speed USB device using at91_ohci and address 2
scsi0 : usb-storage 1-2:1.0
scsi 0:0:0:0: Direct-Access UDISK PDU01-8G 8AH2.0 0.00 PQ: 0 ANSI: 2
sd 0:0:0:0: [sda] 15794176 512-byte logical blocks: (8.08 GB/7.53 GiB)
...
sd 8:0:0:0: [sda] Assuming drive cache: write through
sda:
...
In this case the device is /dev/sda. If your USB stick has more than one partition you will see sda1, sda2, etc. for each partition.
Edit the file /etc/auto.master adding this line:
/var/autofs/removable /etc/auto.removable --timeout=2
Where:
- /var/autofs/removable is the directory where your USB stick will be mounted
- /etc/auto.removable is another file that we have to create
- --timeout=2 tells that the device will be umounted after 2 seconds of idling
Now create the file /etc/auto.removable with this contents:
usbstick -fstype=vfat,rw,umask=002 :/dev/sda
then restart autofs typing:
~# /etc/init.d/autofs restart
Now move to the /var/autofs/removable directory:
~# cd /var/autofs/removable
/var/autofs/removable# ls
/var/autofs/removable# ls usbstick
[ .. list on my files on the usbstick ]
As you can see the usbstick directory will appear when requested and disappear after 2 seconds.
Related links
|