#!/bin/sh # this script is automatically called by hotplug at each plug/unplug event of an usb-storage device. (usb keys, mp3 jukeboxes, digital cameras...) modprobe /dev/scsi # first version by Christophe Combelles (ccomb.free.fr) # tuned/modified/debugged by myself # list devices of type usb-storage, i.e., all devices that are supported by the usb mass-storage driver (usb keys, mp3 stick, APN, etc...) list=`find /proc/scsi -type f|grep usb-storage` for i in $list; do # for each device, get infos vendor=`cat $i |grep Vendor:|sed 's/ *Vendor: *//g'|sed 's/ /_/g'` product=`cat $i |grep Product:|sed 's/ *Product: *//g'|sed 's/ /_/g'` host=`cat $i |grep Host|cut -c13` guid=`cat $i |grep GUID:|sed 's/ *GUID: *//g'|sed 's/ /_/g'` attached=`cat $i |grep Attached:|cut -c16` #here, a workaround for my XDrive II (by Vosonic) that shows no Vendor name if [ "$guid" == "0d7d127000000a3703002602" ]; then vendor="xdrive" fi # find partitions in /dev/scsi/host${host} partitions=`find /dev/scsi/host${host} -type b -name "part*"` # if not attached, remove ! if [ "${attached}" = "N" ]; then #umount the device if it's no more atached, since scsiadd can't work properly if the device is still mounted #the following scsiadd commands might me replaced by the original ones if it works with devices unmounted #The device is not attached, so we check whether device is mounted yet #mounted.device contains full path of mounted but not attached usb-storage devices mount | grep "/dev/scsi/host${host}" | cut -f 1 -d " " > /etc/mounted.devices for i in `cat /etc/mounted.devices`; do bus=`echo "$i" | cut -b 20` target=`echo "$i" | cut -b 28` lun=`echo "$i" | cut -b 33` umount "$i" #previously, this command was something like echo "scsi remove-single-device ${host}" > /proc/scsi #doesn't work on my debian, so i replaced it by scsiadd -r command scsiadd -r ${host} ${bus} ${target} ${lun} > /dev/null done rm /etc/mounted.devices #partitions is set to "none" to enter in the "for p in..." loop and update fstab and /mnt partitions="none" else # if attached and no partitions, add the device to the scsi chain. if [ -z "${partitions}" ]; then #same comment as previously for scsiadd scsiadd ${host} 0 0 0 > /dev/null timeout=20 # now we can detect partitions while [ -z "${partitions}" ]; do timeout=$((${timeout}-1)) sleep 1 partitions=`find /dev/scsi/host${host} -type b -name "part*"` if [ ${timeout} -eq 0 ]; then partitions="none" continue fi done fi fi #for each partition, create a mountpoint and an fstab entry for p in ${partitions}; do # create a mountpoint name if [ `echo ${partitions}|wc -w` -eq 1 ]; then partnum="" else partnum="_`basename $p`" fi if [ "${vendor}" = "${product}" ]; then mntpoint=/mnt/${vendor} else mntpoint=/mnt/${vendor}${partnum} fi # create the mountpoint and the fstab entry if [ "${attached}" = "Y" ]; then if [ "$p" = "none" ]; then exit fi if [ ! -d ${mntpoint} ]; then mkdir ${mntpoint} fi if ! grep "$p ${mntpoint} auto umask=000,user,rw,sync 0 0" /etc/fstab >/dev/null 2>&1; then echo "$p ${mntpoint} auto umask=000,user,rw,sync 0 0" >> /etc/fstab mount -a fi #mount the device, even if the mountpoint was already in the fstab mount ${mntpoint} else #the device is not attached #purge fstab and /mnt if [ -d ${mntpoint} ]; then rmdir ${mntpoint} fi cat /etc/fstab | grep -v "$mntpoint" > /etc/fstab.updated if diff /etc/fstab /etc/fstab.updated >/dev/null ; then rm /etc/fstab.updated else if [ -s /etc/fstab.updated ]; then cat /etc/fstab.updated > /etc/fstab mount -a fi rm /etc/fstab.updated fi fi done done