Udev and photo flash cards in Kubuntu 12.04

From Jens' Hjørne
Revision as of 17:36, 10 September 2013 by Jens (talk | contribs)
Jump to navigation Jump to search

Under Kubuntu 12.04 (Ubuntu 12.04 with KDE as the default windows manager) you can set up which external drives should be automatically mounted. I thought this was great, since I could then set up my 4 flash memory cards for my camera, to be automatically mounted when they were connected. I thought that the system can only handle one external drive per label, and since my camera formats the flash memory card with a specific label (EOS_DIGITAL) I had 4 cards with the same label. So every time I would configure a new card to be automounted, the system would forget that I also wanted the other cards automounted.

At the same time I had written an app to automatically copy all photos on the card to a directory on the hard disk, set the EXIF owner data in each photo, rotate according to the EXIF orientation data, rename the files according to my liking and resetting each file's timestamp to the date in the EXIF data. Since I'm lazy I wanted this program to start automagically when one of the camera's flash memory cards were mounted.

Automatically mounting the flash card

I did some research into how to automount different drives with the same label. And nowhere could I find a refrrence to this no working, so I started testing this. It turned out that you should avoid configuring this too much, then it will work. :-)

In System Settings, under Hardware->Removable Devices, you must make sure that Enable automatic mounting of removable media and Automatically mount removable media when attached are both enabled. What you should not do is to check the Automount on Login and Automount on Attach by each drive. That last part is what made it not work on my laptop.

Automagically running a GUI program at mount

I have to admit that I never found out how to automagically run a program when mounting a drive. But I did find out how to run a program when attaching a disk. In this case this will work just as well.

Starting a program when attaching a device is quite easy using udev.

First you create a new rules file in /etc/udev/rules.d/. The file should have a number lower than the HAL rules file. In my case that file was 90-hal.rules, so I named the new file 89-local-rules.rules. Yes, I know. It's redundant! :-)

This file should contain one line:

ENV{ID_FS_LABEL}=="EOS_DIGITAL", ACTION=="add", SUBSYSTEMS=="usb", SYMLINK+="canoncard", RUN+="/usr/local/bin/run-jbpics-gui.sh"

This line checks for adding a USB device with the label EOS_DIGITAL. When this happens it adds a symlink to the device in /dev/ called canoncard, this would make the symlink /dev/canoncard. Then it runs the script /usr/local/bin/run-jbpics-gui.sh.

If life was easy I would then let the script start the program, and that would be it. But unfortunately this would create 2 problems:

  1. The program would be run as root.
  2. The program would be run without an X Windows context.

The first problem is easy to solve, just run the program like this:

su MYUSERNAME -c '/home/jbang/bin/jb-photo-fetch &'

Since this program is run by root it won't ask for the user's password.

The second problem took a bit of googling to solve. The script ended up looking like this:

#!/bin/bash
set -x
xhost local:jbang
export DISPLAY=:0.0
su MYUSERNAME -c '/usr/local/bin/jb-photo-fetch &'