How to Write Your Own Linux Kernel Module with a Simple Example

來源:互聯網
上載者:User
文章目錄
  • 1. lsmod – List Modules that Loaded Already
  • 2. insmod – Insert Module into Kernel
  • 3. modinfo – Display Module Info
  • 4. rmmod – Remove Module from Kernel
  • 5. modprobe – Add or Remove modules from the kernel
  • 1. Installing the linux headers
  • 2. Hello World Module Source Code
  • 3. Create Makefile to Compile Kernel Module
  • 4. Insert or Remove the Sample Kernel Module

What are kernel modules?

Kernel modules are piece of code, that can be loaded and unloaded from kernel on demand.

Kernel modules offers an easy way to extend the functionality of the base kernel without having to rebuild or recompile the kernel again. Most of the drivers are implemented as a Linux kernel modules. When those drivers are not needed, we can unload only
that specific driver, which will reduce the kernel image size.

The kernel modules will have a .ko extension. On a normal linux system, the kernel modules will reside inside /lib/modules/<kernel_version>/kernel/ directory.

Earlier we discussed how to
compile a kernel from the source.

This tutorial explains how to write a Kernel module using a simple Hello World example.

I. Utilities to Manipulate Kernel Modules1. lsmod – List Modules that Loaded Already

lsmod command will list modules that are already loaded in the kernel as shown beblow.

# lsmodModule                  Size  Used byppp_deflate            12806  0zlib_deflate           26445  1 ppp_deflatebsd_comp               12785  0..
2. insmod – Insert Module into Kernel

insmod command will insert a new module into the kernel as shown below.

# insmod /lib/modules/3.5.0-19-generic/kernel/fs/squashfs/squashfs.ko# lsmod | grep "squash"squashfs               35834  0
3. modinfo – Display Module Info

modinfo command will display information about a kernel module as shown below.

# modinfo /lib/modules/3.5.0-19-generic/kernel/fs/squashfs/squashfs.kofilename:       /lib/modules/3.5.0-19-generic/kernel/fs/squashfs/squashfs.kolicense:        GPLauthor:         Phillip Lougherdescription:    squashfs 4.0, a compressed read-only filesystemsrcversion:     89B46A0667BD5F2494C4C72depends:intree:         Yvermagic:       3.5.0-19-generic SMP mod_unload modversions 686
4. rmmod – Remove Module from Kernel

rmmod command will remove a module from the kernel. You cannot remove a module which is already used by any program.

# rmmod squashfs.ko
5. modprobe – Add or Remove modules from the kernel

modprobe is an intelligent command which will load/unload modules based on the dependency between modules. Refer to
modprobe commands for more detailed examples.

II. Write a Simple Hello World Kernel Module1. Installing the linux headers

You need to install the linux-headers-.. first as shown below. Depending on your distro, use apt-get or yum.

# apt-get install build-essential linux-headers-$(uname -r)
2. Hello World Module Source Code

Next, create the following hello.c module in C programming language.

#include <linux/module.h>    // included for all kernel modules#include <linux/kernel.h>    // included for KERN_INFO#include <linux/init.h>      // included for __init and __exit macrosMODULE_LICENSE("GPL");MODULE_AUTHOR("Lakshmanan");MODULE_DESCRIPTION("A Simple Hello World module");static int __init hello_init(void){    printk(KERN_INFO "Hello world!\n");    return 0;    // Non-zero return means that the module couldn't be loaded.}static void __exit hello_cleanup(void){    printk(KERN_INFO "Cleaning up module.\n");}module_init(hello_init);module_exit(hello_cleanup);

Warning: All kernel modules will operate on kernel space, a highly privileged mode. So be careful with what you write in a kernel module.

3. Create Makefile to Compile Kernel Module

The following makefile can be used to compile the above basic hello world kernel module.

obj-m += hello.oall:    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modulesclean:    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Use the make command to compile hello world kernel module as shown below.

# makemake -C /lib/modules/3.5.0-19-generic/build M=/home/lakshmanan/a modulesmake[1]: Entering directory `/usr/src/linux-headers-3.5.0-19-generic'  CC [M]  /home/lakshmanan/a/hello.o  Building modules, stage 2.  MODPOST 1 modules  CC      /home/lakshmanan/a/hello.mod.o  LD [M]  /home/lakshmanan/a/hello.komake[1]: Leaving directory `/usr/src/linux-headers-3.5.0-19-generic'

The above will create hello.ko file, which is our sample Kernel module.

4. Insert or Remove the Sample Kernel Module

Now that we have our hello.ko file, we can insert this module to the kernel by using insmod command as shown below.

# insmod hello.ko# dmesg | tail -1[ 8394.731865] Hello world!# rmmod hello.ko# dmesg | tail -1[ 8707.989819] Cleaning up module.

When a module is inserted into the kernel, the module_init macro will be invoked, which will call the function hello_init. Similarly, when the module is removed with rmmod, module_exit macro will be invoked, which will call the hello_exit. Using dmesg command,
we can see the output from the sample Kernel module.

Please note that printk is a function which is defined in kernel, and it behaves similar to the printf in the IO library. Remember that you cannot use any of the library functions from the kernel module.

Now you have learned the basics to create your own Linux Kernel module.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.