Description: |
This script will allow you to plug in a USB Mass Storage Device (digital cameras, flash readers, etc) and have Linux automatically mount it, as well as unmount it on unplug. Someday Linux distros will do this for you (if they don't already), but for now, this may be handy. Requires 'hotplug' (for assignment of actions on device plug-in) and 'logger' (for writing status messages to the system log, probably /var/log/messages).
Before you ask questions, read this tutorial, which covers all prerequisites besides those I've already mentioned.
This script was designed for use with my Leica digital camera, so please excuse any hardcoded values including the string 'leica'.
|
#!/usr/bin/perl -w
use strict;
# $Id: usb_mass_storage_automount,v 1.3 2003/09/28 02:01:16 adamm Exp
+$
# This script is '/etc/hotplug/usb/leica' because I have a Leica camer
+a
# Original idea from http://www.buberel.org/linux/usb-automounter.php
#
# requires a row in /etc/hotplug/usb.usermap as described in above URL
+. ex:
# leica 0x03 0x04da 0x2373 0x0000 0x0000 0x00 0x00 0x00 0x00
+ 0x00 0x00 0x00000000
# 0x03 = Ensure correct device by matching the Vendor and Product
+ID
# 0x04da = part of PRODUCT from debugging /etc/hotplug/usb.agent
# 0x2373 = part of PRODUCT from debugging /etc/hotplug/usb.agent
# The leica's PRODUCT (see URL) looks like: 4da/2373/10
# (the last number, "10" is the device ID, and is not used)
our $USERNAME = 'adamm';
our $MOUNTPOINT = '/mnt/usb';
use File::Basename;
use IO::File;
use Sys::Syslog;
sub logInfo ($) { syslog('info', "*INFO* $_[0]") }
sub logError ($) { syslog('err', "*ERROR* $_[0]") }
sub dieNice ($) {
logError($_[0]);
cleanExit(1);
}
sub cleanExit (;$) {
logInfo('exiting');
exit($_[0] || 0);
}
openlog(basename($0),'cons,pid');
logInfo("starting");
dieNice("ACTION env var not set!") unless $ENV{ACTION};
if ( $ENV{ACTION} eq "remove" ) {
logInfo('action is "remove", nothing to do.');
cleanExit();
}
if ( $ENV{ACTION} ne "add" ) {
dieNice("unexpected value '$ENV{ACTION}' for ACTION env var");
}
dieNice('REMOVER env var not set!') unless $ENV{REMOVER};
my $remover_script = <<EndOfRemover;
#!/bin/sh
logger -p user.info -t leicaRemover -i "trying umount -f $MOUNTPOINT"
umount -f $MOUNTPOINT
logger -p user.info -t leicaRemover -i "umount returned \$?"
EndOfRemover
my $remover_fh = new IO::File "> $ENV{REMOVER}"
or dieNice("error writing remover '$ENV{REMOVER}' $!");
print $remover_fh $remover_script;
$remover_fh->close;
chmod 0755, $ENV{REMOVER} or dieNice("chmod failed. $!");
logInfo("created and chmod()ed '$ENV{REMOVER}' to auto-umount device")
+;
my $status = `su - -c 'mount $MOUNTPOINT' $USERNAME`;
logError("mount status: $status ... ($?)") if $? != 0;
cleanExit();
|