Auto run script on disk mount E.g DVD or Bluray

I have a small script that I wanted to run on a disk being loaded/mounted by my fedora 17 system. Fortunately UDEV rules provide a pretty simple way (once you know how) to do this 🙂
First off make a trigger script. I put mine in /usr/bin/disc_trigger the contents are as follows.

# This script simply calls another script then exits properly. 
# The idea for this was found:
/usr/bin/disc_script & exit

You can see the trigger simply calls the “real” script at /usr/bin/disc_script. This script can really contain anything you like. An auto ripping script or something else but its not really important to this post. Just be sure to test them and make sure they work before you continue!

Now the UDEV rules that make the scripts run when a disk is inserted. I added my rule as 99 as they as processed in order and mine will be among the last to be checked.

vi /usr/lib/udev/rules.d/99-disc_script.rules

Contents are as follows

# Siimply executes a script when a disc is mounted. 
# Different disc types can be used as follows:
SUBSYSTEM=="block", KERNEL=="sr0", ENV{ID_CDROM_MEDIA_BD}=="1", RUN+="/usr/bin/disc_trigger"

Now we need to reload the UDEV rules using the following command:

udevadm control –reload

Now the script should be executed when a disc is loaded 😀
To help debug any issues you can execute udevadm control --log-priority=info. This will make UDEV log quite verbosely to /var/log/messages

m00nie 😀