20.1 Linux startup Process analysis
Linux boot process:
After pressing power on, the computer hardware actively reads the BIOS to load hardware information and the hardware system self-test, then the system will actively read the first bootable device (set by the BIOS), at this point can be read into the boot loader (boot loader), The bootloader specifies that the kernel file is used to boot and actually loads the kernel into memory for decompression and execution, at which point the kernel is able to operate in memory and detect all hardware information and load the appropriate driver to make the host run, so that after the kernel detects the hardware and loads the driver, it can log on.
1) Load BIOS hardware information and self-test, and obtain the first bootable device according to the setup
2) Read and execute the boot Loader (i.e., grub,spfdisk) of the MBR in the first boot device
3) Load the kernel according to boot loader settings, the kernel starts to detect the hardware and load the driver
4) After the hardware driver succeeds, kernel will invoke the INIT process actively, and Init will get run-level information
5) Init executes the/etc/rc.d/rc.sysinit file to prepare the operating environment for software execution
6) Init execution of Run-level for each service start
7) init execute/etc/rc.d/rc.local file
8) Init execution Terminal Simulator Mingetty to start the login process, and finally wait for the user to log in
The main features of Boot loader:
Menus available: Users can choose from different startup options, as well as the important functions of multi-boot
Load kernel file: Point directly to the bootable program section to start the operating system
Transfer Other loader: Transfer the boot loader function to other loader responsible for
1): Each operating system has its own boot loader, for multiple operating systems, each operating system must use its own loader to load its own operating system kernel, and the system MBR only one, how to install Windows and Linux on a host at the same time
Answer: Because the boot loader has a menu function, you can choose a different kernel to boot. Because of the ability to transfer control, you can load the loader in other boot sector (the loader default layout of Windows has the control handoff function, so you can't load Linux loader with Windows loader)
2): Assuming that the Linuux is installed on a SATA hard drive, the boot loader and Kenerl files can be booted from the BIOS INT13, and then the kernel will start to take over the system and detect the hardware and attempt to mount the root directory to obtain additional drivers. But the kernel does not know SATA disk, so need to load the SATA disk driver, or can not mount the root directory, but the SATA driver in/lib/modules, can not mount the root directory, and how to read the driver within the/lib/modules?
Answer: Through the virtual file system resolution, the general use of the file name is/BOOT/INITRD. The file can be loaded into memory via boot loader, the file is decompressed and emulated into a root directory in memory, and this emulated in-memory file system provides an executable program that loads the kernel modules needed during the boot process, usually USB,RAID,LVM, SCSI and other file system and hard disk interface driver. When loading is complete, the kernel will be called back to/sbin/init to begin the following normal process
(If Linux is installed on a disk in the IDE interface and the Ext2/ext3 file system is used by default, Linux can be started without initrd)
After the kernel has finished loading hardware detection and driver load, the host hardware is ready, when the kernel will actively invoke the first process, that is,/sbin/init (init pid is 1),/sbin/init's main function is to prepare the software execution environment, including the system host name, Startup of network settings, language processing, file system formats, and other services. All operations are planned through the init configuration file/etc/inittab.
Inittab A very important setting option is the run level
0--halt (System direct shutdown)
1--single user mode (single-user maintenance mode, for maintenance in the case of system problems)
2--multi-user,without NFS (similar to run LEVEL3, but no NFS service)
3--full Multi-User mode (full text mode with network functionality)
4--unused (System hold function)
5--X11 (similar to run LEVEL3 but loaded using x Windows)
6--reboot (restart)
There is a sentence in/etc/inittab: "Si::sysint:/etc/rc.d/rc.sysinit" means: Before starting to load various system services, you have to set up the entire system environment, mainly using/etc/rc.d/rc.sysinit this shell Script to set up the system environment
Start system services with related startup profiles (/ETC/RC.D/RC N &/etc/sysconfig)
The meaning of/ETC/RC.D/RC5 is this:
Use the external first parameter ($) to get the script directory you want to execute, that is,/ETC/RC.D/RC 5 can get/etc/rc5.d/this directory to prepare to process the relevant script
Find/etc/rc5.d/k?? * Start the file and proceed to/etc/rc5.d/k?? * Stop operation
Find/etc/rc5.d/s?? * Start the file and proceed to/etc/rc5.d/s?? * Operation of Start
(/etc/rc5.d files are all linked files, linked to stand alone service startup directory/ETC/INIT.D)
such as/etc/rc5.d/k91capistop-->/etc/init.d/capistop
/etc/rc5.d/s10networkstart-->/etc/init.d/networdkstart
The numbers after S and K indicate the order in which the files are executed, s99local is/etc/rc.d/rc.local is the last execution
User-defined boot loader (/etc/rc.d/rc.local)
If the user has any work that they want to do at startup, write it directly to/etc/rc.d/local, which will automatically load when it starts
The main configuration files that the startup process will use:
1) Module:/etc/modprobe.conf (This file mostly depends on the module that is used to specify the hardware in the system, if the system detects an incorrect driver, or if you want to use an updated driver to match the relevant hardware, you have to manually process the file)
2) Several files under the/etc/sysconfig/*:
Authconfig This file primarily sets the user's identity authentication mechanism, including whether to use local/etc/passwd,/etc/shadow etc.
Clock This file is used to set the loss of the Linux host, can use Greenwich Mean time, or use local times
I18N is used to set up the use of some language
Keyboard&mouse Setting the keyboard and mouse form
The network can set whether to start networking, and set the hostname and gateway information
Network-scripts is primarily used to set up network cards
Switch Run Level:init N
Displays the current run Level:runlevel (the left side of the output represents the previous RunLevel, and the right side represents the current runlevel)
Init 0 shutdown
Init 6 Restart
20.2 Kernel and kernel modules
Kernel and kernel module storage location:
Kernel:/boot/vmlinuz or/boot/vmlinuz-version
Kernel decompression required RAMDISK:/BOOT/INITRD (/boot/initrd-version)
Kernel modules:/lib/modules/version/kernel or/lib/modules/$ (uname-r)/kernel
Kernel Source:/usr/src/linux or/usr/src/kernels (to be installed, not installed by default)
Kernel version:/proc/version
System kernel function:/proc/sys/kernel
Kernel modules and dependencies:
/lib/modules/$ (uname-r)/MODULES.DEP file records the dependencies of the modules supported in the kernel
For example, do a good NIC driver, file name A.ko, how to update kernel dependencies?
Can be implemented using the DEPMOD command (Depmod [-ane])
CP a.ko/lib/module/$ (UNAME-R)/kernel/driver/net
Depmod
View of kernel modules:
Lsmod
After using Lsmod, the system will show the modules that are currently present with the kernel, including: module name modules, size of module sizes, whether this module is used by other modules used by
Modinfo [-ADLN] [Module_name|filename]
such as: Modinfo WMI
Loading and removing of kernel modules:
If you want to manually load the module, the simplest is also recommended to use the Modprobe command to load the module, because Modprobe will actively find the content of MODULES.DEP, first overcome the dependency of the module, the dish determines which modules to load. Insmod is fully loaded with a full file name module and does not actively analyze module dependencies
modprobe [-LCFR] Module_name
Insmod [/full/path/module_name] [parameters]
Example: Try to load the file system module in Cifs.ko
insmod/lib/module/$ (uname-r)/kernel/fs/cifs/cifs.ko
Lsmod | grep CIFS
Delete module: rmmod [-FW] Module_name
20.3 Boot Loader:grub
Boot loader program code execution with set value loading is divided into two stages to execute
Stage1: Execute boot loader main program (this main program must be installed in ScanDisk, that is, MBR or boot sector, because the MBR is too small to be, so MBR or boot sector usually only install boot loader minimum main program, Does not have the relevant configuration file for loader installed)
Stage2: Load all configuration files and related environment parameter files (including file system definition and main profile menu.lst) via boot loader, in general, the configuration files are in/boot
One of the final tasks of the GRUB main program installed on the MBR is to load the kernel files from the disk so that the kernel can drive the entire system's hardware smoothly
Grub's hard drive code, such as HD (0,0), indicates that the first block found is (hd0) and that the first partition of the hard disk is (hd0,0)
/boot/grub/menu.lst (GRUB.CFG)
Default=0 The default startup option, use the first boot menu
Timeout=5 If the keyboard is not moved within 5 seconds, start with the default menu
Hiddenmenu whether the full menu interface is displayed during read seconds
Need INITRD moment for:
The root directory is the same disk as the SATA, USB, or SCSI connection interface
The file system with the mother is LVM, RAID and other special formats
File system where the root directory resides on a non-traditional Linux "recognized" filesystem
Other modules that must be supplied when the kernel is loaded
You can use the MKINITRD command to redo the initrd file
MKINITRD [-v] [--with= module name] INITRD file name kernel version
Example 1: Creating a INITRD virtual hard disk file with the default functionality of MKINITRD
Mkinitrd-v initrd_$ (uanem-r) $ (uname-r)
Example 2: Add 8139too initrd file for this module
Mkinitrd-v--with=8139too initrd_vbirdtest $ (uname-r)
Install Grub (Grub-install is to install grub-related files to the device to wait to be read at startup, but also to set the configuration file Menu.lst, then install the GRUB main program to the MBR or boot sector on the grub shell)
Grub-install [--root-directory=dir] Install_device
Example 1: Installing grub under the current system's MBR, my system is/dev/had
Grub-install/dev/hda
Example 2: My/Home is a standalone/dev/sda3, how to install grub to/dev/sda3
Grub-install--root-directory=/home/dev/sda3
VGA settings in the kernel function
Resolving TTY1~TTY6 resolution Issues
First of all to see if support framebuffer_console this kernel function parameter
grep ' Framebuffer_console '/boot/config-3.2.0-23-generic-pae (if config FRAMEBUFFER console=y indicated support)
Modify the resolution vi/boot/grub/menu.lst (or add a red part to the Grub.cfg file)
Menuentry ' Ubuntu,linux 3.2.0-67-generic-pae '--class Ubuntu--class gnu-linux--class GNU--class os {
Recordfail
Gfxmode $linux _gfx_mode
Insmod Gzio
Insmod Part_msdos
Insmod ext2
Set root= ' (HD0,MSDOS1) '
Search--no-floppy--fs-uuid--set=root C6008246-E4EE-490F-BEC6-3F72108424DD
Linux/boot/vmlinuz-3.2.0-67-generic-pae root=uuid=c6008246-e4ee-490f-bec6-3f72108424dd ro quiet Splash $vt _handoff vga=790
Initrd/boot/initrd.img-3.2.0-67-generic-pae
}
790 corresponds to the resolution of 1024x768, there are other parameters, such as 784~795,769,771,773,775
20.4 Troubleshooting the startup process
1) Forgot password
A. Restart
B. After booting into the Grub menu, press "E" on the menu you want to enter to enter the menu settings
Grub EDIT>KERNEL/VMLINUZ-2.6.18-92.E15 ro root=label=/rhgb quiet single
(My Ubuntu should be linux/boot/vmlinuz-3.2.0-67-generic-pae ROOT=UUID=C6008246-E4EE-490F-BEC6-3F72108424DD ro quiet Splash $VT _handoff Single)
Press ENTER to start entering single-user maintenance mode by pressing B
C. After entering single-user mode, the shell can be executed, input passwd can re-root password, in the execution of Init 5 can be switched to X Window interface
2) Init configuration file Error
A. After the same boot into grub
Grub EDIT>KERNEL/VMLINUZ-2.6.18-92.E15 ro root=label=/rhgb quiet init=/bin/bash
The first process init called by the kernel becomes/bin/bash
B. Enter Mount-o REMOUNT,RW in the shell/(re-mount the root directory to read/write)
Mount-a (refer to/etc/fstab's content to re-mount the file system)
C. Then the rescue work, after the completion of the restart once it is possible