devices created with udev rules are shown in the balena container

Hi together,
I have a problem with udev devices inside a single container.
I inserted the following udev rules in config.json

"ACTION==\"add\", DEVPATH==\"*/usb1/1-1/1-1.5/1-1.5.2/*\", SYMLINK+=\"ttyRS485\"\nACTION==\"add\", DEVPATH==\"*/usb1/1-1/1-1.5/1-1.5.3/*\", SYMLINK+=\"ttyConBridge\"\nACTION==\"add\", DEVPATH==\"*/spi0.1/net/pieth*\", NAME=\"pileft\"\n\n"

Alter flashing the OS (2.31.5+rev2, revpi-core-3) on RevPI connect+ the devices are shown as expected:

    crw--w---- 1 root tty       4,  9 Mar 18  2019 /dev/tty9
    crw-rw---- 1 root dialout 204, 64 Jun 24 08:56 /dev/ttyAMA0
    lrwxrwxrwx 1 root root          7 Mar 18  2019 /dev/ttyConBridge -> ttyUSB1
    lrwxrwxrwx 1 root root          7 Mar 18  2019 /dev/ttyRS485 -> ttyUSB0
    crw-rw---- 1 root dialout 188,  0 Mar 18  2019 /dev/ttyUSB0
    crw-rw---- 1 root dialout 188,  1 Mar 18  2019 /dev/ttyUSB1
    crw------- 1 root root      5,  3 Mar 18  2019 /dev/ttyprintk
    root@revPI-1:~#

The next step was to deploy the balena container using the dockerfile. Here the first lines:

FROM balenalib/armv7hf-ubuntu-node:8-bionic-20200221

RUN apt-get update && apt-get -y install sudo

ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get install -y tzdata wget python-rpi.gpio python3-rpi.gpio
#RUN apt-get install -y tzdata wget

RUN ln -fs /usr/share/zoneinfo/Europe/Berlin /etc/localtime
RUN dpkg-reconfigure -f noninteractive tzdata
...

After starting the single container and check the devices inside the container no udev devices could be found:

crw--w---- 1 root tty       4,  9 Jun 24 10:56 /dev/tty9
crw-rw---- 1 root dialout 204, 64 Jun 24 10:56 /dev/ttyAMA0
crw-rw---- 1 root dialout 188,  0 Jun 24 11:32 /dev/ttyUSB0
crw-rw---- 1 root dialout 188,  1 Jun 24 10:56 /dev/ttyUSB1
crw------- 1 root root      5,  3 Jun 24 10:56 /dev/ttyprintk
root@revPI-1:/#

May be someone has an idea how to fix this issue?

Thanks
Peter

Hi

In your dockerfile, can you add ENV UDEV=on
See the relevant documentation here - https://www.balena.io/docs/learn/develop/runtime/#mounting-external-storage-media

Thank you for your answer and the hint :slight_smile:
I inserted the environment for udev in the dockerfile:

[Build]   main Removing intermediate container 21d0405087d9
[Build]   main  ---> 23e2d45038c4
[Build]   main Step 5/34 : ENV UDEV="on"
[Build]   main  ---> Running in c2332635dc5c

The result is exactly the same.

No additional udev devices in the container.

The user should check under /etc/udev/rules.d/. In case the files are missing then we have to troubleshoot further.
Hey @Peter,
As first step could you please check the logs of the os-udevrules service and check if they are having some syntax issues with their config.json.

As next, please run udevadm monitor/trigger to debug the udev rules and check if the rules are being properly applied and not being overridden, etc.
To do so, you can open two terminals connecting to the host OS for further debugging:

  • On one terminal, you can run udevadm monitor which will print out events sent out by udev rules. Keep this running
  • in the second terminal, run udevadmn trigger which will request device events from the kernel

Please keep me posted on your findings.
Georgia

Thank you for your reponse :slight_smile:

Which logs of the os-udevrules do you mean exactly?

The config.json entry for the udev rules were created using the command:
cat rulefilename | jq -sR .

The balena OS contains the following file created from the config.json:

root@revPI-1:~# ls -l /etc/udev/rules.d
total 2
-rw-r--r-- 1 root root 213 Jun 24 07:35 64.rules
-rw-r--r-- 1 root root  87 Jun 24 07:35 README
root@revPI-1:~# cat /etc/udev/rules.d/64.rules

