#!/bin/sh # name : /usr/sbin/updfstab # this script is automatically called by hotplug at each plug/unplug event of an usb-storage device. (usb keys, mp3 jukeboxes, digital cameras...) # be sure the driver is loaded (tested on debian only, with default settings of devfs) modprobe /dev/scsi # list devices 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` attached=`cat $i |grep Attached:|cut -c16` # find partitions in /dev/scsi/host${host} partitions=`find /dev/scsi/host${host} -name "part*"` #echo "partitions = <${partitions}>" # if not attached, remove ! if [ "${attached}" = "N" ]; then echo "scsi remove-single-device ${host}" > /proc/scsi/scsi partitions="none" else # if attached and no partitions, add the device to the scsi chain. if [ -z "${partitions}" ]; then echo "scsi add-single-device ${host}" > /proc/scsi/scsi timeout=20 # now we can detect partitions while [ -z "${partitions}" ]; do timeout=$((${timeout}-1)) sleep 0,5 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}${partnum} mntpoint=/mnt/${vendor} else #mntpoint=/mnt/${vendor}_${product}${partnum} mntpoint=/mnt/${vendor}${partnum} fi # create the mountpoint and the fstab entry if [ "${attached}" = "Y" ]; then if [ "$p" = "none" ]; then #Xdialog --title "USB hotplug" --msgbox "Could not find partitions in\n${vendor}\nIs it formatted ?" 10 40 exit fi if [ ! -d ${mntpoint} ]; then mkdir ${mntpoint} #echo "created ${mntpoint}" fi if ! grep "$p *${mntpoint}" /etc/fstab >/dev/null 2>&1; then echo "$p ${mntpoint} auto user,noauto,rw,iocharset=utf8 0 0" >> /etc/fstab #echo "added $p ${mntpoint} in fstab" fi else # purge fstab and /mnt # if [ `mount|grep -c "$p on ${mntpoint}"` -eq 0 ]; then # # we can unmount now because we were mounted with option sync # fuser -uv ${mntpoint} > /tmp/toto${mntpoint} # umount ${mntpoint} # fi # if [ `mount|grep -c "$p on ${mntpoint}"` -eq 0 ]; then # # impossible to unmount...! # echo "Houston we have a problem" # fi if [ -d /mnt/${vendor} ]; then rmdir /mnt/${vendor}* #echo "removed ${mntpoint}" fi cat /etc/fstab | grep -v " /mnt/${vendor} " > /etc/fstab.updated if diff /etc/fstab /etc/fstab.updated >/dev/null 2>&1; then rm /etc/fstab.updated else if [ -s /etc/fstab.updated ]; then cat /etc/fstab.updated > /etc/fstab #echo "removed $p ${mntpoint} from fstab" fi rm /etc/fstab.updated fi fi done done