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
ft@0
     1
#!/bin/sh
ft@0
     2
# This script mounts USB mass storage devices when they are plugged in
ft@0
     3
# and unmounts them when they are removed.
ft@0
     4
# Copyright © 2004, 2005 Martin Dickopp
ft@0
     5
# Copyright © 2008, 2009, 2010 Rogério Theodoro de Brito
ft@0
     6
#
ft@0
     7
# This file is free software; the copyright holder gives unlimited
ft@0
     8
# permission to copy and/or distribute it, with or without
ft@0
     9
# modifications, as long as this notice is preserved.
ft@0
    10
#
ft@0
    11
# This file is distributed in the hope that it will be useful,
ft@0
    12
# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
ft@0
    13
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
ft@0
    14
# PARTICULAR PURPOSE.
ft@0
    15
#
ft@0
    16
set -e
ft@0
    17
exec > /dev/null 2>&1
ft@0
    18
ft@0
    19
######################################################################
ft@0
    20
# Auxiliary functions
ft@0
    21
ft@0
    22
# Log a string via the syslog facility.
ft@0
    23
log()
ft@0
    24
{
ft@0
    25
    if [ $1 != debug ] || expr "$VERBOSE" : "[yY]" > /dev/null; then
ft@0
    26
	logger -p user.$1 -t "usbmount[$$]" -- "$2"
ft@0
    27
    fi
ft@0
    28
}
ft@0
    29
ft@0
    30
ft@0
    31
# Test if the first parameter is in the list given by the second
ft@0
    32
# parameter.
ft@0
    33
in_list()
ft@0
    34
{
ft@0
    35
    for v in $2; do
ft@0
    36
	[ "$1" != "$v" ] || return 0
ft@0
    37
    done
ft@0
    38
    return 1
ft@0
    39
}
ft@0
    40
ft@0
    41
ft@0
    42
######################################################################
ft@0
    43
# Main program
ft@0
    44
ft@0
    45
# Default values for configuration variables.
ft@0
    46
ENABLED=1
ft@0
    47
MOUNTPOINTS=
ft@0
    48
FILESYSTEMS=
ft@0
    49
MOUNTOPTIONS=
ft@0
    50
FS_MOUNTOPTIONS=
ft@0
    51
VERBOSE=no
ft@0
    52
ft@0
    53
if [ -r /etc/usbmount/usbmount.conf ]; then
ft@0
    54
    . /etc/usbmount/usbmount.conf
ft@0
    55
    log debug "loaded usbmount configurations"
ft@0
    56
fi
ft@0
    57
ft@0
    58
if [ "${ENABLED:-1}" -eq 0 ]; then
ft@0
    59
    log info "usbmount is disabled, see /etc/usbmount/usbmount.conf"
ft@0
    60
    exit 0
ft@0
    61
fi
ft@0
    62
ft@0
    63
if [ ! -x /sbin/blkid ]; then
ft@0
    64
    log err "cannot execute /sbin/blkid"
ft@0
    65
    exit 1
ft@0
    66
fi
ft@0
    67
ft@0
    68
# Per Policy 9.3.2, directories under /var/run have to be created
ft@0
    69
# after every reboot.
ft@0
    70
if [ ! -e /var/run/usbmount ]; then
ft@0
    71
    mkdir -p /var/run/usbmount
ft@0
    72
    log debug "creating /var/run/usbmount directory"
ft@0
    73
fi
ft@0
    74
ft@0
    75
umask 022
ft@0
    76
ft@0
    77
if [ "$1" = add ]; then
ft@0
    78
ft@0
    79
    # Acquire lock.
ft@0
    80
    log debug "trying to acquire lock /var/run/usbmount/.mount.lock"
ft@0
    81
    lockfile-create --retry 3 /var/run/usbmount/.mount || \
ft@0
    82
	{ log err "cannot acquire lock /var/run/usbmount/.mount.lock"; exit 1; }
ft@0
    83
    trap '( lockfile-remove /var/run/usbmount/.mount )' 0
ft@0
    84
    log debug "acquired lock /var/run/usbmount/.mount.lock"
ft@0
    85
ft@0
    86
    # Grab device information from device and "divide it"
ft@0
    87
    #   FIXME: improvement: implement mounting by label (notice that labels
ft@0
    88
    #   can contain spaces, which makes things a little bit less comfortable).
ft@0
    89
ft@0
    90
    set +e
ft@0
    91
    DEVINFO=$(/sbin/blkid -p $DEVNAME)
ft@0
    92
    BLKID_RESULT="$?"
ft@0
    93
    if [ "$BLKID_RESULT" != "0" ]
ft@0
    94
    then
ft@0
    95
	log info "blkid -p $DEVNAME has retured with $BLKID_RESULT"
ft@0
    96
	log info "Stick is maybe encrypted. Try decrypt"
