Let grub save your bootloader.

Source: Internet
Author: User

Nothing is more irritating than bootloader bad, give full play to the role of Grub 2, let bootloader work well. Why do you say that?

    1. Grub 2 is the most popular bootloader and is used on almost all Linux distributions.
    2. Bootloader is a vital software, but it's very easy to damage.
    3. Grub 2 is a boot loader with scalability and flexibility, and offers a number of customizable options.

Grub 2 is a powerful software that is not the only one in the bootloader world, but is the most popular and is used by almost all major desktop distributions. Grub has two jobs: first, it displays all the installed operating systems on the computer with a menu for you to choose from and secondly, when you select a Linux operating system from the boot menu, grub loads the kernel of the corresponding version of Linux, and it is well known that using Linux requires Bootloader, however, it is a part of the Linux distribution that few people fully understand.

      1, the most important part of Grub 2 is a bunch of text files and two script files, the first thing you need to know is

/etc/default/grub

      , this is a text file that you can set in the general configuration variables and other features of the Grub 2 menu (see "Common user Settings" below).
      2. Another important part of Grub 2 is

/etc/grub.d

      folder, where all the scripts that define each menu item are placed here, the names of these scripts must have a two-bit numeric prefix, which is intended to define the order in which the scripts are executed and the order of the corresponding menu items when the Grub 2 menu is built. For a chestnut: file 00_header is read first, responsible for parsing

/etc/default/grub

      configuration file, then the Linux kernel menu item, located in the 10_linux file, the script in the default

/boot

      partitions Create a regular menu item for each kernel and a restore menus entry.
      3, followed by third-party applications called scripts, such as 30_os-prober and 40_custom, Os-prober script for the kernel and other partitions operating system in the creation of menu items, it can identify the installed

Linux, Windows, BSD

      as well

Mac OS X

      , of course, if your hard drive layout is unique and makes Os-prober unable to find the installed release, you can add a menu item to the 40_custom file (see "Adding custom menu Items" below).
      4. Grub 2 does not require you to manually maintain the configuration file for your startup options: Instead, use the Grub2-mkconfig command to generate

/boot/grub/grub.cfg

      file. This function will parse

/etc/grub.d

      scripts in the directory and

/etc/default/grub

      settings file to define your settings.
Graphical guided repair

Thanks to the Boot repair application, we only need to click the button, Grub 2 can help us solve a lot of problems easily, this beautiful and small application has a more intuitive user interface, can scan and identify a variety of hard disk layout and partitioning scheme, but also to identify and correctly recognize the installed operating system, This application can handle both the master boot Record (MBR)of a traditional computer and the GUID Partition table GUID of the new UEFI computer Partition table (GPT). The easiest way to use boot Repair is to install it into a live Ubuntu session, launch the Ubuntu live release on a bootloader corrupted machine, and install the boot Repair by adding its PPA repository first, as follows:

sudo add-apt-repository ppa:yannubuntu/boot Repair

Then refresh the repository list:

sudo apt-get update

Install the application as follows:

sudo apt-get install-y Boot Repair

