Kernel Startup Process

Source: Internet
Author: User

The specific requirements are as follows:

Grub source code analysis: How does grub support startup selection of multiple systems and kernels (multiboot mechanism ).

1. Introduction to grub (introducing the mainstream system management program grub)

What is grub;
GNU grub is a multi-operating system startup manager. GNU grub is derived from grub (Grand uniied bootloader. Grub was initially designed and applied by Erich Stefan Boleyn;
"Boot Loader" is the first software to run after the computer is started. It is responsible for loading the kernel of an operating system and giving control to the kernel. The kernel is responsible for the remaining initial processes.
Flexibility is an important feature of grub. Grub can recognize the file system and binary kernel, so you can load any operating system without recording the kernel in
The physical location of the disk.

Ii. Grub status quo
Currently, grub is divided into grub legacy and grub 2. The version number is 0.9x and earlier versions are called grub legacy. From 1.x, grub 2 is called. Currently
Grub legacy has stopped development. It is only out of the Bug fix state that no new features are added. All development tasks are transferred to gurb 2.

Iii. Description of grub and stage files (grub1)


Grub contains several images files, two basic (required) stages files (stage1 and stage2), an optional stage (also known as stage1.5), and two network-guided images files
Nxgrub and pxegrub ). Stage1 is a necessary image file used to guide gurb. Usually it is embedded into MBR. Or in the boot sector of a partition. Because the PC Boot Sector is
512 bytes, so stage1 is also 512 bytes. Stage1 is used to load stage 2 or stage 1.5 from a local disk. Due to the size limit, stage1 has
Stage 1.5 is encoded, that is, stage1 cannot recognize the file system format. Stage2 is the core image of grub. It handles almost everything (except loading itself ),
You can usually put it on a file system, but it is not necessary. Once you execute grub-install and Install Boot Loader, The stage1 file is not required. You can move it to any location because it has been embedded into MBR or PBR. Stage1 and stage2 files are generally located in the/boot/GRUB/directory. There are many stage 1.5 files in this directory, all of which are named in the file system format. They aim to build a bridge between stage1 and stage2, that is, loading stage 1.5 and stage2 in Stage 1. The difference between stage1 and stage 1.5 is that stage1 cannot identify the file system, and stage 1.5 can. Because stage2 is too large to be embedded in a fixed area, stage1.5 can be installed after MBR.

Iv. Multi-Operating System Boot manager and working principle
The system boot guide manager is the first program that runs after the computer is started. It is used to load and transmit data to the operating system kernel. Once the kernel is mounted, even if the task of the System Boot manager is completed and exited, other parts of the system boot, such as the system initialization and startup process, are completely controlled by the kernel;
Grub and lilo are the most common operating systems in Linux, BSD, or other UNIX operating systems;

5. Startup Process

After the computer starts, the BIOS will find the first device that can be started (usually hard disk), load the boot program from the MBR, and then hand over the control to this code. MBR is located within the first 512 bytes of the hard disk.

Grub has two different startup methods. One is to directly load an operating system, the other is to chain to load another start loader, and then it will actually load an operating system. In general, the former is more expected, because you do not need to install or maintain other boot loaders, and grub is flexible enough to load an operating system from any hard disk or partition. However, the latter is sometimes needed because grub does not naturally support all existing operating systems.

 

Grub first version

Step 1 of grub is included in MBR. Due to the MBR size limit, what step 1 does is basically the next step of loading grub (stored elsewhere on the hard disk ). Step 1 can be loaded either directly Step 2 or Step 1.5: grub Step 1.5 is included in 30 kilobytes after MBR. Step 1.5 load step 2.

After step 2 is started, it displays an interface for users to select the OS to start. This step is usually in the form of a graphic menu. If the graphic mode is unavailable or you need more advanced control, you can use the grub command line prompt to pass through it, you can manually specify the startup parameters. Grub can also be set to automatically start from a certain kernel after timeout.

 

Grub Version 2

Similar to the first version of grub, boot. IMG is in MBR or in the boot partition like step 1, but it can be read from one slice of any lba48 address. IMG) will read the core. IMG (originated from diskboot. IMG) to read the core. the rest of the IMG. Core. under normal circumstances, IMG is stored in the same place as Step 1.5 and has the same problems. However, moving to a file system or a pure partition causes less trouble than moving or deleting in Step 1.5. Once read, core. IMG reads the default configuration file and other required modules.

 

