Version 3 of USB FAT Semi-Auto-Mount

Updated 2009-02-02 00:55:38 by peterc


This is one of those little scripts you write to make life a bit more convenient. It somewhat replicates the behaviour of the Mac OS X automounter where your USB disks simply appear on your desktop when you plug them in. I use it to get similar behaviour on my Linux box, mounting disks to a mount point by their disk label. The only real difference: we run it manually, instead of waiting for it to trigger whenever the OS feels like it.

The script

 # By peterc
 set mountdirroot "/multimedia"
 set userid pc
 set groupid users

 proc get_mounted {} {
  set tmp [split [exec /bin/mount] "\n"]
  set ::mountdevices [list]
  set ::mountpoints  [list]
  foreach t $tmp { lappend ::mountdevices [lindex [split $t " "] 0] }
  foreach t $tmp { lappend ::mountpoints [lindex [split $t " "] 2] }


 # Mount any devices present
 set pause 0
 foreach device { sda1 sda2 sda3 sda4 sdb1 sdb2 sdb3 sdb4 sdc1 sdc2 sdc3 sdc4 sdd1 sdd2 sdd3 sdd4 sde1 sde2 sde3 sde4 sdf1 sdf2 sdf3 sdf4 } {
  if {[catch { exec /usr/bin/mlabel -i /dev/$device -s :: } tmp ]} { continue }
  set devlabel [lindex $tmp 3]
  puts -nonewline "Found $::mountdirroot/$devlabel at /dev/$device "
  if {[lsearch -exact $mountdevices /dev/$device] > -1} {
   puts "and it is already mounted."
   } else {
   puts "and will mount it now."
  catch { file mkdir /multimedia/$devlabel } ;# We don't really care if it already exists.
  exec mount -o rw,noexec,nodev,sync,noatime,uid=$userid,gid=$groupid,shortname=mixed /dev/$device $mountdirroot/$devlabel


 # Remove any directories present where nothing is mounted.
 set mountdirs [glob -tails -directory $::mountdirroot *]
 foreach d $mountdirs {
  if {[lsearch -exact $mountpoints $mountdirroot/$d] < 0} {
   puts -nonewline "Found surplus mount point $d and will remove it: "
   if {[catch {file delete $mountdirroot/$d} err]} { puts "$err" } else { puts "done." }
   set ::did(rm) 1
   } else {
   # puts "Mount point $d is being used. Will leave that alone."
 if {![info exists ::did(rm)]} { puts "No surplus mount points found." }

Example output

 emu:~# ./
 Found /multimedia/TWO at /dev/sda1 and it is already mounted.
 Found /multimedia/ONE at /dev/sdb1 and will mount it now.
 Found /multimedia/THREE at /dev/sdc1 and it is already mounted.
 Found surplus mount point TEST and will remove it: done.

Config and other things of note

This automount script requires that mtools [L1 ] is installed on your system to read the disk label information. mtools includes FAT support up to FAT32. Other filesystems won't be recognised by mtools and won't be mounted.

The main configurables are mountdirroot (in this case, /multimedia), userid and groupid (username and group you want given to the mounts).

Unlabelled disks and labels with spaces will probably cause this script some grief but not really cause anything serious to happen. Replace spaces with underscores or dashes in your disk labels.

Double-check what devices your harddrives are and remove them from the devices list if they conflict. Better safe than sorry, eh. If you're really keen, I guess you could populate this list by running a glob on your /dev tree, but, I've never had a USB use any device not in that list (so far!).

Spot for comments and questions