Debian Codegraph @

lib/dracut/50fai-autodiscover/fai-scan.sh

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2
3 # autodiscover of the FAI server
4
5 # (c) Thomas Lange 2015,2017
6
7 . /lib/dracut-lib.shscip-shell source . . `/lib/dracut-lib.sh`.
8
9 if ! getargbool 0 fai.discover; then
10 return
11 fi
12
13 port=$(getarg fai.port=)
14 [scip-shell system . . `[`. -z ${port} ] && port=4711 # default value
15 export FAI_MONITOR_PORT=$port
16
17
18 # this script is called with all network interfaces
19 nic=$1
20
21 ivar=/run/initramfs/fai-variablesscip-shell filesystem . . `/run/initramfs/fai-variables`.
22 declare -a faisrv
23 declare -A version
24 declare -a para
25
26 scan_netscip-shell shell fai 6.6 scan_net().() {
27
28 # scan local network for FAI server
29
30 local iprange=$1
31 shift
32 local port=$1
33 local iplist
34 local host
35
36 iplist=$(nmap -n -e $nic --host-timeout 10 --open -oG - -p $port $iprange 2>/dev/nullscip-shell filesystem . . `/dev/null`. | grepscip-shell system . . grep. -v '#' | cutscip-shell system . . cut. -d' ' -f2 | sortscip-shell system . . sort. -n | uniqscip-shell system . . uniq.)
37
38 # check all IPs, if a faiserver is listening there
39 for host in $iplist; do
40 getvarscip-shell shell fai 6.6 getvar(). FAI_VERSION $host
41 if [scip-shell system . . `[`. -n "$FAI_VERSION" ]; then
42 faiserver+=" $host"
43 version[$host]="$FAI_VERSION"
44 fi
45 done
46 }
47
48 getvarscip-shell shell fai 6.6 getvar().() {
49
50 # get a variable from the fai-monitor
51 # use $monserver and $FAI_MONITOR_PORT is no arguments are used
52 local ret
53 local srv
54 local var
55 local port
56
57 var=$1
58 srv=$2
59 port=$3
60
61 : ${port:=$FAI_MONITOR_PORT}
62 : ${srv:=$monserver}
63
64 ret=$(echoscip-shell system . . echo. "VARIABLE $var" | nc $srv $port)
65 if [[ $ret =~ ^OK\ ]]; then
66 val=${ret#OK }
67 eval $var=\$val
68 else
69 eval "$var="
70 fi
71 }
72
73 exec 1>&/dev/console
74 iprange=$(ip addr show up | grepscip-shell system . . grep. -P -o '(?<=inet )\S+' | grepscip-shell system . . grep. -v 127.0.0.1 |headscip-shell system . . head. -1)
75 echoscip-shell system . . echo. "Scanning $iprange for FAI server (port $FAI_MONITOR_PORT)"
76
77 while [scip-shell system . . `[`. 1 ]; do
78
79 dialog --infobox "\n Scanning $iprange for FAI server (port $FAI_MONITOR_PORT)\n" 5 59
80 scan_netscip-shell shell fai 6.6 scan_net(). $iprange $FAI_MONITOR_PORT
81 faisrv=($faiserver)
82
83 if [scip-shell system . . `[`. ${#faisrv[@]} -eq 0 ]; then
84 dialog --colors --yesno "\n \Zr\Z1No FAI server found.\Zn\n\n\n Retry scan?\n" 10 28
85 if [scip-shell system . . `[`. $? -eq 1 ]; then
86 die "No FAI server found."
87 root=
88 return
89 fi
90 elif [scip-shell system . . `[`. ${#faisrv[@]} -gt 1 ]; then
91 echoscip-shell system . . echo. "More than one FAI server found."
92 # build parameter list for the menu
93 for i in "${faisrv[@]}"; do
94 # resolve IP to host name
95 hn=$(getentscip-shell system . . getent. hosts $i)
96 hn=${hn#* }
97 hn=${hn%%.*}
98 str=$(printfscip-shell system . . printf. "%-20s" $hn)
99 para+=("$i" "$str FAI ${version[$i]}")
100 done
101
102 exec 3>&1
103 selection=$(dialog --colors --no-cancel --menu " More than one server found. \n Please select a server:\n\n" 15 70 10 "${para[@]}" "Rescan" "Rescan the network" 2>&1 >&3)
104
105 if [scip-shell system . . `[`. "$selection" = "Rescan" ]; then
106 faiserver=""
107 para=()
108 unset version
109 declare -A version
110 else
111 # one server was selected
112 monserver=${selection}
113 break
114 fi
115 else
116 # we found excatly one FAI server
117 monserver=${faisrv[0]}
118 break
119 fi
120 done
121
122 FAI_VERSION=${version[$monserver]}
123
124 getvarscip-shell shell fai 6.6 getvar(). FAI_FLAGS
125 getvarscip-shell shell fai 6.6 getvar(). NFSROOT
126 getvarscip-shell shell fai 6.6 getvar(). FAI_CONFIG_SRC
127
128 if [scip-shell system . . `[`. -z "FAI_CONFIG_SRC" ]; then
129 getvarscip-shell shell fai 6.6 getvar(). FAI_CONFIGDIR
130 FAI_CONFIG_SRC="nfs://$monserver/$FAI_CONFIGDIR"
131 fi
132
133 dialog --infobox "\n\n FAI server $FAI_VERSION found at $monserver\n\n Using root=$monserver:$NFSROOT\n" 9 50
134 sleepscip-shell system . . sleep. 4
135
136 root=$monserver:$NFSROOT
137 netroot=nfs:$monserver:$NFSROOT:nfsvers=3
138 echoscip-shell system . . echo. "root=$root"
139
140 echoscip-shell system . . echo. "FAI_VERSION=$FAI_VERSION" >> $ivar
141 echoscip-shell system . . echo. "FAI_FLAGS=$FAI_FLAGS" >> $ivar
142 echoscip-shell system . . echo. "root=$monserver:$NFSROOT" >> $ivar
143 echoscip-shell system . . echo. "monserver=$monserver" >> $ivar
144 echoscip-shell system . . echo. "FAI_CONFIG_SRC=$FAI_CONFIG_SRC" >> $ivar
145
146 # Done, all good!
147 rootok=1