LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   udev not detecting change in media for optical drive (https://www.linuxquestions.org/questions/linux-software-2/udev-not-detecting-change-in-media-for-optical-drive-930081/)

GamezR2EZ 02-18-2012 01:21 PM

udev not detecting change in media for optical drive
 
Alright, here is my situation. I have a udev rule setup to run a script every time an audio cd is inserted. This udev rule was working perfectly for 2 days. I restarted the system and now the script will not run. The udev rule is below. Please help me troubleshoot this issue as this was my first udev endeavor.

udev rule:
Code:

SUBSYSTEM=="block", KERNEL=="sr1", ENV{ID_CDROM_MEDIA_CD}=="1", RUN+="/path/to/script &"

There is an exception, when I access the drive in anyway the script kicks off. For example, if I run `dd if=/dev/sr1 of=/dev/null bs=512 count=1` the script will execute immediately. But the purpose of this is so I don't have to execute a command, otherwise I would just launch the script manually.

Suggestions?


---Update---

I am marking this problem as solved. The relevant solution is to open a handle on the drive (basically what dd or another program was doing). Below is a link to a user post tha fixed the issue for me. It is some c code that opens\closes a handle on the drive every second. This causes the drive to update with udev as expected.

https://bbs.archlinux.org/viewtopic.php?id=127880

Relevant code:
Code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/cdrom.h>

int
main(int argc, char *argv[])
{
        if (argc < 2) {
                printf("USAGE: %s DEVICE\n", argv[0]);
                return 1;
        }
        int h;
        for (;;) {
                h = open(argv[1], O_RDONLY | O_NONBLOCK);
                if (h < 0) {
                        perror("open");
                        return 1;
                }
                close(h);
                sleep(1);
        }
}


liberalchrist 02-18-2012 07:41 PM

I have set up a few udev rules, but really know very little. Two thoughts come to mind. Perhaps everything is not ready when udev executes the rule so you need to execute later. The other idea is that you might make something happen elsewhere, like with xinit or profile.d.

uhelp 02-18-2012 08:25 PM

an udev rule alone doesn't say much.
We should know at least the system running.

Does it use startd, or the old way?
Which distro?

To make life easier one should always give a detailed description...

GamezR2EZ 02-18-2012 08:37 PM

Running Debian Testing 64-bit. 3.1.0 kernel

udevadm monitor does not show a change on the device until it is accessed. Once it is accessed everything works as expected. Again this is a new issue since a reboot.

I don't know how to proceed if the udev event doesn't even register until it is accessed. I need a direction to continue troublshooting.

As a note, I have several rules for usb devices and such that all work great, it is only the optical drive that does not work currently. I have swapped the drives to no avail.

liberalchrist 02-18-2012 08:56 PM

If this is the first time you rebooted after making the rule, your drive was already accessed. Udev may be ignoring the rule on startup because of the way it is written. If you add another rule before this one that only sets up the drive, I think everything would work.

First: SUBSYSTEM=="block", KERNEL=="sr1", ENV{ID_CDROM_MEDIA_CD}=="1"

Then: SUBSYSTEM=="block", KERNEL=="sr1", ENV{ID_CDROM_MEDIA_CD}=="1", RUN+="/path/to/script &"

GamezR2EZ 02-18-2012 09:21 PM

There is no disc in the drive on startup. Appending the first string also does not help, but again the udev is not even registering a change on the device until it is accessed. Then it works as designed, and was working previously.

liberalchrist 02-18-2012 09:51 PM

I am out of my natural habitat here, but I will make one more suggestion because I would be as frustrated as you are given the same circumstances. If it were mine, I would reboot. After the machine stabilizes, run "lsmod" to see which modules loaded. Then, access the drive, but don't insert the CD. Run "lsmod" again to see what new module may have loaded. If there is a change, you may have to use one of your startup scripts to load the module at the beginning.

GamezR2EZ 02-18-2012 10:02 PM

Thank you for the useful suggestion, I will try it out. Will report back with results


UPDATE---

That did not help. Although researching a bit more I did find a solution that works. Updated first post.


All times are GMT -5. The time now is 01:20 PM.