SUSE Linux下单独编辑内核模块方法

本文档详细介绍了如何在SUSE Linux环境下单独编辑和编译内核模块。首先展示了测试用的hello.c内核模块,然后创建build目录,并在/usr/src/linux下使用make命令克隆配置。接着提供了用于编译模块的Makefile内容,最后演示了加载和卸载hello.ko模块的步骤。

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

测试的hello.c内核模块

novell@sles11sp3:~/tmp> ls hello.c Makefile

hello.c  Makefile
novell@sles11sp3:~/tmp> cat hello.c Makefile
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
obj-m := hello.o
novell@sles11sp3:~/tmp> cat hello.c
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);


准备build目录

novell@sles11sp3:~> mkdir kernel

novell@sles11sp3:~> cd  kernel

novell@sles11sp3:~kernel>make -C /usr/src/linux O=$(pwd) cloneconfig

编译这个模块所用的Makefile

novell@sles11sp3:~/tmp> cat Makefile

obj-m := hello.o

编译命令

novell@sles11sp3:~/tmp> make -C ~/kernel/ M=$(pwd) modules

make: Entering directory `/home/novell/kernel'
make -C /usr/src/linux-3.0.76-0.11 O=/home/novell/kernel/. modules
  Building modules, stage 2.
  MODPOST 1 modules
make: Leaving directory `/home/novell/kernel'


novell@sles11sp3:~/tmp> ls

hello.c  hello.ko  hello.mod.c  hello.mod.o  hello.o  Makefile  modules.order  Module.symvers


模块加载及卸载

novell@sles11sp3:~/tmp> sudo /sbin/insmod hello.ko
novell@sles11sp3:~/tmp> sudo tail -n5 /var/log/messages
Sep 16 15:34:54 sles11sp3 kernel: [21336.059008] Hello, world
Sep 16 15:35:15 sles11sp3 sudo:   novell : TTY=pts/1 ; PWD=/home/novell/tmp ; USER=root ; COMMAND=/usr/bin/tail -n5 /var/log/messages


novell@sles11sp3:~/tmp> sudo /sbin/rmmod hello.ko

novell@sles11sp3:~/tmp> sudo tail -n3 /var/log/messages
Sep 16 15:35:28 sles11sp3 sudo:   novell : TTY=pts/1 ; PWD=/home/novell/tmp ; USER=root ; COMMAND=/sbin/rmmod hello.ko
Sep 16 15:35:28 sles11sp3 kernel: [21369.558324] Goodbye, cruel world
Sep 16 15:35:32 sles11sp3 sudo:   novell : TTY=pts/1 ; PWD=/home/novell/tmp ; USER=root ; COMMAND=/usr/bin/tail -n3 /var/log/messages
novell@sles11sp3:~/tmp>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值