commit 3535171185c3dbb99a85a09207cd40e6431b8292 Author: Bill Yuan Date: Thu Oct 5 01:52:46 2017 +0000 rc: rename interface according to the MAC add func `ifconfig_rename_by_mac` which will try to rename the interface according to the configured MAC address. e.g. (config in rc.conf) ifconfig_rename_ifs="em0 em1" ifconfig_rename_em1_by_mac="08:00:27:a9:46:c7" ifconfig_rename_em0_by_mac="08:00:27:2d:60:a6" diff --git a/etc/network.subr b/etc/network.subr index ba2d5f8..5cd8d0d 100644 --- a/etc/network.subr +++ b/etc/network.subr @@ -370,6 +370,59 @@ ifnet_rename() return 0 } +# ifconfig_rename_by_mac +# config in rc.conf +# ifconfig_rename_ifs " [if]" +# ifconfig_rename__by_mac="" +# +ifconfig_rename_by_mac() +{ + prefix="ifconfig_rename_" + subfix="_by_mac" + #collect all mac addresses + ifs="`ifconfig -l ether`" + [ -z "$ifs" ] && return 0 + all="" + for ifname_tmp in ${ifs} ; do + mac=`ifconfig $ifname_tmp ether | grep ether | cut -d ' ' -f 2` + all="$mac|$all" + done + + delta=0 + sec1=0 + for cfg_ifname in ${ifconfig_rename_ifs} ; do + #get the config mac + cfg="$`echo "\$prefix$cfg_ifname$subfix"`" + mac=`eval echo $cfg` + #whether the mac is exits in system + found=`echo $all | awk -v mac=$mac -F '|' '{for(i=1; i < NF; i++){if($i == mac){ print "FOUND";}}}'` + if [ "$found" == "FOUND" ]; then + #get current ifname by mac address + ifs="`ifconfig -l ether`" + for ifname_tmp in ${ifs} ; do + the=`ifconfig $ifname_tmp ether | grep ether | cut -d ' ' -f 2` + if [ $mac == $the ]; then + curr_name=$ifname_tmp + fi + done + if [ $curr_name != $cfg_ifname ]; then + #rename if another interface is using the name + ifconfig $cfg_ifname >> /dev/null + if [ $? == 0 ]; then + sec2=`date +%s` + if [ $sec1 == $sec2 ]; then + delta=`expr $delta + 1` + fi + ifconfig $cfg_ifname name `expr $sec2 + $delta` + sec1=$sec2 + fi + ifconfig $curr_name name $cfg_ifname + fi + fi + done + return 0 +} + # # list_net_interfaces type # List all network interfaces. The type of interface returned diff --git a/etc/rc.d/netif b/etc/rc.d/netif index 222220b..d1de54d 100644 --- a/etc/rc.d/netif +++ b/etc/rc.d/netif @@ -56,6 +56,9 @@ network_start() # Create IPv6<-->IPv4 tunnels gif_up + # Rename interfaces by MAC + ifconfig_rename_by_mac + # Rename interfaces. ifnet_rename fi