问题:
有多个摄像头,需要每次设备启动,固定的摄像头的设备号都不变(video0 video1 video2 每次都对应之前的摄像头)
- 每个USB都有个编号,确保插入的USB不变更,或者大小顺序不变,每次重启,内核设备号是从USB编号从小到大依次编号的。
可以通过以下命令查看video设备对应的设备号:
You have new mail in /var/spool/mail/root
[root@localhost SD_IR]# ls -lR /dev/ |grep video
crw-rw----. 1 root video 81, 0 Mar 30 22:17 video0
crw-rw----. 1 root video 81, 1 Mar 30 22:17 video1
crw-rw----. 1 root video 81, 2 Mar 30 22:17 video2
lrwxrwxrwx. 1 root root 9 Mar 30 22:17 81:0 -> ../video0
lrwxrwxrwx. 1 root root 9 Mar 30 22:17 81:1 -> ../video1
lrwxrwxrwx. 1 root root 9 Mar 30 22:17 81:2 -> ../video2
lrwxrwxrwx. 1 root root 12 Mar 30 22:17 usb-HD_Camera_Manufacturer_USB_2.0_Camera-video-index0 -> ../../video0
lrwxrwxrwx. 1 root root 12 Mar 30 22:17 pci-0000:00:14.0-usb-0:1:1.0-video-index0 -> ../../video0
lrwxrwxrwx. 1 root root 12 Mar 30 22:17 pci-0000:00:14.0-usb-0:2:1.0-video-index0 -> ../../video1
lrwxrwxrwx. 1 root root 12 Mar 30 22:17 pci-0000:00:14.0-usb-0:6:1.0-video-index0 -> ../../video2
可以看到,video0对应的USB为pci-0000:00:14.0-usb-0:1:1.0-video-index0
video1对应的USB为pci-0000:00:14.0-usb-0:2:1.0-video-index0
video2对应的USB为pci-0000:00:14.0-usb-0:6:1.0-video-index0
usb-0:1 usb-0:2 usb-0:6 这个跟USB是一一对应的,每次重启不会变。
- 修改udev rules的方法:
检查设备video0的变量:
[root@localhost SD_IR]# udevadm info -a -p $(udevadm info -q path -n /dev/video0)
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:14.0/usb3/3-1/3-1:1.0/video4linux/video0':
KERNEL=="video0"
SUBSYSTEM=="video4linux"
DRIVER==""
ATTR{name}=="USB 2.0 Camera"
ATTR{debug}=="0"
ATTR{index}=="0"
looking at parent device '/devices/pci0000:00/0000:00:14.0/usb3/3-1/3-1:1.0':
KERNELS=="3-1:1.0"
SUBSYSTEMS=="usb"
DRIVERS=="uvcvideo"
ATTRS{bInterfaceClass}=="0e"
ATTRS{iad_bFunctionClass}=="0e"
ATTRS{iad_bFirstInterface}=="00"
ATTRS{bInterfaceSubClass}=="01"
ATTRS{bInterfaceProtocol}=="00"
ATTRS{bNumEndpoints}=="01"
ATTRS{iad_bFunctionSubClass}=="03"
ATTRS{iad_bFunctionProtocol}=="00"
ATTRS{authorized}=="1"
ATTRS{supports_autosuspend}=="1"
ATTRS{iad_bInterfaceCount}=="02"
ATTRS{bAlternateSetting}==" 0"
ATTRS{bInterfaceNumber}=="00"
ATTRS{interface}=="HD USB Camera"
looking at parent device '/devices/pci0000:00/0000:00:14.0/usb3/3-1':
KERNELS=="3-1"
SUBSYSTEMS=="usb"
DRIVERS=="usb"
ATTRS{bDeviceSubClass}=="02"
ATTRS{bDeviceProtocol}=="01"
ATTRS{devpath}=="1"
ATTRS{idVendor}=="05a3"
ATTRS{speed}=="480"
ATTRS{bNumInterfaces}==" 2"
ATTRS{bConfigurationValue}=="1"
ATTRS{bMaxPacketSize0}=="64"
ATTRS{busnum}=="3"
ATTRS{devnum}=="2"
ATTRS{configuration}==""
ATTRS{bMaxPower}=="500mA"
ATTRS{authorized}=="1"
ATTRS{bmAttributes}=="80"
ATTRS{bNumConfigurations}=="1"
ATTRS{maxchild}=="0"
ATTRS{bcdDevice}=="0100"
ATTRS{avoid_reset_quirk}=="0"
ATTRS{quirks}=="0x0"
ATTRS{version}==" 2.00"
ATTRS{urbnum}=="54163754"
ATTRS{ltm_capable}=="no"
ATTRS{manufacturer}=="HD Camera Manufacturer"
ATTRS{removable}=="removable"
ATTRS{idProduct}=="9230"
ATTRS{bDeviceClass}=="ef"
ATTRS{product}=="USB 2.0 Camera"
looking at parent device '/devices/pci0000:00/0000:00:14.0/usb3':
KERNELS=="usb3"
SUBSYSTEMS=="usb"
DRIVERS=="usb"
ATTRS{bDeviceSubClass}=="00"
ATTRS{bDeviceProtocol}=="01"
ATTRS{devpath}=="0"
ATTRS{idVendor}=="1d6b"
ATTRS{speed}=="480"
ATTRS{bNumInterfaces}==" 1"
ATTRS{bConfigurationValue}=="1"
ATTRS{bMaxPacketSize0}=="64"
ATTRS{authorized_default}=="1"
ATTRS{busnum}=="3"
ATTRS{devnum}=="1"
ATTRS{configuration}==""
ATTRS{bMaxPower}=="0mA"
ATTRS{authorized}=="1"
ATTRS{bmAttributes}=="e0"
ATTRS{bNumConfigurations}=="1"
ATTRS{maxchild}=="12"
ATTRS{interface_authorized_default}=="1"
ATTRS{bcdDevice}=="0310"
ATTRS{avoid_reset_quirk}=="0"
ATTRS{quirks}=="0x0"
ATTRS{serial}=="0000:00:14.0"
ATTRS{version}==" 2.00"
ATTRS{urbnum}=="417"
ATTRS{ltm_capable}=="no"
ATTRS{manufacturer}=="Linux 3.10.0-693.el7.x86_64 xhci-hcd"
ATTRS{removable}=="unknown"
ATTRS{idProduct}=="0002"
ATTRS{bDeviceClass}=="09"
ATTRS{product}=="xHCI Host Controller"
looking at parent device '/devices/pci0000:00/0000:00:14.0':
KERNELS=="0000:00:14.0"
SUBSYSTEMS=="pci"
DRIVERS=="xhci_hcd"
ATTRS{irq}=="28"
ATTRS{subsystem_vendor}=="0x8086"
ATTRS{broken_parity_status}=="0"
ATTRS{class}=="0x0c0330"
ATTRS{driver_override}=="(null)"
ATTRS{consistent_dma_mask_bits}=="64"
ATTRS{dma_mask_bits}=="64"
ATTRS{local_cpus}=="f"
ATTRS{device}=="0x8c31"
ATTRS{enable}=="1"
ATTRS{msi_bus}==""
ATTRS{local_cpulist}=="0-3"
ATTRS{vendor}=="0x8086"
ATTRS{subsystem_device}=="0x8c31"
ATTRS{numa_node}=="-1"
ATTRS{d3cold_allowed}=="1"
looking at parent device '/devices/pci0000:00':
KERNELS=="pci0000:00"
SUBSYSTEMS==""
DRIVERS==""
检查设备video1的变量:
[root@localhost SD_IR]# udevadm info -a -p $(udevadm info -q path -n /dev/video1)
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:14.0/usb3/3-2/3-2:1.0/video4linux/video1':
KERNEL=="video1"
SUBSYSTEM=="video4linux"
DRIVER==""
ATTR{name}=="USB 2.0 Camera"
ATTR{debug}=="0"
ATTR{index}=="0"
looking at parent device '/devices/pci0000:00/0000:00:14.0/usb3/3-2/3-2:1.0':
KERNELS=="3-2:1.0"
SUBSYSTEMS=="usb"
DRIVERS=="uvcvideo"
ATTRS{bInterfaceClass}=="0e"
ATTRS{iad_bFunctionClass}=="0e"
ATTRS{iad_bFirstInterface}=="00"
ATTRS{bInterfaceSubClass}=="01"
ATTRS{bInterfaceProtocol}=="00"
ATTRS{bNumEndpoints}=="01"
ATTRS{iad_bFunctionSubClass}=="03"
ATTRS{iad_bFunctionProtocol}=="00"
ATTRS{authorized}=="1"
ATTRS{supports_autosuspend}=="1"
ATTRS{iad_bInterfaceCount}=="02"
ATTRS{bAlternateSetting}==" 0"
ATTRS{bInterfaceNumber}=="00"
ATTRS{interface}=="HD USB Camera"
looking at parent device '/devices/pci0000:00/0000:00:14.0/usb3/3-2':
KERNELS=="3-2"
SUBSYSTEMS=="usb"
DRIVERS=="usb"
ATTRS{bDeviceSubClass}=="02"
ATTRS{bDeviceProtocol}=="01"
ATTRS{devpath}=="2"
ATTRS{idVendor}=="05a3"
ATTRS{speed}=="480"
ATTRS{bNumInterfaces}==" 2"
ATTRS{bConfigurationValue}=="1"
ATTRS{bMaxPacketSize0}=="64"
ATTRS{busnum}=="3"
ATTRS{devnum}=="3"
ATTRS{configuration}==""
ATTRS{bMaxPower}=="500mA"
ATTRS{authorized}=="1"
ATTRS{bmAttributes}=="80"
ATTRS{bNumConfigurations}=="1"
ATTRS{maxchild}=="0"
ATTRS{bcdDevice}=="0100"
ATTRS{avoid_reset_quirk}=="0"
ATTRS{quirks}=="0x0"
ATTRS{version}==" 2.00"
ATTRS{urbnum}=="53656605"
ATTRS{ltm_capable}=="no"
ATTRS{manufacturer}=="HD Camera Manufacturer"
ATTRS{removable}=="removable"
ATTRS{idProduct}=="9230"
ATTRS{bDeviceClass}=="ef"
ATTRS{product}=="USB 2.0 Camera"
looking at parent device '/devices/pci0000:00/0000:00:14.0/usb3':
KERNELS=="usb3"
SUBSYSTEMS=="usb"
DRIVERS=="usb"
ATTRS{bDeviceSubClass}=="00"
ATTRS{bDeviceProtocol}=="01"
ATTRS{devpath}=="0"
ATTRS{idVendor}=="1d6b"
ATTRS{speed}=="480"
ATTRS{bNumInterfaces}==" 1"
ATTRS{bConfigurationValue}=="1"
ATTRS{bMaxPacketSize0}=="64"
ATTRS{authorized_default}=="1"
ATTRS{busnum}=="3"
ATTRS{devnum}=="1"
ATTRS{configuration}==""
ATTRS{bMaxPower}=="0mA"
ATTRS{authorized}=="1"
ATTRS{bmAttributes}=="e0"
ATTRS{bNumConfigurations}=="1"
ATTRS{maxchild}=="12"
ATTRS{interface_authorized_default}=="1"
ATTRS{bcdDevice}=="0310"
ATTRS{avoid_reset_quirk}=="0"
ATTRS{quirks}=="0x0"
ATTRS{serial}=="0000:00:14.0"
ATTRS{version}==" 2.00"
ATTRS{urbnum}=="417"
ATTRS{ltm_capable}=="no"
ATTRS{manufacturer}=="Linux 3.10.0-693.el7.x86_64 xhci-hcd"
ATTRS{removable}=="unknown"
ATTRS{idProduct}=="0002"
ATTRS{bDeviceClass}=="09"
ATTRS{product}=="xHCI Host Controller"
looking at parent device '/devices/pci0000:00/0000:00:14.0':
KERNELS=="0000:00:14.0"
SUBSYSTEMS=="pci"
DRIVERS=="xhci_hcd"
ATTRS{irq}=="28"
ATTRS{subsystem_vendor}=="0x8086"
ATTRS{broken_parity_status}=="0"
ATTRS{class}=="0x0c0330"
ATTRS{driver_override}=="(null)"
ATTRS{consistent_dma_mask_bits}=="64"
ATTRS{dma_mask_bits}=="64"
ATTRS{local_cpus}=="f"
ATTRS{device}=="0x8c31"
ATTRS{enable}=="1"
ATTRS{msi_bus}==""
ATTRS{local_cpulist}=="0-3"
ATTRS{vendor}=="0x8086"
ATTRS{subsystem_device}=="0x8c31"
ATTRS{numa_node}=="-1"
ATTRS{d3cold_allowed}=="1"
looking at parent device '/devices/pci0000:00':
KERNELS=="pci0000:00"
SUBSYSTEMS==""
DRIVERS==""
对比发现不同的是(每个摄像头的urbnum都不一样,显然通过设置udev rules的方法不通用):
ATTRS{urbnum}=="53656605"
对于udev rules的设置详细可参考:
http://www.reactivated.net/writing_udev_rules.html