USB On-The-Go (OTG) support
Enable OTG-capable USB ports to toggle between Host and Device mode
Certain models of Raspberry Pi are fitted with a special type of USB port called USB On-The-Go (OTG). This means it can switch between host mode, e.g. allowing you to plug in and use keyboards, mice or USB storage, and device mode, e.g. your Pi can be plugged into another computer and behave like a network device, a USB storage device, an audio device, etc.
The Raspberry Pis that currently support this feature are the Pi Zero and the Pi 4. Below is a set of scripts to modify the Pi and make it behave like a USB storage device when plugged into another computer.
All of the following commands are designed to be run using REMOTE SHELL

Step 0: Install the otg-usb script

otg-usb.sh
3KB
Binary
Support script
  • Download the attached file
  • Use SEND FILES to transfer the file to /usr/local/bin
    • Before clicking [Start Upload], rename the file to otg-usb
  • Use REMOTE SHELL to make the file executable
    • Run chmod +x /usr/local/bin/otg-usb

Step 1: Enable OTG support

1
otg-usb enable
2
reboot
Copied!

Step 2: Create a new "storage device"

1
otg-usb create /test_usb
2
otg-usb ls /test_usb
Copied!

Step 3: Load files from the local system onto the "storage device"

BEFORE YOU RUN THE COMMAND: replace /path/to/file.zip with a path to an existing file.
1
otg-usb cp /path/to/file.zip /test_usb
2
otg-usb ls /test_usb
Copied!

Step 4: Activate the "storage device" and plug the Pi into another computer

This is where we tell the Linux kernel what type of "gadget" we want the OTG port to behave like. In this case, we want it to look like a USB stick, a.k.a mass storage, so we select g_mass_storage. The file argument tells the kernel to use the file system we created in step 2 as the actual storage.
1
otg-usb start /test_usb
Copied!
Now plug the OTG port into another computer and it should appear as if you just plugged in a USB stick! You should see the file that you added in Step 3. You can read and write to files and directories, just like a regular USB stick.
When you are finished, unplug the USB cable and run the following command to turn off the mass storage driver
1
otg-usb stop
Copied!

Notes

  • You don't need to disconnect the USB cable, it can be left permanently connected. Starting and stopping the driver is enough to cause the "usb stick" to connect and disconnect from the computer.
  • If you want to add more files from the local file system to the "storage device", you must first stop the driver before using the cp or mount commands.
  • Be aware that stopping the driver while it is being written to will likely cause a corruption and should be avoided.
Last modified 6mo ago