How to get host's udev events from a Docker container?
Asked Answered
S

2

7

In a Docker container, I am looking for a way to get the udev events on the host.
Using udevadm monitor, it sends back host's kernel events only in a container.

The question is whether there is a way to detect host's udev events or forward host's event to containers?

Subscription answered 6/4, 2018 at 7:14 Comment(0)
N
13

This is how I made my container receive host events by udev:

docker run --net=host -v /run/udev/control:/run/udev/control

--net=host allows container and host operate through PF_NETLINK sockets, which are used by udev monitor to receive kernel events (found here)

/run/udev/control is a file, which udev monitor uses to check if udevd is already running. If it doesn't exist, monitoring is disabled.

Nitid answered 6/9, 2018 at 12:3 Comment(1)
yes, this is a method to make it but it is not an ideal one since host mode is not secure.Subscription
S
1

Just like above answer pointed out: we could enable --net=host, but host network is not suggested because of multiple known reasons.

In fact this issue happens just because it needs NETLINK to communicate between kernel & user space, but if not using host network, host & container will in different netns, so enabling udev in the container could make them be in the same netns which then means there's no need to use host network.

When we ran into this issue, we did next:

# apt-get install udev

# vim /etc/init.d/udev to comment some special settings:

    1) Comments next:
    #if [ ! -e "/run/udev/" ]; then
    #    warn_if_interactive
    #fi

    2) Comments next:
    #if ! ps --no-headers --format args ax | egrep -q '^\['; then
    #    log_warning_msg "udev does not support containers, not started"
    #    exit 0
    #fi

# root@e751e437a8ba:~# service udev start
  [ ok ] Starting hotplug events dispatcher: systemd-udevd.
  [ ok ] Synthesizing the initial hotplug events (subsystems)...done.
  [ ok ] Synthesizing the initial hotplug events (devices)...done.
  [ ok ] Waiting for /dev to be fully populated...done.
Syrup answered 28/5, 2020 at 7:38 Comment(2)
I can't find section 2) in my host system's /etc/init.d/udev. Do I have to do anything inside the container except service udev start?Bind
The change is for /etc/init.d/udev in container, it's a separate udev system against hosts', so every operation should be operated in container.Syrup

© 2022 - 2024 — McMap. All rights reserved.