« Bug bitten ankle day 2, the horror continues | Climate change bites back? » |
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
<?xml version="1.0" encoding="ISO-8859-1"?> | |
<deviceinfo version="0.2"> | |
<device> | |
<match key="block.device" string="/dev/hdc"> | |
<append key="info.callouts.add" type="strlist">dvdinsert</append> | |
</match> | |
</device> | |
</deviceinfo> |
Next I created a shell script in /usr/libexec called dvdinsert
Code
#!/bin/sh | |
echo $HALD_ACTION >> /var/log/messages | |
case $HALD_ACTION in | |
add) | |
sleep 1 # May be unnecessary, experiment | |
echo dvdinsert DVD inserted >> /var/log/messages | |
/usr/libexec/dvdinsert1 "$HAL_PROP_VOLUME_LABEL" & | |
sleep 1 | |
;; | |
remove) | |
echo DVD removed >> /var/log/messages | |
;; | |
esac |
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.
Code
#!/bin/sh | |
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 | |
/usr/bin/eject | |
echo "eject complete" >> /var/log/messages |
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.
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.
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