如何重新编译单个内核模块?

发布于 2024-12-24 22:13:17 字数 87 浏览 0 评论 0原文

通常内核源代码存储在/usr/src/linux-2.6.x/中。 为了避免在修改模块的源代码时重新编译整个内核,如何重新编译该模块?

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 module?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

辞慾 2024-12-31 22:13:17

切换到源代码树的根目录并运行以下命令:

$ make modules SUBDIRS=drivers/the_module_directory

并安装已编译的模块:

$ make modules_install SUBDIRS=drivers/the_module_directory

注意:正如 lunakid 提到的,后一个命令可能不会构建模块首先,所以要小心。

Switch to the root directory of your source tree and run the following command:

$ make modules SUBDIRS=drivers/the_module_directory

And to install the compiled module:

$ make modules_install SUBDIRS=drivers/the_module_directory

Note: As lunakid mentions, the latter command might not build the module first, so be careful.

忆梦 2024-12-31 22:13:17

从内核版本 3.xx4.xx 开始,过程变得更加复杂(但有希望,所以继续阅读):

  1. make distclean如果您不仅克隆了新源,还用于在
  2. 为模块源创建新文件夹之前构建其他模块(例如:extra
    并仅将与需要构建的模块相关的源文件(从内核源或其他地方)复制到此新文件夹中
  3. 复制 /boot/config-`uname -r` 文件(例如:/boot/config-4.8.0-46-generic)进入内核源文件夹文件.config并运行make oldconfig。如果模块属于内核源代码,请通过调用 make menuconfig 来验证它是否已启用,方法是搜索该模块并在必要时应用字母“M”
  4. 内核源代码根 Makefile 必须使用与当前运行版本相匹配的确切版本组件进行更改(您可以使用 make kernelversion 验证它是否与 uname -r 完全匹配)
  5. 。建议之前也构建脚本
    make script
  6. makepreparemakemodules_prepare 必须在实际模块构建之前执行
  7. Module.symvers从对应运行内核版本的目标系统头文件文件夹复制 /usr/src/linux-headers-`uname -r`/Module.symvers (例如: /usr/src/linux-headers-3.13.0-117-generic/Module.symvers)进入为模块编译准备的新创建的模块源文件文件夹(一个额外的)。
  8. 在模块源代码编译文件夹中创建新的 Makefile 并包含以下行: obj-y +=.o 或者如果源代码很复杂,请使用 < a href="https://www.kernel.org/doc/Documentation/kbuild/modules.txt" rel="noreferrer">here
  9. 只有这样,才是使用 make 构建模块的正确时机 - C <内核源码路径> M=the_module_directory (示例:make -C . M=extra/
  10. 使用命令 modprobe --dump-modversion.ko 验证 CRC模块导出 API 与 Module.symvers 中的相应值之间的匹配。如果失败,请使用命令 modinfo.ko
  11. 验证 kernel.release 文件内容是否与当前运行版本的标头中的内容完全匹配。如果您发现末尾附加了 +,则意味着您一直在编译 git 克隆源代码,并且您的实验性修改导致构建系统通过添加 + 来破坏 localversion 字符串在最后。
  12. 如果在 kernel.release 存储值的尾部仅发现 + 并且它与目标运行内核的确切名称不匹配,

方案如下:

则解决 所有更改,使用 git tag -a强制发布标签移至您的修改之上-f 命令。然后从步骤 8 重建模块

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 += <module_source_file_name>.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 <kernel source path> M=the_module_directory (example: make -C . M=extra/)
  10. Use command modprobe --dump-modversion <module_name>.ko to verify CRC match between module exporting API and corresponding values in Module.symvers. in case of failure use command modinfo <module_name>.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 <tag version> -f command. then rebuild your modules from step 8

顾北清歌寒 2024-12-31 22:13:17

您可以将模块名称或模块目录的路径传递给 make 作为参数。

make path/to/the/module/itself.ko
make path/to/the/module/directory/

You can pass the path to the module name or module directory to make as parameter.

make path/to/the/module/itself.ko
make path/to/the/module/directory/
迷爱 2024-12-31 22:13:17

如果您仅编辑 drivers/net/ethernet/intel/e1000/e1000_main.c 文件中的代码

构建模块。

make scripts prepare modules_prepare
make -C . M=drivers/net/ethernet/intel/e1000

安装模块。

cp drivers/net/ethernet/intel/e1000/e1000.ko /lib/modules/5.1.15/kernel/drivers/net/ethernet/intel/e1000/e1000.ko

In case you have edited just code in drivers/net/ethernet/intel/e1000/e1000_main.c file

Build the module.

make scripts prepare modules_prepare
make -C . M=drivers/net/ethernet/intel/e1000

Install the module.

cp drivers/net/ethernet/intel/e1000/e1000.ko /lib/modules/5.1.15/kernel/drivers/net/ethernet/intel/e1000/e1000.ko
万人眼中万个我 2024-12-31 22:13:17
make -C /lib/modules/$(uname -r)/build M=$(pwd) modules
make -C /lib/modules/$(uname -r)/build M=$(pwd) modules_install

https://askubuntu.com/questions/515407/how-recipe-仅构建一个内核模块

make -C /lib/modules/$(uname -r)/build M=$(pwd) modules
make -C /lib/modules/$(uname -r)/build M=$(pwd) modules_install

https://askubuntu.com/questions/515407/how-recipe-to-build-only-one-kernel-module

尸血腥色 2024-12-31 22:13:17

我无法发表评论。因此,要完成 Niklas B. 针对 kernel-5.15 系列的回答,

请切换到源代码树的根目录并运行以下命令。

make prepare
make modules SUBDIRS=drivers/the_module_directory
make modules_install SUBDIRS=drivers/the_module_directory

modules_install 重新安装所有驱动程序。我不知道是否可以/建议复制您的驱动程序。

使用 rtl8xxxu 驱动程序进行测试。

I can't comment. So to complete the answer from Niklas B. for kernel-5.15 series

Switch to the root directory of your source tree and run the following commands.

make prepare
make modules SUBDIRS=drivers/the_module_directory
make modules_install SUBDIRS=drivers/the_module_directory

modules_install re-install all drivers. I don't know if it's possible / advisable to just copy your driver.

Tested with rtl8xxxu driver.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文