On Sat, Mar 1, 2008 at 12:41 AM, James G. Sack (jim) <[EMAIL PROTECTED]> wrote:
>
> Carl Lowenstein wrote:
> > On Fri, Feb 29, 2008 at 8:01 PM, James G. Sack (jim) <[EMAIL PROTECTED]>
> wrote:
> >> Carl Lowenstein wrote:
> >> > When I put a blank CD-R in the drive, after several seconds an icon
> >> > labeled "Blank CD-R Disc" appears on the desktop. Is there some
> >> > command-line way to recognize this?
> >> >
> >> > I am trying to make my CD-burning shell script more foolproof. If I
> >> > start the script too soon, the call to /usr/bin/cdrecord bombs out
> >> > with a message about "non-writeable disc". I think this really means
> >> > that the system hasn't gotten around to noticing the presence of the
> >> > blank. If I wait until the icon is visible, then there is no problem.
> >> >
> >>
> >> I have absolutely no reason to believe this is the right thing to do,
> >> but a couple of 'messin-around' experiments shows that
> >>
> >> dd if=/dev/cdrom bs=512 count=1 of=/dev/null
> >> mount /dev/cdrom /mnt
> >> both contain "No medium found" if the drive is empty (no disc present)
> >> file -s /dev/sr0
> >> reports /dev/sr0: writable, no read permission
It's late and I'm not sleepy so I'll bite ...
IIRC, there are some special SCSI commands that get sent to the device
to have it report disc information like, is it blank, CD-R, CD-RW,
etc., so dd's return codes are unreliable at best.
Something like cdrecord dev=/dev/sr0 -minfo combined with a little
while/sleep loop would do the trick. sg3_utils might also yield some
useful information.
/me looks ...
sdparm --command=load /dev/sr0 seems to block until the device is
ready on my system (PATA/ATAPI DVD+RW).
while ( ! sdparm --command=load /dev/sr0 )
do
sleep 1
done
cdrecord blah blah blah
scsi_ready (I think it comes with sg3_utils) seems to work too:
# drive tray open:
brak ~ # scsi_ready /dev/sr0
sg_turs /dev/sr0
device not ready
# tray closed, checking right away (blocks until disc ready):
brak ~ # scsi_ready /dev/sr0
sg_turs /dev/sr0
ready
# tray closed, no media
brak ~ # scsi_ready /dev/sr0
sg_turs /dev/sr0
device not ready
It seems to always return 0, so $? is useless. It also doesn't tell
you if the media is blank, but cdrecord can and you're already broken
at that point anyways unless you have a jukebox.
sg_verify and sg_mode also look interesting. cdrecord is probably
the most reliable option.
See cdrecord -minfo, -inq, -load, -lock, -eject, and -dummy
brak ~ # cdrecord -minfo dev=/dev/sr0
Cdrecord-ProDVD-ProBD-Clone 2.01.01a34 (x86_64-unknown-linux-gnu)
Copyright (C) 1995-2007 Jörg Schilling
--snip--
Mounted media class: CD
Mounted media type: CD-R
Disk Is not erasable
data type: standard
disk status: empty
session status: empty
BG format status: none
first track: 1
number of sessions: 1
first track in last sess: 1
last track in last sess: 1
Disk Is unrestricted
data type: standard
disk status: empty
session status: empty
BG format status: none
first track: 1
number of sessions: 1
first track in last sess: 1
last track in last sess: 1
Disk Is unrestricted
Disk type: undefined
last start of lead in: -11634
last start of lead out: 359849
Track Sess Type Start Addr End Addr Size
==============================================
1 1 Blank 0 359846 359847
Next writable address: 0
Remaining writable size: 359847
So really all you need after all is cdrecord -minfo |grep -iE "disk
status:.*empty". I'm pretty sure that hasn't changed in years and if
you're worried about it you can always build a static binary of
cdrecord and keep it with the app. It even gives you enough
information to keep writing sessions to a single CD/DVD RW and get the
options right from a shell script if you're feeling frisky.
Note, cdrecord & co. man pages are great for treating insomnia - I'm
off to bed ;)
-Al
> >>
> >> With a blank writable disc,
> >> dd reports Input/Output error
> >> mount reports a different error
> >> /dev/sr0: reports empty
> >>
> >> All of them first close the tray if it is open.
> >
> > OK, you seem to have found something useful. You must have one of
> > those new-fangled kernels that maps the CD drive to /dev/sr0. My CD
> > drive is /dev/hdc, symlinked to /dev/cdrom.
> >
> > # blank CD in drive, either open or shut
> > [EMAIL PROTECTED] tmp]$ file -sL /dev/cdrom; echo $?
> > /dev/cdrom: ERROR: cannot read `/dev/cdrom' (Input/output error)
> > 0
> > # if drive is open, closes it. Does not return until after disc icon
> > is on screen.
> >
> > # nothing in drive, either open or shut
> > [EMAIL PROTECTED] tmp]$ file -sL /dev/cdrom; echo $?
> > /dev/cdrom: writable, no read permission
> > 0
> > # if drive is open, closes it. Returns immediately, at least to the
> > human observer.
> >
> > Note that exit status of file command is 0 regardless, so that isn't much
> help.
> > But all is not lost:
> >
> > [EMAIL PROTECTED] tmp]$ file -sL /dev/cdrom | grep -q "cannot read"; echo
> $?
> > 1
> > [EMAIL PROTECTED] tmp]$ file -sL /dev/cdrom | grep -q "cannot read"; echo
> $?
> > 0
> >
> > I observe by the behavior of grep(1) that file(1) seems to write to
> > stdout. Interesting that file(1) should report "writable, no read
> > permission" for an empty drive, and "cannot read" for a blank disc.
>
> I thought that was interesting, as well. :-)
>
>
> >
> > Thanks for the ideas, I had run dry. No guarantee that this will work
> > for newer kernels etc. Will try newer distro's tomorrow some time.
> >
> > Late-breaking news. Newer system, with blank CD in drive. file(1)
> > reports "ERROR: cannot read ... "
> > If run again, it reports "empty". So best to grep for "writable, no
> > read permission", I suppose.
>
> Oh yeah, now I remember.. writing utilities that depend on status or
> error messages tend to fail because those messages seem to change -- in
> sync with the current fortune program, I guess.
>
> Regards,
> ..jim [to bed, now!]
>
>
>
>
> --
> [email protected]
> http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-list
>
--
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-list