Home > System Tutorial > LINUX > body text

How to configure and troubleshoot GRUB

WBOY
Release: 2024-01-04 11:00:35
forward
946 people have browsed it

This article will introduce you to the knowledge of GRUB and explain why you need a boot loader and how it adds functionality to the system.

The Linux boot process starts from when you press the power button of your computer until you have a fully functional system. The entire process follows these main steps:

  • 1. A process called POST (Power-On Self-Test) will perform a comprehensive check on your computer hardware components.
  • 2. When POST completes, it transfers control to the bootloader, which then loads the Linux kernel (and initramfs) into memory and execute.
  • 3. The kernel first checks and accesses the hardware, then runs the initialization process (mainly known by its common name init), then the initialization process starts some services, and finally completes the system startup process.

In the seventh lecture of the series ("SysVinit, Upstart, and Systemd"), we introduced some of the service management systems and tools used by modern Linux distributions. You may want to review that lesson before continuing.

GRUB Boot Loader Introduction

On modern systems you will find two main GRUB versions (one is the v1 version sometimes called GRUB Legacy, and the other is ##v2 version), although most latest version distribution systems use the v2 version by default. Today, only Red Hat Enterprise Linux 6 and its derivatives still use version v1.

Therefore, in this guide, we will focus on the features of the

v2 version.

Regardless of the version of

GRUB, a bootloader allows the user to:

    Modify the behavior of the system by specifying the use of different kernels;
  1. Select one to start from multiple operating systems;
  2. Add or edit configuration blocks to change startup options, etc.
Today, the

GNU project maintains GRUB and provides extensive documentation on their website. While you are reading this guide, we strongly recommend that you take a look at the official GNU documentation.

When the system boots, you will see the following

GRUB screen on the main console. Initially, you can follow the prompts to select a kernel among multiple kernel versions (by default, the system will boot with the latest kernel), and you can enter the GRUB command line mode (using c key), or edit startup items (press the e key).

如何配置并排除 GRUB故障

GRUB splash screen

One of the reasons you might consider booting with an older version of the kernel is if a hardware device that previously worked fine is "acting up" after an upgrade (for example, you can refer to this thread in the AskUbuntu forum Link).

During startup,

GRUB v2 will be read from the /boot/grub/grub.cfg or /boot/grub2/grub.cfg file. Configuration file, while the configuration file used by GRUB v1 comes from /boot/grub/grub.conf or /boot/grub/menu.lst. These files should not be manually edited directly, but should be updated via the contents of /etc/default/grub and the files in the /etc/grub.d directory.

On

CentOS 7, when the system initially completes the installation, the following configuration file will be generated:

GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="vconsole.keymap=la-latin1 rd.lvm.lv=centos_centos7-2/swap crashkernel=auto  vconsole.font=latarcyrheb-sun16 rd.lvm.lv=centos_centos7-2/root rhgb quiet"
GRUB_DISABLE_RECOVERY="true"
Copy after login
In addition to online documentation, you can also use the following command to consult the GNU GRUB manual:

# info grub
Copy after login
If you are particularly interested in the options available in the

/etc/default/grub file, you can directly consult the help documentation in the configuration section:

# info -f grub -n 'Simple configuration'
Copy after login

使用上述命令,你会发现 GRUB_TIMEOUT 用于设置启动画面出现和系统自动开始启动(除非被用户中断)之间的时间。当该变量值为 -1 时,除非用户主动做出选择,否则不会开始启动。

当同一台机器上安装了多个操作系统或内核后,GRUB_DEFAULT 就需要用一个整数来指定 GRUB 启动画面默认选择启动的操作系统或内核条目。我们既可以通过上述启动画面查看启动条目列表,也可以使用下面的命令:

在 CentOS 和 openSUSE 系统上
# awk -F/' '$1=="menuentry " {print $2}' /boot/grub2/grub.cfg
Copy after login
在 Ubuntu 系统上
# awk -F/' '$1=="menuentry " {print $2}' /boot/grub/grub.cfg
Copy after login

如下图所示的例子中,如果我们想要使用版本为 3.10.0-123.el7.x86_64 的内核(第四个条目),我们需要将 GRUB_DEFAULT 设置为 3(条目从零开始编号),如下所示:

GRUB_DEFAULT=3
Copy after login

如何配置并排除 GRUB故障

使用旧版内核启动系统

最后一个需要特别关注的 GRUB 配置变量是 GRUB_CMDLINE_LINUX,它是用来给内核传递选项的。我们可以在 内核变量文件 和 man 7 bootparam 中找到能够通过 GRUB 传递给内核的选项的详细文档。

我的 CentOS 7 服务器上当前的选项是:

GRUB_CMDLINE_LINUX="vconsole.keymap=la-latin1 rd.lvm.lv=centos_centos7-2/swap crashkernel=auto  vconsole.font=latarcyrheb-sun16 rd.lvm.lv=centos_centos7-2/root rhgb quiet"
Copy after login

为什么你希望修改默认的内核参数或者传递额外的选项呢?简单来说,在很多情况下,你需要告诉内核某些由内核自身无法判断的硬件参数,或者是覆盖一些内核检测的值。

不久之前,就在我身上发生过这样的事情,当时我在自己已用了 10 年的老笔记本上尝试了衍生自 SlackwareVector Linux。完成安装后,内核并没有检测出我的显卡的正确配置,所以我不得不通过 GRUB 传递修改过的内核选项来让它工作。

另外一个例子是当你需要将系统切换到单用户模式以执行维护工作时。为此,你可以直接在 GRUB_CMDLINE_LINUX 变量中直接追加 single 并重启即可:

GRUB_CMDLINE_LINUX="vconsole.keymap=la-latin1 rd.lvm.lv=centos_centos7-2/swap crashkernel=auto  vconsole.font=latarcyrheb-sun16 rd.lvm.lv=centos_centos7-2/root rhgb quiet single"
Copy after login

编辑完 /etc/default/grub 之后,你需要运行 update-grub (在 Ubuntu 上)或者 grub2-mkconfig -o /boot/grub2/grub.cfg (在 CentOS 和 openSUSE 上)命令来更新 grub.cfg 文件(否则,改动会在系统启动时丢失)。

这条命令会处理早先提到的那些启动配置文件来更新 grub.cfg 文件。这种方法可以确保改动持久化,而在启动时刻通过 GRUB 传递的选项仅在当前会话期间有效。

修复 Linux GRUB 问题

如果你安装了第二个操作系统,或者由于人为失误而导致你的 GRUB 配置文件损坏了,依然有一些方法可以让你恢复并能够再次启动系统。

在启动画面中按下 c 键进入 GRUB 命令行模式(记住,你也可以按下 e 键编辑默认启动选项),并可以在 GRUB 提示中输入 help 命令获得可用命令:

如何配置并排除 GRUB故障

修复 Linux 的 Grub 配置问题

我们将会着重关注 ls 命令,它会列出已安装的设备和文件系统,并且我们将会看看它查找到的东西。在下面的图片中,我们可以看到有 4 块硬盘(hd0hd3)。

貌似只有 hd0 已经分区了(msdos1 和 msdos2 可以证明,这里的 1 和 2 是分区号,msdos 则是分区方案)。

现在我们来看看能否在第一个分区 hd0msdos1)上找到 GRUB。这种方法允许我们启动 Linux,并且使用高级工具修复配置文件,或者如果有必要的话,干脆重新安装 GRUB:

