2

I found several questions/answers about how to open/eject/mount a cd rom/drive. But I am after something else: I would like to run a script whenever the cd drive either closes or opens, it doesn't matter if there is a cd inside or not, nor if it is open or closed, basically I just need to connect a kind of event handler for the »down/up« event of the cd drive's button.

philipp
  • 379

1 Answers1

1

The only solution I've found so far is to constantly poll the drive for its status with ioctl. See Is there a command that detects if the CD drawer is open or closed? It's important to close and reopen the file descriptor on each poll; otherwise, you might not be able to mount the CD or DVD inserted. YMMV, and I'm a newb, so there's probably a better way. On my RHEL 7 box with a USB DVD drive I see the drive status change to 2 (CDS_TRAY_OPEN) when the tray is opened and briefly to 3 (CDS_DRIVE_NOT_READY) when closed before changing to 1 (CDS_NO_DISC) or 4 (CDS_DISC_OK).

I had high hopes for udev, but with my drive I only saw events for adding or removing the drive or opening the tray when it contained a disc or closing the tray. I saw no event for opening an empty tray. You can try running udevadm monitor and see if you get events for opening or closing your tray. If so you can make udev rules to run your script when those events happen: https://opensource.com/article/18/11/udev All my testing was on RHEL 7, so sorry if any of it is irrelevant for Ubuntu 18.04.

nphull
  • 11