use auto instead of allow-hotplug in generated /e/n/interfaces
[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     if [ -n "$VLANS" ] ; then
71       modprobe 8021q
72
73       # vlan=<vid>:<phydevice>
74       for line in $(echo $VLANS | sed 's/ /\n'/) ; do
75         vlandev=${line#*:}
76         vlanid=${line%:*}
77
78         if [ -n "$vlandev" ] && [ -n "$vlanid" ] ; then
79           case "$vlandev" in
80             "$interface")
81               vlan_raw_dev=$interface
82               interface="${vlandev}.${vlanid}"
83               ;;
84           esac
85         fi
86       done
87     fi
88
89     if [ -n "$vlan_raw_dev" ] ; then
90       cat >> $IFFILE << EOF
91 auto ${interface}
92 iface ${interface} inet ${method}
93         vlan-raw-device $vlan_raw_dev
94 EOF
95     else
96       cat >> $IFFILE << EOF
97 auto ${interface}
98 iface ${interface} inet ${method}
99 EOF
100     fi
101
102     unset vlandev vlanid vlan_raw_dev # unset variables to have clean state for next device
103
104     # DNS for resolvconf and /etc/resolv.conf
105     if [ -e "${netconfig}" ]; then
106         . "${netconfig}"
107         if [ -n "${DNSDOMAIN}" ]; then
108             echo "    dns-search ${DNSDOMAIN}" >> $IFFILE
109         fi
110         # make sure we don't have any 0.0.0.0 nameservers
111         IPV4DNSLIST=""
112         for IPV4DNS in ${IPV4DNS0} ${IPV4DNS1}; do
113             [ -n "${IPV4DNS}" ] || continue
114             [ "${IPV4DNS}" != "0.0.0.0" ] || continue
115             IPV4DNSLIST="${IPV4DNSLIST}${IPV4DNS} "
116         done
117         if [ -n "${IPV4DNSLIST}" ]; then
118             echo "    dns-nameservers ${IPV4DNSLIST}" >> $IFFILE
119             for IPV4DNS in ${IPV4DNSLIST}; do
120                 echo "nameserver ${IPV4DNS}" >> $RESOLVCONF
121             done
122         fi
123     fi
124
125     if [ -z "$NODHCPHOSTNAME" -a -n "$HOSTNAME" ]; then
126         echo $HOSTNAME > /root/etc/hostname
127     fi
128
129     unset DEVICE IPV4ADDR IPV4BROADCAST IPV4NETMASK IPV4GATEWAY IPV4DNS0 IPV4DNS1 HOSTNAME DNSDOMAIN NISDOMAIN ROOTSERVER ROOTPATH filename
130     unset IPV4DNS IPV4DNSLIST
131
132     echo>> $IFFILE
133 done
134
135 # dns bootoption
136 if [ -n "$DNSSERVER1" ]
137 then
138         # disable any existing entries
139         if [ -r $RESOLVCONF ]
140         then
141                 sed -i 's/nameserver/# nameserver/' $RESOLVCONF
142         fi
143         for i in $DNSSERVER1 $DNSSERVER2
144         do
145                 echo "nameserver $i" >> $RESOLVCONF
146         done
147 fi
148
149 }