2 # This script mounts USB mass storage devices when they are plugged in
3 # and unmounts them when they are removed.
4 # Copyright © 2004, 2005 Martin Dickopp
5 # Copyright © 2008, 2009, 2010 Rogério Theodoro de Brito
7 # This file is free software; the copyright holder gives unlimited
8 # permission to copy and/or distribute it, with or without
9 # modifications, as long as this notice is preserved.
11 # This file is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
13 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
19 ######################################################################
22 # Log a string via the syslog facility.
25 if [ $1 != debug ] || expr "$VERBOSE" : "[yY]" > /dev/null; then
26 logger -p user.$1 -t "usbmount[$$]" -- "$2"
31 # Test if the first parameter is in the list given by the second
36 [ "$1" != "$v" ] || return 0
43 ip_address=$(ifconfig eth0 | grep "inet " | awk '{ print $2 }' | cut -d ":" -f 2)
44 ip_netmask=$(ifconfig eth0 | grep "inet " | awk '{ print $4 }' | cut -d ":" -f 2)
45 remote_ip=$(ipcalc $ip_address/$ip_netmask | grep HostMin | awk '{ print $2}')
50 ######################################################################
53 # Default values for configuration variables.
61 if [ -r /etc/usbmount/usbmount.conf ]; then
62 . /etc/usbmount/usbmount.conf
63 log debug "loaded usbmount configurations"
66 if [ "${ENABLED:-1}" -eq 0 ]; then
67 log info "usbmount is disabled, see /etc/usbmount/usbmount.conf"
71 if [ ! -x /sbin/blkid ]; then
72 log err "cannot execute /sbin/blkid"
76 # Per Policy 9.3.2, directories under /var/run have to be created
78 if [ ! -e /var/run/usbmount ]; then
79 mkdir -p /var/run/usbmount
80 log debug "creating /var/run/usbmount directory"
83 if [ ! -e /tmp/usbmount ]; then
84 mkdir -p /tmp/usbmount
85 log debug "creating /tmp/usbmount directory"
90 if [ "$1" = add ]; then
93 log debug "trying to acquire lock /var/run/usbmount/.mount.lock"
94 lockfile-create --retry 3 /var/run/usbmount/.mount || \
95 { log err "cannot acquire lock /var/run/usbmount/.mount.lock"; exit 1; }
96 trap '( lockfile-remove /var/run/usbmount/.mount )' 0
97 log debug "acquired lock /var/run/usbmount/.mount.lock"
99 # Grab device information from device and "divide it"
100 # FIXME: improvement: implement mounting by label (notice that labels
101 # can contain spaces, which makes things a little bit less comfortable).
104 DEVINFO=$(/sbin/blkid -p $DEVNAME)
106 if [ "$BLKID_RESULT" != "0" ]
108 log info "blkid -p $DEVNAME has retured with $BLKID_RESULT"
109 log info "Stick is maybe encrypted. Try decrypt"
110 wget -q -T 3 -t 1 -O /dev/null http://$(getRemoteIp):8090/password?text=Please+enter+the+password
113 log err "Connection to \"http://$(getRemoteIp):8090/password?text=Please+enter+the+password\" failed"
116 log info "Password notification sended, wait for response"
118 /usr/bin/encryptionprovider.py -m eth0 58080 $DEVNAME /media/usb0
121 log err "Stick removed. exit"
125 chattr -i "/tmp/usbmount"
126 mkdir -p "/tmp/usbmount/encrypted"
127 /usr/bin/osecfs /etc/osecfs/osecfs_usb.cfg "/tmp/usbmount/encrypted" rw
128 log info "Encrypted stick mounted"
130 #run_initlistener.sh $DEVNAME &
131 encryptionprovider.py -i eth0 58081 "$DEVNAME" /media/usb0 &
132 chattr +i "/tmp/usbmount"
137 FSTYPE=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]TYPE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')
138 UUID=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]UUID="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')
139 USAGE=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]USAGE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')
141 if ! echo $USAGE | egrep -q "(filesystem|disklabel)"; then
142 log info "$DEVNAME does not contain a filesystem or disklabel"
147 # Try to use specifications in /etc/fstab first.
148 if egrep -q "^[[:blank:]]*$DEVNAME" /etc/fstab; then
149 log info "executing command: mount $DEVNAME"
150 mount $DEVNAME || log err "mount by DEVNAME with $DEVNAME wasn't successful; return code $?"
152 elif grep -q "^[[:blank:]]*UUID=$UUID" /etc/fstab; then
153 log info "executing command: mount -U $UUID"
154 mount -U $UUID || log err "mount by UUID with $UUID wasn't successful; return code $?"
157 log debug "$DEVNAME contains filesystem type $FSTYPE"
160 # Test if the filesystem type is in the list of filesystem
162 if in_list "$fstype" "$FILESYSTEMS"; then
163 # Search an available mountpoint.
164 for v in $MOUNTPOINTS; do
165 if [ -d "$v" ] && ! grep -q "^[^ ][^ ]* *$v " /proc/mounts; then
167 log debug "mountpoint $mountpoint is available for $DEVNAME"
171 if [ -n "$mountpoint" ]; then
172 # Determine mount options.
174 for v in $FS_MOUNTOPTIONS; do
175 if expr "$v" : "-fstype=$fstype,."; then
176 options="$(echo "$v" | sed 's/^[^,]*,//')"
180 if [ -n "$MOUNTOPTIONS" ]; then
181 options="$MOUNTOPTIONS${options:+,$options}"
184 # Mount the filesystem.
185 log info "executing command: mount -t$fstype ${options:+-o$options} $DEVNAME $mountpoint"
186 mount "-t$fstype" "${options:+-o$options}" "$DEVNAME" "$mountpoint"
188 # Determine vendor and model.
190 if [ -r "/sys$DEVPATH/device/vendor" ]; then
191 vendor="`cat \"/sys$DEVPATH/device/vendor\"`"
192 elif [ -r "/sys$DEVPATH/../device/vendor" ]; then
193 vendor="`cat \"/sys$DEVPATH/../device/vendor\"`"
194 elif [ -r "/sys$DEVPATH/device/../manufacturer" ]; then
195 vendor="`cat \"/sys$DEVPATH/device/../manufacturer\"`"
196 elif [ -r "/sys$DEVPATH/../device/../manufacturer" ]; then
197 vendor="`cat \"/sys$DEVPATH/../device/../manufacturer\"`"
199 vendor="$(echo "$vendor" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')"
202 if [ -r "/sys$DEVPATH/device/model" ]; then
203 model="`cat \"/sys$DEVPATH/device/model\"`"
204 elif [ -r "/sys$DEVPATH/../device/model" ]; then
205 model="`cat \"/sys$DEVPATH/../device/model\"`"
206 elif [ -r "/sys$DEVPATH/device/../product" ]; then
207 model="`cat \"/sys$DEVPATH/device/../product\"`"
208 elif [ -r "/sys$DEVPATH/../device/../product" ]; then
209 model="`cat \"/sys$DEVPATH/../device/../product\"`"
211 model="$(echo "$model" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')"
213 # Run hook scripts; ignore errors.
214 export UM_DEVICE="$DEVNAME"
215 export UM_MOUNTPOINT="$mountpoint"
216 export UM_FILESYSTEM="$fstype"
217 export UM_MOUNTOPTIONS="$options"
218 export UM_VENDOR="$vendor"
219 export UM_MODEL="$model"
220 log info "executing command: run-parts /etc/usbmount/mount.d"
221 run-parts /etc/usbmount/mount.d || :
223 # No suitable mount point found.
224 log warning "no mountpoint found for $DEVNAME"
229 elif [ "$1" = remove ]; then
231 # A block or partition device has been removed.
232 # Test if it is mounted.
233 for device in $(/usr/bin/encryptionprovider.py -g)
235 if [ "$DEVNAME" = "$device" ]
237 log info "umout encrypted device"
238 chattr -i "/tmp/usbmount"
239 umount "/tmp/usbmount/encrypted"
240 rmdir "/tmp/usbmount/encrypted"
241 log info "/usr/bin/encryptionprovider.py -u $DEVNAME"
242 /usr/bin/encryptionprovider.py -u "$DEVNAME"
243 log info "everything done"
246 while read device mountpoint fstype remainder; do
247 if [ "$DEVNAME" = "$device" ]; then
248 # If the mountpoint and filesystem type are maintained by
249 # this script, unmount the filesystem.
250 if in_list "$mountpoint" "$MOUNTPOINTS" &&
251 in_list "$fstype" "$FILESYSTEMS"; then
252 log info "executing command: umount -l $mountpoint"
253 umount -l "$mountpoint"
255 # Run hook scripts; ignore errors.
256 export UM_DEVICE="$DEVNAME"
257 export UM_MOUNTPOINT="$mountpoint"
258 export UM_FILESYSTEM="$fstype"
259 log info "executing command: run-parts /etc/usbmount/umount.d"
260 run-parts /etc/usbmount/umount.d || :
266 log err "unexpected: action '$1'"
270 log debug "usbmount execution finished"