Udev and photo flash cards in Kubuntu 12.04
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 reference to this not working out of the box, so I started testing why it didn't work for me. 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! :-)
I found a question on superuser.com that showed me how to test for the label on the connected device:
ACTION=="add", SUBSYSTEMS=="usb", ENV{ID_FS_LABEL}=="EOS_DIGITAL", SYMLINK+="canoncard", RUN+="/usr/local/bin/run-jbpics-gui.sh"
In this line ACTION=="add" makes sure the rule is only run when a device is added, while SUBSYSTEMS=="usb" makes sure it's a USB device and ENV{ID_FS_LABEL}=="EOS_DIGITAL" checks the label of the device. The command SYMLINK+="canoncard" adds a symlink (/dev/canoncard) to the original device in /dev/. This isn't really necessary, but it makes it very easy to check if the rule is run when it should be run. The last command, RUN+="/usr/local/bin/run-jbpics-gui.sh", 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:
- The program would be run as root.
- The program would be run without an X Windows context.
The first problem is easy to solve, just run the program using su:
su MYUSERNAME -c '/usr/local/bin/jb-photo-fetch &'
Since this command is run by root it won't ask for the user's password.
The second problem took a bit of googling to solve. Based on a thread on ubuntuforums.org 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 &'
What if I want to start another GUI program from within the first?
I wanted to expand my program to start a photo viewer, in the destination directory the files are copied to, depending on if the user want the viewer to be started or not. But the destination directory isn't created until the first program performs the transfer. This wouldn't be a problem, if I could start the photo viewer from within my program, but I can't, since that would make it impossible to close my program, until the photo viewer closes.
Why not just let the script wait for the program toend, and then start the photo viewer based on info from the program? Because by letting a program started by udev run for a long time, you run the risk of blocking other udev events. This was the reason for the ampersand (&) at the end of the last line in the script that started my GUI program.
To be able to let the wrapper script wait for the GUI program to end, I need to make udev execution independent of how long the wrapper script runs. The easiest way to accomplish this is to let the wrapper script be started in the background by another wrapper script. That means I end up with 2 scripts, a GUI program and an auto generated script to start the photo viewer:
- Outer wrapper script (transferphotos.sh)
- Inner wrapper script (run-jb-photo-fetch.sh)
- GUI program (jb-photo-fetch)
- The script that starts the photo viewer, generated by the GUI program (~/.jbpicutils/run-viewer.sh)
- Inner wrapper script (run-jb-photo-fetch.sh)
I created a rules file (89-local-rules.rules ) in /etc/udev/rules.d:
ACTION=="add", SUBSYSTEMS=="usb", ENV{ID_FS_LABEL}=="EOS_DIGITAL", SYMLINK+="canoncard", RUN+="/usr/local/bin/transferphotos.sh"
This will start the outer wrapper script, transferphotos.sh:
#!/bin/bash /usr/local/bin/run-jb-photo-fetch.sh &
This script ensures that the process gets started, and that udev won't end up waiting for the process to end. The actual work is done in the script named run-jb-photo-fetch.sh:
#!/bin/bash
set -x
xhost local:USERNAME
export DISPLAY=:0.0
su USERNAME -c '/usr/local/bin/jb-photo-fetch'
if [ -f /home/USERNAME/.jbpicutils/run-viewer.sh ]
then
set -x
xhost local:USERNAME
export DISPLAY=:0.0
su USERNAME -c '/home/USERNAME/.jbpicutils/run-viewer.sh &'
su USERNAME -c 'rm -f /home/USERNAME/.jbpicutils/run-viewer.sh'
fi