The post outlines steps to create UDEV rules that run a specific script when a USB device is attached or removed. We can use the udevadm tool to see the information and properties when a device is attached or removed. Using the “subsystem-match=SUBSYSTEM” We can determinate the kind of device that is attached/removed.

 

1. For example, in this case with a USB SUBSYSTEM, you can run command below:

# udevadm monitor --kernel --property --subsystem-match=usb
monitor will print the received events for:
KERNEL - the kernel uevent

 

2. Now, attach or remove the USB device, to trigger a UDEV event.

 

3. Below is the example of output, the information with “<---" lines is important information to considerate to set the rules.

# udevadm monitor --kernel --property --subsystem-match=usb
monitor will print the received events for:
KERNEL - the kernel uevent
KERNEL[6147.486744] add /devices/pci0000:00/0000:00:0c.0/usb1/1-2 (usb)
ACTION=add             <--------------------------------------------------------- ACTION of the device
BUSNUM=001
DEVNAME=/dev/bus/usb/001/010
DEVNUM=010
DEVPATH=/devices/pci0000:00/0000:00:0c.0/usb1/1-2
DEVTYPE=usb_device      
MAJOR=189
MINOR=9
PRODUCT=90c/1000/1100  <--------------------------------------------------------- ENV{PRODUCT}
SEQNUM=2192
SUBSYSTEM=usb          <--------------------------------------------------------- " SUBSYSTEM=="type_of_device"
TYPE=0/0/0

KERNEL[6147.489141] add /devices/pci0000:00/0000:00:0c.0/usb1/1-2/1-2:1.0 (usb)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:0c.0/usb1/1-2/1-2:1.0
DEVTYPE=usb_interface
INTERFACE=8/6/80
MODALIAS=usb:v090Cp1000d1100dc00dsc00dp00ic08isc06ip50in00
PRODUCT=90c/1000/1100
SEQNUM=2193
SUBSYSTEM=usb
TYPE=0/0/0

KERNEL[6206.530205] remove /devices/pci0000:00/0000:00:0c.0/usb1/1-2/1-2:1.0 (usb)
ACTION=remove           <--------------------------------------------------------- ACTION of the device 
DEVPATH=/devices/pci0000:00/0000:00:0c.0/usb1/1-2/1-2:1.0
DEVTYPE=usb_interface
INTERFACE=8/6/80
MODALIAS=usb:v090Cp1000d1100dc00dsc00dp00ic08isc06ip50in00
PRODUCT=90c/1000/1100   <--------------------------------------------------------- ENV{PRODUCT}
SEQNUM=2216
SUBSYSTEM=usb           <--------------------------------------------------------- " SUBSYSTEM=="type_of_device"
TYPE=0/0/0
KERNEL[6206.530551] remove /devices/pci0000:00/0000:00:0c.0/usb1/1-2 (usb)
ACTION=remove
BUSNUM=001
DEVNAME=/dev/bus/usb/001/010
DEVNUM=010
DEVPATH=/devices/pci0000:00/0000:00:0c.0/usb1/1-2
DEVTYPE=usb_device
MAJOR=189
MINOR=9
PRODUCT=90c/1000/1100
SEQNUM=2217
SUBSYSTEM=usb
TYPE=0/0/0

4. Using above example, you can create the rules to run scripts in /etc/udev/rules.d/. For example, you can create a new file "5-usbstick-font.rules", with a syntax similar to :

# cat /etc/udev/rules.d/5-usbstick-font.rules
ACTION=="add", SUBSYSTEM=="usb", ENV{PRODUCT}=="90c/1000/1100", RUN=="/bin/su godiego --command='/home/godiego/run-start.sh'"
ACTION=="remove", SUBSYSTEM=="usb", ENV{PRODUCT}=="90c/1000/1100", RUN+="/bin/su godiego --command='/home/godiego/run-stop.sh'"

 

5. Reload the UDEV rule by running the below command.

# udevadm control --reload

 

Now, if you attach or remove the USB device again, the customized script (like run-start.sh or run-stop.sh) will be executed.

 

آیا این پاسخ به شما کمک کرد؟ 0 کاربر این را مفید یافتند (0 نظرات)