udev的秘密

http://www.reactivated.net/writing_udev_rules.html

http://www.cnitblog.com/luofuchong/archive/2007/12/18/37831.html

udev

udev是会根据sysfs来在/dev目录下创建设备节点。

  • sysfs:是什么呢?

    sysfs是在/sys目录下的东西,比如 /sys/class/block/sda,比如/sys/class/rtc/rtc0/device/rtc/rtc0

udev是一种新的管理/dev目录的方法,它的设计清除了以前的/dev实现的一些问题并提供了鲁棒的路径向后兼容. 为了创建并命名系统中相应的/dev设备结点,udev需要依赖于根据用户提供的规则从sysfs中得到的匹配信息. 本文着重规则书写的过程,udev相关的任务由用户自己完成.

udev规则具有弹性非常强大,这里是一些你使用规则可以达到的结果:

  1. 重命名设备节点的缺省名字为其他名字
  2. 通过创建符号链接到缺省设备节点来提供一个可选的固定的设备节点名字
  3. 基于程序的输出命名设备节点
  4. 改变设备节点的权限和所有权
  5. 一旦但设备节点被创建或删除时(通常是添加设备或拔出设备时)执行一个脚本
  6. 重命名网络接口

规则书写

缺省udev规则存储在/etc/udev/rules.d/50-defaulte-udev.rules里面,但是不要在这个文件里编写。 建议你创建一个文件/etc/udev/rules.d/10-local.rules并把自己的所有规则写到这里面去.

  • 一个设备可以被多条规则匹配到

规则语法

  • 键值对用“,”分开
  • 至少一个匹配键和一个赋值键

这是用来阐述上面内容的一个例子规则:

KERNEL=="hdb",NAME="my_spare_disk"

解释:
匹配键:KERNEL
赋值键:NAME

匹配键

匹配键
KERNEL 为设备匹配的内核名字
SUBSYSTEM 匹配设备的子系统
DRIVER 匹配支持设备的驱动名称

赋值键

  • NAME - the name that shall be used for the device node
  • SYMLINK - a list of symbolic links which act as alternative names for the device node

设备层次结构

  • KERNELS - match against the kernel name for the device, or the kernel name for any of the parent devices

  • SUBSYSTEMS - match against the subsystem of the device, or the subsystem of any of the parent devices

  • DRIVERS - match against the name of the driver backing the device, or the name of the driver backing any of the parent devices

  • ATTRS - match a sysfs attribute of the device, or a sysfs attribute of any of the parent devices

udevadm命令

udevadm info -a -p /sys/class/block/sdb

字符串替换String substitutions

常用的操作符: %k and %n.

  • %k

    evaluates to the kernel name for the device. e.g. “sda3” for a device that would (by default) appear at /dev/sda3

  • %n

    evaluates to the kernel number for the device (the partition number for storage devices), e.g. “3” for /dev/sda3

  • match a literal % in a rule then you must write %%

  • match a literal $ then you must write $$.

ATTRS

设备的属性在/sys/class/rtc/rtc0/device/rtc/rtc0, /sys/class/block/sda/device/ 中可以看得见,一个文件一个ATTRS属性。

在这里插入图片描述

rtc0和rtc1选用其中一个为默认

  • vi /etc/udev/rules.d/51-invengo.rules

    以为缺省默认的是/lib/udev/rules.d/50-udev-default.rules,udev是按照顺序来进行创建设备节点文件。

在这里插入图片描述

  • 参照模仿/lib/udev/rules.d/50-udev-default.rules中rtc0的规则,替换成rtc1的规则

  • 测试rtc是否变成了rtc1.

    上电后timedatectl中显示的确实是rtc1的时间,但是hwclock 默认用的是/dev/rtc0的设备(–help查看)。

  • 在linux上电的时候执行hwclock -s -f /dev/rtc1

猜你喜欢

转载自blog.csdn.net/V__KING__/article/details/115502255