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
sleep 5
echo /var/usb_storage_backing | sudo tee /sys/class/udc/*/device/gadget.0/lun0/file

The kernel docs state the following regarding file

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.

  • "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." Huh?? This is un-clear.
    – Seamus
    Oct 30, 2023 at 18:13
  • @Seamus I understood that when it is a removable drive, the content of the file file gets cleared when the host ejects the removable drive. This is, however, not the case. It apparently means that when it is a removable drive you can clear the content yourself and it will "remove" the drive from the host. I am looking for a way to get notified when on the host side you eject the drive. In Windows this is done as described here support.microsoft.com/en-us/windows/…
    – cbg
    Oct 31, 2023 at 8:24
  • My point was, "what has Windows got to do with Raspberry Pi SE?"
    – Seamus
    Oct 31, 2023 at 11:50
  • My Raspberry Pi is the USB device, the host is a Windows client. It's just additional information regarding my setup. Of course it could be a Linux host as well. My issue is still that on the device side (Raspberry Pi) I can't / don't know how to detect an ejection by the host.
    – cbg
    Oct 31, 2023 at 18:24

1 Answer 1


OK, here's the story AIUI: There's probably not a "canned" method for detecting USB status. Your kernel reference is undated, but it may be quite old. I love it that the kernel devs can't even be bothered to put dates on the s**t they stuff in their repos :/.

Most of what I've seen wrt "detection" of USB storage device status ultimately boils down to some sort of a program or script that does polling. I did several searches, finally settling on this one, and found a couple of items worth reading:

  1. Use libudev or udevadm to build an application (or script in the case of udevadm) to monitor "device events". Here's an old-ish Q&A from SO that gives a few details.

  2. This post from from the Baeldung blog outlines the use of several tools that may be used to construct a script that checks (polls) for the presence of a USB device.

And so the script logic would be roughly as follows:

  1. Generate some output (e.g. lsusb) to establish a "baseline" of connected devices.

  2. Wait (sleep) a while

  3. Run the same command as in 1.

  4. Compare output of step 3 to the baseline & report any diffs.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.