Mounting filesystems using systemd mount units
Mount ext4 by label
In this example we mount an ext4 filesystem with label EXT1 to /mnt/EXT1.
If you need to find out the label:
root@myserver:/# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
xvda
└─xvda1 xfs / a1e1011e-e38f-408e-878b-fed395b47ad6 /
xvdb
xvdc
└─xvdc1 ext4 EXT1 978546e8-0923-4f33-b3f8-c3a1941f0379
xvdd
#
Note that the name of the unit file is important and should be mnt-EXT1.mount
vi /etc/systemd/system/mnt-EXT1.mount
[Unit]
Before=local-fs.target
[Mount]
Where=/mnt/EXT1
What=/dev/disk/by-label/EXT1
Type=ext4
Options=defaults,noatime
[Install]
WantedBy=local-fs.target
Mount ext4 by UUID
In this example we mount the disk by its UUID.
The UUID can be found using lsblk -f
command (see an example in previous section)
[Unit]
Before=local-fs.target
[Mount]
Where=/mnt/EXT1
What=/dev/disk/by-uuid/978546e8-0923-4f33-b3f8-c3a1941f0379
Type=ext4
Options=defaults,noatime
[Install]
WantedBy=local-fs.target
Reload the sysetmd configuration and start the mount
systemctl daemon-reload
systemctl start mnt-EXT1.mount
Make it mounted after reboot
systemctl enable mnt-EXT1.mount
Create an automount option
Instead of mounting after reboot you can use an automount configuration (mount on first access):
vi /etc/systemd/system/mount-EXT1.automount
[Unit]
Description=Automount /mnt/EXT1
#Requires=network-online.target
#After=network-online.target
[Automount]
Where=/mnt/EXT1
TimeoutIdleSec=30
[Install]
WantedBy=multi-user.target
Mount Unit names when mountpoint contains dashes
If your mountpoint contain the dashes you need to replace the dashes within the mount point with \x2d escape sequence
For example, you want to mount to /mnt/d08-test
cd /etc/systemd/system
vi mnt-d08\\x2dtest.mount