Linux kernel compilation and development

Source: Internet
Author: User
Tags aliases

I. Introduction to the Linux kernel
Linux kernel Map:

Linux System Architecture:

Linux kernel Architecture:
With 7 modes of operation, the x86 also achieves 4 levels of RING0-RING3,RING0 at the highest level,
So the Linux user code runs under RING3, the kernel runs in RING0, so the system itself gets
Adequate protection

User space (user mode) go to kernel space (System mode) method:
• System Calls
• Hardware interrupts

Linux kernel Architecture:

Virtual File System VFS:
VFS (virtual file system) hides the specifics of various file systems, providing a unified interface for file operations

Two. Linux Kernel source code
Linux kernel download www.kernel.org
Directory structure:
Unzip the Linux kernel tar post directory
Arch: Code based on different CPU architectures
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 file required for kernel, (platform-independent header file in Include/linux)
lib: library file code (platform-dependent)
mm: Implementing memory management, independent of hardware architecture (in arch related to hardware architecture)
Net: Code for the network protocol
samples: Some examples of kernel programming
scripts: Configuring the kernel Script
Modules for Security:selinux
Sound: Drivers for audio Devices
usr:cpio Command Implementation, the command used to make the root file system (file system and kernel to put a piece of command)
virt: Kernel virtual machine

Linux DOC compilation Generation:

Linux Source root directory/documentation/00-index: Directory index
Linux Source root directory/documentation/howto: Guide
• Build Linux kernel Help documentation: perform make Htmldocs at the Linux source root (documentation)

Ubuntu16 required to execute sudo apt-get install Xmlto installation plugin to generate DOC document

The code in Arch,drivers is often changed in the later development.

three. Linux kernel configuration and compilation
Clean up files (in the Linux source root directory):
make clean: cleans all resulting files only
make Mrproper: Clean up all generated files and config profiles
make Distclean: Clean up all generated files with config profile, and edit with patch files

configuration (Collect hardware information such as CPU model, NIC, etc...):
make config: Interactive configuration based on text mode
make Menuconfig: Menu mode based on text mode (recommended)
make Oldconfig: Use existing. config, but ask for new configuration items
make Xconfig: Graphical configuration (requires installation of a graphical system)
Configuration method:
1) Use the Make Menuconfig operation method:
1> Press Y: Compile > Connect > Image file
2> Press M: Compile
3> Press N: Do nothing
4> Press "SPACEBAR": y,n rotation
Once configured and saved, a. config file is generated under the Linux source root directory
Note: Install the support package on UBUNTU11 to perform the Apt-get installation Libncurses5-dev
2) Use an existing profile template (. config)
1>linux Source root directory/arch/<cpu architecture >/configs/< specific CPU file, copy and rename the corresponding file in. config to the Linux source root directory
2> uses the currently running file (to view with ls/boot/-a) to copy the/BOOT/CONFIG-2.6.18-53.E15 and rename it to. config to the Linux source root directory to perform the above operations. Menuconfig in Copy
The. config file above modifies the file.

Compile kernel:
1) Make Zimage
2) Make Bzimage
Difference: On the X86 platform, Zimage can only be used for cores less than 512k
Get detailed compilation information: Make Zimage v=1 or make Bzimage v=1
The compiled kernel is in the following: arch/<cpu>/boot/directory.
Note: Before compiling the. config profile CP to the root directory, you must enter make Menuconfig and save the exit (otherwise it will not work)

To compile and install the module:
1) Compile kernel module: make modules
2) Install kernel module: Make Modules_install install_mod_path=/lib/modules
Replace the machine core: Copy the compiled kernel module from the kernel source directory to/lib/modules
Make Init RAMDisk (): Enter execution command MKINITRD initrd-2.6.39 (any) 2.6.39 (can be obtained by querying the directory under/lib/modules)
Attention:
MKINITRD command for Redhat inside, Ubuntu command for: mkinitramfs-k/lib/modules/Module Installation location-o initrd-2.6.39 (any) 2.6.39 (can be queried/lib/ Modules under the directory)
If there is no Mkinitramfs command in Ubuntu can be installed with Apt-get install Initrd-tools

Install kernel modules:
1) Manual
1&GT;CP Linux root directory/arch/x86/boot/bzimage/boot/mylinux-2.6.39
2&GT;CP Linux root directory/initrd-2.6.39/boot/initrd-2.6.39
Finally, modify the/etc/grub.conf or/etc/lilo.conf file
2) Automatic
1>make Install: This command automatically completes the above operation (view current kernel version: UNAME-R)
-----------------------------------------------------------------------------
four. Linux kernel module Development
Describe:
The Linux kernel component is very large, the kernel ximage does not contain a component, but when the component needs to be used, dynamic add to the running kernel (also can unload), this mechanism is called "kernel module" mechanism. Kernel modules typically compile modules by using the makefile file

Module installation and uninstallation:
1) Load: Insmod Hello.ko
2) Uninstall: Rmmod Hello
3) Views: lsmod
4) Load (auto-find module dependent): Modprobe Hello
Modprobe will look at the module to be loaded according to the file/LIB/MODULES/VERSION/MODULES.DEP, see if it also depends on other modules, if so, will find the modules first, load them into the kernel

Example Analysis:
1) MODULEDEP/1 (compilation of a module)

