Using halevt to automount media and make them appear on ROX desktop

With the recent addition of halevt in Gentoo’s portage it is now relatively easy to automatically mount media like USB sticks and CD/DVD discs on /media.

What I wanted to do was to emulate my previous set of configs and scripts that ivman used to create icons of automatically mounted media on ROX desktop (called pinboard). I am using ROX pinboard on top of my favorite window manager, fluxbox.

The idea is that halevt is started by the fluxbox startup config file and when a new device is attached to the computer, halevt config calls a script that creates an icon on the ROX pinboard using ROX rpc. When a device needs to be removed ROX pinboard is configured to call a special eject command that checks for a couple of things before unmounting the device and calling the script to remove the icon from ROX pinboard.
Apart from automatically mounting/unmounting of devices I have also added a nice option in the halevt config to unmount and eject the CD/DVD drive when the eject button on the device is used and of course when the CD/DVD is not in use. That emulates a bit the windows behavior that so many users have gotten used to.

Since the script used by halevt involves a lot of file reading/writing and parsing I thought it would be wise to convert my old rox.panelput bash script to perl. And I was correct, the speed difference, even for such simple tasks is more than noticeable.

The installation process. Please take notice of the user executing the commands, $ is for normal user, # is for root:
0) create /usr/local/bin/ path and put it in your shell’s PATH
# mkdir /usr/local/bin
$ echo "export PATH=$PATH:/usr/local/bin/" >> ~/.bashrc

1) install halevt
# echo "sys-apps/halevt ~x86" >> /etc/portage/package.keywords
# emerge halevt


2) create ~/.halevt/HalevtConfigActions.xml in your home directory:
File: HalevtConfigActions.xml (right click->Save-as)

3) Create /usr/bin/eject.hal. Take notice that the script uses bash and not dash!

#!/bin/bash
if [ -z $1 ]; then
    echo "Usage: eject.hal <device>"
    exit
else
  echo "$1" | egrep "^/dev"
  if [ "$?" == "0" ]; then
    #echo "Device First, find mount point"
    TOSCAN=`echo "$1" | sed -e 's/ /\\\\\\\\040/g'`
    TOUMOUNT=`grep "$TOSCAN" /proc/mounts | cut -d" " -f2`
    UDI=`hal-find-by-property --key volume.mount_point --string "$TOUMOUNT"`
    DISC=`hal-get-property --udi "$UDI" --key volume.is_disc`
    /usr/local/bin/rox.panelput.pl Remove "$TOUMOUNT"
    halevt-umount "$1"
    halevt-umount -s
    if [ "$DISC" == "true" ]; then
        /usr/bin/eject -p "$1" &>/dev/null
    fi
  else
    #echo "Mount Point First, find device"
    UDI=`hal-find-by-property --key volume.mount_point --string "$1"`
    DISC=`hal-get-property --udi "$UDI" --key volume.is_disc`
    TOSCAN=`echo "$1" | sed -e 's/ /\\\\\\\\040/g'`
    TOUMOUNT=`grep "$TOSCAN" /proc/mounts | cut -d" " -f1`
    /usr/local/bin/rox.panelput.pl Remove "$1" nowait
    halevt-umount "$1"
    halevt-umount -s
    if [ "$DISC" == "true" ]; then
        /usr/bin/eject -p "$TOUMOUNT" &>/dev/null
    fi
  fi
fi
</device>

File: eject.hal

4) Create /usr/bin/rox.panelput.pl perl script. The script is written very simplistically to be easily understandable and changable.
File: rox.panelput.pl

5) Start halevt from fluxbox startup..make sure ~/.fluxbox/startup file contains at least the following, changing YOURUSERNAME to the one appropriate:
/usr/local/bin/rox.panelput.pl Restore
killall -9 halevt; halevt -f -u YOURUSERNAME -g plugdev &
/usr/bin/rox -p pinboard

6) go to ROX options and change ‘eject command’ to use eject.hal
rox-using-eject-hal-screenshot

That should be all. Upon fluxbox restart halevt should start and when you plug in your usb the set of scripts will create an icons on ROX pinboard for you.

Example:
usb-mounted-by-halevt-and-rox.panelput.pl

References: http://forums.gentoo.org/viewtopic-t-731555-highlight-halevt.html

