What is "mount" [duplicate]
what is "mount"? I have a gut feeling it means "make data storage accessible", but what does it really mean? Please specify the cases when "disk is accessible" and "disk is mounted" are different".
Well, let's get back to the basics:
When you plug in a memory device, such as a flash drive, the Linux kernel creates a special file for it inside the /dev directory. All your devices are represented by one of those files.
When this device is not mounted you cannot access/read/write from it. It's just like a "stand-by" mode, it's ready to be used, but not being used yet.
When you mount the device you have to specify a mountpoint. This is a very confusing concept for users coming from Windows. In Windows, every memory device has its own root. In Linux, they are always mounted inside of one single root.
Mounting a memory device is basically "turning it on" from the "stand-by" mode. It is accessible through the specified mountpoint.
The /mnt directory exists so you can mount things in it. In some distributions (like Ubuntu) there is also a /media directory, for the same purpose.
For simplicity, I'll limit this answer to usual storage devices like disks and flash drives.
When you plug in your device, its driver makes it accessible as a device node, which to user programs looks like a file: e.g. /dev/sda
. Now the disk is already accessible. You can read and write it. Here's an example of how you could look at its MBR (first 512 bytes) in hexadecimal+ASCII form:
sudo head -c 512 /dev/sda | xxd -g1
But if you have some files on that device and you want to access them, this low-level look isn't quite satisfactory. You want to involve a file system driver. For this you mount the device, using mount
utility. To make it more obvious, I'll specify the file system explicitly in the following command (assuming I've made the correct choice of ext4
; if not, mount
will tell you):
sudo mount /dev/sda1 /mnt/ -t ext4
After this, your device will be exposed in /mnt/
in the format user programs expect it: files and directories — instead of just plain sectors.
You may wonder why I used sda1
instead of sda
in the command above. It's because between filesystem level and plain sectors level there's a small layer of partitioning, and here sda1
denotes the first partition on device sda
.
How familiar are you with Windows (or — ugh! — DOS)?
If you have gotten beyond absolute newbie status on either of them,
you know that your main system secondary storage device (hard disk or SSD)
is called C:
. (For simplicity,
I will assume that the computer has only one secondary storage device,
and that it has only one partition.)
The root directory is C:\
, and it contains other directories
like C:\Intel
, C:\Program Files
, C:\Users
, and C:\Windows
.
These can contain yet another layer of directories; e.g.,
C:\Program Files\Oracle
, C:\Users\your_name
, and so on.
- But if you insert an optical disc (CD or DVD) into the appropriate drive,
it becomes
D:
. And it also has a root directory,D:\
, and typically subordinate directories likeD:\Install
. - And if you remember diskettes (“floppy disks”),
you’ll recall that they were disk
A:
(and, in the really old days, you could have a second one, which, naturally, was calledB:
). And the same pattern applies: the root directoryA:\
could contain folders likeA:\GAMES
andA:\WORK
. - Other removable drives (e.g., USB drives)
have drive letters beyond
D
(perhapsF
), and the root directoryF:\
could contain folders likeF:\Project1
. - And if you have network drives,
they might have drive letters near the end of the alphabet (perhaps
Z
), and the root directoryZ:\
could contain folders likeZ:\Project2
.
So your whole computer’s directory layout looked something like this:
A:\ C:\ D:\ F:\ Z:\
⋰ ⋱ ⋰ ⋰ ⋱ ⋱ ⋰ ⋰ ⋰
⋰ ⋱ ⋰ ⋰ ⋱ ⋱ ⋰ ⋰ ⋰
⋰ ⋱ ⋰ ⋰ ⋱ ⋱ ⋰ ⋰ ⋰
GAMES WORK Intel Program Files Users Windows Install Project1 Project2
⋰ ⋰
⋰ ⋰
⋰ ⋰
Oracle user480468
So you could have, in principle,
up to 26 independent filesystem directory trees,
each with its own root directory. And I suppose that works well enough,
especially now that Windows ties them all together in the Computer
(previously known as My Computer
) superfolder.
(But then again, look at how hard it is to install programs
on a disk other than C:
, or to move your user directory off C:
.)
And note that Computer
is a pseudo-folder —
you can’t create files or directories in Computer
.
Unix has a philosophy that includes the concept Do One Thing and Do It Well. (Even the name “Unix” is derived from the prefix “un-” or “uni-”, meaning “one”.) So it’s not surprising that the designers of Unix chose to have a single filesystem directory tree; you’re probably familiar with it:
/
⋰ ⋰ ⋰ ⋮ ⋱ ⋱ ⋱
⋰ ⋰ ⋰ ⋮ ⋱ ⋱ ⋱
⋰ ⋰ ⋰ ⋮ ⋱ ⋱ ⋱
⋰ ⋰ ⋰ ⋮ ⋱ ⋱ ⋱
⋰ ⋰ ⋰ ⋮ ⋱ ⋱ ⋱
⋰ ⋰ ⋰ ⋮ ⋱ ⋱ ⋱
bin dev etc home mnt tmp usr …
⋰ ⋰⋱
⋰ ⋰ ⋱
⋰ ⋰ ⋱
user48046 bin lib
The beauty of this is that you (in your role of system administrator)
can put everything in one partition,
or put /home
in one partition and /usr
in another,
and you (in your role of user) can forget about it.
You don’t need to know what disk (partition) /usr
is on;
it appears under the one-and-only filesystem root (/
) regardless.
The mild drawback is that you (in your role of power user)
must maintain this unified filesystem directory tree when you add
(and remove) storage components.
When you insert a CD or a flash drive, it’s just an entry under /dev
,
which you probably shouldn’t mess with too much.
You can do some things with the device node, like run fsck
or fdisk
;
or possibly configure it as a dedicated database storage device
(e.g., for Oracle) — because the disk is accessible.
But you can’t access the files (and directories) on it,
because they aren’t in the filesystem yet —
because the disk is not mounted.
TL;DR
And this is where mount
comes in.
mount
is the command that attaches a filesystem
(e.g., on a removable device) to the filesystem
(the one starting at /
).1
You’ll notice that there is a /mnt
directory under the /
directory.
Conventionally,
transient (removable) filesystem storage devices are mounted onto directories
under /mnt
; i.e., they are attached to the filesystem there.
And on some systems,
some devices are automatically mounted when they are detected.
But, in theory,
you can mount a filesystem anywhere in the filesystem.
___________
1 Splitting hairs,
mount
is a program that calls the mount()
system call,
which attaches a filesystem to the filesystem.