Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
UDEV-only notifcations for removable media.
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Desktop Environments
View previous topic :: View next topic  
Author Message
VoVaN
l33t
l33t


Joined: 02 Jul 2003
Posts: 672
Location: The Netherlands

PostPosted: Wed May 09, 2012 12:26 pm    Post subject: UDEV-only notifcations for removable media. Reply with quote

Hello,

I've been searching for a _simple_, based only on udev (without any additional daemons), notification solution primarily for notifications regarding removable media. However, I haven't been able to find the one suitable for me. Below is the list of what I've tried:
- uam (in portage): simple and clear, but provides only automounting with notifications. Though it's possible to extend it.
- udev-notify http://udev-notify.learnfree.eu/: Looks good and provides notifications for a lot of events include removable media. However, it runs as a daemon and written in Python, so not acceptable for me.
- uevt (in portage): looks good, but again it's a daemon

Today I've decided to spend some time to write a simple script to do what I want. All you have to do is create two files /lib/udev/rules.d/80-udev-notify.rules and /lib/udev/udev-notify.sh. The files are below. I'm open to any comments, notes, suggestions!

/lib/udev/rules.d/80-udev-notify.rules:
Code:
# Rule set for udev devices notifications

SUBSYSTEM!="block", GOTO="udev_notify_end"

LABEL="udev_notify"

ACTION=="add", RUN+="/lib/udev/udev-notify.sh"
ACTION=="remove", RUN+="/lib/udev/udev-notify.sh"
ACTION=="change", ENV{DISK_MEDIA_CHANGE}=="1", RUN+="/lib/udev/udev-notify.sh"
ACTION=="change", ENV{DISK_EJECT_REQUEST}=="1", RUN+="/lib/udev/udev-notify.sh"

LABEL="udev_notify_end"

# vim:syn=udevrules

/lib/udev/udev-notify.sh
Code:

#!/bin/sh -e
case $ACTION in
        add)
                N_ACTION=added
                ;;
        remove)
                N_ACTION=removed
                ;;
        change)
                N_ACTION=changed
                ;;
        *)
                ;;
esac

# Default title
N_TITLE="Device recognized"
# Default icon
N_ICON=dialog-information
# Setting icon/title for known devices
if [[ $SUBSYSTEM == usb && $DEVTYPE == usb_device && -n $ID_MEDIA_PLAYER ]]; then
        N_TITLE="Multimedia Player $N_ACTION"
        N_ICON=multimedia-player
elif [ $SUBSYSTEM == mmc ]; then
        N_TITLE="SD/MMC Memory $N_ACTION"
        N_ICON=gnome-dev-media-sdmmc
elif [[ $SUBSYSTEM == memstick || $ID_DRIVE_FLASH_MS == 1 ]]; then
        N_TITLE="Memory Stick $N_ACTION"
        N_ICON=gnome-dev-media-ms
elif [ $ID_DRIVE_FLASH_SM == 1 ]; then
        N_TITLE="Smart Media $N_ACTION"
        N_ICON=gnome-dev-media-sm
elif [ $ID_DRIVE_FLASH_CF == 1 ]; then
        N_TITLE="Compact Flash $N_ACTION"
        N_ICON=gnome-dev-media-cf
elif [[ $ID_TYPE == cd && $DEVTYPE == disk ]]; then
        N_TITLE="CD-ROM Drive $N_ACTIONS"
        N_ICON=media-optical
elif [[ $DEVTYPE == disk && $ID_BUS == usb ]]; then
        N_TITLE="USB Storage Device $N_ACTION"
        if [ $ID_DRIVE_FLASH_SD == 1 ]; then
                N_ICON=gnome-dev-media-sdmmc
        else
                N_ICON=drive-removable-media
        fi
elif [[ $DEVTYPE == partition && $ID_FS_USAGE == filesystem ]]; then
        N_TITLE="Disk partition $N_ACTION"
        N_ICON=drive-removable-media
fi

# Device name from udev
if [ -n "$ID_VENDOR_FROM_DATABASE" ]; then
        DEV_NAME=$ID_VENDOR_FROM_DATABASE
fi
if [ -n "$ID_MODEL_FROM_DATABASE" ]; then
                if [ -n "$DEV_INFO" ]; then
                        DEV_INFO="$DEV_INFO $ID_MODEL_FROM_DATABASE"
                else
                        DEV_INFO="$ID_MODEL_FROM_DATABASE"
                fi
fi
if [ -z "$DEV_INFO" ]; then
        if [[ -n "$ID_MODEL_ENC" && -n "$ID_VENDOR_ENC" ]]; then
                DEV_INFO=`echo -e "$ID_VENDOR_ENC $ID_MODEL_ENC" | awk '{printf "%s\n", $_}'`
        fi
fi

# Get additional information in case of filesystem
if [ $ID_FS_USAGE == filesystem ]; then
        DEV_INFO=`lsblk -nrf -o LABEL,MODEL,KNAME,FSTYPE,SIZE $DEVNAME`
        if [ $ACTION == add ]; then
                N_ACTIONS="Mount:pmount ${DEVNAME} `lsblk -nrf -o LABEL $DEVNAME`"
        fi
        #N_SUMMARY="Test <a href=\"test\">TEST</a>"

fi

# Send notification
if [ -n $N_ACTIONS ]; then
        sw-notify-send -i "$N_ICON" -a "$N_ACTIONS" "${N_TITLE}" "$DEV_INFO"
else
        sw-notify-send -i "$N_ICON" "${N_TITLE}" "$DEV_INFO"
fi


P.S. I'm really missing gnome-mount-applet for gnome2 panel. Is anybody knows something similar for LXDE/Openbox. I know there's one for XFCE, but XFCE isn't something I really like...
Back to top
View user's profile Send private message
salahx
Guru
Guru


Joined: 12 Mar 2005
Posts: 438

PostPosted: Wed May 09, 2012 10:09 pm    Post subject: Reply with quote

For notification purposes the udev rules files should be left alone; udev rules should be restricted to device initialization (perms, symlinks, firmware), and destruction. udev re-emits the add/change/create event on different netlink socket after udev processes it. Unfortunately, udev doesn't include a built-in notification config file (and thus another daemon is unavoidable).

Especially for this purpose, however, udev isn't a good idea - there are devices that have flakey add/remove/change notification. Use udisks instead (either udisksctl or the udisks d-bus interface) - it'll take care of polling flakey devices and faking the event for you. There a udisks-glue program out there that may be what you need....
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Desktop Environments All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum