Usb-mount - a desktop user interface to USB-storage (Linux 2.4 and 2.6)

These scripts work for me on Fedora Core 2 and Redhat 9. Several people have reported that they can be made to function on many other Linux distributions with minor configuration changes and some editing. To get them to work on your system you may need knowledge of Linux and shell scripting. If the scripts don't work out of the box, you should look for errors in /var/log/messages (or where ever your system log is kept). If you are not comfortable with reading logs and editing scripts - perhaps these scripts aren't for you.

I no longer have any Linux systems with 2.4 kernels, I think the scripts will still work. All recent modifications have only been tested on Fedora Core 4 running kernel 2.6.

Many distributions are now adequately handling usb - so I haven't spent much time improving usb-mount. If anyone wants to fork a new version, feel free. The new kernel, dbus, etc should make it quite easy to create a simpler script from scratch.

Here is a tar that contains a the scripts, starting config files, documentation and installation instructions.

Here is a URL describing setting it up under Debian with udev http://www.xs4all.nl/~bsamwel/usb_storage_on_debian.html

v0.96
A small bunch of changes:
  • More fixes for newer kernels/distibutions that take longer to setup usb devices;
  • Handle flash card readers that have more than one device (thanks to Edward);
  • Ignore non usb-storage devices (thanks to Wolfgang Klein).
v0.95
Fix for newer kernels/distibutions that take longer to setup usb devices.
  • Usb-mount now loops for up to 10 seconds waiting for a device to be fully setup.
  • Added a DESKTOP_FOLDER_NAME variable to the config.
v0.94
This release contains significant improvements to partition recognition:
  • The diskpart utility, which used to be optional, is now mandatory. Usb-mount now uses diskpart to detect whether a device has a valid partition table.
  • Devices with no partition tables will now be correctly recognised and mounted. Note that the default value of the NO_PARTITIONS_SUFFIX variable is now "" - which works for both Redhat with kernel 2.4 and and Fedora on kernel 2.6.
  • Multiple partition devices are now handled as under one device icon. For example, a 5 partition USB IDE enclosure will now appear as one disk-icon with sub folders for each of the 5 partitions. The mount/umount actions will apply to the entire multi-partition device.
  • I've changed to the old-style awk -v argument to support distros that use mawk.
  • Changed to a default mount option of shortname=mixed for fat32 this seems to work better when dealing with Java source etc.
  • Added a SUDO variable in case sudo isn't in the default path.
  • Added a USE_WHO setting to distros where DO_CONSOLE_USER doesn't work.
v0.93
Some minor fixes and improvements stemming from feedback after the v0.92 release:
  • Fixed ownership of desktop files on Icon changes.
  • Added a directory mask.
  • Made the location of lockfile a config option.
  • Finished desktop signal actions that accidentally made it into v0.92. Controlled by the DESKTOP_SIGNALS and ONLY_SIGNAL_ON_REQUEST options.
  • Made the terminate signal default to -HUP
  • Use -o vfat for FAT16 - fixes mouting problems for some users.
If you enable DESKTOP_SIGNALS, you will have to delete any existing desktop icons so that they will be recreated with the new options. I'm unsure has to how useful the desktop signal actions will be - feedback is welcome. They might be useful if the following combination of settings are enabled:
TERMINATE_SIGNAL="-HUP"
KILL_SIGNAL="-KILL"
ONLY_SIGNAL_ON_REQUEST=1
DESKTOP_SIGNALS=1
UMOUNT_OPTIONS=""
          
This will disable the lazy option to umount - you will then get an error on unmounts if processes are still using the device. In theory you should then be able to then manual select an unmount-with-signal action to terminate the processes and properly unmount the device. But if you have already removed the device, you may still run into problems with corruption and possibly some confusion in the kernel layers.
v0.92
Usb-mount has been enhanced in several areas:
  • Usb-mount now uses the fuser command to determine if processes are using a device during an unmount processing.
  • If Usb-mount v0.92 is configured not to use the lazy (-l) option in UMOUNT_OPTIONS, it will now raise errors if a process is using a device during unmounting and the umount will not actually take place - so you will now know that there is a problem - but if you have already removed the device you may still be in a fix. Some background info: by default usb-mount uses the -l lazy umount option to allow a umount to proceed even if processes are using the mounted media - processes still using a removed device may attempt to write to the device and these processes may possibly corrupt any replacement device. In practice if you treat usb devices like floppies, and not like permanent working spaces, this isn't usually a problem. The kernel currently lacks a perfect way out in this situation and the -l umount option is a hack to get around the problem that umount -f (force) option doesn't work.
  • Usb-mount may now optionally signal and kill processes that are using a device during unmount processing. This has the advantage that the filesystem will (truly) unmount immediately, but on the downside applications may terminate without saving work. By default this new behavior is turned off. This feature is controlled by TERMINATE_SIGNAL and KILL_SIGNAL. If you decide to use this option, be careful to save your work before unplugging any devices.
  • Under kernel 2.6, usb-mount now fully matches SCSI host-number, bus, scsi-id, and lun. Until kernel 2.6 it wasn't possible to deterministically cross reference from the SCSI sub-system to the USB sub-system and obtain anything more than the host-number. There are situations under 2.4 where the host-number is insufficient to recognise a device and it will fail to mount. In practice no one using usb-mount on 2.4 has reported any problems - probably because complex SCSI-based desktop systems are relatively rare.
  • The differences in the usb-mount code for kernel 2.4 and 2.6 have been reduced.
  • The ALPHABETIC_DEVICE_NAMES option is now unnecessary and has been removed.
  • If all of the above gives you the impression that USB-removable-media is a bit hackey in the linux world - then you would be right - but similar issues exist on more popular operating systems (which is not really an excuse, but it makes me feel better anyway).
