Skip to contentSkip to conversation

Proposed addition to automatically mount usb-storage devices

DISCUSSION Andrew Robinson 1 participant 1 message May 20, 2004

lore
Share & tools
lore
#0 lore raw

I'd like to offer this script as an addition to the hotplug system. It is a wrapper script for updfstab that mounts whatever updfstab adds to the /etc/fstab file. It also creates a remover script so that the device will be unmounted when it is unplugged.

The motivation for this script was an external USB drive I wanted to use on my Fedora Core 1 system. After reading as much documentation as I could tolerate, I figured out how to add the device to the /etc/updfstab.conf file. However, I could not get it to automatically mount. Greg Kroah-Hartman was kind enough to answer my query on this list. He suggested devlabel. devlabel does have an automount feature, but appeared to require an existing entry in the /etc/fstab file. This did not seem to honor the "true spirit" of hotplugging. I tried to get devlabel and updfstab to work together with no luck. A message on the Fedora mailing list suggested the user mode script, usb-storage in this case. I could not get it to work, and reading through the usb.agent script (and hotplug.functions), I saw that the usb-storage was executed before updfstab.

It seemed I was left with creating my own solution. Since updfstab is where I wanted to get the device to mount, that seemed to be the opportunity for a wrapper script. There are two other advantages to this approach. First, updfstab is called only in the special case of usb-storage, so the wrapper script would not disturb any other parts of the hotplug system. Second, the wrapper script does not require any additional configuration information. It responds to changes made by updfstab. I envision eventually a configurator that pops up the first time a device is plugged in. The configurator would ask the user if he/she would like the device mounted this time and automatically in the future. The configurator would then make the appropriate changes to the /etc/updfstab.conf file.

Given that this is the first time I've attempted to contribute to a development project and given the minutes and minutes I put into studying the problem, I would not be surprise if the answer I get back is along the lines of "Andrew you ignorant slut. If you had known this and looked at that, then you would see the correct way to do accomplish this is this." My one request is that you please be constructive in your criticism. :)

Thanks!

Andrew Robinson

###############################################################

updfstab.wrapper script:

#!/bin/bash
#

# updfstab.wrapper - monitor additions to the /etc/fstab file made
# by updfstab and mount those filesystems. In addition, create
# the remover file so that those filesystems can be unmounted
# later.
#
# HISTORY:
#
# 10-May-2004  Initial version by Andrew Robinson.
#
# $Id$

# Import the hotplug.functions file to enable use of the log

# messaging routine

cd /etc/hotplug
. ./hotplug.functions
#export DEBUG=yes

# Grab the mount point for each entry in the /etc/fstab file. Skip

# comments and blank lines

FSTAB_BEFORE=$(awk '/^#/ {next}; /^[ ]*$/ {next}; {print $2}' /etc/fstab) debug_mesg "FSTAB_BEFORE: $FSTAB_BEFORE"

# Run updfstab.

if [ -x /usr/sbin/updfstab ]
then
/usr/sbin/updfstab
else
mesg "Cannot find updfstab program."
exit
fi

# Create a similar list of entries after updfstab has manipulated

# the fstab file.

FSTAB_AFTER=$(awk '/^#/ {next}; /^[ ]*$/ {next}; {print $2}' /etc/fstab) debug_mesg "FSTAB_AFTER: $FSTAB_AFTER"

# Compare the two lists to find any additions after updfstab has run.

FSTAB_DIFF=""
for elem in $FSTAB_AFTER
do

     if [ $(echo $FSTAB_BEFORE | grep -c $elem -) -eq 0 ]
     then
         FSTAB_DIFF="${FSTAB_DIFF}${elem} "
     fi

done
debug_mesg "FSTAB_DIFF: $FSTAB_DIFF"

# If there are no differences, bail

if [ "$FSTAB_DIFF" = "" ]
then
mesg "No changes to be made"
exit
fi

# Insure that the directory for the remover script exists

if [ ! -d $(dirname $REMOVER) ]
then

     mkdir -p $(dirname $REMOVER)

fi

# Create the remover file

echo "#!/bin/bash" > $REMOVER
echo ". $HOTPLUG_DIR/hotplug.functions" >> $REMOVER

# Attempt to mount the new mountpoints. Also add the new

# mountpoints to the remover file.

for mountpt in $FSTAB_DIFF
do
mesg "mounting $mountpt"
mount $mountpt
echo "mesg \"unmounting $mountpt\"" >> $REMOVER echo "umount $mountpt" >> $REMOVER
done

# Make the remover file executable

chmod u+x $REMOVER

###############################################################

Changes to hotplug.functions to run updfstab.wrapper:

From:

if echo "$MODULE" | grep -q "usb-storage" > /dev/null 2>&1 ; then

     [ -x /usr/sbin/updfstab ] &&  /usr/sbin/updfstab

fi

To:

if echo "$MODULE" | grep -q "usb-storage" > /dev/null 2>&1 ; then
if [ -x $HOTPLUG_DIR/updfstab.wrapper ]; then
$HOTPLUG_DIR/updfstab.wrapper
elif [ -x /usr/sbin/updfstab ]; then
/usr/sbin/updfstab
fi
fi

-------------------------------------------------------
This SF.Net email is sponsored by: Oracle 10g
Get certified on the hottest thing ever to hit the market... Oracle 10g. Take an Oracle 10g class now, and we'll give you the exam FREE.
http://ads.osdn.com/?ad_id149&alloc_id�66&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel