Simple examples of Linux module modules compilation steps (RPM)

Source: Internet
Author: User
Tags dmesg

Note: The original blog is more detailed and meticulous, so go to the local--aarongao

--------------------------------------------------------------------------------------------------------------- ----------------------------

This article will direct the compilation of the modules that take you into Linux. Of course, in the process of introduction, I will also add some necessary comments, so that beginners can read. The reason to write this article, mainly because from books to school, it may take longer to learn the whole process, because reading is a learning process, and I this article more like a training. So practicality and summative are stronger. Through this article you will learn the basics of compiling a module and a module makefile. and loading (unloading) modules to view some knowledge of system messages;

Disclaimer: This article is written for beginners, if you are already a Linux module compiler master, also please correct my article errors and shortcomings, thank you

First step: Prepare the module source code


First, we're going to write a module file that matches the Linux format so we can start our module compilation. Suppose we have a source file MYMOD.C. Its source code is as follows:

Mymod.c

1#include <linux/module.h>/*introduce module-related macros*/  2#include <linux/init.h>/*Introducing the Module_init () module_exit () function*/  3#include <linux/moduleparam.h>/*introduction of Module_param ()*/  4Module_author ("Yu Qiang"); 5Module_license ("GPL"); 6 Static intNBR =Ten; 7 8Module_param (NBR,int, S_irugo);/*incoming parameters when loading modules*/9   Ten Static int__init Yuer_init (void)/*automatically executes when the module is loaded*/ One {   A     inti;  -      for(i=0; i<nbr; i++)   -     {   thePRINTK (Kern_alert"Hello, how is you.%d\n", i);  -     }   -     return 0;  - }   +    - Static void__exit Yuer_exit (void)   + {   APRINTK (Kern_alert"I come from Yuer ' s module, I has been unlad.\n");  at }   - Module_init (Yuer_init);  -Module_exit (Yuer_exit);

Our source files are almost ready, this is the basic structure of a Linux module.

The 8th line is to export our symbolic variable, NBR, so that you can dynamically modify the value of the variable when you load the module, which will be shown later.

Line 10th, the Yuer_init () function will run when the module loads, and the result of the output can see if our module is successfully loaded.


Step two: Write the makefile file



Let's start by looking at the source files of our makefile, and then we'll explain;

Makefile

  1  obj-m: = MODULES.O #要生成的模块名   2  modules-objs:= MYMOD.O #生成这个模块名所需要的 Target file    4  kdir: =/lib/modules/' Uname-r '/build   5  PWD: = $ (shell pwd)   6   7  default  :   8  make-c $ (kdir) m=< Span style= "color: #000000;" >$ (PWD) modules   9  10  clean:  11  rm-rf *.o. *. cmd *.ko *.mod.c. Tmp_versions 

Now let me explain this makefile. Keep in mind that makefile is uppercase rather than lowercase makefile;
OBJ-M: This variable is to specify which modules you want to claim, that is, module name , module format is obj-m: = < module name >.O, a directory can have multiple obj-m module files, see kernel source
MODULES-OBJS: This variable is a description of the target file modules required by the module, format requirements < module name >-OBJS: = < destination file >
Remember : The module name cannot be the same as the target file name. In this case, the module name can not be taken into mymod;
Kdir: This is the operating system kernel compile directory that we are running, that is, the environment for compiling the module, note that this is not the kernel source directory of the target board, but the directory of the operating system for compiling
m=: Specify the location of our source files
PWD: This is the current working path $ (shell pwd) is a built-in function of make. Used to execute shell commands.
Default: The first target is the one that is executed in makefile

Step three: Compile the module



Now we have prepared the source files we need and the corresponding makefile. We can compile it now.

Enter the source file directory at the end of the
Operation Result:

1make[1]: Entering directory '/usr/src/linux-headers-2.6. -- --generic'  2CC [M]/home/yuqiang/Desktop/mymodule/MYMODULES.O3LD [M]/home/yuqiang/Desktop/mymodule/MODULES.O4Building modules, Stage2. 5Modpost1Modules6cc/home/yuqiang/Desktop/mymodule/MODULES.MOD.O7LD [M]/home/yuqiang/Desktop/mymodule/Modules.ko8make[1]: Leaving directory '/usr/src/linux-headers-2.6. -- --generic'  


