Linux Startup Process Learning

Source: Internet
Author: User

The learning process is a guide to Linux from the master boot record to the first user-space program.
The process of starting a Linux system consists of a series of steps. But whether you're launching a standard X86 desktop or embedded PowerPC, most processes are similar. This article explores the process of Linux from the initial bootloader to the first user-space program. At the same time, We will also learn about other startup-related topics such as boot loader, kernel decompression, initial RAM, and other Linux-enabled elements.

In the early stages, booting a computer means providing a paper tape containing a starter program or manually loading a starter program using the front panel address/data/control switch. Now the computer is equipped with equipment to simplify the boot process, but it doesn't make it easy.

Let's start with a high-level view of Linux booting. Then we'll see what we're doing at each step.

Overview

The picture below gives you a 20000-foot view:

When a system is first started, or is reset, the processor executes the code at the specified location. In a personal computer, this location is located in the basic input/output system (BIOS), where the BIOS is stored in flash memory on the motherboard. Central processing Unit (CPU) located on an embedded system Call the reset vector to start a program on a well-known address, which is located in Flash/rom. In any case, the results are the same. Because the PC provides so much flexibility, the BIOS must decide which device is the candidate for booting. We will explain in detail later.

When a boot device is discovered, the first stage boot loader is loaded into RAM and then executed. The boot loader is less than 512 bytes long (the size of a sector) and his job is to load the second stage of boot loader.

