22

Inspired by this question. Why does Linux need both /dev/cdrom and /media/cdrom?

Why not just access files on the cdrom through /dev/cdrom?

smwikipedia
  • 1,203

7 Answers7

41

/media/cdrom is a convention for the mountpoint, while /dev/cdrom is the special device that could be mounted on the former.

You need both, because they serve different purposes: most applications do not read directly from the special device, but can read from a filesystem (something that is mounted)

Thomas Dickey
  • 76,765
  • 4
    I think @smwikipedia ask about why /dev/cdrom is not used as mountpoint, needing to mount it in another place. I used to think about this also in the past since it's already possible to mount a filesystem on top of a file (descriptor), maybe it's common to do otherwise for legacy reasons... – Piranna Mar 18 '16 at 12:56
  • 2
    @Piranna Yes, you are right. I do want to know why not just directly use /dev/cdrom to access CDROM files, be it called mount point or whatever. – smwikipedia Mar 18 '16 at 13:22
  • @ThomasDickey You said Most applications do not read directly from the special device. I think that's because a file system is easier to interact with than a raw block device. If so, I think my own answer is similar to yours. – smwikipedia Mar 18 '16 at 13:51
  • 7
    If you're dealing with files and directories, then you need a file system and hence the mount point. If you are dealing with the raw data - like when CD burning or copying by dd - there may not be even a valid filesystem, so you need the device file. – rexkogitans Mar 18 '16 at 14:25
  • @smwikipedia for the same reason /dev/sda1 may be mounted on /. – jfs Mar 19 '16 at 14:32
  • @rexkogitans That doesn't explain why you wouldn't use /dev/cdrom itself as the mounting point. – Mehdi Charife Jun 24 '23 at 21:35
  • Using /dev/cdrom itself as the mountpoint would interfere with applications that use the device, e.g., for controlling it (since a mounted filesystem would present a different driver to applications than the device). That's why devices are mounted in a different place (as a filesystem). – Thomas Dickey Jun 24 '23 at 21:50
21

(Thanks for so many answers to my question. After searching the web for a while, I want to share my own understanding.)

According to here:

In Unix-like operating systems, a device file or special file is an interface for a device driver that appears in a file system as if it were an ordinary file.

According to here:

The mount command serves to attach the filesystem found on some device to the big file tree.

So, I think there are 2 different levels of software abstraction here:

  • /dev/cdrom is a device special file. It abstract the CD-ROM hardware as a block IO device. This abstraction is provided by the device driver.

  • /media/cdrom is a mount point for a filesystem. So it provides a higher level of abstraction of the CD-ROM hardware, i.e. as a file system. Such as ISO-9660 file system. And this abstraction is provided by the file system driver.

So basically, 2 different file locations for 2 different levels of abstraction. And in different scenarios, we may need different one. I think other OS such as Windows also provide such different options only that Linux unifies it into a single file hierarchy.

(I guess maybe I should do some experiment by writing some C code on Linux to interact with both /dev/cdrom and /media/cdrom. And see how everything goes on.)

(I will keep learning and refine my understanding as appropriate.)

smwikipedia
  • 1,203
  • 1
    One final bit of info: the /dev/cdrom special file (and actually everything in /dev) is actually just a way to give a name and permissions to a pair of numbers that reference something in the magic parts of the kernel. If you know the magic numbers, the /dev files can be recreated using them eveneif they were deleted; take a look at the docs for the "mknod" command for more esotrica. – Weaver Mar 19 '16 at 06:16
  • 1
    "I think other OS such as Windows also provide such different options only that Linux unifies it into a single file hierarchy." – Windows is very similar, actually. In Unix, everything is a file, and other kinds of objects (devices, kernel objects, …) are represented as files (e.g. in /dev, /proc, or /sys). There is a single file hierarchy, and objects form subhierarchies. In Windows, everything is an object, files are just special kinds of objects. There is a single unified object hierarchy, and filesystems form subhierarchies. But there are two distinct entities, just like in Unix: – Jörg W Mittag Mar 19 '16 at 07:47
  • 2
    a device object for the CD-ROM drive, and a mount point for the filesystem on that CD-ROM. Not also that in modern Windows, the mount point no longer has to be a drive letter, it can be mounted anywhere inside another filesystem, just like in Unix. – Jörg W Mittag Mar 19 '16 at 07:48
16

Why do we have both /dev/cdrom and /media/cdrom?

Why do we have both /dev/sda2 and /home?

Basically, /dev/cdrom is a file. When you access it, you are accessing the individual bits and bytes on the CD (if there is one). Whereas /media/cdrom is a folder. When you access it, you are accessing the files stored on the CD.

Similarly, /dev/sda2 represents the raw contents of the second partition on the first harddrive. You would write to this directly, e.g., if you wanted to format the partition. (The mkfs program literally opens /dev/sda2 or whatever, and writes particular bit patterns onto it.) You then mount /dev/sda2 at, say, /home, and now you can access the actual files. As you access the files through the mount point, the filesystem driver is reading and writing the underlying device file.

This is just the way Unix does things.

  • So I guess /media/cdrom is implemented on top of /dev/cdrom from a software perspective. Right? – smwikipedia Mar 18 '16 at 13:57
  • 1
    @smwikipedia Pretty much, yes. – MathematicalOrchid Mar 18 '16 at 14:01
  • "This is just the way Unix does things." No, in Unix, everything is a file scnr. On a more serious note though, in the Hurd, you do actually mount file systems by putting a translator on a device file that then presents itself as a folder with the content of the FS, even though it's just a process mediating the access to that single file (you still have the distinction between mount point and device file though). Hope that wasn't too confusing. – Christian Mar 18 '16 at 14:41
5
  • You can see that /dev/cdrom is actually a block special file which is a device file.

    $ ls -l /dev/cdrom
    lrwxrwxrwx 1 root root 3 Mar 18 17:00 /dev/cdrom -> sr0
    $ ls -l /dev/sr0
    brw-rw----+ 1 root cdrom 11, 0 Mar 18 17:00 /dev/sr0
    
  • Visit Filesystem Hierarchy Standard:

    /media Mount points for removable media such as CD-ROMs (appeared in FHS-2.3).

    $ ls -ld /media/cdrom/
    drwxr-xr-x 2 root root 4096 Jun 15  2015 /media/cdrom/
    

    This directory contains subdirectories which are used as mount points for removeable media such as floppy disks, cdroms and zip disks.

So, /dev/cdrom and /media/crdom are totally different thing. one is a block/device file for CD-ROM whereas another is a directory for mounting it!

Pandya
  • 24,618
1

A good source of information that covers this (and more) can be found here https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard

  • /media => Mount points for removable media such as CD-ROMs (appeared in FHS-2.3).
  • /mnt => Temporarily mounted filesystems.
1

Why not just access files on the cdrom through /dev/cdrom

In addition to the answers already provided, you cannot use /dev/cdrom to access the cdrom content because mounting /dev/cdrom (the files present on the cdrom) on top of /dev/cdrom (the device) is a forbidden operation.

Under Linux, you can only mount a file system on top of an existing directory, not on top of an existing file or device.

Should the OS allow such an operation, a side effect would be accessing the original /dev/cdrom would no more be possible after the mount.

Should you really want to access your cdrom though /dev/cdrom, here is a hack that should work:

umount /media/cdrom
mv /dev/cdrom /dev/cdrom-org
mkdir /dev/cdrom
mount /dev/cdrom-org /dev/cdrom

Of course, there is no much point doing it.

jlliagre
  • 61,204
1

Linux device drivers are exposed as special files. Utilities/applications perform file operations on these files (ioctl calls) to control the devices.

One example of a utility that would make calls to a special file to control the device is "mount".

The mount utility is a program with the purpose to present the data on the cdrom device as a dir/file structure. This is a well understood and convenient way for users to use the data by simple reading it from the file structure that the mount utility created.

A cdrom device supports many other operations on the special file e.g. to open/close the tray etc.

Many special files used to control devices are stored in /dev to make it simple for developers/users of utilities to know where to look for it.

Furthermore, utilities that present data on devices (e.g. cdrom, sdcard, memory stick etc.) as a dir/file structure does so in /media to make it simple for developers/users to know where to look.

Both are therefore used for its own purpose.

Ernest
  • 11
  • It's helpful. I feel /dev is just like the Windows Device Manager. And thanks for pointing out A cdrom device supports many other operations on the special file e.g. to open/close the tray etc. – smwikipedia Mar 19 '16 at 10:20