[clug] Is a CD in a drive..

Jeremy Kerr jk at ozlabs.org
Wed Mar 25 06:24:08 GMT 2009

Hi Andrew,

> Anyone know a magic cat  /proc/blue/blah/blonk incantation that I can
> use in a (sh) script to tell if a CDROM is in a drive?

The correct way, I believe, is to use the CDROM_DRIVE_STATUS ioctl. I'm 
not sure if there's an easy way to do this using bash, but the following 
python will illustrate the method:

 import os, fcntl, CDROM
 fd = os.open('/dev/cdrom', os.O_RDONLY | os.O_NONBLOCK)
 rc = fcntl.ioctl(fd, CDROM.CDROM_DRIVE_STATUS, 0)

Then you can check rc for CDROM.CDS_NO_DISC or CDROM.CDS_DISC_OK.

bash ioctl bindings, anyone? :)


More information about the linux mailing list