network: rework ip/mac functions

Signed-off-by: AuxXxilium <info@auxxxilium.tech>
This commit is contained in:
AuxXxilium
2024-02-15 23:46:55 +01:00
parent 5154dbd75c
commit b4dba6ef55
4 changed files with 91 additions and 83 deletions

View File

@@ -192,7 +192,6 @@ function arcMenu() {
writeConfigKey "arc.paturl" "" "${USER_CONFIG_FILE}"
writeConfigKey "arc.pathash" "" "${USER_CONFIG_FILE}"
writeConfigKey "arc.sn" "" "${USER_CONFIG_FILE}"
writeConfigKey "arc.mac1" "" "${USER_CONFIG_FILE}"
if [ "${DT}" = "true" ]; then
deleteConfigKey "cmdline.SataPortMap" "${USER_CONFIG_FILE}"
deleteConfigKey "cmdline.DiskIdxMap" "${USER_CONFIG_FILE}"
@@ -990,8 +989,8 @@ function cmdlineMenu() {
3)
MSG="Note: (MAC will not be set to NIC, Only for activation services.)"
sn="${SN}"
mac1="${MAC1}"
mac2="${MAC2}"
mac1="$(readConfigKey "mac.eth0" "${USER_CONFIG_FILE}")" # TO-DO
mac2="$(readConfigKey "mac.eth1" "${USER_CONFIG_FILE}")"
while true; do
dialog --backtitle "$(backtitle)" \ --title "Customize Mac" \
--extra-button --extra-label "Random" \
@@ -1011,9 +1010,9 @@ function cmdlineMenu() {
SN="${sn}"
writeConfigKey "sn" "${SN}" "${USER_CONFIG_FILE}"
MAC1="${mac1}"
writeConfigKey "mac1" "${MAC1}" "${USER_CONFIG_FILE}"
writeConfigKey "mac.eth0" "${MAC1}" "${USER_CONFIG_FILE}"
MAC2="${mac2}"
writeConfigKey "mac2" "${MAC2}" "${USER_CONFIG_FILE}"
writeConfigKey "mac.eth1" "${MAC2}" "${USER_CONFIG_FILE}"
writeConfigKey "arc.builddone" "false" "${USER_CONFIG_FILE}"
BUILDDONE="$(readConfigKey "arc.builddone" "${USER_CONFIG_FILE}")"
break
@@ -2422,11 +2421,11 @@ function fullsysinfo() {
# Shows Networkdiag to user
function networkdiag() {
MSG=""
for iface in $(ls /sys/class/net/ | grep -v lo || true)
do
MSG+="Interface: ${iface}\n"
addr=$(getIP ${iface})
netmask=$(ifconfig eth0 | grep inet | grep 255 | awk '{print $4}' | cut -f2 -d':')
ETHX=$(ls /sys/class/net/ | grep -v lo) || true
for ETH in ${ETHX}; do
MSG+="Interface: ${ETH}\n"
addr=$(getIP ${ETH})
netmask=$(ifconfig ${ETH} | grep inet | grep 255 | awk '{print $4}' | cut -f2 -d':')
MSG+="IP Address: ${addr}\n"
MSG+="Netmask: ${netmask}\n"
MSG+="\n"
@@ -2755,7 +2754,6 @@ function resetLoader() {
initConfigKey "arc.paturl" "" "${USER_CONFIG_FILE}"
initConfigKey "arc.pathash" "" "${USER_CONFIG_FILE}"
initConfigKey "arc.sn" "" "${USER_CONFIG_FILE}"
initConfigKey "arc.mac1" "" "${USER_CONFIG_FILE}"
initConfigKey "arc.staticip" "false" "${USER_CONFIG_FILE}"
initConfigKey "arc.ipv6" "false" "${USER_CONFIG_FILE}"
initConfigKey "arc.offline" "false" "${USER_CONFIG_FILE}"
@@ -2772,6 +2770,8 @@ function resetLoader() {
initConfigKey "arc.odp" "false" "${USER_CONFIG_FILE}"
initConfigKey "arc.hddsort" "false" "${USER_CONFIG_FILE}"
initConfigKey "arc.version" "${ARC_VERSION}" "${USER_CONFIG_FILE}"
initConfigKey "ip" "{}" "${USER_CONFIG_FILE}"
initConfigKey "mac" "{}" "${USER_CONFIG_FILE}"
deleteConfigKey "cmdline.SataPortMap" "${USER_CONFIG_FILE}"
deleteConfigKey "cmdline.DiskIdxMap" "${USER_CONFIG_FILE}"
deleteConfigKey "cmdline.sata_remap" "${USER_CONFIG_FILE}"
@@ -2784,8 +2784,10 @@ function resetLoader() {
fi
CONFDONE="$(readConfigKey "arc.confdone" "${USER_CONFIG_FILE}")"
BUILDDONE="$(readConfigKey "arc.builddone" "${USER_CONFIG_FILE}")"
dialog --backtitle "$(backtitle)" --colors --title "Reset Loader" \
--msgbox "Clean is complete." 5 30
dialog --backtitle "$(backtitle)" --title "Reset Loader" --aspect 18 \
--yesno "Clean successful.\nReboot?" 0 0
[ $? -ne 0 ] && continue
exec reboot
clear
}
@@ -2878,7 +2880,7 @@ while true; do
fi
echo "p \"Arc Patch Settings \" " >>"${TMP_PATH}/menu"
echo "S \"Custom StoragePanel \" " >>"${TMP_PATH}/menu"
echo "D \"DHCP/Static Loader IP \" " >>"${TMP_PATH}/menu"
#echo "D \"DHCP/Static Loader IP \" " >>"${TMP_PATH}/menu" TO-DO
fi
if [ "${ADVOPTS}" = "true" ]; then
echo "5 \"\Z1Hide Advanced Options\Zn \" " >>"${TMP_PATH}/menu"

View File

@@ -94,11 +94,10 @@ done
VID="$(readConfigKey "vid" "${USER_CONFIG_FILE}")"
PID="$(readConfigKey "pid" "${USER_CONFIG_FILE}")"
SN="$(readConfigKey "arc.sn" "${USER_CONFIG_FILE}")"
MAC1="$(readConfigKey "arc.mac1" "${USER_CONFIG_FILE}")"
MAC2="$(readConfigKey "arc.mac2" "${USER_CONFIG_FILE}")"
KERNELLOAD="$(readConfigKey "arc.kernelload" "${USER_CONFIG_FILE}")"
KERNELPANIC="$(readConfigKey "arc.kernelpanic" "${USER_CONFIG_FILE}")"
DIRECTBOOT="$(readConfigKey "arc.directboot" "${USER_CONFIG_FILE}")"
ETHX=$(ls /sys/class/net/ | grep -v lo) || true
declare -A CMDLINE
@@ -129,16 +128,21 @@ CMDLINE['loglevel']="15"
CMDLINE['log_buf_len']="32M"
CMDLINE['sn']="${SN}"
CMDLINE['net.ifnames']="0"
CMDLINE['netif_num']="0"
N=0
if [ "${MACSYS}" = "hardware" ]; then
[[ -z "${MAC1}" && -n "${MAC2}" ]] && MAC1=${MAC2} && MAC2="" # Sanity check
[ -n "${MAC1}" ] && CMDLINE['netif_num']="1" && CMDLINE['mac1']="${MAC1}" && CMDLINE['skip_vender_mac_interfaces']="0,1,2,3,4,5,6,7"
[ -n "${MAC2}" ] && CMDLINE['netif_num']="2" && CMDLINE['mac2']="${MAC2}" && CMDLINE['skip_vender_mac_interfaces']="0,1,2,3,4,5,6,7"
for ETH in ${ETHX}; do
MAC="$(readConfigKey "mac.${ETH}" "${USER_CONFIG_FILE}")"
[ -n "${MAC}" ] && N=$((${N} + 1)) && CMDLINE["mac${N}"]="${MAC}"
done
CMDLINE['skip_vender_mac_interfaces']="0,1,2,3,4,5,6,7"
elif [ "${MACSYS}" = "custom" ]; then
[[ -z "${MAC1}" && -n "${MAC2}" ]] && MAC1=${MAC2} && MAC2="" # Sanity check
[ -n "${MAC1}" ] && CMDLINE['netif_num']="1" && CMDLINE['mac1']="${MAC1}" && CMDLINE['skip_vender_mac_interfaces']="1,2,3,4,5,6,7"
[ -n "${MAC2}" ] && CMDLINE['netif_num']="2" && CMDLINE['mac2']="${MAC2}" && CMDLINE['skip_vender_mac_interfaces']="2,3,4,5,6,7"
for ETH in ${ETHX}; do
MAC="$(readConfigKey "mac.${ETH}" "${USER_CONFIG_FILE}")"
[ -n "${MAC}" ] && N=$((${N} + 1)) && CMDLINE["mac${N}"]="${MAC}"
done
CMDLINE['skip_vender_mac_interfaces']="$(seq -s, ${N} 7)"
fi
CMDLINE['netif_num']="${N}"
# Read cmdline
while IFS=': ' read -r KEY VALUE; do
@@ -165,30 +169,29 @@ if [ "${DIRECTBOOT}" = "true" ]; then
echo -e " \033[1;34mReboot with Directboot\033[0m"
exec reboot
elif [ "${DIRECTBOOT}" = "false" ]; then
ETHX=$(ls /sys/class/net/ | grep -v lo) || true
ETH=$(echo ${ETHX} | wc -w)
STATICIP="$(readConfigKey "arc.staticip" "${USER_CONFIG_FILE}")"
BOOTIPWAIT="$(readConfigKey "arc.bootipwait" "${USER_CONFIG_FILE}")"
echo -e " \033[1;34mDetected ${ETH} NIC.\033[0m \033[1;37mWaiting for Connection:\033[0m"
for N in ${ETHX}; do
echo -e " \033[1;34mDetected ${N} NIC.\033[0m \033[1;37mWaiting for Connection:\033[0m"
for ETH in ${ETHX}; do
IP=""
DRIVER=$(ls -ld /sys/class/net/${N}/device/driver 2>/dev/null | awk -F '/' '{print $NF}')
DRIVER=$(ls -ld /sys/class/net/${ETH}/device/driver 2>/dev/null | awk -F '/' '{print $NF}')
COUNT=0
while true; do
if [[ "${STATICIP}" = "true" && "${N}" = "eth0" && -n "${ARCIP}" ]]; then
ARCIP="$(readConfigKey "arc.ip" "${USER_CONFIG_FILE}")"
ARCIP="$(readConfigKey "ip.${ETH}" "${USER_CONFIG_FILE}")"
if [[ "${STATICIP}" = "true" && -n "${ARCIP}" ]]; then
NETMASK="$(readConfigKey "arc.netmask" "${USER_CONFIG_FILE}")"
IP="${ARCIP}"
NETMASK=$(convert_netmask "${NETMASK}")
ip addr add ${IP}/${NETMASK} dev eth0
ip addr add ${IP}/${NETMASK} dev ${ETH}
MSG="STATIC"
else
IP="$(getIP ${N})"
IP="$(getIP ${ETH})"
MSG="DHCP"
fi
if [ -n "${IP}" ]; then
SPEED=$(ethtool ${N} | grep "Speed:" | awk '{print $2}')
SPEED=$(ethtool ${ETH} | grep "Speed:" | awk '{print $2}')
echo -e "\r \033[1;37m${DRIVER} (${SPEED} | ${MSG}):\033[0m Access \033[1;34mhttp://${IP}:5000\033[0m to connect to DSM via web."
ethtool -s ${ETH} wol g 2>/dev/null
[ ! -n "${IPCON}" ] && IPCON="${IP}"
break
fi
@@ -197,13 +200,12 @@ elif [ "${DIRECTBOOT}" = "false" ]; then
break
fi
sleep 3
if ethtool ${N} | grep 'Link detected' | grep -q 'no'; then
if ethtool ${ETH} | grep 'Link detected' | grep -q 'no'; then
echo -e "\r \033[1;37m${DRIVER}:\033[0m NOT CONNECTED"
break
fi
COUNT=$((${COUNT} + 3))
done
ethtool -s ${N} wol g 2>/dev/null
done
BOOTWAIT=1
w | awk '{print $1" "$2" "$4" "$5" "$6}' >WB

View File

@@ -17,36 +17,37 @@ function getnet() {
done
dialog --clear --backtitle "$(backtitle)" \
--nocancel --title "Mac Setting" \
--menu "Choose a MAC" 0 0 0 \
--menu "Choose a MAC for eth0" 0 0 0 \
--file "${TMP_PATH}/opts" \
2>"${TMP_PATH}/resp"
resp="$(<"${TMP_PATH}/resp")"
[ -z "${resp}" ] && return 1
MAC="${resp}"
writeConfigKey "arc.mac1" "${MAC}" "${USER_CONFIG_FILE}"
writeConfigKey "mac.eth0" "${MAC}" "${USER_CONFIG_FILE}"
elif [ "${ARCPATCH}" = "random" ]; then
# Generate Random Mac
MAC=($(generateMacAddress "${MODEL}" 1))
writeConfigKey "arc.mac1" "${MAC}" "${USER_CONFIG_FILE}"
for ETH in ${ETHX}; do
MACS=$(generateMacAddress "${MODEL}" 1)
writeConfigKey "mac.${ETH}" "${MAC}" "${USER_CONFIG_FILE}"
done
elif [ "${ARCPATCH}" = "user" ]; then
# User Mac
MAC="$(cat /sys/class/net/eth0/address | sed 's/://g')"
RET=1
for N in ${ETH}; do
for ETH in ${ETHX}; do
MAC="$(cat /sys/class/net/${ETH}/address | sed 's/://g')"
dialog --backtitle "$(backtitle)" --title "Mac Setting" \
--inputbox "Type a custom MAC for ${N}. NIC.\n Eq. 001132123456" 0 0 "${MAC}"\
--inputbox "Type a custom MAC for ${ETH}. NIC.\n Eq. 001132123456" 0 0 "${MAC}"\
2>"${TMP_PATH}/resp"
RET=$?
[ ${RET} -ne 0 ] && break 2
MAC="$(<"${TMP_PATH}/resp")"
[ -z "${MAC}" ] && MAC="$(readConfigKey "arc.mac${N}" "${USER_CONFIG_FILE}")"
[ -z "${MAC}" ] && MAC="$(cat /sys/class/net/eth0/address | sed 's/://g')"
[ -z "${MAC}" ] && MAC="$(readConfigKey "mac.${ETH}" "${USER_CONFIG_FILE}")"
[ -z "${MAC}" ] && MAC="$(cat /sys/class/net/${ETH}/address | sed 's/://g')"
MAC="$(echo "${MAC}" | sed "s/:\|-\| //g")"
writeConfigKey "arc.mac${N}" "${MAC}" "${USER_CONFIG_FILE}"
writeConfigKey "arc.macsys" "custom" "${USER_CONFIG_FILE}"
writeConfigKey "mac.${ETH}" "${MAC}" "${USER_CONFIG_FILE}"
[ ${#MAC} -eq 12 ] && break
dialog --backtitle "$(backtitle)" --title "Mac Setting" --msgbox "Invalid MAC" 0 0
done
writeConfigKey "arc.macsys" "custom" "${USER_CONFIG_FILE}"
fi
# Ask for Macsys
dialog --clear --backtitle "$(backtitle)" \
@@ -67,12 +68,8 @@ function getnet() {
# Get Amount of NIC
ETHX=$(ls /sys/class/net/ | grep -v lo) || true
ETH=$(echo ${ETHX} | wc -w)
writeConfigKey "device.nic" "${ETH}" "${USER_CONFIG_FILE}"
# Get actual IP
ARCIP="$(readConfigKey "arc.ip" "${USER_CONFIG_FILE}")"
if [ -n "${ARCIP}" ]; then
IPCON="${ARCIP}"
else
IPCON="$(getIP)"
fi
for ETH in ${ETHX}; do
IPCON="$(readConfigKey "ip.${ETH}" "${USER_CONFIG_FILE}")"
[ -n "${IPCON}" ] && break
done

View File

@@ -39,7 +39,6 @@ initConfigKey "arc.builddone" "false" "${USER_CONFIG_FILE}"
initConfigKey "arc.paturl" "" "${USER_CONFIG_FILE}"
initConfigKey "arc.pathash" "" "${USER_CONFIG_FILE}"
initConfigKey "arc.sn" "" "${USER_CONFIG_FILE}"
initConfigKey "arc.mac1" "" "${USER_CONFIG_FILE}"
initConfigKey "arc.staticip" "false" "${USER_CONFIG_FILE}"
initConfigKey "arc.ipv6" "false" "${USER_CONFIG_FILE}"
initConfigKey "arc.offline" "false" "${USER_CONFIG_FILE}"
@@ -57,28 +56,35 @@ initConfigKey "arc.odp" "false" "${USER_CONFIG_FILE}"
initConfigKey "arc.hddsort" "false" "${USER_CONFIG_FILE}"
initConfigKey "arc.version" "${ARC_VERSION}" "${USER_CONFIG_FILE}"
initConfigKey "device" "{}" "${USER_CONFIG_FILE}"
initConfigKey "ip" "{}" "${USER_CONFIG_FILE}"
initConfigKey "mac" "{}" "${USER_CONFIG_FILE}"
# Init Network
ETHX=$(ls /sys/class/net/ | grep -v lo) || true
ETH=$(echo ${ETHX} | wc -w)
# No network devices
[ ${ETH} -le 0 ] && die "No NIC found! - Loader does not work without Network connection."
MACSYS="$(readConfigKey "arc.macsys" "${USER_CONFIG_FILE}")"
if [ "${MACSYS}" = "custom" ]; then
MACR="$(cat /sys/class/net/eth0/address | sed 's/://g')"
MACA="$(readConfigKey "arc.mac1" "${USER_CONFIG_FILE}")"
if [[ -n "${MACA}" && "${MACA}" != "${MACR}" ]]; then
MAC="${MACA:0:2}:${MACA:2:2}:${MACA:4:2}:${MACA:6:2}:${MACA:8:2}:${MACA:10:2}"
echo "Setting eth0 MAC to ${MAC}"
ip link set dev eth0 address "${MAC}" >/dev/null 2>&1 &&
(/etc/init.d/S41dhcpcd restart >/dev/null 2>&1 &) || true
sleep 2
elif [ -z "${MACA}" ]; then
# Write real Mac to cmdline config
writeConfigKey "arc.mac1" "${MACR}" "${USER_CONFIG_FILE}"
# Write Mac to config
N=0
for ETH in ${ETHX}; do
MACR="$(cat /sys/class/net/${ETH}/address | sed 's/://g')"
initConfigKey "mac.${ETH}" "${MACR}" "${USER_CONFIG_FILE}"
if [ "${MACSYS}" = "custom" ]; then
MACA="$(readConfigKey "mac.${ETH}" "${USER_CONFIG_FILE}")"
if [[ -n "${MACA}" && "${MACA}" != "${MACR}" ]]; then
MAC="${MACA:0:2}:${MACA:2:2}:${MACA:4:2}:${MACA:6:2}:${MACA:8:2}:${MACA:10:2}"
echo "Setting ${ETH} MAC to ${MAC}"
ip link set dev ${ETH} address "${MAC}" >/dev/null 2>&1 &&
(/etc/init.d/S41dhcpcd restart >/dev/null 2>&1 &) || true
sleep 2
fi
echo
fi
echo
fi
ethtool -s ${ETH} wol g 2>/dev/null
N=$((${N} + 1))
done
# Write NIC Amount to config
writeConfigKey "device.nic" "${N}" "${USER_CONFIG_FILE}"
# No network devices
[ ${N} -le 0 ] && die "No NIC found! - Loader does not work without Network connection."
# Get the VID/PID if we are in USB
VID="0x46f4"
@@ -128,26 +134,28 @@ echo
STATICIP="$(readConfigKey "arc.staticip" "${USER_CONFIG_FILE}")"
BOOTIPWAIT="$(readConfigKey "arc.bootipwait" "${USER_CONFIG_FILE}")"
[ -z "${BOOTIPWAIT}" ] && BOOTIPWAIT=20
echo -e "\033[1;34mDetected ${ETH} NIC.\033[0m \033[1;37mWaiting for Connection:\033[0m"
for N in ${ETHX}; do
echo -e "\033[1;34mDetected ${N} NIC.\033[0m \033[1;37mWaiting for Connection:\033[0m"
for ETH in ${ETHX}; do
IP=""
DRIVER=$(ls -ld /sys/class/net/${N}/device/driver 2>/dev/null | awk -F '/' '{print $NF}')
DRIVER=$(ls -ld /sys/class/net/${ETH}/device/driver 2>/dev/null | awk -F '/' '{print $NF}')
COUNT=0
while true; do
if [[ "${STATICIP}" = "true" && "${N}" = "eth0" && -n "${ARCIP}" ]]; then
ARCIP="$(readConfigKey "arc.ip" "${USER_CONFIG_FILE}")"
ARCIP="$(readConfigKey "ip.${ETH}" "${USER_CONFIG_FILE}")"
if [[ "${STATICIP}" = "true" && -n "${ARCIP}" ]]; then
NETMASK="$(readConfigKey "arc.netmask" "${USER_CONFIG_FILE}")"
IP="${ARCIP}"
NETMASK=$(convert_netmask "${NETMASK}")
ip addr add ${IP}/${NETMASK} dev eth0
ip addr add ${IP}/${NETMASK} dev ${ETH}
MSG="STATIC"
else
IP="$(getIP ${N})"
IP="$(getIP ${ETH})"
MSG="DHCP"
fi
if [ -n "${IP}" ]; then
SPEED=$(ethtool ${N} | grep "Speed:" | awk '{print $2}')
SPEED=$(ethtool ${ETH} | grep "Speed:" | awk '{print $2}')
writeConfigKey "ip.${ETH}" "${IP}" "${USER_CONFIG_FILE}"
echo -e "\r\033[1;37m${DRIVER} (${SPEED} | ${MSG}):\033[0m Access \033[1;34mhttp://${IP}:7681\033[0m to connect to Arc via web."
ethtool -s ${ETH} wol g 2>/dev/null
break
fi
if [ ${COUNT} -gt ${BOOTIPWAIT} ]; then
@@ -155,13 +163,12 @@ for N in ${ETHX}; do
break
fi
sleep 3
if ethtool ${N} | grep 'Link detected' | grep -q 'no'; then
if ethtool ${ETH} | grep 'Link detected' | grep -q 'no'; then
echo -e "\r\033[1;37m${DRIVER}:\033[0m NOT CONNECTED"
break
fi
COUNT=$((${COUNT} + 3))
done
ethtool -s ${N} wol g 2>/dev/null
done
# Inform user