GRUB (GRand Unified Boot loader) boot loader (turn) 2021-03-01

Classification column: Linux-01—Introduction and Installation of Linux
One. Introduction to GRUB

First find out the relationship with GNU GRUB. GNU GRUB is divided into two generations, GNU GRUB Legacy and GNU GRUB2. GNU GRUB Legacy is actually the original GNU GRUB 0.xx, the latest version is GNU GRUB 0.97 released in 2005. The development has been stopped and the name has been changed to GNU GRUB Lagecy. GNU GRUB2 is the second generation of GRUB, which will replace the original GNU GRUB (for example, version 0.9x), but it is still in the development stage and has not yet released an official version.

GRUB4DOS is the secondary development of GNU GRUB Lagecy. The project was first initiated in 2003 by Doian. It is currently maintained by him, bean and several other Chinese people. Many people have contributed code. Coupled with the enthusiastic help of the majority of netizens, GRUB4DOS has been continuously improved. With the new GRUB2 not in place and the old GNU GRUB Lagecy with many imperfections, GRUB4DOS is welcomed by more and more people. Many people have regarded GRUB4DOS as a grub legacy that is still under active development. When their patch for grub 0.XX was stranded, they delivered the patch to GRUB4DOS.

1. Introduction to the bootloader

The boot loader can boot the operating system. When the computer wants to boot the operating system, the BIOS will read the first MBR record on the boot medium. Only one operating system's boot record can be stored in a single MBR. Problems will arise when multiple operating systems are required, so a more flexible boot loader is required.

The master boot record itself should contain two types of content: the boot loader and the partition table. When the BIOS boots, it looks for the data stored in the first sector (MBR) of the hard drive. The BIOS uses the data stored in the MBR to activate the boot loader.

Since the BIOS can only access very little data, most boot loaders boot in two stages. In the first stage of booting, the BIOS boots a part of the boot loader, the initial program loader (IPL). IPL queries the partition table so that it can load data located anywhere on different media. First use this step to locate the second stage boot loader (which contains the rest of the loader).

The second stage boot loader is the main body of the boot loader. This is the real part of the boot loader. It contains parts that require more disk space for the loader, such as the user interface and the kernel boot program.

The boot loader is usually configured as a primary boot loader or a secondary boot loader. The main boot program is the first stage boot loader installed on the MBR. The secondary boot loader is the first stage boot loader installed in the bootable partition. A separate boot loader must be installed on the MBR and configured to transfer control to the secondary boot loader.

Many newer Linux boot loaders are particularly useful because they provide varying degrees of interaction, such as advanced GUIs and encrypted passwords, and the ability to boot by selecting the operating system. In this way, multiple operating systems can coexist on the same machine with multiple physical disks.

2. GRUB overview

GNU GRUB (GRand Unified Bootloader) is a program that installs the boot loader into the master boot record, which is located in the beginning sector of a hard disk. It allows specific commands located in the master boot record area to load a GRUB menu or GRUB command environment. This allows the user to start the choice of the operating system, pass specific commands to the kernel when the kernel boots, or determine some system parameters (such as the available RAM size) before the kernel boots.

GRUB supports direct and chain loading boot methods. GRUB can be used for almost all operating systems, most popular file systems, and almost all hard disks recognized by the system BIOS.

3. GRUB features

GRUB contains many features, which makes GRUB more superior than other available boot loaders. Some of the more important features are listed below.

(1) GRUB provides a real command-line-based environment on x86 machines before the operating system starts.

(2) GRUB supports logical block addressing (LBA mode).

The main function of LBA is to find the address of the file on the drive and place it in the drive's hardware. It is used in many IDE and SCSI hard drives. Before using LBA, the hard drive encountered a 1024 cylinder limitation, that is, the BIOS cannot find files after 1024 cylinders (such as a boot loader or kernel file). As long as the system BIOS can support LBA mode (most of them do), then LBA allows GRUB to exceed the 1024 cylinder limit and boot the operating system.

(3) GRUB configuration can be read every time the system boots.

