udev rules targeting every USB device

31,318

Solution 1

Every USB device? Use this simple line in /etc/udev/rules.d/100-usb.rules

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

and restart udev

sudo service udev restart

This is expendable

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

Solution 2

  • These ATTRS{idVendor}=="****", ATTRS{idProduct}=="****" target anything, you could drop them from the rule.

  • However, that way it will be triggered for many tree nodes and for all device even non USB ones.

    Try this rule which target a single node and only USB devices:

    ACTION=="add", SUBSYSTEM=="usb", DRIVER=="usb", RUN+="/usr/local/test.sh"
    
  • How I got that:

    1. Run udevadm monitor -u
    2. Plug a flash drive, it outputs same as this:

      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. Take the head node check its attributes using:

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

      Output:

      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. Now, we checked only 1 device, if we check more devices: phone, mouse... we will find out that SUBSYSTEM=="usb" and DRIVER=="usb" are same for all USB devices. So they can be used for rule match.

Share:
31,318

Related videos on Youtube

kfirba
Author by

kfirba

Updated on September 18, 2022

Comments

  • kfirba
    kfirba almost 2 years

    I'm trying to execute a script whenever a USB is plugged. What I currently have is a demo script in /usr/local/test.sh:

    #!/bin/bash
    
    touch /home/kfir/test.txt
    

    I also have a rules file which tries to match ANY USB in /etc/udev/rules.d/100-usb.rules:

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

    The problem is that the test.txt file is never being created. I also get the following error:

    error message

    I'm clueless as of what to do now. What I want to achieve is fairly simple, when there is a USB storage device being plugged in, run a simple script (creating a test.txt file in this case).

    P.S. the test.sh file is working fine. When I manually ran it, it creates the test.txt file.

  • kfirba
    kfirba almost 9 years
    What I really want to do is to target only flash-drives because my use-case scenario is looking if a directory exists on the flash drive that has just been connected and if it exist copy it over to a pre-defined location. Since I don't know which USB the end-user will use or at which port he will connect it, I decided to target all USB devices and trigger the script that looks for that folder. I actually like the idea that only USB devices will be targeted here. Is it possible to also limit it to only flash-drives? BTW, the **** seems to work. Maybe it's ignoring the rule since it's invalid?
  • user.dz
    user.dz almost 9 years
    Yes it possible, and this case it is more convenient to target the leaf nodes because is the one that corresponds to the partitions (Ex: /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host9/targ‌​et9:0:0/9:0:0:0/bloc‌​k/sdc/sdc1). Lucky that exactly as you want answered here askubuntu.com/q/625243/26246 . Yeah i tried **** and it works :), same for * , i have never tried this, this way is become worthless. (match anything)