Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building kernel modules for different linux version

I am new to writing kernel modules, so facing few non-technical problems.

Since for creating kernel module for a specific kernel version ( say 3.0.0-10, 10 is patch number) requires same version kernel headers, so it looks straight to install kernel headers and start development over there. But kernel headers for patched kernel version are not available. As I have a guest kernel vmlinuz-3.0.0-10 running in machine and upon downloading kernel headers it says not found.

  • other approach is to get the source for that specific kernel, but again problem is same source for patched kernel is not available ( its not necessary to get sources of linux-kernel-3.0.0-10 or even linux-kernel-3.0.0 and 10th patch). In some situation it is possible to get source of running kernel, but not always possible.

  • another is to build kernel other than the running kernel and place built kernel in the machine. But it requires to build the modules of that kernel that is time-consuming and space-consuming process.

So intention of asking this is to know what are the preferences of kernel driver developers. Are there other alternatives ?

Is it possible to compile kernel module in one version and run in another version ( though it is going to give error, but are there any workaround for this ?)

like image 377
peeyush Avatar asked Jun 02 '12 11:06

peeyush


People also ask

Are kernel modules specific to kernel version?

2 Answers. You can't. Each module is compiled for a specific kernel version, and can't be used for kernels that differ from that.

How do I compile only one kernel module?

create new folder somewhere for the module source (example: extra) and copy only source files (from the kernel source or somewhere else) related to the module needed to be build into this new folder. copy /boot/config-`uname -r` file (example: /boot/config-4.8. 0-46-generic) into kernel source folder file .


2 Answers

So, building a new kernel is not a good option as it will require :

  • building kernel
  • building modules and firmware
  • building headers Moving all of above things in appropriate location (if your machine is not same on which you are going to develop module)

So if you have kernel headers for running system then you dont need to download a source code for any kernel version, and while making module use

make -C /lib/modules/kernel-headers-x.y.z/build M=`pwd` modules

and your module will be ready.

If there would be better answers, i will not hesitate to accept any of them.

like image 162
peeyush Avatar answered Oct 14 '22 20:10

peeyush


I know it's a long time since this question asked. I am new in the kernel development. I have also encountered the same error. But now I am able to load my module in the different kernel by which I have built it. Following is the solution:

  1. download the kernel-devel related to the image that you are running. It should have version as close as possible.
  2. Check that the functions you are using in the module are mapped with the header files you have in the kernel-devel.
  3. change the include/generated/utsrelease.h file for UTS_RELEASE value. change it to the version of kernel image running on your HW.
  4. Compile the module using this kernel tree.
  5. Now you can insert your module inside kernel.

Note:: It may cause some unwanted events to be happened as Shahbaz mentioned above. But if you are doing this just for experiments I think its good to go. :)

like image 22
Nitin Avatar answered Oct 14 '22 18:10

Nitin