# UDEV events not showing up in service

**URL:** <https://forums.balena.io/t/udev-events-not-showing-up-in-service/250035>\
**Category:** Product support\
**Created:** [February 5, 2021, 9:18am UTC](https://forums.balena.io/t/udev-events-not-showing-up-in-service/250035 "2021-02-05T09:18:11Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![ben.clerix](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@ben.clerix](https://forums.balena.io/u/ben.clerix)\
**Post date:** [February 5, 2021, 9:18am UTC](https://forums.balena.io/t/udev-events-not-showing-up-in-service/250035/1 "2021-02-05T09:18:11Z")

</div>

Hi,

I’m trying to get UDEV events to reach my container running on a BalenaFin.

I’m testing with a simple USB thumb drive and an an ATR210, manufactured by AccessIS: [ATR210 - TripTick® Barcode & NFC Reader](https://www.access-is.com/products/atr210-triptick)

I made a minimal working example indicated below. I think I followed all recommended practiced as documented:

- [Communicate outside the container - Balena Documentation](https://www.balena.io/docs/learn/develop/runtime/#mounting-external-storage-media)
- [Balena base images - Balena Documentation](https://www.balena.io/docs/reference/base-images/base-images/)

docker-compose file

```
version: '2.1'

services:

   someservice:
    build:
      context: ./
      dockerfile: Dockerfile
    privileged: true
    environment:
      - UDEV=on

```

Dockerfile

```
FROM balenalib/generic-armv7ahf-dotnet:3.1-buster-20210201
WORKDIR /app
CMD sleep infinity

```

Results:  
I ran `udevadm monitor`both on the host and on the service. Then I first removed and then inserted the device.  
[udevadm\_monitor\_container.log](https://forums.balena.io/uploads/short-url/wUuJ8u1YZfAO1Ie73KGyAiRXqYD.log) (9.5 KB) [udevadm\_monitor\_host.log](https://forums.balena.io/uploads/short-url/n1ZtQdVR4QMydVZa3Un2YcpHY1s.log) (18.9 KB)

As you can see, I only see the UDEV events on the host and not on the service.

I noticed that the device does work perfectly fine in the service when I have it inserted and I restart the service. It’s just removing and inserting whilst the service is running that doesn’t work and I think it has to do with the UDEV events not showing up.

Other things I tried:  
I can see that the udev service is running so all should be ok?

```auto
root@8de138db034b:/app# service udev status
[ok] systemd-udevd is running.

```

This seems similar to this topic: [Balenadash keyboard not responding](https://forums.balena.io/t/balenadash-keyboard-not-responding/142449)

If I can provide more info, please let me know.  
Thanks for your time!

---

<div class="post-metadata">

**Author:** ![karaxuna](https://avatars.discourse-cdn.com/v4/letter/k/e480ec/32.png) [@karaxuna](https://forums.balena.io/u/karaxuna)\
**Post date:** [February 5, 2021, 11:40am UTC](https://forums.balena.io/t/udev-events-not-showing-up-in-service/250035/3 "2021-02-05T11:40:48Z")

</div>

Hi, can you try the [sample project](https://github.com/balena-io-examples/balena-storage) and see if it works for your device and usb drive? If it works, we can then continue searching for the issue in your project configuration.

---

<div class="post-metadata">

**Author:** ![ben.clerix](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@ben.clerix](https://forums.balena.io/u/ben.clerix)\
**Post date:** [February 5, 2021, 11:56am UTC](https://forums.balena.io/t/udev-events-not-showing-up-in-service/250035/4 "2021-02-05T11:56:33Z")

</div>

I tested it on device “[balena dashboard](https://dashboard.balena-cloud.com/devices/5bcf7b572b16ca67f7339f9d04ab26fd/summary)”.  
I granted support access for a week on this device.

I see the following in the Logs in BalenaCloud:

> 05.02.21 12:48:50 (+0100) external-drive Idling…

Once again with `udevadm monitor` I see UDEV entries on the host OS, but not in the “external-drive” service.

---

<div class="post-metadata">

**Author:** ![karaxuna](https://avatars.discourse-cdn.com/v4/letter/k/e480ec/32.png) [@karaxuna](https://forums.balena.io/u/karaxuna)\
**Post date:** [February 5, 2021, 1:42pm UTC](https://forums.balena.io/t/udev-events-not-showing-up-in-service/250035/11 "2021-02-05T13:42:32Z")

</div>

Hi, can you try reformatting usb drive to FAT32 or EXT4? I see error in logs (`/usr/src/mount.log`):

```auto
Could not get device information: /dev/sda1                                                                                                                                  
Mounting - Source: /dev/sda1 - Destination: /mnt/storage-usb-UBUNTU\x2020_0-E4EB-429C                                                                                        
Mounting - Source: /dev/sda1 - Destination: /mnt/storage-usb-UBUNTU\x2020_0-E4EB-429C                                                                                        
File system not supported: ntfs                                                                                                                                              
Mounting - Source: /dev/sda1 - Destination: /mnt/storage-usb-UBUNTU\x2020_0-E4EB-429C                                                                                        
Mounting - Source: /dev/sda1 - Destination: /mnt/storage-usb-UBUNTU\x2020_0-E4EB-429C                                                                                        
Could not get device information: /dev/sda1

```

---

<div class="post-metadata">

**Author:** ![ben.clerix](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@ben.clerix](https://forums.balena.io/u/ben.clerix)\
**Post date:** [February 5, 2021, 2:32pm UTC](https://forums.balena.io/t/udev-events-not-showing-up-in-service/250035/13 "2021-02-05T14:32:14Z")

</div>

Yes, I overlooked that. Sorry about that. Plug and play seems to work or at least the udev rules in /etc/udev/rules.d/usb.rules are being called.

I don’t think we need to waste time with the USB thumbdrive, I don’t need that to work in the end.  
Instead I would like to focus on what I actually want to achieve:  
I tried to use the PCSC library to access a smart card reader (ATR210).  
I noticed it only worked when I started the service when the smart card reader is already connected to my BalenaFin.  
To troubleshoot the issue I created an issue with the library github: [ATR210 LIBUSB\_ERROR\_NO\_DEVICE after disconnecting · Issue #95 · LudovicRousseau/PCSC · GitHub](https://github.com/LudovicRousseau/PCSC/issues/95)

The developer mentioned that “The USB device removal is NOT notified by libudev”.  
Based on his advise I ran `udevadm monitor` on both the host and the service.  
He then indicated that the difference in the reported events is the reason that the library was failing.  
I checked if udev is actually running and it is:

```auto
root@8de138db034b:/app# service udev status
[ok] systemd-udevd is running.

```

It’s just the events that are not generated inside the container.

PCSC is using libudev events in some way shape or form, but I didn’t write the code, I only know that the creator mentions that they are needed.

---

<div class="post-metadata">

**Author:** ![ben.clerix](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@ben.clerix](https://forums.balena.io/u/ben.clerix)\
**Post date:** [February 5, 2021, 3:05pm UTC](https://forums.balena.io/t/udev-events-not-showing-up-in-service/250035/14 "2021-02-05T15:05:21Z")

</div>

Ok, after some testing it appears that the UDEV events actually do happen, but they’re just not printed?

I modified the [balena-storage sample](https://github.com/balena-io-examples/balena-storage) in the following ways:  
Added an extra script to the scripts folder (accessis.sh):

```auto
#!/bin/bash
echo "detected accessIS device, arguments are: $@" >> /usr/src/accessis.log

```

and add this rule to `udev/usb.rules`  
`ACTION=="add", ENV{ID_VENDOR}=="Access_IS", RUN+="/bin/sh -c '/usr/src/scripts/accessis.sh'"`

After I plug in my device I do see the following:

```auto
bash-5.1# cat /usr/src/accessis.log 
detected accessIS device, arguments are: 
detected accessIS device, arguments are: 
detected accessIS device, arguments are: 
detected accessIS device, arguments are:

```

So this seems to work?  
I just don’t know what the PCSC library is doing that is failing…

---

<div class="post-metadata">

**Author:** ![ben.clerix](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@ben.clerix](https://forums.balena.io/u/ben.clerix)\
**Post date:** [February 5, 2021, 7:08pm UTC](https://forums.balena.io/t/udev-events-not-showing-up-in-service/250035/15 "2021-02-05T19:08:15Z")

</div>

I tested a sample program that uses libudev from [Detecting USB devices with libudev](https://gavv.github.io/articles/libudev-usb/). This is the library the PCSC uses to detect usb events.  
The source file is here: [https://raw.githubusercontent.com/gavv/snippets/master/udev/udev\_monitor\_usb.c](https://raw.githubusercontent.com/gavv/snippets/master/udev/udev_monitor_usb.c).  
I built it using: `gcc -g -Wall -o udev_monitor_usb udev_monitor_usb.c -ludev` on ubuntu20.04 on a spare laptop.  
In Ubuuntu20.04 I do have the UDEV and KERNEL events in the `udevadm monitor`. this is the output of the program when I insert and remove in my usb thumbdrive:

```auto
ubuntu@ubuntu:~$ ./udev_monitor_usb 
usb usb_device exists 1d6b:0002 /dev/bus/usb/003/001
usb usb_device exists 058f:6387 /dev/bus/usb/003/002
usb usb_device exists 8087:07da /dev/bus/usb/003/003
usb usb_device exists 5986:0400 /dev/bus/usb/003/004
usb usb_device exists 1d6b:0003 /dev/bus/usb/004/001
usb usb_device exists 1d6b:0002 /dev/bus/usb/001/001
usb usb_device exists 8087:8008 /dev/bus/usb/001/002
usb usb_device exists 1d6b:0002 /dev/bus/usb/002/001
usb usb_device exists 8087:8000 /dev/bus/usb/002/002
usb usb_device add 058f:6387 /dev/bus/usb/003/009
usb usb_device bind 058f:6387 /dev/bus/usb/003/009
usb usb_device unbind 0000:0000 /dev/bus/usb/003/009
usb usb_device remove 0000:0000 /dev/bus/usb/003/009

```

When I compile and run this in the “external-drive” service on my balenaFin where I only have the KERNEL events in the `udevadm monitor` when I insert and remove in my usb thumbdrive:

```auto
bash-5.1# ./udev_monitor_usb 
usb usb_device exists 1d6b:0002 /dev/bus/usb/001/001
usb usb_device exists 0424:9514 /dev/bus/usb/001/002
usb usb_device exists 0424:ec00 /dev/bus/usb/001/003
usb usb_device exists 0424:2422 /dev/bus/usb/001/008
usb usb_device exists 0db5:0170 /dev/bus/usb/001/009
usb usb_device exists 0db5:0140 /dev/bus/usb/001/010

```

You can clearly see the absence of the add, bind, unbind and remove commands.

---

<div class="post-metadata">

**Author:** ![dtischler](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/dtischler/32/2483_2.png) [@dtischler](https://forums.balena.io/u/dtischler)\
**Post date:** [February 5, 2021, 8:12pm UTC](https://forums.balena.io/t/udev-events-not-showing-up-in-service/250035/17 "2021-02-05T20:12:00Z")

</div>

Looking at the logs, is `058f:6387` the USB stick then?

```auto
usb usb_device add 058f:6387 /dev/bus/usb/003/009
usb usb_device bind 058f:6387 /dev/bus/usb/003/009

```

Shouldn’t we at least see that the “usb\_device exists” in the second snippet (container) that you’ve provided? Are you certain that the drive is still properly being recognized / mounted in that container (I realize we are trying to get to the bottom of udev logging, but, is the absence of a log in this case due to the fact it’s not even recognized at all?)

---

<div class="post-metadata">

**Author:** ![ben.clerix](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@ben.clerix](https://forums.balena.io/u/ben.clerix)\
**Post date:** [February 5, 2021, 9:14pm UTC](https://forums.balena.io/t/udev-events-not-showing-up-in-service/250035/18 "2021-02-05T21:14:25Z")

</div>

Completely correct. in the previous test I started the program when the usb thumbstick was not inserted yet.  
I now did another test with the usb thumbstick already inserted when the program started. Now the usb device is visible:

```
bash-5.1# ./udev_monitor_usb
usb usb_device exists 1d6b:0002 /dev/bus/usb/001/001
usb usb_device exists 0424:9514 /dev/bus/usb/001/002
usb usb_device exists 0424:ec00 /dev/bus/usb/001/003
usb usb_device exists 058f:6387 /dev/bus/usb/001/012

```

I also tried to remove and insert the usb thumbdrive again, but no events showed up.  
I’ll leave the usb thumbstick inserted if you remotely want to test something.

Edit:  
Maybe these links help?

- [Docker not passing UDEV events to container even after using --net=host - #2 by sebt3 - General Discussions - Docker Forums](https://forums.docker.com/t/docker-not-passing-udev-events-to-container-even-after-using-net-host/92394/2)
- [udev - udevadm does not show all attributes inside a docker container - Stack Overflow](https://stackoverflow.com/questions/41753218/udevadm-does-not-show-all-attributes-inside-a-docker-container)

---

<div class="post-metadata">

**Author:** ![mtoman](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mtoman/32/3599_2.png) [@mtoman](https://forums.balena.io/u/mtoman)\
**Post date:** [February 8, 2021, 1:31pm UTC](https://forums.balena.io/t/udev-events-not-showing-up-in-service/250035/21 "2021-02-08T13:31:08Z")

</div>

Hi,

I have been digging into this and I can confirm this does not indeed work as expected. Considering the `balena-storage`, exactly as you describe, `UDEV` events are dropped even though udev is apparently running:

```auto
bash-5.1# ps 
PID USER TIME COMMAND
    1 root 0:00 /bin/bash /usr/src/scripts/idle.sh
   30 root 0:00 udevd --daemon
   38 root 0:00 sleep 600
   50 root 0:00 /bin/bash
  204 root 0:00 ps
bash-5.1# udevadm monitor
...
KERNEL...
KERNEL...
KERNEL...

```

After killing udev and restarting it manually it starts behaving correctly:

```auto
bash-5.1# kill -15 30
bash-5.1# udevd --daemon
bash-5.1# udevadm monitor
...
KERNEL...
KERNEL...
KERNEL...
UDEV...
UDEV...
UDEV...

```

I am not yet sure about the cause for this behavior but will be digging deeper. In the meantime would you be able to try killing/manually starting udev in your container and confirm that it does in fact help with the original issue that you are trying to solve? Thanks.

---

<div class="post-metadata">

**Author:** ![ben.clerix](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@ben.clerix](https://forums.balena.io/u/ben.clerix)\
**Post date:** [February 8, 2021, 4:09pm UTC](https://forums.balena.io/t/udev-events-not-showing-up-in-service/250035/23 "2021-02-08T16:09:25Z")

</div>

This seems to work for us, we see the UDEV events in `udevadm monitor` and in code.  
Thanks a lot for the help!

I suppose once this gets fixed this thread will be updated so we can remove the workaround from our dockerfile?

---

<div class="post-metadata">

**Author:** ![mtoman](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mtoman/32/3599_2.png) [@mtoman](https://forums.balena.io/u/mtoman)\
**Post date:** [February 8, 2021, 7:57pm UTC](https://forums.balena.io/t/udev-events-not-showing-up-in-service/250035/25 "2021-02-08T19:57:53Z")

</div>

Hi again, glad to hear that helps. We will look for the issue and keep you updated in this thread.

---

<div class="post-metadata">

**Author:** ![mtoman](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mtoman/32/3599_2.png) [@mtoman](https://forums.balena.io/u/mtoman)\
**Post date:** [February 15, 2021, 8:04pm UTC](https://forums.balena.io/t/udev-events-not-showing-up-in-service/250035/37 "2021-02-15T20:04:56Z")

</div>

Hi again,

I have got an update on this: The reason why udev events are not propagated is that we run it in a separate net namespace. We do this because certain network devices become unstable or hang when double-initialized which happens when udev is running in the container - both the on-host and in-container udev instances may try to initialize the device. This is a safe default we chose and there is a note about it in the docs: [Balena base images - Balena Documentation](https://www.balena.io/docs/reference/base-images/base-images/#working-with-dynamically-plugged-devices)

Unfortunately udev internally uses a local network socket to propagate the events so running it in a separate net namespace breaks the connection and the events are not propagated correctly. As long as your connectivity is not affected the best solution for you is not to use the `UDEV` environment variable at all and simply start udev as a part of CMD in your Dockerfile. I hope this is acceptable.

---

<div class="post-metadata">

**Author:** ![ben.clerix](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@ben.clerix](https://forums.balena.io/u/ben.clerix)\
**Post date:** [February 15, 2021, 8:32pm UTC](https://forums.balena.io/t/udev-events-not-showing-up-in-service/250035/39 "2021-02-15T20:32:21Z")

</div>

For now we just kill the udev daemon and restart it in the dockerfile as part of the CMD as you proposed.

We still use a balenalib base image and use the `UDEV` env var. I suppose the `UDEV` env var does not do anything special, but it also will not hurt us in any way? Just to be sure.

---

<div class="post-metadata">

**Author:** ![mtoman](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mtoman/32/3599_2.png) [@mtoman](https://forums.balena.io/u/mtoman)\
**Post date:** [February 15, 2021, 9:09pm UTC](https://forums.balena.io/t/udev-events-not-showing-up-in-service/250035/40 "2021-02-15T21:09:13Z")

</div>

The `UDEV` variable triggers the start of the udev daemon you later need to kill, with the variable not set you would only need to start a fresh udev process.
