Linux kernel compilation steps and configuration details

Source: Internet
Author: User

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 function 5//__init: Represents a child segment in a code snippet, The content inside is only run once and the memory is reclaimed. 6 static int __init hello_init (void) 7 {8     printk (Kern_emerg "Hello world!\n"), 9     return 0;10}11//module unload function,//__e xit:13 static void __exit hello_exit (void) + {     printk (kern_emerg "Hello exit!\n"); 16}17//kernel symbol export function int Add_integ AR (int a,int b), {     return a+b,}22 int sub_integar (int a,int b), {return A-a     ,}26 (module_init O_init); Module_exit (hello_exit); 29//function Export of Export_symbol (Add_integar); Export_symbol (Sub_integar);

Makefile

#第一次执行KERNELRELEASE是空的, so do the Ifneq ($ (kernelrelease),) obj-m: =hello.o#else block elsekdir:=/lib/modules/ 2.6.18-53.el5/buildall: #KDIR    relies on kernel module source code path (kernel compiled installation path) #PWD     indicates where the kernel code is (current directory) #modules compiled module    make-c $ (kdir) M =$ (PWD) modules clean:    rm-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 Information 4 module_license ("GPL");//License Statement 5 Module_aut HOR ("Liyuan");//author declares 6 module_description ("This MODULE is a param example."); /Module Description 7 module_version ("V1.0");//Module alias 8 Module_alias ("A simple Module");//Module Alias 9 10//module parameter one by one static char *name = "Liyuan A RG ";" static int age = 30;13//s_irugo is a parameter permission, also can use the number of the Module_param (Age,int,s_irugo); Module_param (Name,charp,s_irugo ); 16 17 18//Use an external file function, extern int add (int a,int b), 20 21 22//Declare an external kernel symbol function at all extern int Add_integar (int a,int b); exter n int sub_integar (int a,int b); net static int __init mains_init (void) 27 {28//Multi-file compilation printk (Kern_emerg "Para M hi "); vle=add int PRINTK (Kern_emerg" Add value:%d\n ", VLE); 33//Module parameters PRINTK (Kern_emerg" NA Me:%s\n ", name); PRINTK (Kern_emerg" Age:%d\n ", age); 37 38//function with other modules (kernel symbol Export) (Adds=add_integar) ; Subs=sub_integar int (3,1); PRINTK (Kern_emerg "Add_integar:%d\n ", adds); PRINTK (Kern_emerg" Sub_integar:%d\n ", subs); return 0;44}45 the static void __exit Mains_exit ( void) printk ("param exit!"); }50 Wuyi Module_init (mains_init); Module_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# The MA-OBJS in front must be the same as above mama-objs: = MAINS.O add.oelsekdir:=/lib/modules/2.6.18-53.el5/buildall:        make-c $ (kdir) m=$ (PWD) modules clean:    rm-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)

To summarize our installation steps:

       1, get the kernel source, extract to/usr/src           # tar xf linux-3.13.5.tar.xz-c/usr/src           # ln-sv/usr/src/linux-3.13.5  /usr/src/ Linux       2, configuring kernel features (choose a method) Make           config: Traverse select the kernel feature to compile make           allyesconfig: Configure all the compiled kernel features made           Allnoconfig: Not all do not compile make           menuconfig: This is to open a File window selection menu make           kconfig (KDE desktop environment, and the installation of the QT development environment) make           Gconfig (GNOME desktop environment, and install GTK dev Environment)       3, compile kernel           # make [-j #]: #号最多为CPU物理核心总数的两倍, this will be faster OH                   4, install kernel module           # makes Modules_install       5, install kernel           # make install       6, verify and Test           # cat/boot/grub/grub.conf           to see if the new kernel has been added, Then reboot the system and test

Linux kernel compilation steps and configuration details

Related Article

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.