Debian Codegraph @

lib/get-boot-info

raw
1 #! /bin/bash
2
3 #*********************************************************************
4 #
5 # bootinfo -- get boot information via DHCP protocol
6 #
7 # This script is part of FAI (Fully Automatic Installation)
8 # (c) 2003-2025 by Thomas Lange, [email protected]
9 # Universitaet zu Koeln
10 #
11 #*********************************************************************
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 # General Public License for more details.
21 #
22 # A copy of the GNU General Public License is available as
23 # `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
24 # or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You
25 # can also obtain it by writing to the Free Software Foundation, Inc.,
26 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27 #*********************************************************************
28
29 # this script writes received information to $LOGDIR/boot.log
30
31 bootlog=$LOGDIR/boot.log
32 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
33 netdevice_info() {
34
35 # devices that are running
36 netdevices_up=$(ip -br a| awk '/UP / {if ($3) print $1}' | tr '\n' ' ')
37 # netdevices is the list of ethernet devices which will be used for bootpc (maybe dhcp)
38 # if not defined, use boot messages to determine network devices
39 [ -n "$netdevices" ] || netdevices=$netdevices_up
40
41 netdevices_all=$(ip -br a| grep -P -o "^eth\S+|^en\S+"|tr '\n' ' ')
42 }
43 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
44 get_dhcp_info() {
45
46 local tmp=$(mktemp)
47 if [ $boot -eq 1 ]; then
48 return
49 fi
50 boot=1
51
52 if [ -n "$netdevices_up" ]; then
53 dhcpcd -T -f /usr/share/fai/dhcpcd.conf $netdevices | egrep '^new_' > $tmp 2> $LOGDIR/dhcpcd.log
54
55 set -a
56 source $tmp
57 dhclient-perl >>$bootlog
58 rm -f $tmp
59 setnet
60 fi
61 }
62 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
63 setnet() {
64
65 # get network parameters
66 local dummy
67
68 # determine the name of the default network interface
69 # try several times if dhclient needs some more time
70 local i=0
71 NIC1=$(ip route | awk '/^default/ {print $5}'|head -1)
72 while [ ! "$NIC1" -a $i -lt 8 ]; do
73 sleep 2
74 NIC1=$(ip route | awk '/^default/ {print $5}'|head -1)
75 (( i += 1 ))
76 done
77 if [ -z "$NIC1" ]; then
78 NIC1=$(ip route | awk '/ dev / {print $3}'|head -1)
79 fi
80
81 # only write info if interface is known
82 if [ -n "$NIC1" ]; then
83 CIDR=$(ip --br ad sh $NIC1|awk '{print $3}')
84 IPADDR=${CIDR%/*}
85 GATEWAYS=$(ip route | awk '/^default/ {print $3}')
86
87 cat >> $bootlog <<-EOF
88 NIC1=$NIC1
89 IPADDR=$IPADDR
90 CIDR=$CIDR
91 GATEWAYS=$GATEWAYS
92 EOF
93 fi
94 if [ -n "$SERVER" ]; then
95 echo "SERVER=$SERVER" >> $bootlog
96 fi
97
98 if [ -n "$DOMAIN" ]; then
99 # DOMAIN was specified on the kernel command line
100 echo "DOMAIN=$DOMAIN" >> $bootlog
101 fi
102 }
103 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
104 get_fixed_info() {
105
106 if [ $boot -eq 1 ]; then
107 return
108 fi
109 boot=1
110 # ip contains the network parameters
111 echo $ip | grep -q : || echo "Kernel parameter ip= does not contain network parameters."
112 setnet
113 }
114 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
115 nic_list_from_cmdline() {
116
117 # parse nic(s) that are specified on the kernel command line like this: ip=etX:dhcp
118
119 niclist=
120 line=$(< /proc/cmdline)
121
122 for item in $line; do
123 case $item in
124
125 ip=*:dhcp)
126 nic=
127 # count the : char in the argument of ip=
128 n="${item//[^:]}"
129 # if there are more than 5 :, it's an static IP configuration, do not use this nic
130 if [ ${#n} -lt 5 ]; then
131 nic=$(expr match "$item" 'ip=\(.*\):dhcp')
132 niclist+=" $nic"
133 fi
134 ;;
135 esac
136 done
137 }
138 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
139
140 netdevice_info
141 cat > $bootlog <<-EOF
142 netdevices_all="$netdevices_all"
143 netdevices_up="$netdevices_up"
144 netdevices="$netdevices"
145 EOF
146 boot=0
147
148 # no network needed
149 if [ "$ip" == "off" ]; then
150 boot=1
151 fi
152
153 # if ip= parameter contains IP address, get fixed IP data, otherwise use DHCP
154 if [[ $ip =~ ^.*:*[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} ]]; then
155 get_fixed_info
156 fi
157
158 # if devices are specified on the kernel command line, only use those devices for DHCP
159 nic_list_from_cmdline
160 if [ -n "$niclist" ]; then
161 netdevices=$niclist
162 fi
163
164 get_dhcp_info
165
166 # use first Ethernet interface, even if it's not used during installation
167 if [ -z "$NIC1" ]; then
168 # ignore some names
169 NIC1=$(ls /sys/class/net/ | egrep '^en' | head -1)
170 echo "NIC1=$NIC1" >> $bootlog
171 fi
172
173 # DHCP should not overwrite the hostname if it's already set
174 # then remove this line from boot.log
175 read hname < /proc/sys/kernel/hostname
176 if [ -n "$hname" -o "$hname" = '(none)' ]; then
177 sed -i -e '/^HOSTNAME=/d' $bootlog
178 fi
179
180 if ! grep -q DOMAIN= $bootlog; then
181 echo 'Info: no domain name configured, using "localdomain"' >&2
182 echo "DOMAIN=localdomain" >> $bootlog
183 fi