Package: usbmount
Version: 0.0.14-1
Severity: wishlist

Hi,

while discussing usb and automounting on debian-user-german lately I
wrote something like a mini-usbmount in python and later on figured that
usbmount has evolved a lot since last time I looked at it. I figured
that the attached patch might be useful, especially for people who
plug/unplug devices a lot and end up looking into each usb[0-7] dir to
find out where the new device was mounted. 

The patch basically just gets vendor/model from sysfs (I just pushed the
code for that further to the beginning) and then uses
<vendor>_<model>_<device> as the name for the mountpoint in /media. If vendor
and model are not available it uses just /media/usb_<device>, if any of
the both is available it is used. <device> here is the block device name
(i.e. sdXY or as it is here smallusb1, usbstick1 or cfcard). 

To do the unmounting the built-up mountpoint name is saved in a file in
/var/run/usbmount/vmmountpoints, called the same as the mountpoint. Upon
unmounting the directory in /media is removed.

The patch uses sed in 2 or 3 locations, if you know that bash can do the
same, please feel free to change it, such that usbmount doesn't need to
depend on sed. I'm not that much of a bash hacker to know about this
stuff.

The behaviour of usbmount can be controlled with a new configuration
variable called VENDORMODELMOUNTPOINTS, so that the old
mountpoint-behaviour is still intact (and the default).

Andreas

-- System Information:
Debian Release: testing/unstable
  APT prefers unstable
  APT policy: (990, 'unstable'), (500, 'experimental')
Architecture: i386 (i686)
Shell:  /bin/sh linked to /bin/bash
Kernel: Linux 2.6.12-cherry+noradeon+8139c+
Locale: LANG=de_DE.UTF-8, LC_CTYPE=de_DE.UTF-8 (charmap=UTF-8)

Versions of packages usbmount depends on:
ii  lockfile-progs                0.1.10     Programs for locking and unlocking
ii  sed                           4.1.4-2    The GNU sed stream editor
ii  udev                          0.065-1    /dev/ management daemon

usbmount recommends no packages.

-- no debconf information
diff -ur usbmount-0.0.14.org/usbmount usbmount-0.0.14/usbmount
--- usbmount-0.0.14.org/usbmount        2005-07-08 18:51:02.000000000 +0200
+++ usbmount-0.0.14/usbmount    2005-08-07 01:06:47.223626808 +0200
@@ -45,6 +45,7 @@
 MOUNTOPTIONS=""
 FS_MOUNTOPTIONS=""
 VERBOSE="no"
+VENDORMODELMOUNTPOINTS="no"
 
 # Read configuration file.
 if test -r /etc/usbmount/usbmount.conf; then
@@ -95,32 +96,6 @@
        # types to mount.
        if in_list "$fstype" "$FILESYSTEMS"; then
 
-           # Search an available mountpoint.
-           for v in $MOUNTPOINTS; do
-               if test -d "$v" \
-                   && ! grep -q "^[^ ][^ ]*  *$v " /proc/mounts; then
-                   mountpoint="$v"
-                   log debug "mountpoint $mountpoint is available for $DEVNAME"
-                   break
-               fi
-           done
-           if test -n "$mountpoint"; then
-               # Determine mount options.
-               options=
-               for v in $FS_MOUNTOPTIONS; do
-                   if expr "$v" : "-fstype=$fstype,."; then
-                       options="`echo \"$v\" | sed 's/^[^,]*,//'`"
-                       break
-                   fi
-               done
-               if test -n "$MOUNTOPTIONS"; then
-                   options="$MOUNTOPTIONS${options:+,$options}"
-               fi
-
-               # Mount the filesystem.
-               log info "executing command: mount -t$fstype 
${options:+-o$options} $DEVNAME $mountpoint"
-               mount "-t$fstype" "${options:+-o$options}" "$DEVNAME" 
"$mountpoint"
-
                # Determine vendor and model.
                vendor=
                if test -r "/sys$DEVPATH/device/vendor"; then
@@ -145,6 +120,68 @@
                fi
                model="`echo \"$model\" | sed 's/^ *//; s/ *$//'`"
 
+           mountpoint=
+           if test "$VENDORMODELMOUNTPOINTS" == "yes" ; then
+               log debug "Building mountpoint from Vendor, Model and Device"
+               if test -n "$vendor" ; then
+                       mountpoint="${mountpoint}${vendor}_"
+               fi
+               if test -n "$model" ; then
+                       mountpoint="${mountpoint}${model}_"
+               fi
+
+               shortdevname="`echo \"$DEVNAME\" | sed 's#/.*/\(.*\)#\1#'`"
+               if test -z "$mountpoint" ; then
+                       mountpoint="${mountpoint}usb/${shortdevname}"
+               else
+                       mountpoint="${mountpoint}${shortdevname}"
+               fi
+
+               # save the non-media-part of the mounpoint for the file in 
+               # /var/run/usbmount/vmmountpoints/ and set mountpoint to a 
+               # fully qualified path in /media
+               mountpointend="$mountpoint"
+               mountpoint="/media/$mountpoint"
+               log debug "Mountpoint built: $mountpoint"
+               
+               #Create the mountpoint if it doesn't exist
+               if ! test -d "$mountpoint" ; then
+                       log debug "Creating mountpoint $mountpoint"
+                       mkdir -p $mountpoint
+               fi
+               
+           else
+                   # Search an available mountpoint.
+                   for v in $MOUNTPOINTS; do
+                       if test -d "$v" \
+                           && ! grep -q "^[^ ][^ ]*  *$v " /proc/mounts; then
+                           mountpoint="$v"
+                           log debug "mountpoint $mountpoint is available for 
$DEVNAME"
+                           break
+                       fi
+                   done
+           fi
+           if test -n "$mountpoint"; then
+               # Determine mount options.
+               options=
+               for v in $FS_MOUNTOPTIONS; do
+                   if expr "$v" : "-fstype=$fstype,."; then
+                       options="`echo \"$v\" | sed 's/^[^,]*,//'`"
+                       break
+                   fi
+               done
+               if test -n "$MOUNTOPTIONS"; then
+                   options="$MOUNTOPTIONS${options:+,$options}"
+               fi
+
+               # Mount the filesystem.
+               log info "executing command: mount -t$fstype 
${options:+-o$options} $DEVNAME $mountpoint"
+               mount "-t$fstype" "${options:+-o$options}" "$DEVNAME" 
"$mountpoint"
+               if test "$VENDORMODELMOUNTPOINTS" == "yes" ; then
+                   # Save the mountpoint name for unmounting in 
/var/run/usbmount
+                   echo "$mountpointend" > 
"/var/run/usbmount/vmmountpoints/$mountpointend"
+               fi
+
                # Run hook scripts; ignore errors.
                export UM_DEVICE="$DEVNAME"
                export UM_MOUNTPOINT="$mountpoint"
@@ -170,13 +207,30 @@
     # Test if it is mounted.
     while read device mountpoint fstype remainder; do
        if test "$DEVNAME" = "$device"; then
-           # If the mountpoint and filesystem type are maintained by
+           # make a copy of the actual mountpoint for the case when we
+           # use VENDORMODELMOUNTPOINTS
+           mountdir="$mountpoint"
+
+           if test "$VENDORMODELMOUNTPOINTS" == "yes" ; then
+               # If we use VENDORMODELMOUNTPOINTS load the available
+               # mountpoints from /var/run/usbmount
+               # also remove the preceding "/media/" from the mountpoint 
variable
+               MOUNTPOINTS="`ls /var/run/usbmount/vmmountpoints/`"
+               mountdir="`echo $mountpoint | sed 's#/media/##'`"
+           fi
+           
+           # If the mountdir and filesystem type are maintained by
            # this script, unmount the filesystem.
-           if in_list "$mountpoint" "$MOUNTPOINTS" \
+           if in_list "$mountdir" "$MOUNTPOINTS" \
                && in_list "$fstype" "$FILESYSTEMS"; then
                log info "executing command: umount -l $mountpoint"
                umount -l "$mountpoint"
 
+               if test "$VENDORMODELMOUNTPOINTS" == yes ; then
+                   rmdir "$mountpoint"
+                   rm "/var/run/usbmount/vmmountpoints/$mountdir"
+               fi
+
                # Run hook scripts; ignore errors.
                export UM_DEVICE="$DEVNAME"
                export UM_MOUNTPOINT="$mountpoint"
diff -ur usbmount-0.0.14.org/usbmount.conf usbmount-0.0.14/usbmount.conf
--- usbmount-0.0.14.org/usbmount.conf   2005-04-08 16:05:10.000000000 +0200
+++ usbmount-0.0.14/usbmount.conf       2005-08-07 00:24:47.045752080 +0200
@@ -2,6 +2,14 @@
 # storage devices when they are plugged in and unmounts them when they
 # are removed.
 
+# The following configuration option defines wether the mountpoints to use are
+# taken from the list in MOUNTPOINTS or are created by putting a new directory
+# into /media using the form vendor_model_device. The former is default, as
+# some usb storage devices don't tell their vendor or model. But if you
+# know your devices do, the latter let you more easily see which mountpoint
+# corresponds to which device.
+VENDORMODELMOUNTPOINTS="no"
+
 # Mountpoints: These directories are eligible as mointpoints for USB
 # mass storage devices.  A newly plugged in device is mounted on the
 # first directory in this list that exists and on which nothing is

Reply via email to