384f3f64f1a3e370208b6d534671b75b42094532
[live-boot-grml.git] / scripts / boot / 9990-netbase.sh
1 #!/bin/sh
2
3 #set -e
4
5 Netbase ()
6 {
7         if [ -n "${NONETWORKING}" ]
8         then
9                 return
10         fi
11
12         # FIXME: stop hardcoding overloading of initramfs-tools functions
13         . /scripts/functions
14         . /lib/live/boot/9990-initramfs-tools.sh
15
16         log_begin_msg "Preconfiguring networking"
17
18         IFFILE="/root/etc/network/interfaces"
19         DNSFILE="/root/etc/resolv.conf"
20
21         if [ "${STATICIP}" = "frommedia" ] && [ -e "${IFFILE}" ]
22         then
23                 # will use existent /etc/network/interfaces
24                 log_end_msg
25                 return
26         fi
27
28 cat > "${IFFILE}" << EOF
29 auto lo
30 iface lo inet loopback
31
32 EOF
33
34         udevadm trigger
35         udevadm settle
36
37         if [ -z "${NETBOOT}" ] && [ -n "${STATICIP}" ] && [ "${STATICIP}" != "frommedia" ]
38         then
39                 parsed=$(echo "${STATICIP}" | sed -e 's|,| |g')
40
41                 for ifline in ${parsed}
42                 do
43                         ifname="$(echo ${ifline} | cut -f1 -d ':')"
44                         ifaddress="$(echo ${ifline} | cut -f2 -d ':')"
45                         ifnetmask="$(echo ${ifline} | cut -f3 -d ':')"
46                         ifgateway="$(echo ${ifline} | cut -f4 -d ':')"
47                         nameserver="$(echo ${ifline} | cut -f5 -d ':')"
48
49 cat >> "${IFFILE}" << EOF
50 allow-hotplug ${ifname}
51 iface ${ifname} inet static
52     address ${ifaddress}
53     netmask ${ifnetmask}
54 EOF
55
56                         if [ -n "${ifgateway}" ]
57                         then
58
59 cat >> "${IFFILE}" << EOF
60     gateway ${ifgateway}
61
62 EOF
63
64                         fi
65
66                         if [ -n "${nameserver}" ]
67                         then
68
69 cat >> "${DNSFILE}" << EOF
70 nameserver ${nameserver}
71 EOF
72
73                         fi
74                 done
75         else
76                 if [ -z "${NETBOOT}" ] || [ -n "${DHCP}" ]
77                 then
78                         # default, dhcp assigned
79                         method="dhcp"
80                 else
81                         # make sure that the preconfigured interface would not get reassigned by dhcp
82                         # on startup by ifup script - otherwise our root fs might be disconnected!
83                         method="manual"
84                 fi
85
86                 # iterate the physical interfaces and add them to the interfaces list and also add when ethdevice= called on cmdline
87                 if [ "${method}" != dhcp ] || ([ ! -x /root/usr/sbin/NetworkManager ] && [ ! -x /root/usr/sbin/wicd ]) || [ ! -z "${ETHDEVICE}" ]
88                 then
89                         for interface in /sys/class/net/eth* /sys/class/net/ath* /sys/class/net/wlan*
90                         do
91                                 [ -e ${interface} ] || continue
92                                 i="$(basename ${interface})"
93
94 cat >> "${IFFILE}" << EOF
95 allow-hotplug ${i}
96 iface ${i} inet ${method}
97
98 EOF
99
100                         done
101                 fi
102
103                 if [ ! -f /root/etc/resolv.conf ] || [ -z "$(cat /root/etc/resolv.conf)" ]
104                 then
105                         if [ -f /netboot.config ]
106                         then
107                                 # create a resolv.conf if it is not present or empty
108                                 cp /netboot.config /root/var/log/netboot.config
109
110                                 rc_search=$(cat netboot.config | awk '/domain/{print $3}')
111                                 rc_server0=$(cat netboot.config | awk '/dns0/{print $5}')
112                                 rc_server1=$(cat netboot.config | awk '/dns0/{print $8}')
113                                 rc_server0="nameserver ${rc_server0}"
114
115                                 if [ "${rc_server1}" = "0.0.0.0" ]
116                                 then
117                                         rc_server1=""
118                                 else
119                                         rc_server1="nameserver ${rc_server1}"
120                                 fi
121
122 cat > /root/etc/resolv.conf << EOF
123 # /etc/resolv.conf
124 # Autogenerated by live-boot
125 search ${rc_search}
126 domain ${rc_search}
127 ${rc_server0}
128 ${rc_server1}
129 EOF
130
131                                 cat /root/etc/resolv.conf >> /root/var/log/netboot.config
132                         fi
133                 fi
134         fi
135
136         #if [ ! -x /root/usr/sbin/NetworkManager ]
137         #then
138         #       for i in eth0 eth1 eth2 ath0 wlan0
139         #       do
140         #               grep -q "iface ${i}" ${IFFILE} && continue
141         #
142         #cat >> "${IFFILE}" << EOF
143         #allow-hotplug ${i}
144         #iface ${i} inet dhcp
145         #
146         #EOF
147         #
148         #       done
149         #fi
150
151         log_end_msg
152 }