initial commit: 1.5.0
[systemrescuecd.git] / overlay-squashfs-x86 / etc / init.d / netconfig2
1 #!/sbin/runscript
2
3 debugnetcfg2='/var/log/netconfig2.log'
4
5 depend()
6 {
7         before xdm autorun
8         after sysresccd
9 }
10
11 log_netconfig2()
12 {
13         echo "netconfig2 @ $(date +%Y%m%d-%H:%M:%S): ${1}" >> ${debugnetcfg2}
14 }
15
16 print_and_log_netconfig2()
17 {
18         echo "netconfig2 @ $(date +%Y%m%d-%H:%M:%S): ${1}" >> ${debugnetcfg2}
19         echo "netconfig2: ${1}"
20 }
21
22 start()
23 {
24         ebegin "Performing the network configuration..."
25
26         CMDLINE="$(cat /proc/cmdline)"
27
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'
32         then
33                 DHCPHOSTNAME="$(hostname)"
34                 for opt in ${CMDLINE}
35                 do
36                         if echo "${opt}" | grep -q -F 'dhcphostname'
37                         then
38                                 DHCPHOSTNAME=$(echo "${opt}" | cut -d= -f2)
39                                 print_and_log_netconfig2 "DHCPHOSTNAME=${DHCPHOSTNAME}"
40                         fi
41                 done
42                 
43                 NETDEVICES="$(awk -F: '/eth.:|tr.:|ath.:|wlan.:/{print $1}' /proc/net/dev 2>/dev/null)"
44                 dhcpstate=''
45                 attempts=1
46                 maxattempts=25
47                 while [ "${dhcpstate}" != 'ok' ] && [ "${attempts}" -lt "${maxattempts}" ] # retry until we get a dhcp address on at least one interface
48                 do
49                         for curdev in ${NETDEVICES}
50                         do
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'
54                                 then
55                                         linkstate='link-ok'
56                                 elif [ -n "$(which mii-tool)" ] && mii-tool ${curdev} 2>/dev/null | grep -qF 'no link'
57                                 then
58                                         linkstate='no-link'
59                                 else # if not sure, set linkstate='unknown'
60                                         linkstate='unknown'
61                                 fi
62                                 print_and_log_netconfig2  "--- ${curdev}: link=${linkstate}, mac=${mac}"                
63                                 
64                                 # If the link is up, then try (even if another interface already had a dhcp address)
65                                 if [ "${linkstate}" != 'no-link' ]
66                                 then
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}"
69                                         ${cmd} ; res="$?"
70                                         print_and_log_netconfig2  "${cmd} --> ${res}"
71                                         [ "${res}" = '0' ] && dhcpstate='ok'
72                                 fi
73                         done
74                         
75                         if [ "${dhcpstate}" != 'ok' ]
76                         then
77                                 print_and_log_netconfig2 "Cannot get a DHCP address. Check the cables on the ethernet interfaces."
78                         fi
79                         
80                         attempts=$((attempts+1))
81                 done
82         fi
83         
84         # ---- configure the dns nameserver if requested ----
85         for curopt in ${CMDLINE}
86         do
87                 if echo "${curopt}" | grep -q -E '^dns=[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'
88                 then
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}"
93                 fi
94         done
95 }
96