Release new version 1:20210208+grml.5
[live-boot-grml.git] / components / 9990-networking.sh
1 #!/bin/sh
2
3 #set -e
4
5 Device_from_bootif ()
6 {
7         # support for Syslinux IPAPPEND parameter
8         # it sets the BOOTIF variable on the kernel parameter
9
10         if [ -n "${BOOTIF}" ]
11         then
12                 # pxelinux sets BOOTIF to a value based on the mac address of the
13                 # network card used to PXE boot, so use this value for DEVICE rather
14                 # than a hard-coded device name from initramfs.conf. this facilitates
15                 # network booting when machines may have multiple network cards.
16                 # pxelinux sets BOOTIF to 01-$mac_address
17
18                 # strip off the leading "01-", which isn't part of the mac
19                 # address
20                 temp_mac=${BOOTIF#*-}
21
22                 # convert to typical mac address format by replacing "-" with ":"
23                 bootif_mac=""
24                 IFS='-'
25                 for x in $temp_mac
26                 do
27                         if [ -z "$bootif_mac" ]
28                         then
29                                 bootif_mac="$x"
30                         else
31                                 bootif_mac="$bootif_mac:$x"
32                         fi
33                 done
34                 unset IFS
35
36                 # look for devices with matching mac address, and set DEVICE to
37                 # appropriate value if match is found.
38
39                 for device in /sys/class/net/*
40                 do
41                         if [ -f "$device/address" ]
42                         then
43                         current_mac=$(cat "$device/address")
44
45                                 if [ "$bootif_mac" = "$current_mac" ]
46                                 then
47                                         DEVICE=${device##*/}
48                                         break
49                                 fi
50                         fi
51                 done
52         fi
53 }
54
55 do_netsetup ()
56 {
57         modprobe -q af_packet # For DHCP
58
59         udevadm trigger
60         udevadm settle
61
62         [ -n "$ETHDEV_TIMEOUT" ] || ETHDEV_TIMEOUT=15
63         echo "Using timeout of $ETHDEV_TIMEOUT seconds for network configuration."
64
65         if [ -z "${NETBOOT}" ] && [ -z "${FETCH}" ] && [ -z "${HTTPFS}" ] && [ -z "${FTPFS}" ]
66         then
67                 # See if we can select the device from BOOTIF
68                 Device_from_bootif
69
70                 # if ethdevice was not specified on the kernel command line
71                 # make sure we try to get a working network configuration
72                 # for *every* present network device (except for loopback of course)
73                 if [ -z "$ETHDEVICE" ]
74                 then
75                         echo "If you want to boot from a specific device use bootoption ethdevice=..."
76                         for device in /sys/class/net/*
77                         do
78                                 dev=${device##*/}
79                                 if [ "$dev" != "lo" ]
80                                 then
81                                         ETHDEVICE="$ETHDEVICE $dev"
82                                 fi
83                         done
84                 fi
85
86                 # split args of ethdevice=eth0,eth1 into "eth0 eth1"
87                 for device in $(echo "$ETHDEVICE" | sed 's/,/ /g')
88                 do
89                         devlist="$devlist $device"
90                 done
91
92                 for dev in $devlist
93                 do
94                         echo "Executing ipconfig -t $ETHDEV_TIMEOUT $dev"
95                         ipconfig -t "$ETHDEV_TIMEOUT" "$dev" | tee -a /netboot.config
96
97                         # if configuration of device worked we should have an assigned
98                         # IP address, if so let's use the device as $DEVICE for later usage.
99                         # simple and primitive approach which seems to work fine
100                         if ifconfig "$dev" | grep -q -E 'inet.*addr:|inet [0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*'
101                         then
102                                 export DEVICE="$dev"
103                                 break
104                         fi
105                 done
106         else
107                 for interface in ${DEVICE}; do
108                         ipconfig -t "$ETHDEV_TIMEOUT" "${interface}" | tee "/netboot-${interface}.config"
109
110                         [ -e "/run/net-${interface}.conf" ] && . "/run/net-${interface}.conf"
111
112                         if [ "$IPV4ADDR" != "0.0.0.0" ]
113                         then
114                                 break
115                         fi
116                 done
117         fi
118
119         for interface in ${DEVICE}
120         do
121                 # source relevant ipconfig output
122                 OLDHOSTNAME=${HOSTNAME}
123
124                 [ -e "/run/net-${interface}.conf" ] && . "/run/net-${interface}.conf"
125
126                 [ -z "${HOSTNAME}" ] && HOSTNAME="${OLDHOSTNAME}"
127                 export HOSTNAME
128
129                 if [ -n "${interface}" ]
130                 then
131                         # HWADDR used by do_iscsi from 9990-mount-iscsi.sh
132                         # shellcheck disable=SC2034
133                         HWADDR="$(cat "/sys/class/net/${interface}/address")"
134                 fi
135
136                 if [ ! -e "/etc/hostname" ] && [ -n "${HOSTNAME}" ]
137                 then
138                         echo "Creating /etc/hostname"
139                         echo "${HOSTNAME}" > /etc/hostname
140                 fi
141
142                 # Only create /etc/hosts if FQDN is known (to let 'hostname -f' query
143                 # this file). Otherwise DNS will be queried to determine the FQDN.
144                 if [ ! -e "/etc/hosts" ] && [ -n "${DNSDOMAIN}" ]
145                 then
146                         echo "Creating /etc/hosts"
147                         cat > /etc/hosts <<EOF
148 127.0.0.1       localhost
149 127.0.1.1       ${HOSTNAME}.${DNSDOMAIN}        ${HOSTNAME}
150
151 # The following lines are desirable for IPv6 capable hosts
152 ::1     localhost ip6-localhost ip6-loopback
153 ff02::1 ip6-allnodes
154 ff02::2 ip6-allrouters
155 EOF
156                 fi
157
158                 if [ ! -e "/etc/resolv.conf" ]
159                 then
160                         echo "Creating /etc/resolv.conf"
161
162                         if [ -n "${DNSDOMAIN}" ]
163                         then
164                                 echo "domain ${DNSDOMAIN}" > /etc/resolv.conf
165                         fi
166
167                         for i in ${IPV4DNS0} ${IPV4DNS1} ${IPV4DNS1} ${DNSSERVERS}
168                         do
169                                 if [ -n "$i" ] && [ "$i" != 0.0.0.0 ]
170                                 then
171                                         echo "nameserver $i" >> /etc/resolv.conf
172                                 fi
173                         done
174
175                         if [ -n "${DOMAINSEARCH}" ]
176                         then
177                                 echo "search ${DOMAINSEARCH}" >> /etc/resolv.conf
178                         elif [ -n "${DNSDOMAIN}" ]
179                         then
180                                 echo "search ${DNSDOMAIN}" >> /etc/resolv.conf
181                         fi
182                 fi
183
184                 # Check if we have a network device at all
185                 if ! ls /sys/class/net/"$interface" > /dev/null 2>&1 && \
186                    ! ls /sys/class/net/eth0 > /dev/null 2>&1 && \
187                    ! ls /sys/class/net/wlan0 > /dev/null 2>&1 && \
188                    ! ls /sys/class/net/ath0 > /dev/null 2>&1 && \
189                    ! ls /sys/class/net/ra0 > /dev/null 2>&1
190                 then
191                         panic "No supported network device found, maybe a non-mainline driver is required."
192                 fi
193         done
194 }