The question title asks: Why do we need to mount on Linux?
One way to interpret this question: Why do we need to issue explicit mount
commands to make file systems available on Linux?
The answer: we don't.
You don't need to mount file systems explicitly, you can arrange for it to be done automatically, and Linux distributions already do this for most devices, just like Windows and Macs do.
So that probably isn't what you meant to ask.
A second interpretation: Why do we sometimes need to issue explicit mount
commands to make file systems available on Linux? Why not make the operating system always do it for us, and hide it from the user?
This is the question I am reading in the question text, when you ask:
Why not skip mounting altogether, and do the following
ls /dev/cdrom
and have the content of the CD-ROM listed?
Presumably, you mean: why not just have that command do what
ls /media/cdrom
does now?
Well, in that case, /dev/cdrom
would be a directory tree, not a device file. So your real question seems to be: why have a device file in the first place?
I'd like to add an answer to the ones already given.
Why do users get to see device files?
Whenever you use a CD-ROM, or any other device that stores files, a piece of software is used that interprets whatever is on your CD-ROM as a directory tree of files. It is invoked whenever you use ls
or any other kind of command or application that accesses the files on your CD-ROM. That software is the file system driver for the particular file system used to write the files to your CD-ROM. Whenever you list, read or write files on a file system, it's the job of that software to make sure that the corresponding low-level read and write operations are performed on the device in question. Whenever you mount
a file system, you're telling the system which file system driver to use for the device. Whether you do this explicitly with a mount
command, or leave it to the OS to be done automatically, it will need to be done, and of course the file system driver software will need to be there in the first place.
How does a file system driver do its job? The answer: it does it by reading from and writing to the device file. Why? The answer, as you stated already: Unix was designed this way. In Unix, device files are the common low-level abstraction for devices. The really device-specific software (the device driver) for a particular device is supposed to implement opening, closing, reading and writing on the device as operations on the device file. That way, higher-level software (such as a file system driver) doesn't need to know as much about the internal workings of individual devices. The low-level device drivers and the file system drivers can be written separately, by different people, as long as they agree on a common way to interface with each other, and that is what the device files are for.
So file system drivers need the device files.
But why do we, ordinary users, get to see the device files? The answer is that Unix was designed to be used by operating system programmers. It was designed to allow its users to write device drivers and file system drivers. That is in fact how they get written.
The same is true for Linux: you can write your own file system driver (or device driver), install it, and then use it. It makes Linux (or any other variant of Unix) easily extensible (and it is in fact the reason Linux was started): when some new piece of hardware comes on the market, or a new, smarter way to implement a file system is designed, someone can write the code to support it, make it work, and contribute it to Linux.
Device files make this easier.
mount -t umsdos
to mount it, you have all Linux permissions, ownerships, special files, fifos etc. If youmount -t vfat
it behaves like a plain Windows partition.Why not access the /dev/cdrom directory directly?
Because it's not a directory.