原文:https://blog.csdn.net/duanlove/article/details/8066570
介绍
GRUB 2 通过运行/etc/grub.d/folder 目录下的脚本来 建立配置文件 (grub.cfg) ,并且/etc/default/grub这个文件也可以配置。update-grub命令(手动运行,或更新内核的时候会被执行) 可以自动更新grub.cfg .这个命令会激活上面的脚本,并且会寻找系统中存在的操作系统和内核。一旦发现操作系统或内核,那么它们会被加入到 GRUB 2 的启动菜单当中。
通过以下两个命令中的任意一个 可以知道,GRUB2被安装在了哪个分区:
Device: sudo grub-probe -t device /boot/grub
UUID: sudo grub-probe -t fs_uuid /boot/grub
例如:
$ grub-probe -t device /boot/grub/
/dev/sda1
$ sudo grub-probe -t fs_uuid /boot/grub/
efa0f8fd-d69d-4931-8275-d25f8871511d
$ sudo grub-probe -t device /boot/grub/
/dev/sda1
$
$ df -h
文件系统 容量 已用 可用 已用%% 挂载点
/dev/sda5 19G 16G 2.3G 87% /
none 1.5G 672K 1.5G 1% /dev
none 1.5G 104K 1.5G 1% /dev/shm
none 1.5G 356K 1.5G 1% /var/run
none 1.5G 0 1.5G 0% /var/lock
/dev/sda1 3.7G 132M 3.4G 4% /boot # 我这台机的 grub2是装在 /boot 分区的
/dev/sda8 426G 303G 102G 75% /home
/dev/sda7 5.6G 417M 4.9G 8% /opt
df: "/home/swtd1/.gvfs": 无效的跨设备连接
/home 426G 303G 102G 75% /home
安装 在 sda1 的操作系统接管 grub2。
主目录和文件 -- Main Directories & Files
The Menu: /boot/grub/grub.cfg
grub.cfg 这个文件,在以前的grub版本( GRUB 0.97')是 menu.lst 。grub.cfg这个文件不像 menu.list这个文件, menu.list是直接编辑的。
而grub.cfg是用命令(每个命令负责这个配置文件中的某一块的更新)来更新的。
grub.cfg 通过运行update-grub 来更新的。update-grub 其实就跟如下命令是一个作用。
grub-mkconfig -o /boot/grub/grub.cfg.
如果用户希望在一个不同的位置创建一个不同名字的启动菜单的话,就需要通过 “-o” 选项来指定新的路径。
当 grub-install 命令运行的时候,并不创建或者更新 grub.cfg 文件。
直接修改 grub.cfg --- Editing grub.cfg directly
手动修改 grub.cfg 需要注意:
如果其他的任务会运行 update-grub ,那么应该在 update-grub 运行之后, 再编辑 grub.cfg 。也就是说在编辑grub.cfg之前,应该所有需要的内核和相关资源已经有了。
当然,你手动编辑的内容,一般来说在下次运行update-grub 之后,都会被覆盖掉。update-grub 是根据它自己的规则来更新grub.cfg。
grub.cfg的部分内容 是/etc/grub.d 下的脚本生成的。用户可以根据需要编辑这个目录下的脚本。
用户设置: /etc/default/grub
/etc/default/grub 文件包含了很多默认配置。
/usr/sbin$ ls grub-*
grub-install grub-mkdevicemap grub-probe grub-set-default
grub-mkconfig grub-mknetdir grub-reboot grub-setup
在 /usr/sbin 目录下,以 grub- 开头的文件都是 脚本文件,并非二进制可执行文件。
很多预定义的变量包含在/usr/sbin/grub-mkconfig 这个文件中,用如下命令可以列出这些预定义的变量:
grep "export GRUB_DEFAULT" -A50 /usr/sbin/grub-mkconfig | grep GRUB_
/usr/sbin$ grep "export GRUB_DEFAULT" -A50 /usr/sbin/grub-mkconfig | grep GRUB_
export GRUB_DEFAULT \
GRUB_HIDDEN_TIMEOUT \
GRUB_HIDDEN_TIMEOUT_QUIET \
GRUB_TIMEOUT \
GRUB_DEFAULT_BUTTON \
GRUB_HIDDEN_TIMEOUT_BUTTON \
GRUB_TIMEOUT_BUTTON \
GRUB_BUTTON_CMOS_ADDRESS \
GRUB_BUTTON_CMOS_CLEAN \
GRUB_DISTRIBUTOR \
GRUB_CMDLINE_LINUX \
GRUB_CMDLINE_LINUX_DEFAULT \
GRUB_CMDLINE_XEN \
GRUB_CMDLINE_XEN_DEFAULT \
GRUB_CMDLINE_NETBSD \
GRUB_CMDLINE_NETBSD_DEFAULT \
GRUB_TERMINAL_INPUT \
GRUB_TERMINAL_OUTPUT \
GRUB_SERIAL_COMMAND \
GRUB_DISABLE_LINUX_UUID \
GRUB_DISABLE_RECOVERY \
GRUB_VIDEO_BACKEND \
GRUB_GFXMODE \
GRUB_BACKGROUND \
GRUB_THEME \
GRUB_GFXPAYLOAD_LINUX \
GRUB_DISABLE_OS_PROBER \
GRUB_INIT_TUNE \
GRUB_SAVEDEFAULT \
GRUB_BADRAM
脚本: /etc/grub.d/
$ ls /etc/grub.d/
00_header 10_linux 20_memtest86+ 40_custom README
05_debian_theme 20_linux_xen 30_os-prober 41_custom
在 /etc/grub.d/ 目录下的脚本,当 update-grub执行的时候,会被读取,并合并到 /boot/grub/grub.cfg 这个文件中。
grub.cfg文件中的 菜单条目的放置,是由 /etc/grub.d/ 目录下的脚本运行的顺序来决定的。文件名以数字开头的的脚本会被先执行,并且数字小的先执行。比如10_linux 在20_memtest之前执行。而20_memtest 会在40_custom 之前执行。文件名以字母开头的(alphabetic)的脚本在数字命名的脚本之后运行。
自定义的 启动菜单入口(menu entries)可以被添加到40_custom 这个文件中,或者单独创建一个文件。40_custom 中的菜单入口将会出现在启动菜单的最底部。一个以“06_”开头的自定义文件,将出现在 启动菜单的最顶部。
update-grub执行的时候, /etc/grub.d/ 下的脚本会被执行。
脚本描述:
00_header
设置环境变量,比如 系统文件位置、video设置和之前保存的入口(previously saved entries) 。这个也导入了一些相关的东西(存储在/etc/default/grub)。用户通常不需要改变这个文件。
05_debian_theme
这个文件的主要目的是设置GRUB2的背景图片,文本颜色,选择高亮和主题。这些文件初始化菜单显示。关于如何设置字体和显示等请参考:Grub2/Displays (https://help.ubuntu.com/community/Grub2/Displays)
10_linux
识别在 root device中操作系统正在使用的内核,并且为这些内核创建(menu entries) . 这个包含了相应的“recovery mode (恢复模式)”选项。在GRUB 1.99和之后的版本中, 在 启动主菜单页面,仅显示最新的内核。其他的内核被包含在了子启动菜单中( submenu)。 更多submenu相关信息请参考Grub2/Submenus 。
Note:对于早期的GRUB 2版本, 所有位于 boot目录的内核都包含在 main menu 中. 如果要减少显示的内核,那就要删除/boot文件夹下的旧内核,或者使用一个有效的的grub2自定义应用。
20_memtest86+
寻找 /boot/memtest86+.bin ,并且把它作为一个选项加入到 GRUB 2 启动菜单(boot menu)当中。当前没有任何方式可以从这个启动菜单当中去除这个“memtest86+”的入口。但是,可以通过去掉文件可执行权限,并运行update-grub的方式来禁止ofmemtest86+ 的显示:
sudo chmod -x /etc/grub.d/20_memtest86+
sudo update-grub
30_os-prober
这个脚本使用“os-prober” 去搜索linux和其他操作系统,并且把结果放置到GRUB 2 的菜单中。
这个文件包含如 Windows, Linux, OSX, 和 Hurd 这些操作系统的选项。
在这个文件中的一些变量,决定了 在/boot/grub/grub.cfg 文件中 和 GRUB 2 menu 中 最终被显示的某系名字的格式(format of the displayed names)。 熟悉基本脚本的用户可以改变这些变量,以达到改变 GRUB2菜单入口格式 的目的。
The user can insert an entry into /etc/default/grub which disables this script (seeConfiguring GRUB 2). Removing the executable bit from the file will also prevent it from searching for other operating systems.
By default os-prober ignores any disks that possess "dmraid" signatures, which can be listed via sudo dmraid -r -c (if installed).
If the script finds another Ubuntu/Linux operating system, it will attempt to use the titles contained in the10_linux section of that installation'sgrub.cfg file. If not found, the script will construct a menuentry from the actual boot information it locates.
40_custom
这个文件是一个添加自定义启动菜单入口的模板。当执行 update-grub的时候,这个文件的内容会被加入到grub.cfg总。
The contents of this file, below the "exec tail -n +3 $0" line and the default comments, are imported directly into/boot/grub/grub.cfg without any changes.
As the comments at the top of the file state, add custom lines below those already contained in the40_custom file.
The file can be renamed or copied. The file must be executable to be incorporated into the GRUB 2 menu.
As mentioned in the introduction of this section, the name of the file determines the location of its contents in the GRUB 2 menu. As a general rule, if using numerals at the start it is advisable to allow the00_header and05_debian_theme scripts to be run before the first custom menu. These files contain no menuentries and thus will not interfere with a menuentry from a custom script with a lower priority.
Configuring GRUB 2
Configuration changes are normally made to /etc/default/grub and to the custom files located in/etc/grub.d. Any changes made directly to the/boot/grub/grub.cfg are overwritten wheneverupdate-grub is executed either by the user or when called automatically by various system functions.
After editing /etc/default/grub or the scripts in the/etc/grub.d folder the user should runsudo update-grub to incorporate the changes into the GRUB 2 menu.
Some of the most common changes, such as the default OS/kernel and menu timeout, can be changed from within a GUI applications such as Grub Customizer.
/etc/default/grub
This file contains basic settings which would be considered normal for the user to configure. Options include the time the menu is displayed, the default OS to boot, etc. The default options included in file upon installation are only a small number of the variables which GRUB 2 can recognize. The GRUB 2 defined variables can be identified using the command previously documented in theGRUB 2 Settings section or at the end of this section.
In addition to the defined variables, the user may introduce self-created variable(s) and use them in the scripts contained in the/etc/grub.d folder.
Editing the File
The grub file is a system file, therefore any editing must be done by a user with 'Administrator/root' privileges. The file is a simple text file and can be edited by any text editor. The default text editor in Ubuntu is Gedit, and the file can be edited with the following command. "gksu" is the graphical equivalent of "sudo" and the "&" allows the terminal to be used to update GRUB 2 once the user saves the file.
gksu gedit /etc/default/grub &
After making changes and saving the file, the GRUB 2 menu must be updated to include the changes by running:
sudo update-grub
Specific Entries
This section details some of the entries available in the /etc/default/grub file. Much of this information can also be found by referring the theGnu GRUB manual or on the system itself by running the following command:
info -f grub -n 'Simple configuration'
GRUB_DEFAULT=
Sets the default menu entry. Entries may be numeric, a complete menuentry quotation, or "saved"
GRUB_DEFAULT=0 Sets the default menu entry by menu position. The first "menuentry" ingrub.cfg is 0, the second is 1, etc.
Note: Grub 1.99 introduces a submenu menu structure. For a menu item in a submenu, the entry becomes a two-digit entry. The first entry is the position of thesubmenu title in the main menu. The second entry is the position within thesubmenu. If the submenu is the 3rd entry in the main entry, and the user wishes to boot the first entry in thesubmenu, it would be designated as "2>0".See the community documentation for a fuller explanation:Grub2/Submenus.
GRUB_DEFAULT="xxxx" An exact menu entry, including the quotation symbols, may also be used. In this case, location in the menu will not matter.
Example: GRUB_DEFAULT="Ubuntu, Linux 2.6.31-9-generic"
Example (Submenu Entry): GRUB_DEFAULT="2>Ubuntu, Linux 2.6.38-8-generic"
GRUB_DEFAULT=savedThe information in this section applies to GRUB 1.98 and later. The "saved" entry enables the "grub-reboot" and "grub-set-default" commands to set the default OS for future boots.
The default OS for future boots will not be set merely by selecting an OS when booting.
grub-set-default Sets the default boot entry until changed.
The format is sudo grub-set-default X, withX being the menu entry position (starting with 0 as the first entry) or the exact menu string.
Example: sudo grub-set-default 3
Example: sudo grub-set-default "Ubuntu, Linux 2.6.32-15-generic"
To obtain the existing menu entry choice number (starting from 0) or the menu entry "string", run:
grep menuentry /boot/grub/grub.cfg
grub-reboot This command sets the default boot entryfor the next boot only. The format of the command is the same as forgrub-set-default (see above).
GRUB_SAVEDEFAULT=
If set to true this setting will automatically set the last selected OS from the menu as the default OS on the next boot.
No commands need be run to set the default OS.
Any time a menu entry is manually selected from the GRUB 2 menu, it becomes the default OS.
This option currently does not work if your /boot directory resides on an LVM partition or RAID.
For this to work you must also set GRUB_DEFAULT=saved.
Note: Ubuntu's version of Grub 1.99 introduced the submenu feature. Designating a default boot option located in thePrevious Linux versions submenu requires special formatting. For information regarding the submenu feature, please refer toGrub2/Submenus
GRUB_HIDDEN_TIMEOUT=0
Wait this many seconds for the user to press a key. During this period no menu is shown unless the user presses a key. If no key is pressed, control is passed to GRUB _TIMEOUT when the GRUB_HIDDEN_TIMEOUT expires. See the note regarding bugs to this feature at the end of this section.
The developers envisioned using this setting with a GRUB_TIMEOUT value of 0. This would give users a period of time (GRUB_HIDDEN_TIMEOUT) to display the menu by pressing a key, after which the system would boot without the menu being displayed (GRUB_TIMEOUT=0).
GRUB_HIDDEN_TIMEOUT=0
No menu is displayed. The system is immediately booted to the default OS.
This is the default setting with only one identified operating system.
To display the menu under this condition, place a # symbol at the start of the line and ensure the GRUB_TIMEOUT setting is a positive integer.
If the value is set to 0, a keystatus check is performed to determine if theSHIFT key is depressed. If GRUB 2 determines theSHIFT key is depressed during the boot process, the menu will be displayed. This gives the user a method of interrupting an automatic boot which would normally not display the menu.
GRUB_HIDDEN_TIMEOUT=X
X is a positive integer (e.g. 1, 5, 10, etc)
The boot process will pause and display a blank screen or the designated splash image forX seconds. At the end of the time period, the system will boot. No menu will be displayed.
While GRUB_HIDDEN_TIMEOUT is active, the menu can be displayed by pressing any key.
GRUB_HIDDEN_TIMEOUT=
No value entered after the = sign
The menu will be displayed for the number of seconds designated by GRUB_TIMEOUT.
GRUB_HIDDEN_TIMEOUT_QUIET=true
Determines whether a countdown timer is displayed on a blank screen when using theGRUB_HIDDEN_TIMEOUT feature.
true No countdown is displayed. The screen will be blank.
false A counter will display on a blank screen for the duration of the GRUB_HIDDEN_TIMEOUT value.
Note: There is a longstanding confirmed bug on the hidden menu feature in GRUB 1.97 to GRUB 1.99. The menu may not hide as specified in the description on this page. While editing the30_os-prober script can fix this issue, it is beyond the scope of this page.
GRUB_TIMEOUT=10
Sets the time period in seconds for the menu to be displayed before automatically booting unless the user intervenes.
This instruction begins at the expiration of GRUB_HIDDEN_TIMEOUT.
Setting this value to -1 will cause the menu to display until the user makes a selection.
On a single OS system, by default this setting is not used and the menu will not display.
To display the menu on each boot:
Place a comment symbol (#) in front of the GRUB_HIDDEN_TIMEOUT entry
Make the GRUB_TIMEOUT value 1 or higher.
GRUB_DISTRIBUTOR=lsb_release -i -s 2> /dev/null || echo Debian
Retrieves the descriptive name in the menu entry. (Ubuntu, Xubuntu, Debian, etc.)
GRUB_CMDLINE_LINUX
Entries on this line are added to the end of the 'linux' command line (GRUB legacy's "kernel" line) for both normal and recovery modes. It is used to pass options to the kernel.
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
This line imports any entries to the end of the 'linux' line (GRUB legacy's "kernel" line). The entries are appended to the end of the normal mode only.
To view a black screen with boot processes displayed in text, remove "quiet splash".To see the grub splash image plus a condensed text output, use "splash".
#GRUB_TERMINAL=console
Uncomment to disable graphical terminal. This may provide help if the GRUB 2 menu is too large or unreadable. It also may help when using the GRUB_HIDDEN_TIMEOUT feature.
#GRUB_DISABLE_LINUX_UUID="true"
Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
The search line will still use UUIDs. The linux line will use the/dev/sdXY convention when this option is enabled.
#GRUB_GFXMODE=640x480
GRUB 2 will automatically set the menu resolution to what it thinks is the best option. Uncommenting this line will set the resolution to 640x480, or you may change the value to another GRUB-compatible setting.
The setting applies only to the boot menu display, not the resolution of the operating system that boots.
Tip: Setting the same resolution in GRUB 2 and the operating system will decrease boot times slightly.
Although not required, the user can also specify the color bit depth by appending it to the resolution setting. An example would be 1280x1024x24 or 640x480x32.
The user can also add multiple resolutions. If GRUB 2 cannot use the first entry, it will try the next setting. Settings are separated by a comma. Example:1280x1024x16,800x600x24,640x480.
If using a splash image, the resolution setting and the splash image size should be compatible for best results.
If using an entry that produces a "not found" message when runningupdate-grub, try adding or changing the color bitdepth.
Resolutions available to GRUB 2 can be displayed by typing vbeinfo in the GRUB 2 command line. The command line is accessed by typing "c" when the main GRUB 2 menu screen is displayed.
If this line is commented (#) or the resolution is unavailable GRUB 2 uses the default setting determined by/etc/grub.d/00_header.
In GRUB 1.99 (Natty) the 'optimum' resolution is selected by GRUB if no resolution is specified.
#GRUB_DISABLE_LINUX_RECOVERY=true
Allows the user to prevent the recovery options from inclusion on the GRUB 2 menu.
Uncomment (remove the # symbol) to prevent the "Recovery" mode kernel options from appearing in the menu. If you want a "Recovery" option for only one kernel, make a special entry in /etc/grub/40_custom.
GRUB_INIT_TUNE="480 440 1"
Removing the comment symbol (#) allows GRUB 2 to play a single beep just prior to the Grub 2 menu display. More complex tunes can be designed by expanding the pitch/duration values.
The format is tempo [pitch1 duration1] [pitch2 duration2] ...
tempo is set once and applies to all duration settings.
duration is the result of 60/tempo. A duration of 1 with atempo of 60 would produce a 1 second beep. Aduration of 1 with atempo of 480 produces a .125 second beep.
Caution: The menu is displayed after the tune is complete. Creating a long tune will delay the appearance of the menu.
Online documentation is available by typing info grub --index-search play in a terminal. It mentions the ability to play a tune with the commandplay file if the tune is composed with little-endian numbers.
GRUB_BACKGROUND
Sets the background image, enter the full path to the image here. See splash image configuration above for further details and other options.
GRUB_DISABLE_OS_PROBER=true
This entry is used to prevent GRUB from adding the results of os-prober to the menu. A value of "true" disables the os-prober check of other partitions for operating systems, including Windows, Linux, OSX and Hurd, during execution of theupdate-grub command. Using this option rather than removing the executable bit from the/etc/grub.d/30_os-prober file has several advantages:
The setting can be easily changed while making other changes to thegrub file.
While both methods prevent os-prober from running and placing items in the menu display, using this setting allows the30_os-prober script, but not theos-prober command, to run. This script enables the GRUB_HIDDEN_TIMEOUT setting and/or the ability to display a hidden menu by pressing the ESC key (depending on other settings). This functionality is lost if the30_os-prober script is disabled by making it unexecutable.
To inspect the currenlty-available predesignated variables on your system's GRUB 2 library files, run:
grep DEVICE -A40 /usr/sbin/grub-mkconfig
User-Assigned Variables
Variables in the /etc/default/grub file are read and acted upon as needed. This allows users to add their own variables in a central location and then alter the GRUB 2 scripts to meet their needs. Keeping the user-defined variables within this file allows easier management of, and change to, those variables when desired. To add a variable, use the following format:
export <VARIABLE>
The following example would define a variable used to omit a specific Ubuntu partition from the menu:
Define and export the variable in /etc/default/grub :
export GRUB_EXCLUDE_PARTITION="/dev/sda1"
Establish the conditional in /etc/grub.d/30_os-prober in the10_linux section:
if [ $GRUB_EXCLUDE_PARTITION = $LROOT ]; then
or
if [ ! $GRUB_EXCLUDE_PARTITION = $LROOT ]; then
Complete the conditional at the appropriate point in the script
Save the files and run sudo update-grub to incorporate the changes into the GRUB 2 menu.
---------------------
作者:duanlove
来源:CSDN
原文:https://blog.csdn.net/duanlove/article/details/8066570
版权声明:本文为博主原创文章,转载请附上博文链接!