4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Linux kernel module の作成

Last updated at Posted at 2014-05-03

カーネルモジュールを作る際の基本的なメモ.
Ubuntu precise 環境だけれど,今のところ 2.6.x/3.x.x 系ならどのディストリビューションでも根本は同じはず.きっと.

環境
% lsb_release -dcs | tr "\n" ' ' #=> Ubuntu 12.04 LTS precise
% uname -rm                      #=> 3.2.0-27-generic x86_64
% dpkg -l | grep linux-headers   #=> ii  linux-headers-3.2.0-27-generic  3.2.0-27.43  ...
% WORK_DIR=~/work/mod_oppai
% mkdir -p $WORK_DIR && cd $WORK_DIR && pwd  #=> ~/work/mod_oppai
oppai_module.c
#include <linux/module.h>
#include <linux/kernel.h>

MODULE_DESCRIPTION("Test Module");
MODULE_AUTHOR("trashsuite");
MODULE_LICENSE("MIT");

static int oppai_init_module(void)
{
    int error = 0;

    printk("oppai is loaded!");

    if(error) return -ENODEV;

    return 0;
}

static void oppai_cleanup_module(void)
{
    printk("oppai is unloaded!");
}

module_init(oppai_init_module);
module_exit(oppai_cleanup_module);
Makefile
KERNEL_DIR = /lib/modules/$(shell uname -r)/build
BUILD_DIR := $(shell pwd)
VERBOSE   := 0

obj-m      := oppai.o
oppai-objs := oppai_module.o

all:
	make -C $(KERNEL_DIR) SUBDIRS=$(BUILD_DIR) KBUILD_VERBOSE=$(VERBOSE) modules

clean:
	rm -f  *.o *.ko *.mod.c *.symvers *.order .oppai*
	rm -fr .tmp_versions
ビルド・ロード・リムーブ
% make
% modinfo oppai.ko
filename:       oppai.ko
license:        MIT
author:         trashsuite
description:    Test Module
srcversion:     93045A021979E5BA3ED10F6
depends:        
vermagic:       3.2.0-27-generic SMP mod_unload modversions
%
% sudo insmod oppai.ko
% lsmod | grep oppai #=> oppai 12513  0
% sudo rmmod oppai
%
% dmesg | grep oppai
[2566390.630576] oppai: module license 'MIT' taints kernel.
[2566889.643683] oppai is loaded!
[2566893.356545] oppai is unloaded!

'oppai: module license 'MIT' taints kernel.'
意訳: なんで GPL じゃあないんだ,あぁん?汚ぇモジュールだなぁおい!…まぁいい… ksg!

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?