Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Is it possible to detect usb devices in docker/balena container ? #123

Closed
adamvx opened this issue Nov 4, 2020 · 6 comments
Closed

Is it possible to detect usb devices in docker/balena container ? #123

adamvx opened this issue Nov 4, 2020 · 6 comments

Comments

@adamvx
Copy link

adamvx commented Nov 4, 2020

Hi,

I tried to implement this library for my project running on balena but I was not able to make it run. It works on host but not in container even when its started with privileged flag.

I can provide an example to try it out.

First run the test container.

docker run -it --name test --privileged balenalib/raspberrypi4-64-debian-node:11-buster-build bash

Then create and new folder with mkdir app and inside it create new project with npm init -y

Install required package with install_packages libudev-dev and install this library npm i usb-detection. This will take some time because on this specific image are no pre-builded binaries

Then create index.js touch index.js and paste there this snippet.

var usbDetect = require('usb-detection');
usbDetect.startMonitoring();

console.log('Listening...');

usbDetect.on('change', function(device) { console.log('change', device); });

Finally run project with node index.js and try to connect or disconnect any usb device. I was not able to detect any device even if its present in /dev directory inside the container.

Does anyone has been solving similar issue or is there some guide that I am not aware of ?

Thanks for the help.

@MadLittleMods
Copy link
Owner

@adamvx Are you passing the USB devices to the container?

We won't be able to detect anything if you're just plugging and unplugging on the host without passing the USB device through.

@adamvx
Copy link
Author

adamvx commented Nov 4, 2020

@MadLittleMods Of course, all host devices are passed to container through --privileged flag. As I said, I see that device in /dev directory of the container as I see it in host /dev directory when its connected. But no event is fired.

Did you somehow managed to make it work ?

@MadLittleMods
Copy link
Owner

MadLittleMods commented Nov 4, 2020

@adamvx The way I test this library with linux is via a VirtualBox Ubuntu VM. The UI passthrough there is a bit clunky but the test suite does pass if I toggle the USB device through and off in the menus.

@ggbalaazs
Copy link

@Adamx You have to expose udev sockets to the container, mounting /run/udev should help you out.

@iKK001
Copy link

iKK001 commented Dec 26, 2022

No idea what you mean by mounting /run/udev ?? Can you please clarify ??

I have the exact same problem when trying to use the library with an generic-amd64 standard BalenaOS Docker image i.e. It works on host but not in container even when its started with privileged flag...

Here is the steps I take:

I make sure both are installed in my Docker.template file:

RUN sudo apt-get install -y build-essential
RUN sudo apt-get install -y libudev-dev

I also set the following in my Docker.template file:

ENV UDEV=1

And I make sure, the priviledge is set to true in my docker-compose.yml file:

privileged: true

But USB-detection is not working. It only works when running the code locally on my Mac. But it does not work inside the docker container.

Inside my nodejs-code, I have:

import usbDetect from "usb-detection"
usbDetect.startMonitoring()
usbDetect.on("add", () => {
    // ...
}

What exactly is still wrong ?

@iKK001
Copy link

iKK001 commented Dec 26, 2022

I found a temporary solution:

If you set the following inside your Balena docker-compose.yml file, you can detect the USB device plugging:

Set network_mode: host

I also posted this in the Balena Forum as you can see here:
https://forums.balena.io/t/usb-re-connect-fails-from-container/363284/3?u=skuenstler

Still, I am not quite happy with this solution since my multicontainer services are not allowed to constantly run on the same namespace as the HOST. Therefore network_mode: host is not an option !

What if the network_mode is bridge ?? Is there any way that this library works in a multicontainer Balena application ?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants