Grub Startup Process Analysis & GRUB boot program Configuration

Source: Internet
Author: User
When the computer is started, the program in the BIOS first executes self-check. After the self-check is passed, find the boot loader program in the mbr of the first disk that can be started according to the CMOS configuration (usually in the first physical sector of the boot disk, accounting for 416 bytes), and give the control to the boot loader, further completed by Boot Loader Operating SystemKernel loading. After the boot loader finds the kernel, it gives control to the operating system kernel, and the kernel continues to start the system.

It can be seen that boot loader is the second program to be executed during computer startup, and it is the key program to guide the operating system. Boot Loader that can guide the operating system mainly includes Lilo, grub, and MBR programs in windows. Grub is one of the most widely used and excellent boot Bootstrap programs. 51testing software testing network Phal % ~. GD

^ 1h5 ^ "hqd? U "x0GrubName a device51testing software testing network {* w9z; uh) ojyl

First, the device name of grub must be included in parentheses (). Secondly, grub does not distinguish the IDE hard disk from the SCSI hard disk, and uses hdx in a unified manner. x specifies the hard disk number in the BIOS, and starts counting from 0, and the IDE Hard Disk number is smaller than the SCSI hard disk number. Third, grub uses fdx to specify the floppy disk device, and X is the floppy disk drive number. Below are some examples of device naming for GRUB: Q (fd0) indicates 1st floppy disks Q (hd0, 1) indicates 1st partitions of 2nd hard disks Q (hd0, 0) /boot/vmlinuz indicates the vmlinuz file in the boot/directory under the first partition of the 1st hard disks. If no partition is specified, the entire device is used. Otherwise, only the specified partition Q (hd0, 2, A) is used for FreeBSD. FreeBSD has a concept of slice, A partition is further divided into several slice, which indicates that it is slice a in the 1st partition of the 3rd hard disk.
9j [q ~ * Cdroz "L $ E0 Grub Execution Process

After the system is powered on, the programs in the BIOS first perform self-check on the system hardware. After the self-check is passed, load the MBR on the boot disk and give control to the programs in the MBR (stage1 ), execute stage1 to determine whether it is grub. If stage00005 is configured, load stage00005. Otherwise, it is transferred to the start sector. Then, stage2 is loaded and executed, stage2 uses stage1_5 to drive the file system and finds grub. conf. The Boot menu is displayed for the user to choose from. Then, the operating system kernel is loaded based on the user's choice or default configuration, and control is handed over to the operating system kernel. The operating system startup is completed by the kernel.

Grub involves several important files:

The first one is stage1. It is installed in the MBR sector (1st sectors of 0-sided 0 channels), with a size of 512 bytes (446-byte code + 64-byte Partition Table + 2-byte flag 55aa ), it is responsible for loading the Start Program stored in zero-face, zero-track, and 2nd-sector. The second is stage1_5. Stage00005 is responsible for identifying file systems and loading stage2, so stage00005 often has multiple to support reading from different file systems. When grub is installed, grub loads the corresponding stage00005 to 3rd sectors of the 0-side 0 track based on the current/boot/partition type. Stage1_5 is loaded by START. The third is stage2. It displays the boot menu and provides user interaction interfaces, and loads the operating system kernel based on user selection or default configuration. Unlike the first two files, stage2 is stored on the disk/boot/grub. The fourth is menu. lst (/boot/GRUB/grub. conf link ). Grub. conf is a script-based text file that contains the configuration displayed in the menu and the kernel loading configuration of each operating system. Grub displays the Startup menu based on grub. conf and provides an interactive interface with the user. Grub loads the corresponding kernel program based on the selected or default configuration and grub. conf Kernel configuration, and gives control to the kernel program, so that the kernel program can start the real operating system. In addition to the main files described above, grub also includes some disk programs that support interactive functions. It mainly includes grub, grub-install, grub-md5-crypt and grub-terminfo and/usr/bin/mbchk under/sbin/, as well as the device image file (device.map)and menu single background file (splash.xpm.gz) under/sbin ). Through the above analysis, we can easily see that grub actually contains two parts, one of which is installed in the special sector of the disk, and the other part exists as a file. To enable grub to start the operating system, you must first install grub's stage1 and stage00005 (automatically select whether to install according to the file system) to the special sector of the disk. In addition, grub exists in the disk/boot/grub. conf, device. map and other files and programs supporting interaction, and these programs must be in the path specified by the PATH environment variable. With this knowledge, we believe that it is not difficult to install, configure, back up, or repair grub. 51testing software testing network q) TT |/| #?
Install Grub To MBR51testing software testing network E4X wmji 'vt HX

Grub WorkThe directory is under/boot/grub, and make install does not install it to/boot/grub. After the installation is complete, perform the following operations: A)Copy all the files in the/usr/local/share/GRUB/i386-pc/directory to the/boot/GRUB/directory. DownloadSource code package./configure, make, make install)
2z + R? E5ih k a/|. R3 | 0 # CP/usr/local/share/GRUB/i386-pc // */boot/GRUB/ B)Create the grub. conf file under/boot/grub and create a soft link to grub. conf. Menu. lst # CD/boot/GRUB# Touch grub. conf# Ln-s grub. conf menu. lstNote! If grub. conf already exists, it is best not to create it again. You can directly modify it if necessary. C)Check whether/usr/local/sbin and/usr/local/bin are in the value of the PATH variable by running the following command # Env | grep pathIf/usr/local/sbin and/usr/local/bin are not in the PATH variable, run the following command to modify them :# Export Path = $ path/usr/local/sbin:/usr/local/bin D)Installing grub to mbrgrub can be called by BIOS only in MBR during startup. Therefore, grub must be installed in MBR for BIOS call. In fact, stage1 is installed in MBR, or stage1_5 may be installed based on the file system. Below are several examples of installing GRUB :# Grub-install/dev/hda //Install grub on the mbr of the first 1st IDE Hard Disks # Grub-install/dev/SDA// Install grub to the MBR of 1st SCSI hard disks # Grub-install/dev/fd0// Install grub on a floppy disk # Grub-install/dev/hda1// Install grub to the 0 sector of the 1st fast hard drive. This method is often used when other boot programs are used to guide the system, so as not to overwrite other boot programs. E)To install grub in grub command line mode and its operation methods, see section 3. Press the C key when the system displays the Start menu, or execute the/usr/sbin/GRUB program on the command line after the system starts, you can enter the grub command line mode. The basic process for installing grub in command line mode is as follows: Q Specify start deviceGrub> Root (hd0, 0)# Except root, space is required, and no space is allowed in other positions! Here (hd0, 0) refers to the 1st partitions of 1st hard disks. If you cannot determine the partition of the stage1 file containing grub, you can use the find command to find and determine: grub> Find/boot/GRUB/stage1# Searching for stage1grub will search for the file/boot/GRUB/stage1 and display the device name containing the file. This device is the device used above. Q Install GrubGrub> Setup (hd0)# Except after setup, space is required, and no space is allowed in other positions! This command will install grub in the MBR of 1st hard disks. If you do not want to install grub in the MBR but want to install grub in the pilot sector of a partition, you can run the following command to install GRUB: grub> Setup (hd0 , 0)# Except after setup, space is required, and no space is allowed in other positions! This will install grub in the boot sector of the 1st partition of the 1st hard drive. Q Exit GrubGrub> Quit F)Restart the machine. The newly installed grub takes effect.

51testing software testing network uho0mj * BM
Grub
Configuration File example

Grub. conf is the configuration file of grub. Its structure is simple and can be divided into two parts: the first part is global configuration, and the other part is the startup configuration of each operating system. You can configure menus for multiple operating systems. The following is a specific example.

#=========================/ Boot/GRUB/grub. confFile examples============================51testing software testing network 0z ^ s + r2x (_ r C6 ~ C # l
Timeout 30 # Wait for the time (in seconds) for the menu item to be selected. If the time-out period is exceeded, the default option 51testing Software Testing Network s YZR }? 3Y
Default 0 # default option, option 1: 51testing software testing network Q # [MSN,} I sm
Fallback 1 # If the first item fails, start the following Backup option 51testing software testing network F/sa0b9n} 1q
Splashimage = (hd0, 0)/GRUB/splash.xpm.gz # grub startup Screen
Ep dqe + G d u-f0
V & ^ | 7o0f \ 0#StartLinuxConfiguration
B _/J] W * \ x0titlered Hat Linux Enterprise as3 for syd168 # menu title of the startup Item
JC "Crk" l0root (hd0, 0) # specify the root file system, which is/boot of 1st hard disks in 1st sectors. 51testing Software Testing Network | 'lm/i6f7e @ # Yu
Kernel/vmlinuz-2.4.18 Ro root = label =/# kernel in/boot
: B0h: s1z, [0 initrd/initrd-2.4.18-14.img # Start ramdisk under/boot 51testing software testing network! A $ VR _ o; q + dak
51testing software testing network 1qn P; t9s2 ?) P % F '@
#StartWindowsIf onlyLinuxNo51testing qhg. D * C $ L: y6pc
Title Windows2003 enterprise for syd168 # Windows Startup menu title
O) b2l0 ^, ma7r '0root noverify (hd0, 1) # The operating system is in the second partition of hd0, without mount51testing software testing network YN, a "O h" Z ~ 'N4b5e! Ea8 {u
Chainloader + 1 # boot windows51testing software testing network y from the second partition of the first hard disk) BRV | $ D: XJ ~

In/z "E; D0#=========================/ Boot/GRUB/grub. confFile examples============================
X3p) x "G & @; B? 8sq [0

