I followed this wonderful guide:


and I was able to made it working on my loved Pi4B.

Everything works fine as mass storage emulation and my device at the other end of the usb cable, fully recognize and works with PI4... BUT, if, for some reason, I connect to PI4 via SSH to do something (check temp, view some log or other simple action) my device looses connection and starts blinking such as the other side of the cable (PI4B) raised some issue.

Even if I do not touch anything... it works good for a couple of days of uptime, without touching nothing, but the third day my device looses connection and starts blinking such as the other side of the cable (PI4B) raised some issue.

I tried to perform:

modprobe -r g_mass_storage (and I see, power-off of light blinking, that's correct, cause it's like I remove a usb-key)

and then

modprobe g_mass_storage file=/piusb.bin stall=0 removable=y

At this point, I see, power-on of light blinking, correct, but no way to finalize recognizing, like inserting a usb-key again, it never stops to blink.

I literally tried "a tont" of scripts, but I did not solve.

I tried lsusb, usbhid, a lot of scripts to "reset" usb ports (but many are developed to reset attached usb device and not port or hub/controller that I believe is my case, given I use PI4B to emulate mass storage via "g_mass_storage gadget.0: g_mass_storage ready" ), I tried "watch -n 1 'ls -l /proc/bus/input/devices'" with no results, as well as "udevadm monitor" with no result, taking down the usb-service and bring it up again, restart etc without results.

The only way I have to "solve" and "reset" the situation, is : shutdown -r now. A reboot solves, but I would want to understand how to solve without a reboot every time.... cause also it's not so good... you know this is "Windows solving style" ...

Any idea, help, suggestion ? May I have to check another log file to troubleshoot better ? If yes, which ? May I have to increase debugging verbose level of such log file ?

By the way, with a simple reboot everything comes back OK, so must be something that succeded at reboot/boot/startup that is slightly different from what a normal reset, but must be also software based.

Thank you very much in advance!



Here I am with full integration, after reading your comment @Milliways (many thanks for your comment). The following, is exactly what I did.

So, now, step-by-step:

Pi4B out-of-the-box

installed 2023-12-05-raspios-bookworm-arm64-full

enabled SSH

disabled wifi and BT sudo nano /boot/firmware/config.txt Add dtoverlay=disable-wifi under [all] to disable Wi-Fi. Add dtoverlay=disable-bt under [all] to disable Bluetooth. checked > OK

disabled Desktop GUI (I don't want it and don't need it) sudo raspi-config

System Options
Boot/Auto Login B1 Console Text console, requiring user to login OK Exit and confirming to REBOOT Enabled the USB driver

a) sudo nano /boot/config.txt Scroll to the bottom and append the line below: dtoverlay=dwc2 Press CTRL+O followed by Enter to save, and then CTRL+X to quit.

b) sudo nano /etc/modules Append the line below, just after the i2c-dev line: dwc2 Press CTRL+O followed by Enter to save, and then CTRL+X to quit. created a large file to act as the storage medium on the SD card sudo dd bs=1M if=/dev/zero of=/piusb.bin count=87040

87040+0 records in 87040+0 records out 91268055040 bytes (91 GB, 85 GiB) copied, 2629.26 s, 34.7 MB/s

NOTE: of course, Guide reported an example; I needed and created a different, bigger container file checked > OK formatted the container file as a FAT32 file system

sudo mkdosfs /piusb.bin -F 32 -I mkfs.fat 4.2 (2021-01-31)

checked > OK Mounted the container file

First, created a folder on which we can mount the file system:

sudo mkdir /mnt/usb_share

Added this to fstab, the configuration fi le that records our available disk partitions:

sudo nano /etc/fstab

Appended the line below to the end of the file:

/piusb.bin /mnt/usb_share vfat users,umask=000 0 2

NOTE: sudo mount -a >> differently from the guide, returned to me a message inviting to use instead "sudo systemctl daemon-reload" >> DID IT >> good, checked >> OK Downloaded a test file

cd /mnt/usb_share

sudo wget http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4 --2024-01-21 19:24:38-- http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4 Resolving commondatastorage.googleapis.com (commondatastorage.googleapis.com)...,,, ... Connecting to commondatastorage.googleapis.com (commondatastorage.googleapis.com)||:80... connected. HTTP request sent, awaiting response... 200 OK Length: 158008374 (151M) [video/mp4] Saving to: ‘BigBuckBunny.mp4’

BigBuckBunny.mp4 100%[===================>] 150.69M 11.4MB/s in 14s