This avoids the need to rewrite the master boot record every time the user changes the boot option. Most boot loaders cannot read configuration files and use them to set boot options. For example, the user must change a LILO configuration file, and then execute a command to rewrite the new configuration data back to the system's master boot record. This method is more dangerous than the method used by GRUB, because a misconfigured master boot record will make the system unbootable. In using GRUB, if the configuration file is misconfigured and booted, it simply goes to a default command line, allowing users to manually enter commands to run the operating system.

two. GRUB device name

GRUB requires the device name to be enclosed in a (). fd means floppy disk, hd means hard disk (does not distinguish between IDE and SCSI). Secondly, the device is numbered starting from 0, the same is true for the partition, and the partition and the device are separated by a','.

Here are a few examples:
(fd0): indicates the entire floppy disk
(hd0,1): indicates the second partition of the first hard disk in the BIOS
(hd0,0)/boot/vmlinuz: indicates the first one in the BIOS The vmlinuz file in the boot/ directory under the first partition of the hard disk.

If a partition is not specified, it means that the entire device is used, otherwise only the specified partition is used.

Hard Disk Code

Hard disk search order    

Code name in GRUB

First
    

(hd0)(hd0,0)(hd0,1)(hd0,4)(hd0,5)… …

the second
    

(hd1)(hd1,0)(hd1,1)(hd1,4)(hd1,5)… …

The third
    

Hd2) hd2,0) hd2,1) hd2,4 hd2,5)… ...

the fourth
    

(hd3)(hd3,0)(hd3,1)(hd3,4)(hd3,5)… …

… …
    

… … … …

Hard disk search order
    

LILO
    

GRUB

IDE1 master
    

hda, hda1, hda2
    

(hd0), (hd0,0), (hd0,1)

IDE1 slave
    

hdb, hdb1, hdb2
    

(hd1), (hd1,0), (hd1,1)

IDE2 master
    

hdc, hdc1, hdc2
    

(hd2), (hd2,0), (hd2,1)

IDE2 slave
    

hdd, hdd1, hdd2
    

(hd3), (hd3,0), (hd3,1)

three. GRUB configuration file

The GRUB configuration file defaults to "/boot/grub/grub.conf". After GRUB is successfully installed in the master boot sector (MBR) of the hard disk, you can configure GRUB by editing this file without rewriting GRUB to MBR. . GRUB configuration is done through a configuration file located in /boot/grub/grub.conf.

password –-md5 <密码1>
default=0
timeout=5
splashimage=(hd0,0) /boot/grub/splash.xpm.gz
color red/black green/blue
title FC
password –-md5 <密码2>
root (hd0,1)
kernel /vmlinuz-2.6.12-EL ro root=/dev/hda1 quiet vga=787
initrd /initrd-2.6.12-EL.img
title Windows
password –-md5 <密码3>
rootnoverify (hd0,0)
chainloader +1

1.#

Those beginning with "#" are comment lines.

2.default=0

This must be compared with title. Take the above list as an example, don’t we have two titles? Arranged in front and back order, the first title represents 0, the second title1, and so on. This default means that if no other items are selected during the startup process, then the default value (the first title) will be used to start.

3.timeout=5

The number of seconds to display the menu at startup (in seconds). If no button is pressed at this time, the default setting value will be used to start.

4.splashimage=(hd0,0) /boot/grub/splash.xpm.gz

Splashimage is some pictures or related image data displayed on the menu. One aspect of this setting is more interesting. Because there is no hard disk during the boot process, we must clearly indicate which directory in which partition a certain file is located; therefore, the above setting means: /boot/grub/splash in the (hd0,0) partition. xpm.gz, this file is the screen displayed at startup.

5.color red/black green/blue

The color of the GRUB interface can be specified with the color command. Two groups of colors are specified in color. The first group is the color of normal text, and the second group is the color of highlighted text (the currently selected menu item). Corresponding to each group of colors, two colors need to be specified. The first is the foreground color, and the second is the background color. Therefore, a total of four colors need to be specified in color, namely, normal text foreground, normal text background, bright text foreground and bright text background.

color code

black (black) red (red) green (green) brown (brown) blue (blue) magenta (purple) cyan (cyan) light-gray (gray)

dark-gray (dark gray) light-red (bright red) light-green (bright green) yellow (yellow) light-blue (bright blue) light-magenta (bright purple) light-cyan (bright blue) white (white)

