Auto-run the good way

Well, I've finally gotten around to setting up some autorun functionality on my new Kubuntu desktop. If you follow this page regularly, you may recall that I did this with Slackware not too long ago, using a CD monitoring program and a udev script. However, now that I'm using Kubuntu, I can do it the right way.

Both D-BUS and HAL are part of a standard Ubuntu installation. I'll spare you the gory details, but the short version is that D-BUS is a libary/protocol for communicating between programs and HAL is a package that builds on this to create a standard format for listing hardware attached to the system. So, to make a long story short, you can run D-BUS and HAL daemons keep an updates list of what hardware are attached to the system (including individual CDs) and run another program to check when the list is changed. In GNOME, this is the GNOME Volume Manager, which prompts you for the desired action when it detects a new kind of media.

However, KDE doesn't have a volume manager. All it has is the media:/ IOSlave, which, to be perfectly frank, kinda sucks. It is totally non-configurable and pretty much just shows you a list of detected volumes and lets you open them. It's a little like "My Computer" in Windows, except less useful because it also shows you the mounted volumes that you don't care about. Apparently there's a proper volume manager coming in KDE 3.5, but that doesn't help me now.

My solution was to install ivman, which is a simple, a desktop-independent volume manager. It has no GUI, but is configured through several XML files, which it reads ever time a HAL event is detected. The XML files contain (possibly nested) rules that match HAL events, which allow you to execute an action only when the new device matches a specific set of events.

For my own system, I uncommented a couple of the example rules (for playing CDs and DVDs) and added one of my own. It looks like this:
<ivm:Match name="ivm.mountable" value="true">
<ivm:Option name="exec" value="$HOME/.ivman/autofsrun.sh '$hal.info.udi$'" />
</ivm:Match>

This rule matches pretty much anything that can be mounted and passes its HAL UDI to the autofsrun.sh script. This script, which you can download here in its initial form, uses the hal-get-property and udevinfo commands to retreive the name of the device and any symlinks to it. It then uses this information to search for the device in any sctive autofs map files. If the device is found, it gets the full path to the mount point and opens it in the file manager.

I plan to update this script a bit more. In particular, I'd like to add support for autofs program maps as well as a facility to automatically add desktop shortcuts to the autofs mount points. When and if I get around to that, I'll post it as a permanent article.

You can reply to this entry by leaving a comment below. This entry accepts Pingbacks from other blogs.

Add your comments #

A comment body is required. No HTML code allowed. URLs starting with http:// or ftp:// will be automatically converted to hyperlinks.