You need to write a rule for the udev daemon that would run a script on a flash drive connection event.

Wrote a simple rule (file name 10-alabel.rules, the highest priority):

KERNEL=="sd*", ENV{ID_FS_UUID}=="a004d31c-120d-4593-b051-6ef8951d52d0" RUN+="/lib/udev/create_virtual.sh %k" 

Content create_virtual.sh :

 #!/bin/bash echo $1 >> /home/accumplus/1.txt exit 0 

This rule works on removing the flash drive. I also need to add. I add the action parameter:

 ACTION=="add", KERNEL=="sd*", ENV{ID_FS_UUID}=="a004d31c-120d-4593-b051-6ef8951d52d0" RUN+="/lib/udev/create_virtual.sh %k" 

After that, the rule refuses to work in principle. I thought that this was due to the presence of the% k parameter (the device most likely had not yet been added to the dev directory, and I already refer to its name), but the removal of this parameter did not change anything.

How to solve this problem?

UPD

Without specifying the environment variable UUID, the rule is triggered. I tried to display the value of this environment variable in a script that starts when a flash drive is added:

 echo $ID_FS_UUID >> /home/accumplus/1.txt 

Displays exactly the value that I specify in the condition of the rule:

 a004d31c-120d-4593-b051-6ef8951d52d0 
  • start small: leave only SUBSYSTEM=="block" and call the script. and in the script, save the environment variables to a file and see what else to add so that your script will work only once when you connect the block device you need. - aleksandr barakin
  • Left ACTION, RUN and added SUBSYSTEM - the rule is triggered by removing the flash drive ... Which environmental variables should be monitored? - AccumPlus
  • @alexanderbarakin a, no. Just the script is executed with a little late. This line works. - AccumPlus
  • @alexanderbarakin indicated the results in the question - AccumPlus

1 answer 1

The problem was in priority. Apparently, the device does not have time to get a UUID, so the conditions of my rule did not work. Renaming the rules file, specifying a larger number, I achieved the desired result.