network 2.89 KB
Newer Older
1 2 3 4 5
#!/bin/sh
#
# Start the network....
#

6 7
dotconfig=/wr/etc/dot-config
int_file=/etc/network/interfaces
8
log_output=/dev/kmsg
9 10 11 12

# no matter what we do keep lo up
ifup lo &> /dev/null

13
if grep -q '/ nfs' /proc/mounts; then
14
	echo "Running via NFS: leaving eth0 config alone" | tee $log_output
15 16 17
	exit 0
fi

18 19 20 21 22 23 24
# read dot-config
if [ -f $dotconfig ]; then
      . $dotconfig
fi

# kill all previous instances of udhcpc
killall udhcpc &> /dev/null
25 26 27 28 29
# 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
30 31 32 33
ip addr flush dev eth0
ip link set eth0 down
# wait after down to make udhcpc to work properly
sleep 1
34

35
if [ "$CONFIG_ETH0_STATIC" = "y" ] || [ "$CONFIG_ETH0_DHCP_ONCE" = "y" ]; then
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    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

62
if [ "$CONFIG_HOSTNAME_STATIC" = "y" ]; then
63
    if [ -z "$CONFIG_HOSTNAME_STRING" ]; then
64
	echo "empty CONFIG_HOSTNAME_STRING! use wrs" | tee $log_output
65 66
	CONFIG_HOSTNAME_STRING="wrs"
    fi
67
    /bin/hostname "$CONFIG_HOSTNAME_STRING"
68
    echo "$CONFIG_HOSTNAME_STRING"  | tee $log_output > /etc/hostname
69 70 71 72
elif [ "$CONFIG_HOSTNAME_DHCP" = "y" ]; then
    DHCP_OPT_EXTRA="-O hostname -s /wr/bin/dhcp_extra_opt.sh"
fi

73
if [ "$CONFIG_ETH0_DHCP_ONCE" = "y" ]; then
74
    echo "Try DHCP to get IP" | tee $log_output
75
    # try dhcp, if fail use static IP
76
    udhcpc -i eth0 -n $DHCP_OPT_EXTRA | tee $log_output
77
    if [ $? -ne 0 ]; then
78
	echo "Failed to obtain IP address via DHCP, set static IP" | tee $log_output
79
	CONFIG_ETH0_STATIC="y"
80 81
    else
	exit
82 83 84
    fi
fi

85
if [ "$CONFIG_ETH0_STATIC" = "y" ]; then
86
    # ifup to use static parameters from /etc/netwrok/interfaces
87
    echo "Using static IP" | tee $log_output
88
    ifup eth0
89
    exit
90
fi
91

92 93
# 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.
94 95 96 97
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