#1: Data integrity
If you can live with really poor write performance then mount the device in synchronous mode:
Code: Select all
/dev/sda1 /mnt/usb-storage auto sync,dirsync 0 0
That way data gets written to the stick immediately, your application will be blocked until the write is complete. Of course writing is quite slow, so don't use that for swap
On the other hand, there will be no stale buffers. It's OK to unplug whenever the stick is not being accessed.
The above line is the basis for everything described below.
#2: Automounting
I don't know about supermount (never used it) but for my stick I'm using the standard hotplug infrastructure: enable hotplug in the kernel and emerge sys-apps/hotplug. This will (among other things) give you scripts in /etc/hotplug.d and /etc/hotplug/<SUBSYSTEM> (for example: /etc/hotplug/usb)
The scripts in /etc/hotplug.d (usually just "default") will switch incoming hotplug events
by subsystem, you'd modify that if have a new kind of hotplug system. Not interesting.
Now in /etc/hotplug/usb you would place scripts with
the name of a kernel module, e.g. /etc/hotplug/usb/usb-storage, looking like this:
Code: Select all
#!/bin/bash
# NOTE: to make this work, you *MUST* use the options "sync" and "dirsync" in your /etc/fstab !!!
# otherwise you *WILL* lose data
#create a script to undo our actions upon removal
echo "#!/bin/bash" > $REMOVER
chmod a+x $REMOVER
if [ "$INTERFACE" = "8/6/80" ]; then
mount /mnt/usb-storage
echo "umount -f /mnt/usb-storage" >> $REMOVER
fi
This script will be executed when and if
* there is a USB hotplug event
* this event has already caused the module usb-storage.o to be loaded
The environment variables $REMOVER, $INTERFACE and $PRODUCT (and some others I don't remember) will be set by the hotplug infrastructure. The check for $INTERFACE=8/60/80 ensures that we are really dealing with a storage device. If your storage device needs special handling you might also check $PRODUCT (check your hotplug logs to find out your stick's product ID).
The file $REMOVER (if it exists) will be executed upon removal of the exact device it was created for.
The above script is all fine and dandy if you only have one user, one stick, and one location to mount it on. And else
Well, try the following script for /etc/hotplug/usb/usb-storage, which is an extension of the previous example:
Code: Select all
#!/bin/bash
# NOTE: to make this work, you *MUST* use the options "sync" and "dirsync" in your /etc/fstab !!!
# otherwise you *WILL* lose data
#create a script to undo our actions upon removal
echo "#!/bin/bash" > $REMOVER
chmod a+x $REMOVER
# INTERFACE=8/6/80 means "mass storage device"
if [ "$INTERFACE" = "8/6/80" ]; then
mount /mnt/usb-storage
HOSTNAME=`/bin/hostname`
OWNER=`cat /mnt/usb-storage/.owner.$HOSTNAME`
umount /mnt/usb-storage
#remount as correct user
sudo -u $OWNER mount /mnt/usb-storage
# apply directory mappings
HOMEDIR=`grep $OWNER /etc/passwd | awk -F: '{ print $6 }' `
echo $HOMEDIR
awk --assign homedir=$HOMEDIR --assign remover=$REMOVER -F= '
{
system( "mount --bind /mnt/usb-storage/" $1 " " homedir "/" $2 )
system( "echo umount -f " homedir "/" $2 " >> " remover )
}
' /mnt/usb-storage/.directory-map.$HOSTNAME
echo "umount -f /mnt/usb-storage" >> $REMOVER
fi
This script requires
* Kernel 2.4 or higher
* file on the stick: .owner.$HOSTNAME
* file on the stick: .directory-map.$HOSTNAME
* every usb-stick-user must be able to "sudo mount"
* every usb-stick-user must be able to "mount /mnt/usb-stick"
without resorting to sudo
How it works:
1) Mount the stick as root an read the file ".owner.HOSTNAME", where HOSTNAME is the local name. This file contains only a username; you can have a different username on each machine you use the stick on (very useful for taking stuff home from work!)
2) Unmount the root-mounted stick
3) Mount the stick again at the same position, but this time as the user; this action will give you correct permissions on the stick (extremely useful if you're not using FAT)
4) Now read ".directory-map.HOSTNAME", which again allows for one mapping per host. Every line in there is of the form "STICKDIR TARGETDIR". No wildcards/regexes allowed!
5) Bind /mnt/usb-stick/STICK-DIR to ~/TARGETDIR (this is like mounting, just different

)
6) During all of that, keep the remove script up-to-date
The result: you can keep multiple directories on your stick (e.g. ~/Mail, ~/News, ~/ICQ/Logs), and have them mounted to the right directory automagically. You may remove the stick whenever it's not being accessed. You can do so on multiple machines, regardless of differences in username and layout of your homedir.
Hope that helps - have fun moving your data
WARNING: I've tried to keep out at least some obvious security holes. However, it's your machine and they're your users. Trust them or don't, but don't blame me if you get f*ucked using these scripts. The biggest flaw is keeping the user and directory mappings on the stick. If you know of any way to get around that please speak up.