Creating a USB Composite Multi-Function Gadget on Raspberry Pi using ConfigFS¶
Learn how to configure your Raspberry Pi as a multi-functional USB device! Using Linux ConfigFS, you can share a storage volume, emulate an ethernet connection, and provide a serial console simultaneously over a single USB cable.
Introduction¶
Legacy Linux USB gadget modules (like g_ether or g_mass_storage) only allow you to emulate one device type at a time. If you want your Raspberry Pi to act as both a network interface and a USB drive, you must use ConfigFS (specifically, the libcomposite driver).
ConfigFS allows you to build a USB Composite Device. When plugged into a computer, it shows up as multiple independent devices connected through a virtual USB hub.
Requirements & Setup¶
Make sure your Raspberry Pi model supports USB OTG (Pi Zero, Zero W, Zero 2 W, or Pi 4) and that you are using a USB data cable.
1. Enable USB Drivers¶
Edit the boot config file:
Next, add the kernel composite modules to load at boot:
libcomposite here):
Save and exit.
2. Create the ConfigFS Gadget Script¶
The configuration of a composite device is handled entirely by creating directory structures in /sys/kernel/config/usb_gadget/.
We will write a shell script to set up a composite gadget that provides USB Ethernet (RNDIS/CDC) and a Virtual USB Storage Drive.
- Create the configuration script:
- Paste the following script:
- Make the script executable:
3. Enable Service at Boot¶
Run the script automatically when the system starts by wrapping it in a systemd service.
- Create the service:
- Paste the following configuration:
- Enable the service:
- Reboot the Raspberry Pi:
4. Connecting and Configuring the Host¶
Once rebooted, connect the Pi to your computer:
- As a USB Storage Drive: Your host PC will instantly mount a new 256MB flash drive.
- As an Ethernet Card: The host PC will detect a new virtual network adapter.
- To access your Pi over the network, you must configure a static IP on the Pi's
usb0interface.
- To access your Pi over the network, you must configure a static IP on the Pi's
To assign a static IP to the virtual network adapter on the Pi, edit /etc/dhcpcd.conf (or use Netplan / NetworkManager depending on your OS version) and assign 192.168.7.1 to the usb0 interface. Then, you can access the Pi via SSH at username@192.168.7.1.
Troubleshooting¶
- Host does not recognize the Ethernet adapter (Windows):
- Windows occasionally struggles to recognize ConfigFS virtual ethernet adapters by default. You may need to manually open the Device Manager, find the unknown network device, and update its driver choosing the "Remote NDIS (RNDIS) Compatible Device" driver from the Windows system driver list.
- File System Conflicts:
- Warning: Do not mount
/shared_storage.binon both the host computer and the Raspberry Pi simultaneously in write-mode. FAT32 does not support concurrent write access, and doing so will corrupt the filesystem. If you need the Pi to read files written by the host, unmount the drive on the host PC first, then mount it on the Pi.
- Warning: Do not mount