Figuring out if a device is used

From: Vilmos Soti (vilmos_at_vilmos.org)
Date: 04/30/04


Date: 30 Apr 2004 09:33:48 -0700

Hello,

Is there a way to figure out if a device (cdrom) is in use
regardless how the device is called?

For example, say I have the following lines in my fstab:
(edited for brevity)

/dev/cdrom /mnt/cdrom ...
/dev/cdrom_norock /mnt/norock ...
/dev/cdrom_udf /mnt/udf ...

All three, /dev/cdrom, /dev/cdrom_norock, and /dev/cdrom_udf point
to the same hardware device, /dev/hdd which is a cdrom.

What I want to avoid if say, /dev/cdrom is mounted, then the other
two (and also an additional /dev/cdrom) cannot be mounted. I don't
want to have a situation something like this:

#################### Commands start ####################

# ls -l /dev/cdrom
lrwxrwxrwx 1 root root 3 Jan 9 16:19 /dev/cdrom -> hdd

# mount /dev/cdrom /mnt/cdrom
mount: block device /dev/cdrom is write-protected, mounting read-only

# mount /dev/hdd /mnt/floppy
mount: block device /dev/hdd is write-protected, mounting read-only

# df
Filesystem 1k-blocks Used Available Use% Mounted on
/dev/hda2 6095840 3275920 2510268 57% /
...
/dev/cdrom 42608 42608 0 100% /mnt/cdrom
/dev/hdd 42608 42608 0 100% /mnt/floppy
#

#################### Commands end ####################

I am writing a perl program which mounts a cdrom, then copies
the contents of it to a local disk. The whole process is
automated as much as possible since it is used by people who
are not computer experts.

The program takes time to start (since it has to do many
things before the actual reading), but apart from the actual
reading of the cdrom, they can run in parallel. So the
operator can start the program many times with different
parameters (which describe the cdrom). The initialization
runs, then it prompts the operator to insert the appropriate
cd and hit enter to start the reading. I am afraid that there
are many terminals up, and in one, the actual reading takes
place, and the operator makes an error and starts the actual
reading process in another window and essentially reads the
wrong cdrom.

One thing I can do is to iterate over all possible names
(/dev/cdrom, /dev/cdrom_norock, /dev/cdrom_udf) to see
if they are present in /proc/mounts and then make sure that
they are not used. Then I can mount it. But I feel this is a
not an elegant solution, and every time a new link to the
actual cdrom is created, I have to update my program.

So anybody knows if it is possible to figure out if a hardware
device is in use regardless its name?

Thanks, Vilmos



Relevant Pages

  • Re: Figuring out if a device is used
    ... >]the contents of it to a local disk. ... >]reading of the cdrom, ... > Use a lock file, and do not let anyone mount it if the lock file is ...
    (comp.os.linux.misc)
  • Re: [Problem] slow write to dvd-ram since 2.6.7-bk8
    ... to test that when I saw that writing to dvd-ram slowed down when reading ... System disk, cdrom and dvd-ram are ... some io and low load by reading from cdrom (raw read with ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • Re: 7CD changer takes forever to cache dirs
    ... >boot the OS takes forever to load as the changer appears to be reading every ... >reading every cdrom and doing whatever it and the OS ... As this was written for Windows NT, ...
    (microsoft.public.win2000.general)
  • Re: Figuring out if a device is used
    ... /dev/hdd which is a cdrom. ... ]reading of the cdrom, ... Then I can mount it. ... ]device is in use regardless its name? ...
    (comp.os.linux.misc)
  • Re: SUCCESS AT LAST
    ... >> "If the game wants to run from the CD, then your problem is that you ... >> not mount the CDROM drive in the .conf file or manually. ... guess you have to do two separate lines for the two drives.. ...
    (comp.sys.ibm.pc.games.adventure)