How To Mount Volumes Using Systemd Unit File

UNDERSTANDING THE SUBJECT MATTER

For a better understanding, this article is going to be a sequel to one of the articles on this site, managing services on systemd systems with systemd unit file

systemd, as I mentioned in this same article uses unit files for its management. Asides for the fact that systemd can handle services management, it can also manage mount and automount of Linux filesystems, just as “/etc/fstab” would manage filesystems.

As a matter of fact, “/etc/fstab” can’t handle or manage the mount operation of some filesystems. On my system, let’s have a look at the /etc/fstab file.

[root@HQEBPRD system]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Tue Dec 17 03:28:04 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=a67fe375-f3c3-4907-b292-8b819a175cfd /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
UUID=9524f82c-0dfa-4f47-97c8-2af6deee5005 swap  swap    defaults       0 0
[root@HQEBPRD system]#

you can see the filesystems /etc/fstab is managing. The likes “/”, “/boot” and “swap”.

Again, let us have a look at the mounted filesystems on my system.

[root@HQEBPRD system]# df -h

Filesystem             Size  Used Avail Use% Mounted on
devtmpfs               886M     0  886M   0% /dev
tmpfs                  904M     0  904M   0% /dev/shm
tmpfs                  904M  9.4M  894M   2% /run
tmpfs                  904M     0  904M   0% /sys/fs/cgroup
/dev/mapper/rhel-root   17G   12G  5.3G  69% /
/dev/sda1             1014M  180M  835M  18% /boot
/dev/sr0               7.4G  7.4G     0 100% /run/media/root/RHEL-8-1-0-BaseOS-x86_64
tmpfs                  181M  1.2M  180M   1% /run/user/42
tmpfs                  181M  4.0K  181M   1% /run/user/0
[root@HQEBPRD system]#

you can see the “devtmpfs” filesystem mounted on “/dev”, “tmpfs” filesystem mounted on “/dev/shm” and so on. These filesystems are not defined in the /etc/fstab file, so are not managed by “/etc/fstab”.

I guess the next question now would be, “how are these filesystems mounted on the system”?

As I have said earlier, systemd can do beyond services management. It also does the management of mount and automount of volumes on the system, of course with the use of unit files, just the same way we saw it In the previous article

Using systemd to mount volumes can be done in two ways. One way is with the use of mount unit file (mount) and the second way is with the use of automount unit file (automount).

Let’s have a look at systemd’s mount unit files.

[root@HQEBPRD ~]# cd /usr/lib/systemd/system

[root@HQEBPRD system]#
[root@HQEBPRD system]# ls *.mount

dev-hugepages.mount  proc-fs-nfsd.mount             sys-fs-fuse-connections.mount  sys-kernel-debug.mount  var-lib-machines.mount
dev-mqueue.mount     proc-sys-fs-binfmt_misc.mount  sys-kernel-config.mount        tmp.mount               var-lib-nfs-rpc_pipefs.mount

you can see the mount unit files, the dev, tmp, etc, they all have “.mount” extension. This says it all. Systemd is responsible for the mount of the filesystems that are not defined in the fstab but mounted on my system.

Again, let’s have a look at the tmp.mount file.

[root@HQEBPRD system]# cat tmp.mount
#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Temporary Directory (/tmp)
Documentation=man:hier(7)
Documentation=https://www.freedesktop.org/wiki/Software/systemd/APIFileSystems
ConditionPathIsSymbolicLink=!/tmp
DefaultDependencies=no
Conflicts=umount.target
Before=local-fs.target umount.target
After=swap.target

[Mount]
What=tmpfs
Where=/tmp
Type=tmpfs
Options=mode=1777,strictatime,nosuid,nodev

# Make 'systemctl enable tmp.mount' work:
[Install]
WantedBy=local-fs.target
[root@HQEBPRD system]#
systemd unit file

you can see that this script is responsible for mounting the tmpfs filesystem on the /tmp mount point. This is how systemd uses unit files for its management.

Going forward, let’s look at the automount unit files.

[root@HQEBPRD ~]# cd /usr/lib/systemd/system

