Unable to "control" the mounting of servers (or other removable media)

I would suggest using this tutorial:

Additionally, to avoid a long timeout when mounting and the server is not reachable, I would suggest this:

Add ConditionPathExists=/run/samba-server-up to the *.mount file. Now create a service:

File: /etc/systemd/system/ping-server-check.service

[Unit]
Description=Check if Samba Server is reachable
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/ping-server-check.sh

and timer:

File: /etc/systemd/system/ping-server-check.timer

[Unit]
Description=Ping Samba every 30 seconds

[Timer]
OnBootSec=30
OnUnitActiveSec=30
Persistent=true

[Install]
WantedBy=timers.target

and a script:

File /usr/local/bin/ping-server-check.sh

#!/bin/bash
IP="192.168.1.100"
FLAG="/run/samba-server-up"

if ping -c 1 -W 1 "$IP" >/dev/null; then
    touch "$FLAG"
else
    rm -f "$FLAG"
fi

Now, it blocks mounting if the server is not reachable.

1 Like