After grub is started

Once the startup option is selected, grub loads the selected kernel into the memory and gives control to the kernel. In this step, for windows and other operating systems that do not support multi-boot standard, grub can also send control to other starters through chain startup. In this case, the startup programs of other operating systems are saved by grub. Unlike the kernel, other operating systems are started directly from MBR. A Windows-like Startup Menu may be another startup manager, which allows further selection among multiple operating systems that do not support multiple boot. (This is the case where a modern Linux is installed on an existing windows system or a system with multiple Windows versions without modifying the original operating system .)

6. How does grub support multiple systems?

6.1 how to use grub to directly start an OS

Multi-boot is the original format supported by grub. For convenience, we also support Linux, FreeBSD, NetBSD, and OpenBSD. If you want to start other operating systems, you will chain them to load

Run Command Line to start

Chain to load an OS

Multi-boot is not supported, and grub does not have special support for Linux, FreeBSD, NetBSD, and OpenBSD, this involves loading another boot loader and then redirecting to it in real mode.

The chain loader command is used for this purpose. It is usually necessary to load some grub modules to set the appropriate root device. In combination, we can conclude that for a Windows system on the first partition of the first hard disk:

Menuentry "Windows "{

Insmod chain

Insmod NTFS

Set root = (hd0, 1)

Chainloader + 1

}

An additional solution may be required for a system with multiple hard disks.

Chain loading is only supported on pcbios and EFI platforms.

6.2gnu/Linux

It is relatively easy to start GNU/Linux from grub because it is similar to starting a multi-boot compatible OS.

1. Set the root device of grub to be the same as that of GNU/Linux. The search -- file -- set/vmlinuz command or similar command may help you.

2. Use the command Linux to load the kernel:

Grub> Linux/vmlinuz root =/dev/sda1

If you need to specify some kernel parameters, you just need to attach them to the command. For example, if you set 'acpi 'to 'off', you need to do this:

Grub> Linux/vmlinuz root =/dev/sda1 ACPI = off

For complete information on available options, refer to the documentation in the Linux source code tree.

In Linux, grub uses the 32-bit protocol. Some BIOS services in this protocol, such as APM or EDD, are unavailable. In this case, you need to use linux16:

Grub> linux16/vmlinuz root =/dev/sda1 ACPI = off

3. If you use an initrd, run the following command after Linux:

Grub> initrd/initrd

If you use linux16, you need to use initrd16:

Grub> initrd16/initrd

4. Finally, run the command boot.

Note: If you use an initrd and specify the 'mem = 'option to the kernel so that it uses less memory than the actual size, you will also specify the same memory size to grub. To make grub know the size, run the uppermem command before loading the kernel.

6.3 DOS/Windows

Grub cannot start DoS or Windows directly, so you must chain them to load. However, their boot loaders have some critical defects, So chain-based loading alone may not work. To solve this problem, grub provides you with two helper functions.

If you have installed DoS (or Windows) outside the first hard disk, you must use the hard disk Swap Technology because the OS can only be started from the first hard disk. The work und used by grub is the command drivemap, like this:

Drivemap-s (hd0) (hd1)

This performs a virtual switch between your first and second hard disks.

Note: This only works when DOS (or Windows) uses BIOS to access the switched hard disk. This may not work if the OS uses a special drive for the hard disk.

If multiple DoS or windows are installed on the hard disk, another problem may occur, because multiple primary partitions used for DOS/Windows may cause confusion. There is no doubt that you should avoid doing so, but if you want to do so, there is a solution. Use partition hiding/unhide technology.

If grub hides a DoS (or Windows) partition, DOS (or Windows) ignores this partition. If grub unhides a DoS (or Windows) partition, DOS (or Windows) detects this partition. In this way, if you have installed DoS (or Windows) on the first and second partitions of the first hard disk and want to start from the first partition, as shown below:

Parttool (hd0, 1) Hidden-

Parttool (hd0, 2) Hidden +

Set root = (hd0, 1)

Chainloader + 1

Parttool $ {root} boot +

Boot

 

 

 



Kernel Startup Process

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.