little bette readme
[automated-distro-installer] / fai / config / files / root / fai-check / STABLE_BOOTSTRAP
1 #!/bin/bash
2
3 set -eE -o pipefail
4 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
5
6 # Keep it short so we don't delay too much wnen we don't have networking.
7 # In practice, on my home network, on an x200, it took 15 seconds, so
8 # give it an extra 10 seconds, which seems fairly short as I write this.
9 NETWORK_TIMOUT_SECS=25
10 did_fai_check=false
11
12 m() { printf "%s\n" "$*"; "$@"; }
13
14 check-fai() {
15 # we could just as well check if last_boot != /debianstable_boostrap
16 # the intent with this one is just a little clearer.
17 if [[ $did_fai_check == true ]]; then
18 grub-editenv /mnt/grubenv set did_fai_check=os_true
19 # our service does not wait for network-online.target,
20 # because it will wait for too long when we don't have a network
21 # connection. So, we wait for 10 seconds.
22 # ref: https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/
23 deadline=$(( `date +%s` + NETWORK_TIMOUT_SECS ))
24 while ! nc -zu faiserver 69; do
25 if (( `date +%s` > deadline )); then
26 echo "fai-check: hit $NETWORK_TIMOUT_SECS s tftp server timeout"
27 return 0
28 fi
29 sleep 1
30 done
31 m pxe-kexec -n --ignore-whitelist -l fai-generated faiserver ||:
32 else
33 return 0
34 fi
35 }
36
37 first=true
38 for dev in $(btrfs fi show / | sed -rn 's#^\s*devid\s.*\s([^0-9 ]+)\S+$#\1#p' \
39 |sort); do
40 dev+=4
41 mount $dev /mnt
42 if $first; then
43 if [[ -e /mnt/grubenv ]]; then
44 set -x
45 source <(grub-editenv /mnt/grubenv list)
46 set +x
47 fi
48 first=false
49 check-fai
50 else
51 # we make sure there is only 1 grubenv,
52 # so grub can just find the first one, in whatever order
53 # if looks at them, which may not be the same as us.
54 # If the disk dies, we just lose the default boot option,
55 # we will have to do manual steps to replace it anyways.
56 rm -f /mnt/gruvenv
57 fi
58 umount /mnt
59 done
60
61 # the check for last_boot is not needed afaik, just sanity check.
62 if [[ $did_fai_check == true && $last_boot != /debianstable_boostrap ]]; then
63 # no need to reboot if we actually want to boot into this os.
64 reboot
65 fi