Commit 493fe69e authored by Jean-Claude BAU's avatar Jean-Claude BAU

New Startup behavior related to the DHCP service availability

After a general cut in the electrical power supply, the switch can restart before than other generic systems (routeurs, serveurs, DHCP & NTP services,...).
It results that the switch might be not well configured if these services cannot be reached and especially the DHCP service.

So, a new behavior has been introduced to wait for the availability of the DHCP service (if configured) to get the source of the dot-config file and to setup the management port.
parents ea7f0af8 75c6b639
......@@ -45,6 +45,7 @@ config DOTCONF_SOURCE_REMOTE
config DOTCONF_SOURCE_FORCE_DHCP
bool "Force to get the URL to a dot-config via DHCP"
depends on ETH0_DHCP
help
Retrieve a URL to the dot-config via DHCP at boot.
The URL can be configured in the "filename" configuration field of
......@@ -1152,7 +1153,7 @@ config FAN_HYSTERESIS
be used during development to reduce noise generated by a switch.
Don't use in production as this may affect the synchronization
performance.
config FAN_HYSTERESIS_T_DISABLE
int "Disable fans temperature"
default 60
......
......@@ -203,6 +203,7 @@ static int libwr_cfg_read_kconfig(struct kc **all_configs,
struct kc *kc;
int ret = 0;
int len;
int helpIndentation=-1; // Not in help
/* Prevent infinite recursion */
if (depth_level >= READ_KCONFIG_MAX_DEPTH) {
......@@ -227,10 +228,34 @@ static int libwr_cfg_read_kconfig(struct kc **all_configs,
return -1;
while (fgets(s, sizeof(s), f)) {
char *ss=s;
int indentation=0;
/* Remove leading spaces */
while ( *ss==' ' || *ss=='\t')
/* The indentation must be measured to make the difference between
* a key word or a text in help section
*/
/* Remove leading spaces and measure indentation */
while ( *ss==' ' || *ss=='\t') {
indentation++;
ss++;
}
if (helpIndentation==-1 ) {
// Not in help section
if (strncmp(ss,"help",4)==0 || strncmp(ss,"---help---",10)==0 ) {
// Enter in help section
helpIndentation=indentation;
continue;
}
} else {
// In help section
if ( indentation>=helpIndentation )
continue; // Still in help section
helpIndentation=-1; // Exiting help section
}
if ( *ss==0 || *ss=='\n' || *ss=='#' )
continue;
if (sscanf(ss, "source %s", name) == 1) {
/* Recursive call for sourced files */
......
......@@ -10,12 +10,28 @@ dotconfig=/wr/etc/dot-config
tmpconfig=/tmp/dot-config
tmpdir=/tmp
log_output=/dev/kmsg
management_port=eth0
me=`basename "$0"`
set -o pipefail # The return value of a pipeline is the status of
# the last command to exit with a non-zero status,
# or zero if no command exited with a non-zero status
# Needed for return value ($?) of xxx in "xxx | tee yyy"
#
# This function is looping forever until DHCP is reachable
#
wait_dhcp_forever()
{
while : ; do
udhcpc -n -q -i $management_port
if [ "$?" -eq "0" ]; then
break
fi
echo "$me: waiting DHCP service...."
sleep 5
done
}
rm -f "$tmpdir"/dot-config_source
......@@ -58,40 +74,44 @@ if [ "$CONFIG_DOTCONF_SOURCE_REMOTE" = "y" ] || [ "$CONFIG_DOTCONF_SOURCE_TRY_DH
# get URL via DHCP
if [ "$CONFIG_DOTCONF_SOURCE_TRY_DHCP" = "y" ] \
|| [ "$CONFIG_DOTCONF_SOURCE_FORCE_DHCP" = "y" ]; then
if [ "$CONFIG_DOTCONF_SOURCE_TRY_DHCP" = "y" ]; then
echo "try_dhcp" > "$tmpdir"/dot-config_source
fi
if [ "$CONFIG_DOTCONF_SOURCE_FORCE_DHCP" = "y" ]; then
echo "force_dhcp" > "$tmpdir"/dot-config_source
fi
# up eth0 to allow dhcp requests
ifconfig eth0 up
# let udhcpc run /wr/bin/dhcp_get_filename.sh script to get "filename"
# from DHCP (which contain url with dot-config).
/sbin/udhcpc -i eth0 -s /wr/bin/dhcp_get_filename.sh -O bootfile -o \
-n -q -f -t 5 &>/dev/null
if [ -f "$tmpdir"/dot-config_source_url ]; then
# replace CONFIG_DOTCONF_URL with one gotten from dhcp
CONFIG_DOTCONF_URL=`cat "$tmpdir"/dot-config_source_url`
echo "Got dot-config's URL ("$CONFIG_DOTCONF_URL") via DHCP" | tee $log_output >& 2
else
echo "dhcp_error" > "$tmpdir"/dot-config_status
echo "Unable to get dot-config's URL via DHCP, using old dot-config" | tee $log_output >& 2
# apply old dot-config
/wr/bin/apply_dot-config
exit
fi
if [ "$CONFIG_DOTCONF_SOURCE_TRY_DHCP" = "y" ]; then
echo "try_dhcp" > "$tmpdir"/dot-config_source
fi
if [ "$CONFIG_DOTCONF_SOURCE_FORCE_DHCP" = "y" ]; then
echo "force_dhcp" > "$tmpdir"/dot-config_source
fi
# up management port to allow dhcp requests
ifconfig $management_port up
if [ "$CONFIG_DOTCONF_SOURCE_FORCE_DHCP" = "y" ] && [ "$CONFIG_ETH0_DHCP" = "y" ]; then
# Wait DHCP forever only if DHCP is also forced on management port
wait_dhcp_forever
fi
# let udhcpc run /wr/bin/dhcp_get_filename.sh script to get "filename"
# from DHCP (which contain url with dot-config).
/sbin/udhcpc -i $management_port -s /wr/bin/dhcp_get_filename.sh -O bootfile -o \
-n -q -f -t 5 &>/dev/null
if [ -f "$tmpdir"/dot-config_source_url ]; then
# replace CONFIG_DOTCONF_URL with one gotten from dhcp
CONFIG_DOTCONF_URL=`cat "$tmpdir"/dot-config_source_url`
echo "Got dot-config's URL ("$CONFIG_DOTCONF_URL") via DHCP" | tee $log_output >& 2
else
echo "dhcp_error" > "$tmpdir"/dot-config_status
echo "Unable to get dot-config's URL via DHCP, using old dot-config" | tee $log_output >& 2
# apply old dot-config
/wr/bin/apply_dot-config
exit
fi
fi
# replace IPADDR and MACADDR, to have a device-specific name
macaddr=$(cat /sys/class/net/eth0/address)
ipaddr=$(ifconfig eth0 | grep inet | cut -d: -f 2 | cut '-d ' -f 1)
macaddr=$(cat /sys/class/net/${management_port}/address)
ipaddr=$(ifconfig ${management_port} | grep inet | cut -d: -f 2 | cut '-d ' -f 1)
if [ -z "$ipaddr" ]; then
# if no IP address available from barebox try to get IP from old dot-config
# /etc/init.d/network will run later again
/etc/init.d/network
ipaddr=$(ifconfig eth0 | grep inet | cut -d: -f 2 | cut '-d ' -f 1)
ipaddr=$(ifconfig ${management_port} | grep inet | cut -d: -f 2 | cut '-d ' -f 1)
fi
host_name=`hostname`
URL=$(echo $CONFIG_DOTCONF_URL | \
......
......@@ -6,12 +6,29 @@
dotconfig=/wr/etc/dot-config
int_file=/etc/network/interfaces
log_output=/dev/kmsg
management_port=eth0
me=`basename "$0"`
#
# This function is looping forever until DHCP is reachable
#
wait_dhcp_forever()
{
while : ; do
udhcpc -n -q -i $management_port
if [ "$?" -eq "0" ]; then
break
fi
echo "$me: waiting DHCP service ...."
sleep 5
done
}
# no matter what we do keep lo up
ifup lo &> /dev/null
if grep -q '/ nfs' /proc/mounts; then
echo "Running via NFS: leaving eth0 config alone" | tee $log_output
echo "Running via NFS: leaving $management_port config alone" | tee $log_output
exit 0
fi
......@@ -22,13 +39,13 @@ fi
# kill all previous instances of udhcpc
killall udhcpc &> /dev/null
# put eth0 down in case it was up before, but it is not so simple
# put management port down in case it was up before, but it is not so simple
# ifdown to change /var/run/ifstate
# flush to aviod ifup complains
# down to take link down (after reboot ifdown does not put link down)
ifdown eth0 &> /dev/null
ip addr flush dev eth0
ip link set eth0 down
ifdown $management_port &> /dev/null
ip addr flush dev $management_port
ip link set $management_port down
# wait after down to make udhcpc to work properly
sleep 1
......@@ -40,7 +57,7 @@ if [ "$CONFIG_ETH0_STATIC" = "y" ] || [ "$CONFIG_ETH0_DHCP_ONCE" = "y" ]; then
echo "iface lo inet loopback" >> $int_file
echo "" >> $int_file
echo "iface eth0 inet static" >> $int_file
echo "iface $management_port inet static" >> $int_file
if [ "$CONFIG_ETH0_IP" ]; then
echo " address $CONFIG_ETH0_IP" >> $int_file
fi
......@@ -61,8 +78,8 @@ fi
if [ "$CONFIG_HOSTNAME_STATIC" = "y" ]; then
if [ -z "$CONFIG_HOSTNAME_STRING" ]; then
echo "empty CONFIG_HOSTNAME_STRING! use wrs" | tee $log_output
CONFIG_HOSTNAME_STRING="wrs"
echo "empty CONFIG_HOSTNAME_STRING! use wrs" | tee $log_output
CONFIG_HOSTNAME_STRING="wrs"
fi
/bin/hostname "$CONFIG_HOSTNAME_STRING"
echo "$CONFIG_HOSTNAME_STRING" | tee $log_output > /etc/hostname
......@@ -73,19 +90,19 @@ fi
if [ "$CONFIG_ETH0_DHCP_ONCE" = "y" ]; then
echo "Try DHCP to get IP" | tee $log_output
# try dhcp, if fail use static IP
udhcpc -i eth0 -n $DHCP_OPT_EXTRA | tee $log_output
udhcpc -i $management_port -n $DHCP_OPT_EXTRA | tee $log_output
if [ $? -ne 0 ]; then
echo "Failed to obtain IP address via DHCP, set static IP" | tee $log_output
CONFIG_ETH0_STATIC="y"
echo "Failed to obtain IP address via DHCP, set static IP" | tee $log_output
CONFIG_ETH0_STATIC="y"
else
exit
exit
fi
fi
if [ "$CONFIG_ETH0_STATIC" = "y" ]; then
# ifup to use static parameters from /etc/netwrok/interfaces
echo "Using static IP" | tee $log_output
ifup eth0
ifup $management_port
exit
fi
......@@ -94,4 +111,5 @@ fi
echo "Using DHCP to get IP" | tee $log_output
# redirect output from udhcpc into syslog and output about the first lease to
# the kernel log (syslog is not started at this point)
udhcpc -S -b -i eth0 $DHCP_OPT_EXTRA | tee $log_output
wait_dhcp_forever
udhcpc -S -b -i $management_port $DHCP_OPT_EXTRA | tee $log_output
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment