3

The Linux kernel documentation page for building external modules (https://www.kernel.org/doc/Documentation/kbuild/modules.txt) says this:

=== 2. How to Build External Modules

To build external modules, you must have a prebuilt kernel available that contains the configuration and header files used in the build. Also, the kernel must have been built with modules enabled. If you are using a distribution kernel, there will be a package for the kernel you are running provided by your distribution.

An alternative is to use the "make" target "modules_prepare." This will make sure the kernel contains the information required. The target exists solely as a simple way to prepare a kernel source tree for building external modules.

My questions are the following:

  1. To build external modules, you must have a prebuilt kernel available that contains the configuration and header files used in the build

By "prebuilt kernel", does it mean the compiled binary image (generally named vmlinux/vmlinuz)? Why exactly is the binary image needed? Shouldn't the configuration files, header files and compiler be enough?

  1. To build external modules, you must have a prebuilt kernel available that contains the configuration and header files used in the build.

If by prebuilt kernel it means the binary image, then what is the meaning of "contains the configuration and header files"? I can understand the source tree needing to "contain the configuration and header files", but in case of binary, these files are just used to generate instructions right? What is the meaning of "contain" then? By "prebuilt kernel" does it mean the entire source tree where the kernel was built?

  1. Also, the kernel must have been built with modules enabled.

Are they referring to the "make modules" step here or is it something different?

  1. If you are using a distribution kernel, there will be a package for the kernel you are running provided by your distribution.

I suppose they are referring to the kernel-devel package here, which provides the header and configuation files which were used in the kernel building process. Is that correct?

  1. An alternative is to use the "make" target "modules_prepare." This will make sure the kernel contains the information required.

What is the meaning of this? Does this mean that we don't need to have a built kernel binary in order to be able to build external modules if we do a "make modules_prepare" in the source directory?

3

ad 1. and 2. The kernel image is called vmlinux, that's right, but that's not what you actually need when you want to build external modules. It's the configuration and header files from this kernel that is needed.

ad 3. To build modules, internal or external, you need support for loadable modules in this kernel, you want to build the module for, of course, so the kernel has to be configured with __modules enabled_.

A kernel is configured by one of the configuration programs that help you in creating a .config file, in the kernel source tree or in the $KBUILD_OUTPUT path, for off-tree builds.

ad 4. Where you find such packages or how they named depends on your distribution, but I think it is often called kernel-devel. I don't actually know cause I used my own kernel tree for years.

ad 5. Yes, you actually don't need the kernel binary, to compile an external module, but you suppressed the note below

NOTE: "modules_prepare" will not build Module.symvers even if CONFIG_MODVERSIONS is set; therefore, a full kernel build needs to be executed to make module versioning work.

Most kernels use CONFIG_MODVERSIONS, I think. You can see this in your .config file with

$ grep MODVERSIONS .config
CONFIG_MODVERSIONS=y

This means, your built module will only work for that kernel version and configuration you built it for.

So you can build a module for that kernel and kernel version, but you cannot run it without that kernel and kernel version.

That's why you can build an external module for a distribution kernel without the full kernel source tree, if you install the kernel configuration and header files, that distribution kernel was built with.

Actually, most times you just want to build an external module for the kernel you run your system with. If you built the kernel yourself, from the kernel source tree, you will already have a kernel configuration and header files, that match that kernel.

If you run a distribution kernel you have to install that files from the distribution.

  • Thanks for your quick answer. Then I suppose "To build external modules, you must have a prebuilt kernel available " is incorrect? Also by configuration file you mean only .config or more? – Lavya Jul 5 '15 at 8:30
  • 1
    When you create the .config file by some of the make *config options the configuration headers will be built from this .config automatically. There is a target make silentoldconfig that uses a .config file from a possibly older kernel, rewrites it if needed and creates the header files based on that configuration. The target modules_prepare will likely include that step. – ikrabbe Jul 5 '15 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.