使用 udev 自动重命名和编号网卡
我正在编写一个 udev 规则来自动重命名具有特定 MAC 地址的 NIC 并对其进行编号。
生成的规则应该执行与 75-persistent-net-generator.rules
几乎相同的操作(匹配卡的 MAC 地址的前 3 个字节,将其命名为“mycard*”,具体取决于此卡的数量)供应商已安装,将重命名规则写入70-persistent-net.rules
)。
这就是我到目前为止所取得的进展:
# udev rules to name rename cards to mycard
ACTION!="add", GOTO="persistent_mycard_generator_end"
SUBSYSTEM!="net", GOTO="persistent_mycard_generator_end"
# ignore the interface if a name has already been set
NAME=="mycard*", GOTO="persistent_mycard_generator_end"
# device name whitelist
KERNEL!="eth*", GOTO="persistent_mycard_generator_end"
# read MAC address
ENV{MATCHADDR}="$attr{address}"
# match interface type
ENV{MATCHIFTYPE}="$attr{type}"
# ignore non mycard MAC addresses
ENV{MATCHADDR}!="00:11:22:*", GOTO="persistent_mycard_generator_end"
# default comment
ENV{COMMENT}=="", ENV{COMMENT}="mycard connected through ($attr{driver})"
#### THIS IS THE PART I DON'T GET ####
# write rule
DRIVERS=="?*", IMPORT{program}="write_net_rules"
# rename interface if needed
ENV{INTERFACE_NEW}=="?*", NAME="mycard*"
#### THIS IS THE END OF THE PART I DON'T GET ####
LABEL="persistent_mycard_generator_end
“我没有得到的部分”应该做的任务是将一张卡(假设它是 eth3)重命名为 mycard0,或者如果它是系统中具有匹配 MAC 的第二张卡地址 mycard1 等。
提前致谢, 弗洛克拉
I'm writing on an udev-rule to automatically rename and number NICs with specific MAC addresses.
The resulting rule should do nearly the same 75-persistent-net-generator.rules
does (match first 3 bytes of the MAC address of card, name it 'mycard*' depending on how much cards of this vendor are installed, write the renaming-rule into 70-persistent-net.rules
).
This is how far I've come until now:
# udev rules to name rename cards to mycard
ACTION!="add", GOTO="persistent_mycard_generator_end"
SUBSYSTEM!="net", GOTO="persistent_mycard_generator_end"
# ignore the interface if a name has already been set
NAME=="mycard*", GOTO="persistent_mycard_generator_end"
# device name whitelist
KERNEL!="eth*", GOTO="persistent_mycard_generator_end"
# read MAC address
ENV{MATCHADDR}="$attr{address}"
# match interface type
ENV{MATCHIFTYPE}="$attr{type}"
# ignore non mycard MAC addresses
ENV{MATCHADDR}!="00:11:22:*", GOTO="persistent_mycard_generator_end"
# default comment
ENV{COMMENT}=="", ENV{COMMENT}="mycard connected through ($attr{driver})"
#### THIS IS THE PART I DON'T GET ####
# write rule
DRIVERS=="?*", IMPORT{program}="write_net_rules"
# rename interface if needed
ENV{INTERFACE_NEW}=="?*", NAME="mycard*"
#### THIS IS THE END OF THE PART I DON'T GET ####
LABEL="persistent_mycard_generator_end
The task "THE PART I DON'T GET" should do is to rename a card (lets say it's eth3) to mycard0 or if it's the second card in the system with a matching MAC address mycard1 and so on.
Thanks in advance,
flokra
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你在调用write_net_rules之前将ENV{INTERFACE}设置为“mycard0”,它会为你找到第一个未使用的mycardN,并为其写出规则,并返回ENV{INTERFACE_NEW}中的名称。
If you set ENV{INTERFACE} to "mycard0" before calling write_net_rules, it will find the first unused mycardN for you, write out the rule for it, and return the name in ENV{INTERFACE_NEW}.
好的,这是我的解决方案(我已经使用 Debian 5.0 和 Ubuntu 9.04 对其进行了测试,因此我不确定它是否适用于其他发行版的 udev 实现):
75-persistent-mycard-generator.rules< /p>
write_mycard_rules
最重要的更改是 75-persistent-mycard-generator.rules 中的
ENV{INTERFACE}="mycard0"
,它设置卡应获取的名称和match="$match, KERNEL==\"eth*\""
强制 udev 不使用新名称覆盖已使用的内核子系统。OK, here's my solution (I've tested it with Debian 5.0 and Ubuntu 9.04, so I'm not sure if it works with the udev-implementations of other distributions):
75-persistent-mycard-generator.rules
write_mycard_rules
The most important changes are
ENV{INTERFACE}="mycard0"
in 75-persistent-mycard-generator.rules which sets the name the card should get andmatch="$match, KERNEL==\"eth*\""
in write_mycard_rules which forces udev to not override the used Kernel Subsystem with the new name.