Hello World

ほぼ何もしない "Hello World" プログラムを作って Linux Kernel に組み込む単純なモジュールの作り方を見ていきます。まだ、デバイスドライバには遠いです。

ソースコード

C で実行部を、Makefile で構築手順を書きます。Makefile を書かずにコンパイルできるかというと、自分では試したことがないです。煩雑な記述をした割には得るものが無いでしょう。

実行部

filehello_world.c
Expand allFold all
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 
 
 
-
|
|
|
!
 
-
|
!
 
-
|
|
!
 
-
!
 
 
 
 
 
 
 
#include <linux/kernel.h>
#include <linux/module.h>
 
/*
 * Tutorial hello world module init.
 * @return int == 0: Success. 
 *             < 0:  Fail, negative errno number.
 */
static int __init hello_world_init(void)
{       printk(KERN_INFO "%s: Hello world.\n", __func__);
        return 0;
}
 
/*
 * Tutorial hello world module exit.
 * @return void
 */
static void __exit hello_world_exit(void)
{       printk(KERN_INFO "%s: Goodbye world.\n", __func__);
}
 
module_init(hello_world_init);
module_exit(hello_world_exit);
 
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Tutorial hello world.");
MODULE_AUTHOR("Your name or email address.");

Makefile

fileMakefile
Expand allFold all
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
-
|
|
|
|
|
|
|
|
|
!
 
 
-
!
 
 
 
 
 
 
 
 
-
|
# Build Tutorial hello world module.
# To try this tutorial.
# make
# sudo insmod hello_world.ko
# dmesg
# lsmod
# sudo rmmod hello_world.ko
# dmesg
# lsmod
 
KERNEL_SOURCE_TREE:=/lib/modules/$(shell uname -r)/build
PWD:=$(shell pwd)
 
# specify object.
obj-m:=hello_world.o
 
.PHONY: modules clean # install
modules:
        make -C $(KERNEL_SOURCE_TREE) M=$(PWD) modules
 
clean:
        make -C $(KERNEL_SOURCE_TREE) M=$(PWD) clean
 
#install:
#       make -C $(KERNEL_SOURCE_TREE) M=$(PWD) install

module_init, module_exit

Linux Kernel に動的に組み込むモジュールとして作成する

Makefile を書く

Linux Kernel に組み込む

何も起きない? dmesg で確かめよう

printk は何処と結合しているの?

多重に組み込めない

Hello Friend

パラメータを入力する

エラーを返してみよう


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS