Allosaurus' Den Almost geek.

File systems, mount and fstab

| 1303 words | Linux explain

File systems, mount and fstab

Introduction to file system

​ If you are a windows user before(just like me), you might wonder why the file path on Linux is so different from that on windows.

​ The questions as

“Where are my drive C and drive D?”

and

“Why is there a / folder?”

will appear.

Don’t worry, a little bit of knowledge of file systems can answer your questions. Just be patient. lol.

What is a file system?

From Wikipedia:

​ A file system controls how data is stored and retrieved…By separating the data into pieces and giving each piece a name, the information is easily isolated and identified.

​ For example, the C:/ or D:/ is how windows help you deal with data stored in the devices ( HDD, SSD, USB, etc…). Windows names ( you will know a more accurate term, mounts, in following section) those devices with specific names like C:/, D:/, or even U:/ so you don’t need to remember what the drive actually called in computer world(like the ugly UUIDs).

​ Then we can answer the questions above. In fact, there are several file system used from all around the world. Linux use a different file system from Windows. The main file system in Windows is called NTFS, while Linux’s file systems are ext3 or ext4(you can use other file system in Linux, I just list the most common ones). And your USB may be formatted as exFAT.

​ Aside from those, they might both support other file systems, depending on your system distros or versions.

Tips : You can see which file systems are supported in your system by running command cat /proc/filesystems.

​ The detail of how a file system work and its implementation are out-of-scope in this article. I am not going to elaborate them, but you can check my other article: Diving into filesystems.

​ So you now know what a file system is. The next topic is, how can we get access to the saved data with the help of file systems?

​ Let me introduce mount to you.

What does mount mean?

​ If you are not a CS newbie, you might have already heard(or even been familiar with) of this term.

​ As we have mentioned above, the operating system names the devices, but it would be too ambiguous(also it’s not cool enough) to use the word name. Instead, we use the term mount.

​ For instance, in Linux system, the main system partition will be mounted at /, which is a root directory. And that partition (or system volume) is called a mount-point.

​ Likewise, your windows system will mount the system partitions and volumes. But most of the time, you don’t need to do that by yourself. Windows will take care of everything for you, including your Windows partition, external drives, etc…

​ However, on Linux, it’s another story. Many of the distros you use will not automatically mount volumes for you, and that’s how things got complicated.

Mounting in Linux

​ The file system in Linux system arranges all the files in a tree. There is a command called mount, which serves to attach the file system found on some device to the big file tree.

etc
|-- abrt
|   |-- abrt-action-save-package-data.conf
|   |-- abrt.conf
|   |-- gpg_keys
|   `-- plugins
|       |-- CCpp.conf
|       `-- python.conf
|-- acpi
|   |-- actions
|   |   `-- power.sh
|   `-- events
|       |-- power.conf
|       `-- video.conf
|-- adjtime
|-- aliases
|-- aliases.db
|-- alsa
|   `-- alsactl.conf
.....
....
..
..
|-- xinetd.d
|   `-- rsync
|-- xml
|   `-- catalog
|-- yum
|   |-- pluginconf.d
|   |   |-- product-id.conf
|   |   |-- protectbase.conf
|   |   |-- rhnplugin.conf
|   |   `-- subscription-manager.conf
|   |-- protected.d
|   |-- vars
|   `-- version-groups.conf
|-- yum.conf
`-- yum.repos.d
    |-- epel.repo
    |-- epel-testing.repo
    |-- ksplice-uptrack.repo
    |-- redhat.repo
    `-- rhel-source.repo

208 directories, 1452 files

Tips : you can check with your own /etc with command tree /etc

​ The basic usage of the mount command is:

# you need sudo priveliges
$ sudo mount -t <type> <device> <dir>

​ This tells the kernel to attach the file system found on <device>(which is of type <type>) at the directory <dir>.

​ For example, if we have a partition called /dev/sda1 , you can mount it at /run/media/me/myusb, like this:

$ sudo mkdir -p /run/media/me/myusb
$ sudo mount -t vfat /dev/sda1 /run/media/me/myusb
$ sudo chown -R <user> /run/media/me/myusb
# <user> is your user name

​ Then access to the files inside /run/media/me/myusb.

​ You might ask, “why don’t I have to mount my system partition?". Well, by some preset configuration, the system will automatically do this job for you. The key is in fstab file.

What is fstab?

fstab is a file on your Linux system, which is located at /etc/fstab. The file fstab contains descriptive information about the file systems the system can mount. All specified devices within it will be automatically mounted on startup.

​ So, what is fstab like? Here is a simple fstab:

# /etc/fstab
# <device>                                <dir> <type> <options> <dump> <fsck>
UUID=0a3407de-014b-458b-b5c1-848e92a327a3 /     ext4   noatime   0      1
UUID=f9fe0b69-a280-415d-a03a-a32752370dee none  swap   defaults  0      0
UUID=b411dc99-f0a0-4c87-9e05-184977be8539 /home ext4   noatime   0      2

Let’s dig into this table:

  • describes the device that needs to be mounted. With the device’s UUID(like UUID=0A3407de-014b-458b-b5c1-848e92a327a3), you can simply fill in this field. Aside from UUIDs, you can also use kernel name descriptors like /dev/sda1. However, since those names are not alway persistent, It’s best practice to use UUIDs(though they look pretty weird).

Tips: Don’t know how to check UUIDs? use command lsblk -f or blkid!

  • specifies the file system type. For example, swap for a swap partition(or file) and ext4 for fourth extended file system. If you really don’t know which type you should use, set it to auto.
  • is related to some file system related options. For more information, see this.
  • is checked by the dump utility and in most situation, you might probably would like it to be 0.
  • sets the order for file system checks at boot time. You should always set your root device to 1, which means checking it first. For other devices, you can set them to 2, or 0to disable checking.

Should I alway list all of my devices in fstab?

​ You don’t have to. Just remember to list the important devices that need to be mounted automatically when you start your computer, for instance, your system partition, swap partition, etc… If you don’t list them correctly, it might result in an error. For other devices, you can manually mount them when you boot into your system.

​ For some portable devices like an USB, if you put the device’s UUID in /etc/fstab, then you forgot to plug it into your computer, the system might take considerable time to search for the device while booting. Therefore, you don’t always need to have system automatically mount the device for you.

Why can’t I read or write the files in my USB?

​ This is a common questions for Linux newbies. A device mounted isn’t always editable for non-root users since you need sudo privileges to mount that device. So the device(or folder, as we already mounted it)‘s owner is default to root. You have to change the owner or grant yourself more privileges.

​ See this article: Chmod and Chown - a way to gain previleges

Bonus - an auto-mounting script.

​ If you are like me that need to use USBs or some external hard-drives on a daily basis, this script could be a really life-saver. It’s a bundle of commands and aims to mount your devices more easily. ​ Here is the link.

References

*fstab - Arch Wiki
*mount(8) - Linux Manual
*File systems - Arch Wiki

Unless otherwise noted, the content of this site is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License (CC BY-NC-SA 4.0).