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