writing, inserting, displaying and uninstalling of Linux kernel modules

Source: Internet
Author: User
Tags dmesg

Environment: Ubuntu 8.04

Kernel version: 2.6.32.59-debug

1. Writing Documents HELLO.C

#include <linux/init.h>

#include <linux/kernel.h>//PRINTK

/* Write kernel driver must load this header file, the role is to dynamically load the module into the kernel, commonly used macro definitions such as Module_licesence (), Module_author (), and so on in this file */

#include <linux/module.h>

Module_license ("Dual BSD/GPL"); Is the statement that must be selected, which is the license permission for the module, and if not declared, a warning message appears.

static int hello_init (void)

{

PRINTK ("Hello, world – this is the kernel speaking\n"); PRINTK: The difference from Printfde is that it runs in the kernel state and has a message-level mechanism

return 0;

}

static void Hello_exit (void)

{

PRINTK ("Short are the life of a kernel module\n");

}

Module_init (Hello_init); The entry point is no longer the main () function, but should be the Module_init (function name)

Module_exit (Hello_exit); The unload point of the module is specified as the Hello_exit function pointer

2, write Makefile

obj-m:=hello.o

Kernelbuild: =/lib/modules/2.6.32.59-debug//This makefile relies on the Linux kernel source path, if cross-compiling, take the code path running on the Development Board

All

Make-c $ (kernelbuild) m=$ (shell pwd) modules//to the directory where the Linux source is located executes the main makefile and the current path to the main makefile, telling the main makefile to return to the current directory after execution, Executive Makefile

Echo Insmod./hello.ko to turn it on

Clean

RM-RF *.o *.ko *.mod.c *order *.symvers. tmp_versions

3, in the HELLO.C directory make, so the current directory appears Hello.ko

4, Insmod Hello.ko

5. Dmesg-c View kernel information (generated by the fourth step)

6, Lsmod | grep "Hello" command can see Hello kernel module If fourth step is correct

7. Rmmod Hello//delete kernel module Hello.ko

8. Dmesg-c View the kernel information (generated by the seventh step).

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.