Fourth step: Load/Unload our modules



From the compilation above I can see. There is already a modules.ko generated. This is our module. Now we're ready to load it.
First in terminal input:sudo insmod modules.ko
Now let's see if our module has been successfully loaded.
In Terminal input: DMESG | Tail-12 This is the meaning of viewing the kernel output information. Tail-12 shows the last 12 articles;
The results appear as follows:

1[17945.024417] SD9:0:0:0: Attached SCSI generic SG2 type0  2[18046.790019] usb5-8: USB Disconnect, address9  3[19934.224812] Hello, how is is you.0  4[19934.224817] Hello, how is is you.1  5[19934.224818] Hello, how is is you.2  6[19934.224820] Hello, how is is you.3  7[19934.224821] Hello, how is is you.4  8[19934.224822] Hello, how is is you.5  9[19934.224824] Hello, how is is you.6  Ten[19934.224825] Hello, how is is you.7   One[19934.224826] Hello, how is is you.8   A[19934.224828] Hello, how is is you.9  



See it. The initialization function of our module yuer_init () has been successfully run. Indicates that our module has been loaded successfully;
Now let's try uninstalling the module again.
In terminal input:sudo rmmod modules
In terminal input:DMESG | tail-3

1 [19934.2248268  2 [19934.2248289  3 [  20412.046932 from Yuer's module, I has been unlad.  



As you can see from the printed information, our module's exit function has been executed. This means that our module has been successfully uninstalled. To the current position, we are already the compilation of the module to the compilation run is a general understanding of the. For the future in-depth study should be a little help. Below we will look at some simple operations related to the module.


Fifth step: Pass parameters when loading modules


In terminal input:sudo insmod module_name.ko nbr=4
In terminal input:DMESG | tail-6
The results appear as follows:

 1  [20800.655694 ] Hello, how is you. 9  2  [] I come  OneFile module,  I have been unlad.  3  [21334.425373 ] Hello, how is You. 0  4  [] Hello, how is you. 1  5  [] Hello, how is you. 2  6  [] Hello, how is you. 3  

This allows us to see the dynamic setting of one of our variables when the module is loaded. The loop in the initialization function executes only 4 times.
Maybe you'll ask me how I know a module can set those variables. Of course, you can load the variables without setting them up. You can then enter ls/sys/module/<modules_name>/parameters/in the terminal to view it. That's what we're typing here.
In terminal input:ls/sys/moedle/modules/parameters/
Show Results:

1 NBR

If our module is loaded successfully. Finally, we can also view our module information through modinfo . As follows
In terminal input:sudo modinfo modules.ko
Show Results:

1 filename:       modules.ko  2License:        GPL  3Author:         Yu Qiang  4srcversion:     20e9c3c4e02d130e6e92533  5depends:           6 vermagic:       2.6.  586   7 parm:           NBR:int  

This article summarizes:


The relevant knowledge of this article seems to have a little taste. Because this article mainly through a line way to explain the module of the relevant process of writing, in fact, there are many areas in the process can be divergent. For example:
When writing to Module_author ("Yu Qiang"), you should think of
Module_description (simple description of the use of the module);
Module_version (Version string of the module);
Module_alias (alias of the module);
...

When writing to Module_param (NBR, int, s_irugo), you should think of something else.
Export_symbol (name); The function of the module can be exported, which is also the final purpose of the module writing
...

When it comes to Insmod and Modinfo. You should think of something else.
Depmod analysis can load the dependencies of the module and generate MODULES.DEP files and mapping files
Modprobe Linux kernel Add Delete module
...

If you want to become a professional Linux module developers, but also to go a lot of ways to see you, I wish you all good learning.

I am not a Linux related expert, I am just a common Linux related developers (in reading), my purpose is very simple, is to introduce a method, I hope you do not go too many detours in the study. Due to the limitations of personal ability inevitably there are errors and shortcomings, so please do not mind, if found, please ask, I immediately modify. Easy for everyone to learn

Simple examples of Linux module modules compilation steps (RPM)

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.