write out the output of ifconfig to /tmp for backward compatibility.
[bootcd.git] / conf_files / pl_netinit
1 #!/bin/sh
2
3 # the name of the floppy based network configuration
4 # files (checked first). the name planet.cnf is kept
5 # for backward compatibility with old nodes
6 FLOPPY_NET_CONF=planet.cnf
7
8 # location of cd-based network configuration file
9 # (checked if floppy conf file missing)
10 USER_NET_CONF=/usr/boot/user-net.cnf
11
12 # if all other network configuration file sources 
13 # don't exist, fall back to this one (always on the cd)
14 FALLBACK_NET_CONF=/usr/boot/default-net.cnf
15
16 # once a configuration file is found, save it in /tmp
17 # (may be used later by boot scripts)
18 USED_NET_CONF=/tmp/planet.cnf
19
20 # default device to use for contacting PLC
21 DEFAULT_NET_DEV=eth0
22
23 # where to store the temporary dhclient conf file
24 DHCLIENT_CONF_FILE=/tmp/dhclient.conf
25
26 # which fs types we support floppy disks in
27 FLOPPY_FS_TYPES="msdos,ext2"
28
29 FLOPPY_MOUNT_POINT=/mnt/floppy
30 /bin/mkdir -p $FLOPPY_MOUNT_POINT
31
32 # for some backwards compatibility, save the ifconfig <device>
33 # output to this file
34 IFCONFIG_OUTPUT=/tmp/ifconfig
35
36
37 net_init_failed()
38 {
39     echo
40     echo "pl_netinit: network initialization failed,"
41     echo "pl_netinit: shutting down machine in two hours"
42     /bin/sleep 7200
43     /sbin/shutdown -h now
44     exit 1
45 }
46
47 # Function for checking the IP address to see if its sensible.
48 check_ip()
49 {
50     case "$*" in
51         "" | *[!0-9.]* | *[!0-9]) return 1 ;;
52     esac
53     local IFS=.
54     set -- $*
55     [ $# -eq 4 ] &&
56     [ ${1:-666} -le 255 ] && [ ${2:-666} -le 255 ] &&
57     [ ${3:-666} -le 255 ] && [ ${4:-666} -le 255 ]
58 }
59
60 echo "pl_netinit: bringing loopback network device up"
61 /sbin/ifconfig lo 127.0.0.1 up
62
63
64 DEFAULT_NET_CONF=0
65 FOUND_NET_CONF=0
66
67 echo "pl_netinit: looking for network configuration on floppy"
68 /bin/mount -o ro -t $FLOPPY_FS_TYPES /dev/fd0 $FLOPPY_MOUNT_POINT 2>&1 > /dev/null
69 if [[ $? -eq 0 ]]; then
70     floppy_conf_file="$FLOPPY_MOUNT_POINT/$FLOPPY_NET_CONF"
71     if [ -r "$floppy_conf_file" ]; then
72
73         echo "pl_netinit: found floppy configuration file, using"
74         /etc/init.d/pl_validateconf < $floppy_conf_file > $USED_NET_CONF
75         FOUND_NET_CONF=1
76     else
77         echo "pl_netinit: floppy mounted, but no configuration file."
78     fi
79     /bin/umount $FLOPPY_MOUNT_POINT
80 else
81     echo "pl_netinit: no floppy found"
82 fi
83
84
85 if [[ $FOUND_NET_CONF -eq 0 ]]; then
86     echo "pl_netinit: looking for network configuration on cd"
87     if [ -r "$USER_NET_CONF" ]; then
88
89         echo "pl_netinit: found cd configuration file, using"
90         /etc/init.d/pl_validateconf < $USER_NET_CONF > $USED_NET_CONF
91         FOUND_NET_CONF=1
92     fi
93 fi
94
95 if [[ $FOUND_NET_CONF -eq 0 ]]; then
96     echo "pl_netinit: using default network configuration"
97     if [ -r "$FALLBACK_NET_CONF" ]; then
98
99         echo "pl_netinit: found cd default configuration file, using"
100         /etc/init.d/pl_validateconf < $FALLBACK_NET_CONF > $USED_NET_CONF
101         FOUND_NET_CONF=1
102         DEFAULT_NET_CONF=1
103     fi
104 fi
105
106
107 if [[ $FOUND_NET_CONF -eq 0 ]]; then
108     # no network configuration file found. this should not happen as the
109     # default cd image has a backup one. halt.
110     echo "pl_netinit: unable to find even a default network configuration"
111     echo "pl_netinit: file, this cd may be corrupt."
112     net_init_failed
113 fi
114
115 # load the configuration file. if it was a default one (not user specified),
116 # then remove the saved copy from /tmp, but continue on. since a network 
117 # configuration file is required and boot scripts only know about this location
118 # they will fail (as they should) - but the network will be up if dhcp is
119 # available
120
121 echo "pl_netinit: loading network configuration"
122 . $USED_NET_CONF
123
124 if [[ $DEFAULT_NET_CONF -eq 1 ]]; then
125     /bin/rm -f $USED_NET_CONF
126 fi
127
128 # now, we need to find which device to use (ie, eth0 or eth1). start out
129 # by defaulting to eth0, then see if the network configuration file specified
130 # either a mac address (in which case we will need to find the device), or
131 # the device itself
132
133 if [[ -n "$PRIMARY_MAC" ]]; then
134     # the user specified a mac address we should use. find the network
135     # device for it.
136     echo "pl_netinit: looking for a device with mac address $PRIMARY_MAC"
137
138     pushd .
139     cd /sys/class/net
140     for device in *; do
141         dev_address=`cat $device/address`
142         if [[ "$dev_address" == "$PRIMARY_MAC" ]]; then
143             ETH_DEVICE=$device
144             echo "pl_netinit: found device $ETH_DEVICE"
145             break
146         fi
147     done
148     popd
149
150 elif [[ -n "$PRIMARY_DEV" ]]; then
151     # the user specified a virtual ethernet device to use.
152     ETH_DEVICE=$PRIMARY_DEV
153     echo "pl_netinit: using user specified device $ETH_DEVICE"
154
155 else
156     ETH_DEVICE=$DEFAULT_NET_DEV
157     echo "pl_netinit: using default device $ETH_DEVICE"
158
159 fi
160
161
162 # if we couldn't find a device (would happen if PRIMARY_MAC was specified
163 # but we couldn't find a device for that addresS), then abort the rest
164 # of the startup
165
166 if [[ -z "$ETH_DEVICE" ]]; then
167     echo "pl_netinit: unable to find a usable device, check to make sure"
168     echo "pl_netinit: the PRIMARY_MAC field in the configuration file"
169     echo "pl_netinit: cooresponds with a network adapter on this system"
170     net_init_failed
171 fi
172
173
174 # actually check to make sure ifconfig <device> succeeds
175 /sbin/ifconfig $ETH_DEVICE up 2>&1 > /dev/null
176 if [[ $? -ne 0 ]]; then
177     echo "pl_netinit: device $ETH_DEVICE does not exist,"
178     echo "pl_netinit: this is due to either the device not existing,"
179     echo "pl_netinit: or its device drivers not being loaded"
180     net_init_failed
181 fi
182
183 if [[ "$IP_METHOD" == "dhcp" ]]; then
184     echo "pl_netinit: attempting to bring up device with dhcp"
185
186     # setup a dhclient conf file for this device (used to send
187     # our hostname to the dhcp server)
188     echo "interface \"$ETH_DEVICE\" {" > $DHCLIENT_CONF_FILE
189     echo "send host-name \"$HOST_NAME.$DOMAIN_NAME\";" >> $DHCLIENT_CONF_FILE
190     echo "}" >> $DHCLIENT_CONF_FILE
191
192     # touch the redhat net device configuration file so 
193     # dhclient doesn't complain
194     /bin/touch /etc/sysconfig/network-scripts/ifcfg-$ETH_DEVICE
195
196     configured=0
197     while [[ $configured -eq 0 ]]; do
198         /sbin/dhclient -1 -cf $DHCLIENT_CONF_FILE $ETH_DEVICE 
199         if [[ $? -ne 0 ]]; then
200             echo "pl_netinit: dhcp failed, retrying in 2 minutes"
201             /sbin/sleep 120
202         else
203             echo "pl_netinit: dhcp succeeded"
204             configured=1
205             break
206         fi
207     done
208 else
209     echo "pl_netinit: configuring device statically"
210
211     /sbin/ifconfig $ETH_DEVICE $IP_ADDRESS broadcast $IP_BROADCASTADDR \
212         netmask $IP_NETMASK
213     /sbin/route add default gw $IP_GATEWAY dev $ETH_DEVICE
214
215     if [[ -z "$IP_DNS1" ]]; then
216         echo "pl_netinit: no dns server specified, cannot continue."
217         net_init_failed
218     fi
219
220     echo "nameserver $IP_DNS1" > /etc/resolv.conf
221     if [[ -n "$IP_DNS2" ]]; then
222         echo "nameserver $IP_DNS2" >> /etc/resolv.conf
223     fi
224 fi
225
226 # for backwards compatibility
227 /sbin/ifconfig $ETH_DEVICE > $IFCONFIG_OUTPUT
228
229 echo "pl_netinit: network online"
230