Home Systemd Remounting Service
Post
Cancel

Systemd Remounting Service

In systemd, you can create an remount unit to ensure share stay mounted. This would work perfectly, except, LXC does not support this systemd unit. So instead I created a service that runs a script, and a timer to trigger it. Like a cron, but still using systemd.

It works by adding a file named unmounted to the mount folder anchor. When the share is unmounted, this file will be visible. We can test for the file and remount when it’s found.

First steps is to make the folder and add the file

1
2
mkdir /mnt/share
touch /mnt/share/unmounted

Create the Timer

I want this to run as a system service, so I’m going to add the units to /etc/systemd/system. First we add the timer unit.

1
2
cd /etc/systemd/system
nano remount-share.timer
1
2
3
4
5
6
7
8
9
10
11
12
[Unit]
Description=Trigger remount service
Requires=remount-share.service
After=network-online.target
Wants=network-online.target

[Timer]
OnCalendar=*:0/5
Persistent=true

[Install]
WantedBy=timers.target

Create the Service

Now we can create the service unit to the same directory.

1
nano remount-share.service
1
2
3
4
5
6
7
8
[Unit]
Description=Remount unmounted shares
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=/root/remount-share.sh

Remount Script

We can add the script to the root home directory. I like to put it here because system should be able to access it, and it exists alongside the credentials file.

1
2
3
touch /home/root/remount-share.sh
chmod +x /home/root/remount-share.sh
nano /home/root/remount-share.sh
1
2
3
4
5
6
7
8
9
#!/bin/bash

SHARE=/mnt/share
FILE=$SHARE/unmounted

if [ -f "$FILE" ]; then
 echo "$SHARE unmounted. Attempting to remount..."
 mount $SHARE
fi

Enable Services and Verify

1
2
3
4
systemctl daemon-reload
systemctl enable remount-share.timer
systemctl start remount-share.timer
systemctl status remount-share.timer && systemctl status remount-share.service

Run a script to do it for me

All of this has been scripted to make it more convenient.

Don’t just take my word for it. Always inspect the code that will be running on your machines, especially from an untrusted and unsigned source.

1
curl https://gist.githubusercontent.com/binarypatrick/d96331537d2976c3a05ce335b00697ca/raw | sudo bash -s -- "some_share_name"
This post is licensed under CC BY 4.0 by the author.