How to use Hyphens in Systemd Mount Points

Move Fstab mount config that contains hyphens to Systemd.

The Problem

I’d like to move the following /etc/fstab configuration to Systemd:

UUID=73297ccb-8aa8-4e36-96d7-955980097008 /mnt/storage-ssd  xfs rw,nodev,nosuid 0 0
UUID=e90a3068-51b1-454b-b5de-d381305ca6f6 /mnt/storage-k8s  xfs rw,nodev,nosuid 0 0
UUID=2eba5be7-4b63-41da-a413-f30a3a604a13 /mnt/storage-luks xfs rw,nodev,nosuid 0 0

The problem is that all of my mount points contain a hyphen.

The Solution

Tested with systemd-219.

From man systemd.mount:

Mount units must be named after the mount point directories they control. Example: the mount point /home/lennart must be configured in a unit file home-lennart.mount.

From man 5 systemd.unit:

Some unit names reflect paths existing in the file system namespace. Example: a device unit dev-sda.device refers to a device with the device node /dev/sda in the file system namespace. If this applies, a special way to escape the path name is used, so that the result is usable as part of a filename.
Basically, given a path, “/” is replaced by “-” and all other characters which are not ASCII alphanumerics are replaced by C-style “\x2d” escapes (except that “_” is never replaced and “.” is only replaced when it would be the first character in the escaped path). The root directory “/” is encoded as single dash, while otherwise the initial and ending “/” are removed from all paths during transformation.

Create a Systemd service file to mount the SSD disk to /mnt/storage-ssd:

$ sudo touch "/usr/lib/systemd/system/mnt-storage\\x2dssd.mount"

Edit the file and add the following configuration:

[Unit]
Description=Mount SSD disk to /mnt/storage-ssd
DefaultDependencies=no
Conflicts=umount.target
Before=local-fs.target umount.target

[Mount]
What=UUID=73297ccb-8aa8-4e36-96d7-955980097008
Where=/mnt/storage-ssd
Type=xfs
Options=rw,nodev,nosuid

[Install]
WantedBy=local-fs.target

Enable and start the service:

$ sudo systemctl enable --now "mnt-storage\\x2dssd.mount"

Verify status:

$ sudo systemctl status "mnt-storage\\x2dssd.mount"
‚óŹ mnt-storage\x2dssd.mount - Mount SSD disk to /mnt/storage-ssd
   Loaded: loaded (/usr/lib/systemd/system/mnt-storage\x2dssd.mount; enabled; vendor preset: disabled)
   Active: active (mounted) since Sun 2021-03-14 13:09:51 GMT; 4s ago
    Where: /mnt/storage-ssd
     What: /dev/mapper/vg_os-lv_fast
  Process: 23609 ExecMount=/bin/mount UUID=73297ccb-8aa8-4e36-96d7-955980097008 /mnt/storage-ssd -t xfs -o rw,nodev,nosuid (code=exited, status=0/SUCCESS)
    Tasks: 0
   Memory: 0B

Mar 14 13:09:51 kvm1.hl.test systemd[1]: Mounting Mount SSD disk to /mnt/storage-ssd...
Mar 14 13:09:51 kvm1.hl.test systemd[1]: Mounted Mount SSD disk to /mnt/storage-ssd.

2 thoughts on “How to use Hyphens in Systemd Mount Points

Leave a Reply

Your email address will not be published. Required fields are marked *