3 debugnetcfg2='/var/log/netconfig2.log'
13 echo "netconfig2 @ $(date +%Y%m%d-%H:%M:%S): ${1}" >> ${debugnetcfg2}
16 print_and_log_netconfig2()
18 echo "netconfig2 @ $(date +%Y%m%d-%H:%M:%S): ${1}" >> ${debugnetcfg2}
19 echo "netconfig2: ${1}"
24 ebegin "Performing the network configuration..."
26 CMDLINE="$(cat /proc/cmdline)"
28 # ---- run DHCP if asked on cmdline (dodhcp) ----
29 # we must run dhcp from bashlogin even if it has already been done in linuxrc since it updates
30 # system files such as resolv.conf
31 if echo "${CMDLINE}" | grep -q -F 'dodhcp'
33 DHCPHOSTNAME="$(hostname)"
36 if echo "${opt}" | grep -q -F 'dhcphostname'
38 DHCPHOSTNAME=$(echo "${opt}" | cut -d= -f2)
39 print_and_log_netconfig2 "DHCPHOSTNAME=${DHCPHOSTNAME}"
43 NETDEVICES="$(awk -F: '/eth.:|tr.:|ath.:|wlan.:/{print $1}' /proc/net/dev 2>/dev/null)"
47 while [ "${dhcpstate}" != 'ok' ] && [ "${attempts}" -lt "${maxattempts}" ] # retry until we get a dhcp address on at least one interface
49 for curdev in ${NETDEVICES}
51 # Try to find whether or not the link is connected
52 mac=$(ifconfig ${curdev} | grep HWaddr | awk '{print $5}')
53 if [ -n "$(which mii-tool)" ] && mii-tool ${curdev} 2>/dev/null | grep -qF 'link ok'
56 elif [ -n "$(which mii-tool)" ] && mii-tool ${curdev} 2>/dev/null | grep -qF 'no link'
59 else # if not sure, set linkstate='unknown'
62 print_and_log_netconfig2 "--- ${curdev}: link=${linkstate}, mac=${mac}"
64 # If the link is up, then try (even if another interface already had a dhcp address)
65 if [ "${linkstate}" != 'no-link' ]
67 print_and_log_netconfig2 "Attempt ${attempts} of ${maxattempts} to get a DHCP address on ${curdev}..."
68 cmd="dhcpcd -L -n -t 10 -I '' -h ${DHCPHOSTNAME} ${curdev}"
70 print_and_log_netconfig2 "${cmd} --> ${res}"
71 [ "${res}" = '0' ] && dhcpstate='ok'
75 if [ "${dhcpstate}" != 'ok' ]
77 print_and_log_netconfig2 "Cannot get a DHCP address. Check the cables on the ethernet interfaces."
80 attempts=$((attempts+1))
84 # ---- configure the dns nameserver if requested ----
85 for curopt in ${CMDLINE}
87 if echo "${curopt}" | grep -q -E '^dns=[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'
89 setdns="$(echo ${curopt} | sed -r -e 's!^dns=([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$!\1!g')"
90 rm -f /etc/resolv.conf 2>/dev/null
91 echo "nameserver ${setdns}" > /etc/resolv.conf
92 print_and_log_netconfig2 "set nameserver to ${setdns}"