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#
(no special permissions needed)
Code: Select all
root@fv-rpi3b:/home/pi# nano /lib/systemd/system/network-wait-online.service
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
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.
Code: Select all
root@fv-rpi3b:/home/pi# reboot now
In addition to normal usage testing, the following steps help create long network availability delays for testing purposes:
- Configure your Pi for Wi-Fi networking
- Configure the Wi-Fi access point to hide its SSID (“hidden network”)
- In /etc/wpa_supplicant/wpa_supplicant.conf, disable scanning (disable or remove ‘ap_scan’ and ‘scan_ssid’ entries)
- 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
Code: Select all
mnt-media.mount
● ├─-.mount
● ├─system.slice
● └─network-online.target
● └─network-wait-online.service