v0.91
Fix a potential security problem - anyone with privilege to run usb-mount can umount devices other than those mounted under the usb mount path (pointed out by Jeff Minelli). Added a dirsync to help prevent data loss when a device is yanked out. Cleaned up the mount options - added vfat handling. Added an option for systems that don't use alphabetic device names. Added an option to try and resolve the issues with devices with no partitions - if you have one one these please give me some feedback on what works.
v0.9
I've cleaned up the procedure for handling detached devices that haven't first been unmounted. Under Linux 2.6, I found I could detect detached devices by using a combination of sg_map and mount. Under 2.4 I used /proc/partitions to do the same job (thanks to due to Alan McCluskey for drawing my attention to /proc/partitions). These changes also eliminated the use of /var/run/usb to store partition maps. The /var/run/usb directory is still used to store a lock file, but that's all (for now). Richard Tresidder pointed out I should create /var/run/usb if it doesn't exist.
v0.8
I've made some more changes because now that more than a year has past, I realise usb-mount is no longer a temporary solution. There are new installation instructions because I've changed the organisation of installation directory to be more practical. To make upgrades easier config parameters have been removed to an /etc/usb-mount.conf or /usr/local/etc/usb-mount.conf - a config file is now required. Usb-mount's behavior on kernel 2.6 has been improved - got rid of bogus messages. MOUNT_OPTIONS and and UMOUNT_OPTIONS settings have been added to allow for additional generic mount/umount options. Feedback messages have been further improved. A DEBUG setting has been added. Added more doc's including a couple of man pages. Richard Tresidder contributed some distro portability fixes, additional messages, a sync option on mount. Added (slightly) more accurate version history comments. Added a TODO list.
v0.7
Fixes the major problem of correctly identifying the scsi device(s) to mount, but requires an additional package, sg3_utils, to do so. You might not see this problem if you only have a single device plugged in at any given time. This version also supports Linux 2.6 and benefits from it by being able to more precisely identify what to mount. Remember, you will need to get sg3_utils as part of the upgrade.

When I bought my first usb device I had to figure out a way for it interact with the KDE desktop. I rejected playing with /etc/fstab and a modified KDE floppy desktop entry for several reasons:

I decided to side step the issues by interacting directly with the Linux hotplug utilities without involving fstab, kudzu or the automounter. It might not be appropriate on a server system, but on a desktop system it provides a reasonably intuitive interface. When the hotplug module signals a change in the usb-storage, I run a script that does the following:

  1. Obtain a list of all current mounts.
  2. Gather the state of all /proc/scsi/usb-* devices.
  3. Unmount any listed in (1) that are listed as unattached by (2). As well as umounting unattached devices, remove their mount-points, so that any attempt to access them will cause an error.
  4. Mount any listed as attached in (2) that weren't listed in (1) by creating generic mount points called usb-device-1, usb-device-2, an so on. Mount them with the uid option set to the owner of the console.
  5. As each mount-point is created, optionally create .desktop files for each mount-point, The .desktop files are created in the Desktop folder of the console user.

Once a desktop file has been created, further mounts and umounts will toggle the icon of the desktop file to indicate whether the mount-point is active.

The uid and console-owner related behavior can be disabled or hardwired to specific uid's and gids by editing definitions at the top of the usb-mount script.

By default errors are logged to the syslog at the emerg priority. On Redhat this will cause a dialog window to pop up.

Here are some desktop screen shots.

The todo list includes Firewire and storage devices with removable media. I think Firewire might be easy. Devices with removable media might require the integration of supermount - but I'm not sure because I don't own such a device (yet).

Integration/Design Issues

There are many issues involved with fully automated desktop integration of USB/Firewire/IEEE1394 drives. As a guide to anyone else seeking to address this problem, here are the major issues I have encountered, as well as some notes on how usb-mount addresses them:

Last modified: Wed Jan 25 10:35:02 NZDT 2006