The foreground color can use all 16 colors, and the background color can only use the first 8 colors.

The color command is generally used as a global command and used before the first title.

6.hiddenmenu

Indicates that the menu is hidden at startup, unless you press ESC before timeout to see the menu. If you want to display the menu, comment out this setting value.

7.title Fedora Core()

Define the name of the boot project.

8.password --md5 $opeVt0$Y.br.18LyAasRsGdSk

Used to define the password for entering GRUB command mode. You can also define a password for each operating system by putting the password command after the title line. Moreover, the boot password of each operating system can be different, or it can be different from the password that enters the command mode, which guarantees the security of GRUB to the greatest extent. The password encrypted with MD5 is specified here.

9.root (hd0,0)

Used to specify the partition containing the stage file. If there is a /boot partition, the root device is the /boot partition, otherwise it is the / partition.

10.kernel /boot/vmlinuz-2.6.12-EL ro root=/dev/hda1 quiet vga=787

Specify the operating system kernel file, you can also add parameters after the kernel file name. For example, kernel /vmlinuz ro root=LABEL=/ means to mount / partition in read-only mode, and the root partition device is the device whose label is /; you can also give the device name directly.

Decimal code and corresponding resolution and color quality

Color quality/resolution    

640x480
    

800x600
    

1024x768
    

1280x1024
    

Rank

256
    

769
    

771
    

773
    

775
    

8

32768
    

784
    

787
    

790
    

793
    

15

65536
    

785
    

788
    

791
    

794
    

16

16.8M
    

786
    

789
    

792
    

795
    

32

11.initrd /boot/initrd-2.6.12-EL.img

Used to specify RAM Disk files, such as initrd /initrd-2.xx.img.

12.rootnoverify (root)

Similar to root, but will not attempt to mount the partition. For example, it is used to specify the partition where the windows operating system is located.

13.chainloader +1

Used to load another boot loader, usually the boot loader used to load windows. Its parameter is a block list, for example, chainloader (hd0,0)0+1 means to load the first block of the first partition of the first disk, 0 is the starting position (blocks are numbered from 0), and +1 means total How many blocks to read. So chainloader 2+0 means to read the third block (number 2).

14.makeactive

Let the boot items in the boot area (remember the display result of fdisk -l?) have an active flag, regardless of whether it is added or not.

Unlike the configuration file of LILO, grub.conf will be read at boot time and there is no need to update the MBR when it is modified.

There are many other parameters that can be used in the grub.conf file, but the above parameters are enough to make the machine usable. For further information on these and other parameters of grub.conf, please refer to the GRUB man page (man grub.conf).

four. GRUB installation

[root @test root]# grub
# First install it under Super Block and try it out:
grub> root (hd0,0) <==Here, enter the root hard disk sector I want!
Filesystem type is ext2fs, partition type 0x83

grub> setup (hd0,0)   <==实际安装上来!使用 setup 指令!
Checking if "/boot/grub/stage1" exists... yes
Checking if "/boot/grub/stage2" exists... yes
Checking if "/boot/grub/e2fs_stage1_5" exists... yes
Running "embed /boot/grub/e2fs_stage1_5 (hd0,0)"... failed (this is not fatal)
Running "embed /boot/grub/e2fs_stage1_5 (hd0,0)"... failed (this is not fatal)
Running "install /boot/grub/stage1 (hd0,0) /boot/grub/stage2 p /boot/grub/menu
.lst "... succeeded
Done.

# Then install it on MBR and have a look:
grub> root (hd0,0)
Filesystem type is ext2fs, partition type 0x83

grub> setup (hd0)
Checking if "/boot/grub/stage1" exists... yes
Checking if "/boot/grub/stage2" exists... yes
Checking if "/boot/grub/e2fs_stage1_5" exists... yes
Running "embed /boot/grub/e2fs_stage1_5 (hd0)"... 17 sectors are embedded.
succeeded
Running "install /boot/grub/stage1 (hd0) (hd0)1+17 p (hd0,0)/boot/grub/stage2
/boot/grub/menu.lst"... succeeded
Done.
grub> quit             <==离开 grub 啰!

