[Tutor] Reading the CDROM in Linux

Steven D'Aprano steve at pearwood.info
Sat Nov 6 02:25:28 CET 2010


Terry Carroll wrote:
> I have a program that traverses the directory of a CDROM using os.walk. 
> I do most of my work on Windows, but some on (Ubuntu) Linux, and I'd 
> like this to work in both environments.
> 
> On Windows, I do something along the lines of this:
> 
>   startpoint="D:/"

What if the user has two CD drives? What if they have a second hard disk 
mounted on D:/, and a network drive on E:/, and use F:/ or A:/ or Z:/ 
for the CD drive?

If this program is for you, then it is fine to make assumptions about 
where the CD drive will be mounted, but don't make the mistake of 
thinking that they're not assumptions.


>   for (root, dirs, files) in os.walk(startpoint):
>      (stuff)
> 
> What would I use for startpoint in Linux?  I've tried "/dev/sr0" and 
> "/dev/sr0/"; neither work.  (If I recall correctly, the for-loop just 
> drops through; I'm not at my Linux box right now to confirm.)

As a general rule, don't touch anything in /dev unless you know what 
you're doing. /dev/sr0, and it's more user-friendly name /dev/cdrom, are 
devices, not folders. This is how low-level programs get access to the 
raw bytes being read from the device (in this case the CDROM drive) 
*before* it is turned into files.


> A number of other mount points (like /dev/cdrom, I think) are links to 
> /dev/sr0; and don't work either.

No, of course not. If they're links, that will mean that they are 
essentially nicknames or aliases. If Fred Smith doesn't respond when you 
talk to him, then calling him Freddy, Mr Smith, Hey you! or Frederick 
isn't going to work either.

> It *does* work to start at "/media/VOLUMENAME", where VOLUMENAME is the 
> volume ID of the disc; but my program will not know that.  I need to be 
> able to do this without knowing the volume name.

Yes, this is because you need to look at the mount point. The mount 
point is where the CDROM disk is mounted as a file system, in other 
words, where you can see the files on the disk *as files*. If you want 
to read the raw bytes off the disk, you open /dev/cdrom as a file and 
read from it.

On Unix and Linux systems, there are two conventions for mounting 
external media. One is that if you, the user, mount something by hand 
using the "mount" command, it gets placed in /mnt (old-school Unix sys 
admins had keyboards without vowels *wink*). Often people would use 
subdirectories under /mnt:

/mnt/cdrom
/mnt/floppy

are the two most common ones.

The other convention is that modern window/desktop managers like KDE and 
Gnome will automatically mount devices by name under /media. This is 
typically for CDs, DVDs, cameras and mobile phones with file storage, 
USB sticks and portable hard drives, etc.

If you only have one CD drive, and no other devices mounted, you can 
just look at /media and walk over that without caring what the CD drive 
is called. In other words, just use /media as the starting point, and 
let os.walk discover the name of the CD under it.

But if you might have an external hard drive plugged in, or a USB key, 
or similar, then you need to find out what the volume name of the 
mounted CD drive is. That's a good question and I don't have an answer 
right now. Let me think about it and get back to you.


-- 
Steven



More information about the Tutor mailing list