What is the current recommended way to handle USB serial devices being plugged in?
I am working on porting an open source IoT gateway to balenaOS for the commercial pilot of an IoT hub product. The gateway has an add-ons system which adds support for a wide range of hardware including Zigbee and Z-Wave USB dongles (e.g. the Aeotec Z-Stick Z-Wave dongle or the Digi XStick or Conbee II Zigbee dongle).
On Raspbian the add-ons can automatically detect USB serial devices that are plugged into the device at runtime and use them.
Running inside a Docker container on balenaOS I can statically map a device in docker-compose.yml if I know what path it will be mounted at, e.g.
devices:
- /dev/ttyACM0:/dev/ttyACM0
However, if that device is not plugged in when the Docker image starts up it will simply refuse to start and the (headless) gateway becomes inoperable.
Ideally I would like to be able to dynamically detect USB dongles when they are plugged in using USB hotplug and automatically map them into the Docker container.
If I could create a bind mount to just bind the whole of the host’s /dev directory then I could configure device_cgroup_rules to allow the kernel to access certain classes of device, and perhaps use cap_add to add SYS_RAWIO for low level access (I do not want the container to be running privileged mode since that negates most of the security benefits of using a container), e.g.
volumes:
- '/dev:/dev' # Mount the host's dev directory
device_cgroup_rules:
- 'c 188:* rmw' # Allow USB serial devices (XBee, Conbee III, Sonoff)
- 'c 166:* rmw' # Allow ACM devices (Conbee II)
cap_add:
- SYS_RAWIO
However, since bind mounts are not allowed this is not possible.
I can imagine some elaborate sidecar container that runs in privileged mode and pipes serial interfaces into unix sockets, but that would require re-architecting my whole gateway application and its 100 or so adapter add-ons in order for it to run on balenaOS.
Is there a recommended way to detect USB devices being plugged in via USB hotplug and automatically map them into a container?