カーネルモジュールことはじめ

見ての通りなので細かい説明しません、ごめんなさい(ちょー手抜き!

hello.c

#include <linux/module.h>

static void __exit hello_exit(void)
{
  printk("hello kernel module unload\n");
}

static int __init hello_init(void)
{
  printk("hello kernel module!\n");
  return 0;
}

module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");

Makefile

obj-m:= hello.o
all: hello.ko
hello.ko: hello.c
        make -C /usr/src/linux M=`pwd` V=1 modules
clean:
        make -C /usr/src/linux -r M=`pwd` V=1 clean

実行結果

# make
# insmod ./hello.ko
# tail /var/log/kern.log
May 28 14:18:53 YASUI01 kernel: hello kernel module!
# lsmod
Module                  Size  Used by
hello                   1664  0
# rmmod hello
# tail /var/log/kern.log
May 28 14:18:53 YASUI01 kernel: hello kernel module!
May 28 14:19:20 YASUI01 kernel: hello kernel module unload