Inspired by this question. Why does Linux need both
Why not just access files on the cdrom through
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
/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)
(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
/media/cdrom. And see how everything goes on.)
(I will keep learning and refine my understanding as appropriate.)
Why do we have both
Why do we have both
/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.
/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.
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
/mediaMount 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.
/media/crdom are totally different thing. one is a block/device file for CD-ROM whereas another is a directory for mounting it!
A good source of information that covers this (and more) can be found here https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard
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.
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.