Skip to content

Commit

Permalink
优化网络接口检测逻辑,使用 carrier 文件替代 ethtool,修复 setStaticIP, setWirelessAccount…
Browse files Browse the repository at this point in the history
… 卡住的问题
  • Loading branch information
wjz304 committed Nov 16, 2024
1 parent 01aaca5 commit c091d0d
Show file tree
Hide file tree
Showing 20 changed files with 2,294 additions and 2,570 deletions.
6 changes: 3 additions & 3 deletions files/initrd/opt/rr/boot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ else
while [ ${COUNT} -lt $((${BOOTIPWAIT} + 32)) ]; do
MSG=""
for N in ${ETHX}; do
if ethtool ${N} 2>/dev/null | grep 'Link detected' | grep -q 'yes'; then
if [ "1" = "$(cat /sys/class/net/${N}/carrier 2>/dev/null)" ]; then
MSG+="${N} "
fi
done
Expand All @@ -295,11 +295,11 @@ else
DRIVER=$(ls -ld /sys/class/net/${N}/device/driver 2>/dev/null | awk -F '/' '{print $NF}')
echo -en "${N}(${DRIVER}): "
while true; do
if ! ip link show ${N} 2>/dev/null | grep -q 'UP'; then
if [ -z "$(cat /sys/class/net/${N}/carrier 2>/dev/null)" ]; then
echo -en "\r${N}(${DRIVER}): $(TEXT "DOWN")\n"
break
fi
if ethtool ${N} 2>/dev/null | grep 'Link detected' | grep -q 'no'; then
if [ "0" = "$(cat /sys/class/net/${N}/carrier 2>/dev/null)" ]; then
echo -en "\r${N}(${DRIVER}): $(TEXT "NOT CONNECTED")\n"
break
fi
Expand Down
35 changes: 21 additions & 14 deletions files/initrd/opt/rr/include/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ function _get_fastest() {
function _sort_netif() {
local ETHLIST=""
local ETHX="$(ls /sys/class/net/ 2>/dev/null | grep eth)" # real network cards list
for ETH in ${ETHX}; do
local MAC="$(cat /sys/class/net/${ETH}/address 2>/dev/null | sed 's/://g; s/.*/\L&/')"
local BUS="$(ethtool -i ${ETH} 2>/dev/null | grep bus-info | cut -d' ' -f2)"
ETHLIST="${ETHLIST}${BUS} ${MAC} ${ETH}\n"
for N in ${ETHX}; do
local MAC="$(cat /sys/class/net/${N}/address 2>/dev/null | sed 's/://g; s/.*/\L&/')"
local BUS="$(ethtool -i ${N} 2>/dev/null | grep bus-info | cut -d' ' -f2)"
ETHLIST="${ETHLIST}${BUS} ${MAC} ${N}\n"
done
local ETHLISTTMPM=""
local ETHLISTTMPB="$(echo -e "${ETHLIST}" | sort)"
Expand Down Expand Up @@ -378,19 +378,26 @@ function rebootTo() {
###############################################################################
# connect wlanif
# 1 netif name
# 2 enable/disable (1/0)
function connectwlanif() {
[ -z "${1}" -o ! -d "/sys/class/net/${1}" ] && return 1

local CONF=""
[ -z "${CONF}" -a -f "${PART1_PATH}/wpa_supplicant.conf.${1}" ] && CONF="${PART1_PATH}/wpa_supplicant.conf.${1}"
[ -z "${CONF}" -a -f "${PART1_PATH}/wpa_supplicant.conf" ] && CONF="${PART1_PATH}/wpa_supplicant.conf"
[ -z "${CONF}" ] && return 2

if [ -f "/var/run/wpa_supplicant.pid.${1}" ]; then
kill -9 $(cat /var/run/wpa_supplicant.pid.${1})
rm -f /var/run/wpa_supplicant.pid.${1}
if [ "${2}" = "0" ]; then
if [ -f "/var/run/wpa_supplicant.pid.${1}" ]; then
kill -9 $(cat /var/run/wpa_supplicant.pid.${1})
rm -f /var/run/wpa_supplicant.pid.${1}
fi
else
local CONF=""
[ -z "${CONF}" -a -f "${PART1_PATH}/wpa_supplicant.conf.${1}" ] && CONF="${PART1_PATH}/wpa_supplicant.conf.${1}"
[ -z "${CONF}" -a -f "${PART1_PATH}/wpa_supplicant.conf" ] && CONF="${PART1_PATH}/wpa_supplicant.conf"
[ -z "${CONF}" ] && return 2

if [ -f "/var/run/wpa_supplicant.pid.${1}" ]; then
kill -9 $(cat /var/run/wpa_supplicant.pid.${1})
rm -f /var/run/wpa_supplicant.pid.${1}
fi
wpa_supplicant -i ${1} -c "${CONF}" -B -P "/var/run/wpa_supplicant.pid.${1}" >/dev/null 2>&1
fi
wpa_supplicant -i ${1} -c "${CONF}" -B -P "/var/run/wpa_supplicant.pid.${1}" >/dev/null 2>&1
return 0
}

Expand Down
24 changes: 12 additions & 12 deletions files/initrd/opt/rr/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,25 @@ if [ ! "LOCALBUILD" = "${LOADER_DISK}" ]; then
if arrayExistItem "sortnetif:" $(readConfigMap "addons" "${USER_CONFIG_FILE}"); then
_sort_netif "$(readConfigKey "addons.sortnetif" "${USER_CONFIG_FILE}")"
fi
for ETH in ${ETHX}; do
[ "${ETH::4}" = "wlan" ] && connectwlanif "${ETH}" && sleep 1
MACR="$(cat /sys/class/net/${ETH}/address 2>/dev/null | sed 's/://g')"
for N in ${ETHX}; do
MACR="$(cat /sys/class/net/${N}/address 2>/dev/null | sed 's/://g')"
IPR="$(readConfigKey "network.${MACR}" "${USER_CONFIG_FILE}")"
if [ -n "${IPR}" ]; then
if [ -n "${IPR}" ] && [ "1" = "$(cat /sys/class/net/${N}/carrier 2>/dev/null)" ]; then
IFS='/' read -r -a IPRA <<<"${IPR}"
ip addr flush dev ${ETH}
ip addr add ${IPRA[0]}/${IPRA[1]:-"255.255.255.0"} dev ${ETH}
ip addr flush dev ${N}
ip addr add ${IPRA[0]}/${IPRA[1]:-"255.255.255.0"} dev ${N}
if [ -n "${IPRA[2]}" ]; then
ip route add default via ${IPRA[2]} dev ${ETH}
ip route add default via ${IPRA[2]} dev ${N}
fi
if [ -n "${IPRA[3]:-${IPRA[2]}}" ]; then
sed -i "/nameserver ${IPRA[3]:-${IPRA[2]}}/d" /etc/resolv.conf
echo "nameserver ${IPRA[3]:-${IPRA[2]}}" >>/etc/resolv.conf
fi
sleep 1
fi
[ "${ETH::3}" = "eth" ] && ethtool -s ${ETH} wol g 2>/dev/null || true
# [ "${ETH::3}" = "eth" ] && ethtool -K ${ETH} rxhash off 2>/dev/null || true
[ "${N::4}" = "wlan" ] && connectwlanif "${N}" 1 && sleep 1
[ "${N::3}" = "eth" ] && ethtool -s ${N} wol g 2>/dev/null || true
# [ "${N::3}" = "eth" ] && ethtool -K ${N} rxhash off 2>/dev/null || true
done
fi

Expand Down Expand Up @@ -171,7 +171,7 @@ COUNT=0
while [ ${COUNT} -lt 30 ]; do
MSG=""
for N in ${ETHX}; do
if ethtool ${N} 2>/dev/null | grep 'Link detected' | grep -q 'yes'; then
if [ "1" = "$(cat /sys/class/net/${N}/carrier 2>/dev/null)" ]; then
MSG+="${N} "
fi
done
Expand All @@ -192,11 +192,11 @@ for N in ${ETHX}; do
DRIVER=$(ls -ld /sys/class/net/${N}/device/driver 2>/dev/null | awk -F '/' '{print $NF}')
echo -en "${N}(${DRIVER}): "
while true; do
if ! ip link show ${N} 2>/dev/null | grep -q 'UP'; then
if [ -z "$(cat /sys/class/net/${N}/carrier 2>/dev/null)" ]; then
echo -en "\r${N}(${DRIVER}): $(TEXT "DOWN")\n"
break
fi
if ethtool ${N} 2>/dev/null | grep 'Link detected' | grep -q 'no'; then
if [ "0" = "$(cat /sys/class/net/${N}/carrier 2>/dev/null)" ]; then
echo -en "\r${N}(${DRIVER}): $(TEXT "NOT CONNECTED")\n"
break
fi
Expand Down
Loading

0 comments on commit c091d0d

Please sign in to comment.