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

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.

#!/bin/bash
#
# This script simply calls another script then exits properly. 
#
# The idea for this was found:
# http://forums.fedoraforum.org/archive/index.php/t-242389.html
/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:
# ID_CDROM_MEDIA_BD = Bluray
# ID_CDROM_MEDIA_DVD = DVD
# ID_CDROM_MEDIA_CD = CD
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 :D
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 :D