Merge branch 'upstream'
[automated-distro-installer] / fai / config / files / root / fai-check / VOL_BUSTER_BOOTSTRAP
1 #!/bin/bash
2
3 set -eE -o pipefail
4 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
5
6
7
8 usage() {
9 cat <<EOF
10 Usage: ${0##*/} [OPTION]
11 If grub var set, act like pxe rom and pxe-kexec to faiserver
12
13 -f|--force do kexec if we can reach faiserver
14 -h|--help Print help and exit.
15
16 Note: Uses GNU getopt options parsing style
17 EOF
18 exit $1
19 }
20
21
22
23 # Keep it short so we don't delay too much wnen we don't have networking.
24 # In practice, on my home network, on an x200, it took 15 seconds, so
25 # give it an extra 10 seconds, which seems fairly short as I write this.
26 NETWORK_TIMOUT_SECS=25
27 did_fai_check=false
28
29 m() { printf "%s\n" "$*"; "$@"; }
30
31 try-kexec() {
32 deadline=$(( `date +%s` + NETWORK_TIMOUT_SECS ))
33 while ! timeout -s 9 3 nc -zu faiserver 69; do
34 if (( `date +%s` > deadline )); then
35 echo "fai-check: hit $NETWORK_TIMOUT_SECS s tftp server timeout"
36 return 0
37 fi
38 sleep 1
39 done
40 m pxe-kexec -n --ignore-whitelist -l fai-generated faiserver ||:
41 }
42
43 case $1 in
44 -f|--force)
45 try-kexec
46 exit
47 ;;
48 esac
49
50
51 first=true
52 for dev in $(btrfs fi show / | sed -rn 's#^\s*devid\s.*\s([^0-9 ]+)\S+$#\1#p' \
53 |sort); do
54
55 # Decide which is my grub_ext partition. see partition.DEFAULT file
56 # for details
57 if [[ $(blockdev --getsize64 ${dev}4) == 8388608 ]]; then
58 # Old partition scheme
59 grub_extn=${dev}4
60 elif [[ $(blockdev --getsize64 ${dev}5) == 8388608 ]]; then
61 grub_extn=${dev}5
62 else
63 grub_extn=${dev}7
64 fi
65 mount $dev /mnt
66 if $first; then
67 if [[ -e /mnt/grubenv ]]; then
68 set -x
69 source <(grub-editenv /mnt/grubenv list)
70 set +x
71 fi
72 first=false
73 # we could just as well check if last_boot != /debianbuster_boostrap
74 # the intent with this one is just a little clearer.
75 if [[ $did_fai_check == true ]]; then
76 grub-editenv /mnt/grubenv set did_fai_check=os_true
77 # our service does not wait for network-online.target,
78 # because it will wait for too long when we don't have a network
79 # connection. So, we wait for 10 seconds.
80 # ref: https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/
81 try-kexec ||:
82 fi
83 else
84 # we make sure there is only 1 grubenv,
85 # so grub can just find the first one, in whatever order
86 # if looks at them, which may not be the same as us.
87 # If the disk dies, we just lose the default boot option,
88 # we will have to do manual steps to replace it anyways.
89 rm -f /mnt/gruvenv
90 fi
91 umount /mnt
92 done
93
94 # the check for last_boot is not needed afaik, just sanity check.
95 if [[ $did_fai_check == true && $last_boot != /debianbuster_boostrap ]]; then
96 # no need to reboot if we actually want to boot into this os.
97 reboot
98 fi