[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

detecting CDs (was Re: Bug#123948: woody CD not passing 'cdrom' argument)



On Dec 18, Adam Di Carlo wrote:
> Thinking about it more, I don't think there's any real solution other
> than trying to actually sense if there's a CD in the drive and it's an
> official CD.  *sigh*.  Reassigning to boot-floppies.

Well, figuring out if it's a Debian CD should be fairly easy; just
check to see if there is a .disk/info file on the CD-ROM.

Here's some code (untested); MPT is the mount point, DEV is the device
to be tested.

fd = open(DEV, O_NONBLOCK | O_RDONLY);
res = ioctl(fd, CDROM_DISC_STATUS);
if(res < 0 || res==CDS_NO_INFO || res==CDS_NO_DISC || res==CDS_AUDIO)
{
  close(fd);
  return not_a_debian_cd;
}
close(fd);
if(!mount(DEV, MPT, "iso9660", MS_RDONLY | MS_NOSUID | MS_NODEV | 0xC0ED, NULL))
{
  struct stat sbuf;

  if(!stat(MPT "/.disk/info", &sbuf) & S_ISREG(sbuf.st_mode))
    return is_a_debian_cd;
  else
    umount(DEV);
}
return not_a_debian_cd;

Again, I haven't done this in C; howver, a bit of Python that does it
is attached, and it seems to work.

The only slow case seems to be a newly inserted CD.


Chris
-- 
Chris Lawrence <lawrencc@debian.org> - http://www.lordsutch.com/chris/
#!/usr/bin/python

import os, commands
from fcntl import ioctl

# from CDROM import CDROM_DISC_STATUS
CDROM_DISC_STATUS = 0x5327

CDS_NO_INFO, CDS_NO_DISC, CDS_AUDIO = (0, 1, 100)
MS_RDONLY, MS_NOSUID, MS_NODEV = (1, 2, 4)

CDROM = '/dev/cdrom'
MPT = '/cdrom'

fd = os.open(CDROM, os.O_RDONLY | os.O_NONBLOCK)
res = ioctl(fd, CDROM_DISC_STATUS)
if res < 0 or res in (CDS_NO_INFO, CDS_NO_DISC, CDS_AUDIO):
    print 'No CD available (%d)' % res
else:
    err = os.system('mount -t iso9660 -o ro,nosuid,nodev %s %s' % (CDROM, MPT))
    if not err:
        if os.path.exists(MPT + '/.disk/info'):
            print "Data CD in drive; appears to be Debian."
            os.system('cat '+MPT+'/.disk/info')
        else:
            print "Data CD in drive; probably not Debian."
    else:
        print "Weird... a data CD is in the drive, but we can't mount it."
    os.system('umount '+MPT)
    
os.close(fd)

Reply to: