1

To develop driver program, we need /lib/modules//build directory. But I found under docker image of centos, even after I

yum install kernel-devel

There's still no such a directory with all its contents. Question:

(1) how to make it possible to develop driver in a docker linux environment?

(2) is it possible to load this developed module?

Hind Forsum
  • 9,717
  • 13
  • 63
  • 119
  • 1
    Maybe it's possible (to develop and update kernel drivers from inside a -- very privileged -- container), but why ? The purpose of containers to get an isolated environment, but the kernel is shared with the host and all other containers, so there is no isolation. You might as well do it on the host directly. – Thilo Mar 21 '17 at 08:22
  • Note that it's not 2017 anymore and now there is a more exaustive answer that maybe deserves to be marked as solution https://stackoverflow.com/a/70760612/3451846 – Valerio Bozz Jan 16 '23 at 13:40

3 Answers3

4

Docker is not virtual machine.

Ubuntu with docker is not real ubuntu.

If you want to develop with ubuntu, you should use virtualbox or vmware.

Check this link for more information

kingsj0405
  • 149
  • 14
4

Docker uses the kernel of the host machine.

oscarteg
  • 187
  • 1
  • 10
4

After reading this page, I almost gave up building a kernel module in Docker so I'm adding this answer hoping it helps somebody. See also what-is-the-difference-between-kernel-drivers-and-kernel-modules

You can build Kernel modules in Docker as long as the Kernel source required for the build is available inside Docker. Lets say you want to build against the latest kernel source available in your yum repos, you could install the kernel source using yum install kernel-devel. The source will be in /usr/src/kernels/<version> directory. You could install specific version of kernel-devel from your repo if that is what you want.

Then build the module using $ make -C <path_to_kernel_src> M=$PWD where the path to the kernel source would be /usr/src/kernels/<version>.

Read - Kernel Build System » Building External Modules

Docker container uses the kernel of the host machine so if you want to build against the running kernel, i.e., the kernel of the Docker host machine, you could try running the container in privileged mode and mounting the modules directory. docker run --name container_name --privileged --cap-add=ALL -v /dev:/dev -v /lib/modules:/lib/modules image_id See this

You should not load the modules on a kernel that is not the same as the one the module was built for. You could force install it but that is highly discouraged. Remember your running kernel, i.e., the Docker host kernel, is the kernel of the Docker container irrespective of what kernel-devel version you installed.

To see the kernel the module was built for (or built using), run modinfo <module> and look for vermagic value.

Dynamic Kernel Module Support is also worth a read.

Praveen Lobo
  • 6,956
  • 2
  • 28
  • 40
  • 1
    This should be the accepted answer as it actually provides ways to do what OP asked rather than say impossible – Jack G Feb 15 '22 at 08:36