[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: systemd automount unit: run only when server is reachable



On Mon 12 Sep 2022 at 09:31:07 (+0200), Jürgen Bausa wrote:

> I am using systemd automount units (see below) to mount network shares on my laptop
> (debian bullseye). This works fine in principle but I have one big issue:
> 
> At home it is enough to set TimeoutSec to 2 s in the mount unit. Normally the server is
> available and the share is mounted. If the server is down I need to wait for just 2 s
> until I see it is not there. Thats ok.
> 
> But when not at home and using a vpn, the mount unit will not mount with TimeoutSec set to 2 s.
> I need to set it to at least 10 s. Then the mount works. But using 10 s means I always have 
> to wait 10 s for each share the system tries to reach and is not available. This is really
> annoying when starting libreoffice for example (which seems to check for the last used
> documents on startup).
> 
> What I would like to do is to put a test for server availabilty (e.g. ping -c 1 $SERVER) 
> into the automount file. When the server is not available, automount is not run.
> Is this possible? Or do I need to create a spcial unit and put something like
> 
>     Requires=nfs-server-online.target
> 
> in my automount unit? And how would the nfs-server-online unit look like?
> 
> What I am doing at the moment is running a script that checks availability of the nfs server
> every some seconds (via ping) and turns on/off the automount unit accordingly
> (via systemctl start/stop mnt-share.automount). This works, but its not a very elegant solution.
> I am pretty sure it can be done better using systemd only.

I don't remember the details of the complaint, but there are
circumstances where systemd kills off jobs that it is controlling.

You could investigate using cron's @reboot to fire off a number
of tasks, each mounting one of the shares.

I use it myself to restart minidlna after I've got around to
decrypting and mounting /home, which could be any time or not
at all:

# crontab -l
# /root/.cron/crontab-axis last edited 2022-04-25
# Note that this is root's own crontab so it doesn't need the
# user field but it does require installing with crontab.

MAILTO= …

# check for (dist-)updated packages and provoke an email if any are in the cache
0       */3     *       *       *       apt-get -qq -o Acquire::http::Proxy="http://192.168.1.14:3142/"; update && apt-get -qq -d -o Acquire::http::Proxy="http://192.168.1.14:3142/"; dist-upgrade && find /var/cache/apt/archives/ -name '*deb'

# restart streamer when mounting /home gives it access to served files
@reboot /root/.cron/mystreamers-restart.sh

 … …

# cat .cron/mystreamers-restart.sh 
#!/bin/bash
# /root/mystreamers-restart.sh last edited 2022-05-15
# wait for real /home to be mounted (its lost+found appears)
# read the directories to be served
# set the group permissions to allow minidlna to serve the files
# restart the service

Conf=/etc/minidlna.conf
Log=/var/log/minidlna/minidlna.log

while :; do
    if [ ! -d /home/lost+found ]; then
        sleep 60
    else
        if [ -f "$Conf" ]; then
            sed -n '/^media_dir=/s/media_dir=//p' "$Conf" |
                while read Served; do
                    chmod -R go=rX "$Served"
                done
            if [ -f "$Log" ]; then
                date +'Root cron restarted %Y-%m-%d %H:%M:%S' >> "$Log"
            fi
            systemctl restart minidlna.service
        fi
        break
    fi
done

# 

Cheers,
David.


Reply to: