|
Added
Link Here
|
| 1 |
#!/bin/sh |
| 2 |
# |
| 3 |
# Automount hotplugged USB block devices, by Scott Marks (smarks@mobile-mind.com) |
| 4 |
# Based on automount.hotplug by Wout Mertens (wmertens@gentoo.org) |
| 5 |
# |
| 6 |
# Linux v2.6 version |
| 7 |
# This script is released under version 2 of the GPL. |
| 8 |
|
| 9 |
# To install this, either make it /etc/hotplug/usb/usb-storage. |
| 10 |
# It needs to be executable. |
| 11 |
# |
| 12 |
# The devices will be mounted for the console user if applicable. |
| 13 |
# |
| 14 |
# To work, this needs: |
| 15 |
# - v2.6 kernel support: |
| 16 |
# - hotplugging, /proc mounted, /sys mounted |
| 17 |
# - filesystems that will be mounted, like vfat |
| 18 |
# - sh, echo, sed, stat or ls, getent or grep passwd, |
| 19 |
# mount, umount, mkdir, rmdir. logger is used if there. |
| 20 |
# |
| 21 |
# Possible improvements: |
| 22 |
# - Create a desktop entry for the device. |
| 23 |
# - Mount as supermount if available. This will improve behaviour for |
| 24 |
# synchronous writes and hanging mounts, although umount -lf already |
| 25 |
# does a good job. UPDATE: added, but untested. Let me know if it works. |
| 26 |
# - Detect filesystem on device, so filesystems that don't need a mounting |
| 27 |
# user can skip user detection. |
| 28 |
|
| 29 |
# Debugging |
| 30 |
# set -xv |
| 31 |
# exec > /tmp/hotplug.$$ 2>&1 |
| 32 |
|
| 33 |
PATH="/bin:/sbin:/usr/bin:/usr/sbin" |
| 34 |
export PATH |
| 35 |
|
| 36 |
# Proclaim stuff to the world |
| 37 |
mesg () { |
| 38 |
logger -t $0 "$*" |
| 39 |
} |
| 40 |
|
| 41 |
# Is path already mounted? |
| 42 |
is_path_mounted () { |
| 43 |
while read dev path rest |
| 44 |
do |
| 45 |
if [ "$path" = "$1" ] |
| 46 |
then |
| 47 |
return 0 |
| 48 |
fi |
| 49 |
done < /proc/self/mounts |
| 50 |
return 1 |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
# Remove strange characters from a filename |
| 55 |
clean_filename () { |
| 56 |
# Note the lack of quotes around $1, this strips off spaces. |
| 57 |
echo $1 | sed 's/[ /?*\"<>]/_/g' |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
SYSFS=/sys |
| 62 |
|
| 63 |
: DEVPATH=$DEVPATH |
| 64 |
|
| 65 |
# Wait for a file matching host* to appear in $SYSFS$DEVPATH |
| 66 |
|
| 67 |
local count |
| 68 |
for count in 1 2 3 4 5 6 7 8 9 10 |
| 69 |
do |
| 70 |
count is $count |
| 71 |
HOST=`ls -1 $SYSFS$DEVPATH | grep -m 1 '^host[1-9][0-9]*$'` |
| 72 |
[ -z "$HOST" ] || break |
| 73 |
sleep 1 |
| 74 |
done |
| 75 |
|
| 76 |
[ -z "$HOST" ] && mesg No host in $SYSFS$DEVPATH? |
| 77 |
|
| 78 |
|
| 79 |
BLKDEV=scsi/$HOST/bus0/target0/lun0 |
| 80 |
|
| 81 |
|
| 82 |
# Mount all partitions on this device |
| 83 |
: host devices are `ls -m /dev/$BLKDEV` |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
case "$ACTION" in |
| 88 |
add) |
| 89 |
# Figure out user to which to assign mounted partition devices |
| 90 |
CONSOLEUSER=`stat -c%U /dev/console 2>/dev/null` |
| 91 |
if [ -z "$CONSOLEUSER" ] |
| 92 |
then |
| 93 |
set `ls -l /dev/console` |
| 94 |
CONSOLEUSER=$3 |
| 95 |
fi |
| 96 |
|
| 97 |
[ -n "$CONSOLEUSER" ] || CONSOLEUSER=root |
| 98 |
PASSWD=`getent passwd $CONSOLEUSER 2>/dev/null` |
| 99 |
if [ -z "$PASSWD" ] |
| 100 |
then |
| 101 |
PASSWD=`grep "$CONSOLEUSER" /etc/passwd||grep root /etc/passwd` |
| 102 |
fi |
| 103 |
if [ -z "$PASSWD" ] |
| 104 |
then |
| 105 |
mesg Could not get password entry for $CONSOLEUSER |
| 106 |
exit 1 |
| 107 |
fi |
| 108 |
|
| 109 |
set `echo $PASSWD | sed 's/:/ /g'` |
| 110 |
if [ $# -lt 4 ] |
| 111 |
then |
| 112 |
mesg Bad password entry for $CONSOLEUSER |
| 113 |
exit 1 |
| 114 |
fi |
| 115 |
|
| 116 |
# These options should prevent abuse and make it writeable for the |
| 117 |
# console user. |
| 118 |
if grep -q supermount /proc/filesystems |
| 119 |
then |
| 120 |
MOUNTOPTS="-s -t supermount -onoatime,nosuid,umask=077,uid=$3,gid=$4" |
| 121 |
else |
| 122 |
MOUNTOPTS="-s -onoatime,sync,dirsync,nosuid,umask=077,uid=$3,gid=$4" |
| 123 |
fi |
| 124 |
|
| 125 |
# Write the remover as we go |
| 126 |
echo ". /etc/hotplug/usb/usb-storage.remove" > "$REMOVER" |
| 127 |
|
| 128 |
# Mount each partition device |
| 129 |
for p in `ls /dev/$BLKDEV` ; |
| 130 |
do |
| 131 |
if [ "$p" != "disc" ] |
| 132 |
then |
| 133 |
|
| 134 |
# If is there a symbolic link here, use it |
| 135 |
PARTDEV=`find /dev -lname $BLKDEV/$p` |
| 136 |
[ -z "$PARTDEV" ] && PARTDEV="/dev/$BLKDEV/$p" |
| 137 |
|
| 138 |
: Need to mount $PARTDEV |
| 139 |
|
| 140 |
# Mount it |
| 141 |
|
| 142 |
# Make sure we have support for vfat |
| 143 |
modprobe -q vfat |
| 144 |
|
| 145 |
|
| 146 |
# Find the name |
| 147 |
|
| 148 |
|
| 149 |
# Find out where we mount it |
| 150 |
MOUNTPATH=/mnt/usb/storage |
| 151 |
if is_path_mounted "$MOUNTPATH" |
| 152 |
then |
| 153 |
count=1 |
| 154 |
while is_path_mounted "${MOUNTPATH}_${count}" |
| 155 |
do |
| 156 |
count=$(( $count + 1 )) |
| 157 |
done |
| 158 |
MOUNTPATH="${MOUNTPATH}_${count}" |
| 159 |
fi |
| 160 |
|
| 161 |
# Find out device info to make a nice alias |
| 162 |
|
| 163 |
VendorID=`cat $SYSFS$DEVPATH/../idVendor` |
| 164 |
ProductID=`cat $SYSFS$DEVPATH/../idProduct` |
| 165 |
|
| 166 |
PRODUCT=$VendorID.$ProductID |
| 167 |
if [ -x /usr/sbin/lsusb ] |
| 168 |
then |
| 169 |
set `lsusb -d 0x$VendorID:0x$ProductID` |
| 170 |
shift 6 |
| 171 |
PRODUCT="$*" |
| 172 |
fi |
| 173 |
ALIAS=/mnt/usb/`clean_filename "$PRODUCT"` |
| 174 |
if [ -e "$ALIAS" ] |
| 175 |
then |
| 176 |
count=1 |
| 177 |
while [ -e "${ALIAS}_${count}" ] |
| 178 |
do |
| 179 |
count=$(( $count + 1 )) |
| 180 |
done |
| 181 |
ALIAS="${ALIAS}_${count}" |
| 182 |
fi |
| 183 |
|
| 184 |
# Make sure it's a directory |
| 185 |
if [ -e "$MOUNTPATH" ] |
| 186 |
then |
| 187 |
if [ ! -d "$MOUNTPATH" ] |
| 188 |
then |
| 189 |
mesg $MOUNTPATH exists but is not a directory |
| 190 |
continue |
| 191 |
fi |
| 192 |
else |
| 193 |
mkdir -p "$MOUNTPATH" |
| 194 |
if [ $? -ne 0 ] |
| 195 |
then |
| 196 |
mesg Could not create mountpoint $MOUNTPATH |
| 197 |
continue |
| 198 |
fi |
| 199 |
fi |
| 200 |
|
| 201 |
mesg Mounting $PARTDEV on $MOUNTPATH, options $MOUNTOPTS |
| 202 |
|
| 203 |
mount $MOUNTOPTS $PARTDEV $MOUNTPATH |
| 204 |
|
| 205 |
ln -s $MOUNTPATH $ALIAS |
| 206 |
|
| 207 |
echo "remove $PARTDEV" >> "$REMOVER" |
| 208 |
fi |
| 209 |
|
| 210 |
done |
| 211 |
|
| 212 |
chmod +x "$REMOVER" |
| 213 |
;; |
| 214 |
|
| 215 |
remove) |
| 216 |
# Umount each partition device |
| 217 |
mesg Shouldn\'t be here -- $REMOVER missing? |
| 218 |
;; |
| 219 |
|
| 220 |
*) |
| 221 |
exit 1;; |
| 222 |
esac |
| 223 |
|