# Modifies NetworkManager-wait-online.service to replaces the call of # nm-online by waiting for all the wanted routes to be up and that a # gateway is reachable. # Uses ip and fping. # To install for example as: # # /etc/systemd/system/NetworkManager-wait-online.service.d/routes-gw.conf # # Then adjust the ROUTES GW MAXWAIT variables below. [Service] ExecStart= ExecStart=/bin/bash -c "LF=$'\n'; "' \ ROUTES=" \ default \ 1.2.3.0/24 \ 4.5.6.0/24 \ "; \ GW=1.2.3.254; \ \ MAXWAIT=60; \ \ routes_defined () { \ local routes=$(ip route); \ echo "$routes"; \ for i in $ROUTES; do \ [[ "$LF$routes" == *$LF$i* ]] || return 1; \ done; \ return 0; \ }; \ \ while (( SECONDS <= MAXWAIT)); do \ if routes_defined; then \ echo Network Manager Wait Online routes took $SECONDS seconds; \ seconds=$SECONDS; \ if fping -q -a -r $MAXWAIT -t 1000 -B 1 $GW >& /dev/null; then \ s=$((SECONDS-seconds)); \ echo Network Manager Wait Online gateway took $s seconds; \ exit 0; \ else \ s=$((SECONDS-seconds)); \ echo Network Manager Wait Online gateway failed after $s seconds; \ exit 1; \ fi; \ fi; \ sleep 1; \ done; \ echo Network Manager Wait Online routes failed after $SECONDS seconds; \ exit 1; \ \ '