I am trying to build a WiFi USB Stick using a Raspberry Pi Zero W. I want to be able to plug in the RPI to a computer or whatever device able to output data to a USB port and then the specific mounted partition of the RPI will be recognized as a normal USB drive. Then, when a file is saved on this "special" USB drive the file gets automatically uploaded to a server.
I started following the tutorial to build the pi zero w into a smart usb flash drive (link) and then try to inverse the process where an external device saves some information onto the RPi.
Until here I managed but the problem is that the files I saved on the RPi don't appear in the RPi's filesystem unless I reboot the RPi. It seems to be coming from the IO cache however, the sync
function is not helping at all.
This is really problematic as I want to use something like watchdog that would trigger some actions once a new file is detected (basically, upload the new file and then delete it from the device).
In my mind, this is very close to what an Eye-Fi WiFi SD Card does but I am not sure anymore the Raspberry Pi Zero W has the hardware necessary to build such a thing.
Thanks a lot for your time,
EDIT 1:
Here are the steps I did in details to try to emulate a USB mass storage where I could then trigger specific actions when new files are save to it:
Use Etcher with a 64Gb SD Card to flash RASPBIAN STRETCH LITE November 2018
Setup the headless RPi by adding on the
boot
mount an emptyssh
file and the needed information for the wifi connection inwpa_supplicant.conf
Plug the RPi with a USB cable to a Windows computer (not the
PWR_IN
port on the RPi)Connect to the RPi using ssh and updating the system:
sudo apt-get update
andsudo apt-get upgrade
.Enable the USB driver: add
dtoverlay=dwc2
to the bottom of/boot/config.txt
anddwc2
the bottom of/etc/modules
(In my casei2c-dev
was not in/etc/modules
as mentioned by the instructions.Reboot the RPi with
sudo reboot
Create a container file with
sudo dd bs=1M if=/dev/zero of=/piusb.bin count=2048
andsudo mkdosfs /piusb.bin -F 32 -I
Mount the container file with
sudo mkdir /mnt/usb_share
, adding/piusb.bin /mnt/usb_share vfat users,umask=000 0 2
at the end of/etc/fstab
and mounting it withsudo mount -a
.Use the mass storage device mode:
sudo modprobe g_mass_storage file=/piusb.bin stall=0 removable=y
. (In comparison to the initial instructions I removedro=1
and addedremovable=y
to allow Windows to write on the USB)At this point I could see the USB Mass Storage detected on Windows but I couldn't write to it (and Windows displays a warning that the USB Mass Storage may have a problem), then I use the Windows dialog to "Fix" the problem.
At this stage I can read and write on the emulated USB mass storage from my Windows machine. The problem is that when I do
ls -lha /mnt/usb_share
I don't see any modification that I made on the Windows computer. Even unmounting withsudo modprobe -r g_mass_storage
and then remounting it withsudo modprobe g_mass_storage file=/piusb.bin stall=0 removable=y
doesn't change it. The only way to see the changes in the folder from the terminal (The RPi side) is to reboot the RPi withsudo reboot
.I also tried to do some
sync
or to flush the cache and the swap usingsync; echo 3 > /proc/sys/vm/drop_caches
andswapoff -a && swapon -a
but it doesn't make a difference.
EDIT 2:
A quick fix I found is to unmount and remount the partition to force it to write it to the disk. Therefore, I do something like this:
sudo modprobe -r g_mass_storage; sudo umount /mnt/usb_share; sudo mount -a; sudo modprobe g_mass_storage file=/piusb.bin stall=0 removable=y
However, I would definitely like something less extreme to "refresh" the filesystem periodically and then be able to monitor the changes on it. I am open to any suggestions 😛
modprobe
stuff? What I would try first is to turn that off and on again, and unmount/remount the drive. I'm sure there is a simpler solution, though.