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 Linux Kernel compilation and Development

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail the sample analysis of Linux kernel compilation and development. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

I. introduction to Linux kernel

Linux kernel map:

Linux system architecture:

Linux kernel architecture:

Arm has seven working modes, and x86 also implements four different levels of RING0-RING3,RING0.

So the linux user code runs under RING3 and the kernel runs on RING0, so the system itself gets

Adequate protection

User space (user mode) to kernel space (system mode) method:

System call

Hardware interrupt

Linux kernel architecture:

Virtual file system VFS:

VFS (Virtual File system) hides the details of various file systems and provides a unified interface for file operations.

II. Linux kernel source code

Download www.kernel.org for linux kernel

Directory structure:

Directory after decompression of linux kernel tar

Arch: code divided according to cpu architecture

Block: partial block device driver

Crypto: encryption, compression, CRC check algorithm

Documentation: kernel documentation

Drivers: device driver

Fs (Virtual File system vfs): file system

Include: header files required by the kernel, (platform-independent header files are in include/linux)

Lib: library file code (platform-related)

Mm: implement memory management, independent of hardware architecture (related to hardware architecture in arch)

Net: the code of network protocol

Samples: some examples of kernel programming

Scripts: a script to configure the kernel

Module of security:SElinux

Sound: driver for audio device

Usr:cpio command implementation, which is used to make commands for the root file system (commands that put the file system together with the kernel)

Virt: kernel virtual machine

Linux DOC compilation generates:

Linux source root directory / Documentation/00-INDEX: directory index

Linux source root directory / Documentation/HOWTO: guide

Generate linux kernel help documentation: execute make htmldocs in the linux source root directory (Documentation)

Under ubuntu16, you need to execute the sudo apt-get install xmlto installation plug-in to generate doc documents.

What often needs to be changed in later development is the code in arch,drivers.

III. Linux kernel configuration and compilation

Clean up the file (in the linux source root directory):

Make clean: only clean up all generated files

Make mrproper: clean up all generated files and config configuration files

Make distclean: clean up all generated files and config configuration files, and edit and patch files

Configuration (collect hardware information such as cpu model, network card, etc.):

Make config: interactive configuration based on text mode

Make menuconfig: text-based menu mode (recommended)

Make oldconfig: use the existing .config, but ask for new configuration items

Make xconfig: graphical configuration (graphical system is required)

Configuration method:

1) use make menuconfig operation method:

1 > Press y: compile > Connect > Image File

2 > Press m: to compile

3 > Press n: do nothing

4 > Press "Spacebar": YBM n rotate

After configuration and saving, a .config file is generated in the root directory of the linux source code.

Note: execute apt-get install libncurses5-dev on ubuntu11 to install the support pack

2) make use of existing configuration file templates (.config)

1 > linux source root directory / arch//configs/, rename the corresponding file copy to .config to linux source root directory

2 > copy / boot/config-2.6.18-53.e15 and rename it to .config to the root directory of the linux source code using the currently running files (to be viewed with ls / boot/-a) and then you can use make menuconfig to copy

The .config file has been modified

Compile the kernel:

1) make zImage

2) make bzImage

Difference: on X86 platforms, zimage can only be used for kernels less than 512k

Get detailed compilation information: make zimage version 1 or make bzimage version 1

The compiled kernel is in the arch//boot/ directory

Note: before cp the .config configuration file to the root directory to compile the kernel, you must enter make menuconfig and save the exit (otherwise it won't work)

Compile and install the module:

1) compile kernel module: make modules

2) install kernel module: make modules_install INSTALL_MOD_PATH=/lib/modules

Change the kernel of this machine: copy the compiled kernel module from the kernel source directory to / lib/modules

Make init ramdisk (): enter the execution command mkinitrd initrd-2.6.39 (any) 2.6.39 (available by querying the directory under / lib/modules)

Note:

The mkinitrd command is in redhat, and the command for ubuntu is: mkinitramfs-k / lib/modules/ module installation location-o initrd-2.6.39 (any) 2.6.39 (can be obtained by querying the directory under / lib/modules)

If there is no mkinitramfs command in ubuntu, you can install it with apt-get install initrd-tools.

Install the kernel module:

1) Manual

1 > cp linux root directory / arch/x86/boot/bzImage / boot/mylinux-2.6.39

2 > cp linux root directory / initrd-2.6.39 / boot/initrd-2.6.39

* modify / etc/grub.conf or / etc/lilo.conf file

2) automatic

1 > make install: this command automatically completes the above operation (check the current kernel version: uname-r)

IV. Linux kernel module development

Description:

The linux kernel component is so large that the kernel ximage does not contain a component, but is dynamically added to the running kernel (or can be uninstalled) when the component needs to be used. This mechanism is called the "kernel module" mechanism. Kernel modules are usually compiled by using makefile files

Module installation and uninstallation:

1) load: insmod hello.ko

2) Uninstall: rmmod hello

3) View: lsmod

4) load (automatically find module dependencies): modprobe hello

Modprobe will look at the module to be loaded according to the file / lib/modules/version/modules.dep to see if it depends on other modules. If so, it will find these modules first and load them into the kernel first.

Case analysis:

1) moduleDep/1 (compilation of a module)

