Ubuntu 12.04 run a custom command on CD / DVD insertion (EAC and audio CD)

In Ubuntu 12.04 the option to run a custom command on the insertion of removable media has disappeared (this is a very frustrating regression!). Previously I had a custom shell script that would intelligently automate the ripping of audio CD to FLAC, ALAC and MP3 using EAC. This script had become redundant due to the missing option of running custom commands on CD insertion. Here's how I now run a custom command on audio CD detection in Ubuntu 12.04...

For reference specific applications can still be called on detection of removable media, this option now sits under "System Settings > Details > Removable Media". It's not much use for this use case.

I now rely on udev for detecting changes to the state of the CD drive (this can also be extended to USB sticks, DVDs etc.). On detection of a change to the CD drive state an intermediate script is called this is used to check whether an audio CD is present, if so it calls via my traditional CD Rip script. Because EAC is a GUI app I call the script under my username and current X session.

udev is the device manager for the Linux kernel. Primarily, it manages devices in /dev it handles all user space actions when adding/removing devices. It runs as a daemon udevd. When udev detects a change to a device it processes a series of rules to decide what to do next. On Ubuntu 12.04 these rules are located at /etc/udev/rules.d the first step is to create a rule to handle the detection of changes to the CD drive.

1. Create the udev rule for changes to the CD drive state
sudo vi /etc/udev/rules.d/10-srX_change.rules

The first two digits of the rule file name denote the priority which it is run with the low the number the higher the priority. This is set to priority 10 as I have other default Ubuntu rules with priority 70 which I  wish to overrule. The rest of the rule file name is up to the user, it should be descriptive.

Add the follow content:

KERNEL=="sr[0-9]", ACTION=="change", RUN+="/etc/udev/rules.d/DetermineMediaType"

This rule basically cover any changes to /dev/sr0 to /dev/sr9. This covers my SATA CD drives. If you device has a different name change it here.

When a change is detected the udev rule will call the CallCDRip script.

2. Intermediate script for determining media state and type "DetermineMediaType"
This step I use to determine whether 1) a CD/DVD has actually been inserted and 2) whether the disk is a CD or DVD. This script builds heavily on the work from David Lutton (thanks David). This script also produces some very basic logging

Create the interim script:
sudo touch /etc/udev/rules.d/DetermineMediaType

Make it executable:
sudo chmod 755 /etc/udev/rules.d/DetermineMediaType

Edit it:
sudo vi /etc/udev/rules.d/DetermineMediaType

Add the follow content:
#This script is call by udev on cd detection to run the CDRip script under the X session of the main user

#Remove the old log from the previous run
rm DetermineMediaType.log

#Check for CD presence
if [ "$ID_CDROM_MEDIA_CD" == "" ]
echo "No CD detected" >> /etc/udev/rules.d/DetermineMediaType.log
#Check whether CD is a media CD
if [ $ID_CDROM_MEDIA_CD == "1" ]
true # This is True for a CD Audio. Data disk?
echo "CD ripping started" >> /etc/udev/rules.d/DetermineMediaType.log
#Call CD Rip script in the user x environment
set -x
xhost local:USERNAME
export DISPLAY=:0.0
#At last Run the custom CD script
su USERNAME -c "/home/USERNAME/Desktop/CDRip"


#Check for DVD presence
if [ "$ID_CDROM_MEDIA_DVD" == "" ]
        echo "No DVD detected" >> /etc/udev/rules.d/DetermineMediaType.log
#Check whether DVD is a media DVD
if [ $ID_CDROM_MEDIA_DVD == "1" ]
true # This is True for a DVD Video. Data disk?
echo "DVD detected" >> /etc/udev/rules.d/DetermineMediaType.log
exit 0

There's probably an easier and better way to do this please shout in the comments. I wish the that the original functionality that was present in 10.04 whereby you could specify a command or script to be run still existed. Sadly, this work around is a real waste of time and effort for simple functionality.

My original CD Ripping script is here http://confoundedtech.blogspot.co.uk/2011/11/ubuntu-automatically-rip-cd-to-flac-and.html

Hackaday http://hackaday.com/2009/09/18/how-to-write-udev-rules/
David Lutton http://blog.daln.org/?p=48

1 comment:

  1. Hi, thanks for your description!

    For detection, why not use the udev detection ?



    # ID_CDROM_MEDIA_BD = Bluray
    SUBSYSTEM=="block", KERNEL=="sr0", ENV{ID_CDROM_MEDIA_DVD}=="1", RUN+="/usr/..."
    SUBSYSTEM=="block", KERNEL=="sr0", ENV{ID_CDROM_MEDIA_BD}=="1", RUN+="/usr/..."