Ship our own networking script
[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 modprobe af_packet # req'd for DHCP
12
13 # initialize udev
14 # (this /might/ be required for firmware loading to complete)
15 if grep -q noudev /proc/cmdline; then
16    log_begin_msg "Networking: Skipping udev as requested via bootoption noudev."
17 else
18    udevadm trigger
19    udevadm settle
20 fi
21
22 if [ -n "${IP}" ]; then
23    # call into initramfs-tools provided network setup functions, so basic
24    # networking is fine.
25    log_begin_msg "Networking: Waiting for basic network to come up..."
26    configure_networking
27 fi
28
29 # prepare a new /etc/network/interfaces file (and, possibly, a new /etc/resolv.conf)
30 IFFILE="/root/etc/network/interfaces"
31 if [ -L /root/etc/resolv.conf ] ; then
32   # assume we have resolvconf
33   RESOLVCONF=/root/etc/resolvconf/resolv.conf.d/base
34 else
35   RESOLVCONF="/root/etc/resolv.conf"
36 fi
37
38 # config for loopback networking
39 cat > $IFFILE << EOF
40 # Initially generated on boot by initramfs' 23networking.
41
42 auto lo
43 iface lo inet loopback
44
45 EOF
46
47 unset HOSTNAME
48
49 # generate config for each present network device
50 for interface in /sys/class/net/eth* /sys/class/net/ath* /sys/class/net/wlan*; do
51     [ -e ${interface} ] || continue
52     interface=$(basename ${interface})
53     method="dhcp"
54
55     # NODHCP or a previously run ipconfig mean that ifupdown should never
56     # touch this interface (IP-stack wise).
57     netconfig=/run/net-${interface}.conf
58     if [ -n "$NODHCP" ] || [ -e "${netconfig}" ]; then
59         method="manual"
60     fi
61
62     # if boot option "nodhcp" is set but also boot option "dhcp" is
63     # set, then dhcp should win over it as we default to dhcp and if
64     # nodhcp is used as default boot option but "dhcp" is added then it
65     # would be confusing to not get a working network setup
66     if [ "$DHCP" = "true" ] ; then
67         method="dhcp"
68     fi
69
70     cat >> $IFFILE << EOF
71 allow-hotplug ${interface}
72 iface ${interface} inet ${method}
73 EOF
74
75     # DNS for resolvconf and /etc/resolv.conf
76     if [ -e "${netconfig}" ]; then
77         . "${netconfig}"
78         if [ -n "${DNSDOMAIN}" ]; then
79             echo "    dns-search ${DNSDOMAIN}" >> $IFFILE
80         fi
81         # make sure we don't have any 0.0.0.0 nameservers
82         IPV4DNSLIST=""
83         for IPV4DNS in ${IPV4DNS0} ${IPV4DNS1}; do
84             [ -n "${IPV4DNS}" ] || continue
85             [ "${IPV4DNS}" != "0.0.0.0" ] || continue
86             IPV4DNSLIST="${IPV4DNSLIST}${IPV4DNS} "
87         done
88         if [ -n "${IPV4DNSLIST}" ]; then
89             echo "    dns-nameservers ${IPV4DNSLIST}" >> $IFFILE
90             for IPV4DNS in ${IPV4DNSLIST}; do
91                 echo "nameserver ${IPV4DNS}" >> $RESOLVCONF
92             done
93         fi
94     fi
95
96     if [ -z "$NODHCPHOSTNAME" -a -n "$HOSTNAME" ]; then
97         echo $HOSTNAME > /root/etc/hostname
98     fi
99
100     unset DEVICE IPV4ADDR IPV4BROADCAST IPV4NETMASK IPV4GATEWAY IPV4DNS0 IPV4DNS1 HOSTNAME DNSDOMAIN NISDOMAIN ROOTSERVER ROOTPATH filename
101     unset IPV4DNS IPV4DNSLIST
102
103     echo>> $IFFILE
104 done
105
106 # dns bootoption
107 if [ -n "$DNSSERVER1" ]
108 then
109         # disable any existing entries
110         if [ -r $RESOLVCONF ]
111         then
112                 sed -i 's/nameserver/# nameserver/' $RESOLVCONF
113         fi
114         for i in $DNSSERVER1 $DNSSERVER2
115         do
116                 echo "nameserver $i" >> $RESOLVCONF
117         done
118 fi
119
120 }