[ltp] Detecting CDs (is there a disc in the cdrom drive ?)

FeRD linux-thinkpad@linux-thinkpad.org
Fri, 03 Feb 2006 19:47:44 -0500


This is a multi-part message in MIME format.
--------------000007050601050905020700
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Richard Neill wrote:
> Matt Graham wrote:
>> From: Richard Neill <rn214@hermes.cam.ac.uk>
>>
>>> I'm trying to write an acpi script for an X22 ultrabase.
>>> I need to know whether there is a CD or DVD in the drive at the time.
>>> Is there any easy way to find out?
>>
>>
>> Sure, but you can't really do it via the shell.  You have to open() 
>> the CD-R*
>> device with O_NONBLOCK and perform an ioctl() on it.  This is easily 
>> done with
>> a short C program or a Perl script.  
If you want to build on the work of them what done come before you, my 
Fedora Core 4 system has the binary 'cd-info' installed, which in 
addition to being able to print a bunch of textual info about the CD-ROM 
drive and/or contents (which you could toss), also seems to return 0 on 
successful disc read and 1 on attempting to read from an empty drive.

It's part of the libcdio package, apparently.

-FeRD

[ferd@tinky]~% cd-info --no-device-info -q

cd-info version 0.76 i686-redhat-linux-gnu

Copyright (c) 2003, 2004, 2005 R. Bernstein

This is free software; see the source for copying conditions.

There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A

PARTICULAR PURPOSE.

__________________________________


Disc mode is listed as: No information

cd-info: Can't get first track number. I give up.

[ferd@tinky]~% echo $?

1

[ferd@tinky]~% cd-info --no-device-info -q

cd-info version 0.76 i686-redhat-linux-gnu

Copyright (c) 2003, 2004, 2005 R. Bernstein

This is free software; see the source for copying conditions.

There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A

PARTICULAR PURPOSE.

__________________________________


Disc mode is listed as: CD-DATA (Mode 1)

CD-ROM Track List (1 - 1)

  #: MSF       LSN    Type   Green? Copy?

  1: 00:02:00  000000 data   false  no

170: 71:12:66  320316 leadout (718 MB raw, 625 MB formatted)

Media Catalog Number (MCN):

Last CD Session LSN: 0

__________________________________

CD Analysis Report

CD-ROM with ISO 9660 filesystem and joliet extension level 3

ISO 9660: 320316 blocks, label `FC/5-Test2 i386                 '

Application: FC/5-Test2 i386

Preparer   :

Publisher  :

System     : LINUX

Volume     : FC/5-Test2 i386

Volume Set : FC/5-Test2 i386

[ferd@tinky]~% echo $?

0

[ferd@tinky]~% rpm -qf /usr/bin/cd-info

libcdio-0.76-1.2.fc4.rf



--------------000007050601050905020700
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Richard Neill wrote:
<blockquote cite="mid43DD0354.60108@hermes.cam.ac.uk" type="cite">Matt
Graham wrote:
  <br>
  <blockquote type="cite">From: Richard Neill
<a class="moz-txt-link-rfc2396E" href="mailto:rn214@hermes.cam.ac.uk">&lt;rn214@hermes.cam.ac.uk&gt;</a>
    <br>
    <br>
    <blockquote type="cite">I'm trying to write an acpi script for an
X22 ultrabase.
      <br>
I need to know whether there is a CD or DVD in the drive at the time.
      <br>
Is there any easy way to find out?
      <br>
    </blockquote>
    <br>
    <br>
Sure, but you can't really do it via the shell.  You have to open() the
CD-R*
    <br>
device with O_NONBLOCK and perform an ioctl() on it.  This is easily
done with
    <br>
a short C program or a Perl script.  </blockquote>
</blockquote>
If you want to build on the work of them what done come before you, my
Fedora Core 4 system has the binary 'cd-info' installed, which in
addition to being able to print a bunch of textual info about the
CD-ROM drive and/or contents (which you could toss), also seems to
return 0 on successful disc read and 1 on attempting to read from an
empty drive.<br>
<br>
It's part of the libcdio package, apparently.<br>
<br>
-FeRD<br>
<br>
<pre><tt>[ferd@tinky]~% cd-info --no-device-info -q</tt></pre>
<pre><tt>cd-info version 0.76 i686-redhat-linux-gnu</tt></pre>
<pre><tt>Copyright (c) 2003, 2004, 2005 R. Bernstein</tt></pre>
<pre><tt>This is free software; see the source for copying conditions.</tt></pre>
<pre><tt>There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A</tt></pre>
<pre><tt>PARTICULAR PURPOSE.</tt></pre>
<pre><tt>__________________________________</tt></pre>
<pre><tt>
Disc mode is listed as: No information</tt></pre>
<pre><tt>cd-info: Can't get first track number. I give up.</tt></pre>
<pre><tt>[ferd@tinky]~% echo $?</tt></pre>
<pre><tt>1</tt></pre>
<pre><tt>[ferd@tinky]~% cd-info --no-device-info -q</tt></pre>
<pre><tt>cd-info version 0.76 i686-redhat-linux-gnu</tt></pre>
<pre><tt>Copyright (c) 2003, 2004, 2005 R. Bernstein</tt></pre>
<pre><tt>This is free software; see the source for copying conditions.</tt></pre>
<pre><tt>There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A</tt></pre>
<pre><tt>PARTICULAR PURPOSE.</tt></pre>
<pre><tt>__________________________________</tt></pre>
<pre><tt>
Disc mode is listed as: CD-DATA (Mode 1)</tt></pre>
<pre><tt>CD-ROM Track List (1 - 1)</tt></pre>
<pre><tt>  #: MSF       LSN    Type   Green? Copy?</tt></pre>
<pre><tt>  1: 00:02:00  000000 data   false  no</tt></pre>
<pre><tt>170: 71:12:66  320316 leadout (718 MB raw, 625 MB formatted)</tt></pre>
<pre><tt>Media Catalog Number (MCN):</tt></pre>
<pre><tt>Last CD Session LSN: 0</tt></pre>
<pre><tt>__________________________________</tt></pre>
<pre><tt>CD Analysis Report</tt></pre>
<pre><tt>CD-ROM with ISO 9660 filesystem and joliet extension level 3</tt></pre>
<pre><tt>ISO 9660: 320316 blocks, label `FC/5-Test2 i386                 '</tt></pre>
<pre><tt>Application: FC/5-Test2 i386</tt></pre>
<pre><tt>Preparer   :</tt></pre>
<pre><tt>Publisher  :</tt></pre>
<pre><tt>System     : LINUX</tt></pre>
<pre><tt>Volume     : FC/5-Test2 i386</tt></pre>
<pre><tt>Volume Set : FC/5-Test2 i386</tt></pre>
<pre><tt>[ferd@tinky]~% echo $?</tt></pre>
<pre><tt>0</tt></pre>
<pre><tt>[ferd@tinky]~% rpm -qf /usr/bin/cd-info</tt></pre>
<pre><tt>libcdio-0.76-1.2.fc4.rf</tt></pre>
<br>
</body>
</html>

--------------000007050601050905020700--