Skip to content

Instantly share code, notes, and snippets.

@Peregrinox
Last active October 5, 2024 20:41
Show Gist options
  • Save Peregrinox/01cac6054beaad9849b7807d45a08132 to your computer and use it in GitHub Desktop.
Save Peregrinox/01cac6054beaad9849b7807d45a08132 to your computer and use it in GitHub Desktop.
Use Systemd mount units to mount partitions that depend on previous init tasks

https://oguya.ch/posts/2015-09-01-systemd-mount-partition/ https://unix.stackexchange.com/questions/246935/set-systemd-service-to-execute-after-fstab-mount

This started because the fuse mounts on /etc/fstab didn´t process right the "nofail" options and the init of the server will fail if external drive are not present. So I need some alternative command to "dependable" automount some folders or partitions.

find the service that auto-mounts your device

systemctl list-units | grep '/media/Elements' | awk '{ print $1 }'

In my case that command returns:

media-Elements.mount

create basic mount unit

first, install fuse filesystem:

sudo apt-get install bindfs

I want to convert:

/media/Elements/data/mariadb /media/data-mariadb fuse.bindfs force-user=mysql,force-group=mysql,perms=0660:ug+D 0 0

the dash in the name for data-mariadb was a major mistake that caused many errors, but at this point this is how to correct spell names:

systemd-escape -p --suffix=mount "/media/data-mariadb/"

returns "media-data\x2dmariadb.mount" as filename, but that name must be escaped because of filename rules, so:

So, let's start by:

sudo nano /etc/systemd/system/media-data\\x2dmariadb.mount

and fill with:

[Unit]
Description=Mount data directory with permission
Requires=media-Elements.mount
After=media-Elements.mount

[Mount]
What=/media/Elements/data/mariadb
Where=/media/data-mariadb
Type=fuse.bindfs
Options=force-user=mysql,force-group=mysql,perms=0660:ug+D

[Install]
WantedBy=multi-user.target

Start and Enable the unit to start automatically during boot.

$ sudo systemctl daemon-reload
$ sudo systemctl start media-data\\x2dmariadb.mount
$ sudo systemctl enable media-data\\x2dmariadb.mount

You propably noted that I'm mounting a mariadb data partition, for that reason is recomended to edit that service to depend on the mounted data drive https://blog.zencoffee.org/2016/06/customizing-unit-files-systemd/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment