Summary of the Grub Startup Process in Linux

Source: Internet
Author: User
Article Title: summary of the Grub Startup Process in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Linuxer around the world knows what grub is, but many friends may be confused about the process from MBR to grub and then to the specific operating system. This is not the case. A friend of cu sent such a help post:

If a computer is installed with WIN2000, I am now installing LINUX and GRUB. If GRUB is installed in the primary partition, GRUB will directly guide LINUX and WIN2000, I can understand, because the MBR contains GRUB STAGE1 (right ?), MBR guides the system to jump to DBR (active partition) by checking the DPT partition information. When will the active partition be set? So how does one handle the WIN boot information in the MBR when GRUB is installed in the MBR? Is it true that when GRUB is installed to MBR, GRUB will set the partition where GRUB is located as the active partition? Then, during GRUB boot, MBR will find the active partition and find the desired file, and then continue? If you want to load GRUB to other partitions (non-primary boot areas), how does it first start GRUB? Isn't MBR first? Since it has been installed in other partitions and has not been changed to the primary boot zone, the primary boot zone is still the WIN2000 boot data. How can GRUB be started first? Why? Is it related to the active partition? I can see which system is written in the data and which system is started as the active partition. However, in that case, it seems that it cannot be explained, that is, at the very beginning, it is hard to understand and understand.

The following is a summary of the analysis and discussion on this issue by each cu player.

First, let's take a look at the traditional Boot process: loading and running Master Boot Record (MBR) Main Boot zone content (such as lilo ). Scan the partition table, locate the active partition, and load the content of the pilot sector on the active partition to the memory for execution.

The system boot process consists of the following steps)

1. Start the instance;

2. BIOS Power-On Self-Test (POST -- Power On Self Test). The memory address is 0fff: 0000;

3. Read the first Sector of the hard disk (0-headed, 0-way, 1-Sector, that is, Boot Sector) into the memory address 0000: 7c00;

4. Check (WORD) 0000: 7dfe whether it is equal to 0xaa55. If it is not equal to, it resends to try other media. If there is No other starting media, it displays "No rom basic" and then crashes;

5. Jump to 0000: Execute the MBR program at 7c00;

6. Copy the MBR to 0000: 0600 and continue the execution;

7. Search for active partitions in the primary partition table. If no active partition is found or more than one active partition is found, stop;

8. Read the first sector of the active partition into the memory address 0000: 7c00;

9. Check (WORD) 0000: 7dfe whether it is equal to 0xaa55. If it is not equal, "Missing Operating System" is displayed, and then stop, or try to start a floppy disk;

10. Jump to 0000: 7c00 and continue executing the Startup Program of the specific system;

11. Start the system.

When grub is installed to a logical partition, the logical partition loaded by grub must be set as active. However, at this time, grub took over the action after 11 steps: Read grub. conf from stage 1.5. The configuration and user selection determine the next Bootstrap action.

Grub is usually installed in two cases. For MBR installation, GRUB directly overwrites the original MBR boot program. This is why we need to switch back to the "original windows boot mode". We only need to use dos to boot fdisk/mbr. Note that 0000: 7c00 appears in two places in step 1-11. Whether it is dos boot sector or nt loader, it runs from 0000: 7c00. In fact, this small program for developing partition management in ms is equivalent to inserting it in bios boot sector. Grub is also written as a subroutine starting from memory 0000: 7c00, so it is easy to understand that grub can be loaded by BIOS and dos MBR.

After self-check, the boot right is handed over to the hard disk MBR, and grub is started. Windows/linux can be guided by grub. Note: linux does not have to be installed in the active partition, because the boot program is in the MBR! However, windows must be installed in the active partition (bootable windows). The second windows may not be installed in the active partition, but its boot file must be in the active partition.

The general order is:

Grub --> windows?> Find the boot file-boot loading-start windows

Grub --> linux-> Find the boot file (/boot)?> Boot-start linux

If grub is installed on another partition, isn't it MBR? This is the primary partition where grub is installed. It must be set as an active partition. Because MBR (physical master boot partition) does not actually have OS-related boot programs, MBR usually only scans and reads the subsequent Partition Table and finds the corresponding active partition, read and run the 512-byte program for the first sector of the corresponding active partition. The program is responsible for further guiding the corresponding system of the corresponding partition. Therefore, the approximate running order is

BIOS> MBR> GRUB> menu.

In this way, the actual process can be summarized as follows:

1. Start the instance;

2. BIOS Power-On Self-Test (POST -- Power On Self Test). The memory address is 0fff: 0000;

3. Read the first Sector of the hard disk (0-headed, 0-way, 1-Sector, that is, Boot Sector) into the memory address 0000: 7c00;

4. Check (WORD) 0000: 7dfe whether it is equal to 0xaa55. If it is not equal to, it resends to try other media. If there is No other starting media, it displays "No rom basic" and then crashes;

5. Jump to 0000: Execute the MBR program at 7c00;

6. MBR first copies itself to 0000: 0600, and then continues to execute. If XP is installed first and LINUX is not installed in MBR, then the data in this MBR is still the data written by WIN, it is used to search for active partitions in the primary partition table. In this case, you must set the primary partition where GRUB is located as the active partition, in this case, GRUB can be started normally, and STAGE1 of GRUB can be tuned to STAGE1.5 and others to guide the entire system. If you install XP first and then LINUX, but GRUB is installed in MBR, STAGE1 is directly transferred to the memory, and STAGE1 is calling STAGE1.5 and STAGE2 to guide the system. At this time, you do not need to set the primary partition where other GRUB files are located as active partitions. It directly calls STAGE1.5, and then calls STAGE2 to identify the file system, to enable multi-start.

7. Search for active partitions in the primary partition table. If no active partition is found or more than one active partition is found, stop;

8. Read the first sector of the active partition into the memory address 0000: 7c00;

9. Check (WORD) 0000: 7dfe whether it is equal to 0xaa55. If it is not equal, "Missing Operating System" is displayed, and then stop, or try to start a floppy disk;

10. Jump to 0000: 7c00 and continue executing the Startup Program of the specific system;

11. Start the system.

A little bit of information:

Grub that can work normally should include the following files: stage1, stage2, * stage1_5, menu. lst.

The size of stage1 must be 512 bytes. It must be installed (written to) the Master Boot Record of a hard disk or an active partition (this partition should be marked with fdisk as bootable). The primary and only role of stage1 is to find the stage2 file that you store somewhere on the hard disk to complete subsequent work.

Stage2 files can exist in a specific file system. For example, if you split a linux partition and create an ext2 file system on it, copy the file to a directory in the partition. You can also store stage2 directly in a location on the hard disk, that is, a location not partitioned. However, it seems that not many people will do this.

Because the capacity of stage1 is limited (the size of the Master Boot Record MBR and the startup sector can only be 512 bytes), it cannot be identified by the file system, if you store stage2 on an ext2 or fat file system, how can you find this file? This requires the stage00005 files mentioned above, which are responsible for interpreting the file system. The corresponding stage00005 file must be called when stage2 is placed on a file system in what format. For example, if you store stage2 in the ext2 format file system, e2fs_stage1_5 is required; if stage2 is stored in the fat format file system, fat_stage1_5 is required.

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.