How to detect whether there is a CD-ROM in the drive?
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?
linux compact-disc
add a comment |
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?
linux compact-disc
Related: stackoverflow.com/questions/15652520/…
– Aaron Digulla
Dec 8 '17 at 14:34
add a comment |
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?
linux compact-disc
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?
linux compact-disc
linux compact-disc
edited Sep 7 '13 at 9:58
slhck
160k47444466
160k47444466
asked Aug 11 '13 at 13:39
Aaron DigullaAaron Digulla
4,41653561
4,41653561
Related: stackoverflow.com/questions/15652520/…
– Aaron Digulla
Dec 8 '17 at 14:34
add a comment |
Related: stackoverflow.com/questions/15652520/…
– Aaron Digulla
Dec 8 '17 at 14:34
Related: stackoverflow.com/questions/15652520/…
– Aaron Digulla
Dec 8 '17 at 14:34
Related: stackoverflow.com/questions/15652520/…
– Aaron Digulla
Dec 8 '17 at 14:34
add a comment |
6 Answers
6
active
oldest
votes
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
4
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
1
blkidcauses the CD/DVD drive to close. I want something that can check if the platter is OPEN / CLOSED first, I think. Once closed (by a person), then it makes sense to see if there's media present. Any thoughts on that?
– will
Nov 5 '18 at 10:56
add a comment |
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...
Isn't that-gtshould be-eq?
– SHW
Feb 11 '14 at 6:56
A blank CD will fail to be mounted, so using mount does not really work. A broken CD will also fail to be mounted, but it is however present in the drive.
– Étienne
Jan 7 '16 at 16:22
add a comment |
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
add a comment |
You can try with lsblk command:
lsblk -fp
If under FSTYPE for line /dev/sr0 there is nothing -> media not loaded into cdrom drive.
If there is something under FSTYPE, probably iso9660 -> media is loaded into cdrom drive.
Another, I think the simplest way:
cat /dev/sr0 | head -1
If output is:
cat: /dev/sr0: No medium found
-> no media loaded.
If output is anything but this:
cat: /dev/sr0: No medium found
-> media is loaded.
Notice: I didn't try this with audio nor empty cds, but I believe result would be the same.
add a comment |
Here is the shell script I use for my own purposes. It's partially based on Allan's answer.
The reasoning behind it is basically that I was using it in an extended shell command using && and needed it to wait for the device to be ready to mount.
#!/bin/bash
# mountdvd:
# A shell script to wait until the optical drive can be mounted.
#
# Important Notes:
# - By default, this will wait about 10 seconds for the drive to finish reading a newly
# inserted disk.
# - Works best already be given a mount point in /etc/fstab
# - Works best if fs type is set to auto
# - Assumes /etc/fstab allows user to mount device
#
# Example /etc/fstab listing:
# /dev/cdrom /media/dvd auto nofail,auto,user,exec,utf8,noatime,ro,uid=plex,gid=pi 0 0
# Command name
COMMAND=`basename $0`
# Device to mount
DVD_DEVICE=/dev/cdrom
MOUNT_POINT=/media/dvd
# Number of attempts before giving up (Total time = ATTEMPTS * WAIT_TIME, default: 10 seconds)
ATTEMPTS=20
# Wait time in seconds
WAIT_TIME=0.5
# Check if already mounted first
MOUTPUT=`mountpoint -q $MOUNT_POINT`
MSTATUS=$?
if [ $MSTATUS -eq 0 ]; then
echo "$COMMAND: $DVD_DEVICE was already mounted."
exit 0
fi
#for ATTEMPT in {1..$ATTEMPTS}
while [ $ATTEMPTS -gt 0 ];
do
# Attempt to mount device
OUTPUT=`mount $DVD_DEVICE 2>&1`
STATUS=$?
if [ $STATUS -eq 0 ]; then
# Device mounted
exit 0
else
# Double check here, just in case earlier check failed.
if [[ "$OUTPUT" =~ "already mounted" ]]; then
# Device was already mounted
echo "$COMMAND: $DVD_DEVICE was already mounted."
exit 0
fi
fi
if [ $ATTEMPTS -ne 1 ]; then
# Wait a moment before trying again.
sleep $WAIT_TIME
fi
let ATTEMPTS=ATTEMPTS-1
done
echo "$COMMAND: ERROR: Unable to mount $DVD_DEVICE."
exit 1
add a comment |
You can do the following with Python3 and the standard library:
import fcntl
import os
CDROM_DRIVE = '/dev/sr0'
def detect_tray(CDROM_DRIVE):
"""detect_tray reads status of the CDROM_DRIVE.
Statuses:
1 = no disk in tray
2 = tray open
3 = reading tray
4 = disk in tray
"""
fd = os.open(CDROM_DRIVE, os.O_RDONLY | os.O_NONBLOCK)
rv = fcntl.ioctl(fd, 0x5326)
os.close(fd)
print(rv)
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f630588%2fhow-to-detect-whether-there-is-a-cd-rom-in-the-drive%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
4
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
1
blkidcauses the CD/DVD drive to close. I want something that can check if the platter is OPEN / CLOSED first, I think. Once closed (by a person), then it makes sense to see if there's media present. Any thoughts on that?
– will
Nov 5 '18 at 10:56
add a comment |
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
4
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
1
blkidcauses the CD/DVD drive to close. I want something that can check if the platter is OPEN / CLOSED first, I think. Once closed (by a person), then it makes sense to see if there's media present. Any thoughts on that?
– will
Nov 5 '18 at 10:56
add a comment |
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
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
answered Aug 11 '13 at 13:58
user606723user606723
8132916
8132916
4
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
1
blkidcauses the CD/DVD drive to close. I want something that can check if the platter is OPEN / CLOSED first, I think. Once closed (by a person), then it makes sense to see if there's media present. Any thoughts on that?
– will
Nov 5 '18 at 10:56
add a comment |
4
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
1
blkidcauses the CD/DVD drive to close. I want something that can check if the platter is OPEN / CLOSED first, I think. Once closed (by a person), then it makes sense to see if there's media present. Any thoughts on that?
– will
Nov 5 '18 at 10:56
4
4
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
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
1
1
blkid causes the CD/DVD drive to close. I want something that can check if the platter is OPEN / CLOSED first, I think. Once closed (by a person), then it makes sense to see if there's media present. Any thoughts on that?– will
Nov 5 '18 at 10:56
blkid causes the CD/DVD drive to close. I want something that can check if the platter is OPEN / CLOSED first, I think. Once closed (by a person), then it makes sense to see if there's media present. Any thoughts on that?– will
Nov 5 '18 at 10:56
add a comment |
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...
Isn't that-gtshould be-eq?
– SHW
Feb 11 '14 at 6:56
A blank CD will fail to be mounted, so using mount does not really work. A broken CD will also fail to be mounted, but it is however present in the drive.
– Étienne
Jan 7 '16 at 16:22
add a comment |
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...
Isn't that-gtshould be-eq?
– SHW
Feb 11 '14 at 6:56
A blank CD will fail to be mounted, so using mount does not really work. A broken CD will also fail to be mounted, but it is however present in the drive.
– Étienne
Jan 7 '16 at 16:22
add a comment |
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...
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...
answered Aug 11 '13 at 14:14
vparsons0u812vparsons0u812
192
192
Isn't that-gtshould be-eq?
– SHW
Feb 11 '14 at 6:56
A blank CD will fail to be mounted, so using mount does not really work. A broken CD will also fail to be mounted, but it is however present in the drive.
– Étienne
Jan 7 '16 at 16:22
add a comment |
Isn't that-gtshould be-eq?
– SHW
Feb 11 '14 at 6:56
A blank CD will fail to be mounted, so using mount does not really work. A broken CD will also fail to be mounted, but it is however present in the drive.
– Étienne
Jan 7 '16 at 16:22
Isn't that
-gt should be -eq ?– SHW
Feb 11 '14 at 6:56
Isn't that
-gt should be -eq ?– SHW
Feb 11 '14 at 6:56
A blank CD will fail to be mounted, so using mount does not really work. A broken CD will also fail to be mounted, but it is however present in the drive.
– Étienne
Jan 7 '16 at 16:22
A blank CD will fail to be mounted, so using mount does not really work. A broken CD will also fail to be mounted, but it is however present in the drive.
– Étienne
Jan 7 '16 at 16:22
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Jul 4 '15 at 18:43
fixer1234
18.1k144681
18.1k144681
answered Jul 1 '15 at 22:20
AllanAllan
111
111
add a comment |
add a comment |
You can try with lsblk command:
lsblk -fp
If under FSTYPE for line /dev/sr0 there is nothing -> media not loaded into cdrom drive.
If there is something under FSTYPE, probably iso9660 -> media is loaded into cdrom drive.
Another, I think the simplest way:
cat /dev/sr0 | head -1
If output is:
cat: /dev/sr0: No medium found
-> no media loaded.
If output is anything but this:
cat: /dev/sr0: No medium found
-> media is loaded.
Notice: I didn't try this with audio nor empty cds, but I believe result would be the same.
add a comment |
You can try with lsblk command:
lsblk -fp
If under FSTYPE for line /dev/sr0 there is nothing -> media not loaded into cdrom drive.
If there is something under FSTYPE, probably iso9660 -> media is loaded into cdrom drive.
Another, I think the simplest way:
cat /dev/sr0 | head -1
If output is:
cat: /dev/sr0: No medium found
-> no media loaded.
If output is anything but this:
cat: /dev/sr0: No medium found
-> media is loaded.
Notice: I didn't try this with audio nor empty cds, but I believe result would be the same.
add a comment |
You can try with lsblk command:
lsblk -fp
If under FSTYPE for line /dev/sr0 there is nothing -> media not loaded into cdrom drive.
If there is something under FSTYPE, probably iso9660 -> media is loaded into cdrom drive.
Another, I think the simplest way:
cat /dev/sr0 | head -1
If output is:
cat: /dev/sr0: No medium found
-> no media loaded.
If output is anything but this:
cat: /dev/sr0: No medium found
-> media is loaded.
Notice: I didn't try this with audio nor empty cds, but I believe result would be the same.
You can try with lsblk command:
lsblk -fp
If under FSTYPE for line /dev/sr0 there is nothing -> media not loaded into cdrom drive.
If there is something under FSTYPE, probably iso9660 -> media is loaded into cdrom drive.
Another, I think the simplest way:
cat /dev/sr0 | head -1
If output is:
cat: /dev/sr0: No medium found
-> no media loaded.
If output is anything but this:
cat: /dev/sr0: No medium found
-> media is loaded.
Notice: I didn't try this with audio nor empty cds, but I believe result would be the same.
answered Dec 18 '18 at 20:51
DamirDamir
111
111
add a comment |
add a comment |
Here is the shell script I use for my own purposes. It's partially based on Allan's answer.
The reasoning behind it is basically that I was using it in an extended shell command using && and needed it to wait for the device to be ready to mount.
#!/bin/bash
# mountdvd:
# A shell script to wait until the optical drive can be mounted.
#
# Important Notes:
# - By default, this will wait about 10 seconds for the drive to finish reading a newly
# inserted disk.
# - Works best already be given a mount point in /etc/fstab
# - Works best if fs type is set to auto
# - Assumes /etc/fstab allows user to mount device
#
# Example /etc/fstab listing:
# /dev/cdrom /media/dvd auto nofail,auto,user,exec,utf8,noatime,ro,uid=plex,gid=pi 0 0
# Command name
COMMAND=`basename $0`
# Device to mount
DVD_DEVICE=/dev/cdrom
MOUNT_POINT=/media/dvd
# Number of attempts before giving up (Total time = ATTEMPTS * WAIT_TIME, default: 10 seconds)
ATTEMPTS=20
# Wait time in seconds
WAIT_TIME=0.5
# Check if already mounted first
MOUTPUT=`mountpoint -q $MOUNT_POINT`
MSTATUS=$?
if [ $MSTATUS -eq 0 ]; then
echo "$COMMAND: $DVD_DEVICE was already mounted."
exit 0
fi
#for ATTEMPT in {1..$ATTEMPTS}
while [ $ATTEMPTS -gt 0 ];
do
# Attempt to mount device
OUTPUT=`mount $DVD_DEVICE 2>&1`
STATUS=$?
if [ $STATUS -eq 0 ]; then
# Device mounted
exit 0
else
# Double check here, just in case earlier check failed.
if [[ "$OUTPUT" =~ "already mounted" ]]; then
# Device was already mounted
echo "$COMMAND: $DVD_DEVICE was already mounted."
exit 0
fi
fi
if [ $ATTEMPTS -ne 1 ]; then
# Wait a moment before trying again.
sleep $WAIT_TIME
fi
let ATTEMPTS=ATTEMPTS-1
done
echo "$COMMAND: ERROR: Unable to mount $DVD_DEVICE."
exit 1
add a comment |
Here is the shell script I use for my own purposes. It's partially based on Allan's answer.
The reasoning behind it is basically that I was using it in an extended shell command using && and needed it to wait for the device to be ready to mount.
#!/bin/bash
# mountdvd:
# A shell script to wait until the optical drive can be mounted.
#
# Important Notes:
# - By default, this will wait about 10 seconds for the drive to finish reading a newly
# inserted disk.
# - Works best already be given a mount point in /etc/fstab
# - Works best if fs type is set to auto
# - Assumes /etc/fstab allows user to mount device
#
# Example /etc/fstab listing:
# /dev/cdrom /media/dvd auto nofail,auto,user,exec,utf8,noatime,ro,uid=plex,gid=pi 0 0
# Command name
COMMAND=`basename $0`
# Device to mount
DVD_DEVICE=/dev/cdrom
MOUNT_POINT=/media/dvd
# Number of attempts before giving up (Total time = ATTEMPTS * WAIT_TIME, default: 10 seconds)
ATTEMPTS=20
# Wait time in seconds
WAIT_TIME=0.5
# Check if already mounted first
MOUTPUT=`mountpoint -q $MOUNT_POINT`
MSTATUS=$?
if [ $MSTATUS -eq 0 ]; then
echo "$COMMAND: $DVD_DEVICE was already mounted."
exit 0
fi
#for ATTEMPT in {1..$ATTEMPTS}
while [ $ATTEMPTS -gt 0 ];
do
# Attempt to mount device
OUTPUT=`mount $DVD_DEVICE 2>&1`
STATUS=$?
if [ $STATUS -eq 0 ]; then
# Device mounted
exit 0
else
# Double check here, just in case earlier check failed.
if [[ "$OUTPUT" =~ "already mounted" ]]; then
# Device was already mounted
echo "$COMMAND: $DVD_DEVICE was already mounted."
exit 0
fi
fi
if [ $ATTEMPTS -ne 1 ]; then
# Wait a moment before trying again.
sleep $WAIT_TIME
fi
let ATTEMPTS=ATTEMPTS-1
done
echo "$COMMAND: ERROR: Unable to mount $DVD_DEVICE."
exit 1
add a comment |
Here is the shell script I use for my own purposes. It's partially based on Allan's answer.
The reasoning behind it is basically that I was using it in an extended shell command using && and needed it to wait for the device to be ready to mount.
#!/bin/bash
# mountdvd:
# A shell script to wait until the optical drive can be mounted.
#
# Important Notes:
# - By default, this will wait about 10 seconds for the drive to finish reading a newly
# inserted disk.
# - Works best already be given a mount point in /etc/fstab
# - Works best if fs type is set to auto
# - Assumes /etc/fstab allows user to mount device
#
# Example /etc/fstab listing:
# /dev/cdrom /media/dvd auto nofail,auto,user,exec,utf8,noatime,ro,uid=plex,gid=pi 0 0
# Command name
COMMAND=`basename $0`
# Device to mount
DVD_DEVICE=/dev/cdrom
MOUNT_POINT=/media/dvd
# Number of attempts before giving up (Total time = ATTEMPTS * WAIT_TIME, default: 10 seconds)
ATTEMPTS=20
# Wait time in seconds
WAIT_TIME=0.5
# Check if already mounted first
MOUTPUT=`mountpoint -q $MOUNT_POINT`
MSTATUS=$?
if [ $MSTATUS -eq 0 ]; then
echo "$COMMAND: $DVD_DEVICE was already mounted."
exit 0
fi
#for ATTEMPT in {1..$ATTEMPTS}
while [ $ATTEMPTS -gt 0 ];
do
# Attempt to mount device
OUTPUT=`mount $DVD_DEVICE 2>&1`
STATUS=$?
if [ $STATUS -eq 0 ]; then
# Device mounted
exit 0
else
# Double check here, just in case earlier check failed.
if [[ "$OUTPUT" =~ "already mounted" ]]; then
# Device was already mounted
echo "$COMMAND: $DVD_DEVICE was already mounted."
exit 0
fi
fi
if [ $ATTEMPTS -ne 1 ]; then
# Wait a moment before trying again.
sleep $WAIT_TIME
fi
let ATTEMPTS=ATTEMPTS-1
done
echo "$COMMAND: ERROR: Unable to mount $DVD_DEVICE."
exit 1
Here is the shell script I use for my own purposes. It's partially based on Allan's answer.
The reasoning behind it is basically that I was using it in an extended shell command using && and needed it to wait for the device to be ready to mount.
#!/bin/bash
# mountdvd:
# A shell script to wait until the optical drive can be mounted.
#
# Important Notes:
# - By default, this will wait about 10 seconds for the drive to finish reading a newly
# inserted disk.
# - Works best already be given a mount point in /etc/fstab
# - Works best if fs type is set to auto
# - Assumes /etc/fstab allows user to mount device
#
# Example /etc/fstab listing:
# /dev/cdrom /media/dvd auto nofail,auto,user,exec,utf8,noatime,ro,uid=plex,gid=pi 0 0
# Command name
COMMAND=`basename $0`
# Device to mount
DVD_DEVICE=/dev/cdrom
MOUNT_POINT=/media/dvd
# Number of attempts before giving up (Total time = ATTEMPTS * WAIT_TIME, default: 10 seconds)
ATTEMPTS=20
# Wait time in seconds
WAIT_TIME=0.5
# Check if already mounted first
MOUTPUT=`mountpoint -q $MOUNT_POINT`
MSTATUS=$?
if [ $MSTATUS -eq 0 ]; then
echo "$COMMAND: $DVD_DEVICE was already mounted."
exit 0
fi
#for ATTEMPT in {1..$ATTEMPTS}
while [ $ATTEMPTS -gt 0 ];
do
# Attempt to mount device
OUTPUT=`mount $DVD_DEVICE 2>&1`
STATUS=$?
if [ $STATUS -eq 0 ]; then
# Device mounted
exit 0
else
# Double check here, just in case earlier check failed.
if [[ "$OUTPUT" =~ "already mounted" ]]; then
# Device was already mounted
echo "$COMMAND: $DVD_DEVICE was already mounted."
exit 0
fi
fi
if [ $ATTEMPTS -ne 1 ]; then
# Wait a moment before trying again.
sleep $WAIT_TIME
fi
let ATTEMPTS=ATTEMPTS-1
done
echo "$COMMAND: ERROR: Unable to mount $DVD_DEVICE."
exit 1
edited Mar 20 '17 at 10:17
Community♦
1
1
answered Jan 29 '17 at 5:20
JasonJason
1011
1011
add a comment |
add a comment |
You can do the following with Python3 and the standard library:
import fcntl
import os
CDROM_DRIVE = '/dev/sr0'
def detect_tray(CDROM_DRIVE):
"""detect_tray reads status of the CDROM_DRIVE.
Statuses:
1 = no disk in tray
2 = tray open
3 = reading tray
4 = disk in tray
"""
fd = os.open(CDROM_DRIVE, os.O_RDONLY | os.O_NONBLOCK)
rv = fcntl.ioctl(fd, 0x5326)
os.close(fd)
print(rv)
add a comment |
You can do the following with Python3 and the standard library:
import fcntl
import os
CDROM_DRIVE = '/dev/sr0'
def detect_tray(CDROM_DRIVE):
"""detect_tray reads status of the CDROM_DRIVE.
Statuses:
1 = no disk in tray
2 = tray open
3 = reading tray
4 = disk in tray
"""
fd = os.open(CDROM_DRIVE, os.O_RDONLY | os.O_NONBLOCK)
rv = fcntl.ioctl(fd, 0x5326)
os.close(fd)
print(rv)
add a comment |
You can do the following with Python3 and the standard library:
import fcntl
import os
CDROM_DRIVE = '/dev/sr0'
def detect_tray(CDROM_DRIVE):
"""detect_tray reads status of the CDROM_DRIVE.
Statuses:
1 = no disk in tray
2 = tray open
3 = reading tray
4 = disk in tray
"""
fd = os.open(CDROM_DRIVE, os.O_RDONLY | os.O_NONBLOCK)
rv = fcntl.ioctl(fd, 0x5326)
os.close(fd)
print(rv)
You can do the following with Python3 and the standard library:
import fcntl
import os
CDROM_DRIVE = '/dev/sr0'
def detect_tray(CDROM_DRIVE):
"""detect_tray reads status of the CDROM_DRIVE.
Statuses:
1 = no disk in tray
2 = tray open
3 = reading tray
4 = disk in tray
"""
fd = os.open(CDROM_DRIVE, os.O_RDONLY | os.O_NONBLOCK)
rv = fcntl.ioctl(fd, 0x5326)
os.close(fd)
print(rv)
answered Oct 15 '18 at 22:40
ScottScott
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f630588%2fhow-to-detect-whether-there-is-a-cd-rom-in-the-drive%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Related: stackoverflow.com/questions/15652520/…
– Aaron Digulla
Dec 8 '17 at 14:34