Linux kernel:
Kernel design Genre:
Single core design, but fully learn the advantages of micro-kernel system design, for the kernel introduced a modular mechanism, the core height of modularity;
After the kernel is modularized, some of the most basic and important content is compiled into kernel core, while other functions are provided as modules, and dynamic loading and unloading of each kernel module is supported;
Components of the kernel:
Kernel: Kernel core files, typically bzimage, compressed image files, usually kernel core files are saved in/boot/directory, with the name Vmlinuz-version-release
Kernel object (KO): kernel objects, kernel additional function module, usually this kind of file is placed in/lib/modules/version-release
Note: Kernel module and kernel core, version number must match strictly;
Kernel module is actually part of the kernel source code, but in the process of compiling the kernel, because its function may not be required by kernel core, so it is compiled in the way of module;
When compiling the kernel, the functions of the kernel usually have the following choices;
[] kernel function:no, do not choose to compile this function;
[M] Kernel function:modules, compile this function into kernel module use; This function does not occupy the inside and space, only occupies the disk space;
[*] Kernel Function:yes, this function is compiled directly into kernel core;
RAMDisk: internal and supplemental files, auxiliary files, for kernel core, this file is not necessary, whether to use this file depends on the kernel can directly drive the storage device of ROOTFS;
Driver for device, SCSI device driver
Logical device drivers, LVM driver, soft raid driver, etc.;
File system;
Cpio-i-F Initramfs-2.6.32-573.el6.x86_64.img
Simplified Rootfs
Note: In general, the kernel core file and the RAMDisk file must have the exact same version number;
Related commands for kernel management:
Uname: Prints current system-related information (kernel version number, hardware architecture, host name, operating system type, etc.).
Common options:
-N: Show node name
-r: Displays the kernel version number, both version and release
-A: Show All information
Example:
Uname-a
Lsmod: Displays status information for modules that have been loaded into the kernel.
Displays kernel modules that have been loaded by the Linux kernel core;
The contents of the Lsmod display are divided into three fields:
Module Name module size is quoted and who is quoted;
Example:
Lsmod
Modinfo: Displays information about the kernel module.
Common options:
-F field: Displays only the information for the specified field, usually only one field is specified;
-N: Displays only the absolute path of the module file; (most commonly used)
-A: Displays only the author information of the module;
-D: Displays only the description of the module;
-L: Show only license information;
-P: Display only module parameter information;
Example:
Modinfo
Depmod: can generate module dependent mapping file, when building an embedded system, it is necessary for this command to generate the corresponding file, used by Modprobe.
Options:
-A or--all: Analyze all available modules;
-D or debug: Perform an error-troubleshooting mode;
-e: The output cannot be referenced by symbols;
-I: Do not check the version of the symbol table;
-m< file > or system-map< file;: Use the specified symbol table file;
-S or--system-log: Errors are recorded in the system record;
-V or--verbose: Displays detailed information when executed;
-V or--version: Displays version information;
--help: Displays Help.
Commands to implement dynamic loading and unloading of kernel modules:
Insmod: Load the specified kernel module, but cannot automatically resolve the dependencies between modules;
Example:
Insmod Modinfo-n Btrfs
Note: filename: The absolute path to the module file;
Rmmod: Used to remove the specified kernel module from the currently running kernel.
Example:
Rmmod RAID1
Modprobe: Removing modules from the kernel or inserting modules into the kernel;
Default configuration file:/etc/modprobe.conf,/etc/modprobe.d/*
Example:
Modprobe module_name: Load module, automatic identification and resolution of dependencies;
Modprobe-r module_name: Unload module
RAMDisk Management:
RAMDisk files are made by specific applications based on the current hardware device information and file system information after the operating system installation is complete;
RAMDisk File Authoring Tools:
CentOS 5:
MKINITRD: Create an image file to load the RAMDisk to load RAMDisk when Linux is powered on.
Options:
-F: Overwrite the existing file if the name of the specified image is duplicated with the existing file;
-V: Displays detailed information when executing;
--omit-scsi-modules: Do not load the SCSI module;
--preload=< module name;: Specify the module to be loaded;
--with=< module name;: Specify the module to be loaded;
--version: Displays version information.
Example:
Mkinitrd-v-F Myinitrd.img $ (uname-r)
CentOS 6/7:
Dracut: Create an image file to load the RAMDisk to load RAMDisk when Linux is powered on.
Example:
dracut/boot/initramfs-$ (UNAME-R). IMG $ (UNAME-R)
MKINITRD script:
Two important pseudo-file systems for internal and information output:
/proc: The main output interface of kernel state and statistic, and also provides an interface--/proc/sys that can input configuration information and complete real-time configuration of kernel parameters;
/proc/* (except sys directory): Information output, read only;
/proc/sys: Can read and write, can accept user-specified "new value", to achieve the kernel corresponding functions or features of the real-time configuration;
To view the status information or statistics of the kernel output, use the cat command directly;
To modify or set kernel features or features:
Use the echo command to modify or set by overwriting the output redirection;
echo "Value" >/proc/sys/path/to/parameter
View kernel parameters and configure many of the features in/proc/sys, and also use:
Sysctl command:
Sysctl-configure Kernel parameters at runtime
Sysctl: Used to dynamically modify the kernel's operating parameters while the kernel is running, and the available kernel parameters are in directory/proc/sys.
To view kernel parameters:
Sysctl-a: View all kernel parameters that can be modified;
Sysctl variable: View the setpoint of the specified kernel parameter;
Configure a value for a kernel parameter (feature or attribute):
Sysctl-w Variable=value
Note: In this method, "=" cannot write whitespace characters on both ends;
Sysctl-p: Set the memory parameters according to the configuration file, re-read and load all the setting parameters in the configuration file and make it take effect;
Several kernel parameters are commonly used:
Net.ipv4.ip_forward:Linux core forwarding function, routing function; value 0,1
Net.ipv4.icmp_echo_ignore_all: Ignores all ping operation requests originating from external hosts, and takes value 0,1
Vm.drcp_caches: Clear buffer and cache, release physical memory, value: 0,1,2
Kernel.hostname: Host name currently in effect:
Ddos:dynamic deny services, dynamic denial of service attacks
Dchp:ip address exhaustion Policy, sending a large number of random MAC address DHCP discover messages
Dns:arp attacks, ARP spoofing,
Gateway Spoofing
Source IP Address Spoofing
Dead Ping:
/sys:sysfs
Specifically for the user to provide the use of the pseudo-file system, the output kernel identifies the various hardware devices related to the property information, but also includes the kernel of the hardware characteristics of the settable information, for some parameters to modify the specific format to adjust or set the hardware operating characteristics;
Echo '---' >/sys/class/scsi_host/host2/scan
Udev
Create device files for each hardware device by reading the information of the hardware device in the/sys directory;
Udev is a process that runs in user space;
Special tools: Udevadmin,hotplug ...
When the kernel has been loaded into memory, if the operating system is installed on the SDA disk, the kernel needs to identify the SDA disk and mark it as a device (creating a device file) before it can be mounted;
In order to enable such devices to be used in the future, the kernel creates device files for the devices to be used by each kernel through the built-in Devtmpfs, and such files can be mounted as file systems, transferred from the kernel directly to the dev directory in the real rootfs. For devices that are newly inserted after the operating system starts, it is necessary to rely on udev to identify and create the device files;
Udev's ability to create device files for devices depends primarily on its predefined rules, which are typically stored in Udev's rule configuration file:
/etc/udev/runles.d
/usr/lib/udev/rules.d
How to customize the kernel--compile the kernel source code:
http://kernel.org Kernel Maintenance of the official site, you can get the kernel source code package;
Prerequisites for compiling source code:
1. Development environment
Development tools: Gcc,make,automake,qt,gtk,ncurese
Package Group:
"Development Tool", "Server Platform development"
"Development tools", "Server platform development"
Ncurses-devel
Header file:/usr/include/*.h
2. Obtain information about each hardware device on the target host;
Cpu:
# Cat/proc/cpuinfo
# LSCPU
# X86info-a
PCI Device:
# LSPIC[-V|-VV]
USB Device:
#lsubs [-V|-VV]
Block device:
#lsblk
Learn more about Hardware device information:
# Hal-device (CENTOS6 available package name: hal-0.5.14-14.e16.x86_64)
3. Get the relevant options for the target host system feature:
For example: What kind of file system should be used by target host;
Whether the target host needs to start the security protection mechanism;
...
General steps for compiling and installing an application:
1. #./.configure arg1 [arg2 ...]
2. # make
3. # make Install
General steps for compiling and installing the kernel:
1. You need to prepare or generate a. config file that records the compilation details of the kernel:
Which functions are compiled directly into the kernel;
Which functions are compiled into kernel modules;
Which features are not enabled in this compilation;
Make Menuconfig|xconfig|config
2. Start compiling the kernel
Make [-j #]
Multi-threaded compilation, the compilation process can be carried out in parallel on multiple CPU cores;
3. Install the module file:
Make Module-install
4. Install kernel core files and generate Grub boot menu;
Make install
Install the Bziage file, install to/boot/vmlinuz-version-release
To generate a Initramfs file that exactly matches the kernel version
Edit the Grub configuration file to generate the boot menu item;
Screen: A free software developed by the GNU Program for command-line terminal switching.
Example:
Screen (open screen)
Ctrl +a;d (remove screen)
Screen-ls (List display screen)
Screen-r screen_id (Resume connection to the specified screen)
Exit (turn screen off)
Configuration options for the kernel:
64-bit kernel
Whether the 64-bit kernel is supported
General Setup--->
Common Configuration items:
() local version-append to kernel release
The custom local version number, appended to the kernel version number, is defined by the mutant;
((none)) default hostname
Defines the default hostname when no host name is set;
Enable Loadable Module support
Whether to support dynamic loading and unloading of kernel modules;
Enable the block layer
Support for enabling block layers, usually select support;
Processor type and Features:
Processor types and Features
Processor Family (generic-x86-64)--(core 2/newer Xeon)
Select processor Type
Power Management and ACPI options:
Power management and Advanced Power management interface options
Executable file Formats/emulations
Specifies the format of the executable file, which is the default elf, to #! The beginning of the file also has executable characteristics;
Networking Support
Network protocol stacks in the kernel
Networking options
[] IPv6 support
Device drivers:
Device drivers
File system:
DOS/FAT/NT File Systems
[M] NTFS support
Kernel hacking:
Kernel Debugging related content
Security options:
NAS SELinux Support
Security options
Cryptographic API:
Encrypted decryption of application changes;
Virtualization:
Virtualization-related
How to configure the kernel:
1.make Config: Kernel configuration is based on a single-line command to traverse all kernel functions, so the configuration of each kernel option is interactive;
2.make menuconfig: Configuration window based on curses text mode;
3.make gconfig: A window Configuration interface based on the GTK development environment; Generally, as long as the "desktop Platform development" package group is installed;
4.make xconfig: A window Configuration interface based on the Qt development environment; Generally, as long as the "desktop Platform development" package group is installed;
5.make Defconfig: Based on the kernel to provide a default configuration template for the target platform configuration;
6.make Allnoconfig: All functions are not compiled (all select No) configuration mode;
7.make Allyesconfig: All functions are compiled into the core (all Select Yes) configuration mode;
How the kernel is compiled:
1. Full compilation: Make [-j #]
2. Partial compilation:
A. Compile only the relevant source code in a subdirectory;
# Cd/user/src/linux
# make [-j #] Dir_name/
B. compiling only specific modules
# Cd/usr/src/linux
# make [Dir/]file.ko
Example:
# Cd/usr/src/linux
# Make Drivers/net/ethernet/intel/e1000/e1000.ko
3. Cross-compiling: the target platform for compiling is not the same as the platform currently being compiled;
Make Arch=arch_name
Example:
# Make Arch=arm
Kernel recompilation:
1. Delete the contents of all/usr/src/inux-version directories, release them from the source code package, and then recompile the previous steps again;
2. First clean up the previous compilation interface:
Make clean:
Cleans up most compilation-generated files, but retains. config files;
Make Mrproper:
Clean up all compiled and generated files, including. config and other backup files;
Make Distclean:
Equivalent to make mrproper, but also to clean up the various patches and the backup files of the compiler itself;
Compiling the installation kernel instance
~]# Tar XF linux-3.10.99.tar.xz-c/usr/src
~]# CD/USR/SRC
src]# LN-SV linux-3.10.99 Linux
src]# Cd/usr/src/linux
linux]# cp/boot/config-$ (uname-r)./.config
linux]# make Menuconfig
linux]# Make-j 4
linux]# make Modules_install
linux]# make Install
linux]# reboot
Reboot after reboot in the Grub menu to select the newly compiled kernel to boot, if you can see the login prompt, then the kernel compilation upgrade succeeded!
You can then try to compile other more advanced versions of the kernel that are different from the current operating system version, try several versions, and learn about the features of the different versions of the kernel;
#26 Linux Kernel (kernel) detailed with uname, Lsmod, Modinfo, Depmod, Insmod, Rmmod, modprobe ... Command usage