Version 4 of USB FAT Semi-Auto-Mount

Updated 2009-02-02 01:31:45 by peterc

Rationale

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

 #!/usr/local/bin/tclsh
 # 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] }
  }

 get_mounted

 # 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."
   continue
   } 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
  }

 get_mounted

 # 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:~# ./mount-usb-fat.sh
 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. You may need to add "mtools_skip_check=1" to your /etc/mtools.conf to ensure your random USB disks don't fall foul of mtools dreaded "total number of sectors not a multiple of sectors per track" error. This error seems to crop up fairly regularly with larger FAT32 partitions.

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!).

Other filesystems (NTFS, ext2/3/4, ReiserFS, ISO9660, etc) won't be recognised by mtools and therefore this script won't attempt to mount them.


Spot for comments and questions