How to: Compile Linux kernel modules

本文介绍如何在Linux环境下编译内核模块。包括获取内核源代码、创建Makefile文件、编译模块等步骤,并以hello.c为例,演示了从源代码到加载模块的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html

This is one the essential and important task. Many time we upgrade our kernel and some precompiled drivers won't work with Linux. Especially if you have weird hardware; then vendor may send you driver code aka C files to compile. Or even you can write your own Linux kernel driver. Compiling kernel driver is easy. Kernel 2.6.xx makes it even much more easier. Following steps are required to compile driver as module:

1) You need running kernel source code; if you don't have a source code download it fromkernel.org. Untar kernel source code (tar ball) in /usr/src using tar command:
$ tar -zxvf kernel* -C /usr/src

To be frank kernel headers are more than sufficient to compile kernel modules / drivers. See how to install kernel headers under Debian / Ubuntu Linux or RHEL / CentOS / Fedora Linux.

2) Next go to your kernel module source code directory and simply create the Makefile file as follows (assuming your kernel module name is foo):
$ vi Makefile

3) Add following text to it:

obj-m = foo.o
KVERSION = $(shell uname -r)
all:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean

4) Compile module using make command (module build can be done by any user) :
$ make
It will finally creates the foo.ko module in current directory. You can see all actual compile command stored in .foo* files in same directory.

5) Once module compiled successfully, load it using insmod or modprobe command. You need to be root user or privileged user to run insmod:
# insmod foo.ko

Example: hello.c module

1) hello.c C source code. Copy following code and save to hello.c
$ mkdir demo; cd demo
$ vi hello.c

2)Add following c source code to it:

#include <linux/module.h>       /* Needed by all modules */
#include <linux/kernel.h>       /* Needed for KERN_INFO */
#include <linux/init.h>         /* Needed for the macros */
static int __init hello_start(void)
{
printk(KERN_INFO "Loading hello module...\n");
printk(KERN_INFO "Hello world\n");
return 0;
}
static void __exit hello_end(void)
{
printk(KERN_INFO "Goodbye Mr.\n");
}
module_init(hello_start);
module_exit(hello_end);

This is an example modified from original source for demonstration purpose.

3) Save the file. Create new Makefile as follows:
$ vi Makefile
Append following make commands:

obj-m = hello.o
KVERSION = $(shell uname -r)
all:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean

4) Save and close the file.

5) Compile hello.c module:
$ make

6) Become a root user (use su or sudo) and load the module:
$ su -
# insmod hello.ko

Note you can see message on screen if you are logged in as root under run level 3.

7) Verify that module loaded:
# lsmod | less

8) See message in /var/log/message file:
# tail -f /var/log/message

9) Unload the module:
# rmmod hello

10) Load module when Linux system comes up. File /etc/modules use to load kernel boot time. This file should contain the names of kernel modules that are to be loaded at boot time, one per line. First copy your module to /lib/modules/$(uname -r)/kernel/drivers. Following are suggested steps:

(a) Create directory for hello module:
# mkdir -p /lib/modules/$(uname -r)/kernel/drivers/hello
(b) Copy module:
# cp hello.ko /lib/modules/$(uname -r)/kernel/drivers/hello/
(c) Edit /etc/modules file under Debian Linux:
# vi /etc/modules
(d) Add following line to it:
hello
(e) Reboot to see changes. Use lsmod or dmesg command to verify module loaded or not.
# cat /proc/modules
OR
# lsmod | less

See also:

  • Read man pages of lsmod, rmmod, modprobe, modules
  • Documentation located in your kernel source directory (for example /usr/src/linux-2.6.xx.xx/Documentation/) and README file located under kernel source code tree /usr/src/linux-2.6.xx.xx/README
  • Read TLDP.org tutorial online.
