mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-10 22:21:11 +08:00
Translated tech/20170316 An introduction to GRUB2 configuration for your Linux machine.md
This commit is contained in:
parent
7cf8c21dbd
commit
3db4b875b3
@ -1,138 +0,0 @@
|
||||
ictlyh Translating
|
||||
An introduction to GRUB2 configuration for your Linux machine
|
||||
============================================================
|
||||
|
||||
> Learn how the GRUB boot loader works to prepare your system and launch your operating system kernel.
|
||||
![An introduction to GRUB2 configuration in Linux](https://opensource.com/sites/default/files/styles/image-full-size/public/images/life/car-penguin-drive-linux-yellow.png?itok=ueZE5mph "An introduction to GRUB2 configuration in Linux")
|
||||
>Image by : Internet Archive [Book][5] [Images][6]. Modified by Opensource.com. CC BY-SA 4.0
|
||||
|
||||
When researching my article from last month, _[An introduction to the Linux][1][ boot and startup process][2]_ , I became interested in learning more about GRUB2\. This article provides a quick introduction to configuring GRUB2, which I will mostly refer to as GRUB for simplicity.
|
||||
|
||||
### GRUB
|
||||
|
||||
GRUB stands for _GRand Unified Bootloader_ . Its function is to take over from BIOS at boot time, load itself, load the Linux kernel into memory, and then turn over execution to the kernel. Once the kernel takes over, GRUB has done its job and it is no longer needed.
|
||||
|
||||
GRUB supports multiple Linux kernels and allows the user to select between them at boot time using a menu. I have found this to be a very useful tool because there have been many instances that I have encountered problems with an application or system service that fails with a particular kernel version. Many times, booting to an older kernel can circumvent issues such as these. By default, three kernels are kept–the newest and two previous–when **yum** or **dnf** are used to perform upgrades. The number of kernels to be kept before the package manager erases them is configurable in the **/etc/dnf/dnf.conf** or **/etc/yum.conf** files. I usually change the **installonly_limit** value to 9 to retain a total of nine kernels. This has come in handy on a couple occasions when I had to revert to a kernel that was several versions down-level.
|
||||
|
||||
### GRUB menu
|
||||
|
||||
The function of the GRUB menu is to allow the user to select one of the installed kernels to boot in the case where the default kernel is not the desired one. Using the up and down arrow keys allows you to select the desired kernel and pressing the **Enter** key continues the boot process using the selected kernel.
|
||||
|
||||
The GRUB menu also provides a timeout so that, if the user does not make any other selection, GRUB will continue to boot with the default kernel without user intervention. Pressing any key on the keyboard except the **Enter** key terminates the countdown timer which is displayed on the console. Pressing the **Enter** key immediately continues the boot process with either the default kernel or an optionally selected one.
|
||||
|
||||
The GRUB menu also provides a "rescue" kernel, in for use when troubleshooting or when the regular kernels don't complete the boot process for some reason. Unfortunately, this rescue kernel does not boot to rescue mode. More on this later in this article.
|
||||
|
||||
### The grub.cfg file
|
||||
|
||||
The **grub.cfg** file is the GRUB configuration file. It is generated by the **grub2-mkconfig** program using a set of primary configuration files and the grub default file as a source for user configuration specifications. The **/****boot/grub2/****grub.cfg** file is first generated during Linux installation and regenerated when a new kernel is installed.
|
||||
|
||||
The **grub.cfg** file contains Bash-like code and a list of installed kernels in an array ordered by sequence of installation. For example, if you have four installed kernels, the most recent kernel will be at index 0, the previous kernel will be at index 1, and the oldest kernel will be index 3\. If you have access to a **grub.****cfg** file you should look at it to get a feel for what one looks like. The **grub.cfg** file is just too large to be included in this article.
|
||||
|
||||
### GRUB configuration files
|
||||
|
||||
The main set of configuration files for **grub.cfg** is located in the **/etc/grub.d **directory. Each of the files in that directory contains GRUB code that is collected into the final grub.cfg file. The numbering scheme used in the names of these configuration files is designed to provide ordering so that the final **grub.cfg** file is assembled into the correct sequence. Each of these files has a comment to denote the beginning and end of the section, and those comments are also part of the final grub.cfg file so that it is possible to see from which file each section is generated. The delimiting comments look like this:
|
||||
|
||||
```
|
||||
### BEGIN /etc/grub.d/10_linux ###
|
||||
|
||||
### END /etc/grub.d/10_linux ###
|
||||
```
|
||||
|
||||
These files should not be modified unless you are a GRUB expert and understand what the changes will do. Even then you should always keep a backup copy of the original, working **grub.****cfg** file. The specific files, **40_custom** and **41_custom** are intended to be used to generate user modifications to the GRUB configuration. You should still be aware of the consequences of any changes you make to these files and maintain a backup of the original **grub.****cfg** file.
|
||||
|
||||
You can also add your own files to the /etc/grub.d directory. One reason for doing that might be to add a menu line for a non-Linux operating system. Just be sure to follow the naming convention to ensure that the additional menu item is added either immediately before or after the **10_linux** entry in the configuration file.
|
||||
|
||||
### GRUB defaults file
|
||||
|
||||
Configuration of the original GRUB was fairly simple and straightforward. I would just modify **/boot/grub/grub.conf** and be good to go. I could still modify GRUB2 by changing **/boot/grub2/grub.****cfg**, but the new version is considerably more complex than the original GRUB. In addition, **grub.cfg** may be overwritten when a new kernel is installed, so any changes may disappear. However, the GNU.org GRUB Manual does discuss direct creation and modification of **/boot/grub2/grub.cfg**.
|
||||
|
||||
Changing the configuration for GRUB2 is fairly easy once you actually figure out how to do it. I only discovered this while researching GRUB2 for a previous article. The secret formula is in the **/etc/default **directory, with a file called, naturally enough, grub, which is then used in concert with a simple terminal command. The **/etc/default** directory contains configuration files for a few programs such as Google Chrome, useradd, and grub.
|
||||
|
||||
The **/etc/default/grub** file is very simple. The grub defaults file has a number of valid key/value pairs listed already. You can simply change the values of existing keys or add other keys that are not already in the file. Listing 1, below, shows an unmodified **/etc/default/gru**b file.
|
||||
|
||||
```
|
||||
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="rd.lvm.lv=fedora_fedora25vm/root
|
||||
rd.lvm.lv=fedora_fedora25vm/swap
|
||||
rd.lvm.lv=fedora_fedora25vm/usr rhgb quiet"
|
||||
GRUB_DISABLE_RECOVERY="true"
|
||||
```
|
||||
|
||||
_Listing 1: An original grub default file for Fedora 25._
|
||||
|
||||
[Section 5.1 of the GRUB Manual][7] contains information about all of the possible keys that can be included in the grub file. I have never had needed to do anything other than modifying the values of some of the keys that are already in the grub default file. Let's look at what each of these keys means as well as some that don't appear in the grub default file.
|
||||
|
||||
* **GRUB_TIMEOUT **The value of this key determines the length of time that the GRUB selection menu is displayed. GRUB offers the capability to keep multiple kernels installed simultaneously and choose between them at boot time using the GRUB menu. The default value for this key is 5 seconds, but I usually change that to 10 seconds to allow more time to view the choices and make a selection.
|
||||
* **GRUB_DISTRIBUTOR **This key defines a [sed][3] expression that extracts the distribution release number from the /etc/system-release file. This information is used to generate the text names for each kernel release that appear in the GRUB menu, such as "Fedora". Due to variations in the structure of the data in the system-release file between distributions, this sed expression may be different on your system.
|
||||
* **GRUB_DEFAULT **Determines which kernel is booted by default. That is the "saved" kernel which is the most recent kernel. Other options here are a number which represents the index of the list of kernels in **grub.cfg**. Using an index such as 3, however, to load the fourth kernel in the list will always load the fourth kernel in the list even after a new kernel is installed. So using an index will load a different kernel after a new kernel is installed. The only way to ensure that a specific kernel release is booted is to set the value of **GRUB_DEFAULT** to the name of the desired kernel, like 4.8.13-300.fc25.x86_64.
|
||||
* **GRUB_SAVEDEFAULT **Normally, this option is not specified in the grub defaults file. Normal operation when a different kernel is selected for boot, that kernel is booted only that one time. The default kernel is not changed. When set to "true" and used with **GRUB_DEFAULT=saved** this option saves a different kernel as the default. This happens when a different kernel is selected for boot.
|
||||
* **GRUB_DISABLE_SUBMENU **Some people may wish to create a hierarchical menu structure of kernels for the GRUB menu screen. This key, along with some additional configuration of the kernel stanzas in **grub.****cfg** allow creating such a hierarchy. For example, the one might have the main menu with "production" and "test" sub-menus where each sub-menu would contain the appropriate kernels. Setting this to "false" would enable the use of sub-menus.
|
||||
* **GRUB_TERMINAL_OUTPUT **In some environments it may be desirable or necessary to redirect output to a different display console or terminal. The default is to send output to the default terminal, usually the "console" which equates to the standard display on an Intel class PC. Another useful option is to specify "serial" in a data center or lab environment in which serial terminals or Integrated Lights Out (ILO) terminal connections are in use.
|
||||
* **GRUB_TERMINAL_INPUT **As with **GRUB_TERMINAL_OUTPUT**, it may be desirable or necessary to redirect input from a serial terminal or ILO device rather than the standard keyboard input.
|
||||
* **GRUB_CMDLINE_LINUX **This key contains the command line arguments that will be passed to the kernel at boot time. Note that these arguments will be added to the kernel line of grub.cfg for all installed kernels. This means that all installed kernels will have the same arguments when booted. I usually remove the "rhgb" and "quiet" arguments so that I can see all of the very informative messages output by the kernel and systemd during the boot and startup.
|
||||
* **GRUB_DISABLE_RECOVERY **When the value of this key is set to "false," a recovery entry is created in the GRUB menu for every installed kernel. When set to "true" no recovery entries are created. Regardless of this setting, the last kernel entry is always a "rescue" option. However, I encountered a problem with the rescue option, which I'll talk more about below.
|
||||
|
||||
There are other keys that I have not covered here that you might find useful. Their descriptions are located in Section 5.1 of the [GRUB Manual 2][8].
|
||||
|
||||
### Generate grub.cfg
|
||||
|
||||
After completing the desired configuration it is necessary to generate the **/boot/grub2/grub.****cfg** file. This is accomplished with the following command.
|
||||
|
||||
```
|
||||
grub2-mkconfig > /boot/grub2/grub.cfg
|
||||
```
|
||||
|
||||
This command takes the configuration files located in /etc/grub.d in sequence to build the **grub.****cfg** file, and uses the contents of the grub defaults file to modify the output to achieve the final desired configuration. The **grub2-mkconfig** command attempts to locate all of the installed kernels and creates an entry for each in the **10_Linux** section of the **grub.****cfg** file. It also creates a "rescue" entry to provide a method for recovering from significant problems that prevent Linux from booting.
|
||||
|
||||
It is strongly recommended that you do not edit the **grub.****cfg** file manually because any direct modifications to the file will be overwritten the next time a new kernel is installed or **grub2-mkconfig** is run manually.
|
||||
|
||||
### Issues
|
||||
|
||||
I encountered one problem with GRUB2 that could have serious consequences if you are not aware of it. The rescue kernel does not boot, instead, one of the other kernels boots. I found that to be the kernel at index 1 in the list, i.e., the second kernel in the list. Additional testing showed that this problem occurred whether using the original **grub.****cfg** configuration file or one that I generated. I have tried this on both virtual and real hardware and the problem is the same on each. I only tried this with Fedora 25 so it may not be an issue with other Fedora releases.
|
||||
|
||||
Note that the "recovery" kernel entry that is generated from the "rescue" kernel does work and boots to a maintenance mode login.
|
||||
|
||||
I recommend changing **GRUB_DISABLE_RECOVERY** to "false" in the grub defaults file, and generating your own **grub.cfg**. This will generate usable recovery entries in the GRUB menu for each of the installed kernels. These recovery configurations work as expected and boot to runlevel 1—according to the runlevel command—at a command line entry that requests a password to enter maintenance mode. You could also press **Ctrl-D** to continue a normal boot to the default runlevel.
|
||||
|
||||
### Conclusions
|
||||
|
||||
GRUB is the first step after BIOS in the sequence of events that boot a Linux computer to a usable state. Understanding how to configure GRUB is important to be able to recover from or to circumvent various types of problems.
|
||||
|
||||
I have had to boot to recovery or rescue mode many times over the years to resolve many types of problems. Some of those problems were actual boot problems due to things like improper entries in **/etc/fstab** or other configuration files, and others were due to problems with application or system software that was incompatible with the newest kernel. Hardware compatibility issues might also prevent a specific kernel from booting.
|
||||
|
||||
I hope this information will help you get started with GRUB configuration.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
作者简介:
|
||||
|
||||
David Both - David Both is a Linux and Open Source advocate who resides in Raleigh, North Carolina. He has been in the IT industry for over forty years and taught OS/2 for IBM where he worked for over 20 years. While at IBM, he wrote the first training course for the original IBM PC in 1981. He has taught RHCE classes for Red Hat and has worked at MCI Worldcom, Cisco, and the State of North Carolina. He has been working with Linux and Open Source Software for almost 20 years.
|
||||
|
||||
|
||||
----------------
|
||||
|
||||
via: https://opensource.com/article/17/3/introduction-grub2-configuration-linux
|
||||
|
||||
作者:[David Both ][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://opensource.com/users/dboth
|
||||
[1]:https://opensource.com/article/17/2/linux-boot-and-startup
|
||||
[2]:https://opensource.com/article/17/2/linux-boot-and-startup
|
||||
[3]:https://en.wikipedia.org/wiki/Sed
|
||||
[4]:https://opensource.com/article/17/3/introduction-grub2-configuration-linux?rate=QrIzRpQ3YhewYlBD0AFp0JiF133SvhyAq783LOxjr4c
|
||||
[5]:https://www.flickr.com/photos/internetarchivebookimages/14746482994/in/photolist-ot6zCN-odgbDq-orm48o-otifuv-otdyWa-ouDjnZ-otGT2L-odYVqY-otmff7-otGamG-otnmSg-rxnhoq-orTmKf-otUn6k-otBg1e-Gm6FEf-x4Fh64-otUcGR-wcXsxg-tLTN9R-otrWYV-otnyUE-iaaBKz-ovcPPi-ovokCg-ov4pwM-x8Tdf1-hT5mYr-otb75b-8Zk6XR-vtefQ7-vtehjQ-xhhN9r-vdXhWm-xFBgtQ-vdXdJU-vvTH6R-uyG5rH-vuZChC-xhhGii-vvU5Uv-vvTNpB-vvxqsV-xyN2Ai-vdXcFw-vdXuNC-wBMhes-xxYmxu-vdXxwS-vvU8Zt
|
||||
[6]:https://www.flickr.com/photos/internetarchivebookimages/14774719031/in/photolist-ovAie2-otPK99-xtDX7p-tmxqWf-ow3i43-odd68o-xUPaxW-yHCtWi-wZVsrD-DExW5g-BrzB7b-CmMpC9-oy4hyF-x3UDWA-ow1m4A-x1ij7w-tBdz9a-tQMoRm-wn3tdw-oegTJz-owgrs2-rtpeX1-vNN6g9-owemNT-x3o3pX-wiJyEs-CGCC4W-owg22q-oeT71w-w6PRMn-Ds8gyR-x2Aodm-owoJQm-owtGp9-qVxppC-xM3Gw7-owgV5J-ou9WEs-wihHtF-CRmosE-uk9vB3-wiKdW6-oeGKq3-oeFS4f-x5AZtd-w6PNuv-xgkofr-wZx1gJ-EaYPED-oxCbFP
|
||||
[7]:https://www.gnu.org/software/grub/manual/grub.html#Simple-configuration
|
||||
[8]:https://www.gnu.org/software/grub/manual/grub.html#Simple-configuration
|
||||
[9]:https://opensource.com/user/14106/feed
|
||||
[10]:https://opensource.com/article/17/3/introduction-grub2-configuration-linux#comments
|
||||
[11]:https://opensource.com/users/dboth
|
@ -0,0 +1,137 @@
|
||||
Linux GRUB2 配置简介
|
||||
============================================================
|
||||
|
||||
> 学习 GRUB 引导加载器如何工作以便准备系统并启动操作系统内核。
|
||||
![Linux GRUB2 配置简介](https://opensource.com/sites/default/files/styles/image-full-size/public/images/life/car-penguin-drive-linux-yellow.png?itok=ueZE5mph "Linux GRUB2 配置简介")
|
||||
>图片来源 : Internet Archive [Book][5] [Images][6]. Opensource.com 修改。 CC BY-SA 4.0
|
||||
|
||||
自从上个月研究我的文章 _[Linux][1] [引导和启动过程][2]简介_ 开始,我对更深入了解 GRUB2 产生了兴趣。这篇文章提供了配置 GRUB2 的快速介绍,为了简便,我大多数情况下会使用 GRUB 指代。
|
||||
|
||||
### GRUB
|
||||
|
||||
GRUB 表示 _GRand Unified Bootloader_。它的功能是在启动时从 BIOS 接管掌控、加载自身、加载 Linux 内核到内存,然后再把执行权交给内核。一旦内核开始掌控,GRUB 就完成了它的任务也就不再需要了。
|
||||
|
||||
GRUB 支持多种 Linux 内核并允许用户在启动时通过菜单在其中选择。我发现这是一种非常有用的工具,因为我有很多次遇到一个应用程序或者系统服务对于特定内核版本失败的问题。多次引导一个较旧的内核时就会出现类似的问题。默认情况下,使用 **yum** 或 **dnf** 进行更新时会保存三个内核 - 最新的以及两个比较旧的。在被包管理器删除之前保存的内核数目可以在 **/etc/dnf/dnf.conf** 或 **/etc/yum.conf** 文件中配置。我通常把 **installonly_limit** 的值修改为 9 以便保留 9 个内核。当我不得不恢复到低几个版本的内核时这非常有用。
|
||||
|
||||
### GRUB 菜单
|
||||
|
||||
GRUB 菜单的功能是当默认的内核不是想要的一个时允许用户从已经安装的内核中选择一个进行引导。通过上下箭头键允许你选中想要的内核,敲击 **Enter** 键会使用选中的内核继续引导进程。
|
||||
|
||||
GRUB 菜单也提供了超时机制,因此如果用户没有选择任何其它,GRUB 就会在没有用户干预的情况下使用默认内核继续引导。敲击键盘上除了 **Enter** 键的任何键会停止终端上显示的倒数计时器。立即敲击 **Enter** 键会使用默认内核或者选中的一个继续引导进程。
|
||||
|
||||
GRUB 菜单提供了一个 "rescue" 内核,用于故障排除或者由于某些原因导致的常规内核不能完成启动过程。不幸的是,这个 rescue 内核不会引导到恢复模式。文章后面会更详细介绍这方面的东西。
|
||||
|
||||
### grub.cfg 文件
|
||||
|
||||
**grub.cfg** 文件是 GRUB 配置文件。它由 **grub2-mkconfig** 程序使用一组主配置文件以及 grub 默认文件作为用户配置的源生成。**/****boot/grub2/****grub.cfg** 文件首先在 Linux 安装时生成,安装新内核时又会重新生成。
|
||||
|
||||
**grub.cfg** 文件包括了类似 Bash 的代码以及一个按照安装顺序排序的已安装内核数组列表。例如,如果你有 4 个已安装内核,最新的内核索引是 0,前一个内核索引是 1,最旧的内核索引是 3。如果你能访问 **grub.****cfg** 文件,你应该看看去感受一下它看起来是什么样。**grub.cfg** 太大也就没有包含在这篇文章中。
|
||||
|
||||
### GRUB 配置文件
|
||||
|
||||
**grub.cfg** 的主要配置文件都在 **/etc/grub.d** 目录。该目录中的每个文件都包含了收集到最终 grub.cfg 文件中的 GRUB 代码。这些配置文件的命名模式被设计为能够提供排序功能,使得最终的 **grub.cfg** 文件被汇编成正确的顺序。每个文件都有注释表明该部分的开始和结束,这些注释也是最终 grub.cfg 文件的一部分,从而可以看出每个部分是由哪个文件生成。分隔注释看起来像:
|
||||
|
||||
```
|
||||
### BEGIN /etc/grub.d/10_linux ###
|
||||
|
||||
### END /etc/grub.d/10_linux ###
|
||||
```
|
||||
|
||||
这些文件不应该被修改,除非你是一个 GRUB 专家并明白更改会发生什么。即使如此,修改 **grub.****cfg** 文件时你也总该保留一个原始文件的备份。 **40_custom** 和 **41_custom** 两个特别的文件用于生成用户对 GRUB 配置的修改。对这些文件的更改你仍然要注意它的后果,并保存一份原始 **grub.****cfg** 文件的备份。
|
||||
|
||||
你也可以把你自己的文件添加到 /etc/grub.d 目录。这样做的一个原因可能是为非 Linux 操作系统添加菜单行。要注意遵循命名规则确保配置文件中额外的菜单选项刚好在 **10_linux** 条目之前或之后。
|
||||
|
||||
### GRUB 默认文件
|
||||
|
||||
原始 GRUB 的配置非常简单而且直接。我只需要修改 **/boot/grub/grub.conf** 就可以了。我还可以通过更改 **/boot/grub2/grub.****cfg** 修改 GRUB2,但和原始的 GRUB 相比新版本相对更加复杂。另外,安装一个新内核时 **grub.cfg** 可能会被重写,因此任何修改都可能消失。当然,GNU.org GRUB 手册确实有直接创建和修改 **/boot/grub2/grub.cfg** 的讨论。
|
||||
|
||||
一旦你明白了如何做,更改 GRUB2 配置就会变得非常简单。我在研究之前 GRUB2 文章的时候才明白这个。秘方就在 **/etc/default** 目录,一个自然称为 grub 的文件,然后和简单终端命令一起使用。**/etc/default** 目录包括了一些类似 Google Chrome、 useradd、 和 grub 程序的配置文件。
|
||||
|
||||
**/etc/default/grub** 文件非常简单。grub 默认文件已经列出了一些有效的键值对。你可以简单地更改现有键的值或者添加其它文件中还没有的键。下面的列表 1 显示了一个没有更改过的 **/etc/default/grub** 文件。
|
||||
|
||||
```
|
||||
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="rd.lvm.lv=fedora_fedora25vm/root
|
||||
rd.lvm.lv=fedora_fedora25vm/swap
|
||||
rd.lvm.lv=fedora_fedora25vm/usr rhgb quiet"
|
||||
GRUB_DISABLE_RECOVERY="true"
|
||||
```
|
||||
|
||||
_列表 1:Fedora 25 一个原始 grub 默认文件。_
|
||||
|
||||
[GRUB 手册章节 5.1][7]包括了所有可以添加到 grub 文件的键的信息。除了修改 grub 默认文件已经有的一些键的值之外我从不需要做其它事情。让我们看看这些以及一些 grub 默认文件中没有出现的每个键意味着什么。
|
||||
|
||||
* **GRUB_TIMEOUT** 这个键的值决定了 GRUB 选择菜单显示的时间长度。GRUB 提供了同时保存多个安装内核并在启动时使用 GRUB 菜单在其中选择的功能。这个键的默认值是 5 秒,但我通常修改为 10 秒使得有更多时间查看选项并作出选择。
|
||||
* **GRUB_DISTRIBUTOR** 这个键定义了一个从 /etc/system-release 文件中提取发行版本的 [sed][3] 表达式。这个信息用于生成 GRUB 菜单中出现的每个内核发行版的文本名称,例如 “Fedora”。由于不同发行版之间 system-release 文件结构的差异,在你的系统中这个 sed 表达式可能有些不同。
|
||||
* **GRUB_DEFAULT** 决定默认引导哪个内核。这也是“保存”的最新内核。这里的其它选项代表了 **grub.cfg** 中列表的索引。使用索引号 3,就会总是加载列表中的第四个内核,即使安装了一个新内核。因此使用索引在安装一个新内核后会加载不同的内核。确保引导特定内核版本的唯一方法是设置 **GRUB_DEFAULT** 的值为想要内核的名称,例如 4.8.13-300.fc25.x86_64。
|
||||
* **GRUB_SAVEDEFAULT** 通常,grub 默认文件不会指定这个选项。当选择不同内核进行引导时,正常操作下该内核只会启动一次。默认内核不会改变。当设置为 “true” 并和 **GRUB_DEFAULT=saved** 一起使用时这个选项保存一个不同内核作为默认值。当选择不同内核进行引导时会发生这种情况。
|
||||
* **GRUB_DISABLE_SUBMENU** 一些人可能会希望 GRUB 菜单显示创建内核的层级菜单结构。这个键和 **grub.****cfg** 中一些额外内核配置允许创建这样的层级结构。例如,一个主菜单中可能有 “production 和 “test” 子菜单,每个子菜单中包括了一些合适的内核。设置它为 “false” 可以启用子菜单。
|
||||
* **GRUB_TERMINAL_OUTPUT** 一些环境下可能需要或者必要将输出重定向到一个不同的显示控制台或者终端。默认是把输出发送到默认终端,通常 “console” 等价于 Intel 系列个人电脑的标准输出。另一个有用的选择是在使用串行终端或者 Integrated Lights Out (ILO) 终端连接的数据中心或者实验室环境中指定 “serial”。
|
||||
* **GRUB_TERMINAL_INPUT** 和 **GRUB_TERMINAL_OUTPUT** 类似,可能需要或者必要重定向输入为串行终端或者 ILO设备、而不是标准键盘输入。
|
||||
* **GRUB_CMDLINE_LINUX** 这个键包括了在启动时会传递给内核的命令行参数。注意这些参数会被添加到 grub.cfg 所有已安装内核的内核行。这意味着所有已安装的内核在启动时都会有相同的参数。我通常删除 “rhgb” 和 “quiet” 参数以便我可以看到引导和启动时内核和 systemd 输出的所有内核信息消息。
|
||||
* **GRUB_DISABLE_RECOVERY** 当这个键的值被设置为 “false”,GRUB 菜单中就会为每个已安装的内核创建一个恢复条目。当设置为 “true” 时就不会创建任何恢复条目。不管这个设置怎样,最后的内核条目总是一个 “rescue” 选项。但是,rescue 选项中我遇到了一个问题,下面我会详细介绍。
|
||||
|
||||
还有一些你可能觉得有用但我没有在这里介绍的键。它们的描述可以在 [GRUB 手册 2][8] 的 5.1 章节找到。
|
||||
|
||||
### 生成 grub.cfg
|
||||
|
||||
完成所需的配置之后,就需要生成 **/boot/grub2/grub.****cfg** 文件。这通过下面的命令完成。
|
||||
|
||||
```
|
||||
grub2-mkconfig > /boot/grub2/grub.cfg
|
||||
```
|
||||
|
||||
这个命令按照顺序使用位于 /etc/grub.d 的配置文件构建 **grub.****cfg** 文件,然后使用 grub 默认文件的内容修改输出以便获得最终所需的配置。**grub2-mkconfig** 命令尝试定位所有已安装的内核并在 **grub.****cfg** 文件的 **10_Linux** 部分新建条目。它还创建一个 “rescue” 条目提供一个用于从阻止 Linux 启动的严重问题中恢复的方法。
|
||||
|
||||
强烈建议你不要手动编辑 **grub.****cfg** 文件,因为任何对该文件的直接修改都会在下一次安装新内核或者手动运行 **grub2-mkconfig** 时被重写。
|
||||
|
||||
### 问题
|
||||
|
||||
我遇到一个如果没有意识到就可能导致严重后果的 GRUB2 问题。恢复内核没有启动,反而启动了另外一个内核。我发现那是列表中索引为 1 的内核,也就是列表中的第二个内核。额外的测试发现不管使用原始的还是我生成的 **grub.****cfg** 配置文件都会发生这个问题。我在虚拟机和真实硬件上都尝试过而且都发生了这个问题。我只测试了 Fedora 25 因此其它 Fedora 发行版本可能没有这个问题。
|
||||
|
||||
注意从 “rescue” 内核生成的 “recovery” 内核条目不能工作或者引导到维护模式登录。
|
||||
|
||||
我推荐将 grub 默认文件中 **GRUB_DISABLE_RECOVERY** 的值更改为 “false”,然后生成你自己的 **grub.cfg**。这会在 GRUB 菜单中为每个已安装的内核生成可用的恢复条目。这些恢复配置能像期望那样工作并在请求输入密码的命令行条目中引导到运行级别 1 - 根据运行级别命令 - 进入维护模式。你也可以按 **Ctrl-D** 继续正常的引导进入默认运行级别。
|
||||
|
||||
### 总结
|
||||
|
||||
GRUB 是引导 Linux 计算机到可用状态过程中事件发生顺序在 BIOS 之后的第一步。理解如何配置 GRUB 对于恢复或者处理多种类型的问题非常重要。
|
||||
|
||||
这么多年来我多次不得不引导到恢复或者 rescue 模式以便解决多种类型的问题。其中的一些问题确实是类似 **/etc/fstab** 或其它配置文件中不恰当条目导致的引导问题,也有一些是由于应用程序或者系统软件和最新的内核不兼容的问题。硬件兼容性问题也可能妨碍特定的内核启动。
|
||||
|
||||
我希望这些信息能对你开启 GRUB 配置之旅有所帮助。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
作者简介:
|
||||
|
||||
David Both - David Both 是一个居住在 Raleigh,北卡罗来纳州的 Linux 和开源倡导者。他在 IT 界已经有超过 40 年,并在他工作的 IBM 执教 OS/2 20 多年。在 IBM 的时候,他在 1981 年开设了第一个最初 IBM 个人电脑的培训课程。他在红帽教授过 RHCE 课程并在 MCI Worldcom、 Cisco、 和北卡罗来纳州工作过。他已经在 Linux 和开源软件方面工作将近 20 年。
|
||||
|
||||
|
||||
----------------
|
||||
|
||||
via: https://opensource.com/article/17/3/introduction-grub2-configuration-linux
|
||||
|
||||
作者:[David Both ][a]
|
||||
译者:[ictlyh](https://github.com/ictlyh)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://opensource.com/users/dboth
|
||||
[1]:https://opensource.com/article/17/2/linux-boot-and-startup
|
||||
[2]:https://opensource.com/article/17/2/linux-boot-and-startup
|
||||
[3]:https://en.wikipedia.org/wiki/Sed
|
||||
[4]:https://opensource.com/article/17/3/introduction-grub2-configuration-linux?rate=QrIzRpQ3YhewYlBD0AFp0JiF133SvhyAq783LOxjr4c
|
||||
[5]:https://www.flickr.com/photos/internetarchivebookimages/14746482994/in/photolist-ot6zCN-odgbDq-orm48o-otifuv-otdyWa-ouDjnZ-otGT2L-odYVqY-otmff7-otGamG-otnmSg-rxnhoq-orTmKf-otUn6k-otBg1e-Gm6FEf-x4Fh64-otUcGR-wcXsxg-tLTN9R-otrWYV-otnyUE-iaaBKz-ovcPPi-ovokCg-ov4pwM-x8Tdf1-hT5mYr-otb75b-8Zk6XR-vtefQ7-vtehjQ-xhhN9r-vdXhWm-xFBgtQ-vdXdJU-vvTH6R-uyG5rH-vuZChC-xhhGii-vvU5Uv-vvTNpB-vvxqsV-xyN2Ai-vdXcFw-vdXuNC-wBMhes-xxYmxu-vdXxwS-vvU8Zt
|
||||
[6]:https://www.flickr.com/photos/internetarchivebookimages/14774719031/in/photolist-ovAie2-otPK99-xtDX7p-tmxqWf-ow3i43-odd68o-xUPaxW-yHCtWi-wZVsrD-DExW5g-BrzB7b-CmMpC9-oy4hyF-x3UDWA-ow1m4A-x1ij7w-tBdz9a-tQMoRm-wn3tdw-oegTJz-owgrs2-rtpeX1-vNN6g9-owemNT-x3o3pX-wiJyEs-CGCC4W-owg22q-oeT71w-w6PRMn-Ds8gyR-x2Aodm-owoJQm-owtGp9-qVxppC-xM3Gw7-owgV5J-ou9WEs-wihHtF-CRmosE-uk9vB3-wiKdW6-oeGKq3-oeFS4f-x5AZtd-w6PNuv-xgkofr-wZx1gJ-EaYPED-oxCbFP
|
||||
[7]:https://www.gnu.org/software/grub/manual/grub.html#Simple-configuration
|
||||
[8]:https://www.gnu.org/software/grub/manual/grub.html#Simple-configuration
|
||||
[9]:https://opensource.com/user/14106/feed
|
||||
[10]:https://opensource.com/article/17/3/introduction-grub2-configuration-linux#comments
|
||||
[11]:https://opensource.com/users/dboth
|
Loading…
Reference in New Issue
Block a user