Release new version 1:20210208+grml.5
[live-boot-grml.git] / components / 9990-grml-networking.sh
1 #!/bin/sh
2
3 #set -e
4
5 Grml_Networking () {
6
7 if [ -n "${NONETWORKING}" ]; then
8    return 0
9 fi
10
11 log_begin_msg "Preconfiguring Grml networking"
12
13 modprobe af_packet # req'd for DHCP
14
15 # initialize udev
16 # (this /might/ be required for firmware loading to complete)
17 if grep -q noudev /proc/cmdline; then
18    log_begin_msg "Networking: Skipping udev as requested via bootoption noudev."
19 else
20    udevadm trigger
21    udevadm settle
22 fi
23
24 if [ -n "${IP}" ]; then
25    # call into initramfs-tools provided network setup functions, so basic
26    # networking is fine.
27    log_begin_msg "Networking: Waiting for basic network to come up..."
28    configure_networking
29 fi
30
31 # prepare a new /etc/network/interfaces file (and, possibly, a new /etc/resolv.conf)
32 IFFILE="/root/etc/network/interfaces"
33 if [ -L /root/etc/resolv.conf ] ; then
34   # assume we have resolvconf
35   RESOLVCONF=/root/etc/resolvconf/resolv.conf.d/base
36 else
37   RESOLVCONF="/root/etc/resolv.conf"
38 fi
39
40 # config for loopback networking
41 cat > $IFFILE << EOF
42 # Initially generated on boot by initramfs,
43 # interfaces(5) file used by ifup(8) and ifdown(8)
44
45 # Include files from /etc/network/interfaces.d:
46 source /etc/network/interfaces.d/*
47
48 auto lo
49 iface lo inet loopback
50
51 EOF
52
53 unset HOSTNAME
54
55 # generate config for each present network device
56 for interface in /sys/class/net/* ; do
57     [ -e ${interface} ] || continue
58     interface=$(basename ${interface})
59     [ "${interface}" = "lo" ] && continue
60     method="dhcp"
61
62     # NODHCP or a previously run ipconfig mean that ifupdown should never
63     # touch this interface (IP-stack wise).
64     netconfig=/run/net-${interface}.conf
65     if [ -n "$NODHCP" ] || [ -e "${netconfig}" ]; then
66         method="manual"
67     fi
68
69     # if boot option "nodhcp" is set but also boot option "dhcp" is
70     # set, then dhcp should win over it as we default to dhcp and if
71     # nodhcp is used as default boot option but "dhcp" is added then it
72     # would be confusing to not get a working network setup
73     if [ "$DHCP" = "true" ] ; then
74         method="dhcp"
75     fi
76
77     if [ -n "$VLANS" ] ; then
78       modprobe 8021q
79
80       # vlan=<vid>:<phydevice>
81       for line in $(echo $VLANS | sed 's/ /\n'/) ; do
82         vlandev=${line#*:}
83         vlanid=${line%:*}
84
85         if [ -n "$vlandev" ] && [ -n "$vlanid" ] ; then
86           case "$vlandev" in
87             "$interface")
88               vlan_raw_dev=$interface
89               interface="${vlandev}.${vlanid}"
90               ;;
91           esac
92         fi
93       done
94     fi
95
96     if [ -n "$vlan_raw_dev" ] ; then
97       cat >> $IFFILE << EOF
98 auto ${interface}
99 iface ${interface} inet ${method}
100         vlan-raw-device $vlan_raw_dev
101 EOF
102     else
103       cat >> $IFFILE << EOF
104 allow-hotplug ${interface}
105 iface ${interface} inet ${method}
106 EOF
107     fi
108
109     unset vlandev vlanid vlan_raw_dev # unset variables to have clean state for next device
110
111     # DNS for resolvconf and /etc/resolv.conf
112     if [ -e "${netconfig}" ]; then
113         . "${netconfig}"
114         if [ -n "${DNSDOMAIN}" ]; then
115             echo "    dns-search ${DNSDOMAIN}" >> $IFFILE
116         fi
117         # make sure we don't have any 0.0.0.0 nameservers
118         IPV4DNSLIST=""
119         for IPV4DNS in ${IPV4DNS0} ${IPV4DNS1}; do
120             [ -n "${IPV4DNS}" ] || continue
121             [ "${IPV4DNS}" != "0.0.0.0" ] || continue
122             IPV4DNSLIST="${IPV4DNSLIST}${IPV4DNS} "
123         done
124         if [ -n "${IPV4DNSLIST}" ]; then
125             echo "    dns-nameservers ${IPV4DNSLIST}" >> $IFFILE
126             for IPV4DNS in ${IPV4DNSLIST}; do
127                 echo "nameserver ${IPV4DNS}" >> $RESOLVCONF
128             done
129         fi
130     fi
131
132     if [ -z "$NODHCPHOSTNAME" -a -n "$HOSTNAME" ]; then
133         echo $HOSTNAME > /root/etc/hostname
134     fi
135
136     unset DEVICE IPV4ADDR IPV4BROADCAST IPV4NETMASK IPV4GATEWAY IPV4DNS0 IPV4DNS1 HOSTNAME DNSDOMAIN NISDOMAIN ROOTSERVER ROOTPATH filename
137     unset IPV4DNS IPV4DNSLIST
138
139     echo>> $IFFILE
140 done
141
142 # dns bootoption
143 if [ -n "$DNSSERVERS" ]
144 then
145         # disable any existing entries
146         if [ -r $RESOLVCONF ]
147         then
148                 sed -i 's/nameserver/# nameserver/' $RESOLVCONF
149         fi
150         for i in $DNSSERVERS
151         do
152                 echo "nameserver $i" >> $RESOLVCONF
153         done
154 fi
155
156 }