kiekus/kiekmon-blk

73 lines
1.9 KiB
Bash
Executable File

#!/bin/sh
set -e
action=watch
blkpattern=
blkname=
blkuuid=
blkfs=
mntprefix=/run/kiekblk
usage() {
echo "usage: $0 [-h] [-r | -w [-n PARTNAME] [-u PARTUUID] [-t FSTYPE] [-m MNTPREFIX]] PATTERN" >&2
}
while getopts hwrn:u:t:m: opt; do
case "$opt" in
w) action=watch;;
r) action=remove;;
d) devpattern="$OPTARG";;
n) blkname="$OPTARG";;
u) blkuuid="$OPTARG";;
t) blkfs="$OPTARG";;
m) mntprefix="$OPTARG";;
h) usage; exit 0;;
?) usage; exit 255;;
esac
done
shift $(($OPTIND - 1))
blkpattern="${1:-*}"
case "$action" in
watch)
inotifywait -e create,delete -m /dev | {
devices=
while read -r _ action devname; do
case "$devname" in
$blkpattern);;
*) continue;;
esac
case "$action" in
CREATE)
eval $(blkid /dev/$devname | cut -d: -f2-)
if [ -n "$blkname" ] && [ "$blkname" != "$NAME" ]; then
continue
fi
if [ -n "$blkuuid" ] && [ "$blkuuid" != "$UUID" ] && [ "$blkuuid" != "$PARTUUID" ]; then
continue
fi
if [ -n "$blkfs" ] && [ "$blkfs" != "$FSTYPE" ]; then
continue
fi
mkdir -p "$mntprefix/$devname"
if mount ${blkfs:+-t "$blkfs"} /dev/"$devname" "$mntprefix/$devname"; then
devices="$(printf "%s\n%s\n" "$devices" "$devname")"
rp-make "$mntprefix/$devname" protocol=file rm_command="$0 -r '$mntprefix/$devname'"
else
rmdir "$mntprefix/$devname"
fi
;;
DELETE)
devices=$(printf "%s\n" "$devices" | grep -v "^$devname\$")
;;
esac
done
}
;;
remove)
umount "$blkpattern"
rmdir "$blkpattern"
;;
esac