Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I know my CD-ROM device (/dev/sr0) but how can I detect from a script whether the drive is empty or whether there is a disk in it?

share|improve this question

3 Answers 3

up vote 3 down vote accepted

You can get information about any block device using the command blkid.

[root@arch32-vm ~]# blkid /dev/sr0
/dev/sr0: UUID="2013-05-31-23-04-19-00" LABEL="ARCH_201306" TYPE="iso9660" PTTYPE="dos"
[root@arch32-vm ~]# echo $?
0

If I remove the disk, I don't get any output and exit value is 2. (0 means success. A non-zero value will typically mean something abnormal happen or an error occurred)

[root@arch32-vm ~]# blkid /dev/sr0
[root@arch32-vm ~]# echo $?
2
share|improve this answer
2  
blkid detects partitions, If i put a audio CD, it is not detected. Didn't find a easier solution than create a program using ioctl as described in stackoverflow.com/questions/15652520/… –  BatchyX May 17 '14 at 19:55

Try mounting the device.

mount -t iso9660 /dev/sr0 /mnt/cdrom

Then check the return value $?

If the return is 0, "good" then there was disc present. Else, it will return not good "1" or anything but "0"

So to check silently, I would script it as so.

cdrom_mount=0
mount -t iso9660 /dev/sr0 /mnt/cdrom >/dev/null 2>&1
if [[ $? -gt 0 ]]
    then
    cdrom_mount=true
else
    cdrom_mount=false
fi

This is a very simplistic example, but you could do something similar...

share|improve this answer
    
Isn't that -gt should be -eq ? –  SHW Feb 11 '14 at 6:56

The issue with this shell-scripting approach is that none of the shell commands, mount, lsblk, blkid, can wait/block/pause and determine whether a cdrom is reporting "no medium found" because the tray has just closed and it is initializing itself to read the cd, or because there is no cd in the device, and "no medium found" will be reported forever. So you can choose a reasonable number of tries to pester the cdrom device at a certain sleep interval before giving up, as in the shell script below, or you can write a piece of c code with a few ioctl calls, and get some information from the cdrom, directly through the kernel.

#!/bin/sh

# cd.close
#
# Close the CD-ROM tray, and mount the CD-ROM device:
#
# mount status codes: see man mount(8)
# ------------------------------------
# 0   success
# 1   incorrect invocation or permissions
# 2   system error (out of memory, cannot fork, no more loop devices)
# 4   internal mount bug
# 8   user interrupt
# 16  problems writing or locking /etc/mtab
# 32  mount failure
# 64  some mount succeeded (in the case of mount -a)

CDROM=/dev/sr0
TRIES="1 2 3"
INTERVAL=5
MOUNT=0

TOKENS=( $TRIES )
STOP=${TOKENS[-1]}

for i in $TRIES; do
echo close: ATTEMPT $i of $STOP
output=`mount $CDROM -t iso9660 /cdrom 2>&1`
status=$?
echo mount: OUTPUT $output
echo mount: STATUS $status
if [ $status -eq 0 ]; then
  MOUNT=1
  break
else
  if [[ "$output" =~ "already mounted" ]]; then
    MOUNT=1
    break
  fi
fi
if [ $i -eq $STOP ]; then
  break
fi
echo sleep: $INTERVAL SECONDS...
sleep $INTERVAL
done

if [ $MOUNT -eq 1 ]; then
  echo final: MOUNTED $CDROM
  printf "final: LABEL "
  volname $CDROM
else
  echo final: NO MEDIUM
fi
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.