3

The "How to Build External Modules" section of the kernel.org kbuild documentation ( https://www.kernel.org/doc/Documentation/kbuild/modules.txt ) says:

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."

My question is, alternative to what? Alternative to

"have a prebuilt kernel available that contains the configuration and header files"

or

"the kernel must have been built with modules enabled"

or something else?

Lavya
  • 1,605

1 Answers1

2

It's an alternative to "using a distribution [...] package", which is synonymous with having "the configuration and header files" available.

You should include a .config before you do this. Most distro kernels have this available in /proc/config.gz; copy that into the top of the source tree and

gunzip -c config.gz > .config

This will only work if the source version is >= the running kernel. Note that if you intend to use the module with the running kernel, the source version number should be exactly the same.

goldilocks
  • 87,661
  • 30
  • 204
  • 262
  • Thank you for your answer. When you say "You should include a .config before you do this.", do you mean that I should also have the whole source tree of the kernel I want to build the module for? So If I understand correctly, I can either have the header and config files, or, the complete source tree and .config file where I need to do make modules_prepare? – Lavya Jun 10 '15 at 16:55
  • 1
    Yes. You can't build a module without a kernel source. The "make targets" referred to in that documentation are in that source. Ideally, you should use a distro package for that (they all have one). Alternately -- notice I am kind of repeating something here ;) -- you could download one from kernel.org and go the make modules_prepare route. But if you do that, make sure it is has the same version number (they are all archived if you descend into https://www.kernel.org/pub/linux/kernel/). – goldilocks Jun 10 '15 at 17:11
  • thanks! I assume by "make sure it is has the same version number" you mean that the version number of the source I am using should be the same as of the kernel I intend to insert/run the module in. Is that correct? So, if my running kernel is 3.19.5-100.fc20.i686+PAE, I should download the sources corresponding to 3.19.5-100.fc20.i686+PAE from fedora and do make module_prepare in that and then build modules with it. Is that correct? Thanks once again for your help! – Lavya Jun 10 '15 at 17:31
  • 1
    Yes. On fedora it's the -devel package. Pretty sure you do have to do the make modules_prepare thing with it too, unless they've done so already -- in which case doing it again won't do any harm. The tree will be in /usr/src/kernels/[version] and will have an appropriate .config in it. – goldilocks Jun 10 '15 at 17:44
  • 1
    If that fails (I just tried w/ a fedora package and it did), just download 3.19.5 from kernel.org, pop in your .config, and use that. – goldilocks Jun 10 '15 at 17:51
  • installation of -header and devel packages didn't work - yum can't find the packages it seems. I downloaded 3.19.5 from kernel.org and compiled module with it but the kernel ( 3.19.5-100.fc20.i686+PAE) doesn't accept it: invlalid module format. so i'm compiling the vanilla 3.19.5 now, and will boot in that kernel and compile the module with it. that should probably work. – Lavya Jun 11 '15 at 06:44
  • just to update, as expected, everything works smoothly if I compile and install vanilla linux from kernel.org. I can live with that for now. Will find out why I couldn't find out fedora headers/sources later. thanks! – Lavya Jun 11 '15 at 08:40
  • 1
    If you use the config from /proc/config.gz that should work -- you'll end up with the exact same kernel sans Fedora patches. Those are not hugely significant, though. Fedora is my distro of choice and I've been rolling my own kernels from the vanilla source there for years and never had a problem. Pay attention to steps 4, 5, and 6 here: http://unix.stackexchange.com/a/115621/25985 – goldilocks Jun 11 '15 at 12:08