R&D/OS

커널 모듈 개발

sunshout 2013. 4. 22. 18:40

o  커널 모듈 개발

참조: http://www.tldp.org/LDP/lkmpg/2.6/html/index.html

¡ 커널 모듈 프로그래밍 skeleton

 

hello.c 파일

/*

 * hello.c - This is simplest kernel module

 */

#include <linux/module.h>    /* for all modules */

#include <linux/kernel.h>    /* for KERN_INFO   */

#include <linux/init.h>      /* for macro       */

 

#define DRIVER_AUTHOR "Choonho Son <choonho.son@gmail.com>"

#define DRIVER_DESC "Hello driver"

 

static int __init hello_init(void)

{

        printk(KERN_INFO "Hello Kernel module !\n");

        return 0;

}

 

static void __exit hello_exit(void)

{

        printk(KERN_INFO "Goodby Kernel module !\n");

}

 

module_init(hello_init);

module_exit(hello_exit);

 

MODULE_LICENSE(“GPL”);

MODULE_AUTHOR(DRIVER_AUTHOR);

MODULE_DESCRIPTION(DRIVER_DESC);

 

 

__init, __exit 매크로는 드라이버가 kernel built-in driver이거나 loadable module일 때 다르게 동작하기 위해서 사용됨 (ex. 드라이버가 built-in driver 일때 __exitno effect)

MODULE_LICENSE 매크로는 hello: module license 'unspecified' taints kernel 과 같은 메시지를 없애 준다.

 

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

 

모듈 정보 확인(modinfo)

[root@openxen module]# modinfo hello.ko

filename:       hello.ko

description:    Hello driver

author:         Choonho Son <choonho.son@gmail.com>

license:        GPL

srcversion:     2FC7B97F147F4BC48582F08

depends:

vermagic:       3.8.0+ SMP mod_unload modversions