[ltp] Detecting CDs (is there a disc in the cdrom drive ?)
Richard Neill
linux-thinkpad@linux-thinkpad.org
Sun, 29 Jan 2006 17:36:17 +0000
Dear All,
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?
I can't see anything helpful in /proc, or from hdparm. Unfortunately,
mount only tells me if there is a mounted data CD present. I want to
know whether there is any disc, eg (audio cd or dvd) present.
The purpose of the script is thus:
1)User presses the "I want to eject the ultrabase" button
2)Script responds to the acpi event by shutting down the drive if
possible, then unregistering the IDE interface. Or it complains loudly
if you have a mounted filesystem.
3)User then physically removes the ultrabase.
Thanks for your help.
Richard
P.S. my current script is below, and uses a truly horrid hack with
cddb-id. There *must* be a better way!
/etc/acpi/actions/ultrabase_eject.sh
------------------------------------------
#!/bin/bash
echo "syncing disks for safety"
sync
echo "Unregistering IDE interface for CDROM and preparing to eject."
#What if there is a disk in the drive?
#Case 1: there is an umounted CDROM, or an AudioCD/DVD which is
#NOT playing.
#OK to proceed.
#Case 2: there is a mounted CDROM. We mustn't proceed.
#Actually, we could *sometimes* unmount it (using eject), but safer to
#leave it for the user.
if grep hdc /proc/mounts >/dev/null; then
echo "There is a mounted filesystem on /dev/hdc. Cannot eject
it."
echo 9 > /proc/acpi/ibm/beep
exit 1
fi
#Case 3: there is a AudioCD/DVD which is currently playing.
#How do we detect this? It could be just using cdp (which has
#no process)
#Assuming that we have already ruled out a mounted filesystem, then...
if cddb-id /dev/hdc >/dev/null 2>&1; then
echo "There is a CD in the drive. Ejecting it"
eject /dev/hdc
fi
#This has the bug that we don't really want to eject the disk; we merely
#want to stop it playing.
#Unregister IDE interface 1. This WILL cause a kernel panic if there
#is anything in the drive!
idectl 1 off
#Tell the thinkpad the bay is to be ejected
echo eject > /proc/acpi/ibm/bay
#Beep happily
echo 6 > /proc/acpi/ibm/beep
echo "OK to undock"