Mounting drives or shares in Linux

Radu Zaharia

--

Photo by lilzidesigns on Unsplash

Ever since I started using Linux as my main operating system I was immediately introduced to the concept of drive mount. It has to do with the Linux file system and the way it handles disk access. Windows for example handles a drive like an independent unit, assigning a drive letter for each disk or network share. There is drive C and drive D: they are handled like totally separate entities. But Linux integrates every drive and network share into its file system by a process called mounting.

In time I learned quite a few ways to mount a resource in the Linux file system and I would like to share them here because some of them felt quite new and interesting.

The mount command

Photo by Gabriel Heinzer on Unsplash

Of course, the most basic method has to be opening a terminal and writing a command. In this case, the command is called mount:

#sudo mkdir /mnt/media
#sudo mount 192.168.68.116:/mnt/storage/media /mnt/media

First we create a folder where we want to mount the resource (drive or network share, in this case it’s a NFS network share) and then we run the mount command. Both commands must be ran as admin because they tweak the root Linux file system. If we have a secondary SSD we want to mount, the command would look like this:

#sudo mount /dev/sdb1 /mnt/media

The fstab file

Photo by Hello I'm Nik on Unsplash

Instead of always manually mounting a network share or a disk, we can instruct Linux to mount it automatically at boot. This is done by editing the fstab file. All mounts defined here will happen during boot, before any user has yet logged in, so they will be available for everybody.

The file is located at /etc/fstab and looks like this:

#
# /etc/fstab
# Created by anaconda on Sat Jan 1 03:57:29 2022
#

UUID=... / btrfs subvol=root,compress=zstd:1 0 0
UUID=... /boot ext4 defaults 1 2
UUID=... /boot/efi vfat umask=0077,shortname=winnt 0 2
UUID=... /home btrfs subvol=home,compress=zstd:1 0 0

You have the disk UUID identifier, the mount location (/home), the file system type (btrfs) and some mount options. We can add here at the bottom a new line for our NFS network share. It doesn’t have an UUID as an identifier, but the resource locator 192.168.68.116:/mnt/storage/media. The mount location will be /mnt/media as before and the file system is NFS. We can assign mount options here so we will use rw to mount for reading and writing. This is how /etc/fstab would look:

#
# /etc/fstab
# Created by anaconda on Sat Jan 1 03:57:29 2022
#

UUID=... / btrfs subvol=root,compress=zstd:1 0 0
UUID=... /boot ext4 defaults 1 2
UUID=... /boot/efi vfat umask=0077,shortname=winnt 0 2
UUID=... /home btrfs subvol=home,compress=zstd:1 0 0
192.168.68.116:/mnt/storage/media /mnt/media nfs rw

If we would have an ext4 formatted secondary SSD, we would use:

#
# /etc/fstab
# Created by anaconda on Sat Jan 1 03:57:29 2022
#

UUID=... / btrfs subvol=root,compress=zstd:1 0 0
UUID=... /boot ext4 defaults 1 2
UUID=... /boot/efi vfat umask=0077,shortname=winnt 0 2
UUID=... /home btrfs subvol=home,compress=zstd:1 0 0
/dev/sdb1 /mnt/media ext4 defaults 1 2

Automount

Photo by veeterzy on Unsplash

Both mount command and fstab will mount the drives and keep them mounted until explicitly told to unmount using the umount command. But there is a different kind of mounting that does so on demand, meaning it will mount the resource when needed and then after an idle time, the resource will be automatically unmounted. This is called automount.

Automount configuration is done using a hierarchy of configuration files. Like a tree we configure the root and then we configure branches, subbranches and so on as we will see in a moment. After we give the list of mount resources to automount, it takes care of both mounting on access and unmounting on idle for us. Automount usually has to be installed first:

#sudo dnf install autofs
#sudo systemctl enable autofs

Automount has a master configuration file located at /etc/auto.master where we put the root folders of our mounts:

/mnt    /etc/mnt.autofs    --timeout=40

The mount entries in /etc/mnt.autofs will all be mounted in /mnt with an unmount timeout of 40 minutes. And now in /etc/mnt.autofs we add:

media    -fstype:nfs    192.168.68.116:/mnt/storage/media

This configuration together with auto.master instructs automount to mount the NFS share located at 192.168.68.116:/mnt/storage/media to the media subfolder of the /mnt folder, so basically to /mnt/media.

Now that automount is configured, we need to restart the autofs service:

#sudo systemctl restart autofs

And if we access the /mnt/media folder by doing a simple ls /mnt/media, automount will kick in and perform the mount operation for us. Also, 40 minutes later, /mnt/media will be automatically unmounted.

Systemd mount

Photo by Possessed Photography on Unsplash

Systemd also has the ability to take over both normal and automatic mounts. This allows us to condition mounting much better and create complex systemd unit dependency trees that include mount points.

If we want to mount /mnt/media we need to create a mnt-media.mount file in /etc/systemd/system and put the mount description there:

[Unit]
Description=Media network share

[Mount]
What=192.168.68.116:/mnt/storage/media
Where=/mnt/media
Type=nfs
Options=rw

[Install]
WantedBy=multi-user.target

The mount description contains the same familiar elements: the mount location, the NFS share, mount type and mount options. Now we just need to enable this systemd mount service:

#sudo systemctl enable mnt-media.mount
#sudo systemctl start mnt-media.mount

Now that the mount point is defined as a systemd service, if we add another file mnt-media.automount the /mnt/media mount point will become an automount, with mount on demand and unmount on idle, all fully managed by systemd. This is what the mnt-media.automount file should look like:

Description=Automount for the Media network share

[Automount]
Where=/mnt/media

[Install]
WantedBy=multi-user.target

Note that the description above does not have the mount type and other mount information as they are already provided by mnt-media.mount. Now we can enable the automount configuration:

#sudo systemctl enable mnt-media.automount
#sudo start mnt-media.automount

Gnome Virtual File System

Photo by Brigitta Schneiter on Unsplash

There is one more rather peculiar mounting option. All the above mount methods were working straight with the root Linux file system and required root privileges to execute. The Gnome Virtual File System or GVFS (there is something similar for KDE) works entirely in user space. Its main command is gio which has mount and unmount options.

It is used by the Gnome environment to handle USB volumes that are randomly plugged in or ejected from the system, optical drives and network shares. For example if you define discoverable network shares on a network server, they will all show up in Gnome Files in the Other Locations area. If you click on a SMB share for example there, it will be automatically mounted in the GVFS without you having to enter any administration password.

The mount location is determined automatically by the mount type, but all are placed in /run/user/[UID]/gvfs folder. I wrote a bit about gio and GVFS here if you are interested to know more about this virtual file system.

And here we are at the end of the article. I hope you found it useful and were able to learn something new about how Linux works. Feel free to use the comments for any clarification or question. See you in the next article!

--

--

Responses (3)

Write a response