-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[busybox] 1.37.0-8: add support for networkmanager & fix mdev helper mac addr bug #2983
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,5 +1,6 @@ | ||||||||||
#!/bin/sh | ||||||||||
# Copyright (c) 2012, Piotr Karbowski <[email protected]> | ||||||||||
# Copyright (c) 2025, Julian Droske <[email protected]> | ||||||||||
# All rights reserved. | ||||||||||
# | ||||||||||
# Redistribution and use in source and binary forms, with or without modification, are | ||||||||||
|
@@ -28,7 +29,9 @@ | |||||||||
# First it will run nameif to rename interfaces if configured in /etc/mactab | ||||||||||
# Then it will dump known interfaces to new mactab file. | ||||||||||
# Next it will parse old /etc/mactab and copy all interfaces which wasnt added in step two. | ||||||||||
# On the end, it will replace /etc/mactab with the new one. | ||||||||||
# And it will replace /etc/mactab with the new one, controlled by '--write-mactab'. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
# Finally it will dispatch a udev event for current nic if executed by mdev, controlled by | ||||||||||
# '--notify-udev'. | ||||||||||
Comment on lines
+33
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
# Info: Step two will only care about eth*, wlan*, ath*, wifi* and ra* interfaces. | ||||||||||
|
||||||||||
|
@@ -39,6 +42,10 @@ if [ -n "${USER}" ] && [ "${USER}" != 'root' ]; then | |||||||||
exit 1 | ||||||||||
fi | ||||||||||
|
||||||||||
command_exists() { | ||||||||||
command -v "${1}" >/dev/null 2>&1 | ||||||||||
} | ||||||||||
|
||||||||||
in_comma_list() { | ||||||||||
# Check whatever $1 is in the comma separated list $2. | ||||||||||
local x | ||||||||||
|
@@ -51,6 +58,30 @@ in_comma_list() { | |||||||||
return 1 | ||||||||||
} | ||||||||||
|
||||||||||
get_nic_macaddr() { | ||||||||||
# Try to get real mac address from nic $1. | ||||||||||
local nic="$1" | ||||||||||
local macaddr | ||||||||||
if command_exists ethtool; then | ||||||||||
macaddr="$(ethtool -P ${nic} 2>/dev/null)" | ||||||||||
macaddr="${macaddr##*: }" | ||||||||||
if [ -n "${macaddr}" ] && [ "${macaddr}" != 'not set' ]; then | ||||||||||
echo "${macaddr}" | ||||||||||
return 0 | ||||||||||
fi | ||||||||||
fi | ||||||||||
if command_exists ip; then | ||||||||||
macaddr="$(ip link show dev ${nic} |grep permaddr)" | ||||||||||
macaddr="${macaddr##*permaddr }" | ||||||||||
if [ -n "${macaddr}" ]; then | ||||||||||
echo "${macaddr}" | ||||||||||
return 0 | ||||||||||
fi | ||||||||||
fi | ||||||||||
# Fallback to get mac address from sysfs. | ||||||||||
cat "/sys/class/net/${nic}/address" | ||||||||||
} | ||||||||||
|
||||||||||
lockfile='/etc/mactab.settle-nics_lockfile' | ||||||||||
while : ; do | ||||||||||
if ! test -f "${lockfile}" && ( set -o noclobber; echo "$$" > "${lockfile}") 2> /dev/null; then | ||||||||||
|
@@ -60,27 +91,28 @@ while : ; do | |||||||||
fi | ||||||||||
done | ||||||||||
|
||||||||||
case "$1" in | ||||||||||
--write-mactab) | ||||||||||
write_mactab='true' | ||||||||||
tmpfile="/etc/mactab.settle-nics_tmpfile.$$" | ||||||||||
rm -f "${tmpfile}" | ||||||||||
;; | ||||||||||
'') | ||||||||||
tmpfile='/dev/null' | ||||||||||
;; | ||||||||||
*) | ||||||||||
echo "Wrong argument!" >&2 | ||||||||||
exit 1 | ||||||||||
;; | ||||||||||
|
||||||||||
esac | ||||||||||
for opt in "$@"; do | ||||||||||
case "${opt}" in | ||||||||||
--write-mactab) | ||||||||||
write_mactab='true' | ||||||||||
tmpfile="/etc/mactab.settle-nics_tmpfile.$$" | ||||||||||
rm -f "${tmpfile}" | ||||||||||
;; | ||||||||||
--notify-udev) | ||||||||||
notify_udev='true' | ||||||||||
;; | ||||||||||
'') | ||||||||||
tmpfile='/dev/null' | ||||||||||
;; | ||||||||||
*) | ||||||||||
echo "Wrong argument!" >&2 | ||||||||||
exit 1 | ||||||||||
;; | ||||||||||
esac | ||||||||||
done | ||||||||||
|
||||||||||
trap 'status="$?"; rm -f "${lockfile}"; test -f "${tmpfile}" && rm -f "${tmpfile}"; exit "${status}"' INT TERM EXIT | ||||||||||
|
||||||||||
# iwlwifi bug: must wait for several seconds for setting real MAC address | ||||||||||
sleep 3 | ||||||||||
|
||||||||||
# If we do have configured nics but the configured names are already used by something else, rename it to temp name. | ||||||||||
# I wish nameif or ifrename could be smart enough to do it itself... | ||||||||||
inconf_nics="" | ||||||||||
|
@@ -94,7 +126,7 @@ if [ -f '/etc/mactab' ]; then | |||||||||
inconf_nics="${inconf_nics},${nic}" | ||||||||||
inconf_macs="${inconf_macs},${macaddr}" | ||||||||||
if [ -e "/sys/class/net/${nic}" ]; then | ||||||||||
curr_macaddr="$(cat /sys/class/net/${nic}/address)" | ||||||||||
curr_macaddr="$(get_nic_macaddr ${nic})" | ||||||||||
if [ "${curr_macaddr}" != "${macaddr}" ]; then | ||||||||||
# Okey, so kernel added another NIC with name which we preserved for other NIC. | ||||||||||
echo "Looks like '${nic}' is preserved. renaming current '${nic}' to '${nic}_tmp' ..." >&2 | ||||||||||
|
@@ -107,7 +139,7 @@ if [ -f '/etc/mactab' ]; then | |||||||||
fi | ||||||||||
|
||||||||||
# Run nameif so all interfaces will be renamed to specified names. | ||||||||||
if command -v nameif >/dev/null 2>&1 && test -f /etc/mactab; then | ||||||||||
if command_exists nameif && test -f /etc/mactab; then | ||||||||||
nameif | ||||||||||
fi | ||||||||||
|
||||||||||
|
@@ -128,47 +160,51 @@ for nic in /sys/class/net/*_tmp; do | |||||||||
done | ||||||||||
done | ||||||||||
|
||||||||||
if [ "${write_mactab}" != 'true' ]; then | ||||||||||
# We don't want write mactab thus there is no reason to check the network interfaces' mac addresses and so on... | ||||||||||
exit 0 | ||||||||||
fi | ||||||||||
|
||||||||||
printf '%s\n' "# Generated by settle-nics from mdev-like-a-boss." >> "${tmpfile}" | ||||||||||
# We don't want write mactab thus there is no reason to check the network interfaces' mac addresses and so on... | ||||||||||
if [ "${write_mactab}" = 'true' ]; then | ||||||||||
printf '%s\n' "# Generated by settle-nics from mdev-like-a-boss." >> "${tmpfile}" | ||||||||||
|
||||||||||
# First get all the macs of current accessable nics | ||||||||||
detected_nics="" | ||||||||||
detected_macs="" | ||||||||||
for i in /sys/class/net/*; do | ||||||||||
unset device macaddr | ||||||||||
device="${i##*/}" | ||||||||||
case "${device}" in | ||||||||||
*_tmp) | ||||||||||
;; | ||||||||||
eth[0-9]*|wlan[0-9]*|ath[0-9]*|wifi[0-9]*|ra[0-9]*) | ||||||||||
macaddr="$(cat /sys/class/net/${device}/address 2>/dev/null)" | ||||||||||
if [ -n "${macaddr}" ] && ! in_comma_list "${macaddr}" "${inconf_macs}" && ! in_comma_list "${device}" "${inconf_nics}"; then | ||||||||||
detected_nics="${detected_nics},${device}" | ||||||||||
detected_macs="${detected_macs},${macaddr}" | ||||||||||
printf '%-15s %s\n' "${device}" "${macaddr}" >> "${tmpfile}" | ||||||||||
fi | ||||||||||
;; | ||||||||||
esac | ||||||||||
done | ||||||||||
|
||||||||||
# Now lets parse current /etc/mactab so we no loose any configured but not available at this moment interface. | ||||||||||
if [ -f '/etc/mactab' ]; then | ||||||||||
unset device macaddr | ||||||||||
while read device macaddr; do | ||||||||||
# First get all the macs of current accessable nics | ||||||||||
detected_nics="" | ||||||||||
detected_macs="" | ||||||||||
for i in /sys/class/net/*; do | ||||||||||
unset device macaddr | ||||||||||
device="${i##*/}" | ||||||||||
case "${device}" in | ||||||||||
'#'*) | ||||||||||
*_tmp) | ||||||||||
;; | ||||||||||
*) | ||||||||||
if [ -n "${macaddr}" ] && ! in_comma_list "${macaddr}" "${detected_macs}" && ! in_comma_list "${device}" "${detected_nics}"; then | ||||||||||
eth[0-9]*|wlan[0-9]*|ath[0-9]*|wifi[0-9]*|ra[0-9]*) | ||||||||||
macaddr="$(get_nic_macaddr ${device} 2>/dev/null)" | ||||||||||
if [ -n "${macaddr}" ] && ! in_comma_list "${macaddr}" "${inconf_macs}" && ! in_comma_list "${device}" "${inconf_nics}"; then | ||||||||||
detected_nics="${detected_nics},${device}" | ||||||||||
detected_macs="${detected_macs},${macaddr}" | ||||||||||
Comment on lines
+174
to
+180
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a copy of original code? Could we split a function out and make it easy for reading? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, it's a wrapper of the original impl of "--write-mactab", and I intended to make it be the same level as that of "--notify-udev", so that the structure will be more clear.
Well, according to the original code, the structure is like:
So I was trying to keep it. But it's okay to split them out. And I also think it's time to refactor it, because it's pretty old and didn't share the same idea as the impl of "--notify-udev". i.e. the latter is intended to be executed only by mdev & expects only one device per instance, while the original code can be run by user & processes all nics. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm willing to see 3 being split into a function, then you could save some indentation.
I would second the idea, but let's make everything works first :) |
||||||||||
printf '%-15s %s\n' "${device}" "${macaddr}" >> "${tmpfile}" | ||||||||||
fi | ||||||||||
;; | ||||||||||
esac | ||||||||||
done < '/etc/mactab' | ||||||||||
done | ||||||||||
|
||||||||||
# Now lets parse current /etc/mactab so we no loose any configured but not available at this moment interface. | ||||||||||
if [ -f '/etc/mactab' ]; then | ||||||||||
unset device macaddr | ||||||||||
while read device macaddr; do | ||||||||||
case "${device}" in | ||||||||||
'#'*) | ||||||||||
;; | ||||||||||
*) | ||||||||||
if [ -n "${macaddr}" ] && ! in_comma_list "${macaddr}" "${detected_macs}" && ! in_comma_list "${device}" "${detected_nics}"; then | ||||||||||
printf '%-15s %s\n' "${device}" "${macaddr}" >> "${tmpfile}" | ||||||||||
fi | ||||||||||
;; | ||||||||||
esac | ||||||||||
done < '/etc/mactab' | ||||||||||
fi | ||||||||||
|
||||||||||
mv "${tmpfile}" '/etc/mactab' | ||||||||||
fi | ||||||||||
|
||||||||||
mv "${tmpfile}" '/etc/mactab' | ||||||||||
# It's time to dispatch a udev event | ||||||||||
# This only works when in mdev.conf as which sets a necessary envar SEQNUM, we may not set a custom one | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
if [ "${notify_udev}" = 'true' ] && [ -n "${SEQNUM}" ] && command_exists libudev-helper; then | ||||||||||
libudev-helper | ||||||||||
fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No action required. Actually we should fix this: this script makes use of bash features (
local
for example).We could create a split repository to maintain these helpers later.