fvisagie
Posts: 11
Joined: Wed Apr 19, 2017 11:27 am
Location: Man

GUIDE: Reliably wait for network when booting Jessie

Sat Jun 24, 2017 5:03 pm

Superseded - posted an improved implementation (controlled by Wait for Network) to the Raspbian forum.

This guide uses Jessie's systemd init system to implement simple and reliable detection of network availability for mounting remote drives, etc.

Guide

1. Change to super-user

Code: Select all

pi@fv-rpi3b:~ $ sudo su
root@fv-rpi3b:/home/pi#
2. In /lib/systemd/system, create network-wait-online.service
(no special permissions needed)

Code: Select all

root@fv-rpi3b:/home/pi# nano /lib/systemd/system/network-wait-online.service
Paste in the following:

Code: Select all

#
# Uses 'hostname --all-fqdns' to confirm that both: IP address[es] assigned, and DNS operational
#

[Unit]
Description=Wait for Network to be Online
Documentation=man:systemd.service(5) man:systemd.special(7)
Conflicts=shutdown.target
After=network.target
Before=network-online.target

[Service]
Type=oneshot
ExecStart=/bin/bash -c 'while [ -z $(hostname --all-fqdns) ]; do sleep 1; done'
TimeoutStartSec=1min 30s

[Install]
WantedBy=network-online.target

Code: Select all

Ctrl-O

Code: Select all

Ctrl-X
to write the file and exit

3. Enable network-wait-online.service

Code: Select all

root@fv-rpi3b:/home/pi# systemctl enable network-wait-online.service
Created symlink from /etc/systemd/system/network-online.target.wants/network-wait-online.service to /lib/systemd/system/network-wait-online.service.
4. Reboot

Code: Select all

root@fv-rpi3b:/home/pi# reboot now
Testing

In addition to normal usage testing, the following steps help create long network availability delays for testing purposes:
  1. Configure your Pi for Wi-Fi networking
  2. Configure the Wi-Fi access point to hide its SSID (“hidden network”)
  3. In /etc/wpa_supplicant/wpa_supplicant.conf, disable scanning (disable or remove ‘ap_scan’ and ‘scan_ssid’ entries)
  4. Reboot or re-acquire the network

Why I prefer this approach

It provides reliable detection

When configured to Wait for Network, the Jessie systemd init process detects dhcpcd obtaining either static or leased interface configuration/s, before it proceeds to network.target and network-online.target. At that exact point, however, interface address/es are not yet configured, and name resolution comes later still.

When not configured to Wait for Network (or when dhcpcd reaches its timeout while waiting), dhcpcd changes to a background daemon and the init process proceeds immediately. This means network.target and network-online.target are reached even without the availability of interface configuration/s.

Compounding the problem further is the fact that - as standard - network.target and network-online.target do no further checking. network-online.target is therefore often reached before interface configurations have been applied, and in cases even before they have been obtained.

This approach adds the necessary checking to network-online.target, testing for both IP configuration and DNS operation.

It is deterministic

Delay-based approaches may waste boot time or not wait long enough. This approach waits as long as necessary and not any longer to ensure reliable network availability.

It is future-proof?

By integrating into the Jessie systemd init process in a standard way, the configuration for this solution will hopefully survive OS upgrades ;-).

Other approaches

Boot or user login delays:
Fixed delays usually either waste time by waiting too long, or are unreliable because they do not necessarily wait long enough.

fstab noauto, x-systemd.automount options:
This delays mounting until first access. Similarly to above it is not guaranteed to introduce a long enough delay (and in practice usually doesn't).

fstab _netdev or x-systemd.requires=network-online.target options:
These are redundant with a standard Jessie systemd installation -

Code: Select all

pi@fv-rpi3b:~ $ man systemd.special

network-online.target

All mount units for remote network file systems automatically pull in this unit, and order themselves after it.

Code: Select all

pi@fv-rpi3b:~ $ systemctl list-dependencies mnt-media.mount
mnt-media.mount
● ├─-.mount
● ├─system.slice
● └─network-online.target
As mentioned above, reaching network-online.target on a standard configuration does not guarantee anything. Once network-wait-online.service has been enabled:

Code: Select all

mnt-media.mount
● ├─-.mount
● ├─system.slice
● └─network-online.target
●   └─network-wait-online.service
RPi 3B with Raspbian Stretch 2017-09-07 (updated) and Kodi 18.2

Glasairman
Posts: 12
Joined: Fri Mar 16, 2012 9:55 am
Location: Munich, Germany

Re: GUIDE: Reliably wait for network when booting Jessie

Wed Dec 27, 2017 5:16 pm

Are you sure about this?

I have read that any diversion from the standard systemd settings should be in /etc/systemd/system/xxx.service NOT in /lib/systemd.

/etc/systemd won't be overwritten by the next upgrade and comes after /lib/systemd.. and therefore takes priority .

Of course systemctl enable etc. is also needed

fvisagie
Posts: 11
Joined: Wed Apr 19, 2017 11:27 am
Location: Man

Re: GUIDE: Reliably wait for network when booting Jessie

Fri Dec 29, 2017 10:06 am

You may well be right, I didn't manage to find much authoritative information at the time.
Glasairman wrote:
Wed Dec 27, 2017 5:16 pm
I have read
I'd be interested in your source, if you wouldn't mind sharing it?
RPi 3B with Raspbian Stretch 2017-09-07 (updated) and Kodi 18.2

Return to “Troubleshooting”