Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tlug] Crazy automount solution



On my local LUG (COLUG, the Central Ohio Linux Users Group) mailing
list, some chap posted about how he could get his USB hard drive to
automount. I thought several of you on this list might be amused by or
interested in my solution:

On Tue, 18 Jan 2005 21:34:02 -0500, Name Withheld <foobar@example.com> wrote:

> I have one of those USB flash drives.   (nifty devices, BTW).  When I
> plug it in the USB port of my PC, it is not automatically mounted.  I
> can manually mount the device.  Is there a way to have the USB device
> automatically mount after I plug it into the USB port?

You should mention which desktop environment (e.g. GNOME or KDE) you
are running, as DE's (yes, the apostrophe *is* correct when making an
acronym plural) usually provide an automount mechanism that causes a
nifty icon to show up on your desktop.

Now, there is probably a way to accomplish this with the kernel
automounter, but let me give you a solution in the spirit of Unix:

1. Create an entry in your /etc/fstab, something like this:

/dev/sda1  /mnt/usbhdd  vfat  user  0 0

Your entry may be a bit different depending on the device, mount
point, and file system. See the manpage for mount(8) for more
information.

2. Write a shell script, ${HOME}/bin/automount.sh:

#!/bin/sh

# A list of devices that you want to automount (i.e. the device that appears
# when you plug your USB device in)
DEVS="/dev/sda1 /dev/sdb1"

# Set this to the number of seconds that should elapse between polling
# attempts (the lower the number, the more responsive, but more CPU
# time will be consumed)
SLEEPTIME=15

# Loop forever
while [ 1 ]; do

 # Iterate through devices
 for i in ${DEVS}; do

   # Does this device exist?
   if [ -e $i ]; then

     # Do not mount devices that are already mounted
     mount | grep "^$i" >/dev/null 2>&1
     if [ $? != 0 ]; then mount $i; fi

   fi # if (found the device)

 done # for (traversing devices)

 sleep ${SLEEPTIME}

done # while (looping forever)

3. Make your script executable:

chmod +x ${HOME}/bin/automount.sh

4. Add a line to your /etc/inittab so that your script will run automatically:

ams:234:respawn:${HOME}/bin/automount.sh

Substitute the value of "echo ${HOME}" for ${HOME} in the above line!

5. Tell init to reread /etc/inittab:

sudo init q

or, if you do not have sudo(8) installed and configured[1], you can use su:

su root -c 'init q'

Now, plugging in a drive should result in it being auto-mounted within
${SLEEPTIME} seconds.

-Josh

[1] If you do not have sudo(8) configured, get thee to this page!
   http://www.courtesan.com/sudo/


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links