This is an old revision of the document!


dvd_drive_status

This program is a successor to the small program trayopen (which also inspired this one). trayopen works great, but it doesn't poll two things: if the drive is busy or if it has media. dvd_drive_status adds that feature plus a few more.

Context

The reason this program was written, was because I was having problems having programs access the DVD drive before it is ready. Duplication is simple.

  • Open your DVD tray
  • Put a DVD in (yes I realize these steps are stupid, bear with me)
  • Leave the tray open, and run a command that accesses the drive, such as lsdvd
  • The DVD tray will close
  • lsdvd (or whatever program) will try to access the disc while the drive is still not ready
  • get frustrated and have to re-run your program once the drive is ready

The problem here is that the OS will close the tray, and pass off control of the device back to the user, while the device is still in a non-ready status. So, using the above example, even closing it with eject, it will still complain, because eject finishes before the drive is ready:

$ eject -t /dev/dvd
$ lsdvd /dev/dvd

To fix this, we need to properly check if a drive is ready to be accessed or not. Just being “closed” doesn't mean it is. I wanted to solve that problem, so this is what I came up with.

Drive and Tray States

For reference, here are the possible states that a drive / tray can have. Or, at least, can be determined programatically:

  • tray is open
  • tray is opening / closing
  • tray is closed, no media
  • tray is closed, has media

In every state, the DVD drive is ready to send commands to it, and not have your program puke, except for if it is opening or closing.

I should clarify. When I say “opening or closing” I don't mean just the physical state of the tray, this also includes the time that either the OS or the drive has to poll the tray and see what's going on. If your drive has an LED on front, you'll see it lighting up after you close the tray.

It's that state where the drive is not ready to be accessed. And when the drive's not ready to access, then automated programs will complain because it thinks there's not a DVD in the drive – even if there is. Basically, you can't effectively know if something is in a proper state.

Checking Drive / Tray Status

Anyway, inspired by trayopen and having a desire to learn C myself, I came up with this little program, dvd_drive_status. And by little, I mean that. ALL it does is just query the state of the DVD drive using the Linux cdrom.h.

A nice bonus from the CD driver is that it lets you see if there is media in the tray or not, while the tray is closed. So again, this is very helpful for scripts that want to automate querying the status of your DVD drive.


Navigation