DVD Drives

Polling a Drive: Is Open / Has Media

Polling DVD drives to see if it is closed or not is a real pain to do in Linux. (Edit: it was! Check out dvd_drive_status which solves everything here)

I highly recommend reading this blog post where I describe various ways I poll the drive to see if it is empty or not.

Also, check out trayopen, which I added to this wiki after writing that blog post.

The reason is because there are race conditions.

Consider this scenario, with this sequenced timeline:

  1. Open tray
  2. Insert media into tray
  3. Close tray
  4. Access device

Ideally, this would work perfectly fine. However, the problem happens after step #3, closing the tray. The device becomes available before it is actually ready to read from. This is particularly annoying with DVD drives because some firmware (most of them, that I've seen) for the devices will freak out if you are trying to access the data encrypted by CSS.

The best solution I've found to work around this mess? Close the DVD tray and wait 30 seconds for the device to finish polling. Seriously. Just avoid the problem by applying some patience.

You can duplicate the scenario if you like, like this:

Insert a DVD into your tray, and tell Linux to close the tray:

eject -t /dev/dvd

eject will finish and return an exit code of 0. If you physically watch your computer though, you will see the LED flashing as the disk is being polled.

Now, immediately after eject has finished, run a command to read the DVD drive contents:

lsdvd /dev/dvd

Next, run dmesg to see if anything unexpected has happened. For this, you'll need the actual device name, and not the symlink. So, /dev/dvd is going to be a symlink (most likely) to /dev/sr0.

[ 3788.293554] sr 0:0:0:0: [sr0]
[ 3788.293561] Result: hostbyte=DID_OK driverbyte=DRIVER_SENSE
[ 3788.293564] sr 0:0:0:0: [sr0]
[ 3788.293566] Sense Key : Illegal Request [current]
[ 3788.293569] sr 0:0:0:0: [sr0]
[ 3788.293576] Add. Sense: Read of scrambled sector without authentication
[ 3788.2935