商务合作加Q:411239339

Linux kernel task 1:Hello world

浏览:1049次阅读
3 条评论

共计 2760 个字符,预计需要花费 7 分钟才能阅读完成。

开始这个计划源于一个 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

 

正文完
扫码赞助
post-qrcode
 0
果子
版权声明:本站原创文章,由 果子 于2014-05-04发表,共计2760字。
转载说明:除特殊说明外本站文章皆由果较瘦原创发布,转载请注明出处。
评论(3 条评论)
匿名
2015-01-13 11:43:45 回复

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

   Denglu  中国湖南省长沙市电信
匿名
2015-01-13 11:43:45 回复

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

   Denglu  中国台湾省台中
2014-05-05 10:50:14 回复

学习了,顶果子!

 Windows  Firefox  中国上海上海市电信