For my application, I need to automatically run something whenever a flash drive is plugged into the RPi, and that code needs to know the contents of the flash drive.
I am using a custom Raspberry Pi OS based on pi-gen.
So far I have been able to do the following:
1. Plug in a flash drive and find `sd#` via `lsblk`, then use e.g. `sudo mount /dev/sda1 /media/my-location` to mount it, then read the files. Great, this works manually.
2. Set up a udev action like this:
I was able to successfully run both `connect-service-port-drive.sh` and `disconnect-service-port-drive.sh`.
However, from what I understand, the `/dev/sd#` file does not show up until after udev is finished. This means that I cannot `mount /dev/sd# /media/my-location` to inspect the contents of the flash drive DURING the udev script execution. If I `ls /dev` during the udev script, it doesn't show any `sd#` devices.
What should my approach be here? Create a systemd service that periodically polls to see if a flash drive is connected? Set some kind of magic setting such that udev works? Perform some other kind of auto-mount that will allow me to read the files during udev? Somehow read the files without mounting? Any help would be appreciated
.
I am using a custom Raspberry Pi OS based on pi-gen.
So far I have been able to do the following:
1. Plug in a flash drive and find `sd#` via `lsblk`, then use e.g. `sudo mount /dev/sda1 /media/my-location` to mount it, then read the files. Great, this works manually.
2. Set up a udev action like this:
Code:
ACTION=="add", SUBSYSTEM=="usb", RUN="/opt/connect-service-port-drive.sh"ACTION=="remove", SUBSYSTEM=="usb", RUN="/opt/disconnect-service-port-drive.sh"However, from what I understand, the `/dev/sd#` file does not show up until after udev is finished. This means that I cannot `mount /dev/sd# /media/my-location` to inspect the contents of the flash drive DURING the udev script execution. If I `ls /dev` during the udev script, it doesn't show any `sd#` devices.
What should my approach be here? Create a systemd service that periodically polls to see if a flash drive is connected? Set some kind of magic setting such that udev works? Perform some other kind of auto-mount that will allow me to read the files during udev? Somehow read the files without mounting? Any help would be appreciated
Statistics: Posted by nullromo — Tue Jun 10, 2025 4:58 pm