After the installation is finished, the application will be launched, and the interface (consisting of a pair of keys) before it can be scanned for your hard drive, then the user is able to repair most of the broken bootloader by pressing the Recommended repair recommended repair button as instructed by the tool, repairing After bootloader, the tool will output a short URL (it is recommended to record it), this URL contains the hard disk details: partition information and important Grub 2 files (such as/etc/default/grub and/boot/grub/grub.cfg Content, if the tool does not solve bootloader problem, you can share this URL in your distribution forum, so that other people can analyze your hard disk layout to give you advice.

Bootloader First Aid

The Grub 2 boot issue causes the system to be in several different states, and the text displayed on the screen (where the Bootloader menu should be displayed) indicates the current state of the system:

    • If the system is aborted at the grub> prompt, it indicates that the GRUB 2 module has been loaded, but the Grub.cfg file cannot be found.
    • Currently the full version of the Grub 2 command-line shell, you can resolve this issue in a number of ways. If you see a grub rescue> prompt, it indicates that bootloader cannot find the Grub 2 module or can not find any boot files for boot file.
    • If your screen displays only the word ' GRUB ', it indicates that bootloader cannot find the most basic information that is normally located in the master boot record of the main bootstrap.

You can fix such errors by using the live CD or in the Grub 2 shell, and if you're lucky, Bootloader has the grub> prompt and you get the Grub 2 Shell's power to help you out, and the next few commands work in GRU B> and Grub rescue> prompt, set pager=1 command settings display paging pager, preventing text from rolling over the screen, you can also use the LS command to list all the partitions identified by grub, as follows:

Grub> ls (hd0) (HD0,MSDOS5) (HD0,MSDOS6) (HD1,MSDOS1)

As you can see, this command lists the partitions and lists the partition table scheme (that is, msdos). You can also use LS on each partition to find your root filesystem:

Grub> ls (hd0,5)/lost+found/var/etc/media/bin/initrd.gzboot/dev/home/selinux/srv/tmp/vmlinuz

You can not write the Msdos section of the partition name, similarly, if you forget the trailing slash trailing slash only input ls (hd0,5), then you will get the partition information, such as the file system type, the overall size and the last modification time, if you have multiple partitions, you can use cat to read /etc/issue The contents of the file to determine the release version, in the form of:

Cat (hd0,5)/etc/issue

Assuming you find the root filesystem in (hd0,5), make sure it contains the /boot/grub directory, and the kernel image you want to boot into, such as Vmlinuz-3.13.0-24-generic, enter the following command at this time:

Grub> set root= (hd0,5) grub> linux/boot/vmlinuz-3.13.0-24-generic root=/dev/sda5grub> initrd/boot/ Initrd.img-3.13.0-24-generic

The first command takes grub to the partition where we want to boot into the distribution, then the second command tells you where the grub kernel image is in the partition, where the root file system is located, and the last line sets the location of the virtual file system initial RAMDisk file, which you can use tab Full function complement kernel name and virtual file system initrd:initial RAMDisk name, save time and effort, input complete, enter boot at the next grub> prompt, grub will boot into the specified operating system. If you are at the Grub rescue> prompt, the situation will be slightly different. Because Bootloader is not able to find and load any required modules, you need to add these modules manually:

Grub rescue> Set root= (hd0,5) Grub rescue> insmod (hd0,5)/boot/grub/normal.modgrub rescue> normalgrub> Insmod Linux

As shown above, as before, using the LS command to list all partitions, using the SET command to mark up, and then add the normal module, this module will be restored to the standard grub> mode, if the Linux module is not loaded, the next command will be added, if the module has been loaded , you can point the bootloader to the kernel image and the virtual file system Initrd file as before, and then use boot launch release, prefect! Once the release has started successfully, don't forget to regenerate the new configuration file for Grub, using the command:

Grub-mkconfig-o/boot/grub/grub.cfg

You also need to install a copy of the bootloader in the MBR, using the command:

sudo grub2-install/dev/sda

If you want to disable the script in the /etc/grub.d directory, you just need to remove its executable bit, such as using the chmod-x/etc/grub.d/20_memtest86+ will be able to "Memory Test" option to remove from the menu.

Grub 2 and UEFI

In UEFI-enabled machines (most of the machines listed in recent years) debugging the broken Grub 2 added difficulty, restoring Grub 2 installed on UEFI machines and installing on non-UEFI machines is not very different, just new firmware processing method is not the same, resulting in many kinds of recovery results, for UEFI-based systems, do not install anything on the MBR, instead, you install the Linux EFI bootloader in the EFI System partition EFI Systems Partition (ESP) and use the tool to set it as the EFI default launcher, which For Linux users It is efibootmgr , and for window users it is bcdedit . As it stands, Grub 2 should be properly installed before installing any major Windows 8 compatible desktop Linux distributions, and if bootloader is damaged, you can use the live distribution to repair the machine and, when you start live media, make sure it is in UEFI mode The boot menu for each removable drive of the computer will be two: one normal and one EFI tag, using the EFI variable in the /sys/firmware/efi/ file. In the live environment, mounting the installation of the previous tutorial to hang off the system's root file system, in addition to mount the ESP partition, assuming that the partition is /dev/sda1 , you can mount as follows:

sudo mount/dev/sda1/mnt/boot/efi

Then use modprobe efivars to load the Efivars module before chroot to the installed release. Fedora Users can reinstall bootloader using the following command

Grub2-mkconfig-o/boot/grub2/grub.cfgyum Reinstall Grub2-efi Shim

The grub2-mkconfig-o/boot/grub2/grub.cfg command is to generate a new configuration file. Ubuntu users use the following command instead

Apt-get Install--reinstall GRUB-EFI-AMD64

Once the bootloader is properly seated, exit chroot, unmount all partitions, and reboot to the Grub 2 menu.

Grub lost

The best feature of Grub 2 is that it can be reinstalled at any time, so when other systems like Windows are replaced with their own bootloader, which causes grub 2 to be lost, you can use the live distribution to reinstall Grub in a few steps, assuming you're in /dev/ Sda5 installs a release version, to reinstall Grub, you only need to first create a mount directory for the release with the following command:

sudo mkdir-p/mnt/distro

Then mount the partition as follows:

Mount/dev/sda5/mnt/distro

The Grub can then be re-installed as follows:

Grub2-install--ROOT-DIRECTORY=/MNT/DISTRO/DEV/SDA

This command overwrites the MBR information on the /DEV/SDA device, points to the current Linux system, and rewrites some Grub 2 files, such as Grubenv and Device.map, and another problem is common on computers with multiple distributions: When you install a new Li Nux release, its bootloader should be able to find all the installed distributions, once not, just boot into the new installation of the release version, and run

Grub2-mkconfig

Before running this command, make sure that the root partition of the missing release version in the boot menu is mounted, and if the distribution you want to add has a separate /root and/home partition, before running Grub2-mkconfig, simply mount the containing /root partition, although Grub 2 can find most distributions, an additional step is required to try to add the installed Fedora System in Ubuntu: If you installed fedora with the default settings, the release installer has created an LVM partition You need to install the LVM2 driver using the release version of the package management system, as follows

sudo apt-get install lvm2

After execution, the Grub 2 os-prober script will be able to locate and add Fedora to the boot menu.

Common user Settings

Grub 2 has a number of configurable variables, and here are some of the common variables you might most likely modify in the /etc/default/grub file:

    • The Grub_default variable specifies the default startup item, which can be set to a numeric value, such as 0, to indicate the first menu item, or to "saved", which points to the menu item that was selected at the last startup.
    • The grub_timeout variable specifies the dwell time before the default menu item is started.
    • Grub_cmdline_linux lists the kernel command line parameters to pass to all LINUX menu items.
    • When the Grub_disable_recovery variable is set to True, the Recovery Mode menu item is not generated, which launches the release version in single-user mode, allowing you to repair the system with command-line tools.
    • The Grub_gfxmode variable is also useful, which specifies the resolution of the text display on the menu, which can be set to any value supported by your graphics card.

Grub 2 has a command-line mode, which is accessed by pressing C on the bootloader menu.

thorough repair of bootloader

If the Grub2-install command does not work properly, leaving you unable to boot into Linux, you need to completely reinstall and reconfigure the bootloader, you need to use the powerful chroot function to switch the runtime from the live CD environment to the Linux we want to repair the security Installation location, any Linux live CD with chroot tools can do this, but you need to make sure that the system architecture of live media is consistent with the architecture of the system on your hard disk, so if you want to chroot to a 64-bit system, you must use the AMD64 live distribution. After booting into the live release, you first need to check the partitions on the machine, use fdisk-l to list all partitions on the disk, and log the partition of the Grub 2 system you want to repair, assuming we want to recover from the release version installed in/dev/sda5 bootload Er. Start the terminal using the following command to mount the partition:

sudo mount/dev/sda5/mnt

In this case, you need to bind Bindgrub 2 bootloader to enter the directory to detect other operating systems:

$ sudo mount--bind/dev/mnt/dev$ sudo mount--bind/dev/pts/mnt/dev/pts$ sudo mount--bind/proc/mnt/proc$ sudo mount --bind/sys/mnt/sys

You can leave the live environment to enter the release version installed in the /dev/sda5 partition by chroot:

$ sudo chroot/mnt/bin/bash

You can now install, detect, and upgrade Grub, as before, using the following command to reload Bootloader:

sudo grub2-install/dev/sda

Because the Grub2-install command cannot create the grub.cfg file, it needs to be created manually, as follows

sudo grub-mkconfig-o/boot/grub/grub.cfg

This should be all right, now you have a new copy of Grub 2, listing all the operating systems and distributions on the machine, and before restarting the computer, you need to exit the chroot system in turn and unmount all partitions as follows:

$ exit$ sudo umount/mnt/sys$ sudo umount/mnt/proc$ sudo umount/mnt/dev/pts$ sudo umount/mnt/dev$ sudo umount/mnt

Now you can safely restart the computer, and it should fall back to Grub 2 control, you have fixed this bootloader.

Add a custom menu item

If you want to add a menu item to the Bootloader menu, you need to add a boot section boot stanza to the 40_custom file, for example, you can use it to show a menu item to launch a Linux distribution installed in a removable USB drive, assuming your USB drive is SDB1, and the Vmlinuz kernel image and the virtual file system Initrd are located in the root (/) directory, add the following to the 40_custom file:

Menuentry "Linux on USB" {   set root= (hd1,1)   linux/vmlinuz root=/dev/sdb1 ro quiet splash initrd/initrd.img   }

Using their UUID can achieve more accurate results than using device and partition names, such as

Set Root=uuid=54f22dd7-eabe

Use the following command to get the UUID of all connected drives and partitions:

sudo blkid

You can also add a menu item to a distribution on your disk that is not found by the Os-prober script, as long as you know where the distribution is installed and where the kernel and virtual file system Initrd are located.

Free to provide the latest Linux technology tutorials Books, for open-source technology enthusiasts to do more and better: http://www.linuxprobe.com/

Let grub save your bootloader.

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.