[Cialug] fstab options

Daniel A. Ramaley daniel.ramaley at DRAKE.EDU
Wed May 17 09:58:19 CDT 2006


On Wednesday 22 March 2006 15:35, David Champion wrote:
>Some versions of Linux will automount USB storage devices, floppies,
>cdroms... (i.e. Mandriva) if they're either present at boot, or
> plugged in later.
>
>I believe Mandriva is using hotplug -
> http://linux-hotplug.sourceforge.net/.

This thread is a bit stale, but i thought i'd report back that i found 
time to solve the problem, and how i did it. Now my external USB drive 
automatically mounts on boot, or whenever i plug it in (if it wasn't 
plugged in on boot). It also automatically unmounts if i disconnect the 
drive or turn it off.

The solution does involve hotplug--that must be installed first. Also 
give the device a disk label (see the e2label command) and put an entry 
in /etc/fstab using that label.

Once hotplug is installed, check if /etc/hotplug/usb.usermap exists. If 
not, create it. On my system, there was an example usb.usermap 
in /usr/share/doc/hotplug/examples/usb.usermap that i was able to copy 
to /etc/hotplug/. Add a line to the file for your device that looks 
like this:
usb-storage 0x00f 0x04bf 0x6830 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00

What this line does is tell hotplug to run a script called usb-storage 
when a device is plugged in that matches all the numeric fields. The 
first such field (0x00f) is used for storage devices, such as the hard 
drive that i was trying to get working. The next 2 fields are specific 
to my drive; if you want to write a mount script that will work for all 
storage devices, replace those fields with 0x00. The script that i 
wrote should actually work for anything. But anyway, to get those 2 
numbers for a specific device, plug the device in and look 
at /proc/bus/usb/devices. In that file find the entry for the device 
and copy the Vendor and ProdID fields to the second and third numeric 
entries in the line above.

The next thing to do is create a script usb-storage to be run when the 
line matches. That script needs to be in /etc/hotplug/usb/. The script 
will be passed a number of variables that might appear useful that look 
like this:
    ACTION=add
    DEVFS=/proc/bus/usb
    DEVICE=/proc/bus/usb/005/004
    DEVPATH=/devices/pci0000:00/0000:00:1d.7/usb5/5-3/5-3:1.0
    INTERFACE=8/6/80
    PRODUCT=04b4/6830/0001
    REMOVER=/var/run/usb/%proc%bus%usb%005%004
    TYPE=usb
Unfortunately, these variables aren't actually helpful for figuring out 
what device to mount. I have not found a reliable way to translate any 
of those devices into a /dev/sd* device that the mount command will 
accept. A few other people have written scripts that supposedly perform 
this task, but each of them that i examined required information from 
either /proc or /sys that simply does not exist (perhaps it did in 
earlier versions of the kernel, i really don't know). So my script, 
rather than trying to do the proper thing of identifying from the above 
information which device to actually mount, is an ugly hack that scans 
for /dev/sd* devices which are listed in /etc/fstab but not currently 
mounted, and mounts them. I consider this to be a terrible approach to 
the problem, but i don't know of a way to do it better. My script is 
below, after the references.

References:
https://listman.redhat.com/archives/rhl-devel-list/2003-August/msg00115.html
http://www.greenfly.org/tips/usb_drive.html
http://www.xs4all.nl/~bsamwel/usb_storage_on_debian.html



#!/bin/sh
#
# USB mount script
# Copyright  2006-05-16  Dan Ramaley
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 
USA
#
#
# This script should be run by hotplug when a USB drive is plugged in.
# Unfortunately, there is not a reliable way to determine the new
# drive's /dev entry. So this script simply looks for SCSI devices that
# are not mounted but are listed in /etc/fstab, and attempts to mount
# them.
#
# To install:
# Move this script to /etc/hotplug/usb/usb-storage. Add a line to
# /etc/hotplug/usb.usermap that looks like:
# usb-storage 0x00f 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00

PATH=/sbin:/bin:/usr/sbin:/usr/bin

# This will be needed once a proper remove routine is written.
#function link {
#    if [ -n "$REMOVER" ]; then
#        mkdir -p `dirname "$REMOVER"`
#        ln -s "$0" "$REMOVER"
#    fi
#}

# If device name or label is found in fstab, mount it and create an
# unmount script.
function mountit {
    mkdir -p `dirname "$REMOVER"`
    echo '#!/bin/sh' > "$REMOVER"
    chmod a+x "$REMOVER"

    # Get a list of devices that are mounted
    #MOUNT=`mount | cut -d ' ' -f 1`
    MOUNT=`mount | grep -Eo '^/dev/\w+'`

    for drive in sda sdb sdc sdd sde sdf sdg sdh ; do
        MOUNTED=`echo $MOUNT | grep "/dev/$drive"`
        # If the drive is a block device and is not mounted
        if [ -b "/dev/$drive" -a -z "$MOUNTED" ] ; then
            for partition in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ; do
                DEV="/dev/$drive$partition"
                if [ -b "$DEV" ] ; then
                    LABEL=`e2label "$DEV"`
                    # Mount the partition by name
                    if grep -wq "^$DEV" /etc/fstab ; then
                        mount "$DEV"
                        #link
                        echo "/bin/umount -f \"$DEV\"" >> "$REMOVER"
                    # Mount the partition by label
                    elif grep -wq "^LABEL=$LABEL" /etc/fstab ; then
                        mount -L "$LABEL"
                        #link
                        echo "/bin/umount -f \"$DEV\"" >> "$REMOVER"
                    fi
                fi
            done
        fi
    done
}


case "$ACTION" in
    add)
        # Unfortunately, the actual device is not created until *after*
        # this script is run, so the useful parts have to be run in a
        # subshell with a delay.
        # If you want to handle different devices differently, put a 
case
        # statement here that switches on $PRODUCT.
        ( sleep 2; mountit ) &
        ;;
    remove)
        # This needs to be fleshed out into a proper remove routine. The
        # current method is rather hackish.
        ;;
esac


------------------------------------------------------------------------
Dan Ramaley                            Dial Center 118, Drake University
Network Programmer/Analyst             2407 Carpenter Ave
+1 515 271-4540                        Des Moines IA 50311 USA


More information about the Cialug mailing list