Set Pi gadget mode natively in Balena

Is it possible to set gadget mode configurations natively in Balena? For instance, by setting configuration variables or something like that.
Guidances suggest setting configurations manually after the flash (in the “resin-boot” folder) or manually change config.txt and cmdline.txt to enable the gadget mode. The only thing in these approaches is that configurations would be rewritten after the OS update.

Is there is a way to add some “update resistible” configuration for the gadget mode?

Hello,
Thanks for reaching out. To suggest a better solution, can you please help us with some context about your use-case with gadget mode? or what are you trying to achieve with gadget mode on a balena device?

Assuming you are using gadget mode to gain access to the device easily without WiFi or SSH config, balena already manages to solve those problems by easy WiFi configuration when creating a managed device or SSH’ing into them directly for development purposes using local mode. The solutions we have in mind would be hacky at best, which would need you to connect your device to the internet rendering the purpose of gadget mode as null.

Hello!
gadget mode for the device is needed to make it reachable via USB

Hi! @vipulgupta2048
Are there any updates on this topic? Do you know the way to set a gadget mode for the PI in Balena?

And if there is a possibility to update device descriptor (name, serial number) for this device in a gadget mode via Balena?

Hey, sorry for the delay in reaching back.
I couldn’t find any way to explicitly set gadget mode for the Pi using balena.
To enable the gadget mode, we need to add modules-load=dwc2,g_ether but this doesn’t work in balenaOS. The modules_load kernel parameter uses a systemd service to load modules (see systemd-modules-load.service(8) - Linux manual page) and that won’t work.

A workaround could be to add your configuration that is needed for local mode through a container. The dtoverlay parameters can be added using the dashboard + the kernel modules could be loaded inside a container (balenaOS already ships with them). This setup would make your changes persistent through OS updates as well.

Please do let us know on the thread if you managed to find another way instead, thanks!

Hi! Could you please describe this solution in detail? I set the “dtoverlay” parameters in the dashboard, but what about the other part?

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.