You are not logged in.

#1 2009-01-03 22:35:21

gregn
Member
Registered: 2008-12-31
Posts: 5

HAL Storage Devices Mount Script (dmenu)

Recently I switched to xmonad as my window manager of choice, which by the way is incredible.  Anyhow, I was looking to quickly mount one of my labeled NTFS partitions (I haven't gotten used to everything being /dev/sd[a-z][0-9] yet) so I opened Nautilus.  Sure enough, I couldn't use Nautilus's HAL mounting features without running even more GNOME software.  So I looked around for some more options and realized the only other thing even close was thunar-volman.

Anyway, I figured that I shouldn't have to run software that I don't want/need to be able to use HAL (and yes, I know I could just put them all in /etc/fstab and forget about it) so I figured I'd write a little script to take care of it.  There is a package called Ivman that acts as sort of a daemon to automatically mount hotplugged storage devices.  The SuSE version of it comes with a Python script called "halmount.py".  I modified that script slightly to give another command line option: --ntfs-as-ntfs-3g.  Then, since I used xmonad, I wrote a shell script to use it to grab all mountable or mounted (depending on the task at hand -- mounting or unmounting) volumes & pipe them to dmenu and...well anyway, look at the scripts if you want more info:

#!/bin/bash
# dmenu_hal
# GPLv3, Gregory Newman <greg@nerdherdproductions.com>
#
# Used in conjunction with dmenu to bring up a list of mountable storage devices
# detected by the HAL from which the user can choose to mount or unmount any or
# all of said devices.
# 
# Best used in conjunction with a tiling window manager like xmonad or awesome3
#

# CHANGE THIS LINE TO THE LOCATION OF THE SCRIPTS IF YOU DO NOT PUT THEM IN YOUR
# PATH SOMEWHERE
cd ~/.xmonad

case $1 in
    mounted)
        echo "*ALL*">.halmount.tmp
        ./halmount.py|grep " on "|awk '{split($0,arr1," on ");split(arr1[2],arr2," type ");print arr1[1]" ["arr2[1]"] ["arr2[2]"]";}'>>.halmount.tmp 
        cat .halmount.tmp|dmenu|xargs ./dmenu_hal unmount 2>&1| tr -dc ._a-zA-Z0-9:/"\n"" "|xargs -0 notify-send "dmenu HAL Unmounted:"
        ;;
    unmounted)
        echo "*ALL*">.halmount.tmp
        ./halmount.py|grep " \-> "|awk '{split($0,arr1," -> ");split(arr1[2],arr2," type ");print arr1[1]" ["arr2[1]"] ["arr2[2]"]";}'>>.halmount.tmp
        cat .halmount.tmp|dmenu|xargs ./dmenu_hal mount 2>&1| tr -dc ._a-zA-Z0-9:/"\n"" "|xargs -0 notify-send "dmenu HAL Mounted:"
        #./dmenu_hal mount `cat .halmount.tmp`
        ;;
    mount)
        if [ "$2" = "*ALL*" ]; then
#           ./halmount.py -a
            ./halmount.py -a --ntfs-as-ntfs-3g
        else
            ./halmount.py --ntfs-as-ntfs-3g $2
#           ./halmount.py $2
        fi
        ;;
    unmount)
        if [ "$2" = "*ALL*" ]; then
            ./halmount.py -ua
        else
            ./halmount.py -u $2
        fi
        ;;
    *)
        echo "Usage:"
        echo "dmenu_hal <command>"
        echo ""
        echo "Commands:"
        echo "      mounted     - Show all currently mounted volumes & unmount when selected."
        echo "      unmounted   - Show all currently unmounted & available volumes & mount when selected."
        ;;
    esac
exit 0

You can also download the script, along with halmount.py at the following links:

dmenu_hal - http://etius.org/files/dmenu_hal
halmount.py - http://etius.org/files/halmount.py
dmenu_hal.tgz - http://etius.org/files/dmenu_hal.tgz (both files)

Make sure you set both files +x.  Since I use xmonad, I have it in my ~/.xmonad directory - you can put it wherever you want, but if the location is not in your $PATH, change the first line of the shell script (THE ONE WITH THE CAPSLOCK COMMENT ABOVE IT) to cd to the location of the files.  In order to incorporate it into xmonad, I simply used the following lines:

    , ((modMask,               xK_i     ), spawn "~/.xmonad/dmenu_hal unmounted")
    , ((modMask,               xK_u     ), spawn "~/.xmonad/dmenu_hal mounted")

Just put that in your keybinds list and you're all set!  If you use anything but xmonad, sorry, but you're on your own.
It may complain about the 'uid=' option that it sets when you mount all...you have two options, either run it as sudo with the NOPASSWD setting (man sudoers) or hit the hotkey again and mount it individually.

Anyhow, you'll also need Python (obviously) and dbus-python installed for the halmount.py script to run.  I don't know if anyone will get any use out of this, but here it is.  It can definitely use some improvements...if two volumes have the same label, it won't mount the second one so make sure your volumes are either labeled differently or not labeled at all.  So yeah, it's not polished...but it works for me.  So...enjoy...?

Greg Newman
mjuad on #archlinux

Offline

#2 2009-01-03 23:00:41

ijanos
Member
From: Budapest, Hungary
Registered: 2008-03-30
Posts: 443

Re: HAL Storage Devices Mount Script (dmenu)

Cooool! Thanks a lot! I was started to implement this in python to use for an openbox pipe menu, but never finished it.
Mine only lists hotpluggable devices, maybe i will start hacking this one but first i have some exams to pass.

Offline

#3 2009-01-03 23:37:42

gregn
Member
Registered: 2008-12-31
Posts: 5

Re: HAL Storage Devices Mount Script (dmenu)

I think I posted this in the wrong section...would it be better in the Desktop Environments section?

Offline

#4 2009-01-04 00:14:20

Zariel
Member
Registered: 2008-10-07
Posts: 446

Re: HAL Storage Devices Mount Script (dmenu)

thats pretty awesome, could you make it toggle them?

Offline

Board footer

Powered by FluxBB