From d4e4ff64cbf6c69f0502a82ee11a57263c7907c9 Mon Sep 17 00:00:00 2001 From: "Kai Sven Engelbart (PEA4-Fe)" Date: Fri, 5 Mar 2021 11:11:02 +0100 Subject: [PATCH] Add usb-detect script and udev rule --- usb-detect/10-usb-detect.rules | 1 + usb-detect/usb-detect.sh | 55 ++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 usb-detect/10-usb-detect.rules create mode 100755 usb-detect/usb-detect.sh diff --git a/usb-detect/10-usb-detect.rules b/usb-detect/10-usb-detect.rules new file mode 100644 index 0000000..98c55a8 --- /dev/null +++ b/usb-detect/10-usb-detect.rules @@ -0,0 +1 @@ +ACTION=="add", KERNEL=="sd[b-z]1", SUBSYSTEM=="block", RUN+="/home/kske/usb.sh" diff --git a/usb-detect/usb-detect.sh b/usb-detect/usb-detect.sh new file mode 100755 index 0000000..bd5c921 --- /dev/null +++ b/usb-detect/usb-detect.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +# udev script for mounting a block device +# and processing the files it contains + +# To use it, place 10-usb-detect.rules +# in /etc/udev/rules.d + +# Redirect stdout and stderr to log file +LOGFILE=/var/log/usb-detect.log +exec &>> $LOGFILE + +# Processing function (file name is passed as parameter) +PROCESS_FUNC=cat + +echo "USB device detected at $DEVNAME" + +MOUNTPOINT=/mnt$DEVNAME + +echo "Mounting device at $MOUNTPOINT" + +mkdir -p $MOUNTPOINT +mount $DEVNAME $MOUNTPOINT + +echo "Extracting data..." + +shopt -s nullglob +found=0 +for f in $MOUNTPOINT/*.[0-9] +do + found=1 + echo "Processing $f..." + $PROCESS_FUNC $f + echo "Removing $f..." + rm $f +done +shopt -u nullglob + +[ $found -eq 0 ] && echo "$MOUNTPOINT is empty" + +echo "Umounting..." +umount $MOUNTPOINT + +echo "Removing $MOUNTPOINT..." +rm -r $MOUNTPOINT + +if [ -z `ls -A /mnt/dev` ] +then + echo "Removing /mnt/dev/..." + rmdir /mnt/dev +else + echo "Not removing /mnt/dev/ as other devices are mounted." +fi + +echo "Done!"