Linux内核模块编程之Helloworld(初级)

注意printk那里,KERN_ALERT和打印消息之间是没有逗号的,搞得劳资查了半天才发现一直没有提示信息的原因

#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");//MODULE_LICENSE()指明认证方式,现在支持的有:“GPL” “GPL v2" "GPL and additional rights" "Dual BSD/GPL" "Dual MIT/GPL" "Dual MPL/GPL" "Proprietary",这是内核2.6里新添加的,实验发现它不是必需的。
static int hello_init(void)
{
     printk(KERN_ALERT "Hello, World\n");//printk是内核级别的打印函数,KERN_ALERT是指该条信息是警告信息
     return 0;
}
static int hello_exit(void)
{
     printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);//模块入口
module_exit(hello_exit);//模块出口

下面我们来写Makefile(命名为makefile我的会提示错误),-C 去内核源码目录下读取Makefile,m=返回当前路径执行当前目录下的Makefile
。我觉得Makefile这个文件挺6的,该文件会根据xx.o查找相应的xx.c文件

TARGET=hello
KDIR=/usr/src/kernels/3.10.0-514.el7.x86_64 //找到内核文件所在路径,系统不同路径也不同,可以使用find / -name kernel查找
PWD=$(shell pwd)   //这个是指执行shell命令pwd,即用PWD记录当前路径
obj-m=$(TARGET).o
default:
    make -C $(KDIR) M=$(PWD) modules

那么如何运行呢,首先make

[04:21:42] make
make -C /usr/src/kernels/3.10.0-514.el7.x86_64 M=/root/kernel modules
make[1]: Entering directory `/usr/src/kernels/3.10.0-514.el7.x86_64'
  CC [M]  /root/kernel/hello.o
/root/kernel/hello.c: In function ‘hello_exit’:
/root/kernel/hello.c:14:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
In file included from /root/kernel/hello.c:1:0:
/root/kernel/hello.c: In function ‘__exittest’:
include/linux/init.h:305:4: warning: return from incompatible pointer type [enabled by default]
  { return exitfn; }     \
    ^
/root/kernel/hello.c:17:1: note: in expansion of macro ‘module_exit’
 module_exit(hello_exit);
 ^
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /root/kernel/hello.mod.o
  LD [M]  /root/kernel/hello.ko
make[1]: Leaving directory `/usr/src/kernels/3.10.0-514.el7.x86_64'
[04:21:45] ls
hello.c  hello.ko  hello.mod.c  hello.mod.o  hello.o  Makefile  modules.order  Module.symvers

现在进行加载模块,首先在当前终端

[04:21:47] tail -f /var/log/messages
May 22 04:01:01 bogon systemd: Started Session 727 of user root.
May 22 04:01:01 bogon systemd: Starting Session 727 of user root.
May 22 04:10:01 bogon systemd: Started Session 728 of user root.
May 22 04:10:01 bogon systemd: Starting Session 728 of user root.
May 22 04:18:35 bogon kernel: Hello, World//在另一个终端执行insmod ./hello.ko才会出现这个
May 22 04:18:49 bogon kernel: Goodbye, cruel world//在另一个终端执行rmmod hello才会出现这个
May 22 04:20:01 bogon systemd: Started Session 729 of user root.
May 22 04:20:01 bogon systemd: Starting Session 729 of user root.
May 22 04:20:01 bogon kernel: Hello, World
May 22 04:20:11 bogon kernel: Goodbye, cruel world
May 22 04:22:06 bogon kernel: Hello, World

我在另一个终端的执行过程就是进入该目录,然后执行insmod ./hello.ko 然后第一个终端就会显示Hello,world。在执行rmmod hello 就会显示Goodbye, cruel world

但是!根据系统版本的不同还是什么其他原因,有时候信息在/var/log/messages 里看到,使用dmesg可以看到

[root@bogon kernel]# dmesg|tail -5
[  123.690748] test: module verification failed: signature and/or required key missing - tainting kernel
[  876.865300] Hello, World
[  908.638904] Goodbye, cruel world
[ 1235.101965] e1000: ens33 NIC Link is Down
[ 1241.118672] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[root@bogon kernel]# 

或者是在加载或者移除模块这里看到
这里写图片描述

也可以像下面这么写,注意,uname -r那里是使用的反引号就是tab键上面的那个键,$(PWD)当前工作目录,clean清除,打印信息是显示在另一个终端的,例如我就是在物理机上使用ssh链接虚拟机centos7,然后执行下面的命令,而在虚拟机的终端上则会显示打印的那几条信息

