Program to check whether a disk is in a drive?
Hello,
Is there a Linux command to check whether there's a disk in a drive? Even an API function in C or C++? Basically, what I'm looking for is something that would be used like this: if diskin /dev/cdrom then ... fi The best I've been able to come up with is to mount it, then try to unmount it, and if the umount command returns successfully, there was a disk in the drive. But this doesn't work for music CDs, DVDs, or blank CDs - only for media that contain a filesystem that Linux recognizes. My primary use for this is I want to make all the disks in all my drives get ejected automatically at shutdown, just like on a Mac (I often forget disks in drives), but I don't want my CD trays popping out if there's nothing in them. |
How about a small hack which uses the Workman library used by KDE? Here's the program:
Code:
int main (void) { gcc diskin.c -L`kde-config --prefix`/lib -lworkman -I `kde-config --prefix`/include -o diskin Use as you mentioned: (argv[1] = /dev/cdrom not really used) Code:
if diskin /dev/cdrom Code:
#include <libwm/wm_cdrom.h> Arvind |
I forgot to mention. If argv[1] is an invalid device, it still returns 0. I don't believe it's that much of a big problem.
Arvind |
Thanks! I'll have to look into that wm_cd_init function.
I think it would definitely be worthwhile to make sure the argument is valid - after all, a program should work every single time and people sometimes make typos. That could be done by checking whether the device file exists. Better still would be to check whether it's actually a disk drive, but I don't know how. |
Quote:
The main part of checking if a CD is in is done. You do the rest. Arvind |
Of course; I didn't mean it that way. I meant it's worth my while to do the checking. I'm grateful for your suggestions.
|
I just got around to trying that wm_cd_init function, and the program wouldn't compile. It seems that I don't have the "workman" library installed. What package is it part of?
|
Hi,
Somehow I did not receive the notification for the message of yours. So, I'm this late in replying. The workman library is a part of "kdemultimedia" package. I think you can use the "eject" command's source directly. You will just have to prevent it from opening the drive if it fails to find a CD. I'm looking at the sources now. I'll tell you if I find out more about it. Arvind |
Hi,
/usr/include/linux/cdrom.h says: Code:
/* drive status possibilities returned by CDROM_DRIVE_STATUS ioctl */ "if (n_option)" [Line 903] [eject-2.0.13] Code:
#ifdef CDROM_DRIVE_STATUS You can rip out everything you don't want from eject.c. Arvind |
Thanks a lot! I'll have to try that, but I might not get a chance for a few days. That ioctl looks promising.
|
Sure. But, tell me how it goes.
Arvind |
I've completed the program. You're right that ioctl returns 2 when, according to the documentation, it should return 1. I've tried it on both my internal DVD-ROM and external CD Writer. Strange. Do you think it's a bug in the Linux kernel?
I was just thinking, too bad this only works for CD-ROM drives; I was looking for a way to check whether a disk is in any drive. I guess there's no single command to do that, right, since each piece of hardware is different? Anyway, for my purposes, this is just fine, and is much less of an ugly hack than I was doing before. Here's the complete C program: Code:
/*This code may be used in any way, except you may not call it your own |
Hi,
Quote:
Quote:
The cdrom.h is meant to be a uniform CDROM device interfce. I think it supports (or will support) DVD drives. It has some "capability" flags to find out whether the drive is a DVD drive. But, I don't know why it's not working. What happens when you try? Maybe you got the device wrong? If I get some time, I'll see what's going on. Arvind |
Ah good, the kernel maintainers know about this, and it's specifically introduced so that newer drives will work properly, as far as I can see. See this:
http://www.ussg.iu.edu/hypermail/lin...02.2/0281.html Arvind |
Quote:
|
All times are GMT -5. The time now is 02:45 AM. |