针对每个 USB 设备的 udev 规则

针对每个 USB 设备的 udev 规则

我正在尝试在插入 USB 时执行一个脚本。我目前有一个演示脚本/usr/local/test.sh

#!/bin/bash

touch /home/kfir/test.txt

我还有一个规则文件,它尝试匹配任何 USB /etc/udev/rules.d/100-usb.rules

ACTION=="add", ATTRS{idVendor}=="****", ATTRS{idProduct}=="****", RUN+="/usr/local/test.sh"

问题是该test.txt文件从未被创建。我还收到以下错误:

错误信息

我现在不知道该怎么做。我想要实现的目标相当简单,当插入 USB 存储设备时,运行一个简单的脚本(在本例中创建一个 test.txt 文件)。

PS 该test.sh文件运行正常。当我手动运行它时,它会创建该test.txt文件。

答案1

每个 USB 设备?使用下面这行简单的代码/etc/udev/rules.d/100-usb.rules

ACTION=="add", RUN+="/bin/mkdir /tmp/folder1"

并重新启动udev

sudo service udev restart

这是消耗品

ATTRS{idVendor}=="****", ATTRS{idProduct}=="****", 

答案2

  • 这些ATTRS{idVendor}=="****", ATTRS{idProduct}=="****"针对任何事物,你可以将它们从规则中删除。

  • 但是,这样一来,它将被许多树节点和所有设备甚至非 USB 设备触发。

    尝试这个针对单个节点和仅 USB 设备的规则:

    ACTION=="add", SUBSYSTEM=="usb", DRIVER=="usb", RUN+="/usr/local/test.sh"
    
  • 我如何得到这个结果:

    1. 跑步udevadm monitor -u
    2. 插入闪存驱动器,其输出与此相同:

      UDEV  [13394.985946] add      /devices/pci0000:00/0000:00:14.0/usb1/1-3 (usb)                                                                        
      UDEV  [13394.991173] add      /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0 (usb)                                                                
      UDEV  [13394.992509] add      /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host9 (scsi)                                                         
      UDEV  [13394.993436] add      /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host9/scsi_host/host9 (scsi_host)                                    
      UDEV  [13395.978250] add      /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host9/target9:0:0 (scsi)                                             
      UDEV  [13395.979665] add      /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host9/target9:0:0/9:0:0:0 (scsi)                                     
      UDEV  [13395.980363] add      /devices/virtual/bdi/8:32 (bdi)                                                                                        
      UDEV  [13395.981822] add      /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host9/target9:0:0/9:0:0:0/scsi_disk/9:0:0:0 (scsi_disk)              
      UDEV  [13395.982998] add      /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host9/target9:0:0/9:0:0:0/bsg/9:0:0:0 (bsg)                          
      UDEV  [13395.983447] add      /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host9/target9:0:0/9:0:0:0/scsi_device/9:0:0:0 (scsi_device)          
      UDEV  [13395.983970] add      /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host9/target9:0:0/9:0:0:0/scsi_generic/sg4 (scsi_generic)            
      UDEV  [13396.473531] add      /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host9/target9:0:0/9:0:0:0/block/sdc (block)                          
      UDEV  [13396.528471] add      /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host9/target9:0:0/9:0:0:0/block/sdc/sdc1 (block)
      
    3. 取头节点检查其属性:

      udevadm info -a --path=/sys/devices/pci0000:00/0000:00:14.0/usb1/1-3
      

      输出:

      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/usb1/1-3':
          KERNEL=="1-3"
          SUBSYSTEM=="usb"
          DRIVER=="usb"
          ATTR{bDeviceSubClass}=="00"
          ATTR{bDeviceProtocol}=="00"
          ATTR{devpath}=="3"
          ATTR{idVendor}=="125f"
          ATTR{speed}=="480"
          ATTR{bNumInterfaces}==" 1"
          ATTR{bConfigurationValue}=="1"
          ATTR{bMaxPacketSize0}=="64"
          ATTR{busnum}=="1"
          ATTR{devnum}=="5"
          ATTR{configuration}==""
          ATTR{bMaxPower}=="98mA"
          ATTR{authorized}=="1"
          ATTR{bmAttributes}=="80"
          ATTR{bNumConfigurations}=="1"
          ATTR{maxchild}=="0"
          ATTR{bcdDevice}=="0100"
          ATTR{avoid_reset_quirk}=="0"
          ATTR{quirks}=="0x0"
          ATTR{serial}=="2b712383811292"
          ATTR{version}==" 2.00"
          ATTR{urbnum}=="673"
          ATTR{ltm_capable}=="no"
          ATTR{manufacturer}=="ADATA"
          ATTR{removable}=="unknown"
          ATTR{idProduct}=="c96a"
          ATTR{bDeviceClass}=="00"
          ATTR{product}=="ADATA USB Flash Drive"
      
    4. 现在,我们只检查了 1 个设备,如果我们检查更多设备:手机、鼠标……我们会发现所有 USB 设备SUBSYSTEM=="usb"DRIVER=="usb"相同。因此它们可用于规则匹配。

相关内容