Drives, Mounting and Linux

From LQWiki
Jump to: navigation, search

Overview

A common source of confusion for people new to Linux and other Unix-like operating systems is how it deals with drives, partitions, filesystems and mounting various things to access the files on their drive(s). This wiki article hopes to explain some of these misconceptions, along with how we "mount" drives to access our files stored on them in Linux. The following also applies to other Unix-like systems as well as all Linux-based operating systems.

The filesystem hierarchy

In Unix-like and Linux-based operating systems "drive letters", such as C: drive, are not used. Normal directories (also known as "folders") are used in Unix-like and Linux-based operating systems in place of "drive letters", as much as they are used as "containers" for files and other directories. A filesystem hierarchy is used instead, and any filesystems mounted are "attached" to this hierarchy that begins at / The partition that contains the filesystem mounted at / is known as the "root partition", this filesystem in turn is known as the "root filesystem" (but the "root partition" and the "root filesystem" are not the same thing as the "root user". See the root disambiguation wiki page). The hierarchy itself, being the /, is known as the "root directory". It might be easier to think of it as an upside down tree, with the trunk at the top of the tree rather than at the bottom. In order to access files stored on another drive in Linux, the filesystem of the drive that contains those files must be "mounted", or in other words, "attached" to the filesystem hierarchy. Filesystems can be attached at almost any point of the filesystem hierarchy, for example, you can create a directory in /mnt and mount your drive within /mnt if you don't always want to have it mounted. It's important to understand that, when you mount a drive, you are actually mounting the filesystem, not the partition that contains the filesystem. This is often a source of confusion and misunderstanding in Unix-like and Linux-based operating systems.

Partitions in Linux

Partitions are much the same in Linux as they are in other operating systems, and have the same meaning and serve the same purpose. In that, they are essentially a "logical container", or a "logical division" of a physical drive of some description, this may be a hard drive, SSD drive, USB flash drive, etc. In other words, partitions serve as a way to logically divide a drive in to different sections, as physically cutting a drive in half for example, would destory it. Linux fully supports both MBR based partitioning, as well as GPT based partitioning. Modern computers use UEFI rather than the traditional BIOS, therefore your computer's internal hard drive, SSD drive, NVME drive or other drive will most likely be using GPT partitioning by default.

MBR-based and GPT partitioning

Master Boot Record (MBR) based partitioning is the older partitioning scheme introduced in 1983, which is limited to a maximum of 4 "primary partitions", along with a 2 TB disk size limit. If you want more than 4 partitions in MBR-based partitioning, you would need to create an "extended partition" along with "logical partitions" within that, therefore you could only have a maximum of 3 "primary partitions" in that case. GPT (GUID Partition Table) partitioning is much newer and does away with the limitations of MBR-based partitioning. For example, in GPT partitioning, there are only "partitions", no "primary", "extended" or "logical partitions". Therefore, you can have as many GPT partitions as drive space will allow, without the restrictions MBR-based partitioning imposes.

Filesystems

In order for an operating system to be able to retrieve data from a disk drive of some description, that data (aka files) needs to be in an organized structure of some sort, this is known as a filesystem. Therefore partitions are "formatted" with a filesystem of some description, Linux-based operating systems like other operating systems, has it's own "native" filesystems. Without a filesystem, the operating system would have no way to know where one file ends, and the next file begins on the disk. Filesystems also tell the operating system what permissions are associated with said files and directories, along with any other attributes files and directories have.

How mounting works in Linux

Unlike some other operating systems, Unix-like and Linux-based operating systems do not automatically mount any filesystems they might find. While it depends on the particular filesystem as to how it would normally be mounted, any filesystems that are mounted on boot would be defined in /etc/fstab However, a user can manually mount a filesystem at any time once the system has booted, and that user has logged in. Each drive in Linux-based operating systems is identified by a "device node", along with each partition on a drive. For example, if we had an SSD drive, this might get a device node like /dev/sda, with the first partition in this case being identified as /dev/sda1 It does depend on the type of drive as to exactly what it's particular device node will be called, as a different type of drive might have it's own naming convention. For example, the first DVD/Blu-ray drive normally gets a device node of /dev/sr0 for one example. In any case, this is how we tell the system which hardware device contains the filesystem we wish to mount. Depending on exactly how said filesystem is being mounted, we also may need to supply other information to the system, but either way, we also need to tell the system exactly where to "attach" said filesystem to, on the filesystem hierarchy. We also need to make sure that the specified directory actually exists in the first place, we also need to make sure said directory is empty.

Mount point

Simply put, a "mount point" is simply a directory somewhere on the filesystem hierarchy another filesystem is "mounted" to, or in other words, is "attached" to. This includes directories defined in the /etc/fstab file, which tells the kernel which filesystems are to be mounted at boot time. This also includes filesystems that are manually mounted after boot by the user, by your desktop environment's "automounter", etc. If the directory you plan to use is not empty before mounting said filesystem, it's existing contents will become inaccessible while said filesystem is mounted. You will also have problems unmounting a filesystem if the directory being used as a mount point is already being used as a mount point.

How to mount a drive in Linux

There are two common ways to mount a filesystem in Linux, this includes both /etc/fstab and the user manually mounting a filesystem after boot using the mount command. However, there are also other ways to mount a filesystem in Linux as well, such as your desktop environment's "automounter", which is commonly used for mounting USB flash drives connected to the system after boot. You can use the device node for said partition, the filesystem's "volume label", or the filesystem's UUID to manually mount a filesystem using the mount command. In the /etc/fstab file, it's strongly recommended that you use the filesystem's UUID to mount it, as there is absolutely no guarantee that the same device node will be assigned to said drive on the next boot. If for example, you're mounting a USB flash drive via your desktop environments "automounter" and it's filesystem doesn't have a "volume label", then the name of the directory (aka it's "mount point") will be the same as the filesystem's UUID. If it does have a "volume label", then the name of the directory created by the "automounter" would be the same as said filesystem's "volume label". You can see the mount wiki page for some examples of how to manually mount a filesystem at the command-line using the mount command, as well as the mountpoint wiki page for more information.

See Also