how can I detect on the Pi when my mass storage device was ejected by the host?
I am using a Raspberry Pi 4 as a mass storage device with the dwc2 module. I am "inserting" the mass storage device via the following command, which then successfully leads the host to detect a removable device:
sudo modprobe g_mass_storage file=/var/usb_storage_backing removable=1 ro=1
Following the instructions from https://docs.kernel.org/usb/mass-storage.html#sysfs-entries I can also "eject" and "reinsert" the mass storage device from the Pi by doing the following:
echo | sudo tee /sys/class/udc/*/device/gadget.0/lun0/file
echo /var/usb_storage_backing | sudo tee /sys/class/udc/*/device/gadget.0/lun0/file
The kernel docs state the following regarding
When read it returns the path to the backing file for the given logical unit. If there is no backing file (possible only if the logical unit is removable), the content is empty.
which lead me to believe that when the host ejects the mass storage device, the content of
/sys/class/udc/*/device/gadget.0/lun0/file would be empty. This is, however, not the case, at least not with a Windows 10 host.
Is there any way to detect on the Pi when the host system has ejected the mass storage device? Preferably in a way feasible with a bash script.