1 # include 2 # include 3 4 / / Module entry function 5 / / _ _ init: indicates a sub-segment in a code segment that runs only once and reclaims memory. 6 static int _ init hello_init (void) 7 {8 printk (KERN_EMERG "hello world!\ n"); 9 return 0 position 10} 11 / / Module unload function 12 / / _ _ exit:13 static void _ exit hello_exit (void) 14 {15 printk (KERN_EMERG "hello exit!\ n"); 16} 17 / / Kernel symbolic export function 18 int add_integar (int aMint b) 19 {20 return aint b 21} 22 int sub_integar (int a pencil int b) 23 {24 return a sub_integar b; 25} 26 27 module_init (hello_init); 28 module_exit (hello_exit); 29 / / function export 30 EXPORT_SYMBOL (add_integar); 31 EXPORT_SYMBOL (sub_integar)

Makefile:

# * the KERNELRELEASE execution is empty, so execute the ifneq in the else ($(KERNELRELEASE)) ) obj-m: = hello.o # else block elseKDIR:= / lib/modules/2.6.18-53.el5/build all: # KDIR depends on the kernel module source code path (kernel compilation installation path) # PWD indicates where the kernel code is (current directory) # modules compiles the module make-C $(KDIR) KDIR $(PWD) modules clean: rm-f * .ko * .o * .mod .o * .mod.c * .symvers * .order endif

2) moduleDep/2 (compilation of two modules)

# include # include / / module optional information MODULE_LICENSE ("GPL"); / / license statement MODULE_AUTHOR ("liyuan"); / / author statement MODULE_DESCRIPTION ("This module is a param example."); / / module description MODULE_VERSION ("V1.0"); / / module alias MODULE_ALIAS ("a simple module"); / / module alias / / module parameter static char * name = "liyuan arg"; static int age = 30 / / S_IRUGO is a parameter permission, or you can use the numeric module_param (age,int,S_IRUGO); module_param (name,charp,S_IRUGO); / / use the external file function extern int add (int aline int b); / / declare the external kernel symbol function extern int add_integar (int aline int b); extern int sub_integar (int aline int b) Static int _ init mains_init (void) {/ / Multi-file compilation printk (KERN_EMERG "param hi"); int vle=add (1Magne2); printk (KERN_EMERG "add value:%d\ n", vle); / / Module parameter printk (KERN_EMERG "name:% s\ n", name); printk (KERN_EMERG "age:% d\ n", age) / / using functions of other modules (kernel symbol export) int adds=add_integar (3subs 1); int subs=sub_integar (3jing1); printk (KERN_EMERG "add_integar:% d\ n", adds); printk (KERN_EMERG "sub_integar:% d\ n", subs); return 0;} static void _ exit mains_exit (void) {printk ("param exit!");} module_init (mains_init) 52 module_exit (mains_exit)

Add.c

Int add (int aline int b) {return aheadb;}

Makefile

Ifneq ($(KERNELRELEASE) ) # more than two kernel source files generate a separate kernel module name ma # kernel ma obj-m: = ma.o # the following ma-objs must be preceded by ma ma-objs: = mains.o add.oelseKDIR:= / lib/modules/2.6.18-53.el5/build all: make-C $(KDIR) KDIR $(PWD) modules clean: rm-f * .ko * .o * .mod.o * .mod.c * .symvers * .order endif

Run the module with parameters: insmod hello.ko name=yuan age=12

Kernel symbol export (/ proc/kallsyms records the names and addresses of all exported symbols in the kernel):

The operation of one kernel module depends on the function implementation of another kernel module. You must first run * kernel modules, which requires kernel symbol export.

Note:

Error message: disagrees about version of symbol struct_module insmod:error inserting...

There will be a mismatch between kernel modules when developing kernel modules. Is the linux kernel you are currently running and depends on the compilation link

The kernel version does not match. The solution:

Forcibly insert using modprobe-- force-modversion

You can use uname-r to view the currently running kernel version

Printk kernel printing:

Printk has eight priorities in, decreasing by priority:

KERN_EMERG 0

Used for urgent messages, often those that break down

KERN_ALERT 1

Information that requires immediate action.

KERN_CRIT 2

Serious situation

KERN_ERR 3

Error condition

KERN_WARNING (printk default level) 4

Warning of a problem

KERN_NOTICE 5

Normal, but it's still noteworthy.

KERN_INFO 6

Information message

KERN_DEBUG 7

Used as debug messa

No matter what level it is, it will be printed in / var/log/messages (after messages can be deleted, run the kernel to test kernel printing) console print (priority configuration / proc/sys/kernel/printk)

6 4 1 7

Console_loglevel

Default_message_loglevel

Minimum_console_level

Default_console_loglevel

Errors when installing the 2.6.39 kernel in vm+redhat

Launch the Times could not find filesystem'/ dev/root'

Solution method

a. Select the following corresponding options through make menuconfig

General setup-- >

[*] enable deprecated sysfs features to support old userspace tools

When you are successful, the one below is also *.

b. Modify the .config file

Modify the CONFIG_SYSFS_DEPRECATED_V2 in the .config file to change what was originally commented out

Change CONFIG_SYSFS_DEPRECATED_V2 to CONFIG_SYSFS_DEPRECATED_V2=y

This is the end of this article on "sample Analysis of Linux Kernel compilation and Development". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it 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

Servers

Wechat

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

12
Report