16 Responses to “Using halevt to automount media and make them appear on ROX desktop”

  1. June 23rd, 2009 | 18:05
    Using Mozilla Firefox Mozilla Firefox 3.5 on Gentoo Linux Gentoo Linux

    tired just to read this post 😛

    why don’t you use a decent window manager that needs just hal to be present 😀

  2. June 23rd, 2009 | 20:27
    Using Mozilla Firefox Mozilla Firefox 3.0.11 on Mac OS X Mac OS X 10

    Can you tell me a window manager (not a Desktop Environment) that can mount icons on the desktop ? There isn’t. Because no window manager manages the “desktop”. So you probably wanted to say: “why don’t you use a decent DESKTOP ENVIRONMENT that needs just hal to be present 😀 “…
    That’s like comparing oranges to cars.

    Anyway, I guess what you had in mind was the “lightweight” fork of gnome called XFCE which is built upon gnome’s libs…Riiiiight. If I were to use a DM instead of a WM I would use LXDE. XFCE is too bloated for me and it depends way too much on gnome which is currently even more bloated than KDE. Since gnome is in a very wrong path, XFCE follows it, whether it wants it or not. Unless XFCE builds it’s own set up libraries it’s just a “gnome-light”. IMHO LXDE is much more innovative than XFCE was/is.
    Well I’d be glad if you showed me a DE which only depends on HAL and can appear icons on the desktop.

    Anyway, I don’t really use fluxbox + ROX. I initially created the fluxbox + ROX combination for iloog in order for the livecd to have a “desktop with icons”. Then I added ivman + my custom scripts to the combination to have icons for automatically mounted devices. Now I changed the ivman config with halevt config and the bash version of rox.panelput with a perl one.

    This post contains the configs + the scripts in full source. The config is halevt’s default with modifications to use rox.panelput.pl instead of just halevt-mount. The script is not thaaaat big, it’s just written in a very “python” way. It’s not an oneliner either though…but it surely can become one…it’s perl 😛

    Anyway…Linux is all about the art of doing stuff your own way…If we were to have everything made for us by others, whatever bloated, we would all use …. XXXX (and XXXX is surely neither gnome nor kde nor anything linux related)

  3. June 23rd, 2009 | 21:46
    Using Mozilla Firefox Mozilla Firefox 3.5 on Gentoo Linux Gentoo Linux

    xfce has nothing to do with gnome libs. it depends only on gtk. just like lxde.

    the desktop icons are handled by xfwm (the wm) so my answer was accurate 😛

    for instance enlightenment is also a wm (a little unstable though) that can handle desktop icons and removable devices.

  4. June 23rd, 2009 | 22:35
    Using Mozilla Firefox Mozilla Firefox 3.0.11 on Mac OS X Mac OS X 10

    on http://www.xfce.org/projects/ it says:

    Desktop Manager (http://www.xfce.org/projects/xfdesktop/)
    This program sets the background image and provides a root window menu, desktop icons or minimized icons and a windows list.

    Window Manager (http://www.xfce.org/projects/xfwm4/)
    It manages the placement of application windows on the screen, provides window decorations and manages workspaces or virtual desktops.

    So, I was correct. At least for xfce. Enlightment is an exception in the window managers world. D16 did not have desktop icon support while E17 has. So enlightment was a WM only and is now a WM/DE hybrid mix. You can still have enlightment E17 as a WM only so it’s not considered a DE.

  5. June 24th, 2009 | 18:23
    Using Mozilla Firefox Mozilla Firefox 3.5 on Gentoo Linux Gentoo Linux

    using a lightweight wm has its cons and its pros. from my point of view i can sacrifice just a little more from my ram just in order to have some things working out of the box. easy access to removable devices is one of these things. 😉

  6. Antonio
    June 26th, 2009 | 00:05
    Using Kazehakase Kazehakase 0.5.4 on Debian GNU/Linux Debian GNU/Linux

    This error appears when I run halevt with configurations and files of your post:

    ______________________________________________
    minino@MiniNo:~/.halevt$ halevt
    /home/minino/.halevt/HalevtConfigActions.xml:10: parser error : xmlParseEntityDecl: entity MOUNTABLE not terminated
    <halevt:Configuration version="0.1" xmlns:halevt="http://www.environnement.ens.f
    ^
    /home/minino/.halevt/HalevtConfigActions.xml:10: parser error : internal error
    <halevt:Configuration version="0.1" xmlns:halevt="http://www.environnement.ens.f
    ^
    /home/minino/.halevt/HalevtConfigActions.xml:10: parser error : DOCTYPE improperly terminated
    <halevt:Configuration version="0.1" xmlns:halevt="http://www.environnement.ens.f
    ^
    /home/minino/.halevt/HalevtConfigActions.xml:10: parser error : Start tag expected, '<' not found
    <halevt:Configuration version="0.1" xmlns:halevt="http://www.environnement.ens.f
    ^
    parse_config.c:374 (halevt_parse_config) Document not parsed successfully
    manager.c:346 (main) Configuration file /home/minino/.halevt/HalevtConfigActions.xml parsing failed
    minino@MiniNo:~/.halevt$
    ———————————————

    I am in a Debian stable. I had to install boolstuff libs from Sid without errors of dependencies, but I don't know if it could be important here.

    Where could be the problem?

  7. Antonio
    June 27th, 2009 | 18:29
    Using Mozilla Firefox Mozilla Firefox 2.0.0.14 on Fedora Linux Fedora Linux

    I think HalevtConfigActions.xml has not a well formed xml format. It has extrange characters that halevt doen’t understand (neither firefox).

  8. Antonio
    June 27th, 2009 | 22:02
    Using Dillo Dillo 0.8.6

    Ok, I think lines that starts with

  9. Antonio
    June 27th, 2009 | 22:07
    Using Safari Safari 525.1 on Linux Linux

    Sorry, but dillo doen’t work very well here.

    lines that starts with

    And I don’t know if these lines are incompletes or cuts for some reason.

  10. Antonio
    June 27th, 2009 | 22:10
    Using Safari Safari 525.1 on Linux Linux

    Arrrgh!

    halevt:Device match…

  11. June 28th, 2009 | 10:21
    Using Mozilla Firefox Mozilla Firefox 3.0.11 on Gentoo Linux Gentoo Linux

    There were some typos in the xml. I have uploaded a new HalevtConfigActions.xml file and tested it. I think it works just fine now.

    Sorry for the delay but I was away from home the last 3-4 days.

  12. Eric
    August 1st, 2009 | 01:02
    Using Debian IceWeasel Debian IceWeasel 3.0.9 on Debian GNU/Linux Debian GNU/Linux

    The script rox.panelput.pl is not working with halevt version 0.1.3. (They have changed something in the way they keep track of the mounted devices, so the path $HOME/.halevt/mounted/devices no longer exists). I’m using instead this little script (rox.panelput.sh):

    #!/bin/sh
    rox –RPC << EOF

    $2
    `basename $2`

    EOF

    with this eject.hal (it’s just the same but with all occurrences of rox.panelput.pl changed to rox.panelput.sh) :

    #!/bin/bash
    if [ -z $1 ]; then
    echo “Usage: eject.hal ”
    exit
    else
    echo “$1” | egrep “^/dev”
    if [ “$?” == “0” ]; then
    #echo “Device First, find mount point”
    TOSCAN=`echo “$1” | sed -e ‘s/ /\\\\\\\40/g’`
    TOUMOUNT=`grep “$TOSCAN” /proc/mounts | cut -d” ” -f2`
    UDI=`hal-find-by-property –key volume.mount_point –string “$TOUMOUNT”`
    DISC=`hal-get-property –udi “$UDI” –key volume.is_disc`
    /usr/local/bin/rox.panelput.sh Remove “$TOUMOUNT”
    halevt-umount “$1”
    halevt-umount -s
    if [ “$DISC” == “true” ]; then
    /usr/bin/eject -p “$1” &>/dev/null
    fi
    else
    #echo “Mount Point First, find device”
    UDI=`hal-find-by-property –key volume.mount_point –string “$1″`
    DISC=`hal-get-property –udi “$UDI” –key volume.is_disc`
    TOSCAN=`echo “$1” | sed -e ‘s/ /\\\\\\\40/g’`
    TOUMOUNT=`grep “$TOSCAN” /proc/mounts | cut -d” ” -f1`
    /usr/local/bin/rox.panelput.sh Remove “$1” nowait
    halevt-umount “$1”
    halevt-umount -s
    if [ “$DISC” == “true” ]; then
    /usr/bin/eject -p “$TOUMOUNT” &>/dev/null
    fi
    fi
    fi

    And this HalevtConfigActions.xml:

    <!DOCTYPE halevt:Configuration [

    ]>

    <!– blank CD/DVD

    –>

    <!–
    This rule matches removable devices which have volume.policy.should_mount true
    or don't have volume.policy.should_mount false.
    Upon insertion, they are mounted and recorded by halevt-mount.
    -m 002 sets the umask of mount to 002 when pmount is used system-wide
    to mount the device. This allows regular users to read the device.

    !–>

    <!–
    When a device get a mount point, it is passed to halevt-mount which can
    record it. The property that changes is volume.is_mounted, and the action is
    executed when the value of volume.is_mounted becomes true.

    !–>

    <!–
    Example of a use of OnInit. At startup all the devices are matched and the exec
    comand is run for those that match. Here we match all the removable devices
    that aren't already mounted and mount and register them.

    !–>

    And that’s it. It works just fine for me. Tanks a lot for your work.

  13. August 1st, 2009 | 07:29
    Using Mozilla Firefox Mozilla Firefox 3.5.1 on Linux Linux

    I have only tested it with the latest halevt version (0.1.4). I’ll try and check it also on 0.1.3 and see what needs fixing…thnx for letting me know.

  14. Danielsan
    January 19th, 2010 | 01:21
    Using Mozilla Firefox Mozilla Firefox 2.0 on Linux Linux

    Hello, I’m trying to use your guide on Ubuntu 9.10 but I can not make it work.
    I’m using the binary Rox 2.10 and halevt 0.1.3-3 (and also your scripts).
    I really like Rox but I don’t have your same skills and knowledge, you could create a guide for newbie?
    Thanks in advice,
    Danielsan
    (forgive my English, I used an automatic translator)

  15. Danielsan
    January 19th, 2010 | 01:34
    Using Mozilla Firefox Mozilla Firefox 2.0 on Linux Linux

    I forgot to write that use Openbox as WM

  16. August 18th, 2011 | 22:13
    Using PHP PHP

    […] I found this blog post which helped explain a lot about the process. I did have to compile my own […]

Leave a reply