Linux Mint – Automatically mounting volumes at boot time

How to Install Windows Server 2019

Another scenario we’ve not gone over so far is automatically mounting filesystems when your system is booted. A great example of this is adding a secondary hard disk. As your needs grow, adding more fixed storage may make sense. Your author, for example, recently added a new hard disk to his PC due to the fact that so many virtual machines were installed, and there was no room for them on the primary hard drive. In this example, a secondary hard disk offers the benefit of segregating virtual hard disks onto their own dedicated storage. Another example may include setting up a secondary hard drive for a backup program to use. While another hard drive inside the same physical PC is not technically considered a backup (one good power surge or a fire could render all hard drives inside the same case useless), it’s still better than nothing.

It’s important to note that it’s not required to ensure that secondary storage is mounted at boot as you can always double-click on a secondary hard disk in Nemo when you’re ready to use it. The benefit of having a disk automatically mounted at boot time is that it would then be available right after the booting, so you don’t have to remember to do it. In the preceding example of a virtual machine program storing its files on a secondary disk, the software would show an error message if you tried to use it, and its storage wasn’t available. If you set up the disk to mount during boot, you wouldn’t have to remember to mount the drive before starting the virtual machine software. Another example is backup software that automatically backs up your PC. If your backup disk isn’t mounted, then the backup software wouldn’t be able to access it and complete your backup.

It would become somewhat annoying to manually mount your secondary storage each time you start your PC. If you forgot to do so, the files stored on that hard drive would not be available. A tried-and-true (and very old) method of solving this issue is the /
etc/fstab file. The fstab file is a simple text file containing information for local storage. The file is automatically generated when you install a typical Linux distribution, and it is located where the mountpoints for your root filesystem and any other partitions you created during installation are designated. As an example, execute the following command to see the current contents of the fstab file:


cat /etc/fstab

The following screenshot shows the sample fstab file output:

The output will show storage devices that are currently configured to be mounted automatically. At first, this will consist only of your local storage devices, the one you set up during installation. In the second column, you’ll see the mountpoints for each of your devices places in the filesystem. In the fstab output, the installation uses a separate home and root partition designated by / and /home, respectively. The long series of characters you see on the screen after the term UUID is the Universally Unique Identifier of the drive. We’ll discuss what this means near the end of this chapter. However, for now, just think of it as a generated serial number of the partition that allows the system to differentiate it from others. Finally, you’ll see some additional options such as the filesystem type and the order in which the partition or drive is checked for errors.

Note

The beauty of this file is that it can be edited, allowing you to create entries for additional media earlier and beyond what was created during the installation. Not only does it allow you to automatically mount secondary hard disks, you can also use it to automatically mount network shares and treat them as if they were local devices. Again, the /etc/fstab file is not the only method that you can use in order to mount secondary hard drives. If you have a secondary hard drive installed in your computer, you can simply access it from the GUI without editing any configuration files. However, adding the drive to the /etc/fstab file gives you a bit more control, such as which folder it is mounted in, so you can expect the contents to be not only mounted automatically but always available in the same place.

In order to best describe how to edit the fstab file, let’s use an example. Suppose you purchased an additional hard disk and have already installed it on your computer and formatted it with the ext3 filesystem. After starting your computer, you can execute the following command in order to determine the virtual device file for the hard drive’s partition(s):


sudo fdisk -l

Like we discussed earlier, with flash drives, the output will allow you to determine the virtual device file for your disk. Based on this knowledge, you can then add a mountpoint for the drive by copying an existing entry and placing it at the end of the file. The following command line will suit this example:


/dev/sdb1  /mnt/mydisk  rw,relatime,data=ordered 0 0

In the preceding example, we’re going to mount /dev/sdb1 (the first or only partition of the secondary disk) to the /mnt/mydisk directory. If /mnt/mydisk doesn’t exist, you will need to create it. Note that you can use just about any directory you wish for the mountpoint, thought it has to actually exist. Next, after the mountpoint, we have some options. The rw option is likely self-explanatory; it means that we would like the disk to be mounted, read, and written, so changes can be made to the data it will contain. The latter options control when data is written to the disk. You can learn more about the various fstab options by perusing the output of the man command against fstab ( man fstab).

When you make changes to the /etc/fstab file, the changes are not immediately active. When you restart your computer, the fstab file will be parsed on startup and all entries will be made active. However, the easiest way to test your fstab file is to execute the following command in your terminal:


sudo mount -a

When you execute that command, you may not get any output at all. If not, that’s good—this means that the command didn’t have a problem with your fstab file. If, on the other hand, you do get an output, it will be the errors that will give you a clue as to which line of the fstab file the command has an issue with. This way, you can test your fstab file without having to reboot your computer. This will definitely save your time.

Comments are closed.