How can I detect if CDRom is a DVD on Linux

893 Views Asked by At

I'm looking to create a function in C++ running on Linux that returns true if the CDRom media is a DVD and false if its anything else (e.g. Audio CD).

I have been using ioctl with linux/cdrom.h. I tried using the DVD_READ_STRUCT but it always returns true. Maybe I'm using it incorrectly.

dvd_struct s
if (ioctl(hDEV, DVD_READ_STRUCT, &s)) {
    return true;
}
2

There are 2 best solutions below

0
On

The official documentation is a bit more helpful. You have to specify the request type and any required inputs before calling ioctl.

// Is it a DVD?
dvd_struct ds;
ds.type = DVD_STRUCT_PHYSICAL;
ds.physical.layer_num=0;
result = ioctl(drive, DVD_READ_STRUCT, &ds);

if (result == -1) {
    perror("Probably not a DVD: ");
} else {
    printf("Layer 0: %i to %i.\n", ds.physical.layer[0].start_sector, ds.physical.layer[0].end_sector);
}

The really interesting stuff requires issuing SCSI commands like dvd+rw-tools, cdrkit, and cdrdao. Doing this is a bit painful, though, and it's not necessary if you don't need to know if the disc is recordable, rewritable, or pressed.

1
On

Look at /proc/sys/dev/cdrom/info, it contains something like this:

CD-ROM information, Id: cdrom.c 3.20 2003/12/17

drive name:         sr0
drive speed:        125
drive # of slots:   1
Can close tray:     1
Can open tray:      1
Can lock tray:      1
Can change speed:   1
Can select disk:    0
Can read multisession:  1
Can read MCN:       1
Reports media changed:  1
Can play audio:     1
Can write CD-R:     1
Can write CD-RW:    1
Can read DVD:       1
Can write DVD-R:    1
Can write DVD-RAM:  1
Can read MRW:       0
Can write MRW:      0
Can write RAM:      1

(it is updated by the kernel and available in all distros) You can use this information in addition to the ioctl's from cdrom.h. Also keep in mind that cdrom.h is an attempt to create a standard interface, it does not yet cater for all manufacturers, some still using SCSI-codes or some other proprietary schemes. So to be safe you should also check at least using the SCSI ioctl codes - do #include <scsi/... to have them available.