Skip to content
Snippets Groups Projects
network 2.89 KiB
#!/bin/sh
#
# Start the network....
#

dotconfig=/wr/etc/dot-config
int_file=/etc/network/interfaces
log_output=/dev/kmsg

# 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
	exit 0
fi

# read dot-config
if [ -f $dotconfig ]; then
      . $dotconfig
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
# 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
# wait after down to make udhcpc to work properly
sleep 1

if [ "$CONFIG_ETH0_STATIC" = "y" ] || [ "$CONFIG_ETH0_DHCP_ONCE" = "y" ]; then
    echo "Write IP address"
    echo "# File generated from dot-config, do not edit!" > $int_file
    echo "# Configure Loopback" >> $int_file
    echo "auto lo" >> $int_file
    echo "iface lo inet loopback" >> $int_file
    echo "" >> $int_file

    echo "iface eth0 inet static" >> $int_file
    if [ "$CONFIG_ETH0_IP" ]; then
	echo "    address $CONFIG_ETH0_IP" >> $int_file
    fi
    if [ "$CONFIG_ETH0_MASK" ]; then
	echo "    netmask $CONFIG_ETH0_MASK" >> $int_file
    fi
    if [ "$CONFIG_ETH0_NETWORK" ]; then
	echo "    network $CONFIG_ETH0_NETWORK" >> $int_file
    fi
    if [ "$CONFIG_ETH0_BROADCAST" ]; then
	echo "    broadcast $CONFIG_ETH0_BROADCAST" >> $int_file
    fi
    if [ "$CONFIG_ETH0_GATEWAY" ]; then
	echo "    gateway $CONFIG_ETH0_GATEWAY" >> $int_file
    fi

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"
    fi
    /bin/hostname "$CONFIG_HOSTNAME_STRING"
    echo "$CONFIG_HOSTNAME_STRING"  | tee $log_output > /etc/hostname
elif [ "$CONFIG_HOSTNAME_DHCP" = "y" ]; then
    DHCP_OPT_EXTRA="-O hostname -s /wr/bin/dhcp_extra_opt.sh"
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
    if [ $? -ne 0 ]; then
	echo "Failed to obtain IP address via DHCP, set static IP" | tee $log_output
	CONFIG_ETH0_STATIC="y"
    else
	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
    exit
fi

# Try to get IP via dhcp if failed run dhcp client forever in background.
# If no information how to get IP address is available use this option.
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