Systemd unit for managing USB gadgetsFri 19 October 2018 by Lars Kellogg-Stedman Tags raspberrypi usb
The Pi Zero (and Zero W) have support for acting as a USB gadget: that means that they can be configured to act as a USB device -- like a serial port, an ethernet interface, a mass storage device, etc.
There are two different ways of configuring this support. The first only allows you to configure a single type of gadget at a time, and boils down to:
- Enable the dwc2 overlay in
This process is more fully documented here.
The second mechanism makes use of the
libcomposite driver to create
multifunction gadgets. The manual procedure is documented in the
kernel documentation. While it's a useful feature, the
configuration process requires several steps and if you only do it
infrequently it can be easy to forget.
In order to make this easier for me to manage, I've wrapped the process up in a systemd template unit that takes care of the various steps necessary to both create and remove a multifunction USB gadget.
Once installed, creating a gadget that offers both a serial interface and a network interface is as simple as:
Create a file
systemctl start usb-gadget@g0.
You can remove the gadget by running
systemctl stop usb-gadget@g0.
As with any systemd service, you can mark the unit to start
automatically when your system boots by running
The systemd-usb-gadget project can be found at: