Need to check whether the CD-ROM is loaded or not

9,723

Solution 1

lock is simply telling you whether or not the eject button on the drive (if any) is active. Usually on Linux, when an optical disc is mounted, the eject button is disabled.

To determine whether anything is mounted in the optical drive, you can check the contents of /etc/mtab and look for either the mount point (e. g. /mnt/cdrom) or the device for the optical drive (e. g. /dev/cdrom).

Solution 2

According to my testing the best way is:

blockdev --getsize64 /dev/sr0

No need for mount, it doesn't care if medium is empty, audio, ...

If you get this error:

blockdev: cannot open /dev/sr0: No medium found

it means CD-ROM is NOT loaded. If your output is a number, zero or higher (the number represents size in bytes), it means your CD-ROM is loaded.

Of course you could use head, file, cd-info, possible other tools (cat, grep, sed can be used but they are not so useful when disc/medium is not empty), but essentialy you will end up looking for error/output "No medium found" line (the exception is file tool).

As I said, the best one is blockdev (to my opinion), but if you want, you can use other approaches, like:

head -1 /dev/sr0

No terminal output - means cd-rom is loaded but disc is empty.
Error line "No medium found" - means cd-rom is not loaded.
Any other terminal output (except errors) - means cd-rom is loaded.

cd-info

The shortest one for typing. Look for "No medium found" line - if you find it no medium loaded, anything else actually means media is loaded. Plus you will get additional useful info about your cd-rom.

file -s /dev/sr0

terminal output "/dev/sr0: writable, no read permission" means no medium loaded, terminal output "/dev/sr0: empty" means medium is loaded, but empty.

Solution 3

Check for 'ID_CDROM_MEDIA' in udevadm info -q property /dev/cdrom, although this will of course only work if the drive's initialization of the freshly input CD has been finished after a short moment.

Share:
9,723

Related videos on Youtube

ramp
Author by

ramp

Updated on September 18, 2022

Comments

  • ramp
    ramp over 1 year

    I checked the file /proc/sys/dev/cdrom/info

    $ cat /proc/sys/dev/cdrom/info
    
    CD-ROM information, Id: cdrom.c 3.20 2003/12/17
    
    drive name:             sr0
    drive speed:            1
    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:           1
    Can write MRW:          1
    Can write RAM:          1
    

    I was not able to find which column should I take into consideration to confirm the CD-ROM is loaded.

    And also google searches told me check the value of file /proc/sys/dev/cdrom/lock and this value was 1 in my case. How this should be interpreted as?

    Or is there any other way to get this information.

  • Jaleks
    Jaleks almost 7 years
    If the CDROM is just loaded (inside tray) nothing has to be already mounted, so this will help only in the cases where the CD is mounted - which will e.g. not be the case if it's empty or an audio CD.