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
?
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
?
/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)
/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
/dev/cdrom
to access CDROM files, be it called mount point or whatever.
– smwikipedia
Mar 18 '16 at 13:22
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
dd
- there may not be even a valid filesystem, so you need the device file.
– rexkogitans
Mar 18 '16 at 14:25
/dev/cdrom
itself as the mounting point.
– Mehdi Charife
Jun 24 '23 at 21:35
/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
(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.)
/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
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.
/media/cdrom
is implemented on top of /dev/cdrom
from a software perspective. Right?
– smwikipedia
Mar 18 '16 at 13:57
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!
A good source of information that covers this (and more) can be found here https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard
/dev/cdrom
- while the question doesn't reference /mnt
.
– maxschlepzig
Mar 18 '16 at 13:02
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.
/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
/dev/sda1
and/
? – user253751 Mar 19 '16 at 05:55