Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Ubuntu compiling Kernel Module and content embodiment system Log

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about Ubuntu compiling kernel modules and sample analysis in the Syslog. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1.Linux login interface

1. Check the current file directory:

After connecting to the Linux system through Xshell

Enter command: ls

two。 Create a new code/kernel folder

two。 Write code

1. Create hello_module.c

Instruction: vim hello_module.c

two。 Press I to enter edit mode and enter the following code

/ / hello_module.c # include # include # include 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")

The above code is explained as follows:

(1) # include: must. The module.h header file contains the structural definition of the module and the version control of the module. Any module program should include this header file.

(2) # include: kernel.h contains commonly used kernel functions, such as the printk () function in the above program

(3) # include: must. Init.h contains declarations for the module_init () and module_exit () functions

(4) module_init (): must. Module load function, load module the function is executed automatically and initializes the operation

(5) module_exit (): must. The module unloads the function, and when the module is unloaded, the function executes automatically and cleans up the operation; (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 have the obligation to disclose the modified source code. This is a mandatory open source protocol, so generally writing driver code needs to show the declaration and follow this agreement, otherwise the kernel UI will issue a warning of contamination.

(7) MODULE_AUTHOR (): describe the author information of the module

(8) MODULE_DESCRIPTION (): briefly describe the purpose and function of the module.

(9) MODULE_ALIAS (): alias provided for the user control

(10) printk (): kernel output function that prints the contents of the system file "/ var/log/kern.log" by default.

3. Save the exit and observe the file directory

Press the button ESC

: wq

3. Write Makefile files

Vim Makefile

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

The above code is explained as follows:

(1) obj-m: = .o: define the name of the module to be generated

(2) KERNELBUILD: = / lib/modules/$ (shell uname-r) / build: KERNELBUILD is a custom name and is used to point to the kernel compilation directory where Linux is running, where the "uname-r" logo shows the corresponding kernel version

(3) CURRENT_PATH: = $(shell pwd): CURRENT_PATH is a custom name and is used to point to the current directory

(4) all: actions performed by compilation

(5) the action required by clean:zhixing make clean. "make clean" is used to clear object files (files with the suffix ".o") and executable files generated by the last make command.

Wq save exit

4. Compile:

Input instruction: make

You can see the compiled file

Check the compiler module

You can also check further through the modinfo command:

5. Insert module

Insert the module through the insmod command, and after the insertion is completed, you can check whether the current module has been loaded into the system through the lsmod command:

The first one is.

After the system loads the module, a new directory named by the module name is also created under the "/ sys/module" directory:

6. View log output

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

Thank you for reading! This is the end of this article on "Ubuntu compiling kernel modules and sample analysis in the system log". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report