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==""

 

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