2024-01-21 19:24:51 (11.1 MB/s) - ‘BigBuckBunny.mp4’ saved [158008374/158008374]

checked > OK flushed any cached data to the disk


done > OK Tested mass storage device mode

sudo modprobe g_mass_storage file=/piusb.bin stall=0 removable=y

checked > OK

NOTE: in the guide you will see "sudo modprobe g_mass_storage file=/piusb.bin stall=0 ro=1", that will worked too, but, given I need a read/write and not a read-only device, I changed it to "removable=y"

sudo modprobe -r g_mass_storage

checked > OK

LOG: [Jan22 17:58] Mass Storage Function, version: 2009/09/11 [ +0.000018] LUN: removable file: (no medium) [ +0.000058] LUN: removable file: /piusb.bin [ +0.000005] Number of LUNs=1 [ +0.000153] g_mass_storage gadget.0: Mass Storage Gadget, version: 2009/09/11 [ +0.000008] g_mass_storage gadget.0: userspace failed to provide iSerialNumber [ +0.000004] g_mass_storage gadget.0: g_mass_storage ready [ +0.000008] dwc2 fe980000.usb: bound driver g_mass_storage enabled Samba

sudo apt-get update sudo apt-get install samba winbind -y

sudo nano /etc/samba/smb.conf

Scrolled down to the end of the file and append the lines below:

[usb] browseable = yes path = /mnt/usb_share guest ok = yes read only = no create mask = 777

saved and exit

then: sudo systemctl restart smbd.service

then: cat /etc/hostname

inserted my private hostname

checked access by Windows10 >> perfect OK

I see two shared folders:

nobody (never touched, I don't know what it is)

usb (the good one: it contains all my files and work like a charm)

Automated USB device reconnect

sudo pip3 install watchdog >> NOTE1: unfortunately, as for Raspberry original documentation (I discovered after my setup) this command doesn't work, but I did not know and I solved, googling around with this one:

sudo pip install watchdog --break-system-packages

checked pip package installation and working > OK

As for the guide: "We then need some code to start a timer whenever something changes in the shared folder. The timer is reset to zero every time a new change occurs, and the USB reconnect is only triggered if we see 30 seconds of inactivity after a change."

We’ve written a program to do this. To download it, type:

cd /usr/local/share sudo wget http://rpf.io/usbzw -O usb_share.py sudo chmod +x usb_share.py

done > OK


for the reason I written before, I had to edit this usb_share.py script, this way:

sudo nano usb_share.py

changed from "modprobe g_mass_storage file=/piusb.bin stall=0 ro=1" to "modprobe g_mass_storage file=/piusb.bin stall=0 removable=y"

checked reboot > checked loading script, changing files, view update of the folder >> all working with no issue NEXT STEP, mine specific step (the reason I bought Pi4B is to have a "customizable via script" usb mass storage):

created a test_rename_script in python in order to test renaming of 3 file inside the root my usb mass storage container file, one time per day, in the middle of the night

done, checked > OK

created a rename_all_script in python in order to renaming file every file inside the root my usb mass storage container file, one time per day, in the middle of the night

done, checked > OK

mkdir myscripts

put these scripts here

setting up Cron and its log

installed rsyslog

sudo apt install rsyslog

sudo nano /etc/rsyslog.conf cron.* /var/log/cron.log

checked Cron execution and log > OK coming back to the Guide: Background service We need to make this program into a background service, so that it starts automatically at boot time. We can do that by making it into a systemd unit. Enter the commands below:

cd /etc/systemd/system sudo nano usbshare.service

This will start a new blank file. In the new file, enter:

[Unit] Description=USB Share Watchdog

[Service] Type=simple ExecStart=/usr/local/share/usb_share.py Restart=always

[Install] WantedBy=multi-user.target

Press CTRL+O followed by Enter to save, and then CTRL+X to quit. Now we need to register the service, enable it, and set it running:

sudo systemctl daemon-reload sudo systemctl enable usbshare.service sudo systemctl start usbshare.service

Whenever you copy files over to the network share, the USB device should automatically reconnect after 30 seconds of inactivity.

done, checked > OK That'all, awaiting your kind feedback and support, thanks!

  • Thank you @goldilocks admin. To be honest I read the policies before writing but, indeed, you are right, I misunderstood the meaning, so now I proceed to edit my first post, with the details suggested by Milliways. Hoping this is ok now, Best
    – flic47921
    Commented Feb 5 at 14:27


Your Answer

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

Browse other questions tagged or ask your own question.