Linux kernel task 1:Hello world

2014/05/0401:54:25 3

开始这个计划源于一个Linux kernel challenge.网址为:

http://eudyptula-challenge.org/

想要参加的朋友可以发送电子邮件报名参加。以下是我的第一个任务内容:

This is Task 01 of the Eudyptula Challenge
------------------------------------------
 
Write a Linux kernel module, and stand-alone Makefile, that when loaded
prints to the kernel debug log level, "Hello World!"  Be sure to make
the module unloadable as well.
 
The Makefile should build the kernel module against the source for the
currently running kernel, or, use an environment variable to specify
what kernel tree to build it against.
 
Please show proof of this module being built, and running, in your
kernel.  What this proof is is up to you.  I'm sure you can come up with
something.  Also be sure to send the kernel module you wrote, along with
the Makefile you created to build the module.
 
Remember to use your ID assigned in the Subject: line when responding to
this task, so that I can figure out who to attribute it to.  You can
just respond to the task with the answers and all should be fine.
 
If you forgot, your id is "xxxxxxxxxx".  But of course you have not
forgotten that yet, you are better than that.


好了,开始我的Task1:hello world! 用VIM新建hello.c

#include <linux/module.h>
#include <linux/init.h>
#include <linux/version.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("chin,chenqiin@gmail.com");
MODULE_DESCRIPTION("Task 01 of the Eudyptula Challenge");
MODULE_LICENSE("GPL");
MODULE_VERSION("Version-0.0.2");
 
static int hello_init(void)
{
        printk(KERN_DEBUG"Hello,world!\n");
        return 0;
}
static void hello_exit(void)
{
        printk(KERN_DEBUG"Hello exit!\n");
}
module_init(hello_init);
module_exit(hello_exit);


创建一个Makefile

#Change KDIR to your kernel src
KDIR := /lib/modules/(shell uname -r)/build
PWD :=(shell pwd)
obj-m := hello.o
all:
        make -C (KDIR) M=(PWD) modules
clean:
        make -C (KDIR) M=(PWD) clean


执行make命令就会看到生成一个hello.ko的文件:
执行加载:

insmod hello.ko


可以输入如下命令查看模块是否已经加载:
 

lsmod | grep hello

此时因为是调试级别,所以你查看/var/log/messages文件时,会看不到有消息输出。

此时牵涉到的知识有内核日志级别优先级问题了,网上查了下资料,其日志级别宏定义如下:

printk有8个loglevel,定义在<linux/kernel.h>中:
#define KERN_EMERG          "<0>"    /* system is unusable */
#define KERN_ALERT          "<1>"    /* action must be taken immediately */
#define KERN_CRIT             "<2>"    /* critical conditions */
#define KERN_ERR               "<3>"    /* error conditions */
#define KERN_WARNING      "<4>"    /* warning conditions */
#define KERN_NOTICE         "<5>"    /* normal but significant condition */
#define KERN_INFO             "<6>"    /* informational */
#define KERN_DEBUG          "<7>"    /* debug-level messages */

只有当printk打印信息时的loglevel小于console loglevel的值(优先级高于console loglevel),这些信息才会被打印到console上。

改变console loglevel的方法有如下几种:

1.启动时Kernel boot option:loglevel=level

2.运行时Runtime: dmesg -n level

(注意:demsg -n level 改变的是console上的loglevel,dmesg命令仍然会打印出所有级别的系统信息。)

3.运行时Runtime: echo $level > /proc/sys/kernel/printk

此时再次查看dmesg信息

dmesg | tail -n 2

可以采用以下命令执行卸载:
 

rmmod hello

 

  • 微信扫码赞助
  • weinxin
  • 支付宝赞助
  • weinxin

发表评论

您必须才能发表评论!

目前评论:3   其中:访客  0   博主  0

    • 匿名

      人家说了啊,要用纯文本发送邮件,不能用html格式发送

      • 匿名

        請問你怎麼用gmail 加入的 我用gmail都會被reject 因為HTML format

        • lslzhang

          学习了,顶果子!