Using GRUB2 to recover a system that cannot be booted

Source: Internet
Author: User
Tags uuid

Most Linux computers use Grand Unified Bootloader (GRUB)-More specifically GRUB 2-to control transfers from the computer's firmware to the kernel. GRUB 2 provides complex boot-time user interaction capabilities that provide you with the ability to control the boot process. You may not want to use these features every day, but they are important when dealing with problems, such as when a computer fails to boot after performing a kernel upgrade, disk replacement, or other system changes. Some tips and GRUB commands can help you boot your computer in these situations, saving you valuable time. You can also use the Emergency boot disk to boot your normal Linux system, even if there is a problem during the normal initial phase of the boot loader activation.

In general, grub recovery solves two types of problems: the problem that can be solved with the grub's built-in shell and the need for external tools. This article describes both types of problems and uses Super GRUB2 Disk as an example of an external tool that can be used when necessary. (This article focuses on Grub 2, but some of the information provided here also applies to Grub Legacy.) From migrate to Grub 2, you can learn the difference between Grub Legacy and Grub 2. ) understand the control structure of GRUB 2

Before delving into the recovery details, you should understand some of the basics of how GRUB works. The computer's boot process is complex and if the boot process deviates from the boot path, a certain understanding of the boot path can help you solve the problem. I'll start by describing where the boot code and GRUB files are located on your computer so that you can find them, or identify which files might be missing when a missing file causes a problem. The full description of the GRUB configuration file format is outside the scope of this article, but I'll cover the basics of this configuration to help you correct simple errors such as the boot file system that you specified incorrectly. Locate GRUB file

Most boot processes on computers that use the basic input/output system (BIOS) involve code that is stored in various locations on disk. These include master boot records (master boot Record, MBR), an unassigned disk sector, and a boot sector for partitions (also known as Partition boot records [Partition boot Record, PBR]). These records can be overwritten by other boot loaders, overwritten by viruses or low-level disks with the requirements of the GRUB conflict, or corrupted by low-level utilities such as DD. When these corruptions occur, GRUB is unlikely to fully boot, and you may need to use a tool such as Super GRUB2 Disk.

Many newer computers use the Extensible Firmware Interface (extensible firmware Interface, EFI) instead of the BIOS. On these systems, the GRUB code is not stored in the MBR, PBR, or unallocated disk sector. They reside in an EFI boot loader file with an. efi extension on the EFI System partition (ESP), which is a partition that typically appears at the beginning of a disk and has a file allocation table (allocation table, FAT) format. EFI systems are not vulnerable to the same type of low-level boot loader as the BIOS system, but they may fail due to changes to the computer's Non-volatile RAM (NVRAM) settings. In these cases, you may need to perform an emergency boot and then restore GRUB using the Efibootmgr utility as the default boot loader.

In addition to the low-level BIOS or EFI boot files, GRUB 2 relies on traditional files in/boot/grub. These files include file system drivers, video drivers, fonts, and GRUB configuration files (grub.cfg). Because these files are located in the normal Linux file system, the early boot phase must contain at least one basic Linux file system driver. If these files are corrupted, grub may start normally, but you cannot start your operating system, or grub may start and display only one grub> prompt. Edit configuration file

On most systems, the GRUB 2 configuration file is/boot/grub/grub.cfg or/boot/grub2/grub.cfg. However, some EFI based systems place it in a directory on ESP. This directory may be a/boot/efi/efi/grub/grub.cfg or other similar location. In most cases, however, the Grub.cfg file cannot be edited directly, and it is pieced together by a script. You can find some pieces of composition in the/ETC/GRUB.D directory. If you need to change your standard GRUB 2 configuration, you can edit the files.

Regardless of how it is built, GRUB.CFG contains global options and a Boot code section (stanza) that is specific to the operating system or kernel. The Linux distribution can set its global GRUB 2 configuration correctly for most computers. If you have an uncommon configuration, you may need to analyze the global GRUB 2 configuration to find the cause of the problem.

The boot options for GRUB 2 define each operating system or kernel. These boot code sections are typically located in the second part of the Grub.cfg file. Listing 1 shows an example: Listing 1. Boot Linux sample GRUB 2 Code section

Menuentry ' Ubuntu, with Linux 3.2.0-24-generic-pae ' {    recordfail    gfxmode $linux _gfx_mode insmod    Insmod part_gpt    insmod reiserfs    set root= ' (HD0,GPT6) '    search--no-floppy--fs-uuid--set=root 313324F5-A9ED-4E80-B541-DC9E5EEB89FC    linux   /vmlinuz-3.2.0-23-generic-pae root=/dev/sda7 ro quiet Splash $VT _handoff    initrd  /initrd.img-3.2.0-23-generic-pae}

Some key points of the content in Listing 1 include the INSMOD command to load the driver module. The set root line identifies the partition from which the kernel and initial RAM disks are read, but the search line then overwrites this value and finds the partition based on the universal Unique identifier (UUID) number of the file system it contains. The Linux line identifies a Linux kernel and sets the options passed to it. The INITRD line identifies an initial RAM disk file that is passed to the kernel.

interacting with GRUB 2 at boot time

To be able to fix the problem, you first need to understand the normal boot process and how it can be adjusted. Typically, you can use the built-in editor of GRUB 2 to adjust the boot options to fix smaller problems. Sometimes the built-in shell of GRUB 2 can be used to recover from more serious problems. understanding the normal boot process

Traditionally, GRUB shows a text-mode menu (similar to the one shown in Figure 1) to show your boot options. (In many of today's installations, GRUB hides the menu, but it can be pulled out by a button.) ) Figure 1. GRUB Menu

During the normal boot process, use the UP and DOWN ARROW keys to navigate the menu, and then press Enter to select the option you want. On a Linux system, GRUB then loads the kernel and the initial RAM disk and transfers control of the computer to the kernel. Change boot Options

If you see a GRUB option similar to Figure 1, but your options are not yet in effect, there may be a problem with the boot option. GRUB contains a simple text editor that you can use at run time to temporarily change your boot code section. To change the boot options, select the option closest to the target you want to achieve in the GRUB menu, and then press e . The result is similar to Figure 2: Figure 2. GRUB Text Editor

The rows in Figure 2 are the same as those in the boot Code section in Listing 1. You can edit these options just as you would with the text mode editor in Linux. All changes that you perform in this editor are temporary. (To learn how to make permanent changes, see performing a permanent fix, later in this article.) )

Editing a boot code section in a text editor is typically done to perform a one-time boot change. For example, suppose you want to boot to single-user mode to perform low-level maintenance, but there is no single user option in GRUB. To do this, you edit the boot code section and add a single to the end of the Linux line. When you're done, press ctrl-x or F10 to boot, just like the tip at the bottom of the screen.

If you create a new GRUB option but it does not start, you can check this boot option to discover the problem. Perhaps this option contains input errors, such as Linu rather than Linux. Perhaps you omitted the INITRD line. Perhaps you specified the wrong boot file system. In general, you can use the knowledge of your system and GRUB 2 configuration to correct this type of problem. However, in other cases, you may lack the relevant knowledge. For example, you might want to know what the identifier of the boot file system is. In these cases, or when the problem becomes more serious, you can use the GRUB 2 shell. using the GRUB 2 shell

GRUB contains its own built-in shell, where you can type commands similar to those typed in Bash or other Linux text-mode shells. According to the Linux standard, the GRUB shell is simple, but it is sufficient to deal with many of the emergency maintenance tasks. To enter the shell from the GRUB main menu (Figure 1), press C. To enter the shell from the GRUB editor (Figure 2), press ctrl-c or F2. The result is similar to Figure 3: Figure 3. GRUB Shell

The GRUB 2 Shell supports a series of commands, many of which are similar or equivalent to commands in grub.cfg that control the boot process of a menu drive. If you are familiar with the GRUB 2 profile format, you can type commands in the Shell to boot the computer. In fact, a recovery operation is likely to be performed using the shell. Table 1 provides some of the commands that are more likely to be useful in this regard. Grub 2 supports many other commands that you can learn from the grub documentation. table 1. Common GRUB 2 command line commands

Command Options explain
Boot No Boot a defined kernel or chained loader.
Cat FileName Displays the contents of the specified file.
ConfigFile FileName Loads the specified configuration file.
Help [Command] Display Help-a list of commands or help on a specified command.
Initrd FileName Loads the specified initial RAM disk file.
Insmod Module_name

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: 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.