« Bug bitten ankle day 2, the horror continuesClimate change bites back? »

Running a shell script when a DVD is inserted using HAL


  06:06:53 pm by wdawe, Categories: centos, linux

Now that hard drives are so cheap I decided to copy a whole stack of archival DVD's onto a hard drive. I wanted to use a shell script because the copy was running on a machine that didn't have a monitor or keyboard attached. The idea was to feed it DVD's every time the drawer popped open until the stack was done.

Step 1 was to add a HAL callout to run a script whenever the DVD was inserted. I create a file called 31-dvdinsert.fdi in /usr/share/hal/fdi/information/20thirdparty/ which is where these things go in Centos or Redhat 5 systems. The name isn't that important but the extension .fdi is. Forget the .fdi and it won't work. On this particular system the DVD drive resides at /dev/hdc, using ls -l /dev/cdrom will show you where it is on your system. The contents of the file were:


<?xml version="1.0" encoding="ISO-8859-1"?>
<deviceinfo version="0.2">
  <match key="block.device" string="/dev/hdc">
                <append key="info.callouts.add" type="strlist">dvdinsert</append>

Next I created a shell script in /usr/libexec called dvdinsert


echo $HALD_ACTION >> /var/log/messages
case $HALD_ACTION in
    sleep 1  # May be unnecessary, experiment
    echo  dvdinsert DVD inserted >> /var/log/messages
    /usr/libexec/dvdinsert1 "$HAL_PROP_VOLUME_LABEL" &
    sleep 1
    echo DVD removed >> /var/log/messages

The purpose of this script is to call another script to do the actual copying. HAL callouts can only run for a very short time before they are terminated. This script passes the Volume name of the DVD inserted to dvdinsert1 which is run as a detached process. THe contents of dvdinsert1 are shown below.


    echo  dvdinsert1 DVD inserted >> /var/log/messages
    echo $1 >> /var/log/messages
    mount /dev/hdc /media/dvd
    echo "mount complete" >> /var/log/messages
    cp -a /media/dvd "/mnt/backup/$1"
    echo "copy complete" >> /var/log/messages
    sleep 1
    umount /media/dvd
    echo "umount complete" >> /var/log/messages
    echo "eject complete" >> /var/log/messages

This script does the actual copying of the files to a subdirectory found that it creates in /mnt/backup where an external usb drive is mounted. It also puts some messages in /var/log/messages so that you can keep track of the copy progress. You will need to create the /media/dvd mountpoint so the DVD can be mounted by the script. If you know a better way to do this feel free to leave a comment.


Comment from: Anthony [Visitor]

One really shouldn’t echo stuff straight into /var/log/messages; use logger(1) to talk to syslogd and get your messages properly timestamped and all.

Some means of dealing with volume ID collisions could be fun too, especially as they might be the same disk again, might be a second copy of the same volume (with errors in different places, maybe), or might be a different one entirely.

06/25/08 @ 09:34
Comment from: wdawe [Member]  

The reason for echoing to /var/log/messages is to aid in script troubleshooting. Of course anyone who find this script useful is encouraged to improve upon it and post the results for others to improve upon.

06/29/08 @ 14:32
Comment from: P [Visitor]

Thanks for the good howto!

I couldn’t get this to work, but then I found the problem. In debian, /usr/libexec doesn’t exist, so I put my dvdinsert script into /usr/local/bin. But that path is not visible to hal by default[1].

The solution was to put a symlink in /usr/lib/hal/scripts dir, which points to my dvdinsert script in /usr/local/bin.

Hope this helps someone.

/ P?r

[1] http://people.freedesktop.org/~david/hal-spec/hal-spec.html#device-properties-info-callouts

06/05/09 @ 15:30
Comment from: CloneDVD [Visitor]

Thank you for the good tutorial.

12/02/10 @ 05:02
Cool web tools, EEPC tips and Linux info. Browse around, I'm sure you will find something to interest you.


  XML Feeds

powered by b2evolution