Solution: automounting usb-storage devices as user

Shawn Iverson shawn at nccsc.k12.in.us
Tue Feb 10 12:50:40 UTC 2004


The following script will automount usb-storage devices as the first user
logged in, concurrent with the behavior of other devices, such as the floppy
or CD-ROM.

See bugzilla report before using this script:  
https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=113787

</etc/hotplug/usb/usb-storage START>
#!/bin/bash

# Proposed "universal" usb-storage script
#
# 02/08/2004
# Shawn Iverson 
# shawn<at>nccsc.k12.in.us
#
# Taken in part from: FedoraNEWS.ORG 2004-01-27 tip #30
# 		          bugzilla.redhat.com Report 113787
#
# PURPOSE: Determines the device to be automounted and provides a workaround

#	     for the device being mounted in the context of root.
# 
# THEORY:  Following in step with dynamic /etc/fstab updates, this script 
#	     should in theory accommodate a wide range of /dev/sd* devices.
#
# METHOD:  Read the last entry of /etc/fstab following updfstab execution
#	     and grab the first user logged in by looking at the device
owner.
#	     This script assumes that the last entry of /etc/fstab
identifies
#	     the usb-storage device.
#
# TODO:   Probably need to somehow verify that the last line in /etc/fstab
#         did in fact originate from updfstab in hotplug add, which would 
#	    have just executed.  If the last line were a different /dev/sd* 
#	    device, that device may be mounted.
#
LASTLINE=`tail -n 1 /etc/fstab`
USBDEV=`echo $LASTLINE | egrep -o ^\/dev\/sd[[:alnum:]]*`
if [ -n $USBDEV ] ; then
   MNTPOINT=`echo $LASTLINE | awk '{print $2}'`
   
   # Determine the user context in which the device should mount
   # NOTE:  The device listed in the last line of /etc/fstab must
   #        have an /etc/security/console.perms entry in order for 
   #        the user context to be valid.
   MNTUSER=`ls -l $USBDEV | awk '{print $3}'` 
   # Mount the device in the context of the user
   su - -c "mount $MNTPOINT" $MNTUSER
   
   # TODO:  Is there a way to force data to be written immediately to the
   #	    device?  Doing so would prevent data loss when the device is
   #	    unplugged before it is unmounted.   
 
   # Remount to enable special options
   # NOTE:  Doing this prevents the user from unmounting manually.
   # mount -o remount,sync,dirsync,nosuid,nodev,noatime $MNTPOINT

   # Set up the umount script
   echo '#!/bin/bash' > "$REMOVER"
   echo umount $MNTPOINT >> "$REMOVER"
   chmod u+x $REMOVER
fi
</etc/hotplug/usb/usb-storage END>

-- 
Shawn Iverson
Technology Associate
New Castle Community School Corporation
765-593-6691
shawn at nccsc.k12.in.us








More information about the users mailing list