[root@bogon modules]# cat first.c
#include<linux/kernel.h>
#include<linux/module.h>
int init_module(void){
    printk(KERN_ALERT   "hi,this is bp\n");
    return 0;
}
void cleanup_module(void){
    printk(KERN_ALERT   "goobye bp\n");
}
[root@bogon modules]# cat Makefile 
obj-m=first.o
default:
    make -C /usr/src/kernels/`uname -r` M=$(PWD) modules
clean:
    make -C /usr/src/kernels/`uname -r` M=$(PWD) clean
[root@bogon modules]# make
make -C /usr/src/kernels/`uname -r` M=/root/modules modules
make[1]: Entering directory `/usr/src/kernels/3.10.0-514.el7.x86_64'
  Building modules, stage 2.
  MODPOST 1 modules
make[1]: Leaving directory `/usr/src/kernels/3.10.0-514.el7.x86_64'
[root@bogon modules]# modinfo first.ko//查看模块信息
filename:       /root/modules/first.ko
rhelversion:    7.3
srcversion:     2523BB278E7311D9141E7F4
depends:        
vermagic:       3.10.0-514.el7.x86_64 SMP mod_unload modversions 
[root@bogon modules]# insmod first.ko
[root@bogon modules]# rmmod first
[root@bogon modules]# ls
a.c      a.mod.o  first.ko     first.o   hello.mod.c  Makefile
a.ko     a.o      first.mod.c  hello.c   hello.mod.o  modules.order
a.mod.c  first.c  first.mod.o  hello.ko  hello.o      Module.symvers
[root@bogon modules]# make clean
make -C /usr/src/kernels/`uname -r` M=/root/modules clean
make[1]: Entering directory `/usr/src/kernels/3.10.0-514.el7.x86_64'
  CLEAN   /root/modules/.tmp_versions
  CLEAN   /root/modules/Module.symvers
make[1]: Leaving directory `/usr/src/kernels/3.10.0-514.el7.x86_64'
[root@bogon modules]# ls
a.c  first.c  hello.c  Makefile
[root@bogon modules]# 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ARM 是一种广泛使用的 CPU 架构,而 Linux 内核是一个开放源代码的操作系统内核。在 ARM 平台上,我们可以通过内核模块编程的方式与内核进行交互,实现一些自定义的功能。 下面,我们将介绍如何在 ARM Linux 上编写内核模块,并输出一个简单的 "Hello World" 消息。 ## 1. 环境准备 在开始编写内核模块之前,需要先准备好开发环境。具体步骤如下: 1. 安装交叉编译工具链。ARM 平台上的应用程序和内核模块需要使用交叉编译工具链进行编译。可以从官网下载对应的交叉编译工具链,也可以使用已经编译好的交叉编译工具链。 2. 下载内核源代码。可以从官网下载对应版本的内核源代码,也可以使用已经编译好的内核源代码。 3. 配置内核源代码。需要在内核源代码根目录下运行配置命令 `make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- menuconfig` 进行配置,选择需要的模块和功能。 ## 2. 编写内核模块 在准备好开发环境之后,可以开始编写内核模块了。具体步骤如下: 1. 创建一个新的文件夹,用于存放内核模块代码。 2. 创建一个新的 C 文件,命名为 `hello.c`。 3. 在 `hello.c` 文件中编写以下代码: ```c #include <linux/init.h> #include <linux/module.h> 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); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("A simple hello world module"); ``` 这段代码定义了一个简单的内核模块,当模块加载时会输出 "Hello, world!" 消息,当模块卸载时会输出 "Goodbye, world!" 消息。 4. 使用交叉编译工具链进行编译。在终端中进入 `hello.c` 文件所在的文件夹,运行以下命令进行编译: ```bash arm-linux-gnueabi-gcc -Wall -Werror -O2 -o hello.ko -c hello.c ``` 这个命令将生成一个名为 `hello.ko` 的内核模块文件。 ## 3. 加载和卸载内核模块 在编写好内核模块后,我们需要将它加载到内核中进行测试。具体步骤如下: 1. 将 `hello.ko` 文件复制到 ARM Linux 系统上。 2. 在终端中进入 `hello.ko` 文件所在的文件夹,运行以下命令以加载内核模块: ```bash insmod hello.ko ``` 这个命令将调用内核中的 `init_module` 函数,执行 `hello_init` 函数,输出 "Hello, world!" 消息。 3. 查看系统日志,可以看到 "Hello, world!" 消息。 ```bash dmesg ``` 4. 在终端中运行以下命令以卸载内核模块: ```bash rmmod hello ``` 这个命令将调用内核中的 `cleanup_module` 函数,执行 `hello_exit` 函数,输出 "Goodbye, world!" 消息。 5. 再次查看系统日志,可以看到 "Goodbye, world!" 消息。 至此,我们已经成功地在 ARM Linux 上编写了一个简单的内核模块,并输出了 "Hello, world!" 消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值