Fives. GRUB's MD5 encryption method

1. Use grub-md5-crypt to generate GRUB md5 password

Use grub-md5-crypt to encrypt the GRUB password. For example, we want to set the grub password to 123456, so we first need to use md5 to encrypt the 123456 password

# /sbin/grub-md5-crypt

PassWord: Enter 123456 here

Retype password: enter 123456 again

$1$7uDL20$eSB.XRPG2A2Fv8AeH34nZ0

$1$7uDL20$eSB.XRPG2A2Fv8AeH34nZ0 is the value generated after encryption is performed by grub-md5-crypt. We have to write down this value, it is still useful.

2. Change /etc/grub.conf

For example, the contents of my original /etc/grub.conf file are as follows.

default=1
timeout=10
splashimage=(hd0,7)/boot/grub/splash.xpm.gz
title Fedora Core (2.4.22-1.2061.nptl)
root (hd0,7)
kernel /boot/vmlinuz-2.4.22-1.2061.nptl ro root=LABEL=/
initrd /boot/initrd-2.4.22-1.2061.nptl.img
title Windows XP
rootnoverify (hd0,0)
chainloader +1

So I want to add the password --md5 $1$7uDL20$eSB.XRPG2A2Fv8AeH34nZ0 line in /etc/grub.conf, and lock, where should I add it, please see the following change example;

timeout=10
splashimage=(hd0,7)/boot/grub/splash.xpm.gz
password --md5 $1$7uDL20$eSB.XRPG2A2Fv8AeH34nZ0
title Fedora Core (2.4.22-1.2061.nptl)
lock
root (hd0,7)
kernel /boot/vmlinuz-2.4.22-1.2061.nptl ro root=LABEL=/
initrd /boot/initrd-2.4.22-1.2061.nptl.img
title 视窗系统XP
rootnoverify (hd0,0)
chainloader +1

Lock means to lock Redhat Fedora. If it will prompt an error when starting. At this time, you should press the P key, and then enter the password.

Use password, lock commands to implement several encryption methods as follows:

1) Simply encrypt the GRUB interface, but not the booted system. Add a line below the timeout line: password --md5 PASSWORD

2) Encrypt the GRUB interface and at the same time encrypt the booted system. Add a line under the timeout line: password --md5 PASSWORD Add a line under the title line: lock

3) There are multiple booted systems at the same time, and they are separately encrypted for specific system instances (the GRUB operation interface is not encrypted). Add a line below the title line: lock and add a line immediately below the lock line: password --md5 PASSWORD Note : Lock cannot be used independently.

Let's take a closer look. From the /etc/grub.conf we changed above, is the password generated by /grub-md5-crypt used in the first step? ? Is it a little safe?

six. GRUB interface

1. Start menu interface

After the Linux operating system is correctly installed, you can boot the system from the hard disk to enter GRUB. The boot menu interface is shown in the figure below, and the keys that can be used in this interface are shown in the table below. GRUB configuration file can be selected in this menu interface

button
    

Function Description

↓↑
    

Use the up and down arrow keys to move between the startup menu items

Enter
    

Enter the enter key to start the current menu item

e
    

Select the "e" key to edit the current boot menu item

a
    

Select "a" key to add kernel startup parameters

c
    

Select the "c" key to enter GRUB's command line mode (shell)

2. Start menu item editing interface

In the GRUB boot menu interface, select the "e" key to enter the GRUB boot menu item editing interface, as shown in the figure below. The operation keys that can be used in this interface are shown in the following table. This interface provides a flexible configuration interface, which is very useful for debugging the operating system startup configuration.

button
    

Function Description

↓↑
    

Use the up and down arrow keys to move between the startup menu items

b
    

Select the "b" key to start the current menu item

e
    

Select the "e" key to edit the currently selected line

d
    

Select the "d" key to delete the current line

c
    

Select the "c" key to enter GRUB's command line mode

The
    

Select the "o" key to insert a line after the current line

THE
    

Select the "O" key to insert a row before the current row

Esc
    

Select "Esc" to return

3. Command line interface (shell)

