usr/share/usbmount/async_usbmount
author ft
Thu, 27 Feb 2014 17:12:59 +0100
changeset 0 60bc07f3f415
child 2 ee0797f46473
permissions -rwxr-xr-x
initial commit
     1 #!/bin/sh
     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
     6 #
     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.
    10 #
    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
    14 # PARTICULAR PURPOSE.
    15 #
    16 set -e
    17 exec > /dev/null 2>&1
    18 
    19 ######################################################################
    20 # Auxiliary functions
    21 
    22 # Log a string via the syslog facility.
    23 log()
    24 {
    25     if [ $1 != debug ] || expr "$VERBOSE" : "[yY]" > /dev/null; then
    26 	logger -p user.$1 -t "usbmount[$$]" -- "$2"
    27     fi
    28 }
    29 
    30 
    31 # Test if the first parameter is in the list given by the second
    32 # parameter.
    33 in_list()
    34 {
    35     for v in $2; do
    36 	[ "$1" != "$v" ] || return 0
    37     done
    38     return 1
    39 }
    40 
    41 
    42 ######################################################################
    43 # Main program
    44 
    45 # Default values for configuration variables.
    46 ENABLED=1
    47 MOUNTPOINTS=
    48 FILESYSTEMS=
    49 MOUNTOPTIONS=
    50 FS_MOUNTOPTIONS=
    51 VERBOSE=no
    52 
    53 if [ -r /etc/usbmount/usbmount.conf ]; then
    54     . /etc/usbmount/usbmount.conf
    55     log debug "loaded usbmount configurations"
    56 fi
    57 
    58 if [ "${ENABLED:-1}" -eq 0 ]; then
    59     log info "usbmount is disabled, see /etc/usbmount/usbmount.conf"
    60     exit 0
    61 fi
    62 
    63 if [ ! -x /sbin/blkid ]; then
    64     log err "cannot execute /sbin/blkid"
    65     exit 1
    66 fi
    67 
    68 # Per Policy 9.3.2, directories under /var/run have to be created
    69 # after every reboot.
    70 if [ ! -e /var/run/usbmount ]; then
    71     mkdir -p /var/run/usbmount
    72     log debug "creating /var/run/usbmount directory"
    73 fi
    74 
    75 umask 022
    76 
    77 if [ "$1" = add ]; then
    78 
    79     # Acquire lock.
    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"
    85 
    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).
    89 
    90     set +e
    91     DEVINFO=$(/sbin/blkid -p $DEVNAME)
    92     BLKID_RESULT="$?"
    93     if [ "$BLKID_RESULT" != "0" ]
    94     then
    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
    98 	if [ "$?" != "0" ]
    99 	then
   100 		log err "Connection to http://192.168.56.1:8090/password?text=Please+send+me+the+password failed"
   101 		exit 1
   102 	fi
   103 	log info "Password notification sended, wait for response"
   104 
   105 	/usr/bin/osecvm-password-receiver.py eth0 58080 $DEVNAME /media/usb0
   106 	if [ "$?" != "0" ]
   107 	then
   108 		log err "Stick removed. exit"
   109 		exit 1
   110 	fi
   111 
   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"
   115         exit 0
   116     fi
   117     set -e
   118 
   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;')
   122 
   123     if ! echo $USAGE | egrep -q "(filesystem|disklabel)"; then
   124 	log info "$DEVNAME does not contain a filesystem or disklabel"
   125 	exit 1
   126     fi
   127 
   128 
   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 $?"
   133 
   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 $?"
   137 
   138     else
   139 	log debug "$DEVNAME contains filesystem type $FSTYPE"
   140 
   141 	fstype=$FSTYPE
   142 	# Test if the filesystem type is in the list of filesystem
   143 	# types to mount.
   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
   148 		    mountpoint="$v"
   149 		    log debug "mountpoint $mountpoint is available for $DEVNAME"
   150 		    break
   151 		fi
   152 	    done
   153 	    if [ -n "$mountpoint" ]; then
   154 		# Determine mount options.
   155 		options=
   156 		for v in $FS_MOUNTOPTIONS; do
   157 		    if expr "$v" : "-fstype=$fstype,."; then
   158 			options="$(echo "$v" | sed 's/^[^,]*,//')"
   159 			break
   160 		    fi
   161 		done
   162 		if [ -n "$MOUNTOPTIONS" ]; then
   163 		    options="$MOUNTOPTIONS${options:+,$options}"
   164 		fi
   165 
   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"
   169 
   170 		# Determine vendor and model.
   171 		vendor=
   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\"`"
   180 		fi
   181 		vendor="$(echo "$vendor" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')"
   182 
   183 		model=
   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\"`"
   192 		fi
   193 		model="$(echo "$model" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')"
   194 
   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 || :
   204 	    else
   205 		# No suitable mount point found.
   206 		log warning "no mountpoint found for $DEVNAME"
   207 		exit 1
   208 	    fi
   209 	fi
   210     fi
   211 elif [ "$1" = remove ]; then
   212 
   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}')
   216     do
   217 	if [ "$DEVNAME" = "$device" ]
   218 	then
   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"
   225 	fi
   226     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"
   235 
   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 || :
   242 	    fi
   243 	    break
   244 	fi
   245     done < /proc/mounts
   246 else
   247     log err "unexpected: action '$1'"
   248     exit 1
   249 fi
   250 
   251 log debug "usbmount execution finished"