ACTION=="add", DEVPATH=="*/usb1/1-1/1-1.5/1-1.5.2/*", SYMLINK+="ttyRS485"
ACTION=="add", DEVPATH=="*/usb1/1-1/1-1.5/1-1.5.3/*", SYMLINK+="ttyConBridge"
ACTION=="add", DEVPATH=="*/spi0.1/net/pieth*", NAME="pileft"

and the devices are shown as follows:

      crw--w---- 1 root tty       4,  9 Mar 18  2019 /dev/tty9
      crw-rw---- 1 root dialout 204, 64 Jun 24 11:37 /dev/ttyAMA0
      lrwxrwxrwx 1 root root          7 Mar 18  2019 /dev/ttyConBridge -> ttyUSB1
      lrwxrwxrwx 1 root root          7 Mar 18  2019 /dev/ttyRS485 -> ttyUSB0
      crw-rw---- 1 root dialout 188,  0 Mar 18  2019 /dev/ttyUSB0
      crw-rw---- 1 root dialout 188,  1 Mar 18  2019 /dev/ttyUSB1
      crw------- 1 root root      5,  3 Mar 18  2019 /dev/ttyprintk

udevadm monitor during udevadm trigger: udevadm_monitor.log (41.3 KB)

After execution of udevadm trigger the udev devices are not created any more!?

crw--w---- 1 root tty       4,  9 Jun 25 07:19 /dev/tty9
crw-rw---- 1 root dialout 204, 64 Jun 25 07:19 /dev/ttyAMA0
crw-rw---- 1 root dialout 188,  0 Jun 25 07:19 /dev/ttyUSB0
crw-rw---- 1 root dialout 188,  1 Jun 25 07:19 /dev/ttyUSB1
crw------- 1 root root      5,  3 Jun 25 07:19 /dev/ttyprintk

After rebooting the whole device, the corresponding udev’s are back again. But in balena os only and not in the container.

Hello,
If you only need these udev rules to be applied in your container (not the host OS), you should place them in the container, not in config.json.
Something like COPY your_rules.rules /etc/udev/rules.d/your_rules.rules in your Dockerfile.
Please also enable udev in the container with ENV UDEV=on in the Dockerfile.
Please check https://www.balena.io/docs/learn/develop/runtime/#mounting-external-storage-media

Otherwise (if you need to apply the udev rules in the host os), you can try adding a docker-compose.yml file with the needed devices in the devices section of your service, like here https://www.balena.io/docs/learn/develop/multicontainer/#docker-composeyml-file .

Thanks for your responses :slight_smile:
I need the rules in the container only.

I changed the dockerfile and inserted the two lines:

ENV UDEV=on
COPY 64.rules /etc/udev/rules.d/64.rules

[Build]   main  ---> a798915e0910
[Build]   main Step 5/35 : ENV UDEV="on"
[Build]   main  ---> Running in c0a0b3614fe4
 ...
[Build]   main  ---> c079d5e04840
[Build]   main Step 21/35 : COPY ["64.rules","/etc/udev/rules.d/64.rules"]
[Build]   main  ---> 71d2a367ba2e

The file is available in the corresponding directory inside the container:

root@revPI-1:/# cat /etc/udev/rules.d/64.rules
ACTION=="add", DEVPATH=="*/usb1/1-1/1-1.5/1-1.5.2/*", SYMLINK+="ttyRS485"
ACTION=="add", DEVPATH=="*/usb1/1-1/1-1.5/1-1.5.3/*", SYMLINK+="ttyConBridge"
ACTION=="add", DEVPATH=="*/spi0.1/net/pieth*", NAME="pileft"
root@revPI-1:/#

I removed the udev rules from config.json.
So no udev devices are shown in the balena OS any more.

The result is, that no udev devices are shown in the container:

crw--w---- 1 root tty       4,  9 Jun 25 13:25 /dev/tty9
crw-rw---- 1 root dialout 204, 64 Jun 25 13:25 /dev/ttyAMA0
crw-rw---- 1 root dialout 188,  0 Jun 25 13:30 /dev/ttyUSB0
crw-rw---- 1 root dialout 188,  1 Jun 25 13:25 /dev/ttyUSB1
crw------- 1 root root      5,  3 Jun 25 13:25 /dev/ttyprintk

Do you see the devices being added in the container with udevadm monitor ?

Here the result from udevadm monitor during the execution udevadm trigger using another window:

udevadm_monitor_container.log (20.7 KB)

I see nothing of my udev rules.

The kernel events seem to be “change” events, can you try modifying the ACTION field of your udev rules ?

Or better, can you try unplugging and re-plugging the devices while udevadm monitor runs ?