I've got a Rpi Zero 2 W working well in USB Gadget mode providing: mass storage, ethernet and serial connections. I'm doing this with libcomposite and the guides here: https://github.com/thagrol/Guides . I've got an image file as the backing store for the mass storage on the pi. It's formatted as exfat
but I can mount it and read from it from the pi.
From the mass-storage guide, section 3.4 How It Works And Why Write Access Is Bad it explains that the USB Host accesses the filesystem as though it's a physical disk and as such the pi can't know what's going on with what the host might be doing. e.g. a file write may not have finished and the pi might read half written files.
The mass storage is setup like this:
...
modprobe libcomposite
mkdir -p /sys/kernel/config/usb_gadget/$GADGETDIR
cd /sys/kernel/config/usb_gadget/$GADGETDIR
echo 0x1d6b > idVendor # Linux Foundation
echo 0x0104 > idProduct # Multifunction Composite Gadget
echo 0x0100 > bcdDevice # v1.0.0
echo 0x0200 > bcdUSB # USB2
mkdir -p strings/0x409
echo $SERIAL > strings/0x409/serialnumber
echo $MANUFACTURER > strings/0x409/manufacturer
echo $PRODUCT > strings/0x409/product
mkdir -p configs/c.1/strings/0x409
...
FILE=/usbdisk.img
mkdir -p ${FILE/img/d}
mount -o loop -t exfat $FILE ${FILE/img/d}
mkdir -p functions/mass_storage.usb0
echo 1 > functions/mass_storage.usb0/stall
echo 0 > functions/mass_storage.usb0/lun.0/cdrom
echo 0 > functions/mass_storage.usb0/lun.0/ro
echo 0 > functions/mass_storage.usb0/lun.0/nofua
echo $FILE > functions/mass_storage.usb0/lun.0/file
Is there a way to refresh the pi's view of the contents of the file? I've tried umount
ing and mount
ing, but I can't see any added/changed files.
I'm really trying to do 2 things:
- refresh the amount of available space on the USB drive (the contentsof the img file)
- use this as a backdoor file transfer method, even if it might be a bit unreliable.
I've got an E-Ink display on the PI and I'd like it to be a 'smart' usb drive. Periodically I'll refresh the free space and redraw the screen, if it's changed. A manual "refresh" command would be fine.
Also, if I'm connected via the serial port (I can't use the ethernet gadget on my work PC), I could refresh the cache and see files I've updated on there.