Mount using systemd

Feb 07

systemd mount units


Auto mounting a device using systemd is a simple secure approach.

For one - you don't have to edit your fstab - the slightest error in fstab can render your system unbootable.

Lets assume you have an USB device which may or may not be available at boot time and when you plug it you want it to be mounted on a special path - it could be dedicated backup disk.

Mount define


There is one mandatory rule - the name of the file. The file must be named as the path it mounts to. If your mount path is /data/backup the file must be named data-backup.mount.

Create a file as root

$ sudo touch /etc/systemd/system/data-backup.mount

Open the file with your favorite CLI editor or a gvfs compliant editor (xed or gedit)

$ xed admin:/etc/systemd/system/data-backup.mount

Template

Insert the following template into the file - and modify the description

[Unit]
Description=Mount Backup disk (/data/backup)
#DefaultDependencies=no
#Conflicts=umount.target
#Before=local-fs.target umount.target
#After=swap.target

[Mount]
What=/dev/disk/by-uuid/
Where=
#Type=
#Options=

[Install]
WantedBy=multi-user.target

Mount

The section [Mount] is the same data as you see in your fstab - except the What which must the full path to the device. The nature of systemd makes it impossible to rely on the traditional path /dev/sda1 - luckily there is another /dev/disk/by-uuid/ we can use.

What

To get the device uuid you can use lsblk command and the -no UUID to retrieve the uuid you need for the mount.

$ lsblk -no UUID /dev/sdy1
67f922cd-a61f-4d5e-84c0-ac8335a7ce67

Then insert the UUID into the file

What=/dev/disk/by-uuid/67f922cd-a61f-4d5e-84c0-ac8335a7ce67

Where

Next type in the path where you want the device mounted. Normally we would create the path beforehand as this is required for fstab - but not with systemd. If the path do not exist it will be created.

default permissions is 755 owned by root

Where=/data/backup

Type

The Type is optional. If you don't know you can retrieve the file system the same way as the UUID.

$ lsblk -no FSTYPE /dev/sdy1
ext4
Type=ext4

Options

The Options is optional. As with fstab it takes a comma separated list of options for the filesystem mounted.

Options=defaults,rw,noatime

Save the file

Test mount


Reload the systemd daemon

$ sudo systemd daemon-reload

Note the state of the mount

$ systemctl status data-backup.mount

Mount the device

$ sudo systemctl start data-backup.mount

Now you can verify the service

$ systemctl status data-backup.mount

Note that the folder was created by systemd - list the content of the mount

note permissions changed to partition content

$ ls -la /data/backup

To demonstrate the automount - first unmount the data-mount

$ sudo systemctl stop data-mount.mount

Verify the content of the mount point - it should now be empty.

$ ls -la /data/backup

Automount


Now we proceed to create an automount for the mount. Create a file - it must be named using the same rule as the mount but with the extension of .automount

$ sudo touch /etc/systemd/system/data-backup.automount

the automount service will fail if the device path is mounted

Automount define

Edit the file and insert the following content and save it.

[Unit]
Description=Automount backup partition
ConditionPathExists=/data/backup

[Automount]
Where=/data/backup
TimeoutIdleSec=10

[Install]
WantedBy=multi-user.target

Test automount


Now enable and start the automount for data-backup

$ sudo systemctl enable --now data-backup.automount

You know you unmounted the device - don't mount it - just access the /data/backup folder from terminal or a file manager - you will find systemd mounts it behind the scenes.

Conclusion


This approach is a safer way than editing your fstab. Worst case the device is not mounted - if you make a mistake in fstab - the system breaks.

Next Post Previous Post