From e1aa312145461be75fd965bccd4ebf768662a7de Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Wed, 24 Oct 2018 21:36:23 +0200 Subject: [PATCH 01/20] gitignore: Ignore common editor backups Signed-off-by: Olliver Schinagl --- .gitignore | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..741518f --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +# Swap +[._]*.s[a-v][a-z] +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Session +Session.vim + +# Temporary +.netrwhist +*~ +# Auto-generated tag files +tags +# Persistent undo +[._]*.un~ From 5673854b35b5f621b0eaaf9f7895f924643f2256 Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Tue, 23 Oct 2018 21:31:36 +0200 Subject: [PATCH 02/20] Use consistent file layout The current file is mixing tabs and spaces on various indents. Since tabs are use the most, I suppose that's what was intended to be used. Signed-off-by: Olliver Schinagl --- usbmount | 280 +++++++++++++++++++++++++++---------------------------- 1 file changed, 139 insertions(+), 141 deletions(-) diff --git a/usbmount b/usbmount index f3ba6b7..a2d2337 100755 --- a/usbmount +++ b/usbmount @@ -22,9 +22,9 @@ exec > /dev/null 2>&1 # Log a string via the syslog facility. log() { - if [ $1 != debug ] || expr "$VERBOSE" : "[yY]" > /dev/null; then - logger -p user.$1 -t "usbmount[$$]" -- "$2" - fi + if [ $1 != debug ] || expr "$VERBOSE" : "[yY]" > /dev/null; then + logger -p user.$1 -t "usbmount[$$]" -- "$2" + fi } @@ -32,10 +32,10 @@ log() # parameter. in_list() { - for v in $2; do - [ "$1" != "$v" ] || return 0 - done - return 1 + for v in $2; do + [ "$1" != "$v" ] || return 0 + done + return 1 } @@ -51,25 +51,25 @@ FS_MOUNTOPTIONS= VERBOSE=no if [ -r /etc/usbmount/usbmount.conf ]; then - . /etc/usbmount/usbmount.conf - log debug "loaded usbmount configurations" + . /etc/usbmount/usbmount.conf + log debug "loaded usbmount configurations" fi if [ "${ENABLED:-1}" -eq 0 ]; then - log info "usbmount is disabled, see /etc/usbmount/usbmount.conf" - exit 0 + log info "usbmount is disabled, see /etc/usbmount/usbmount.conf" + exit 0 fi if [ ! -x /sbin/blkid ]; then - log err "cannot execute /sbin/blkid" - exit 1 + log err "cannot execute /sbin/blkid" + exit 1 fi # Per Policy 9.3.2, directories under /var/run have to be created # after every reboot. if [ ! -e /var/run/usbmount ]; then - mkdir -p /var/run/usbmount - log debug "creating /var/run/usbmount directory" + mkdir -p /var/run/usbmount + log debug "creating /var/run/usbmount directory" fi umask 022 @@ -77,138 +77,136 @@ umask 022 if [ "$1" = add ]; then - # Acquire lock. - log debug "trying to acquire lock /var/run/usbmount/.mount.lock" - lockfile-create --retry 3 /var/run/usbmount/.mount || \ - { log err "cannot acquire lock /var/run/usbmount/.mount.lock"; exit 1; } - trap '( lockfile-remove /var/run/usbmount/.mount )' 0 - log debug "acquired lock /var/run/usbmount/.mount.lock" - - # Query udev for the expected device information (as we are now running in - # a service's context and the env variables have been lost) - eval $(udevadm info --query=env --export "$DEVNAME" | grep -v '^[^=]*\..*=') - - # Grab device information from device and "divide it" - # FIXME: improvement: implement mounting by label (notice that labels - # can contain spaces, which makes things a little bit less comfortable). - DEVINFO=$(/sbin/blkid -p $DEVNAME) - FSTYPE=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]TYPE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;') - UUID=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]UUID="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;') - USAGE=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]USAGE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;') - - if ! echo $USAGE | egrep -q "(filesystem|disklabel)"; then - log info "$DEVNAME does not contain a filesystem or disklabel" - exit 0 - fi - - # Try to use specifications in /etc/fstab first. - if egrep -q "^[[:blank:]]*$DEVNAME" /etc/fstab; then - log info "executing command: mount $DEVNAME" - mount $DEVNAME || log err "mount by DEVNAME with $DEVNAME wasn't successful; return code $?" - - elif grep -q "^[[:blank:]]*UUID=\"?$UUID\"?" /etc/fstab; then - log info "executing command: mount -U $UUID" - mount -U $UUID || log err "mount by UUID with $UUID wasn't successful; return code $?" - - else - log debug "$DEVNAME contains filesystem type $FSTYPE" - - fstype=$FSTYPE - # Test if the filesystem type is in the list of filesystem - # types to mount. - if in_list "$fstype" "$FILESYSTEMS"; then - # Search an available mountpoint. - for v in $MOUNTPOINTS; do - if [ -d "$v" ] && ! grep -q "^[^ ][^ ]* *$v " /proc/mounts; then - mountpoint="$v" - log debug "mountpoint $mountpoint is available for $DEVNAME" - break - fi - done - if [ -n "$mountpoint" ]; then - # Determine mount options. - options= - for v in $FS_MOUNTOPTIONS; do - if expr "$v" : "-fstype=$fstype,."; then - options="$(echo "$v" | sed 's/^[^,]*,//')" - break - fi - done - if [ -n "$MOUNTOPTIONS" ]; then - options="$MOUNTOPTIONS${options:+,$options}" - fi + # Acquire lock. + log debug "trying to acquire lock /var/run/usbmount/.mount.lock" + lockfile-create --retry 3 /var/run/usbmount/.mount || \ + { log err "cannot acquire lock /var/run/usbmount/.mount.lock"; exit 1; } + trap '( lockfile-remove /var/run/usbmount/.mount )' 0 + log debug "acquired lock /var/run/usbmount/.mount.lock" + + # Query udev for the expected device information (as we are now running in + # a service's context and the env variables have been lost) + eval $(udevadm info --query=env --export "$DEVNAME" | grep -v '^[^=]*\..*=') + + # Grab device information from device and "divide it" + # FIXME: improvement: implement mounting by label (notice that labels + # can contain spaces, which makes things a little bit less comfortable). + DEVINFO=$(/sbin/blkid -p $DEVNAME) + FSTYPE=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]TYPE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;') + UUID=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]UUID="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;') + USAGE=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]USAGE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;') + + if ! echo $USAGE | egrep -q "(filesystem|disklabel)"; then + log info "$DEVNAME does not contain a filesystem or disklabel" + exit 0 + fi - # Mount the filesystem. - log info "executing command: mount -t$fstype ${options:+-o$options} $DEVNAME $mountpoint" - mount "-t$fstype" "${options:+-o$options}" "$DEVNAME" "$mountpoint" - - # Determine vendor and model. - vendor= - if [ -r "/sys$DEVPATH/device/vendor" ]; then - vendor="`cat \"/sys$DEVPATH/device/vendor\"`" - elif [ -r "/sys$DEVPATH/../device/vendor" ]; then - vendor="`cat \"/sys$DEVPATH/../device/vendor\"`" - elif [ -r "/sys$DEVPATH/device/../manufacturer" ]; then - vendor="`cat \"/sys$DEVPATH/device/../manufacturer\"`" - elif [ -r "/sys$DEVPATH/../device/../manufacturer" ]; then - vendor="`cat \"/sys$DEVPATH/../device/../manufacturer\"`" - fi - vendor="$(echo "$vendor" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')" - - model= - if [ -r "/sys$DEVPATH/device/model" ]; then - model="`cat \"/sys$DEVPATH/device/model\"`" - elif [ -r "/sys$DEVPATH/../device/model" ]; then - model="`cat \"/sys$DEVPATH/../device/model\"`" - elif [ -r "/sys$DEVPATH/device/../product" ]; then - model="`cat \"/sys$DEVPATH/device/../product\"`" - elif [ -r "/sys$DEVPATH/../device/../product" ]; then - model="`cat \"/sys$DEVPATH/../device/../product\"`" + # Try to use specifications in /etc/fstab first. + if egrep -q "^[[:blank:]]*$DEVNAME" /etc/fstab; then + log info "executing command: mount $DEVNAME" + mount $DEVNAME || log err "mount by DEVNAME with $DEVNAME wasn't successful; return code $?" + elif grep -q "^[[:blank:]]*UUID=\"?$UUID\"?" /etc/fstab; then + log info "executing command: mount -U $UUID" + mount -U $UUID || log err "mount by UUID with $UUID wasn't successful; return code $?" + else + log debug "$DEVNAME contains filesystem type $FSTYPE" + + fstype=$FSTYPE + # Test if the filesystem type is in the list of filesystem + # types to mount. + if in_list "$fstype" "$FILESYSTEMS"; then + # Search an available mountpoint. + for v in $MOUNTPOINTS; do + if [ -d "$v" ] && ! grep -q "^[^ ][^ ]* *$v " /proc/mounts; then + mountpoint="$v" + log debug "mountpoint $mountpoint is available for $DEVNAME" + break + fi + done + if [ -n "$mountpoint" ]; then + # Determine mount options. + options= + for v in $FS_MOUNTOPTIONS; do + if expr "$v" : "-fstype=$fstype,."; then + options="$(echo "$v" | sed 's/^[^,]*,//')" + break + fi + done + if [ -n "$MOUNTOPTIONS" ]; then + options="$MOUNTOPTIONS${options:+,$options}" + fi + + # Mount the filesystem. + log info "executing command: mount -t$fstype ${options:+-o$options} $DEVNAME $mountpoint" + mount "-t$fstype" "${options:+-o$options}" "$DEVNAME" "$mountpoint" + + # Determine vendor and model. + vendor= + if [ -r "/sys$DEVPATH/device/vendor" ]; then + vendor="`cat \"/sys$DEVPATH/device/vendor\"`" + elif [ -r "/sys$DEVPATH/../device/vendor" ]; then + vendor="`cat \"/sys$DEVPATH/../device/vendor\"`" + elif [ -r "/sys$DEVPATH/device/../manufacturer" ]; then + vendor="`cat \"/sys$DEVPATH/device/../manufacturer\"`" + elif [ -r "/sys$DEVPATH/../device/../manufacturer" ]; then + vendor="`cat \"/sys$DEVPATH/../device/../manufacturer\"`" + fi + vendor="$(echo "$vendor" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')" + + model= + if [ -r "/sys$DEVPATH/device/model" ]; then + model="`cat \"/sys$DEVPATH/device/model\"`" + elif [ -r "/sys$DEVPATH/../device/model" ]; then + model="`cat \"/sys$DEVPATH/../device/model\"`" + elif [ -r "/sys$DEVPATH/device/../product" ]; then + model="`cat \"/sys$DEVPATH/device/../product\"`" + elif [ -r "/sys$DEVPATH/../device/../product" ]; then + model="`cat \"/sys$DEVPATH/../device/../product\"`" + fi + model="$(echo "$model" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')" + + # Run hook scripts; ignore errors. + export UM_DEVICE="$DEVNAME" + export UM_MOUNTPOINT="$mountpoint" + export UM_FILESYSTEM="$fstype" + export UM_MOUNTOPTIONS="$options" + export UM_VENDOR="$vendor" + export UM_MODEL="$model" + log info "executing command: run-parts /etc/usbmount/mount.d" + run-parts /etc/usbmount/mount.d || : + else + # No suitable mount point found. + log warning "no mountpoint found for $DEVNAME" + exit 1 + fi fi - model="$(echo "$model" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')" - - # Run hook scripts; ignore errors. - export UM_DEVICE="$DEVNAME" - export UM_MOUNTPOINT="$mountpoint" - export UM_FILESYSTEM="$fstype" - export UM_MOUNTOPTIONS="$options" - export UM_VENDOR="$vendor" - export UM_MODEL="$model" - log info "executing command: run-parts /etc/usbmount/mount.d" - run-parts /etc/usbmount/mount.d || : - else - # No suitable mount point found. - log warning "no mountpoint found for $DEVNAME" - exit 1 - fi fi - fi elif [ "$1" = remove ]; then - # A block or partition device has been removed. - # Test if it is mounted. - while read device mountpoint fstype remainder; do - if [ "$DEVNAME" = "$device" ]; then - # If the mountpoint and filesystem type are maintained by - # this script, unmount the filesystem. - if in_list "$mountpoint" "$MOUNTPOINTS" && - in_list "$fstype" "$FILESYSTEMS"; then - log info "executing command: umount -l $mountpoint" - umount -l "$mountpoint" - - # Run hook scripts; ignore errors. - export UM_DEVICE="$DEVNAME" - export UM_MOUNTPOINT="$mountpoint" - export UM_FILESYSTEM="$fstype" - log info "executing command: run-parts /etc/usbmount/umount.d" - run-parts /etc/usbmount/umount.d || : - fi - break - fi - done < /proc/mounts + # A block or partition device has been removed. + # Test if it is mounted. + while read device mountpoint fstype remainder; do + if [ "$DEVNAME" = "$device" ]; then + # If the mountpoint and filesystem type are maintained by + # this script, unmount the filesystem. + if in_list "$mountpoint" "$MOUNTPOINTS" && + in_list "$fstype" "$FILESYSTEMS"; then + log info "executing command: umount -l $mountpoint" + umount -l "$mountpoint" + + # Run hook scripts; ignore errors. + export UM_DEVICE="$DEVNAME" + export UM_MOUNTPOINT="$mountpoint" + export UM_FILESYSTEM="$fstype" + log info "executing command: run-parts /etc/usbmount/umount.d" + run-parts /etc/usbmount/umount.d || : + fi + break + fi + done < /proc/mounts else - log err "unexpected: action '$1'" - exit 1 + log err "unexpected: action '$1'" + exit 1 fi log debug "usbmount execution finished" From a6781cdddd8598861c60fb7d07aca822250e4662 Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Tue, 23 Oct 2018 21:16:07 +0200 Subject: [PATCH 03/20] Add .editorconfig editor configuration file Ensure consistent editing where supported by adding the optional .editorconfig. This does not enforce any style and only works if the editor supports it (via a plugin for example). Signed-off-by: Olliver Schinagl --- .editorconfig | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..528a570 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = tab +insert_final_newline = true +trim_trailing_whitespace = true From 6a2bab3f6e06998fd9644dfb9dc790bc392a9ffe Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Wed, 24 Oct 2018 21:14:44 +0200 Subject: [PATCH 04/20] Put udevadm in a variable By putting udevadm in a variable we only have one place to have the whole string, could easily replace it with UDEVADM="$(command -v udevadm)" or otherwise. Furthermore add a test for the executability of the binary. Signed-off-by: Olliver Schinagl --- usbmount | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/usbmount b/usbmount index a2d2337..9b5332e 100755 --- a/usbmount +++ b/usbmount @@ -50,6 +50,8 @@ MOUNTOPTIONS= FS_MOUNTOPTIONS= VERBOSE=no +UDEVADM="/bin/udevadm" + if [ -r /etc/usbmount/usbmount.conf ]; then . /etc/usbmount/usbmount.conf log debug "loaded usbmount configurations" @@ -65,6 +67,11 @@ if [ ! -x /sbin/blkid ]; then exit 1 fi +if [ ! -x "${UDEVADM}" ]; then + log err "cannot execute '${UDEVADM}'" + exit 1 +fi + # Per Policy 9.3.2, directories under /var/run have to be created # after every reboot. if [ ! -e /var/run/usbmount ]; then @@ -86,7 +93,7 @@ if [ "$1" = add ]; then # Query udev for the expected device information (as we are now running in # a service's context and the env variables have been lost) - eval $(udevadm info --query=env --export "$DEVNAME" | grep -v '^[^=]*\..*=') + eval $("${UDEVADM}" info --query=env --export "$DEVNAME" | grep -v '^[^=]*\..*=') # Grab device information from device and "divide it" # FIXME: improvement: implement mounting by label (notice that labels From ea3c64ed6cab122767663aad898cffeb8663b9b0 Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Wed, 24 Oct 2018 21:10:22 +0200 Subject: [PATCH 05/20] Depend on systemd (for udevadm/systemd-escape) As udevadm, systemd-escape and the various service files are hard requirement, list it as explicitly so in the debian control file. Signed-off-by: Olliver Schinagl --- debian/control | 1 + 1 file changed, 1 insertion(+) diff --git a/debian/control b/debian/control index 68ea40a..01f7771 100644 --- a/debian/control +++ b/debian/control @@ -13,6 +13,7 @@ Package: usbmount Architecture: all Depends: lockfile-progs, + systemd, udev, ${misc:Depends} Recommends: From d485c2f49a0ffc3a89542e0d321941b1df807cc4 Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Wed, 24 Oct 2018 21:06:32 +0200 Subject: [PATCH 06/20] Drop grep from udevadm According to the udevadm manual page --export produces a parseable list. -x, --export Print output as key/value pairs. Values are enclosed in single quotes. So using grep to drop lines that are not key/value pairs is just a resource waste without doing anything. Signed-off-by: Olliver Schinagl --- usbmount | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usbmount b/usbmount index 9b5332e..2826391 100755 --- a/usbmount +++ b/usbmount @@ -93,7 +93,7 @@ if [ "$1" = add ]; then # Query udev for the expected device information (as we are now running in # a service's context and the env variables have been lost) - eval $("${UDEVADM}" info --query=env --export "$DEVNAME" | grep -v '^[^=]*\..*=') + eval $("${UDEVADM}" info --query=env --export "$DEVNAME") # Grab device information from device and "divide it" # FIXME: improvement: implement mounting by label (notice that labels From 0aa917fc81c352f547bd2ad5bd10ddc4f9ae036e Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Tue, 23 Oct 2018 20:47:17 +0200 Subject: [PATCH 07/20] Put blkid in a variable By putting blkid in a variable we only have one place to have the whole string, could easily replace it with BLKID="$(command -v blkid)" or otherwise. Signed-off-by: Olliver Schinagl --- usbmount | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/usbmount b/usbmount index 2826391..1b875b9 100755 --- a/usbmount +++ b/usbmount @@ -50,6 +50,7 @@ MOUNTOPTIONS= FS_MOUNTOPTIONS= VERBOSE=no +BLKID="/sbin/blkid" UDEVADM="/bin/udevadm" if [ -r /etc/usbmount/usbmount.conf ]; then @@ -62,8 +63,8 @@ if [ "${ENABLED:-1}" -eq 0 ]; then exit 0 fi -if [ ! -x /sbin/blkid ]; then - log err "cannot execute /sbin/blkid" +if [ ! -x "${BLKID}" ]; then + log err "cannot execute '${BLKID}'" exit 1 fi @@ -98,7 +99,7 @@ if [ "$1" = add ]; then # Grab device information from device and "divide it" # FIXME: improvement: implement mounting by label (notice that labels # can contain spaces, which makes things a little bit less comfortable). - DEVINFO=$(/sbin/blkid -p $DEVNAME) + DEVINFO=$("${BLKID}" -p $DEVNAME) FSTYPE=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]TYPE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;') UUID=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]UUID="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;') USAGE=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]USAGE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;') From 728c0c07b5abbdb15200d1266db1f4b25a7e64d8 Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Wed, 24 Oct 2018 19:54:39 +0200 Subject: [PATCH 08/20] Depend on util-linux (for blkid) The busybox variant of blkid does not work by default with usbmount. So ensure we depend on the real thing. Things not supported: -p gets ignored and 'USAGE' is not returned. -o value is not supported -s FIELD is ignored It basically just dumps the known info and that's that. Signed-off-by: Olliver Schinagl --- debian/control | 1 + 1 file changed, 1 insertion(+) diff --git a/debian/control b/debian/control index 01f7771..3bc0d13 100644 --- a/debian/control +++ b/debian/control @@ -12,6 +12,7 @@ Homepage: http://github.com/rbrito/usbmount Package: usbmount Architecture: all Depends: + util-linux, lockfile-progs, systemd, udev, From ec8ba8491cf87d210ffefb3f5edc174f91058818 Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Tue, 23 Oct 2018 20:50:21 +0200 Subject: [PATCH 09/20] Put the usbmount directories in variables As with blkid, having the standard usbmount directories in variables makes use of these variables less error-prone on usage. Signed-off-by: Olliver Schinagl --- usbmount | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/usbmount b/usbmount index 1b875b9..9a3eab5 100755 --- a/usbmount +++ b/usbmount @@ -52,14 +52,16 @@ VERBOSE=no BLKID="/sbin/blkid" UDEVADM="/bin/udevadm" +USBMOUNT_CONF="/etc/usbmount/usbmount.conf" +USBMOUNT_VAR="/var/run/usbmount/" -if [ -r /etc/usbmount/usbmount.conf ]; then - . /etc/usbmount/usbmount.conf +if [ -r "${USBMOUNT_CONF}" ]; then + . "${USBMOUNT_CONF}" log debug "loaded usbmount configurations" fi if [ "${ENABLED:-1}" -eq 0 ]; then - log info "usbmount is disabled, see /etc/usbmount/usbmount.conf" + log info "usbmount is disabled, see ${USBMOUNT_CONF}" exit 0 fi @@ -75,9 +77,9 @@ fi # Per Policy 9.3.2, directories under /var/run have to be created # after every reboot. -if [ ! -e /var/run/usbmount ]; then - mkdir -p /var/run/usbmount - log debug "creating /var/run/usbmount directory" +if [ ! -e "${USBMOUNT_VAR}" ]; then + mkdir -p "${USBMOUNT_VAR}" + log debug "creating ${USBMOUNT_VAR} directory" fi umask 022 @@ -86,11 +88,11 @@ umask 022 if [ "$1" = add ]; then # Acquire lock. - log debug "trying to acquire lock /var/run/usbmount/.mount.lock" - lockfile-create --retry 3 /var/run/usbmount/.mount || \ - { log err "cannot acquire lock /var/run/usbmount/.mount.lock"; exit 1; } - trap '( lockfile-remove /var/run/usbmount/.mount )' 0 - log debug "acquired lock /var/run/usbmount/.mount.lock" + log debug "trying to acquire lock ${USBMOUNT_VAR}/.mount.lock" + lockfile-create --retry 3 "${USBMOUNT_VAR}/.mount" || \ + { log err "cannot acquire lock ${USBMOUNT_VAR}/.mount.lock"; exit 1; } + trap '( lockfile-remove "${USBMOUNT_VAR}/.mount" )' 0 + log debug "acquired lock ${USBMOUNT_VAR}/.mount.lock" # Query udev for the expected device information (as we are now running in # a service's context and the env variables have been lost) From 0a8b31125996c478c5fd02b9b6878a177221354d Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Tue, 23 Oct 2018 20:51:37 +0200 Subject: [PATCH 10/20] Quote and use curly bracers around variables Using quotes and curly braces around shell variables everywhere can be considered good programming practise andtends to be more consistent. Signed-off-by: Olliver Schinagl --- usbmount | 144 +++++++++++++++++++++++++------------------------- usbmount.conf | 2 +- 2 files changed, 73 insertions(+), 73 deletions(-) diff --git a/usbmount b/usbmount index 9a3eab5..c7433dc 100755 --- a/usbmount +++ b/usbmount @@ -22,8 +22,8 @@ exec > /dev/null 2>&1 # Log a string via the syslog facility. log() { - if [ $1 != debug ] || expr "$VERBOSE" : "[yY]" > /dev/null; then - logger -p user.$1 -t "usbmount[$$]" -- "$2" + if [ "${1}" != "debug" ] || expr "${VERBOSE}" : "[yY]" > /dev/null; then + logger -p "user.${1}" -t "usbmount[$$]" -- "${2}" fi } @@ -32,8 +32,8 @@ log() # parameter. in_list() { - for v in $2; do - [ "$1" != "$v" ] || return 0 + for v in ${2}; do + [ "${1}" != "${v}" ] || return 0 done return 1 } @@ -48,7 +48,7 @@ MOUNTPOINTS= FILESYSTEMS= MOUNTOPTIONS= FS_MOUNTOPTIONS= -VERBOSE=no +VERBOSE="no" BLKID="/sbin/blkid" UDEVADM="/bin/udevadm" @@ -85,7 +85,7 @@ fi umask 022 -if [ "$1" = add ]; then +if [ "${1}" = "add" ]; then # Acquire lock. log debug "trying to acquire lock ${USBMOUNT_VAR}/.mount.lock" @@ -96,126 +96,126 @@ if [ "$1" = add ]; then # Query udev for the expected device information (as we are now running in # a service's context and the env variables have been lost) - eval $("${UDEVADM}" info --query=env --export "$DEVNAME") + eval $("${UDEVADM}" info --query=env --export "${DEVNAME}") # Grab device information from device and "divide it" # FIXME: improvement: implement mounting by label (notice that labels # can contain spaces, which makes things a little bit less comfortable). - DEVINFO=$("${BLKID}" -p $DEVNAME) - FSTYPE=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]TYPE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;') - UUID=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]UUID="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;') - USAGE=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]USAGE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;') + DEVINFO="$("${BLKID}" -p "${DEVNAME}")" + FSTYPE="$(echo "${DEVINFO}" | sed 's/.*[[:blank:]]TYPE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')" + UUID="$(echo "${DEVINFO}" | sed 's/.*[[:blank:]]UUID="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')" + USAGE="$(echo "${DEVINFO}" | sed 's/.*[[:blank:]]USAGE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')" - if ! echo $USAGE | egrep -q "(filesystem|disklabel)"; then - log info "$DEVNAME does not contain a filesystem or disklabel" + if ! echo "${USAGE}" | egrep -q "(filesystem|disklabel)"; then + log info "${DEVNAME} does not contain a filesystem or disklabel" exit 0 fi # Try to use specifications in /etc/fstab first. - if egrep -q "^[[:blank:]]*$DEVNAME" /etc/fstab; then - log info "executing command: mount $DEVNAME" - mount $DEVNAME || log err "mount by DEVNAME with $DEVNAME wasn't successful; return code $?" - elif grep -q "^[[:blank:]]*UUID=\"?$UUID\"?" /etc/fstab; then - log info "executing command: mount -U $UUID" - mount -U $UUID || log err "mount by UUID with $UUID wasn't successful; return code $?" + if egrep -q "^[[:blank:]]*${DEVNAME}" "/etc/fstab"; then + log info "executing command: mount ${DEVNAME}" + mount "${DEVNAME}" || log err "mount by DEVNAME with ${DEVNAME} wasn't successful; return code ${?}" + elif grep -q "^[[:blank:]]*UUID=\"?${UUID}\"?" "/etc/fstab"; then + log info "executing command: mount -U ${UUID}" + mount -U "${UUID}" || log err "mount by UUID with ${UUID} wasn't successful; return code ${?}" else - log debug "$DEVNAME contains filesystem type $FSTYPE" + log debug "${DEVNAME} contains filesystem type ${FSTYPE}" - fstype=$FSTYPE + fstype="${FSTYPE}" # Test if the filesystem type is in the list of filesystem # types to mount. - if in_list "$fstype" "$FILESYSTEMS"; then + if in_list "${fstype}" "${FILESYSTEMS}"; then # Search an available mountpoint. - for v in $MOUNTPOINTS; do - if [ -d "$v" ] && ! grep -q "^[^ ][^ ]* *$v " /proc/mounts; then - mountpoint="$v" - log debug "mountpoint $mountpoint is available for $DEVNAME" + for v in ${MOUNTPOINTS}; do + if [ -d "${v}" ] && ! grep -q "^[^ ][^ ]* *${v} " "/proc/mounts"; then + mountpoint="${v}" + log debug "mountpoint ${mountpoint} is available for ${DEVNAME}" break fi done - if [ -n "$mountpoint" ]; then + if [ -n "${mountpoint}" ]; then # Determine mount options. options= - for v in $FS_MOUNTOPTIONS; do - if expr "$v" : "-fstype=$fstype,."; then - options="$(echo "$v" | sed 's/^[^,]*,//')" + for v in ${FS_MOUNTOPTIONS}; do + if expr "${v}" : "-fstype=${fstype},."; then + options="$(echo "${v}" | sed 's/^[^,]*,//')" break fi done - if [ -n "$MOUNTOPTIONS" ]; then - options="$MOUNTOPTIONS${options:+,$options}" + if [ -n "${MOUNTOPTIONS}" ]; then + options="${MOUNTOPTIONS}${options:+,${options}}" fi # Mount the filesystem. - log info "executing command: mount -t$fstype ${options:+-o$options} $DEVNAME $mountpoint" - mount "-t$fstype" "${options:+-o$options}" "$DEVNAME" "$mountpoint" + log info "executing command: mount -t${fstype} ${options:+-o${options}} ${DEVNAME} ${mountpoint}" + mount "-t${fstype}" "${options:+-o${options}}" "${DEVNAME}" "${mountpoint}" # Determine vendor and model. vendor= - if [ -r "/sys$DEVPATH/device/vendor" ]; then - vendor="`cat \"/sys$DEVPATH/device/vendor\"`" - elif [ -r "/sys$DEVPATH/../device/vendor" ]; then - vendor="`cat \"/sys$DEVPATH/../device/vendor\"`" - elif [ -r "/sys$DEVPATH/device/../manufacturer" ]; then - vendor="`cat \"/sys$DEVPATH/device/../manufacturer\"`" - elif [ -r "/sys$DEVPATH/../device/../manufacturer" ]; then - vendor="`cat \"/sys$DEVPATH/../device/../manufacturer\"`" + if [ -r "/sys${DEVPATH}/device/vendor" ]; then + vendor="`cat \"/sys"${DEVPATH}"/device/vendor\"`" + elif [ -r "/sys${DEVPATH}/../device/vendor" ]; then + vendor="`cat \"/sys"${DEVPATH}"/../device/vendor\"`" + elif [ -r "/sys${DEVPATH}/device/../manufacturer" ]; then + vendor="`cat \"/sys"${DEVPATH}"/device/../manufacturer\"`" + elif [ -r "/sys${DEVPATH}/../device/../manufacturer" ]; then + vendor="`cat \"/sys"${DEVPATH}"/../device/../manufacturer\"`" fi - vendor="$(echo "$vendor" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')" + vendor="$(echo "${vendor}" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')" model= - if [ -r "/sys$DEVPATH/device/model" ]; then - model="`cat \"/sys$DEVPATH/device/model\"`" - elif [ -r "/sys$DEVPATH/../device/model" ]; then - model="`cat \"/sys$DEVPATH/../device/model\"`" - elif [ -r "/sys$DEVPATH/device/../product" ]; then - model="`cat \"/sys$DEVPATH/device/../product\"`" - elif [ -r "/sys$DEVPATH/../device/../product" ]; then - model="`cat \"/sys$DEVPATH/../device/../product\"`" + if [ -r "/sys${DEVPATH}/device/model" ]; then + model="`cat \"/sys"${DEVPATH}"/device/model\"`" + elif [ -r "/sys${DEVPATH}/../device/model" ]; then + model="`cat \"/sys"${DEVPATH}"/../device/model\"`" + elif [ -r "/sys${DEVPATH}/device/../product" ]; then + model="`cat \"/sys"${DEVPATH}"/device/../product\"`" + elif [ -r "/sys${DEVPATH}/../device/../product" ]; then + model="`cat \"/sys"${DEVPATH}"/../device/../product\"`" fi - model="$(echo "$model" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')" + model="$(echo "${model}" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')" # Run hook scripts; ignore errors. - export UM_DEVICE="$DEVNAME" - export UM_MOUNTPOINT="$mountpoint" - export UM_FILESYSTEM="$fstype" - export UM_MOUNTOPTIONS="$options" - export UM_VENDOR="$vendor" - export UM_MODEL="$model" + export UM_DEVICE="${DEVNAME}" + export UM_MOUNTPOINT="${mountpoint}" + export UM_FILESYSTEM="${fstype}" + export UM_MOUNTOPTIONS="${options}" + export UM_VENDOR="${vendor}" + export UM_MODEL="${model}" log info "executing command: run-parts /etc/usbmount/mount.d" - run-parts /etc/usbmount/mount.d || : + run-parts "/etc/usbmount/mount.d" || : else # No suitable mount point found. - log warning "no mountpoint found for $DEVNAME" + log warning "no mountpoint found for ${DEVNAME}" exit 1 fi fi fi -elif [ "$1" = remove ]; then +elif [ "${1}" = "remove" ]; then # A block or partition device has been removed. # Test if it is mounted. while read device mountpoint fstype remainder; do - if [ "$DEVNAME" = "$device" ]; then + if [ "${DEVNAME}" = "${device}" ]; then # If the mountpoint and filesystem type are maintained by # this script, unmount the filesystem. - if in_list "$mountpoint" "$MOUNTPOINTS" && - in_list "$fstype" "$FILESYSTEMS"; then - log info "executing command: umount -l $mountpoint" - umount -l "$mountpoint" + if in_list "${mountpoint}" "${MOUNTPOINTS}" && + in_list "${fstype}" "${FILESYSTEMS}"; then + log info "executing command: umount -l ${mountpoint}" + umount -l "${mountpoint}" # Run hook scripts; ignore errors. - export UM_DEVICE="$DEVNAME" - export UM_MOUNTPOINT="$mountpoint" - export UM_FILESYSTEM="$fstype" + export UM_DEVICE="${DEVNAME}" + export UM_MOUNTPOINT="${mountpoint}" + export UM_FILESYSTEM="${fstype}" log info "executing command: run-parts /etc/usbmount/umount.d" - run-parts /etc/usbmount/umount.d || : + run-parts "/etc/usbmount/umount.d" || : fi break fi - done < /proc/mounts + done < "/proc/mounts" else - log err "unexpected: action '$1'" + log err "unexpected: action '${1}'" exit 1 fi diff --git a/usbmount.conf b/usbmount.conf index 97f7ccc..c4f489f 100644 --- a/usbmount.conf +++ b/usbmount.conf @@ -50,4 +50,4 @@ FS_MOUNTOPTIONS="" # If set to "yes", more information will be logged via the syslog # facility. -VERBOSE=no +VERBOSE="no" From cb70ddb22d3496bd480862cdd31e92fd4c956b53 Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Tue, 23 Oct 2018 21:02:01 +0200 Subject: [PATCH 11/20] Remove legacy `..` backtick syntax It has a series of undefined behaviors related to quoting in POSIX. It imposes a custom escaping mode with surprising results. It's exceptionally hard to nest. Signed-off-by: Olliver Schinagl --- usbmount | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/usbmount b/usbmount index c7433dc..3467f69 100755 --- a/usbmount +++ b/usbmount @@ -153,25 +153,25 @@ if [ "${1}" = "add" ]; then # Determine vendor and model. vendor= if [ -r "/sys${DEVPATH}/device/vendor" ]; then - vendor="`cat \"/sys"${DEVPATH}"/device/vendor\"`" + vendor="$(cat \"/sys"${DEVPATH}"/device/vendor\")" elif [ -r "/sys${DEVPATH}/../device/vendor" ]; then - vendor="`cat \"/sys"${DEVPATH}"/../device/vendor\"`" + vendor="$(cat \"/sys"${DEVPATH}"/../device/vendor\")" elif [ -r "/sys${DEVPATH}/device/../manufacturer" ]; then - vendor="`cat \"/sys"${DEVPATH}"/device/../manufacturer\"`" + vendor="$(cat \"/sys"${DEVPATH}"/device/../manufacturer\")" elif [ -r "/sys${DEVPATH}/../device/../manufacturer" ]; then - vendor="`cat \"/sys"${DEVPATH}"/../device/../manufacturer\"`" + vendor="$(cat \"/sys"${DEVPATH}"/../device/../manufacturer\")" fi vendor="$(echo "${vendor}" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')" model= if [ -r "/sys${DEVPATH}/device/model" ]; then - model="`cat \"/sys"${DEVPATH}"/device/model\"`" + model="$(cat \"/sys"${DEVPATH}"/device/model\")" elif [ -r "/sys${DEVPATH}/../device/model" ]; then - model="`cat \"/sys"${DEVPATH}"/../device/model\"`" + model="$(cat \"/sys"${DEVPATH}"/../device/model\")" elif [ -r "/sys${DEVPATH}/device/../product" ]; then - model="`cat \"/sys"${DEVPATH}"/device/../product\"`" + model="$(cat \"/sys"${DEVPATH}"/device/../product\")" elif [ -r "/sys${DEVPATH}/../device/../product" ]; then - model="`cat \"/sys"${DEVPATH}"/../device/../product\"`" + model="$(cat \"/sys"${DEVPATH}"/../device/../product\")" fi model="$(echo "${model}" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')" From 3f7548eb6173d1e9740b05863ed9f5b779ecc054 Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Wed, 24 Oct 2018 19:57:15 +0200 Subject: [PATCH 12/20] Do not parse output of blkid The blkid tool can output exactly what we need using various parameters. This saves us some sed headaches. The only 'downside' is we can't use our own names for the variables, which affects 'FSTYPE' so that was renamed (rather then do a FSTYPE=$TYPE). Signed-off-by: Olliver Schinagl --- usbmount | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/usbmount b/usbmount index 3467f69..fe28e90 100755 --- a/usbmount +++ b/usbmount @@ -98,13 +98,10 @@ if [ "${1}" = "add" ]; then # a service's context and the env variables have been lost) eval $("${UDEVADM}" info --query=env --export "${DEVNAME}") - # Grab device information from device and "divide it" + # Grab device information from device # FIXME: improvement: implement mounting by label (notice that labels # can contain spaces, which makes things a little bit less comfortable). - DEVINFO="$("${BLKID}" -p "${DEVNAME}")" - FSTYPE="$(echo "${DEVINFO}" | sed 's/.*[[:blank:]]TYPE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')" - UUID="$(echo "${DEVINFO}" | sed 's/.*[[:blank:]]UUID="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')" - USAGE="$(echo "${DEVINFO}" | sed 's/.*[[:blank:]]USAGE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')" + eval "$("${BLKID}" -o value -p -s TYPE -s USAGE -s UUID "${DEVNAME}")" if ! echo "${USAGE}" | egrep -q "(filesystem|disklabel)"; then log info "${DEVNAME} does not contain a filesystem or disklabel" @@ -119,9 +116,9 @@ if [ "${1}" = "add" ]; then log info "executing command: mount -U ${UUID}" mount -U "${UUID}" || log err "mount by UUID with ${UUID} wasn't successful; return code ${?}" else - log debug "${DEVNAME} contains filesystem type ${FSTYPE}" + log debug "${DEVNAME} contains filesystem type ${TYPE}" - fstype="${FSTYPE}" + fstype="${TYPE}" # Test if the filesystem type is in the list of filesystem # types to mount. if in_list "${fstype}" "${FILESYSTEMS}"; then From 26c256527d1c77def5a46750fdf878079240ea35 Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Wed, 24 Oct 2018 20:36:35 +0200 Subject: [PATCH 13/20] Remove depracted egrep Using egrep is considered deprecated and should be avoided and not used. While we could use grep -q -E, using two simple string comparissions avoid having to call an external tool, making the script lighter and cleaner. Signed-off-by: Olliver Schinagl --- usbmount | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usbmount b/usbmount index fe28e90..b519179 100755 --- a/usbmount +++ b/usbmount @@ -103,7 +103,7 @@ if [ "${1}" = "add" ]; then # can contain spaces, which makes things a little bit less comfortable). eval "$("${BLKID}" -o value -p -s TYPE -s USAGE -s UUID "${DEVNAME}")" - if ! echo "${USAGE}" | egrep -q "(filesystem|disklabel)"; then + if [ "${USAGE}" != "filesystem" ] && [ "${USAGE}" != "disklabel" ]; then log info "${DEVNAME} does not contain a filesystem or disklabel" exit 0 fi From a365b606099ce52194840ce38e24e2eea5366ce7 Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Wed, 24 Oct 2018 20:45:58 +0200 Subject: [PATCH 14/20] Replace depracted egrep with grep -E Use grep -E when quering fstab to avoid the deprecated egrep. Signed-off-by: Olliver Schinagl --- usbmount | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usbmount b/usbmount index b519179..10d254d 100755 --- a/usbmount +++ b/usbmount @@ -109,7 +109,7 @@ if [ "${1}" = "add" ]; then fi # Try to use specifications in /etc/fstab first. - if egrep -q "^[[:blank:]]*${DEVNAME}" "/etc/fstab"; then + if grep -q -E "^[[:blank:]]*${DEVNAME}" "/etc/fstab"; then log info "executing command: mount ${DEVNAME}" mount "${DEVNAME}" || log err "mount by DEVNAME with ${DEVNAME} wasn't successful; return code ${?}" elif grep -q "^[[:blank:]]*UUID=\"?${UUID}\"?" "/etc/fstab"; then From 1fdd5da9b8b44e1056fedd786050d98234b37f3a Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Wed, 24 Oct 2018 20:57:40 +0200 Subject: [PATCH 15/20] Use mountpoint instead of grep /proc/mounts Parsing /proc/mounts by hand is not needed as we have the 'mountpoint' utility (part of util-linux and busybox). This eases some regex foo which improves readbility. Signed-off-by: Olliver Schinagl --- usbmount | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usbmount b/usbmount index 10d254d..dd8a66c 100755 --- a/usbmount +++ b/usbmount @@ -124,7 +124,7 @@ if [ "${1}" = "add" ]; then if in_list "${fstype}" "${FILESYSTEMS}"; then # Search an available mountpoint. for v in ${MOUNTPOINTS}; do - if [ -d "${v}" ] && ! grep -q "^[^ ][^ ]* *${v} " "/proc/mounts"; then + if [ -d "${v}" ] && ! mountpoint -q "${v}"; then mountpoint="${v}" log debug "mountpoint ${mountpoint} is available for ${DEVNAME}" break From 3079c8e76b1dc6074bbd1ec49a4e9024bb9018e7 Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Wed, 24 Oct 2018 22:15:37 +0200 Subject: [PATCH 16/20] Ensure proper exit While not strictly needed it is considered good practise to ensure proper exit at the end of the script. Signed-off-by: Olliver Schinagl --- usbmount | 1 + 1 file changed, 1 insertion(+) diff --git a/usbmount b/usbmount index dd8a66c..e8c4702 100755 --- a/usbmount +++ b/usbmount @@ -217,3 +217,4 @@ else fi log debug "usbmount execution finished" +exit 0 From ba72fedbf0b56624c7649fa48e890746497c11d3 Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Wed, 24 Oct 2018 21:16:36 +0200 Subject: [PATCH 17/20] Add the -r parameter to read By default, read will interpret backslashes before spaces and line feeds, and otherwise strip them. This is rarely expected or desired. Signed-off-by: Olliver Schinagl --- usbmount | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usbmount b/usbmount index e8c4702..bec2ac4 100755 --- a/usbmount +++ b/usbmount @@ -192,7 +192,7 @@ elif [ "${1}" = "remove" ]; then # A block or partition device has been removed. # Test if it is mounted. - while read device mountpoint fstype remainder; do + while read -r device mountpoint fstype remainder; do if [ "${DEVNAME}" = "${device}" ]; then # If the mountpoint and filesystem type are maintained by # this script, unmount the filesystem. From a41da1c2478a8be1e98c452a31f45a7e39f9353b Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Wed, 24 Oct 2018 21:18:14 +0200 Subject: [PATCH 18/20] Remove unused variables The read utility reads multiple variables with a dummy variable called 'remainder'. Instead, use the more common and well recognized by checkers '_' (underscore) as a dummy variable. Signed-off-by: Olliver Schinagl --- usbmount | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usbmount b/usbmount index bec2ac4..524b6f8 100755 --- a/usbmount +++ b/usbmount @@ -192,7 +192,7 @@ elif [ "${1}" = "remove" ]; then # A block or partition device has been removed. # Test if it is mounted. - while read -r device mountpoint fstype remainder; do + while read -r device mountpoint fstype _; do if [ "${DEVNAME}" = "${device}" ]; then # If the mountpoint and filesystem type are maintained by # this script, unmount the filesystem. From ffe40663ab3e770b6d2bd1a8bfae10200c29a08d Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Wed, 24 Oct 2018 21:19:55 +0200 Subject: [PATCH 19/20] Ensure grep is called with -E when using regex When passing regex to grep, make this explicit with the -E parameter. Signed-off-by: Olliver Schinagl --- usbmount | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usbmount b/usbmount index 524b6f8..8022267 100755 --- a/usbmount +++ b/usbmount @@ -112,7 +112,7 @@ if [ "${1}" = "add" ]; then if grep -q -E "^[[:blank:]]*${DEVNAME}" "/etc/fstab"; then log info "executing command: mount ${DEVNAME}" mount "${DEVNAME}" || log err "mount by DEVNAME with ${DEVNAME} wasn't successful; return code ${?}" - elif grep -q "^[[:blank:]]*UUID=\"?${UUID}\"?" "/etc/fstab"; then + elif grep -q -E "^[[:blank:]]*UUID=\"?${UUID}\"?" "/etc/fstab"; then log info "executing command: mount -U ${UUID}" mount -U "${UUID}" || log err "mount by UUID with ${UUID} wasn't successful; return code ${?}" else From a54a2eeb2522b3c249906efedecec05b58b46474 Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Wed, 24 Oct 2018 21:20:42 +0200 Subject: [PATCH 20/20] Add sourced file helper for shellcheck With usbmount now being shellcheck compliant, there is only one remaining warning, SC1091 about our sourced file. While we cannot scan whatever the user has installed, we can point shellcheck to the local default config instead. Note, that this will still produce a warning on shellcheck.net as it obviously still cannot access the file. Signed-off-by: Olliver Schinagl --- usbmount | 1 + 1 file changed, 1 insertion(+) diff --git a/usbmount b/usbmount index 8022267..fb5fd24 100755 --- a/usbmount +++ b/usbmount @@ -56,6 +56,7 @@ USBMOUNT_CONF="/etc/usbmount/usbmount.conf" USBMOUNT_VAR="/var/run/usbmount/" if [ -r "${USBMOUNT_CONF}" ]; then + # shellcheck source=usbmount.conf . "${USBMOUNT_CONF}" log debug "loaded usbmount configurations" fi