# linux Kernel Learning 10 - Start with the first kernel module

Posted by iFlex on Sat, 31 Aug 2019 14:37:37 +0200

linux Kernel Learning 10 - Start with the first kernel module

1. Preparations

  1. c File
#include <linux/init.h>
#include <linux/module.h>

// The kernel module initialization function can load a kernel module through the insmod command
static int __init my_test_init(void){
    printk("my first kernel module init\n");
    return 0;

// The kernel module exits the function and can uninstall a kernel module using the rmmod command
static void __exit my_test_exit(void){

//Kernel entry my_test_init()
//my_test-exit() when the kernel exits the exit function of this module

// Use the accepted software license agreement
// Author information describing the module
// Briefly describe the purpose or function of the module
//Provide an appropriate alias for the user control
  1. makefile file file
  1 BASEINCLUDE ?=/lib/modules/`uname -r`/build
  3 mytest-objs  :=my_test.o
  4 obj-m  :=mytest.o
  6 all :
  7         $(MAKE) -C      $(BASEINCLUDE)  M=$(PWD)        modules;
  8 clean:
  9         $(MAKE)  -C $(BASEINCLUDE)  SUBDIRS=$(PWD) clean;
 10         rm -f *.ko;

Third line
<Module Name>-objs: =<Target File>.o
Line 4
Obj-m: =<module name>.o

Lines 6-7 indicate the action to be compiled for execution.
Lines 8-10 indicate the action required to perform make clean
Then type make command to compile

$ make
  1. View Kernel Information
$ uname -r

$ cd /lib/modules/4.15.0-58-generic 
$ ls -l

total 5300
drwxr-xr-x  2 root root    4096 Aug  6 03:45 initrd
drwxr-xr-x 16 root root    4096 Aug 21 02:36 kernel
-rw-r--r--  1 root root 1269888 Aug 21 02:36 modules.alias
-rw-r--r--  1 root root 1250246 Aug 21 02:36 modules.alias.bin
-rw-r--r--  1 root root    7629 Aug  6 03:45 modules.builtin
-rw-r--r--  1 root root    9685 Aug 21 02:36 modules.builtin.bin
-rw-r--r--  1 root root  551723 Aug 21 02:36 modules.dep
-rw-r--r--  1 root root  780064 Aug 21 02:36 modules.dep.bin
-rw-r--r--  1 root root     317 Aug 21 02:36 modules.devname
-rw-r--r--  1 root root  206075 Aug  6 03:45 modules.order
-rw-r--r--  1 root root     540 Aug 21 02:36 modules.softdep
-rw-r--r--  1 root root  590768 Aug 21 02:36 modules.symbols
-rw-r--r--  1 root root  720722 Aug 21 02:36 modules.symbols.bin
drwxr-xr-x  3 root root    4096 Aug 21 02:36 vdso

  1. Results after compilation
$ ls 
Makefile       Module.symvers  mytest.ko     mytest.mod.o  mytest.o
modules.order  my_test.c       mytest.mod.c  my_test.o

You can check that the compiled module is correct by using the file command.You can see the ELF file for the programming x86-64 architecture, which is already successful

$ file mytest.ko
mytest.ko: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), BuildID[sha1]=eb0017daac8924d6450529c21430501d0cbefa7e, not stripped

Further checks can also be made with modinfo

$ modinfo mytest.ko
filename:       /home/groot/test/mytest.ko
alias:          mytest
description:    MY TEST KERNEL MODULE
author:         xiao
license:        GPL
srcversion:     C4ABC60E9EB1421D8527091
retpoline:      Y
name:           mytest
vermagic:       5.0.0-25-generic SMP mod_unload 

  1. Validation module
$ sudo insmod mytest.ko
$dmesg |grep first

View Kernel Print Log

[39568.716952] my first kernel module init

You can also use the lsmod command to see if the current mytest module has been loaded into the system

Module                  Size  Used by
mytest                 16384  0  //This is the custom touch module

Once the module is loaded, a new directory is created in the sys/modules directory, such as a directory named mytest for the mytest module.

$ sudo tree -a             
├── coresize
├── holders
├── initsize
├── initstate
├── notes
│   └── .note.gnu.build-id
├── refcnt
├── sections
│   ├── .exit.text
│   ├── .gnu.linkonce.this_module
│   ├── .init.text
│   ├── __mcount_loc
│   ├── .note.gnu.build-id
│   ├── .note.Linux
│   ├── .rodata.str1.1
│   ├── .strtab
│   └── .symtab
├── srcversion
├── taint
└── uevent

3 directories, 17 files
  1. Unload module
$ sudo  rmmod mytest.ko

3. Summary

  1. Module Load Function: When a module is loaded, the function is automatically executed first, usually with some initialization.
  2. Module unload function: When a module is unloaded, the function is also automatically executed, performing some cleanup.
  3. Block License Statement: Kernel modules must declare licenses or the kernel will issue a pollution warning.
  4. Module parameters: optional, added as required
  5. Module Author and Description Statement: This information generally needs to be refined
  6. Module export symbols: add as needed, optional

4. Other

The process here refers to the introductory chapter of Run Bar linux Kernel.If you want to learn the linux kernel, you can buy the introductory chapter of the linux kernel and the linux kernel of the running bar.
Here's the stuff for the author's uncle github address Here's the linux kernel that your dumb uncle taught you.Here's his video tutorial Video Address Kernel Address

Topics: Linux sudo Makefile Programming