Ubuntu compiles kernel modules, and the content is reflected in the system log

Ubuntu compiles kernel modules, and the content is reflected in the system log

1.Linux login interface

1. Check the current file directory:

After connecting to the Linux system through Xshell

Enter the command: ls

insert image description here

2. Create a new code/kernel folder

insert image description here

2. Write code

1. Create hello_module.c

Command: vim hello_module.c

2. Press i to enter edit mode and enter the following code

 //hello_module.c #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> ​ static int __init hello_init(void){ printk("This is hello_module, welcome to Linux kernel \n"); return 0; } ​ static void __exit hello_exit(void){ printk("see you next time!\n"); } ​ module_init(hello_init); module_exit(hello_exit); ​ MODULE_LICENSE("GPL"); MODULE_AUTHOR("Mr Q"); MODULE_DESCRIPTION("hello kernel module"); MODULE_ALIAS("hello");

insert image description here

The above code is explained as follows:
(1) #include <linux/module.h>: Required. The module.h header file contains the structure definition of the module and the version control of the module. Any module program must include this header file;
(2) #include <linux/kernel.h>: kernel.h contains commonly used kernel functions, such as the printk() function in the above program;
(3) #include <linux/init.h>: Required. init.h contains declarations of module_init() and module_exit() functions;
(4) module_init(): required. Module loading function, when loading a module, this function is automatically executed to perform initialization operations;
(5) module_exit(): required. Module uninstallation function, which is automatically executed when the module is uninstalled to perform cleanup operations; (6) MODULE_LICENSE(): indicates the software license agreement accepted by the module code. The Linux kernel is an open source project using GPL V2, which requires all individuals or organizations that use and modify the Linux kernel code to make the modified source code public. This is a mandatory open source agreement, so generally, when writing driver code, it is necessary to explicitly declare and follow this agreement, otherwise the kernel UI will issue a contaminated warning;
(7) MODULE_AUTHOR(): describes the author information of the module;
(8) MODULE_DESCRIPTION(): briefly describe the purpose and function of the module;
(9) MODULE_ALIAS(): Alias ​​provided for user controls;
(10) printk(): kernel output function, which prints the contents of the system file "/var/log/kern.log" by default.

3. Save and exit, and press ESC to view the file directory;
:wq

insert image description here

3. Write the Makefile

vim Makefile

 obj-m := hello_module.o
 ​
 KERNELBUILD := /lib/modules/$(shell uname -r)/build
 CURRENT_PATH := $(shell pwd)
 ​
 all:
     make -C $(KERNELBUILD) M=$(CURRENT_PATH) modules
 ​
 clean:
         make -C $(KERNELBUILD) M=$(CURRENT_PATH) clean

The above code is explained as follows:
(1) obj-m := <module name>.o: defines the name of the module to be generated (2) KERNELBUILD := /lib/modules/$(shell uname -r)/build: KERNELBUILD is a custom name used to point to the kernel compilation directory of the running Linux, where the "uname -r" identifier shows the corresponding kernel version;
(3) CURRENT_PATH := $(shell pwd): CURRENT_PATH is a custom name used to point to the current directory;
(4) all: actions executed by compilation (5) clean: actions required by make clean. “make clean” is used to clear the object files (files with the suffix “.o”) and executable files generated by the last make command.

:wq save and exit

4. Compile:

Enter the command: make

insert image description here

You can see the compiled files

insert image description here

Check compiled modules

insert image description here

You can also use the modinfo command to further check:

insert image description here

5. Insert module

Insert the module using the insmod command. After the insertion is complete, you can use the lsmod command to check whether the current module has been loaded into the system:

insert image description here

The first one is.

After the system loads the module, it will also create a new directory named after the module under the "/sys/module" directory:

insert image description here

6. View log output

Since prink() uses the default output level in this demonstration, you can view the output results through the "dmesg" or "tail /var/log/kern.log" command.

insert image description here

This is the end of this article about Ubuntu compiling kernel modules and the content reflected in the system log. For more relevant content about Ubuntu compiling kernel modules, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the implementation process of building a kernel tree in Ubuntu 12.04
  • Ubuntu 16.04 kernel upgrade steps

<<:  Concat() of combined fields in MySQL

>>:  Using CSS3 and JavaScript to develop web color picker example code

Recommend

Detailed explanation of Nginx regular expressions

Nginx (engine x) is a high-performance HTTP and r...

How to check PCIe version and speed in Linux

PCIE has four different specifications. Let’s tak...

Vue custom components use event modifiers to step on the pit record

Preface Today, when I was using a self-written co...

vue-pdf realizes online file preview

This article example shares the specific code of ...

Typescript+react to achieve simple drag and drop effects on mobile and PC

This article shares the specific code of typescri...

Super simple implementation of Docker to build a personal blog system

Install Docker Update the yum package to the late...

A detailed introduction to the use of block comments in HTML

Common comments in HTML: <!--XXXXXXXX-->, wh...

Design reference WordPress website building success case

Each of these 16 sites is worth reading carefully,...

Automatic failover of slave nodes in replication architecture in MySQL 8.0.23

I have been in contact with MGR for some time. Wi...

How to perfectly implement the grid layout with intervals on the page

Typical layout examples As shown in the above pic...

MySQL-group-replication configuration steps (recommended)

MySQL-Group-Replication is a new feature develope...

Linux CentOS6.9 installation graphic tutorial under VMware

As a technical novice, I am recording the process...

Implementing parameter jump function in Vue project

Page Description:​ Main page: name —> shisheng...

Example of how to configure the MySQL database timeout setting

Table of contents Preface 1. JDBC timeout setting...