0

I want to reliably and thoroughly identify that a usb pendrive has been connected. I'm using a reduced version¹ of Debian Buster packed in a live ISO image running in VirtualBox.

The process of scanning and identifying is made through a bash script, basically a while loop that is triggered after some user action. The script will always run in the mentioned OS above. It's not that I just want to visually and occasionaly know the information.

Problem:

  • lsblk, fdisk only detect the already connected pendrives; when adding a new one, the commands won't see it.

  • lsusb, dmesg, usb-devices: the output of these commands are somewhat confusing to me, and I can't really find a distinctive pattern that lets me identify that the usb is a pendrive and not other usb device. Maybe you can point me to what pattern should I look like in any of these commands.

  • udevadm needs to be running before the pendrive is plugged. I can ask the user if they is going to plug a pendrive, tell them to wait a few seconds to plug, then run udevadm monitor. But this seems to me a little cumbersome.


The solutions I'm pondering right know are in the lines of comparing a before and after of the output of the latter commands. But alas, I really don't like that kind of approach.


As you see I have a lot of options to work with, but my question is more about to find the most optimal and reliable solution.


¹ Details of this are important? I'm not sure.

schrodingerscatcuriosity
  • 12,087
  • 3
  • 29
  • 57
  • 5
    What do you mean by "detect"? A udev rule will detect a pendrive and can [run a script when it does](https://unix.stackexchange.com/questions/28548/how-to-run-custom-scripts-upon-usb-device-plug-in). This way, you can send a signal to your main process, either via `kill -s` or [IPC](https://opensource.com/article/19/4/interprocess-communication-linux-storage). – iFreilicht Oct 17 '20 at 22:50
  • @iFreilicht Thanks! That is the right path, I forgot absolutely about udev rules. If you want to write an answer I will gladly accept it. – schrodingerscatcuriosity Oct 18 '20 at 00:34
  • Sorry, I don't have time to write a good answer right now. I suggest you take a look at [this answer](https://unix.stackexchange.com/a/55554/67771) and [this question](https://unix.stackexchange.com/q/45791/67771), that should be enough to get you started. Should be enough for you to answer the question yourself :) – iFreilicht Oct 20 '20 at 08:20
  • 1
    @iFreilicht Oh I didn't have a problem to get it working, I knew about udev rules but really forgot about them, you made me remind of them :) – schrodingerscatcuriosity Oct 20 '20 at 08:23
  • I'm very curious about how exactly you got it working. Perhaps you could answer your own question? – Jaap Joris Vens Oct 20 '20 at 09:42

0 Answers0