udev使用记录

在麒麟操作系统的V10和V10SP1版本中,udev目前和systemd项目合并在一起了,也就是说udev服务已经放在systemd中。udev是在用户空间中执行,可以动态建立/删除设备文件,允许每个人都不用关心主/次设备号,并且可以根据需要固定名称。
udev的工作过程如下:

  1. 当内核检测到系统中出现了新设备后,内核会通过netlink套接字发送uevent。
  2. udev获取内核发送的信息,进行规则的匹配。匹配的事物包括SUBSYSTEM、ACTION等数据。
  3. 匹配成功后根据规则创建设备文件。

udev的配置文件是/etc/udev/udev.conf,里面记录着:

 # udev_rules - The name and location of the udev rules file 
 udev_rules="/etc/udev/rules.d/"

也就是说,我们写各种匹配规则可以放在/etc/udev/rules.d/目录下。在该目录,一般每个文件都代表某个系列的规则,比如声卡,网卡,usb等等的系列。
文件的每一行代表一个规则。每个规则分成一个或多个匹配部分和赋值部分。匹配部分用匹配专用的关键字来表示,相应的赋值部分用赋值专用的关键字来表示。
匹配关键字包括:ACTION(行为)、KERNEL(匹配内核设备名)、BUS(匹配总线类型)、SUBSYSTEM(匹配子系统名)、ATTR(属性)等。
赋值关键字包括:NAME(创建的设备文件名)、RUN(运行程序)、SYMLINK(符号创建链接名)、OWNER(设置设备的所有者)、GROUP(设置设备的组)、IMPORT(调用外部程序)、MODE(节点访问权限)等。
举个例子:
在某HDMI声卡的规则,识别到声卡后,需要执行脚本设置音量。

KERNEL=="card0", SUBSYSTEM=="drm", ACTION=="change", RUN+="/usr/lib/pulse-13.99.1/scripts/hdmi_sound_toggle.sh"

u盘自动挂载。

ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/read_ahead_kb}="4096" RUN+="/root/usbmount.sh %k"

我们如果一开始不知道设备上报的信息,可以使用udevadm查看sys或者dev目录下的设备:
sys目录的:

root@ubuntu:/home/jian# udevadm info -a -p /sys/devices/pci0000:00/0000:00:11.0/0000:02:01.0/net/ens33

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/pci0000:00/0000:00:11.0/0000:02:01.0/net/ens33':
    KERNEL=="ens33"
    SUBSYSTEM=="net"
    DRIVER==""
    ATTR{name_assign_type}=="4"
    ATTR{flags}=="0x1003"
    ATTR{proto_down}=="0"
    ATTR{dormant}=="0"
    ATTR{duplex}=="full"
    ATTR{type}=="1"
    ATTR{carrier_down_count}=="7"
    ATTR{carrier}=="1"
    ATTR{ifalias}==""
    ATTR{testing}=="0"
    ATTR{napi_defer_hard_irqs}=="0"
    ATTR{gro_flush_timeout}=="0"
    ATTR{link_mode}=="0"
    ATTR{broadcast}=="ff:ff:ff:ff:ff:ff"
    ATTR{ifindex}=="2"
    ATTR{operstate}=="up"
    ATTR{dev_port}=="0"
    ATTR{addr_len}=="6"
    ATTR{address}=="00:0c:29:54:81:2e"
    ATTR{addr_assign_type}=="0"
    ATTR{netdev_group}=="0"
    ATTR{speed}=="1000"
    ATTR{dev_id}=="0x0"
    ATTR{tx_queue_len}=="1000"
    ATTR{mtu}=="1500"
    ATTR{carrier_up_count}=="7"
    ATTR{carrier_changes}=="14"
    ATTR{threaded}=="0"
    ATTR{iflink}=="2"

dev:

root@ubuntu:/home/jian# udevadm info -a /dev/sda1

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/pci0000:00/0000:00:10.0/host32/target32:0:0/32:0:0:0/block/sda/sda1':
    KERNEL=="sda1"
    SUBSYSTEM=="block"
    DRIVER==""
    ATTR{stat}=="     178       29    13470       31        2        0        2        0        0       68       32        0        0        0        0        0        0"
    ATTR{size}=="1048576"
    ATTR{start}=="2048"
    ATTR{discard_alignment}=="0"
    ATTR{ro}=="0"
    ATTR{inflight}=="       0        0"
    ATTR{alignment_offset}=="0"
    ATTR{partition}=="1"

  looking at parent device '/devices/pci0000:00/0000:00:10.0/host32/target32:0:0/32:0:0:0/block/sda':
    KERNELS=="sda"
    SUBSYSTEMS=="block"
    DRIVERS==""
    ATTRS{events_async}==""
    ATTRS{ro}=="0"
    ATTRS{events}==""
    ATTRS{removable}=="0"
    ATTRS{ext_range}=="256"
    ATTRS{hidden}=="0"
    ATTRS{events_poll_msecs}=="-1"
    ATTRS{inflight}=="       0        0"
    ATTRS{alignment_offset}=="0"
    ATTRS{discard_alignment}=="0"
    ATTRS{capability}=="40"
    ATTRS{size}=="1048576000"
    ATTRS{diskseq}=="11"
    ATTRS{range}=="16"
    ATTRS{stat}=="   17204     8205  1263444    14175     5885     4401   113634     2409        0    17076    16584        0        0        0        0        0        0"

通过这种方式看到设备上报的信息后就可以根据其属性设置其规则了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小坚学Linux

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值