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
42 ######################################################################
45 # Default values for configuration variables.
53 if [ -r /etc/usbmount/usbmount.conf ]; then
54 . /etc/usbmount/usbmount.conf
55 log debug "loaded usbmount configurations"
58 if [ "${ENABLED:-1}" -eq 0 ]; then
59 log info "usbmount is disabled, see /etc/usbmount/usbmount.conf"
63 if [ ! -x /sbin/blkid ]; then
64 log err "cannot execute /sbin/blkid"
68 # Per Policy 9.3.2, directories under /var/run have to be created
70 if [ ! -e /var/run/usbmount ]; then
71 mkdir -p /var/run/usbmount
72 log debug "creating /var/run/usbmount directory"
77 if [ "$1" = add ]; then
80 log debug "trying to acquire lock /var/run/usbmount/.mount.lock"
81 lockfile-create --retry 3 /var/run/usbmount/.mount || \
82 { log err "cannot acquire lock /var/run/usbmount/.mount.lock"; exit 1; }
83 trap '( lockfile-remove /var/run/usbmount/.mount )' 0
84 log debug "acquired lock /var/run/usbmount/.mount.lock"
86 # Grab device information from device and "divide it"
87 # FIXME: improvement: implement mounting by label (notice that labels
88 # can contain spaces, which makes things a little bit less comfortable).
91 DEVINFO=$(/sbin/blkid -p $DEVNAME)
93 if [ "$BLKID_RESULT" != "0" ]
95 log info "blkid -p $DEVNAME has retured with $BLKID_RESULT"
96 log info "Stick is maybe encrypted. Try decrypt"
97 wget -q -T 3 -t 1 -O /dev/null http://192.168.56.1:8090/password?text=Please+send+me+the+password
100 log err "Connection to http://192.168.56.1:8090/password?text=Please+send+me+the+password failed"
103 log info "Password notification sended, wait for response"
105 /usr/bin/osecvm-password-receiver.py eth0 58080 $DEVNAME /media/usb0
108 log err "Stick removed. exit"
112 mkdir -p "/var/run/usbmount/encrypted"
113 /usr/bin/osecfs /etc/osecfs/osecfs_usb.cfg "/var/run/usbmount/encrypted" rw
114 log info "Encrypted stick mounted"
119 FSTYPE=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]TYPE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')
120 UUID=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]UUID="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')
121 USAGE=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]USAGE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')
123 if ! echo $USAGE | egrep -q "(filesystem|disklabel)"; then
124 log info "$DEVNAME does not contain a filesystem or disklabel"
129 # Try to use specifications in /etc/fstab first.
130 if egrep -q "^[[:blank:]]*$DEVNAME" /etc/fstab; then
131 log info "executing command: mount $DEVNAME"
132 mount $DEVNAME || log err "mount by DEVNAME with $DEVNAME wasn't successful; return code $?"
134 elif grep -q "^[[:blank:]]*UUID=$UUID" /etc/fstab; then
135 log info "executing command: mount -U $UUID"
136 mount -U $UUID || log err "mount by UUID with $UUID wasn't successful; return code $?"
139 log debug "$DEVNAME contains filesystem type $FSTYPE"
142 # Test if the filesystem type is in the list of filesystem
144 if in_list "$fstype" "$FILESYSTEMS"; then
145 # Search an available mountpoint.
146 for v in $MOUNTPOINTS; do
147 if [ -d "$v" ] && ! grep -q "^[^ ][^ ]* *$v " /proc/mounts; then
149 log debug "mountpoint $mountpoint is available for $DEVNAME"
153 if [ -n "$mountpoint" ]; then
154 # Determine mount options.
156 for v in $FS_MOUNTOPTIONS; do
157 if expr "$v" : "-fstype=$fstype,."; then
158 options="$(echo "$v" | sed 's/^[^,]*,//')"
162 if [ -n "$MOUNTOPTIONS" ]; then
163 options="$MOUNTOPTIONS${options:+,$options}"
166 # Mount the filesystem.
167 log info "executing command: mount -t$fstype ${options:+-o$options} $DEVNAME $mountpoint"
168 mount "-t$fstype" "${options:+-o$options}" "$DEVNAME" "$mountpoint"
170 # Determine vendor and model.
172 if [ -r "/sys$DEVPATH/device/vendor" ]; then
173 vendor="`cat \"/sys$DEVPATH/device/vendor\"`"
174 elif [ -r "/sys$DEVPATH/../device/vendor" ]; then
175 vendor="`cat \"/sys$DEVPATH/../device/vendor\"`"
176 elif [ -r "/sys$DEVPATH/device/../manufacturer" ]; then
177 vendor="`cat \"/sys$DEVPATH/device/../manufacturer\"`"
178 elif [ -r "/sys$DEVPATH/../device/../manufacturer" ]; then
179 vendor="`cat \"/sys$DEVPATH/../device/../manufacturer\"`"
181 vendor="$(echo "$vendor" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')"
184 if [ -r "/sys$DEVPATH/device/model" ]; then
185 model="`cat \"/sys$DEVPATH/device/model\"`"
186 elif [ -r "/sys$DEVPATH/../device/model" ]; then
187 model="`cat \"/sys$DEVPATH/../device/model\"`"
188 elif [ -r "/sys$DEVPATH/device/../product" ]; then
189 model="`cat \"/sys$DEVPATH/device/../product\"`"
190 elif [ -r "/sys$DEVPATH/../device/../product" ]; then
191 model="`cat \"/sys$DEVPATH/../device/../product\"`"
193 model="$(echo "$model" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')"
195 # Run hook scripts; ignore errors.
196 export UM_DEVICE="$DEVNAME"
197 export UM_MOUNTPOINT="$mountpoint"
198 export UM_FILESYSTEM="$fstype"
199 export UM_MOUNTOPTIONS="$options"
200 export UM_VENDOR="$vendor"
201 export UM_MODEL="$model"
202 log info "executing command: run-parts /etc/usbmount/mount.d"
203 run-parts /etc/usbmount/mount.d || :
205 # No suitable mount point found.
206 log warning "no mountpoint found for $DEVNAME"
211 elif [ "$1" = remove ]; then
213 # A block or partition device has been removed.
214 # Test if it is mounted.
215 for device in $(/usr/bin/truecrypt -l | awk '{ print $2}')
217 if [ "$DEVNAME" = "$device" ]
219 log info "encrypted device was removed"
220 umount "/var/run/usbmount/encrypted"
221 rmdir "/var/run/usbmount/encrypted"
222 log info "/usr/bin/truecrypt -d $DEVNAME"
223 /usr/bin/truecrypt -d "$DEVNAME"
224 log info "everything done"
227 while read device mountpoint fstype remainder; do
228 if [ "$DEVNAME" = "$device" ]; then
229 # If the mountpoint and filesystem type are maintained by
230 # this script, unmount the filesystem.
231 if in_list "$mountpoint" "$MOUNTPOINTS" &&
232 in_list "$fstype" "$FILESYSTEMS"; then
233 log info "executing command: umount -l $mountpoint"
234 umount -l "$mountpoint"
236 # Run hook scripts; ignore errors.
237 export UM_DEVICE="$DEVNAME"
238 export UM_MOUNTPOINT="$mountpoint"
239 export UM_FILESYSTEM="$fstype"
240 log info "executing command: run-parts /etc/usbmount/umount.d"
241 run-parts /etc/usbmount/umount.d || :
247 log err "unexpected: action '$1'"
251 log debug "usbmount execution finished"