#!/bin/bash # openbox pipe menu to [u]mount removable & view mounts # (c) Denis Kaganovich, Anarchy or GPLv2 notify="/usr/bin/notify-send -t 6000" xml(){ x="${x//&/&}" x="${x///>}" x="${x//\'/'}" x="${x//\"/"}" } _removable(){ [ "$DEVNAME" != "$d" -o -b "$d" ] || return 1 local x= i s= for i in /sys/block/*/"$DEVNAME"; do [ -e "$i" ] && s="${i/$DEVNAME}" && break done [ -z "$s" ] && s="/sys/block/$DEVNAME" && ! [ -e "$s" ] && return 1 read x <"$s/removable" read model <"$s/device/model" || read model <"$s/device/name" return 0 } _umnt(){ autofs=false while read d m t f x; do model= f=",$f," dd="$d" case "$1:$2" in /etc/fstab:mount) [ "$UID" = 0 -o -z "${f##*,users,*}" -o -z "${f##*,user,*}" ] || continue ;;& /etc/fstab:umount) [ "$UID" = 0 -o -z "${f##*,users,*}" ] || continue ;;& /etc/mtab:umount) [ -z "${f##*,user=$USER,*}" ] || continue ;;& /etc/fstab:*) d1= case "$d" in UUID=*|LABEL=*) d1="${d%%=*}" d1="/dev/disk/by-${d1,,}/${d#*=}" ;;& LABEL=*) if ! [ -L "$d1" ]; then d1=$(/bin/mount -nfv "$d" |& { d1= while read i; do x="${i##*/dev/}" [ "$x" = "$i" ] && continue i="/dev/${x%% *}" [ -b "$i" ] || continue [ -n "$d1" -a "$d1" != "$i" ] && break d1="$i" done echo -n "$d1" }) echo "$d1" fi ;; esac [ -n "$d1" ] && d="$d1" [ -L "$d" ] && d=`readlink -f "$d"` ;;& *:mount|*:umount) DEVNAME="${d#/dev/}" _removable || continue ;;& *:fs) case "$d $m $t" in /var/run/auto.dev\ /mnt/auto/disk\ autofs)autofs=true;continue;; *\ autofs)continue;; */*\ *\ *);; *)continue;; esac DEVNAME="${d#/dev/}" _removable ;;& /etc/fstab:*) d1= while read d1 m1; do [ "$d1" = "$d" ] && break done " ;; *:umount)x="'$d'";;& *:mount)x="'$m'";;& *) xml echo "${notify_}/bin/$2 $x" ;; esac done <"$1" $autofs || return echo '' for x in /mnt/auto; do echo "" done for x in /dev/disk/by-uuid/*; do d=`readlink -f "$x"`||continue DEVNAME="${d#/dev/}" xml while read i m t f; do if [ "$i $m $t" = "/var/run/auto.dev /mnt/auto/$DEVNAME autofs" ]; then _removable && r='~' || r= echo "" fi done &1`" || { r=$? e="ERROR[$r]" } [ $r = 0 ] && case "$1" in /bin/umount)/usr/bin/eject "$2";; # "ejected" usb can be detached too esac i="$*" i="${i%% *}" shift m="${i##*/} $* $e${m:+ $m}" $notify "$m" return $r } obuser(){ local i user= i=`grep -sazl "^/usr/bin/openbox$" /proc/[0-9]*/cmdline` || return 1 i="/proc/${i//[^0-9 ]/}/environ" # only single session [ -e "$i" ] || return 1 export DBUS_SESSION_BUS_ADDRESS=`grep -sazh "^DBUS_SESSION_BUS_ADDRESS=" "$i"|sed -e "s:\x0:\n:g" -e 's:^.*=::'` user=`grep -sazh "^USER=" "$i"|sed -e "s:\x0:\n:g" -e 's:^.*=::'` } case "$1:$ACTION" in block:remove) # udev/hotplug: umount every removed notify=exec [ -x "${notify%% *}" ] && obuser && [ -n "$DBUS_SESSION_BUS_ADDRESS" ] && notify_=_exec ${notify_} /bin/umount -l "/dev/$DEVNAME" ;; block:add) # udev/hotplug: mount only if openbox user found d="/dev/$DEVNAME" _removable && obuser && [ -n "$user" ] || exit m="/mnt/auto/$DEVNAME" mkdir -p "$m" notify_='_exec ' [ -n "$DBUS_SESSION_BUS_ADDRESS" -a -x "${notify%% *}" ] || notify_= ${notify_}sudo -u "$u" /bin/mount "$d" "$m" || rmdir "$m" ;; exec:*) shift _exec "${@}" exit $? ;; *) notify_="$0 exec " [ -n "$DBUS_SESSION_BUS_ADDRESS" -a -x "${notify%% *}" ] || notify_= echo "" USER="${2:-$USER}"2 case "$1:$UID" in umount:0|fs:*) _umnt /proc/mounts "${@}" ;; umount:*) _umnt /etc/mtab "${@}" _umnt /etc/fstab "${@}" ;; mount:*) _umnt /etc/fstab "${@}" ;; esac echo '' ;; esac 2>/dev/null