cleanup classes, populate ssh server keys
[automated-distro-installer] / fai / config / files / root / fai-check / STABLE_BOOTSTRAP
index 29bb1fbb7272813e1755b349000bef79bd7c3cd4..c06702901fd46a1f097da9d51ad569e7e1aae492 100755 (executable)
@@ -3,13 +3,32 @@
 set -eE -o pipefail
 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
 
-fai_check=false
+# Keep it short so we don't delay too much wnen we don't have networking.
+# In practice, on my home network, on an x200, it took 15 seconds, so
+# give it an extra 10 seconds, which seems fairly short as I write this.
+NETWORK_TIMOUT_SECS=25
+did_fai_check=false
+
+m() { printf "%s\n" "$*";  "$@"; }
+
 check-fai() {
     # we could just as well check if last_boot != /debianstable_boostrap
     # the intent with this one is just a little clearer.
     if [[ $did_fai_check == true ]]; then
-        fai_check=true
-        pxe-kexec -n --ignore-whitelist -l fai-generated faiserver ||:
+        grub-editenv /mnt/grubenv set did_fai_check=os_true
+        # our service does not wait for network-online.target,
+        # because it will wait for too long when we don't have a network
+        # connection. So, we wait for 10 seconds.
+        # ref: https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/
+        deadline=$(( `date +%s` + NETWORK_TIMOUT_SECS ))
+        while ! nc -zu faiserver 69; do
+            if (( `date +%s` > deadline )); then
+                echo "fai-check: hit $NETWORK_TIMOUT_SECS s tftp server timeout"
+                return 0
+            fi
+            sleep 1
+        done
+        m pxe-kexec -n --ignore-whitelist -l fai-generated faiserver ||:
     else
         return 0
     fi
@@ -22,7 +41,9 @@ for dev in $(btrfs fi show / | sed -rn 's#^\s*devid\s.*\s([^0-9 ]+)\S+$#\1#p' \
     mount $dev /mnt
     if $first; then
         if [[ -e /mnt/grubenv ]]; then
+            set -x
             source <(grub-editenv /mnt/grubenv list)
+            set +x
         fi
         first=false
         check-fai
@@ -37,7 +58,8 @@ for dev in $(btrfs fi show / | sed -rn 's#^\s*devid\s.*\s([^0-9 ]+)\S+$#\1#p' \
     umount /mnt
 done
 
-if $fai_check && [[ $last_boot != /debianstable_boostrap ]]; then
+# the check for last_boot is not needed afaik, just sanity check.
+if [[ $did_fai_check == true && $last_boot != /debianstable_boostrap ]]; then
     # no need to reboot if we actually want to boot into this os.
     reboot
 fi