Linux 2.6.xx kernel module programming

Source: Internet
Author: User
From: http://hi.baidu.com/liu_bin0101/blog/item/02ff1afc043443f5fd037f95.html

2.6.xx kernel is very different from 2.4.xx kernel programming. The following text briefly describes how to implement the kernel module (Hello World) in 2.6.xx.

Environment: Ubuntu 704
Kernel: 2.6.20-15-generic, which can be viewed through uname-R
GCC version: 4.1.2


I. Preparations
Required development library for installing Kernel

# Sudo apt-Get install linux-kernel-devel

Install the kernel header file

# Sudo apt-Get install linux-headers-'uname-R'

Of course, tools such as gcc/make are inherently needed.


2. Write the code hello. C.

As follows:

// ------------------ Hello. c -------------------//


# Ifndef _ KERNEL __

# DEFINE _ KERNEL __


# Endif

# Ifndef Module

# Define Module

# Endif


# Include <Linux/init. h>

# Include <Linux/module. h>

# Include <Linux/kernel. h>


Static int hello_init (void)

{

Printk (kern_alert "Hello world! /N ");

Return 0;

}


Static void hello_exit (void)

{

Printk (kern_alert "bye world! /N ");

}


Module_init (hello_init );

Module_exit (hello_exit );


Module_license ("GPL ");

Module_author ("wfj ");


// ------------------- End of Hello. c -------------------//


3. Compile the MAKEFILE file and put it in the same directory as hello. C.

OBJ-M: = Hello. o

Kernelbuild: =/lib/modules/'uname-R'/build

Default:

Make-C $ (kernelbuild) M = $ (shell PWD) Modules

Clean:

Rm-RF *. O *. Ko *. Mod. C. *. cmd. tmp_versions

(Note the required tab in makefile)

Iv. Compilation Module


# Sudo make

Compilation Module

At this time, there will be hello. KO in the folder where hello. C is located. This is the kernel module we need, haha!

# Sudo make clean

Clean up the compilation garbage, and hello. Ko will also be cleared.

4. Insert a module to make it work


# Sudo insmod./Hello. Ko

We can use dmesg to see the generated kernel information, hello World!


# Sudo rmmod./Hello

Use dmesg to view bye world!

This is the simplest module compilation process in 2.6.xx.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.