问题
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 ?)
回答1:
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.
回答2:
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:
- download the kernel-devel related to the image that you are running. It should have version as close as possible.
- Check that the functions you are using in the module are mapped with the header files you have in the kernel-devel.
- change the include/generated/utsrelease.h file for UTS_RELEASE value. change it to the version of kernel image running on your HW.
- Compile the module using this kernel tree.
- 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. :)
回答3:
There is a way to build a module on one kernel and insert it in another. It is by turning off a certain configuration. I am not telling you which configuration it is because this is ABSOLUTELY DANGEROUS. The reason is that there may be changes between the kernels that could cause your module to behave differently, often resulting in a total freeze.
What you should do is to build the module against an already-built kernel (or at least a configured one). If you have a patched kernel, the best thing you can do is to build that kernel and boot your OS with that.
I know this is time consuming. I have done it many many times and I know how boring it can get, but once you do it right, it makes your life much easier. Kernel compilation takes about 2 hours or so, but you can parallelize it if you have a multi-core CPU. Also, you can always let it compile before you leave the office (or if at home, before going to bed) and let it work at night.
In short, I strongly recommend that you build the kernel you are interested in yourself.
来源:https://stackoverflow.com/questions/10861872/building-kernel-modules-for-different-linux-version