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:
#!/bin/bash
#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" == "" ]
then
false
echo "No CD detected" >> /etc/udev/rules.d/DetermineMediaType.log
else
#Check whether CD is a media CD
if [ $ID_CDROM_MEDIA_CD == "1" ]
then
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"

fi
fi

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


Summary
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

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

7 comments:

  1. Hi, thanks for your description!

    For detection, why not use the udev detection ?

    Regards
    Markus

    -->

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

    ReplyDelete
  2. This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion, thank you. cd duplication services

    ReplyDelete
  3. You can receive notifications of every Tenders information issued by multi-funding organisations and Ireland government agencies, and you can search for the best Business relevant Ireland tender for your company.Tenders and contractors bidding information service providers in a global tenders and bidding market by staying up to date with all tenders from Ireland data provided by Ireland government agencies and commercial enterprises for e-Procurement Businesses. BidDetail has a list of Ireland Government Tender.

    ReplyDelete
  4. I really appreciate your help. Thank you.
    BLOG

    ReplyDelete

Note: only a member of this blog may post a comment.