Kernel module learning-compile your first kernel module hello_module

Source: Internet
Author: User


1. Download the source code
Xin @ Xin-desktop :~ $ Sudo apt-Get source linux-image-2.6.32-38-generic
Final installation in Directory:/usr/src/linux-2.6.32/
2. Compile the source code hello_module.c and makefile.
(1) create a directory named mytest.
Xin @ Xin-desktop :~ /Mytest $ VI hello_module.c

# Include <Linux/init. h>/* printk () */# include <Linux/module. h>/* _ init _ exit */static int _ init hello_init (void)/* when loading a function by using the insmod command, automatically run the */{printk (kern_info "Hello world enter \ n"); Return 0;} static void _ exit hello_exit (void)/* module to uninstall the function, when the rmmod command is run, the system will automatically execute */{printk (kern_info "Hello World exit \ n");} module_init (hello_init); module_exit (hello_exit ); module_author ("Xin");/* module author, optional */module_license ("dual BSD/GPL");/* module license, which describes the permission of the kernel module, required */module_description ("a simple hello World module");/* module description, optional */


(2) Write a makefile file:
The content is as follows:

# Target file obj-M: = hello_module.o # When the module contains multiple files, add this sentence # module-objs: = file1.o file. O # Replace the kernel path with your own kernel path based on the actual situation. Replace the embedded path with the embedded path. Specify the PC path> path kdir of the PC: =/usr/src/linux-2.6.32/# Module source file path PWD: = $ (shell PWD) All: $ (make)-C $ (kdir) subdirs = $ (PWD) modules @ rM-RF *. mod. * @ rM-RF. *. CMD @ rM-RF *. o @ rM-RF module. * clean: Rm-RF *. ko



3. Compile
Xin @ Xin-desktop :~ /Mytest $ make
Make: Entering directory '/usr/src/linux-2.6.32'

Error: Kernel configuration is invalid.
Include/Linux/Autoconf. h or include/config/auto. conf are missing.
Run 'make oldconfig & make prepare' on Kernel SRC to fix it.

Warning: Symbol version dump/usr/src/linux-2.6.32/module. symvers
Is missing; modules will have no dependencies and modversions.

........................................
........................................

Solution:
Xin @ Xin-desktop:/usr/src/linux-2.6.32 $ sudo make oldconfig
Xin @ Xin-desktop: // usr/src/linux-2.6.32 $ sudo make prepare

Re-compile:
Xin @ Xin-desktop :~ /Mytest $ make
Still wrong:
Make: Entering directory '/usr/src/linux-2.6.32'

Warning: Symbol version dump/usr/src/linux-2.6.32/module. symvers
Is missing; modules will have no dependencies and modversions.

CC [m]/home/Xin/mytest/hello_module.o
Building modules, stage 2.
Modpost 1 modules
/Bin/sh: scripts/MoD/modpost: not found
Make [1]: *** [_ modpost] Error 127
Make: *** [modules] Error 2
Make: Leaving directory '/usr/src/linux-2.6.32'

If the prompt says no scripts/MoD/modpost is displayed, compile it.
Xin @ Xin-desktop: // usr/src/linux-2.6.32 $ make scripts
Hostcc scripts/genksyms. o
Shipped scripts/genksyms/lex. c
Shipped scripts/genksyms/parse. h
Shipped scripts/genksyms/keywords. c
Hostcc scripts/genksyms/lex. o
Shipped scripts/genksyms/parse. c
Hostcc scripts/genksyms/parse. o
Hostld scripts/genksyms
CC scripts/MoD/empty. o
Hostcc scripts/MoD/mk_elfconfig
Mkelf scripts/MoD/elfconfig. h
Hostcc scripts/MoD/file2alias. o
Hostcc scripts/MoD/modpost. o
Hostcc scripts/MoD/sumversion. o
Hostld scripts/MoD/modpost
Hostcc scripts/kallsyms
Hostcc scripts/conmakehash

OK.

Then we solve this warning problem (we didn't pay attention to it before, but we found it was still fatal later ):
Warning: Symbol version dump/usr/src/linux-2.6.32/module. symvers
Is missing; modules will have no dependencies and modversions.

Cause: Normally, the header core package does not contain the module. symvers file. To obtain this file, you can only download the header file of the same version of the core.
Apt-Get install !!!, This module. symvers is available only in the downloaded header file. Copy module. symvers to the core package.
Then re-compile the driver: Xin @ Xin-desktop :~ /Mytest $ make
This will solve the issue of warning and version mismatch.
(I don't know how many people will encounter such a problem. Anyway, no one in Google can understand this problem)

4. Load modules
Xin @ Xin-desktop :~ /Mytest $ ls
Hello_module.c modules. Order hello_module.ko makefile
Xin @ Xin-desktop :~ /Mytest $ sudo insmod hello_module.ko
Xin @ Xin-desktop :~ /Mytest $ lsmod | grep hello_module
Hello_module 593 0

PS:
The above warning can be compiled successfully. However, after the load is executed
Xin @ Xin-desktop :~ /Mytest $ sudo insmod hello_module.ko
Error: insmod: Error inserting 'hellomod. ko':-1 invalid module format
I checked a lot of information. The general point is:
The reason why the kernel cannot load the module is that the string that records the version number (formal statement: Version stamp) is different from that of the currently running kernel module, called vermagic.
In this case, you can use sudo tail/var/log/messages
You should see a prompt similar to the following in the last line:
Dec 19 13:42:29 localhost kernel: hellomod: Version magic '2. 6.24.2 SMP mod_unload 686 4 kstacks 'shoshould be' 2. 6.27.7-134. fc10.i686 SMP mod_unload 686 4 kstacks'
And I only show:
Mar 29 16:57:34 Xin-desktop kernel: [6964.464931] hello_module: No symbol version for module_layout
However, the comparison between modinfo and uname-R is indeed different.

I modified makefie in the source directory according to the provided method.
Change the value of makefile line 1-4 to the same value as the current kernel.
Version = 2
Patchlevel = 6
Sublevel = 32
Extraversion =. 54 + drm33.21
Name = man-eating seals of antiquity

Determine the value of your current kernel through
VI/lib/modules/'uname-R'/build/makefile

However, after this change, I still cannot load the above, and the version number is not the same as that written into makefile. The compilation is completed as follows:
2.6.32.57 + drm33.23 SMP mod_unload modversions 586.
Even if I changed the makefile in the/usr/src/linux-2.6.32 kernel source code, this is also the case, I have not figured out where the kernel version is set.
Finally, after this warning is eliminated, although the kernel version is still different, it can be loaded.
Video information of the currently loaded kernel module:
Xin @ Xin-desktop :~ /Mytest $ modinfo video
Filename:/lib/modules/2.6.32-38-generic/kernel/Drivers/ACPI/video. Ko
License: GPL
Description: ACPI video driver
Author: Bruno ducrot
Srcversion: a52e70043443277d9757c8e
Alias: ACPI *: lnxvideo :*
Depends: Output
Vermagic: 2.6.32-38-generic SMP mod_unload modversions 586
Parm: brightness_switch_enabled: bool

Hello_module information:
Xin @ Xin-desktop :~ /Mytest $ modinfo hello_module.ko
Filename: hello_module.ko
Alias: a simplest Module
Description: a simple hello World module
License: Dual BSD/GPL
Author: Xin
Srcversion: b757e1eead29f16bf0b6737
Depends:
Vermagic: 2.6.32.57 + drm33.23 SMP mod_unload modversions 586

The vermagic of the two is different, but the hello_module.ko module can be loaded.

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.