查看设备信息:
操作udev,可以使用udevadm命令,如果我们要查看/dev/sda 设备节点信息,我们可以使用下面命令:
命令:udevadm info -a --name=sda
root@ubuntu:/sys/kernel/debug/usb# udevadm info -a --name=sda
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/host2/target2:0:0/2:0:0:0/block/sda':
KERNEL=="sda"
SUBSYSTEM=="block"
DRIVER==""
ATTR{alignment_offset}=="0"
ATTR{capability}=="50"
ATTR{discard_alignment}=="0"
ATTR{events}==""
ATTR{events_async}==""
ATTR{events_poll_msecs}=="-1"
ATTR{ext_range}=="256"
ATTR{inflight}==" 0 0"
ATTR{range}=="16"
ATTR{removable}=="0"
ATTR{ro}=="0"
ATTR{size}=="104857600"
ATTR{stat}==" 31150 512 1309166 61636 7972 25458 293096 2600 0 25092 64168"
looking at parent device '/devices/pci0000:00/0000:00:10.0/host2/target2:0:0/2:0:0:0':
KERNELS=="2:0:0:0"
SUBSYSTEMS=="scsi"
DRIVERS=="sd"
ATTRS{device_blocked}=="0"
ATTRS{device_busy}=="0"
ATTRS{dh_state}=="detached"
ATTRS{eh_timeout}=="10"
ATTRS{evt_capacity_change_reported}=="0"
ATTRS{evt_inquiry_change_reported}=="0"
ATTRS{evt_lun_change_reported}=="0"
ATTRS{evt_media_change}=="0"
ATTRS{evt_mode_parameter_change_reported}=="0"
ATTRS{evt_soft_threshold_reached}=="0"
ATTRS{inquiry}==""
ATTRS{iocounterbits}=="32"
ATTRS{iodone_cnt}=="0x9918"
ATTRS{ioerr_cnt}=="0x3"
ATTRS{iorequest_cnt}=="0x9918"
ATTRS{model}=="VMware Virtual S"
ATTRS{queue_depth}=="32"
ATTRS{queue_ramp_up_period}=="120000"
ATTRS{queue_type}=="simple"
ATTRS{rev}=="1.0 "
ATTRS{scsi_level}=="3"
ATTRS{state}=="running"
ATTRS{timeout}=="180"
ATTRS{type}=="0"
ATTRS{vendor}=="VMware, "
looking at parent device '/devices/pci0000:00/0000:00:10.0/host2/target2:0:0':
KERNELS=="target2:0:0"
SUBSYSTEMS=="scsi"
DRIVERS==""
looking at parent device '/devices/pci0000:00/0000:00:10.0/host2':
KERNELS=="host2"
SUBSYSTEMS=="scsi"
DRIVERS==""
looking at parent device '/devices/pci0000:00/0000:00:10.0':
KERNELS=="0000:00:10.0"
SUBSYSTEMS=="pci"
DRIVERS=="mptspi"
ATTRS{broken_parity_status}=="0"
ATTRS{class}=="0x010000"
ATTRS{config}==""
ATTRS{consistent_dma_mask_bits}=="32"
ATTRS{d3cold_allowed}=="0"
ATTRS{device}=="0x0030"
ATTRS{dma_mask_bits}=="32"
ATTRS{driver_override}=="(null)"
ATTRS{enable}=="1"
ATTRS{irq}=="17"
ATTRS{local_cpulist}=="0"
ATTRS{local_cpus}=="00000000,00000000,00000000,00000001"
ATTRS{msi_bus}=="1"
ATTRS{numa_node}=="-1"
ATTRS{subsystem_device}=="0x1976"
ATTRS{subsystem_vendor}=="0x15ad"
ATTRS{vendor}=="0x1000"
looking at parent device '/devices/pci0000:00':
KERNELS=="pci0000:00"
SUBSYSTEMS==""
DRIVERS==""
root@ubuntu:/sys/kernel/debug/usb#
其中比较常用的:
匹配键:“==”,“!=”
赋值键:“=”,“+=”,":=
KERNEL - 匹配设备的内核名字
SUBSYSTEM - 匹配设备的子系统
DRIVER - 匹配设备驱动名
NAME - 应当被采用为设备节点的名字
SYMLINK - 一系列被作为设备节点替补名字的符号链接
KERNELS - 硬件端口号,有些设备的 idVendor和idProduct 是相同,依靠这两个并不能完全准确的区分目标设备,这时候就需要用 KERNELS 硬件端口号。
以当前 ubuntu 虚拟机为平台做个实验
cd /etc/udev
root@ubuntu:/etc/udev# tree
.
├── hwdb.d
├── rules.d
│ └── 99-vmware-scsi-udev.rules
└── udev.conf
2 directories, 2 files
root@ubuntu:/etc/udev#
root@ubuntu:/etc/udev#
我们来通过 udev 修改插入的当前U盘的设备名
在没有插入u盘前 /dev 下只有 sda相关节点。在插入u盘后,出现sdb相关节点, 则当前u盘对应的设备节点就是sdb。 我们将插入的u盘节点名 固定位 usb-Upan。其实就是原来的 设备名的一个 符号链接文件名,就是个别名,原来的名字还是存在的。
插入U盘,udevadm info -a --name=sdb 查看/dev/sdb 设备节点信息:
root@ubuntu:/etc/udev/rules.d# udevadm info -a --name=sdb
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:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35/target35:0:0/35:0:0:0/block/sdb':
KERNEL=="sdb"
SUBSYSTEM=="block"
DRIVER==""
ATTR{alignment_offset}=="0"
ATTR{capability}=="51"
ATTR{discard_alignment}=="0"
ATTR{events}=="media_change"
ATTR{events_async}==""
ATTR{events_poll_msecs}=="-1"
ATTR{ext_range}=="256"
ATTR{inflight}==" 0 0"
ATTR{range}=="16"
ATTR{removable}=="1"
ATTR{ro}=="0"
ATTR{size}=="15232000"
ATTR{stat}==" 153 3898 8218 620 1 0 1 0 0 324 620"
looking at parent device '/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35/target35:0:0/35:0:0:0':
KERNELS=="35:0:0:0"
SUBSYSTEMS=="scsi"
DRIVERS=="sd"
ATTRS{device_blocked}=="0"
ATTRS{device_busy}=="0"
ATTRS{dh_state}=="detached"
ATTRS{eh_timeout}=="10"
ATTRS{evt_capacity_change_reported}=="0"
ATTRS{evt_inquiry_change_reported}=="0"
ATTRS{evt_lun_change_reported}=="0"
ATTRS{evt_media_change}=="0"
ATTRS{evt_mode_parameter_change_reported}=="0"
ATTRS{evt_soft_threshold_reached}=="0"
ATTRS{inquiry}==""
ATTRS{iocounterbits}=="32"
ATTRS{iodone_cnt}=="0xeb"
ATTRS{ioerr_cnt}=="0x1"
ATTRS{iorequest_cnt}=="0xeb"
ATTRS{max_sectors}=="240"
ATTRS{model}=="Cruzer Force "
ATTRS{queue_depth}=="1"
ATTRS{queue_type}=="none"
ATTRS{rev}=="1.00"
ATTRS{scsi_level}=="7"
ATTRS{state}=="running"
ATTRS{timeout}=="30"
ATTRS{type}=="0"
ATTRS{vendor}=="SanDisk "
looking at parent device '/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35/target35:0:0':
KERNELS=="target35:0:0"
SUBSYSTEMS=="scsi"
DRIVERS==""
looking at parent device '/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35':
KERNELS=="host35"
SUBSYSTEMS=="scsi"
DRIVERS==""
looking at parent device '/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0':
KERNELS=="3-2:1.0"
SUBSYSTEMS=="usb"
DRIVERS=="usb-storage"
ATTRS{authorized}=="1"
ATTRS{bAlternateSetting}==" 0"
ATTRS{bInterfaceClass}=="08"
ATTRS{bInterfaceNumber}=="00"
ATTRS{bInterfaceProtocol}=="50"
ATTRS{bInterfaceSubClass}=="06"
ATTRS{bNumEndpoints}=="02"
ATTRS{supports_autosuspend}=="1"
looking at parent device '/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2':
KERNELS=="3-2"
SUBSYSTEMS=="usb"
DRIVERS=="usb"
ATTRS{authorized}=="1"
ATTRS{avoid_reset_quirk}=="0"
ATTRS{bConfigurationValue}=="1"
ATTRS{bDeviceClass}=="00"
ATTRS{bDeviceProtocol}=="00"
ATTRS{bDeviceSubClass}=="00"
ATTRS{bMaxPacketSize0}=="64"
ATTRS{bMaxPower}=="224mA"
ATTRS{bNumConfigurations}=="1"
ATTRS{bNumInterfaces}==" 1"
ATTRS{bcdDevice}=="0100"
ATTRS{bmAttributes}=="80"
ATTRS{busnum}=="3"
ATTRS{configuration}==""
ATTRS{devnum}=="7"
ATTRS{devpath}=="2"
ATTRS{idProduct}=="557d"
ATTRS{idVendor}=="0781"
ATTRS{ltm_capable}=="no"
ATTRS{manufacturer}=="SanDisk"
ATTRS{maxchild}=="0"
ATTRS{product}=="Cruzer Force"
ATTRS{quirks}=="0x0"
ATTRS{removable}=="unknown"
ATTRS{serial}=="4C530001130404110045"
ATTRS{speed}=="480"
ATTRS{urbnum}=="677"
ATTRS{version}==" 2.10"
looking at parent device '/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3':
KERNELS=="usb3"
SUBSYSTEMS=="usb"
DRIVERS=="usb"
ATTRS{authorized}=="1"
ATTRS{authorized_default}=="1"
ATTRS{avoid_reset_quirk}=="0"
ATTRS{bConfigurationValue}=="1"
ATTRS{bDeviceClass}=="09"
ATTRS{bDeviceProtocol}=="01"
ATTRS{bDeviceSubClass}=="00"
ATTRS{bMaxPacketSize0}=="64"
ATTRS{bMaxPower}=="0mA"
ATTRS{bNumConfigurations}=="1"
ATTRS{bNumInterfaces}==" 1"
ATTRS{bcdDevice}=="0404"
ATTRS{bmAttributes}=="e0"
ATTRS{busnum}=="3"
ATTRS{configuration}==""
ATTRS{devnum}=="1"
ATTRS{devpath}=="0"
ATTRS{idProduct}=="0002"
ATTRS{idVendor}=="1d6b"
ATTRS{interface_authorized_default}=="1"
ATTRS{ltm_capable}=="no"
ATTRS{manufacturer}=="Linux 4.4.0-21-generic xhci-hcd"
ATTRS{maxchild}=="4"
ATTRS{product}=="xHCI Host Controller"
ATTRS{quirks}=="0x0"
ATTRS{removable}=="unknown"
ATTRS{serial}=="0000:03:00.0"
ATTRS{speed}=="480"
ATTRS{urbnum}=="104"
ATTRS{version}==" 2.00"
looking at parent device '/devices/pci0000:00/0000:00:15.0/0000:03:00.0':
KERNELS=="0000:03:00.0"
SUBSYSTEMS=="pci"
DRIVERS=="xhci_hcd"
ATTRS{acpi_index}=="16777984"
ATTRS{broken_parity_status}=="0"
ATTRS{class}=="0x0c0330"
ATTRS{consistent_dma_mask_bits}=="64"
ATTRS{d3cold_allowed}=="1"
ATTRS{device}=="0x0779"
ATTRS{dma_mask_bits}=="64"
ATTRS{driver_override}=="(null)"
ATTRS{enable}=="1"
ATTRS{irq}=="0"
ATTRS{label}=="usb_xhci"
ATTRS{local_cpulist}=="0"
ATTRS{local_cpus}=="00000000,00000000,00000000,00000001"
ATTRS{msi_bus}=="1"
ATTRS{numa_node}=="-1"
ATTRS{subsystem_device}=="0x0779"
ATTRS{subsystem_vendor}=="0x15ad"
ATTRS{vendor}=="0x15ad"
looking at parent device '/devices/pci0000:00/0000:00:15.0':
KERNELS=="0000:00:15.0"
SUBSYSTEMS=="pci"
DRIVERS=="pcieport"
ATTRS{broken_parity_status}=="0"
ATTRS{class}=="0x060400"
ATTRS{consistent_dma_mask_bits}=="32"
ATTRS{d3cold_allowed}=="0"
ATTRS{device}=="0x07a0"
ATTRS{dma_mask_bits}=="32"
ATTRS{driver_override}=="(null)"
ATTRS{enable}=="2"
ATTRS{irq}=="24"
ATTRS{local_cpulist}=="0"
ATTRS{local_cpus}=="00000000,00000000,00000000,00000001"
ATTRS{msi_bus}=="1"
ATTRS{numa_node}=="-1"
ATTRS{subsystem_device}=="0x07a0"
ATTRS{subsystem_vendor}=="0x15ad"
ATTRS{vendor}=="0x15ad"
looking at parent device '/devices/pci0000:00':
KERNELS=="pci0000:00"
SUBSYSTEMS==""
DRIVERS==""
可以看出 该 sdb 的硬件端口号是 “3-2:1.0”
在/etc/udev/rule.d 下创建相应的规则文件,为 sdb 起一个别名: usb-Upan
root@ubuntu:/etc/udev/rules.d# touch 01-Upan.rules
增加一个 01-Upan.rules
内容:
KERNELS==“3-2:1.0”, MODE:=“0777”, GROUP:=“dialout”, SYMLINK+=“usb-Upan”
这样就为 sdb 取了一个别名为 “usb-Upan” ,以后在程序里直接访问 “usb-Upan”,就可以与该u盘通信了。
运行以下命令使修改立即生效:
sudo udevadm trigger
或者然后拔插u盘,果然出现了 “usb-Upan”
注意是符号链接
mhr@ubuntu:/dev$ ls -l usb-Upan
lrwxrwxrwx 1 root root 3 1月 19 00:14 usb-Upan -> sdb
同理 SD卡 操作也类似: 示例中硬件端口号是随机写的,思路是一样的,只不过这里 也指定了原生的设备名为 “sd*”,增加了筛选条件。
KERNELS=="1-2.3",KERNEL=="sd*",SYMLINK+="usbsda%n",OPTIONS="ignore_remove"
KERNELS=="1-2.4",KERNEL=="sd*",SYMLINK+="usbsdb%n",OPTIONS="ignore_remove"
KERNELS=="1-2.2",KERNEL=="sd*",SYMLINK+="usbsdc%n",OPTIONS="ignore_remove"