GRUB has two ways to enter the command line interface. Enter the command line interface from the GRUB boot menu or use the GRUB command to enter the command line interface in the shell state. The command line interface obtained by the two methods is slightly different. Because the grub command runs in the Linux operating system, many commands cannot be used due to the restrictions of the operating system. The commands supported by the GRUB boot menu to enter the command line interface are relatively complete.

(1) Features of GRUB command line interface

The GRUB command line interface provides a convenient and friendly way of command line interaction. Its main features include:

    Provide online help command "help", and get detailed help for each command.
    You can use the left and right arrow keys to edit the line command.
    You can use the up and down arrow keys to scroll through the history commands.
    You can use the "Tab" key to complete commands and paths.

(2) Enter the command line interface from the GRUB boot menu

Select the "c" key from the GRUB boot menu interface or the menu item editing interface to enter the GRUB command line interface. As shown below. Description: Use the "Esc" key to return to the menu interface, use the help command to get the currently available commands in GRUB, and use a command as the parameter of the help command to get a detailed help description of the command.

(3) Enter the GRUB command line interface from the Linux shell

You can also use the grub command to enter the GRUB command line interface. The full path of the command is "/sbin/grub".

Seven. GRUB command reference

The commands in GRUB can be divided into three categories:

    Menu commands can only be used in the global part of the configuration file.
    Conventional commands can be used in all parts of the configuration file and can be used in the command line interface.
    Command line and menu item commands can be used in the menu item definition part of the configuration file to be used in the command line interface.

1. Menu command

Menu commands can only be used in the global configuration part of the grub configuration file, and cannot be used in the grub command line interactive interface. The menu commands should be placed before other commands in the configuration file.
1. default // set the menu item to start by default
2. fallback // set the menu item to return after a menu item fails to start
3. hiddenmenu // hide the menu interface
4. timeout // set the delay time for the menu to automatically start
5 , Title //Start a menu item

2. Regular command

General commands can be used in the configuration file and the grub command line interactive interface. The general commands that can be used are
1. bootp //initialize the network device through bootp
2. color // set the color of the menu interface
3. device // specify the device file as the drive
4. dhcp //Initialize the network device through DHCP
5. hide //Hide a partition
6. Ifconfig // Manually configure the network device
7. Pager // Change the state of the internal page program
8. partnew // Create a new primary partition
9. Parttype //Change the partition type
10, password, set password
11, rarp for the menu interface , //initialize network settings through RARP
12, serial //set serial device
13, setkey //set keyboard mapping
14, splashimage //set GRUB startup Background image file
15, termainal // select terminal type
16, tftpserver // specify TFTP server
17, unhide // restore a hidden partition

3. Command line and menu item commands

The command line and menu item commands can be used in the menu item settings of the GRUB configuration file, and can also be used in the GRUB command interactive interface.
1. bolcklist // Display the location of the partition where a file is located (block list notation)
2. boot // Start the operating system
3. cat // Display the file content
4. Chainloader // Give the boot control to another boot loader
5 , Cmp // compare two files
6, configfile // load the existing GRUB configuration file
7, debug // set to debug mode
8, displayapm // display APM BIOS information
9, displaymem // display memory configuration
10, embed / /Embed Stage 1.5 file
11, find //Find all devices that include a file
12, fstest //Test the file system
13, geometry //Display the physical information of a drive
14, halt //Stop the computer operation (software shutdown)
15, help //Display GRUB command help information
16, impsprobe //Query symmetric multiprocessor (SMP) information
17, initrd //Load initrd file
18, install //Install GRUB
19, ioprobe //Query the input and output of a drive (I/O) port
20, kernel //boot the operating system kernel
21. lock // lock a GRUB navigation menu item so that it can be started after entering a password
22, makeactive // ​​activate a main partition
23, map // virtual map a drive
24, md5crypt // use MD5 encryption password
25, module //Load module
26, modulenounzip //Load module without decompression
27, pause //Pause and wait for key
28, quit //Exit GRUB
29, reboot //Restart the computer
30, read //Read the content in memory
31 , Root //Set GRUB's root device
32, rootnoverify //Device GRUB's root device but do not mount the file system
33, savedefault //Save the current boot menu item as the default startup
34, setup //Automatically install GRUB
35, testload / /Test read a file from the file system
36, testvbe //Test VESA BIOS EXTENSION
37, uppermem //Forcibly set the size of the host's upper memory
38, vbeprobe //Query VESA BIOS EXTENSION information

