various fixes, data subvols changed, mostly handled elsewhere now
[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 NETWORK_TIMOUT_SECS=10
8 fai_check=false
9 check-fai() {
10 # we could just as well check if last_boot != /debianstable_boostrap
11 # the intent with this one is just a little clearer.
12 if [[ $did_fai_check == true ]]; then
13 fai_check=true
14 # ref: https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/
15 deadline=$(( `date +%s` + NETWORK_TIMOUT_SECS ))
16 while ! systemctl status network-online.target && \
17 (( `date +%s` < deadline )); do
18 sleep 1
19 done
20 pxe-kexec -n --ignore-whitelist -l fai-generated faiserver ||:
21 else
22 return 0
23 fi
24 }
25
26 first=true
27 for dev in $(btrfs fi show / | sed -rn 's#^\s*devid\s.*\s([^0-9 ]+)\S+$#\1#p' \
28 |sort); do
29 dev+=4
30 mount $dev /mnt
31 if $first; then
32 if [[ -e /mnt/grubenv ]]; then
33 source <(grub-editenv /mnt/grubenv list)
34 fi
35 first=false
36 check-fai
37 else
38 # we make sure there is only 1 grubenv,
39 # so grub can just find the first one, in whatever order
40 # if looks at them, which may not be the same as us.
41 # If the disk dies, we just lose the default boot option,
42 # we will have to do manual steps to replace it anyways.
43 rm -f /mnt/gruvenv
44 fi
45 umount /mnt
46 done
47
48 if $fai_check && [[ $last_boot != /debianstable_boostrap ]]; then
49 # no need to reboot if we actually want to boot into this os.
50 reboot
51 fi