diff --git a/kvmapp/system/init.d/S30eth b/kvmapp/system/init.d/S30eth index 17db97f..604a673 100755 --- a/kvmapp/system/init.d/S30eth +++ b/kvmapp/system/init.d/S30eth @@ -1,119 +1,67 @@ #!/bin/sh -. /etc/profile +RESERVE_INET="192.168.90.1/22" -# /boot/eth.nodhcp for example -# ipaddr/net gw[optional] -# 192.168.0.101/24 192.168.0.1 -# 192.168.3.116/22 +clean_line() { + echo "$1" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//;s/\r//g' +} -RESERVE_INET="192.168.0.1/24" +calc_gw() { + addr="$1" + netid="$2" + IFS=. read a b c d <>24)&255 )).$(( (gwnum>>16)&255 )).$(( (gwnum>>8)&255 )).$(( gwnum&255 ))" +} start() { - printf "start ethernet: " - if [ -e /boot/eth.nodhcp ] - then - [ -e /boot/eth.nodhcp ] && - cat /boot/eth.nodhcp | while read inet gw - do - addr=${inet%/*} - netid=${inet#*/} - [ -z $gw ] && - gw=$( echo $addr| ( IFS='.' read a b c d; echo $(( - (((((($a<<8)+$b)<<8)+$c)<<8)+$d) - & (((1<<$netid)-1)<<(32-$netid)) - )) - )) && - gw=$(($gw>>24&0xff)).$(($gw>>16&0xff)).$(($gw>>8&0xff)).$((1+( $gw>>0&0xff ))) + if [ -e /boot/eth.nodhcp ]; then + while IFS= read -r line || [ -n "$line" ]; do + line=$(clean_line "$line") + [ -z "$line" ] && continue + set -- $line + inet="$1" + gw="$2" + [ -z "$inet" ] && continue + addr=${inet%/*} + netid=${inet#*/} + [ -z "$gw" ] && gw=$(calc_gw "$addr" "$netid") + arping -Dqc2 -Ieth0 "$addr" || continue + ip a add "$inet" brd + dev eth0 + ip r add default via "$gw" dev eth0 + echo -e "nameserver $gw" >> /etc/resolv.conf + break + done < /boot/eth.nodhcp - arping -Dqc2 -Ieth0 $addr || continue - ip a add $inet brd + dev eth0 - ip r add default via $gw dev eth0 - cp -f /etc/resolv.conf /etc/resolv.conf.old - cat > /etc/resolv.conf << EOF -nameserver $gw -nameserver 8.8.4.4 -nameserver 8.8.8.8 -nameserver 114.114.114.114 -nameserver 119.29.29.29 -nameserver 223.5.5.5 -EOF - break - done && - ip a show dev eth0|grep inet || ( - udhcpc -i eth0 -t 3 -T 1 -A 5 -b -p /run/udhcpc.eth0.pid &>/dev/null - ip a show dev eth0|grep inet - ) || ( - # failed to apply dynamic addr, need a available static addr to visit the LAN - inet=$RESERVE_INET - addr=${inet%/*} - ip a add $inet brd + dev eth0 - ) || exit 1 + ip a show dev eth0 | grep inet > /dev/null || { + udhcpc -i eth0 -t 3 -T 1 -A 5 -b -p /run/udhcpc.eth0.pid &>/dev/null + ip a show dev eth0 | grep inet > /dev/null + } || { + inet=$RESERVE_INET + addr=${inet%/*} + ip a add "$inet" brd + dev eth0 + } || exit 1 else - (udhcpc -i eth0 -t 10 -T 1 -A 5 -b -p /run/udhcpc.eth0.pid) & + udhcpc -i eth0 -t 10 -T 1 -A 5 -b -p /run/udhcpc.eth0.pid & fi - - echo "OK" } + stop() { - [[ ! -e "/run/udhcpc.eth0.pid" ]] && echo "udhcpc is not running..." && exit 1 - kill `cat /run/udhcpc.eth0.pid` + [ ! -e "/run/udhcpc.eth0.pid" ] && exit 1 + kill "$(cat /run/udhcpc.eth0.pid)" rm /run/udhcpc.eth0.pid } case "$1" in - start) - start - ;; - stop) - stop - ;; - restart|reload) - $0 stop - $0 start - ;; - doc) - cat <