Eight. GRUB repair

1. What should I do if grub does not display the menu?

When I enter the grub interface after booting, but there is no menu, only a grub> prompt is left. How to start it? Don't worry, look below:

grub>cat (hd0,6)/boot/grub/grub.conf (为了看参数)
grub>root (hd0,6)
grub>kernel (hd0,6) /vmlinuz-2.4.18-14 ro root=LABEL=/
grub>initrd (hd0,6) /initrd-2.4.18-14.img
grub>boot

Start it! Some of the above figures need to be changed according to your actual situation. The above method can also be used to test the newly compiled kernel.

2. Recover grub damaged by Windows

If you use grub to boot Linux and windows, when windows goes wrong and reinstalled, grub in the MBR will be destroyed, and grub needs to be restored.

1. Put the first one of the Linux installation CD into the CD-ROM drive, then restart the machine, and boot the system with the CD-ROM drive in BOIS.

2. After the installation interface comes out, press the F4 key, which is the linux rescue mode.

3. A series of keyboards and a few simple preparations, and then "continued" the process, not to mention here, it is relatively simple.

4. Then there will be a prompt like this: sh#

5. We can operate GRUB. Enter grub: sh#grub and a prompt like this will appear: grub>
we can enter: grub>root (hdX, Y) grub>setup (hd0) after such a character

If successful, there will be a successful...

Here X, if it is a disk, is 0, if the root partition of the Linux you are installing is on the second hard disk, then X is 1; Y is the root partition where the Linux system is installed. setup (hd0) is to write GRUB to the MBR of the hard disk.

3. Use installation disk to repair Grub in Linux operating system

1. Put the first piece of the installation disk in the CD-ROM drive, then restart the machine, and boot the system with the CD-ROM drive in BOIS.

2. After the installation interface comes out, press the [F4] key, which is the linux rescue mode.

3. A series of keyboards and a few simple configurations, and then [continue]. . . This process, I won't talk about it, is relatively simple.

4. Then there will be such characters:

sh#

5. We can operate GRUB.

sh#grub

Characters like this will appear:

grub>

We can enter after such a character:

grub>root (hdX,Y)
grub>setup (hd0)
If successful, there will be a successful...

Here X, if it is a disk, is 0, if the root partition of the linux you are installing is on the second hard disk, then X is 1; Y is the root partition where the linux system is installed. setup (hd0) is to write GRUB to the MBR of the hard disk.

Let me give you an example, if I take my hard drive as an example. I installed XP on the first hard disk and installed RH73 on the hda 9 of the first hard disk. I have two hard disks in total. If I lose GRUB, I use this method to get it back. If you don't know which partition your linux is installed on, that is, you don't know what Y is, it doesn't matter, first enter root (hdX, and then use [TAB] to view it, and you will understand it.

The operation is as follows: [There are instructions on the previous boot up]

sh# grub

The following words will appear:

grub>

Then do this again. If I know that my linux is installed on the first hard disk, but I don’t know which partition it is installed on, I can enter root (hd0, and then use the [TAB] key to complete it, and then I will understand Grub>root (hd0,8)
grub>setup (hd0)

Finally, press the [RESET] key, restart, and it will be OK.

If there are multiple Windows systems, how can they be booted out? You should use hide and unhide instructions; for example, we have installed two Windows, one is windows 98 located at (hd0,0), and the other is WindowsXP located at (hd0,1); then we will use it hide instruction;

title Win98
unhide (hd0,0)
hide (hd0,1)
rootnoverify (hd0,0)
chainloader +1
makeactive

title WinXP
unhide (hd0,1)
hide (hd0,0)
rootnoverify (hd0,1)
chainloader +1
makeactive
——————————————
Copyright statement: This article is a CSDN blogger "ztguang "The original article is in compliance with the CC 4.0 BY-SA copyright agreement. Please attach the link to the original source and this statement for reprinting.
Original link: https://blog.csdn.net/ztguang/article/details/51011784

Guess you like

Origin blog.csdn.net/ccpit2b2c/article/details/114266040