How to recompile just a single kernel module?

前端 未结 5 831
-上瘾入骨i
-上瘾入骨i 2021-01-29 22:42

Usually kernel source are stored in /usr/src/linux-2.6.x/. To avoid to recompile the entire kernel if I modify a module\'s source, how can I recompile just that mod

5条回答
  •  野性不改
    2021-01-29 22:59

    since kernel versions 3.x.x and 4.x.x the procedure gets more complicated (but there is a hope, so keep reading):

    1. make distclean if you haven't just cloned a new source but used to build other modules before
    2. 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
    3. copy /boot/config-`uname -r` file (example: /boot/config-4.8.0-46-generic) into kernel source folder file .config and run make oldconfig. if the module belongs to the kernel source, verify if it has been enabled by calling make menuconfig, by searching for the module and applying letter 'M' if necessary
    4. kernel source root Makefile has to be altered with exact version components matching the current running one (you may verify with make kernelversion if it matches exactly the uname -rone)
    5. there is been a strong suggestion to build scripts also before with make scripts
    6. make prepare and make modules_prepare has to be executed prior to the actual module build
    7. Module.symvers has to be copied from the target system headers folder corresponding running kernel version /usr/src/linux-headers-`uname -r`/Module.symvers (example: /usr/src/linux-headers-3.13.0-117-generic/Module.symvers) into the newly created module source files folder prepared for the module compilation (the one extra in example).
    8. create new Makefile inside module source compilation folder having following line: obj-y += .o or if the source code is complicated, use the guidance from here
    9. only then it's the right time to build module with make -C M=the_module_directory (example: make -C . M=extra/)
    10. Use command modprobe --dump-modversion .ko to verify CRC match between module exporting API and corresponding values in Module.symvers. in case of failure use command modinfo .ko instead
    11. verify if kernel.release file content match exactly the one from headers of the current running version. if you'll discover + appended at the end, it means you've been compiling git clonned source and your experimental modifications caused build system to compromise the localversion string by adding + at the end.
    12. if only + has been discovered at the tail of kernel.release stored value and it's a mismatch with the exact name of the target running kernel,

    the solution would be following:

    commit all your changes, force release tag to shift above your modifications with the git tag -a -f command. then rebuild your modules from step 8

提交回复
热议问题