LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-18-2012, 01:21 PM   #1
GamezR2EZ
Member
 
Registered: Jan 2009
Posts: 31

Rep: Reputation: 4
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);
	}
}

Last edited by GamezR2EZ; 02-18-2012 at 11:29 PM.
 
Old 02-18-2012, 07:41 PM   #2
liberalchrist
Member
 
Registered: Jun 2011
Location: Chester, SC
Distribution: Slackware Current
Posts: 142

Rep: Reputation: 33
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.
 
Old 02-18-2012, 08:25 PM   #3
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
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...
 
Old 02-18-2012, 08:37 PM   #4
GamezR2EZ
Member
 
Registered: Jan 2009
Posts: 31

Original Poster
Rep: Reputation: 4
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.
 
Old 02-18-2012, 08:56 PM   #5
liberalchrist
Member
 
Registered: Jun 2011
Location: Chester, SC
Distribution: Slackware Current
Posts: 142

Rep: Reputation: 33
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 &"
 
Old 02-18-2012, 09:21 PM   #6
GamezR2EZ
Member
 
Registered: Jan 2009
Posts: 31

Original Poster
Rep: Reputation: 4
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.
 
Old 02-18-2012, 09:51 PM   #7
liberalchrist
Member
 
Registered: Jun 2011
Location: Chester, SC
Distribution: Slackware Current
Posts: 142

Rep: Reputation: 33
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.
 
Old 02-18-2012, 10:02 PM   #8
GamezR2EZ
Member
 
Registered: Jan 2009
Posts: 31

Original Poster
Rep: Reputation: 4
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.

Last edited by GamezR2EZ; 02-18-2012 at 11:25 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
dvd drive media not detecting in fedora 10 on my acer aspire5738z laptop kiran_y2 Linux - Hardware 3 09-21-2011 04:38 AM
[SOLVED] UDev rules and detecting powered up SATA HDD in 5.25 enclosure lpallard Linux - Hardware 4 01-08-2011 06:47 PM
[SOLVED] Using UDEV to automatically run a script on optical disc mount? atavus Linux - Software 8 03-04-2010 09:48 PM
change filesystem and OS on a vaio without optical drive Rupertt Linux - Laptop and Netbook 4 02-24-2008 09:50 AM
Detecting the CD drive (or other hardware) without udev? synx Linux - Kernel 1 03-16-2007 10:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration