Set Pi gadget mode natively in Balena

Sure thing. To enable gadget mode on a Raspberrypi device:

  1. Define the Dtoverlay parameter dtoverlay=dwc2 for your device through the configuration tab in the balenaCloud dashboard.

  2. To load kernel modules needed for gadget mode and make sure your changes are persistent through OS updates try running:

A privileged container that executes modprobe -a dwc2 g_serial --first-time command on device startup to load the kernel modules needed for gadget mode. Next, set therestart: 'no' rule for this service in the docker-compose.yml file like this:

version: '2.1'

services:
  gadget-mode-enable:
    privileged: true
    restart: "no"
    build: 
      context: "./path/to/Dockerfile"

This will allow the service to just run the command then stop the container. This would also remove the need for a privileged container running on your device, and is a more secure way of going about this process.

Hopefully, that helps. Please do give this a dry run before running on a production device and let us know if this works for your use-case.