红联Linux门户
Linux帮助

使用udev绑定硬件设备

发布时间:2016-03-09 09:50:45来源:linux网站作者:齐鲁至滇

运行下面的命令可以得到 ttyS0 的设备所在。
udevadm info -q path -n /dev/ttyS0

我的输出如下:
/devices/pnp0/00:07/tty/ttyS0
 
那么我们就可以用上面的命令来得到 ttyS0 的信息了,运行如下的命令:
udevadm info -a -p $(udevadm info -q path -n /dev/ttyS0)


root@adimm:/dev# udevadm info -a -p $(udevadm info -q path -n /dev/ttyS0)
Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

looking at device '/devices/pnp0/00:07/tty/ttyS0':
KERNEL=="ttyS0"
SUBSYSTEM=="tty"
DRIVER==""

looking at parent device '/devices/pnp0/00:07':
KERNELS=="00:07"
SUBSYSTEMS=="pnp"
DRIVERS=="serial"
ATTRS{id}=="PNP0501"

looking at parent device '/devices/pnp0':
KERNELS=="pnp0"
SUBSYSTEMS==""
DRIVERS==""


然后,我们就可以编写我们的 /etc/udev/rules.d/10-local.rules 文件了。
sudo vim /etc/udev/rules.d/10-local.rules
 
在里面加入这几个变量信息,如下。
KERNEL=="ttyS0", SUBSYSTEMS=="pnp", ATTRS{id}=="PNP0501", SYMLINK+="lx200_link"

上面的 KERNEL=="ttyS0", SUBSYSTEMS=="pnp" 我们可以根据上面的输出直接拷贝过去的。我们保存这个文件。


一般我们要使这个规则文件生效,要热插拔我们的设备以产生一个事件或在设备中的 event 文件中增加信息以达到发送事件的目的来更新我们的 udev 规则,但这里有个更加方便的方法,我们可以运行下面这个命令。

sudo udevadm test /sys/class/block/ttyS0

这样我们就更新了我们的规则。


本文永久更新地址:http://www.linuxdiyf.com/linux/18744.html