How is emlog used? 1: Configure, compile, and install emlog If you want to compile emlog for use with the currently running kernel, simply run make Otherwise, you have to set either KVER (for linux kernel sources, located in /lib/modules/<KVER>/build) or KDIR (for any other path): make KDIR=/usr/src/linux Two files should be generated: the kernel module itself (emlog.ko), and the nbcat utility that will be described later. You can use them directly from the current directory or you can install them via make install 2: Load the emlog module into the kernel If you chose to use emlog directly from the current directly, insert the module into the kernel using the insmod command insmod emlog.ko Otherwise, modprobe should work: modprobe emlog To specify a different maximum buffer size limit: modprobe emlog emlog_max_size=2048 If successful, a message similar to emlog:emlog_init: version 0.70 running, major is 251, MINOR is 1, max size 1024 K. should show up in your kernel log (type dmesg to see it). You can also verify that the module has been inserted by typing lsmod or cat /proc/modules. 3: Create device files for emlog By default, a device file /dev/emlog is created (if you have devtmpfs mounted onto /dev and/or have udev running) with a minimal allocated buffer. It's ready to be written to/read from. If you need more devices/buffers, you should can use the mkemlog program to create device files that your processes can write to. Usage mkemlog <logdevname> [size_in_kilobytes] [mode] 3.1: Examples usage mkemlog Create a log file with a 8k buffer with file permissions 0660 mkemlog /tmp/testlog Create a log file with a 17k buffer with file permissions 0660 mkemlog /tmp/testlog 17 Create a log file with a 12k buffer with file permissions 0644 mkemlog /tmp/testlog_12k 12 0644 Create a log file with a 18k buffer with file permissions 0644, owned by a user with UID==1000. mkemlog /tmp/testlog_18k 18 0644 1000 The mkemlog requires the /dev/emlog file to be cemlog
最新发布
04-02
<think>好的,我需要帮助用户解决关于配置、编译、安装、加载和使用emlog模块,以及设置缓冲区大小、检查内核日志和使用mkemlog工具创建设备文件的问题。首先,我需要整理用户的需求,确保覆盖所有提到的步骤。 首先,用户提到了emlog,这可能是一个内核模块,用于日志记录。接下来需要指导配置和编译。通常,编译内核模块需要Makefile,所以可能需要检查Makefile是否正确,特别是内核版本和路径是否正确。引用中的内容提到配置器的初始化,这可能涉及到构建过程中的配置步骤,比如引用[1]和[2]提到的初始化方法和构建生命周期。不过这些引用看起来是关于安全构建器的,可能与emlog的配置不完全相关,但可能需要考虑类似的初始化流程。 然后是安装模块,使用insmod或modprobe加载模块。这里可能需要处理依赖关系,比如引用的库路径。引用[3]提到了libssl.so.3的路径配置和ldconfig,这可能与动态库加载有关,但不确定是否直接适用于内核模块。不过加载内核模块时可能需要确保依赖的符号或模块已经加载,但通常内核模块不涉及用户空间的库,所以可能不需要处理ldconfig,但需要确保内核头文件正确安装。 设置缓冲区大小可能在模块参数中指定,用户需要在加载模块时传递参数,比如使用insmod emlog.ko buffer_size=4096。需要检查模块是否支持参数,并在代码中声明模块参数。 检查内核日志通常使用dmesg命令,所以需要指导用户使用dmesg | grep emlog来查看相关日志信息。 创建设备文件部分,用户提到了mkemlog工具。通常,创建设备文件可以使用mknod,但可能有专门的工具来自动处理主次设备号的分配。需要说明如何使用mkemlog,可能需要先确定模块注册的设备号,然后使用工具创建,或者工具自动处理。 现在需要确保每个步骤的准确性。例如,编译内核模块需要正确版本的内核头文件,安装模块到/lib/modules/对应的目录,运行depmod更新模块依赖,加载模块时可能需要sudo权限。缓冲区大小作为模块参数是否正确处理,设备节点的权限是否正确设置。 可能遇到的问题:模块编译错误,可能是内核版本不匹配;加载模块时找不到符号,可能依赖其他模块;设备文件无法创建,权限不足或工具路径不正确;缓冲区大小设置无效,模块未正确解析参数。 需要分步骤详细说明,并参考用户提供的引用。例如,在配置和初始化阶段,参考引用[1]和[2]提到的初始化方法,确保配置器正确初始化和构建。在解决库路径问题时,参考引用[3]中的方法,配置动态库路径,但可能更多适用于用户空间程序而非内核模块。 总结步骤: 1. 配置模块:检查Makefile,确保内核头文件正确。 2. 编译:使用make命令。 3. 安装模块:复制到/lib/modules/$(uname -r)/目录,运行depmod。 4. 加载模块:insmod或modprobe,可能带参数设置缓冲区大小。 5. 检查内核日志:dmesg查看输出。 6. 创建设备文件:使用mkemlog工具,可能需要指定主次设备号或自动获取。 7. 测试设备文件:读写操作验证功能。 需要验证每个步骤的正确性,并提供命令示例。例如,在设置缓冲区大小时,检查模块是否导出参数,使用sysfs或modinfo查看可用参数。在创建设备文件时,确保设备号正确,权限适当。 可能还需要处理模块的自动加载,如配置/etc/modules-load.d/,以及使用udev规则自动创建设备节点,但用户可能只需要基础步骤。需要确保回答清晰,步骤正确,并考虑到常见问题,如权限问题、内核版本兼容性等。</think>### 配置、编译与安装emlog模块 #### 一、环境准备 1. **安装内核头文件** 确保已安装与当前内核版本匹配的开发工具包: ```bash sudo apt-get install linux-headers-$(uname -r) build-essential ``` #### 二、配置与编译 1. **编写Makefile** 在emlog源码目录中创建`Makefile`,内容参考: ```makefile obj-m := emlog.o KDIR := /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) default: $(MAKE) -C $(KDIR) M=$(PWD) modules ``` *注:`emlog.c`需包含模块参数定义(如`buffer_size`)及设备注册逻辑[^2]。* 2. **编译模块** 执行以下命令生成`.ko`文件: ```bash make ``` #### 三、安装与加载模块 1. **安装模块到系统目录** ```bash sudo cp emlog.ko /lib/modules/$(uname -r)/kernel/drivers/ sudo depmod -a # 更新模块依赖 ``` 2. **加载模块并设置缓冲区大小** ```bash sudo insmod /lib/modules/$(uname -r)/kernel/drivers/emlog.ko buffer_size=4096 # 或使用modprobe(需先配置模块别名) sudo modprobe emlog buffer_size=4096 ``` #### 四、验证模块状态 1. **检查模块加载状态** ```bash lsmod | grep emlog ``` 2. **查看内核日志** ```bash dmesg | tail -n 20 | grep emlog # 过滤emlog相关日志 ``` #### 五、创建设备文件 1. **使用mkemlog工具** 假设工具已编译,执行以下命令创建设备节点: ```bash sudo mkemlog /dev/emlog0 # 自动分配主/次设备号 ``` *注:需确保工具代码中调用`mknod`并正确获取设备号[^3]。* 2. **验证设备权限** ```bash ls -l /dev/emlog0 # 确认权限为666(需在代码中设置) ``` --- ### 关键配置详解 #### 1. **缓冲区大小设置** - 在模块代码中声明模块参数: ```c static int buffer_size = 1024; module_param(buffer_size, int, S_IRUGO); ``` - 加载时通过`buffer_size=值`动态调整。 #### 2. **设备文件自动创建** - 在模块初始化函数中调用`register_chrdev`注册字符设备: ```c major = register_chrdev(0, "emlog", &fops); # 动态分配主设备号 ``` - `mkemlog`工具应解析`/proc/devices`获取主设备号并调用: ```bash mknod /dev/emlog0 c $major 0 ``` --- ### 常见问题解决 1. **模块加载失败** - 错误:`Unknown symbol in module` **解决**:执行`sudo modprobe dependencies`加载依赖模块。 2. **设备文件无法读写** - 错误:`Permission denied` **解决**:在代码中设置`device_create`时指定权限,或执行: ```bash sudo chmod 666 /dev/emlog0 ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值