1#include <linux/module.h>2#include <linux/init.h>3 4 //Module Entry function5 //__init: Represents a segment of a snippet in which content runs only once and reclaims memory.6 Static int__init Hello_init (void)7 {8PRINTK (Kern_emerg"Hello world!\n");9     return 0;Ten } One //Module Unload function A //__exit: - Static void__exit Hello_exit (void) - { thePRINTK (Kern_emerg"Hello exit!\n"); - } - //kernel symbol export function - intAdd_integar (intAintb) + { -     returnA +b; + } A intSub_integar (intAintb) at { -     returnA-b; - } -  - Module_init (hello_init); - Module_exit (hello_exit); in //function Export - Export_symbol (Add_integar); toExport_symbol (Sub_integar);

Makefile

#第一次执行KERNELRELEASE是空的, so execute else inside of IFNEQ ($ (kernelrelease),) obj-M: =hello.o#else block Else kdir:=/lib/modules/2.6. el5/buildall: #KDIR    relies on the kernel module source code path (kernel compilation installation path) #PWD     indicates where the kernel code (current directory) #modules compiled module     -C $ (kdir) m=$ (PWD) modules Clean    :-F *.ko *.o *.mod.o *.mod.c *.symvers *. orderendif
    

2) MODULEDEP/2 (compilation of two modules)

1#include <linux/module.h>2#include <linux/init.h>3 //Module Optional Information4Module_license ("GPL");//License Statement5Module_author ("Liyuan");//Author Statement6Module_description ("This module is a param example.");//Module Description7Module_version ("V1.0");//Module Aliases8Module_alias ("a simple module");//Module Aliases9 Ten //Module Parameters One Static Char*name ="Liyuan Arg"; A Static intAge = -; - //S_irugo is a parameter permission, or you can use a numeric -Module_param (age,int, S_irugo); the Module_param (Name,charp,s_irugo); -  -  - //using external file functions + extern intAddintAintb); -  +  A //declaring an external kernel symbol function at extern intAdd_integar (intAintb); - extern intSub_integar (intAintb); -  - Static int__init Mains_init (void) - { -      //Multi-file compilation in  -PRINTK (Kern_emerg"param Hi"); to     intVle=add (1,2); +PRINTK (Kern_emerg"Add value:%d\n", VLE); -     //Module Parameters the  *PRINTK (Kern_emerg"Name:%s\n", name); $PRINTK (Kern_emerg"Age :%d\n", age);Panax Notoginseng  -     //functions using other modules (kernel symbol export) the     intAdds=add_integar (3,1); +     intSubs=sub_integar (3,1); APRINTK (Kern_emerg"Add_integar:%d\n", adds); thePRINTK (Kern_emerg"Sub_integar:%d\n", subs); +     return 0; - } $  $ Static void__exit Mains_exit (void) - { -Printk"param exit!"); the } - Wuyi Module_init (mains_init); theModule_exit (Mains_exit);

Add.c

int Add (int A,int  b) {    return a +b;}

Makefile

Ifneq ($ (kernelrelease),) #两个以上内核源文件 generate a separate kernel module name ma# kernel maobj-M: =ma.o# under the MA-OBJS front must be the same as above Mama -OBJS: = mains.o add.oElsekdir:=/lib/modules/2.6. el5/buildall:        -C $ (kdir) m=$ (    PWD) modules clean:-F *.ko *.o *. MOD.O *.mod.c *.symvers *. Orderendif

run the parameter module : 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 implementation of the function of another kernel module, and the first kernel module must be run before the kernel symbol export is required.

Attention:
Error message: Disagrees about version of symbol Struct_module Insmod:error inserting ...
When you develop kernel modules, the kernel modules do not match. Is the Linux kernel you are currently running with the build connection depends on the
Kernel version mismatch, workaround:
• Force insertion with modprobe--force-modversion
• Use Uname-r to view the currently running kernel version

PRINTK Kernel Print:
The PRINTK in <linux/kernel.h> has 8 priorities, decreasing by priority:
· Kern_emerg 0
For emergency messages, often the ones that crash.
· Kern_alert 1
Messages that need immediate action
· Kern_crit 2
Serious situation
· Kern_err 3
Error condition
· Kern_warning (PRINTK default level) 4
A warning of a problem
· Kern_notice 5
Normal, but still worthy of attention
· Kern_info 6
Informational messages
· Kern_debug 7
Use as a debug message

Regardless of the level will be printed in the/var/log/messages (messages can be deleted, run the kernel to test kernel printing) Console printing (priority configuration/PROC/SYS/KERNEL/PRINTK)

6 4 1 7
· Console_loglevel
· Default_message_loglevel
· Minimum_console_level
· Default_console_loglevel


Error when installing 2.6.39 kernel in Vm+redhat
Start times could not find filesystem '/dev/root '
Workaround
A. Select the options below through make Menuconfig
General Setup--
[*] enable deprecated SYSFS features to support old userspace tools
It's the one that's underneath when you're successful.
B. Modifying a. config file
Modify the config_sysfs_deprecated_v2 in the. config file to change the originally commented out
CONFIG_SYSFS_DEPRECATED_V2 changed into config_sysfs_deprecated_v2=y

follow my shared learning to discuss more technical knowledge

personal website : http://www.liyuan3210.com

Linux kernel compilation and development

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.