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
44 auto lo
45 iface lo inet loopback
46
47 EOF
48
49 unset HOSTNAME
50
51 # generate config for each present network device
52 for interface in /sys/class/net/* ; do
53     [ -e ${interface} ] || continue
54     interface=$(basename ${interface})
55     [ "${interface}" = "lo" ] && continue
56     method="dhcp"
57
58     # NODHCP or a previously run ipconfig mean that ifupdown should never
59     # touch this interface (IP-stack wise).
60     netconfig=/run/net-${interface}.conf
61     if [ -n "$NODHCP" ] || [ -e "${netconfig}" ]; then
62         method="manual"
63     fi
64
65     # if boot option "nodhcp" is set but also boot option "dhcp" is
66     # set, then dhcp should win over it as we default to dhcp and if
67     # nodhcp is used as default boot option but "dhcp" is added then it
68     # would be confusing to not get a working network setup
69     if [ "$DHCP" = "true" ] ; then
70         method="dhcp"
71     fi
72
73     if [ -n "$VLANS" ] ; then
74       modprobe 8021q
75
76       # vlan=<vid>:<phydevice>
77       for line in $(echo $VLANS | sed 's/ /\n'/) ; do
78         vlandev=${line#*:}
79         vlanid=${line%:*}
80
81         if [ -n "$vlandev" ] && [ -n "$vlanid" ] ; then
82           case "$vlandev" in
83             "$interface")
84               vlan_raw_dev=$interface
85               interface="${vlandev}.${vlanid}"
86               ;;
87           esac
88         fi
89       done
90     fi
91
92     if [ -n "$vlan_raw_dev" ] ; then
93       cat >> $IFFILE << EOF
94 auto ${interface}
95 iface ${interface} inet ${method}
96         vlan-raw-device $vlan_raw_dev
97 EOF
98     else
99       cat >> $IFFILE << EOF
100 allow-hotplug ${interface}
101 iface ${interface} inet ${method}
102 EOF
103     fi
104
105     unset vlandev vlanid vlan_raw_dev # unset variables to have clean state for next device
106
107     # DNS for resolvconf and /etc/resolv.conf
108     if [ -e "${netconfig}" ]; then
109         . "${netconfig}"
110         if [ -n "${DNSDOMAIN}" ]; then
111             echo "    dns-search ${DNSDOMAIN}" >> $IFFILE
112         fi
113         # make sure we don't have any 0.0.0.0 nameservers
114         IPV4DNSLIST=""
115         for IPV4DNS in ${IPV4DNS0} ${IPV4DNS1}; do
116             [ -n "${IPV4DNS}" ] || continue
117             [ "${IPV4DNS}" != "0.0.0.0" ] || continue
118             IPV4DNSLIST="${IPV4DNSLIST}${IPV4DNS} "
119         done
120         if [ -n "${IPV4DNSLIST}" ]; then
121             echo "    dns-nameservers ${IPV4DNSLIST}" >> $IFFILE
122             for IPV4DNS in ${IPV4DNSLIST}; do
123                 echo "nameserver ${IPV4DNS}" >> $RESOLVCONF
124             done
125         fi
126     fi
127
128     if [ -z "$NODHCPHOSTNAME" -a -n "$HOSTNAME" ]; then
129         echo $HOSTNAME > /root/etc/hostname
130     fi
131
132     unset DEVICE IPV4ADDR IPV4BROADCAST IPV4NETMASK IPV4GATEWAY IPV4DNS0 IPV4DNS1 HOSTNAME DNSDOMAIN NISDOMAIN ROOTSERVER ROOTPATH filename
133     unset IPV4DNS IPV4DNSLIST
134
135     echo>> $IFFILE
136 done
137
138 # dns bootoption
139 if [ -n "$DNSSERVERS" ]
140 then
141         # disable any existing entries
142         if [ -r $RESOLVCONF ]
143         then
144                 sed -i 's/nameserver/# nameserver/' $RESOLVCONF
145         fi
146         for i in $DNSSERVERS
147         do
148                 echo "nameserver $i" >> $RESOLVCONF
149         done
150 fi
151
152 }