6 source /etc/init.d/pl_functions
8 # the name of the floppy based network configuration
9 # files (checked first). the name planet.cnf is kept
10 # for backward compatibility with old nodes, and only
11 # the floppy disk is searched for files with this name.
12 # new files are named plnode.txt and can be located on
13 # a floppy or usb device or on the cdrom
14 OLD_NODE_CONF_NAME=planet.cnf
15 NEW_NODE_CONF_NAME=plnode.txt
17 # one location of cd-based network configuration file
18 # (checked if floppy conf file missing and usb
19 # configuration file is missing)
20 CD_NET_CONF_BOOT=/usr/boot/$NEW_NODE_CONF_NAME
22 # the other location of cd-based network configuration file
23 CD_NET_CONF_ROOT=/usr/$NEW_NODE_CONF_NAME
25 # if all other network configuration file sources
26 # don't exist, fall back to this one (always on the cd)
27 FALLBACK_NET_CONF=/usr/boot/default-node.txt
29 # a temporary place to hold the old configuration file
30 # off of the floppy disk if we find it (so we don't have
31 # to remount the floppy later)
32 TMP_OLD_FLOPPY_CONF_FILE=/tmp/oldfloppy_planet.cnf
34 # once a configuration file is found, save it in /tmp
35 # (may be used later by boot scripts)
36 USED_NET_CONF=/tmp/planet.cnf
38 # default device to use for contacting PLC if not specified
39 # in the configuration file
42 # where to store the temporary dhclient conf file
43 DHCLIENT_CONF_FILE=/tmp/dhclient.conf
45 # which fs types we support finding node configuration files on
46 # (will be based as a -t parameter to mount)
47 NODE_CONF_DEVICE_FS_TYPES="msdos,ext2"
49 # a temporary place to mount devices that might contain configuration
51 CONF_DEVICE_MOUNT_POINT=/mnt/confdevice
52 /bin/mkdir -p $CONF_DEVICE_MOUNT_POINT
54 # for some backwards compatibility, save the ifconfig <device>
55 # output to this file after everything is online
56 IFCONFIG_OUTPUT=/tmp/ifconfig
58 # set to 1 if the default network configuration was loaded off the cd
59 # (no other configuration file found)
63 function net-init-failed() {
64 verbose-message "pl_netinit: network initialization failed with interface $ETH_DEVICE"
65 verbose-forensics "failed to configure $ETH_DEVICE"
66 hang-and-shutdown "net-init-failed"
70 # find and parse a node network configuration file. return 0 if not found,
71 # return 1 if found and parsed. if this is the case, DEFAULT_NET_CONF will
72 # be set to 1. For any found configuration file, $USED_NET_CONF will
73 # contain the validated contents
74 function find-node-config() {
75 /bin/rm -f $TMP_OLD_FLOPPY_CONF_FILE 2>&1 > /dev/null
77 verbose-message "pl_netinit: looking for node configuration file on floppy"
79 /bin/mount -o ro -t $NODE_CONF_DEVICE_FS_TYPES /dev/fd0 \
80 $CONF_DEVICE_MOUNT_POINT 2>&1 > /dev/null
81 if [[ $? -eq 0 ]]; then
83 # 1. check for new named file first on the floppy disk
84 if [ -r "$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME" ]; then
85 conf_file="$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME"
86 verbose-message "pl_netinit: found node configuration file $conf_file"
87 /etc/init.d/pl_validateconf < $conf_file > $USED_NET_CONF
88 /bin/umount $CONF_DEVICE_MOUNT_POINT
91 # since we have the floppy mounted already, see if an old file
92 # exists there so we don't have to remount the floppy when we need
93 # to check for an old file on it (later in the order). if it does
94 # just copy it off to a special location
95 elif [ -r "$CONF_DEVICE_MOUNT_POINT/$OLD_NODE_CONF_NAME" ]; then
96 conf_file="$CONF_DEVICE_MOUNT_POINT/$OLD_NODE_CONF_NAME"
97 /bin/cp -f $conf_file $TMP_OLD_FLOPPY_CONF_FILE
98 verbose-message "pl_netinit: found old named configuration file, checking later."
100 verbose-message "pl_netinit: floppy mounted, but no configuration file."
103 /bin/umount $CONF_DEVICE_MOUNT_POINT
105 verbose-message "pl_netinit: no floppy could be mounted, continuing search."
108 # 2. check for a new named file on removable flash devices (those
109 # that start with sd*, because usb_storage emulates scsi devices).
110 # to prevent checking normal scsi disks, also make sure
111 # /sys/block/<dev>/removable is set to 1
113 verbose-message "pl_netinit: looking for node configuration file on flash based devices"
115 # make the sd* hd* expansion fail to an empty string if there are no sd
119 for device in /sys/block/[hsv]d*; do
120 removable=$(cat $device/removable)
121 if [[ $removable -ne 1 ]]; then
125 partitions=$(/bin/awk "\$4 ~ /$(basename $device)[0-9]*/ { print \$4 }" /proc/partitions)
126 for partition in $partitions ; do
127 check_dev=/dev/$partition
129 verbose-message "pl_netinit: looking for node configuration file on device $check_dev"
130 /bin/mount -o ro -t $NODE_CONF_DEVICE_FS_TYPES $check_dev \
131 $CONF_DEVICE_MOUNT_POINT 2>&1 > /dev/null
132 if [[ $? -eq 0 ]]; then
133 if [ -r "$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME" ]; then
134 conf_file="$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME"
135 verbose-message "pl_netinit: found node configuration file $conf_file"
136 /etc/init.d/pl_validateconf < $conf_file > $USED_NET_CONF
137 verbose-message "pl_netinit: found configuration"
138 /bin/umount $CONF_DEVICE_MOUNT_POINT
142 verbose-message "pl_netinit: ERROR - not found"
144 /bin/umount $CONF_DEVICE_MOUNT_POINT
149 # normal filename expansion setting
152 # 3. see if there is an old file on the floppy disk. if there was,
153 # the file $TMP_OLD_FLOPPY_CONF_FILE will be readable.
154 if [ -r "$TMP_OLD_FLOPPY_CONF_FILE" ]; then
155 conf_file=$TMP_OLD_FLOPPY_CONF_FILE
156 verbose-message "pl_netinit: found node configuration file $conf_file"
157 /etc/init.d/pl_validateconf < $conf_file > $USED_NET_CONF
162 # 4. check for plnode.txt on the cd at /usr/boot
163 verbose-message "pl_netinit: looking for network configuration on cd in /usr/boot"
164 if [ -r "$CD_NET_CONF_BOOT" ]; then
166 verbose-message "pl_netinit: found cd configuration file $CD_NET_BOOT_CONF"
167 /etc/init.d/pl_validateconf < $CD_NET_CONF_BOOT > $USED_NET_CONF
172 # 5. check for plnode.txt on the cd at /usr
173 verbose-message "pl_netinit: looking for network configuration on cd in /usr"
174 if [ -r "$CD_NET_CONF_ROOT" ]; then
176 verbose-message "pl_netinit: found cd configuration file $CD_NET_CONF_ROOT"
177 /etc/init.d/pl_validateconf < $CD_NET_CONF_ROOT > $USED_NET_CONF
182 # 6. no node configuration file could be found, fall back to
183 # builtin default. this can't be used to install a machine, but
184 # will at least let it download and run the boot manager, which
185 # can inform the users appropriately.
186 verbose-message "pl_netinit: using default network configuration"
187 if [ -r "$FALLBACK_NET_CONF" ]; then
188 verbose-message "pl_netinit: found cd default configuration file $FALLBACK_NET_CONF"
189 /etc/init.d/pl_validateconf < $FALLBACK_NET_CONF > $USED_NET_CONF
198 verbose-message "pl_netinit: bringing loopback network device up"
199 ip address add dev lo 127.0.0.1/8
203 if [ $? -eq 0 ]; then
204 # no network configuration file found. this should not happen as the
205 # default cd image has a backup one. halt.
206 verbose-message "pl_netinit: ERROR - unable to find even a default network configuration"
207 verbose-message "pl_netinit: file, this cd may be corrupt."
211 # load the configuration file. if it was a default one (not user specified),
212 # then remove the saved copy from /tmp, but continue on. since a network
213 # configuration file is required and boot scripts only know about this location
214 # they will fail (as they should) - but the network will be up if dhcp is
217 verbose-message "pl_netinit: loading network configuration"
220 if [[ $DEFAULT_NET_CONF -eq 1 ]]; then
221 /bin/rm -f $USED_NET_CONF
224 # initialize IPMI device
225 if [[ -n "$IPMI_ADDRESS" ]] ; then
226 echo -n "pl_netinit: initializing IPMI: "
227 cmd="ipnmac -i $IPMI_ADDRESS"
228 if [[ -n "$IPMI_MAC" ]] ; then
229 cmd="$cmd -m $IPMI_MAC"
235 # now, we need to find which device to use (ie, eth0 or eth1). start out
236 # by defaulting to eth0, then see if the network configuration file specified
237 # either a mac address (in which case we will need to find the device), or
241 if [[ -n "$NET_DEVICE" ]]; then
242 # the user specified a mac address we should use. find the network
244 NET_DEVICE=$(tr A-Z a-z <<< $NET_DEVICE)
248 dev_address=$(cat $device/address | tr A-Z a-z)
249 if [ "$device" == "$NET_DEVICE" -o "$dev_address" == "$NET_DEVICE" ]; then
251 verbose-message "pl_netinit: found device $ETH_DEVICE with mac address $dev_address"
258 # if we didn't find a device yet, check which is the primary
259 if [[ -z "$ETH_DEVICE" ]]; then
260 pushd /etc/sysconfig/network-scripts > /dev/null
261 for conf in ifcfg-*; do
262 egrep -q '^PRIMARY=["'"'"']?[yY1t]' $conf || continue
263 ETH_DEVICE=${conf#ifcfg-}
269 # still nothing? fail the boot.
270 if [[ -z "$ETH_DEVICE" ]]; then
271 verbose-message "pl_netinit: unable to find a usable device, check to make sure"
272 verbose-message "pl_netinit: the NET_DEVICE field in the configuration file"
273 verbose-message "pl_netinit: corresponds with a network adapter on this system"
277 # within a systemd-driven startup, we often see this stage
278 # triggered before the network interface is actually exposed
280 # so allow for some small delay here
284 ### check if the interface is known
285 # instead of using ifconfig, check the kernel area
286 if [ -e /sys/class/net/$ETH_DEVICE ]; then
287 verbose-message "pl_netinit: device present $ETH_DEVICE"
290 verbose-message "pl_netinit: waiting for device $ETH_DEVICE - ${COUNTER}s/${ALLOW}s"
292 COUNTER=$(($COUNTER+1))
293 [ $COUNTER -ge $ALLOW ] && net-init-failed
298 # actually check to make sure ifconfig <device> succeeds
300 verbose-message Initializing $ETH_DEVICE
301 /usr/libexec/nm-ifup $ETH_DEVICE || /sbin/ifconfig $ETH_DEVICE up || {
302 verbose-message "pl_netinit: device $ETH_DEVICE does not exist, most likely"
303 verbose-message "pl_netinit: this CD does not have hardware support for your"
304 verbose-message "pl_netinit: network adapter. please send the following lines"
305 verbose-message "pl_netinit: to your PlanetLab support for further assistance"
309 # my understanding is this is used to upload to MyPLC
310 # details like mac addresses and the like
311 # for backwards compatibility, in a best-effort manner,
312 # we try and compensate for the disappearance of ifconfig
313 ( /sbin/ifconfig $ETH_DEVICE || ip address show $ETH_DEVICE ) > $IFCONFIG_OUTPUT
315 verbose-message "pl_netinit: network online"
317 # patch for f22 and above - if /etc/resolv.conf is empty or missing
318 function pl-netinit-patch-resolv-conf () {
321 # missing file : patch needed
322 if ! [ -f $file ]; then
324 # empty file : patch needed
326 contents=$(grep -v '^#' $file)
327 contents=$(echo $contents)
328 [ -z "$contents" ] && needed=true
330 if [ -z "$needed" ]; then
331 echo pl_netinit has no need to patch $file
335 verbose-message "pl_netinit: patching $file"
336 source /etc/sysconfig/network-scripts/ifcfg-${ETH_DEVICE}
337 # delete because it may be a symlink to /run/systemd/resolve/resolv.conf
338 # which looks really weird (ls -lL /etc/resolv.conf does not show anything)
340 # mention the DNS servers defined in the config - if any
341 [ -n "$DNS1" ] && echo nameserver $DNS1 >> $file
342 [ -n "$DNS2" ] && echo nameserver $DNS2 >> $file
343 # also add landmark for good measure
344 echo "nameserver 8.8.8.8" >> $file
347 pl-netinit-patch-resolv-conf /etc/resolv.conf
349 verbose-forensics "pl_netinit epilogue"