Reading Notes on "laruence's Linux house dish": Linux kernel compilation and management,
1. Introduction to the kernel and getting the kernel source code 1.1 What is the kernel?
- Kernel: the kernel is the bottom layer of the entire operating system. It is responsible for the entire hardware driver and provides the kernel functions required by various systems, includes firewall mechanisms, whether to support LVM, Quota, and other file systems. If your kernel cannot identify the latest hardware, it cannot be driven, and you certainly cannot use it.In fact, the kernel is just a file on the system. This file contains the detection program and driver module for various hardware of the driver host.Kernel files are generally stored in
/boot/vmlinuzDirectory
- The purpose of the Kernel module is to compile some less commonly used drivers from the Kernel into a module without replacing the Kernel or re-compiling the Kernel, the kernel can load this module to the kernel during the normal operation of the system. The directory is
/lib/modules/$(uname -r)/kernel/
1.2 Why update the kernel
Kernel compilation focuses onWhat do you want your Linux to do.If you do not need to work, you do not need to add it to your kernel! In this way, Linux can run more stably and smoothly. That's why weThe main reason for compiling the kernel.
- Possible purpose of kernel Compilation:
- Requirements for new functions
- The original kernel is too bloated
- Stability in combination with hardware
- Other requirements (such as embedded systems)
Although re-compiling the kernel can optimize your hardware, these optimization steps have little impact on the overall performance. Therefore, if you want to increase the performance to compile the kernel, basically, the benefits are not great. HoweverSystem StabilityTo recompile the kernel. Because the main task of the kernel is to control the hardware, before compiling the kernel, please first understand your hardware configuration and the future features of your host. Because the kernel isThe simpler the betterSo the hacker just needs to compile the future functions of this host.
1.3 kernel source code extraction, installation, and observation
- Kernel source code decompression and placement Directory: kernel source code is generally recommended
/usr/src/kernels/Directory.tar -jxvf linux-X.X.XX.X.tar.bz2 -C /usr/src/kernels/. A new directory is generated under/usr/src/kernels, which is the linux-X.X.XX.X directory.
- Directory under the kernel source code:
- Arch: Most projects related to the hardware platform refer to the CPU category, such as x86, x86_64, and Xen virtual support.
- Block: Set data related to the device group. block data usually refers to mass media storage. It also includes whether ext3 and other file systems are supported or not.
- Crypto: Encryption technologies supported by the kernel, such as md5 or des.
- Documentation: A bunch of instruction files related to the kernel. If you are very interested in the kernel, check it out here.
- Drivers: Some hardware drivers, such as display adapters, network cards, and PCI-related hardware;
- Firmware: Some Old-style hardware micro-script (firmware) data;
- Fs: Filesystems supported by the kernel, such as vfat, reiserfs, and nfs;
- Include: Some headers that can be called by other processes define data;
- Init: Some kernel initialization definition functions, including mounting and calling of init programs;
- Ipc: Defines the communication between various programs in the Linux operating system;
- Kernel: Defines the kernel program, Kernel Status, thread, program schedule (schedule), program signal (signle), etc.
- Lib: Some function libraries;
- Mm: Data related to memory units, including swap and virtual memory;
- Net: Network-related protocol data, as well as firewall modules (net/ipv4/netfilter;
- Security: Security settings including selinux;
- Sound: Audio-related modules;
- Virt: Information related to Virtual machines. Currently, the Kernel supports KVM (Kernel base Virtual Machine)
This data will give a general impression first, at least in the future, if you want to use the patch method to add additional new features, where do you want to put your source code, here we can provide some guidance. Of course, it is better to go to the Documentation directory and check the correct description, which will be more helpful for your kernel compilation.
2. Pre-processing of kernel compilation and selection of kernel functions
Purpose of the kernelIt is about managing hardware and providing system core functions. Therefore, you must first find your system hardware and plan your host's future tasks, in this way, the kernel suitable for your host can be compiled. Therefore, it is important to compile the entire kernel.Select the desired feature.
2.1 hardware environment view and core functional requirements
- View the hardware environment:/proc/cpuinfo and lspci
2.2 keep the source code clean: make mrproper
After learning about hardware-related data, we have to process the residual files under the kernel source code. If this is the first compilation, but we do not know whether the target file is retained in the downloaded source code (*. o) and related configuration files exist. In this case, we can use the following commands to process the target files and configuration files of the compilation process:make mrproper
This command also deletes the core function Selection file that you have previously selected, so this operation is performed almost only before the first core compilation, and the rest of the time, if you want to delete the residual data in the previous compilation process, release:make clean
2.3 start to select the core function: make XXconfig
/Boot/There is a file named config-xxx under it. That file is actually a list of core functions. The operation we are going to perform is actually to make this file. The subsequent compilation operations are actually handled through this file. The selection of core features will finally generate a hidden file named. config under/usr/src/kernels/linux-X.X.XX.X/, which is the file of/boot/config-xxx. You can create this file in many ways. The common methods are as follows:
- Make menuconfig:The most commonly used mode is that similar graphical interfaces can be displayed in text mode. You do not need to start X Window to select the core function menu.
- Make oldconfig:Use existing. /. config File Content. Use the default value in the file to list the new feature options in the core of the new version for the user to select. This simplifies the selection of core features! As a feature selection after upgrading the core source code, it is a very useful project!
- Make xconfig:The graphical interface display based on the basic functions of the Qt graphic interface requires the support of X window. For example, KDE is the X Window designed through Qt, so you can use this project in the KDE screen.
- Make gconfig:The graphic interface display based on the basic functions of Gtk graphic interface requires the support of X window. For example, GNOME is the X Window designed through Gtk, so you can use this project in the GNOME screen.
- Make config:The oldest function Selection Method. Each project is listed in a column for you to choose from. If the setting is incorrect, you can only select again. It is not recommended.
2.4 for details about how to configure the core functions, refer to laruence's linux private house food. 3. Compile and install the kernel. 3.1 compile the kernel and kernel module.
- The kernel and kernel module must be compiled first. You can useMake helpCheck all the available compilation parameters to see the following basic functions:
- Make vmlinux # uncompressed Core
- Make modules# Only Kernel Modules
- Make bzImage# Compressed core (preset)
- Make all # perform the preceding three actions
- Basically, we will perform the following operations:
- Make clean # Clear temporary archives first
- Make bzImage # compile the core
- Make modules # re-compile the module
3.2 actual installation Module
We know that the modules are placed in the/lib/modules/$ (uname-r) directory. If the modules of the same version are repeatedly compiled and installed later, conflicts will occur. Solution:
- Rename the old Module Directory before installing the core module to the target directory.
- When making menuconfig, the Local version in General setup is changed to a new name.
Install the module to the correct target directory:make modules_installIn the end, the relevant modules of your core will be established under/lib/modules.
3.3 start to install the new core and multi-core menu (grub)
- Move the core to/boot and keep the old Core File
cp /usr/src/kernels/linux-X.X.XX.X/arch/x86/boot/bzImage /boot/vmlinuz-X.X.XX.XXXX# Actual Kernel
cp /usr/src/kernels/linux-X.X.XX.X/.config /boot/config-X.X.XX.XXXX# Backing up configuration files
- Create the corresponding Initial Ram Disk (initrd ):
mkinitrd -v /boot/initrd-X.X.XX.XXXX.img X.X.XX.XXXX
- Edit the boot menu (grub): Edit/Boot/grub/menu. lst(For specific configuration, refer)
Summary: Kernel compilation steps and kernel Deletion
- Kernel compilation steps:
- First download the kernel source code (such as SPRM)
- The following uses a Tarball to unbind the original code to the/usr/src/kernels directory.
- Delete old data first: make mrproper
- Start to select core functions, which can be used
make menuconfig,make oldconfig,make gconfigAnd so on.
- Clear the intermediate temporary storage disk information:
make clean
- Start Kernel File and kernel module Compilation:
make bzImage,make modules
- Start kernel module installation:
make modules_install
- To start Kernel File Installation, you can use the following methods:
make installOr manually copy the core file to/boot/grub.
- Create an initrd file;
- Modify the/boot/grub/menu. lst file
- Delete the kernel:
- First, you can delete the source code: rm-rf/usr/src/kernels/linux-X.X.XX
- Then, delete the kernel module directory rm-rf/lib/modules/X. X. XX.
- Finally, delete the kernel files and initrd files in/boot/and the title training in/boot/grub/menu. lst.