Skip to content

Commit e404a85

Browse files
author
Patrick M
committed
add post
1 parent ca6cc68 commit e404a85

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
layout: post
3+
title: "systemd Remounting Service"
4+
date: 2025-07-07 -0500
5+
category: "General"
6+
tags: "lxc"
7+
---
8+
9+
## `systemd` Auto-mounting Service
10+
11+
In `systemd`, you can create an remount unit to ensure share stay mounted. This would work perfectly, exepct, LXC does not support this `systemd` unit. So instead I created a service that runs a script, and a timer. Like a cron, but still using `systemd`
12+
13+
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.
14+
15+
First steps is to make the folder and add the file
16+
17+
```bash
18+
mkdir /mnt/share
19+
touch /mnt/share/unmounted
20+
```
21+
22+
## Create the Timer
23+
24+
I want this to run as a system service, so I'm going to add the units to `/etc/systemd/system`.
25+
26+
```bash
27+
cd /etc/systemd/system
28+
nano remount-share.timer
29+
```
30+
31+
```ini
32+
[Unit]
33+
Description=Trigger remount service
34+
Requires=remount-share.service
35+
After=network-online.target
36+
Wants=network-online.target
37+
38+
[Timer]
39+
OnCalendar=*:0/5
40+
Persistent=true
41+
42+
[Install]
43+
WantedBy=timers.target
44+
```
45+
46+
## Create the Service
47+
48+
Now we can create the service unit
49+
50+
```bash
51+
nano remount-share.service
52+
```
53+
54+
```ini
55+
[Unit]
56+
Description=Remount unmounted shares
57+
After=network-online.target
58+
Wants=network-online.target
59+
60+
[Service]
61+
Type=oneshot
62+
ExecStart=/root/remount-share.sh
63+
```
64+
65+
## Remount Script
66+
67+
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.
68+
69+
```bash
70+
nano /home/root/remount-share.sh
71+
```
72+
73+
```bash
74+
#!/bin/bash
75+
76+
SHARE=/mnt/share
77+
FILE=$SHARE/unmounted
78+
79+
if [ -f "$FILE" ]; then
80+
echo "$SHARE unmounted. Attempting to remount..."
81+
mount $SHARE
82+
fi
83+
```
84+
85+
## Enable Services and Verify
86+
87+
```bash
88+
systemctl daemon-reload
89+
systemctl enable remount-share.timer
90+
systemctl start remount-share.timer
91+
systemctl status remount-share.timer && systemctl status remount-share.service
92+
```
93+
94+
## Run a script to do it for me
95+
96+
All of this has been scripted to make it more convenient.
97+
98+
> 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.
99+
{: .prompt-warning }
100+
101+
```bash
102+
curl https://gist.githubusercontent.com/binarypatrick/c3bf8b572158655f438fa0843ffc9f2f/raw | sudo bash
103+
```

0 commit comments

Comments
 (0)