ft@0
    97
	wget -q -T 3 -t 1 -O /dev/null http://192.168.56.1:8090/password?text=Please+send+me+the+password
ft@0
    98
	if [ "$?" != "0" ]
ft@0
    99
	then
ft@0
   100
		log err "Connection to http://192.168.56.1:8090/password?text=Please+send+me+the+password failed"
ft@0
   101
		exit 1
ft@0
   102
	fi
ft@0
   103
	log info "Password notification sended, wait for response"
ft@0
   104
ft@0
   105
	/usr/bin/osecvm-password-receiver.py eth0 58080 $DEVNAME /media/usb0
ft@0
   106
	if [ "$?" != "0" ]
ft@0
   107
	then
ft@0
   108
		log err "Stick removed. exit"
ft@0
   109
		exit 1
ft@0
   110
	fi
ft@0
   111
ft@0
   112
	mkdir -p "/var/run/usbmount/encrypted"
ft@0
   113
	/usr/bin/osecfs /etc/osecfs/osecfs_usb.cfg "/var/run/usbmount/encrypted" rw
ft@0
   114
	log info "Encrypted stick mounted"
ft@0
   115
        exit 0
ft@0
   116
    fi
ft@0
   117
    set -e
ft@0
   118
ft@0
   119
    FSTYPE=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]TYPE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')
ft@0
   120
    UUID=$(echo "$DEVINFO"   | sed 's/.*[[:blank:]]UUID="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')
ft@0
   121
    USAGE=$(echo "$DEVINFO"  | sed 's/.*[[:blank:]]USAGE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')
ft@0
   122
ft@0
   123
    if ! echo $USAGE | egrep -q "(filesystem|disklabel)"; then
ft@0
   124
	log info "$DEVNAME does not contain a filesystem or disklabel"
ft@0
   125
	exit 1
ft@0
   126
    fi
ft@0
   127
ft@0
   128
ft@0
   129
    # Try to use specifications in /etc/fstab first.
ft@0
   130
    if egrep -q "^[[:blank:]]*$DEVNAME" /etc/fstab; then
ft@0
   131
	log info "executing command: mount $DEVNAME"
ft@0
   132
	mount $DEVNAME || log err "mount by DEVNAME with $DEVNAME wasn't successful; return code $?"
ft@0
   133
ft@0
   134
    elif grep -q "^[[:blank:]]*UUID=$UUID" /etc/fstab; then
ft@0
   135
        log info "executing command: mount -U $UUID"
ft@0
   136
	mount -U $UUID || log err "mount by UUID with $UUID wasn't successful; return code $?"
ft@0
   137
ft@0
   138
    else
ft@0
   139
	log debug "$DEVNAME contains filesystem type $FSTYPE"
ft@0
   140
ft@0
   141
	fstype=$FSTYPE
ft@0
   142
	# Test if the filesystem type is in the list of filesystem
ft@0
   143
	# types to mount.
ft@0
   144
	if in_list "$fstype" "$FILESYSTEMS"; then
ft@0
   145
	    # Search an available mountpoint.
ft@0
   146
	    for v in $MOUNTPOINTS; do
ft@0
   147
		if [ -d "$v" ] && ! grep -q "^[^ ][^ ]*  *$v " /proc/mounts; then
ft@0
   148
		    mountpoint="$v"
ft@0
   149
		    log debug "mountpoint $mountpoint is available for $DEVNAME"
ft@0
   150
		    break
ft@0
   151
		fi
ft@0
   152
	    done
ft@0
   153
	    if [ -n "$mountpoint" ]; then
ft@0
   154
		# Determine mount options.
ft@0
   155
		options=
ft@0
   156
		for v in $FS_MOUNTOPTIONS; do
ft@0
   157
		    if expr "$v" : "-fstype=$fstype,."; then
ft@0
   158
			options="$(echo "$v" | sed 's/^[^,]*,//')"
ft@0
   159
			break
ft@0
   160
		    fi
ft@0
   161
		done
ft@0
   162
		if [ -n "$MOUNTOPTIONS" ]; then
ft@0
   163
		    options="$MOUNTOPTIONS${options:+,$options}"
ft@0
   164
		fi
ft@0
   165
ft@0
   166
		# Mount the filesystem.
ft@0
   167
		log info "executing command: mount -t$fstype ${options:+-o$options} $DEVNAME $mountpoint"
ft@0
   168
		mount "-t$fstype" "${options:+-o$options}" "$DEVNAME" "$mountpoint"
ft@0
   169
ft@0
   170
		# Determine vendor and model.
ft@0
   171
		vendor=
ft@0
   172
		if [ -r "/sys$DEVPATH/device/vendor" ]; then
ft@0
   173
		    vendor="`cat \"/sys$DEVPATH/device/vendor\"`"
ft@0
   174
		elif [ -r "/sys$DEVPATH/../device/vendor" ]; then
ft@0
   175
		    vendor="`cat \"/sys$DEVPATH/../device/vendor\"`"
ft@0
   176
		elif [ -r "/sys$DEVPATH/device/../manufacturer" ]; then
ft@0
   177
		    vendor="`cat \"/sys$DEVPATH/device/../manufacturer\"`"
ft@0
   178
		elif [ -r "/sys$DEVPATH/../device/../manufacturer" ]; then
ft@0
   179
		    vendor="`cat \"/sys$DEVPATH/../device/../manufacturer\"`"
ft@0
   180
		fi
ft@0
   181
		vendor="$(echo "$vendor" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')"
ft@0
   182
ft@0
   183
		model=
ft@0
   184
		if [ -r "/sys$DEVPATH/device/model" ]; then
ft@0
   185
		    model="`cat \"/sys$DEVPATH/device/model\"`"
ft@0
   186
		elif [ -r "/sys$DEVPATH/../device/model" ]; then
ft@0
   187
		    model="`cat \"/sys$DEVPATH/../device/model\"`"
ft@0
   188
		elif [ -r "/sys$DEVPATH/device/../product" ]; then
ft@0
   189
		    model="`cat \"/sys$DEVPATH/device/../product\"`"
ft@0
   190
		elif [ -r "/sys$DEVPATH/../device/../product" ]; then
ft@0
   191
		    model="`cat \"/sys$DEVPATH/../device/../product\"`"
ft@0
   192
		fi
ft@0
   193
		model="$(echo "$model" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')"
ft@0
   194
ft@0
   195
		# Run hook scripts; ignore errors.
ft@0
   196
		export UM_DEVICE="$DEVNAME"
ft@0
   197
		export UM_MOUNTPOINT="$mountpoint"
ft@0
   198
		export UM_FILESYSTEM="$fstype"
ft@0
   199
		export UM_MOUNTOPTIONS="$options"
ft@0
   200
		export UM_VENDOR="$vendor"
ft@0
   201
		export UM_MODEL="$model"
ft@0
   202
		log info "executing command: run-parts /etc/usbmount/mount.d"
ft@0
   203
		run-parts /etc/usbmount/mount.d || :
ft@0
   204
	    else
ft@0
   205
		# No suitable mount point found.
ft@0
   206
		log warning "no mountpoint found for $DEVNAME"
ft@0
   207
		exit 1
ft@0
   208
	    fi
ft@0
   209
	fi
ft@0
   210
    fi
ft@0
   211
elif [ "$1" = remove ]; then
ft@0
   212
ft@0
   213
    # A block or partition device has been removed.
ft@0
   214
    # Test if it is mounted.
ft@0
   215
    for device in $(/usr/bin/truecrypt -l | awk '{ print $2}')
ft@0
   216
    do
ft@0
   217
	if [ "$DEVNAME" = "$device" ]
ft@0
   218
	then
ft@0
   219
	    log info "encrypted device was removed"
ft@0
   220
	    umount "/var/run/usbmount/encrypted"
ft@0
   221
	    rmdir "/var/run/usbmount/encrypted"
ft@0
   222
	    log info "/usr/bin/truecrypt -d $DEVNAME"
ft@0
   223
	    /usr/bin/truecrypt -d "$DEVNAME"
ft@0
   224
	    log info "everything done"
ft@0
   225
	fi
ft@0
   226
    done
ft@0
   227
    while read device mountpoint fstype remainder; do
ft@0
   228
	if [ "$DEVNAME" = "$device" ]; then
ft@0
   229
	    # If the mountpoint and filesystem type are maintained by
ft@0
   230
	    # this script, unmount the filesystem.
ft@0
   231
	    if in_list "$mountpoint" "$MOUNTPOINTS" &&
ft@0
   232
		in_list "$fstype" "$FILESYSTEMS"; then
ft@0
   233
		log info "executing command: umount -l $mountpoint"
ft@0
   234
		umount -l "$mountpoint"
ft@0
   235
ft@0
   236
		# Run hook scripts; ignore errors.
ft@0
   237
		export UM_DEVICE="$DEVNAME"
ft@0
   238
		export UM_MOUNTPOINT="$mountpoint"
ft@0
   239
		export UM_FILESYSTEM="$fstype"
ft@0
   240
		log info "executing command: run-parts /etc/usbmount/umount.d"
ft@0
   241
		run-parts /etc/usbmount/umount.d || :
ft@0
   242
	    fi
ft@0
   243
	    break
ft@0
   244
	fi
ft@0
   245
    done < /proc/mounts
ft@0
   246
else
ft@0
   247
    log err "unexpected: action '$1'"
ft@0
   248
    exit 1
ft@0
   249
fi
ft@0
   250
ft@0
   251
log debug "usbmount execution finished"