When the second stage boot loader is loaded into RAM and executed, the boot animation is displayed on the screen, and the Linux and optional initial RAM disk (the temporary root filesystem) is loaded into memory. When the image is loaded, the second stage of boot The loader passes control to the kernel image, and then the kernel image is decompressed and initialized. At this stage, the second stage of boot loader checks the system hardware, enumerates the relevant hardware devices, mounts the root device, and then loads the necessary kernel modules. When the work is done, the first User space program (init Open, high-level system initialization is run.

This is a concise description of the Linux startup process. Now let's dive into the details of the Linux boot process.

System Open

The system boot phase depends on booting the hardware on the Linux system. On an embedded platform, a boot-boot environment is enabled after the system has been power-up or reset. Examples include U-boot,redboot, and Micromonitor from Lucent. The embedded platform is equipped with a boot monitor. These programs are located in a specific area of flash memory on the target hardware, It also provides a way to download the Linux kernel image to flash memory and run it. In addition to having saved and started the Linux kernel image, these boot monitors run some system tests and hardware initialization. In an embedded target platform, These start to see the boot loader that usually cover the first and second stages.

提取MBR为了查看你的MBR的内容,使用下面这个命令:# dd if=/dev/hda of=mbr.bin bs=512 count=1# od -xa mbr.bindd命令,需要在root模式下运行,从/dev/hda中读取第一个512字节然后将他们写入到mbr.bin文件中.od命令以hex和ASCII格式打印二进制文件.

In the PC, boot Linux starts at address 0xffff0 in the BIOS. The first step in the BIOS is to start self-detection (POST). The job of post is to run a hardware check. The second step in the BIOS is the local device enumeration and initialization.

For different uses of a given BIOS feature, the BIOS consists of two parts: The post code and the runtime service. After the post is completed, it is purged from memory, but the BIOS runtime service is retained and is available in the target operating system.

In order to start an operating system, the BIOS runtime looks for active and bootable devices in the order of the CMOS-defined priorities. A boot device can be a floppy disk, a CD-ROM, a partition located on a hard disk, a device located on a network, or even a USB flash stick.

Typically, Linux is booted from the hard disk, and the MBR on the hard disk contains the basic boot loader. The MBR is a 512-byte sector that is located in the first sector of the disk (0 heads, the first sector of the 0 tracks).

Stage 1 Boot Loader

The basic boot loader in the MBR is a 512-byte image that contains the program source code and a small partition table. The first 446 bytes are the basic boot loader, which contains executable and error message text. The next 64 bytes are the partitioned table, which contains the records generated for each of the four partitions. The MBR ends with two bytes, and the two bytes are defined as the Devil Number (0xaa55). Devil Digital bit MBR verification check service.

The following chart is the anatomy of the MBR:

The basic boot loader's job is to mount and load the second boot loader (second stage). It works by querying the available partitions on the partitioned table. When it finds an active partition, he iterates over the remaining partitions on the table to make sure they are inactive. When this is verified, The boot record on the active partition is read from the device to run in RAM.

Stage 2 Boot Loader

The second, or second, boot loader can be more visually called the kernel loader. The task at this stage is to load the Linux kernel and the optional initial RAM disk.

GRUB阶段 boot loaders:/boot/grub目录包含阶段1,阶段1.5和阶段2的boot loaders,和交替加载器的数量一样.

The first and second stages of the boot loaders together are called Linux Loader (LILO) or grub. Because LILO has some drawbacks and is corrected in grub, we study grub.

The best thing about grub is that it contains an understanding of the Linux file system. Instead of using the native sector on the disk as LILO, GRUB can load a Linux kernel from the ext2 or ext3 file system. It passes the two stage boot The loader is decomposed into a three-stage boot loader. Phase 1 (MBR) boot phase 1.5 boot loader, which understands the special file system that contains the Linux kernel image. Examples include reiserfs_stage1_5 (loading from the Reiser log file system) or e2fs_stage1_5(Load from ext2 or ext3 file system). When the stage 1.5 boot loader is loaded and running, Stage 2 boot loader can be loaded.

As Phase 2 is loaded, grub can present a list of available kernels (defined in /etc/grub.conf , /etc/grub/menu.lst soft connections from and /etc/grub.conf ). You can choose a kernel and you can even modify it with other kernel options. You can also use command line scripting to improve manual control by starting a process.

With the second stage of boot loader running in memory, the file system is queried, and the default kernel images and initrd mirrors are loaded into memory. As the mirror is ready, the boot loader of Stage 2 calls the kernel image.

Kernel
在GRUB中手动启动通过GRUB的命令行,你可以启动一个带有命名的`initrd`的特定内核.就像下面这样:grub> kernel /bzImage-2.6.14.2    [Linux-bzImage, setup=0x1400, size=0x29672e]grub> initrd /initrd-2.6.14.2.img    [Linux-initrd @ 0x5f13000, 0xcc199 bytes]grub> bootUncompressing Linux... OK, booting the kernel.如果你不知道要启动的内核的名称,进需要输入‘/‘,然后按下Tab键即可.GRUB将会显示内核和initrd镜像的列表.

With kernel mirroring running in memory and the control given by the boot loader of Stage 2, the kernel phase begins. Kernel mirroring is not an executable kernel, but a compressed kernel image. This is a zimage (compressed image, less than 512 bytes) or a bzimage ( Large compression mirrors, larger than 512 bytes), they were previously compressed using zlib. At the beginning of this kernel image is a routine that takes some hardware setup and then extracts the kernel image and puts him in high memory. If an initial RAM disk is available, the routine will move him into memory. and Mark later use. Then the routine calls the kernel, and the kernel boot starts.

When Bzimage is called, the program runs from the beginning in the start routine ./arch/i386/boot/head.S . This routine does some basic hardware setup and calls the routines that are located ./arch/i386/boot/compressed/head.S in startup_32 . This routine sets a basic environment (stack, etc.) and empties the Block (BSS) where the symbol begins. The kernel is then decompressed by a call to the function decompress_kernel (located ./arch/i386/boot/compressed/misc.c ). When the kernel is decompressed, he is called. This is another startup_32 function, but the function is ./arch/i386/kernel/head.S in.

In a new startup_32 function (also known as a switch or 0 process), the page table is initialized and the memory page becomes available. The type of CPU with any optional FPU is detected and stored behind. start_kernel The function is then called ( init/main.c ), which takes you to the architecture of the non-specific Linux kernel. This is essentially a function of the Linux kernel. main

Here is the main function stream that the Linux kernel i386 starts:

With the start_kernel call to the function, a series of initialization functions are called to set interrupts, run memory configurations, and load the original RAM disk. Finally, a kernel_thread call to (located arch/i386/kernel/process.c ) begins the init function, which is the first user-space process. Finally, The idle process is started and the scheduler can control it (after the call cpu_idle ). With the interruption available, the preemptive scheduler controls to provide multi-tasking.

During kernel boot, the initialized RAM disk (INITRD) that was loaded into memory by the 2nd stage boot loader is copied into RAM and mounted. This is initrd a temporary root filesystem service located in RAM, And the kernel starts completely without any physical disks hanging. The kernel can be very small after the necessary kernel modules for external devices can be initrd , but a large number of hardware configurations are supported. After the kernel boot, you can formally equip the root file system pivot_root The initrd root file system is unloaded and the real root filesystem is mounted.

initrdThe function allows you to create a micro Linux kernel that is driven to be compiled into a loadable module. These loadable modules give the kernel access to the disk and to the file system on disk, as if it were a driver for other hardware resources. Because the root file system is a file system located on disk, initrd The function provides a way for the boot loader to access the disk and mount the real root file system. In an embedded platform without a hard drive, it can be the initrd ultimate root filesystem, or the final root filesystem can be mounted over a network file system (NFS).

Init

After the kernel is started and initialized, the kernel opens the first user-space app. This is the first program to compile a call using the standard C library. Before this point in this process, no standard C programs are executed.

In a desktop Linux system, the first application that is launched is usually /sbin/init . But it doesn't have to be. There are very few embedded systems that need to init provide extensive initialization (as is the /etc/inittab case with configuration). In most cases, You can invoke a simple shell script to launch the necessary embedded applications.

Summarize

Much like Linux itself, the Linux boot process is very flexible and supports a large number of processors and hardware platforms. At the beginning, the loaded boot loader a simple way to start the Linux.lilo boot loader to extend the boot capability, However, there is a lack of file system awareness. The latest boot loader implementations, such as Grub, allow Linux to boot from the file system.

Linux Startup Process Learning

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.