# ls (hd0,msdos1)/
Copy after login

从高亮区域可以发现,grub2 目录就在这个分区:

如何配置并排除 GRUB故障

查找 Grub 配置

一旦我们确信了 GRUB 位于 (hd0, msdos1),那就让我们告诉 GRUB 该去哪儿查找它的配置文件并指示它去尝试启动它的菜单:

set prefix=(hd0,msdos1)/grub2
set root=(hd0,msdos1)
insmod normal
normal
Copy after login

如何配置并排除 GRUB故障

查找并启动 Grub 菜单

然后,在 GRUB 菜单中,选择一个条目并按下回车键以使用它启动。一旦系统成功启动后,你就可以运行 grub2-install /dev/sdX 命令修复问题了(将 sdX 改成你想要安装 GRUB 的设备)。然后启动信息将会更新,并且所有相关文件都会得到恢复。

# grub2-install /dev/sdX
Copy after login

其它更加复杂的情景及其修复建议都记录在 Ubuntu GRUB2 故障排除指南 中。该指南中阐述的概念对于其它发行版也是有效的。

总结

本文向你介绍了 GRUB,并指导你可以在何处找到线上和线下的文档,同时说明了如何面对由于引导加载相关的问题而导致系统无法正常启动的情况。

幸运的是,GRUB 是文档支持非常丰富的工具之一,你可以使用我们在文中分享的资源非常轻松地获取已安装的文档或在线文档。

你有什么问题或建议吗?请不要犹豫,使用下面的评论框告诉我们吧。我们期待着来自你的回复!


The above is the detailed content of How to configure and troubleshoot GRUB. For more information, please follow other related articles on the PHP Chinese website!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template