centos7编写内核模块
时间: 2023-10-24 11:08:33 浏览: 301
编写内核模块需要以下步骤:
1. 安装内核源代码和编译工具
2. 编写内核模块代码
3. 编写 Makefile 文件
4. 编译内核模块
5. 加载内核模块
下面是一个简单的示例:
1. 安装内核源代码和编译工具
```
sudo yum install kernel-devel gcc
```
2. 编写内核模块代码
```c
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
static int __init hello_init(void)
{
printk(KERN_INFO "Hello, world!\n");
return 0;
}
static void __exit hello_exit(void)
{
printk(KERN_INFO "Goodbye, world!\n");
}
module_init(hello_init);
module_exit(hello_exit);
```
3. 编写 Makefile 文件
```makefile
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
```
4. 编译内核模块
```
make
```
5. 加载内核模块
```
sudo insmod hello.ko
```
阅读全文
相关推荐


















