我喜欢在连接 eth0 时打开绿色 LED(通过 GPIO 连接)。断开连接时,我喜欢打开绿色 LED 和打开红色 LED。
认为 udev 可能是适合它的地方。我创建了简单的演示规则:
KERNEL=="eth0", SUBSYSTEM=="net", ACTION=="add", RUN+="/sbin/set_BSPleds eth0 on"
此规则应在添加 eth0 时调用脚本。它从未被执行过。 在我通过在 shell 中输入“udevadm monitor -k -u”来查看 udev 监视器之后。当我拔下/插入 LAN 电缆时没有发生任何事件。
root@sama5d3xek:/etc/udev/rules.d# udevadm monitor -k -uh0
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent
root@sama5d3xek:/etc/udev/rules.d#
似乎没有 eth0 的 uevents。以太网驱动程序由我的 ATMEL 提供。我正在 Yocto 的帮助下构建自定义 Linux。
我的问题是,如何将“链接断开”/“链接起来”事件发送到 udev? 如果它不适用于 udev,最好的替代方法是什么?
最佳答案
正如其他人已经提到的,似乎不能为此使用 udev。
Ubuntu: wait for network link up and execute a bash command 建议
inotifywait -e modify /sys/class/net/eth0/carrier; echo 'Change detected'
(目前,/在我的盒子的 nfs 上,所以我真的不能说它是否有效。)
在其他帖子中,存在一些关于在/sys: inotify_add_watch fails on /sys/class/net/eth0/operstate 上使用 inotify API 的问题。
我认为正确的方法是使用 netlink(7) API,最好是通过 netplugd 等守护进程。
希望对您有所帮助:)
关于linux - 如何为 ETH "link down"/"link up"设置 udev 规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40676914/