My favorites | Sign in
yad
Project Home Downloads Wiki Issues Source
Search
for
USBFlash  
Simple solution for automount removable usb media
Phase-Implementation, Phase-Design, Featured
Updated Mar 26, 2012 by ananasik

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

#! /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

Comment by franco.massimiliano, Nov 16, 2011

i think that:

gsu umount $1

should be:

gksu umount $1

Comment by project member ananasik, Nov 17, 2011

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


Sign in to add a comment
Powered by Google Project Hosting