
組み込みLinuxシステムの場合、複数のネットワークインタフェースがある場合は、起動するたびに常に同じインタフェース名を持つようにするにはどうすればよいですか?
つまり、たとえば、eth0 は常に 1 つの物理イーサネット ポートにマッピングされ、eth1 は常に次の物理イーサネット ポートにマッピングされます。
私のLinux "distro"は独自に開発され、devtmpfsを使用して/ devを埋めます。初期化(および他のほとんどのタスク)にはbusyboxを使用し、システムの起動と終了にはカスタム初期化スクリプトを使用します。
mdevやudevのホットプラグ機能は必要ありません。 「固定」イーサネットポートを言うことです。
答え1
これは x86_64 アーキテクチャの Linux 3.9.0 に適用されます。
#!/bin/sh
# This assumes the interfaces come up with default names of eth*.
# The interface names may not be correct at this point, however.
# This is just a way to get the PCI addresses of all the active
# interfaces.
PCIADDRLIST=
for dir in /sys/class/net/eth* ; do
[ -e $dir/device ] && {
PCIADDRLIST="`readlink -f $dir/device` ${PCIADDRLIST}"
}
done
# Now assign the interface names from an ordered list that maps
# to the PCI addresses of each interface.
# IFNAMES could come from some config file. "dummy" is needed because of
# my limited tr- and awk-fu.
IFNAMES="eth0 eth1 eth2 dummy"
for dir in `echo ${PCIADDRLIST} | tr " " "\n" | sort` ; do
[ -e $dir/net/*/address ] && {
MACADDR=`cat $dir/net/*/address`
IFNAME=`echo $IFNAMES | awk '{print $1}'`
IFNAMES=`echo $IFNAMES | awk '{ for (i=2; i<=NF; i++) printf "%s ", $i; }'`
echo -n "$IFNAME "
nameif $IFNAME mac=$MACADDR
}
done