4

Simple question: how to add rule for usb flash drives only in /lib/udev/rules.d/ catalog?

For e.g. if I want to allow only usb flash drives for my PC to be inserted. Or allow only my flash drive. Is there any device type/grupot for that purpose?

Could anybody provide some example for noob, please? I hope it is easy to solve :)

1 Answers1

2

I answered a similar question with an example a while ago:

Running the script when it detects I/O

Use this command,

udevadm monitor --property

Insert the USB and check a property that is common to the devices that you want to use, e.g:

ID_TYPE=disk
ID_USB_DRIVER=usb-storage

Then create your rule

sudo gedit /etc/udev/rules.d/96-myusb.rules

with those values in

ACTION=="add", SUBSYSTEM=="usb",ENV{ID_TYPE}=="disk", ENV{ID_USB_DRIVER}=="usb-storage",RUN+="/usr/local/bin/myusb-add.sh"

ACTION=="remove", SUBSYSTEM=="usb",ENV{ID_TYPE}=="disk",ENV{ID_USB_DRIVER}=="usb-storage",RUN+="/usr/local/bin/myusb-remove.sh"

The rules file will only affect those devices that match the ENV{ID_TYPE} and ENV{ID_USB_DRIVER}. It will not affect any other device. If you want to restrict the rest of devices, maybe you can create a rule that matches the rest of devices and does nothing in the RUN.

If you want to run the action only when a certain usb device is connected to an specific USB port, try adding ENV{DEVPATH}="yourUSBPATH" to the rules file. Substitute yourUSBPATH for the path that udevadm monitor --property reports when you connect the device to the port that you want to use. I haven't tested this but I think it's a logical approach.

Katu
  • 3,663