[root@HQEBPRD system]# ls *.automount

proc-sys-fs-binfmt_misc.automount

[root@HQEBPRD system]#

On my system, I only have one automount unit file, let’s see the content of the file.

[root@HQEBPRD system]# cat proc-sys-fs-binfmt_misc.automount
#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Arbitrary Executable File Formats File System Automount Point
Documentation=https://www.kernel.org/doc/html/latest/admin-guide/binfmt-misc.html
Documentation=https://www.freedesktop.org/wiki/Software/systemd/APIFileSystems
DefaultDependencies=no
Before=sysinit.target
ConditionPathExists=/proc/sys/fs/binfmt_misc/
ConditionPathIsReadWrite=/proc/sys/

[Automount]
Where=/proc/sys/fs/binfmt_misc

The file, just as the mount unit file, is easily interpreted and understandable.

As I have said earlier, to mount volumes, you have two options. You can either use the mount unit file or the automount unit file depending on your preference.

The most important thing is to follow the format and template of the unit files. All object unit files have templates and it must be followed.

Having understood the subject matter, let’s see how volumes can be mounted using systemd in the “ACTION TIME” section.

ACTION TIME

How To Mount and Unmount Volumes Using Systemd With Mount Unit File (Examples)

To mount a volume, sdb1 on the mount point, /tekneed, take the following steps.

1. create a mount unit file in the directory “/etc/systemd/system” directory

[root@HQPRD2 system]# cd /etc/systemd/system/
[root@HQPRD2 system]# vi tekneed.mount
[Unit]
Description=mounting with systemd

[Mount]
What=/dev/sdb1
Where=/tekneed
Type=ext4

[Install]
WantedBy=multi-user.target

NB: make sure that the unit file name,tekneed.mount corresponds with the mount point, /tekneed. The corresponding name here is tekneed.

2. To mount the volume, start the service.

[root@HQPRD2 system]# systemctl start tekneed.mount

3. Verify that the service is started

[root@HQPRD2 system]# systemctl status tekneed.mount

● tekneed.mount - mounting with systemd
   Loaded: loaded (/etc/systemd/system/tekneed.mount; disabled; vendor preset: disabled)
   Active: active (mounted) since Mon 2020-03-23 16:32:25 PDT; 6min ago
    Where: /tekneed
     What: /dev/sdb1
  Process: 8585 ExecMount=/bin/mount /dev/sdb1 /tekneed -t ext4 (code=exited, status=0/SUCCESS)

Mar 23 16:32:25 HQPRD2 systemd[1]: Mounting mounting with systemd...
Mar 23 16:32:25 HQPRD2 systemd[1]: Mounted mounting with systemd.
[root@HQPRD2 system]#

4. Verify that the volume is mounted

[root@HQPRD2 system]# df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        38G   15G   24G  38% /
devtmpfs        900M     0  900M   0% /dev
tmpfs           910M     0  910M   0% /dev/shm
tmpfs           910M  9.6M  901M   2% /run
tmpfs           910M     0  910M   0% /sys/fs/cgroup
/dev/sda1       297M  127M  171M  43% /boot
tmpfs           182M     0  182M   0% /run/user/0
/dev/sdb1       2.0G  6.0M  1.9G   1% /tekneed
[root@HQPRD2 system]#

5. To make it persistent, enable the service

[root@HQPRD2 system]# systemctl enable tekneed.mount

Created symlink from /etc/systemd/system/multi-user.target.wants/tekneed.mount to /etc/systemd/system/tekneed.mount.
[root@HQPRD2 system]#

6. To make it non persistent, you can disable it again.

[root@HQPRD2 system]# systemctl disable tekneed.mount

Removed symlink /etc/systemd/system/multi-user.target.wants/tekneed.mount.
[root@HQPRD2 system]#

7. To unmount the volume, stop the service

[root@HQPRD2 system]# systemctl stop tekneed.mount

8. Verify that the volume has been unmounted

