A rule needs to be added to run a script whenever udev detects that a new drive has been inserted. Create the file /etc/udev/rules.d/99-usb-automount.rules containing:
KERNEL=="sd[a-z]?", RUN+="/usr/local/bin/usb_mount",
ENV{REMOVE_CMD}="/usr/local/bin/usb_mount"
Then add the script file /usr/local/bin/usb_mount containing:
#!/bin/bash
# $Id$
#
# Script to automaticlly mount usb devices. This script is designed to work
# with udev
#
#commands used in the script
MOUNT="/bin/mount"
UMOUNT="/bin/umount -l"
MKDIR="/bin/mkdir -p"
RMDIR="/bin/rmdir"
#the root of where all auto mouted devices will be mounted to.
MOUNT_ROOT=/media
MNT_POINT=$MOUNT_ROOT/${DEVNAME##/dev/}
if [ -z "$ID_FS_TYPE" ]
then
logger "unidentified fs type: '$ID_FS_TYPE'"
exit 0
fi
logger "running $0"
logger "ACTION=$ACTION ID_BUS=$ID_BUS DEVNAME=$DEVNAME ID_FS_TYPE=$ID_FS_TYPE"
# check we are adding a usb device
if [ "$ID_BUS" != "usb" ]
then
logger "ignoring non usb device"
exit 0
fi
# check the action to process
case "$ACTION" in
add )
logger "mounting new device"
#create the directory we will mount to
$MKDIR "$MNT_POINT"
#
#mount the usb device with the following options
#
# ro - read only
# noexec - Do not allow direct execution of any binaries on the mounted file system
# nodev - Do not interpret character or block special devices on the file system
#
#$MOUNT -t $ID_FS_TYPE $DEVNAME $MNT_POINT -o ro,noexec,nodev
$MOUNT -t $ID_FS_TYPE $DEVNAME $MNT_POINT -o noexec,nodev
#check if mount suceeded
if [ $? -eq 0 ]
then
# sucess, we mounted ok.
logger "mounted $DEVNAME at $MNT_POINT"
else
# we failed to mount so remove the mount point
logger "failed to mount $DEVNAME at $MNT_POINT"
$RMDIR "$MNT_POINT"
fi
;;
remove )
logger "unmounting existing device"
# check if the device is really mounted
#if [ (grep -q "^$DEVNAME" /proc/mounts || grep -q "^$DEVNAME" /etc/mtab) ]
#then
# logger "$DEVNAME not mouted"
#endif
#unmount the device. We will refer using the mount point not the device name
#becase there is a posibility the device could be removed before we
#do the umount.
$UMOUNT "$MNT_POINT"
#remove the mount point
$RMDIR "$MNT_POINT"
logger "unmounted $DEVNAME and removed $MNT_POINT"
;;
* )
logger "ignoring unkown action"
;;
esac
No comments:
Post a Comment