The preceding example briefly describes how to configure Linux and Windows in grub. For more startup configurations, refer to the following content. 5. Bootstrap multi-system configurationGrub supports multi-OS boot. After being guided by grub, you can enter the command line mode or menu mode. You can use the flexible command line mode to select the operating system for each partition and specify the boot parameters. Grub supports three boot Methods: Direct boot of the operating system kernel, indirect boot through chainload, and network boot of the operating system. Linux, FreeBSD, OpenBSD, NetBSD, and gun Mach supported by grub can be directly guided, but grub does not support operating systems (such as Windows ), the second method chainload is required. Let's take a look at these guidance methods: (1) direct guidance
, T9r (q0co h1v0 configuration process is usually as follows:
_ PN $ km0 ~ A [0a) use the root command to set the root device that contains the operating system kernel
C0h n2j9? 4lm0b) use the kernel command to load the kernel image file. If there are parameters in the kernel boot, you can add the parameters directly to the 51testing software testing network 3ez0e {: Y (Y % L, V $ C ,@
C) use module or modulenounzip to load the kernel module 51testing software testing network {~; Lt9 'd6mf6i! W
D) Start boot.
+ T8koz s0x A: \ 0 (2) chainload pilot
Z [f r: I ^ ss0a) configure the grub root device, use rootnoverify (hdx, Y) to specify 51testing Software Testing Network + L@4l6eg.x (j)
B) Start the boot and use chainloader + 1 to specify the boot records. "+ 1" indicates that grub reads the first sector of the partition. 51testing software testing network? Q Gs. I
C) Start boot.
Wt! A $ x *} # Al A0 and above are general chainloader methods. For DOS and Windows, you can simply use two commands for guidance: chainloader (hdx, Y) + 1, then boot, where X and Y are used to specify the Partition Number.
-W % V3S! A9c; M | RHR. y0 (3) boot from the network:
2 h @ J % BK! To enable grub to support network boot, j4h0 needs to enable the network support option during compilation (see netboot/readme. netboot in the source file ). In addition, you must set two services in the Network: Dynamic IP Service (BOOTP, DHCP, or RARP) and FTP service. Then, run BOOTP, DHCP, or RARP for different servers. If everything is correctly set, grub will provide the IP address, the IP address netmask, the IP address of the TFTP server, and the IP address of the gateway. Finally, obtain the image file of the operating system from the Internet. The following is an example: 51testing Software Testing Network + w0] I g. gw7nzm3 \
Grub> bootp51testing Software Testing Network # jr2s i7j4ack
Probing... [NE * 000]
Z3 ^ ~? \! A4y; j0i0ne2000 base...
* C r (do $ n t' 0address: 192.168.110.23 netmask: 255.255.255.0
7j9ynj $ SML} "v0server: 192.168.110.14 Gateway: 192.168.110.1
3jon9t9ncyz0grub> root (ND)
'P ~ SD ~ 0 grub> kernel/tftproot/gnumach.gz root = sd0s1
Oqv. A # dc0 ========================================================== ====================================
/X F & eh-K0

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? Assume that grub is installedOthersPartition (non-primary boot zone), how does one implement grub first? 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, it seems that there is no way to explain it, that is, at the very beginning, I have never understood it, but I cannot understand it clearly.

 

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.

 

SystemGuiding ProcessIt consists of the following steps (taking hard drive as an example)

 

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 the memory of 0000: 7c00, so it 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 boot file-boot loading-start Windows

Grub --> Linux-> Find the boot file (/BOOT)-> Boot Load-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 the 512-byte program of the first sector of the corresponding active partition and run it. This program is responsible for guiding the corresponding system of the corresponding partition step by step. 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 of other grub files 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.

Reference Source:
1thuud ~ 3b4g0ftp: // alpha.gnu.org/gnu/grub/51testingsoftware testing network 2oz'nls (R '~ \
Http://www.linuxeden.com/html/sysadmin/20070530/26936.html51testingsoftware testing network 4pcz7kuswe

Http://soft.zdnet.com.cn/software_zone/2007/1006/537431.shtml

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.