Menu

USBFlash

Introduction

This example demonstrate how to use yad --notification. There is two part of example - first one is a set of udev rules for automounting usb flash drives, and second one - script which watches for creating or deleting new mount points and show notification icon with menu for unmounting flash drives

Details

Additional software:

  • udev
  • inotify-tools

Udev rules usually placed in file /etc/udev/rules.d/11-auto-mount.rules

udev rules

KERNEL!="sd[a-z]*", GOTO="auto_mount_end"
ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="auto_mount_end"

# Set environment
ACTION=="add", IMPORT{program}="/sbin/blkid -o udev -p -s TYPE -s LABEL %N"

# Global mount options
ACTION=="add", ENV{mount_options}="relatime,users,umask=0"

# Filesystem specific options
ACTION=="add", ENV{ID_FS_TYPE}=="vfat", ENV{mount_options}="%E{mount_options},showexec"
ACTION=="add", ENV{ID_FS_TYPE}=="ntfs", ENV{mount_options}="%E{mount_options},utf8"

# Get mount point
# use basename to correctly handle labels such as ../mnt/foo
ACTION=="add", ENV{ID_FS_LABEL}=="?*", PROGRAM="/usr/bin/basename '%E{ID_FS_LABEL}'", ENV{dir_name}="%c"
ACTION=="add", ENV{dir_name}!="?*", ENV{dir_name}="usbhd-%k"

# Main action
ACTION=="add", ENV{dir_name}=="?*", RUN+="/bin/mkdir -p '/mnt/usb/%E{dir_name}'", RUN+="/bin/mount -o %E{mount_options} /dev/%k '/mnt/usb/%E{dir_name}'"
ACTION=="remove", ENV{dir_name}=="?*", RUN+="/bin/umount -l '/mnt/usb/%E{dir_name}'", RUN+="/bin/rmdir '/mnt/usb/%E{dir_name}'"

LABEL="auto_mount_end"

# label must be cleared
ENV{ID_FS_LABEL}=""

notification script

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#! /bin/sh
# -*- mode: sh -*-
#
# Manage removable media
# Author: Victor Ananjevsky <ananasik@gmail.com>, 2009
#

BASEDIR=/mnt/usb
PIPE=$(mktemp -u --tmpdir ${0##*/}.XXXXXXXX)

function on_exit () {
    echo "quit" >&3
    rm -f $PIPE
}

function on_unmount () {
    gsu umount $1
    ret=$?
    if [[ $ret -eq 0 ]]; then
    notify-send -u normal -i drive-removable-media -t 900 \
        "${1##*/}" "${1##*/} unmounted successfully"
    else
    notify-send -u critical -i drive-removable-media -t 1200 \
        "${1##*/}" "Unmount ${1##*/} failed (error code $ret)!!!"
    fi
}

function update_state () {
    MENU=
    for d in $(find $BASEDIR -mindepth 1 -maxdepth 1 -type d); do
    MENU="${d##*/}!sh -c 'on_unmount $d'|$MENU" 
    done
    if [[ -z $MENU ]]; then
    echo "visible:false" >&3
    else
    echo "visible:true" >&3
    echo "menu:$MENU" >&3
    fi
}

mkfifo $PIPE
exec 3<> $PIPE

export -f on_unmount
trap on_exit EXIT

yad --notification --kill-parent --listen \
    --image=drive-removable-media --text="Removable media" \
    --command="xdg-open $BASEDIR" <&3 &

update_state

inotifywait -m -e create -e delete $BASEDIR 2> /dev/null | while read LINE; do
    case $(echo $LINE | awk '{print $2}') in
    "CREATE,ISDIR") update_state ;;
    "DELETE,ISDIR") update_state ;;
    esac
done

gsu in this script is a graphical frontend for su from examples


Discussion

  • Anonymous

    Anonymous - 2011-11-16

    Originally posted by: franco.massimiliano

    i think that:

    gsu umount $1

    should be:

    gksu umount $1

     
  • Anonymous

    Anonymous - 2011-11-17

    Originally posted by: ananasik

    gsu (graphical su) - is an another yad script (su frontend) from examples. but, of course, you may use any of your favorite su-s

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.