LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Using UDEV to automatically run a script on optical disc mount? (http://www.linuxquestions.org/questions/linux-software-2/using-udev-to-automatically-run-a-script-on-optical-disc-mount-792428/)

atavus 03-01-2010 04:11 PM

Using UDEV to automatically run a script on optical disc mount?
 
I'm trying to get a script to run whenever an optical disc is mounted by the system but can't get it to work. I'm running Fedora 12 64bit and the drive is SATA device sr0.

Following online guides, I created a new rule file called '10-my.rules' in '/etc/udev/rules.d' which contains the following:
Code:

KERNEL=="sr0", RUN+="/usr/local/bin/myscript"
But nothing happens when a disc is mounted. The script itself runs fine manually and is clearly evident when launched so the problem must be the rule file. I've tried changing the name of the rule file in a few incremental steps from 10-my.rule to 99-my.rule to no effect.

I've gathered some information about my optical drive using the udevadm tool.
Here is the result of an info query:
Code:

P: /devices/pci0000:00/0000:00:1f.2/host1/target1:0:0/1:0:0:0/block/sr0
N: sr0
S: block/11:0
S: scd0
S: disk/by-path/pci-0000:00:1f.2-scsi-1:0:0:0
S: disk/by-label/METROPOLIS_USA_NTSC
S: cdrom
S: dvd
E: UDEV_LOG=3
E: DEVPATH=/devices/pci0000:00/0000:00:1f.2/host1/target1:0:0/1:0:0:0/block/sr0
E: MAJOR=11
E: MINOR=0
E: DEVNAME=/dev/sr0
E: DEVTYPE=disk
E: ID_CDROM=1
E: ID_CDROM_DVD=1
E: ID_CDROM_MEDIA=1
E: ID_CDROM_MEDIA_DVD=1
E: ID_CDROM_MEDIA_STATE=complete
E: ID_CDROM_MEDIA_SESSION_COUNT=1
E: ID_CDROM_MEDIA_TRACK_COUNT=1
E: ID_CDROM_MEDIA_TRACK_COUNT_DATA=1
E: ID_VENDOR=ATAPI
E: ID_VENDOR_ENC=ATAPI\x20\x20\x20
E: ID_MODEL=iHOS104
E: ID_MODEL_ENC=iHOS104\x20\x20\x20\x20\x20\x20\x20\x20\x20
E: ID_REVISION=WL0B
E: ID_TYPE=cd
E: ID_BUS=scsi
E: ID_PATH=pci-0000:00:1f.2-scsi-1:0:0:0
E: ID_FS_LABEL=METROPOLIS_USA_NTSC
E: ID_FS_LABEL_ENC=METROPOLIS_USA_NTSC
E: ID_FS_TYPE=udf
E: ID_FS_USAGE=filesystem
E: ACL_MANAGE=1
E: GENERATED=1
E: DKD_PRESENTATION_NOPOLICY=0
E: DKD_MEDIA_AVAILABLE=1
E: DEVLINKS=/dev/block/11:0 /dev/scd0 /dev/disk/by-path/pci-0000:00:1f.2-scsi-1:0:0:0 /dev/disk/by-label/METROPOLIS_USA_NTSC /dev/cdrom /dev/dvd

Here is the result of monitoring udev while a disc is inserted:
Code:

KERNEL[1266604947.007297] change  /devices/pci0000:00/0000:00:1f.2/host1/target1:0:0/1:0:0:0 (scsi)
KERNEL[1266604947.008405] change  /devices/pci0000:00/0000:00:1f.2/host1/target1:0:0/1:0:0:0/block/sr0 (block)
UDEV  [1266604947.008441] change  /devices/pci0000:00/0000:00:1f.2/host1/target1:0:0/1:0:0:0 (scsi)
UDEV  [1266604947.663998] change  /devices/pci0000:00/0000:00:1f.2/host1/target1:0:0/1:0:0:0/block/sr0 (block)


smoker 03-01-2010 05:38 PM

Have you tried a simpler rule to see if it's the rule or the script that's a problem ?

i.e.
Code:

KERNEL=="sr0", NAME="my_optical_disk"
What does udevinfo say about the optical drive ?

atavus 03-02-2010 12:56 PM

Hi, thanks for taking time to help me out. Your suggestion of using a simpler rule, I'm afraid, isn't possible since the rule I tested...
Code:

KERNEL=="sr0", RUN+="/usr/local/bin/myscript"
...contains only a single criteria.
Code:

KERNEL=="sr0"
Also, udevinfo is a deprecated function in Fedora 12 and has been replaced by udevadm, which I did use and the results were included in my original post.

catkin 03-02-2010 01:23 PM

How do you know your script isn't being run?

Changing to 99-my.rule should ensure that it is, lexically, the last rule processed but you could try changing RUN+= to RUN= to ensure it is not subsequently changed.

You can generate (too much!) udev logging (in /var/log files) by changing udev_log in etc/udev/udev.conf to "debug". Maybe "info" will give enough information without going to "debug".

smoker 03-02-2010 01:45 PM

Quote:

Originally Posted by atavus (Post 3882930)
Hi, thanks for taking time to help me out. Your suggestion of using a simpler rule, I'm afraid, isn't possible since the rule I tested...
Code:

KERNEL=="sr0", RUN+="/usr/local/bin/myscript"
...contains only a single criteria.
Code:

KERNEL=="sr0"
Also, udevinfo is a deprecated function in Fedora 12 and has been replaced by udevadm, which I did use and the results were included in my original post.

I think you missed the point of my post.

The criteria isn't the problem, the script not running is.

Unless you can prove that any rule you supply works, then you cannot say whether the script is even being called.

If you read the udev docs you will see that any script does not run in the context of a shell, and is not going to output anything to a terminal through stdout.

If you can prove that another simpler rule works, then you can concentrate on the script.
I even supplied an example where I didn't change the device, just the action.

or not.

atavus 03-03-2010 07:47 PM

Again, thanks for the help. The script I'm trying to run is very obvious when executed - it's an automatic disc ripper that firstly pops up a dialogue box via zenity for 30 seconds giving an option to exit the script, after that it rips whatever is in the drive, ejects the disc and sets up an encoding cue.

I tried smoker's idea of testing a simple rule
Code:

KERNEL=="sr0", NAME="my_optical_disk"
which had it's intended effect so the rule is working.

I suspect this may be an issue with my script being launch by the system instead of my user account or root. I'll look into more.

catkin 03-03-2010 10:34 PM

Quote:

Originally Posted by atavus (Post 3884816)
... firstly pops up a dialogue box via zenity ...

That can't work from within the udev system; zenity needs to know which display to use and udev doesn't have one.

atavus 03-04-2010 03:37 PM

You're right, Catkin, and that was the problem. Zenity was taking down the script from the get-go since it was the first line in there. However I found a solution by adding an 'export DISPLAY' command to the code.
Code:

export DISPLAY=:0.0; zenity --question --title="Auto Disc Ripper" --text="Allow automatic disc rip?" --timeout=15
Once that was solved the UDEV command was easy; it will launch my script when a disc is mounted by the optical drive. Also, the rule file had to run last with the name "99-my.rule" in order to function.
Code:

SUBSYSTEM=="block", KERNEL=="sr0", ENV{DKD_MEDIA_AVAIABLE}=="1", RUN+="/usr/local/bin/detectdisc"
Everything is now working.

catkin 03-04-2010 09:48 PM

Quote:

Originally Posted by atavus (Post 3886032)
Code:

SUBSYSTEM=="block", KERNEL=="sr0", ENV{DKD_MEDIA_AVAIABLE}=="1", RUN+="/usr/local/bin/detectdisc

Glad you solved it :)

In case anyone wants to copy this technique, the closing double quote is missing from RUN+="/usr/local/bin/detectdisc.


All times are GMT -5. The time now is 11:05 AM.