linux内核的一些基础知识: 内核设备编号及udev等

1. Linux内核设备编号

分为主设备编号和次设备编号,前者为dev_t的高12位,后者为dev_t的低20位.

对于表示设备节点的inode结构,i_rdev字段包含设备编号.下列操作用于从一个inode中获得主设备号和次设备号:
 

unsigned int iminor(struct inode *inode);

unsigned int imajor(struct inode *inode);

cat /proc/devices可以获知系统中注册的设备.第一列为主设备编号,第二列为设备名.eg:

$ cat /proc/devices
Character devices:
  1 mem
  4 /dev/vc/0
  4 tty
  4 ttyS

ls /dev/ -alt | more可以获知对应设备的主次设备号,日期前的两列跟别给出了对应设备的主设备号和次设备号.eg:

$ ls /dev/ -alt | more
total 4
crw-rw-rw-   1 root tty       5,   2 May 26 10:05 ptmx
drwxrwxrwt   2 root root         200 May 26 10:05 shm
drwxr-xr-x   2 root root        3720 May 25 20:43 char
drwxr-xr-x   3 root root           0 May 25 20:43 hugepages
crw-rw-rw-+  1 root kvm      10, 232 May 25 20:43 kvm

2. udev可以利用内核通过netlink发出的uevent信息动态创建设备文件节点.

可以借助udev中的udevadm info工具查找规则文件能利用的内核信息和sysfs属性信息.

如运行: udevadm info -a -p /sys/devices/platform/serial8250/tty/ttyS10 将得到:

$ udevadm info -a -p /sys/devices/platform/serial8250/tty/ttyS10

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/platform/serial8250/tty/ttyS10':
    KERNEL=="ttyS10"
    SUBSYSTEM=="tty"
    DRIVER==""

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

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

如果/dev/下面的节点已经被创建,但是不知道它对应的/sys具体节点路径,可以采用

udevadm info -a -p $(udevadm info -q path -n /dev/节点名)命令来反向分析.如:

[LiKaige@bogon ~]$ udevadm info -a -p $(udevadm info -q path -n /dev/snd/pcmC0D0c)

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:07.0/sound/card0/pcmC0D0c':
    KERNEL=="pcmC0D0c"
    SUBSYSTEM=="sound"
    DRIVER==""
    ATTR{pcm_class}=="generic"

  looking at parent device '/devices/pci0000:00/0000:00:07.0/sound/card0':
    KERNELS=="card0"
    SUBSYSTEMS=="sound"
    DRIVERS==""
    ATTRS{id}=="Loongson"
    ATTRS{number}=="0"

  looking at parent device '/devices/pci0000:00/0000:00:07.0':
    KERNELS=="0000:00:07.0"
    SUBSYSTEMS=="pci"
    DRIVERS=="azx_pci_driver"
    ATTRS{irq}=="122"
    ATTRS{subsystem_vendor}=="0x0000"
    ATTRS{broken_parity_status}=="0"
    ATTRS{devspec}==""
    ATTRS{class}=="0x040300"
    ATTRS{driver_override}=="(null)"
    ATTRS{consistent_dma_mask_bits}=="64"
    ATTRS{dma_mask_bits}=="64"
    ATTRS{local_cpus}=="000f"
    ATTRS{device}=="0x7a07"
    ATTRS{enable}=="1"
    ATTRS{msi_bus}==""
    ATTRS{local_cpulist}=="0-3"
    ATTRS{vendor}=="0x0014"
    ATTRS{subsystem_device}=="0x0000"
    ATTRS{numa_node}=="0"
    ATTRS{d3cold_allowed}=="0"

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章