Browse Source

systemd: Wait for default routes to emerge before reaching network-online.target

Signed-off-by: Maximilian Wilhelm <max@sdn.clinic>
Maximilian Wilhelm 5 months ago
parent
commit
9b54ea7e2c
3 changed files with 75 additions and 0 deletions
  1. 22 0
      systemd/init.sls
  2. 38 0
      systemd/wait-for-routes
  3. 15 0
      systemd/wait-for-routes.service

+ 22 - 0
systemd/init.sls

@@ -7,3 +7,25 @@ systemctl-daemon-reload:
   cmd.wait:
     - name: systemctl daemon-reload
     - watch: []
+
+
+#
+# Install service to wait for routing adjancies to come up (if needed)
+#
+/etc/systemd/system/wait-for-routes.service:
+  file.managed:
+    - source: salt://systemd/wait-for-routes.service
+    - watch_in:
+      - cmd: systemctl-daemon-reload
+
+wait-for-routes.service:
+  service.running:
+    - enable: true
+    - require:
+      - file: /etc/systemd/system/wait-for-routes.service
+      - file: /usr/local/sbin/wait-for-routes
+
+/usr/local/sbin/wait-for-routes:
+  file.managed:
+   - source: salt://systemd/wait-for-routes
+   - mode: 755

+ 38 - 0
systemd/wait-for-routes

@@ -0,0 +1,38 @@
+#!/bin/sh
+#
+# Wait for routing adjacencies to come up and produce a default route
+#
+# Maximilian Wilhelm <max@sdn.clinic>
+#  -- Mon, 05 Apr 2021 02:31:58 +0200
+
+# Wait for this amount of seconds before giving up
+timeout=30
+
+# Wait for IPv4 default route to emerge
+(
+	for n in $(seq 1 $timeout); do
+		if ip -4 route | grep -q "^default"; then
+			break
+		fi
+
+		sleep 1
+	done
+)&
+
+# Wait for IPv6 default route to emerge
+(
+	for n in $(seq 1 $timeout); do
+		if ip -6 route | grep -q "^default"; then
+			break
+		fi
+
+		sleep 1
+	done
+
+)&
+
+
+# WAit for both sub-shells to finish
+wait
+
+exit 0

+ 15 - 0
systemd/wait-for-routes.service

@@ -0,0 +1,15 @@
+[Unit]
+Description=Wait for routing adjacencies to come up
+DefaultDependencies=no
+Conflicts=shutdown.target
+Requisite=networking.service
+After=networking.service
+Before=network-online.target
+
+[Service]
+Type=oneshot
+ExecStart=/usr/local/sbin/wait-for-routes
+RemainAfterExit=yes
+
+[Install]
+WantedBy=network-online.target