[root@HQPRD2 system]# df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        38G   15G   24G  38% /
devtmpfs        900M     0  900M   0% /dev
tmpfs           910M     0  910M   0% /dev/shm
tmpfs           910M  9.6M  901M   2% /run
tmpfs           910M     0  910M   0% /sys/fs/cgroup
/dev/sda1       297M  127M  171M  43% /boot
tmpfs           182M     0  182M   0% /run/user/0

How To Mount and Unmount Volumes Using Systemd With automount Unit File (Examples)

To mount a volume, sdb1 on the mount point, /tekneed, take the following steps.

1. create a mount unit file in the directory “/etc/systemd/system” directory

[root@HQPRD2 system]# vi /etc/systemd/system/tekneed.mount
[Unit]
Description=mounting with systemd

[Mount]
What=/dev/sdb1
Where=/tekneed
Type=ext4

[Install]
WantedBy=multi-user.target

NB: make sure that the unit file name,tekneed.mount corresponds with the mount point, /tekneed. The corresponding name here is tekneed.

2. create an automount unit file that corresponds with the mount unit file in the directory, “/etc/systemd/system”

[root@HQPRD2 system]# vi /etc/systemd/system/tekneed.automount
[Unit]
Description=automounting with systemd

[Automount]
Where=/tekneed
Type=ext4

[Install]
WantedBy=multi-user.target

3. To mount the volume, start the service.

[root@HQPRD2 system]# systemctl start tekneed.mount

4. Verify that the service is started

[root@HQPRD2 system]# systemctl status tekneed.automount

● tekneed.automount - automounting with systemd
   Loaded: loaded (/etc/systemd/system/tekneed.automount; disabled; vendor preset: disabled)
   Active: active (waiting) since Mon 2020-03-23 17:23:00 PDT; 6min ago
    Where: /tekneed

Mar 23 17:23:00 HQPRD2 systemd[1]: [/etc/systemd/system/tekneed.automount:6...t'
Mar 23 17:23:00 HQPRD2 systemd[1]: Set up automount automounting with systemd.
Hint: Some lines were ellipsized, use -l to show in full.
[root@HQPRD2 system]#

5. Verify that the volume is mounted

[root@HQPRD2 system]# df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        38G   15G   24G  38% /
devtmpfs        900M     0  900M   0% /dev
tmpfs           910M     0  910M   0% /dev/shm
tmpfs           910M  9.7M  901M   2% /run
tmpfs           910M     0  910M   0% /sys/fs/cgroup
/dev/sda1       297M  127M  171M  43% /boot
tmpfs           182M     0  182M   0% /run/user/0
/dev/sdb1       2.0G  6.0M  1.9G   1% /tekneed

6. you can change directory to the mount point

[root@HQPRD2 system]# cd /tekneed

[root@HQPRD2 tekneed]# ls

lost+found
[root@HQPRD2 tekneed]#

7. To make it persistent, enable the service

[root@HQPRD2 system]# systemctl enable tekneed.automount

Created symlink from /etc/systemd/system/multi-user.target.wants/tekneed.automount to /etc/systemd/system/tekneed.automount.

8. To make it non persistent, you can disable it again.

[root@HQPRD2 tekneed]# systemctl disable tekneed.automount

Removed symlink /etc/systemd/system/multi-user.target.wants/tekneed.automount.
[root@HQPRD2 tekneed]#

9. To unmount the volume, stop the service

[root@HQPRD2 tekneed]# systemctl stop tekneed.automount

[root@HQPRD2 tekneed]#

10. Verify that the device has been unmounted

[root@HQPRD2 system]# df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        38G   15G   24G  38% /
devtmpfs        900M     0  900M   0% /dev
tmpfs           910M     0  910M   0% /dev/shm
tmpfs           910M  9.6M  901M   2% /run
tmpfs           910M     0  910M   0% /sys/fs/cgroup
/dev/sda1       297M  127M  171M  43% /boot
tmpfs           182M     0  182M   0% /run/user/0

RHCSA Exam Practice Question On How To Mount Volumes Using systemd unit file

Your feedback is welcomed. If you love others, you will share with others

Be the first to comment

Leave a Reply

Your email address will not be published.


*