Reworking AutoFS and media:/

This week I decided to re-evaluate my approach to removable media. For a while, I had been using a combination of my old Slackware automount config with ivman for autorun functionality. This worked well enough, but it wasn't terribly satisfying.

The behavior I really wanted was almost what Kubuntu Breezy uses by default, i.e. when you insert a new removable device (including CDs and USB drives), an icon is added to your desktop and the device is opened in a file manager window. The only problem with this is that Kubuntu uses the old "static" mounting approach, as opposed to automount. This, of course, introduces the limitation that you have to either manually unmount a device before removing it, or just pull it out while it's still mounted and hope for the best. I don't like that.

My personal feeling is that removable devices should be more or less like they are in Windows, i.e. the user shouldn't know or care whether a device is mounted. A device should be transparently mounted when it is accessed and unmounted when it is not longer in use. I don't want to have to "safely remove" a USB drive that doesn't need to be safely removed. Likewise, I don't want right-click on a CD drive icon to unmount and eject the disk when I can just push a button on the drive. And, quite frankly, I'm sick of UNIX desktops treating this like a perfectly normal and natural thing to do.

So, on to my solution to this problem. The main problem was getting some good dynamic desktop icons. The ones built into KDE are good, but they operate with the media:/ IO-slave, which assumes static mounting. I briefly tried writing a script to dynamically create desktop files, but quickly realized that I didn't have enough time or interest to write anything as full-featured as what KDE already has. So, clearly, the best way to proceed was to get KDE to use my autofs mount points instead of media:/ URIs.

This part actually proved much easier than I expected. It turns out that KDE has MIME types for removable media, such as media/removable_mounted and media/dvd_unmounted. Like any other MIME type, you can change the file associations for these. So all I needed to do was come up with a simple script script to convert the media:/URI that gets passed to the file handler program into an autofs mount point.

The only problem here was that my old automount configuration used static mount points like /var/autofs/optical/cdrw. The URIs my script gets as parameters, however, look like media:/hdc. I could just implement a simple look-up table, or do some trickery with udevinfo or symlinks in /dev, but I wanted something more natural and extensible. So how could I make that conversion without doing something overly complex or ad hoc?

The answer is: I couldn't. However, I could adjust my automount configuration to make writing the script trivial. I do this by taking advantage of the wildcard feature of automount. Basically, automount allows you to have entries in your map files that use "*" as the key and "&" as a substitution character for that key.

This allows me to dramatically simplify my map files. For instance, the /etc/auto.removable file, which defines automount points for my USB storage devices, used to have 4 entries - one for each USB storage device I own. The new one has a single entry:
* -fstype=auto,sync,user,umask=0000 :/dev/&
This simply matches a key name to a device node. So, e.g., when I access /var/autofs/removable/hdc, automount will mount /dev/hdv on that directory.

So now I can much more easily match a media:/URI to an automount point. Now, media:/sda1 will be mounted on /var/autofs/removable/sda1 and, thanks to a similar map file for CD and DVD drives, media:/hdc will be mounted on /var/autofs/optical/hdc.

Given this, my handler script for media MIME types becomes clear.
#!/bin/sh
Branch=`echo "$2" | sed 's/media:\/*//'`
kfmclient openURL /var/autofs/$1/$Branch

All it needs is two parameters - the autofs map to use and the media:/ URi. I simply hard-code the appropriate map in the file association for each MIME type and I'm all set.

The only other thing to do is fix up the autorun functionality. Kubuntu Breezy uses ivman to mount newly inserted devices and then open the media:/ URi in Konqueror. Fixing this involves editing the /etc/ivman/IvmConfigActions.xml file. Luckily, this file is well commented, so fixing it is easy. Just delete or comment out the section that tries "to mount any mountable volume at all" and change the "open konqueror" rule to use kfmclient exec instead of kfmclient openURL. That will cause kfmclient to use the modified file association instead of going straight to the media IO-slave.

That pretty much does it. I've only found one problem, which is both significant and weird. After inserting a new device, when I click on the desktop icon the first time, it opens using my autofs script. However, the second time I click it, it opens with a media:/ URI instead. If I refresh the desktop, I can open the device with my script again, but only the first time. But if I use the right-click "open" menu item, it works correctly every time. It makes no sense. I think I'll be working on that for a while.

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.