自定义 GRUB 主题
自定义 GRUB 主题教程
本教程汇集了多年来收集的信息。虽然我本人也对内容做出了贡献,但许多匿名人士也提供了宝贵的见解和指导。虽然我无法一一列举所有贡献者的功劳,但我衷心感谢每一位分享知识的人。
创建 GRUB2 主题
创建目录
首先,为新主题文件创建一个空目录。建议在 中创建一个子目录/boot/grub2/themes/。
创建目录:
1 | sudo mkdir /boot/grub2/themes/test/ |
创建主题文件
创造
theme.txt:建议将文件命名为
theme.txt。1
sudo nano /boot/grub2/themes/test/theme.txt
更改 GRUB 参数:
编辑 GRUB 配置文件:
1
sudo nano /etc/default/grub
查找
GRUB_THEME中的参数/etc/default/grub。GRUB_THEME要切换到您的主题,请在文件中 设置参数/etc/default/grub。添加以下行,如果已存在则修改它:
1
GRUB_THEME=/boot/grub2/themes/test/theme.txt
笔记:
- 在符号后面
"="写入主主题文件的绝对路径。 - 如果该行
GRUB_THEME=...已存在于/etc/default/grub,则重写它以匹配GRUB_THEME=/boot/grub2/themes/test/theme.txt。 - 同一个参数不应该有两行。
- 在符号后面
更新引导加载程序配置文件:
直到您重新生成引导加载程序配置文件时,此文件中的更改才会应用。
update-grub2通常,系统中 会有一个脚本。应该使用 root 权限运行它。
1
sudo 更新-grub2
- 如果
update-grub2不可用,请使用以下命令:
1
sudo grub2-mkconfig -o /boot/grub2/grub.cfg
笔记:
- 该命令的
update-grub2作用与 相同grub2-mkconfig。 update-grub2每次对 进行更改时都应该调用/etc/default/grub。
GRUB2 主题选项
大多数 GRUB2 主题选项在 中有描述theme.txt。GRUB2 在每次启动过程中都会解析此文件。重新启动即可查看theme.txt主题目录的更改。
常见概念
全局选项和组件名称的错误将在启动过程中显示。组件选项的错误则不会显示。
元素类型
- **根元素:**的顶层
theme.txt,设置全局选项。 - **启动菜单(boot_menu):**设置启动菜单选项。
- **进度条(progress_bar):**水平超时指示器。
- **循环进度(circular_progress):**循环超时指示器。
- **标签(label):**输出文本字符串。
- **图像(image):**输出图像。
- **垂直容器(vbox):**垂直输出元素。
- **水平容器(hbox):**水平输出元素。
- **画布(canvas):**为每个子元素设置的坐标。
根元素是拉伸到屏幕尺寸的画布。
图片
图片会被拉伸至指定尺寸,如果尺寸较小,则按实际尺寸拉伸。请注意不同的显示器宽高比(例如 4:3 和 16:9)。
- **标识图像:**使用绝对宽度和高度以避免扭曲。
- **渐变:**使用 1px 宽度的渐变图像实现各种效果。
图形装饰
元素的图形装饰由9个区域组成:中心,南,西南,西等。
- 非拉伸区域: 4 个对角区域。
- **延伸区域:**垂直方向为东/西区域,水平方向为南/北区域,两者同时为中心区域。
确保区域大小的一致性以避免扭曲。
字体
使用实用程序创建字体grub2-mkfont。详情请参阅“字体创建指南”。
测试方案说明
- 清晰度增加 5 倍,alpha 通道为 75%。
- 为了清晰起见,使用深红色背景。
图形元素包括:
- 收件箱:
inbox_*.png - 按钮:
button_*.png - 菜单:
menu_*.png - 滚动条:
scrollbar_frame_*.png,scrollbar_thumb_*.png - 进度条:
progress_*.png - 循环进步中心图片:
center.png - 圆形进度条图像:
tick.png
通用属性
- 坐标:
left, top, width, height- 包含元素的矩形。 - **ID:**特殊值影响元素显示。
根元素
包含所有主题元素和全局选项。
- **全局选项:**使用“-”作为名称(例如“terminal-box”),使用“:”作为分隔符(例如“title_text:”GNU Grub“”)。
- **元素选项:**使用“_”作为名称(例如“item_height”),使用“=”作为分隔符(例如“item_height = 20”)。
桌面颜色和图像
- **桌面颜色:**背景颜色。
- **桌面图像:**背景图像,放置在主题目录中并缩放到屏幕分辨率。
标题设置
- 标题文字、颜色、字体:
- **隐藏标题:**设置
title-text=""。 - **中心对齐:**基线距顶部 40+ 上升。
- **隐藏标题:**设置
例子:
1 | title-text: “标题文本” |
控制台装饰
- **装饰:**控制台矩形外部,内边距为 3px。默认背景为黑色,可通过 更改
/etc/default/grub。
控制台字体
- **字体:**控制台字体的名称。添加新字体后,运行
update-grub2。
例子:
1 | 终端字体:“Droid Sans Mono Regular 11” |
启动菜单
- **主要元素:**列出已安装的操作系统。
- 坐标:
left, top, width, height- 矩形坐标。 - **字体和颜色:**用于选定的和非活动的菜单项。
例子:
1 | +启动菜单{ |
物品装饰
- 选定物品装饰:
selected_item_pixmap_style - 启动菜单装饰:
menu_pixmap_style
例子:
1 | +启动菜单{ |
填充和间距
- **项目填充:**从每一侧填充打印区域。
- **身高计算:**使用给定的身高公式。
例子:
1 | item_padding = 20 |
图标和图标空间
- 默认值:
icon_width = 32icon_height = 32item_icon_space = 4
- **自由空间:**无论图标是否存在,都应缩进。
例子:
1 | 图标宽度 = 0 |
为了全面了解,请参阅 GRUB2 主题参考。
使用图标
要使用图标,请image在主题目录中创建一个名为的目录。将 PNG 格式的图标放入此目录中。图标文件名应与特定的预定义类相对应。默认类为:
windows>osgnu-linux>gnu>ososx>darwin>oshurd>gnu>os
最具体的类优先。例如,如果windows.png存在 ,则将使用它;否则os.png将不使用。主系统类(即调用脚本的系统的名称update-grub2)也可用,并且优先于gnu-linux。例如,在 ROSA 系统上,此类为“rosa”。您可以在/boot/grub2/grub.cfg文件中找到这些类。
滚动条设置
对于详细的滚动条设置,请考虑以下几点:
- 请勿使用北、南和中心以外的切片
scrollbar_thumb。 - 使用全部 9 片
scrollbar_frame。 - 通过向相应的切片添加透明像素来增加填充。
- 滚动条绘制在中心切片的右侧,并且仅在需要时绘制。
scrollbar_frame和 的宽度scrollbar_thumb可以不同。- 该
scrollbar_width参数指定滚动条中心部分的宽度。如果scrollbar_width和 的东、西切片的总宽度scrollbar_frame小于菜单图形装饰东切片的宽度,则滚动条将无法完全绘制。 - 同时设置
scrollbar_frame和scrollbar_thumb。如果缺少其中一个,滚动条将不可见。
以下是正确显示的滚动条的示例:
1 | +启动菜单{ |
请记住,菜单垂直部分的内容宽度至少应为滚动条宽度与东切片的总和menu_pixmap_style。
在下一节中,我们将探讨进度条和循环进度指示器,它们对于增强您的 GRUB 主题至关重要。
进度条
进度条是一个水平指示器,用于显示启动进度或超时。以下是其基本属性和示例:
基本设置:
- **坐标:**设置进度条的位置和大小。
- **像素图:**指定进度条元素的图像。
元素属性:
- **左、上、宽、高:**定义包含进度条的矩形。
- **像素图:**用于进度条背景的图像文件。
- **前景:**用于进度条前景的图像文件。
例子:
1
2
3
4
5
6
7
8+进度条{
剩余 = 50%-150
顶部 = 50%+150
宽度 = 300
高度 = 30
像素图 = “progress_bg.png”
前景 =“progress_fg.png”
}
循环进步
圆形进度指示器以圆形格式显示启动进度或超时。以下是基本属性和示例:
基本设置:
- **坐标:**设置圆形进度指示器的位置和大小。
- **中心图像:**显示在圆圈中心的图像。
- **刻度图像:**用于进度刻度的图像。
元素属性:
- **左、上、宽、高:**定义包含圆形进度指示器的矩形。
- **center_image:**用于圆形进度中心的图像文件。
- **tick_image:**用于刻度的图像文件。
例子:
1
2
3
4
5
6
7
8+ 循环进度 {
左 = 50%-50
顶部 = 50%+50
宽度 = 100
高度 = 100
center_image = "center.png"
tick_image = "tick.png"
}
标签
标签用于在 GRUB 主题中显示文本字符串。以下是基本属性和示例:
基本设置:
- **坐标:**设置标签的位置和大小。
- **文本、字体、颜色:**指定标签的文本、字体和颜色。
元素属性:
- **左、上、宽、高:**定义包含标签的矩形。
- **text:**要显示的文本字符串。
- **字体:**文本使用的字体。
- **颜色:**文本的颜色。
例子:
1
2
3
4
5
6
7
8
9+标签{
剩余 = 50%-200
顶部 = 50%-50
宽度 = 400
高度 = 100
文本 =“欢迎使用 GRUB”
字体 =“Droid Sans Mono Regular 16”
颜色=“#FFFFFF”
}
图片
图片用于显示 GRUB 主题中的图形元素。以下是基本属性和示例:
基本设置:
- **坐标:**设置图像的位置和大小。
- **文件:**指定图像文件。
元素属性:
- **左、上、宽、高:**定义包含图像的矩形。
- **file:**要显示的图像文件。
例子:
1
2
3
4
5
6
7+ 图片 {
剩余 = 50%-100
顶部 = 50%-100
宽度 = 200
高度 = 200
文件 =“logo.png”
}
容器
容器用于垂直(vbox)或水平(hbox)对元素进行分组。以下是每种类型的基本属性及其示例:
VBox(垂直容器)
基本设置:
- **坐标:**设置容器的位置和大小。
- **元素:**列出 vbox 中包含的元素。
元素属性:
- **左、上、宽、高:**定义包含 vbox 的矩形。
- 元素: vbox 内的元素列表。
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18+ 垂直框 {
剩余 = 50%-150
顶部 = 50%-150
宽度 = 300
高度 = 400
元素 = {
+标签{
文本 = “选项 1”
字体 =“Droid Sans Mono Regular 16”
颜色=“#FFFFFF”
}
+标签{
文本 = “选项 2”
字体 =“Droid Sans Mono Regular 16”
颜色=“#FFFFFF”
}
}
}
HBox(水平容器)
基本设置:
- **坐标:**设置容器的位置和大小。
- **元素:**列出水平盒子内包含的元素。
元素属性:
- **left、top、width、height:**定义包含hbox的矩形。
- **elements:**水平盒子内的元素列表。
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18+ 水平盒子 {
剩余 = 50%-150
顶部 = 50%-50
宽度 = 300
高度 = 100
元素 = {
+标签{
文本 = “选项 1”
字体 =“Droid Sans Mono Regular 16”
颜色=“#FFFFFF”
}
+标签{
文本 = “选项 2”
字体 =“Droid Sans Mono Regular 16”
颜色=“#FFFFFF”
}
}
}
最后步骤
在文件中创建并配置主题元素后theme.txt,您需要:
保存
theme.txt**文件:**确保它位于/boot/grub2/themes/test/。**更新 GRUB 配置:**运行以下命令来应用新主题:
1
sudo 更新-grub2
**重新启动:**重新启动系统以查看生效的更改。
结论
创建自定义 GRUB 主题涉及组织图形元素、设置其属性以及配置引导加载程序的整体外观。按照本教程中提供的示例和指南,您可以创建一个独特且外观精美的 GRUB 主题,以增强系统的启动体验。
如需进一步定制,请参阅 GRUB2 主题参考以获取有关其他属性和选项的详细信息。
Custom GRUB Theme Tutorial
This tutorial combines information compiled over time. While I have contributed to its content, many anonymous sources have also provided valuable insights and instructions. Although I cannot specifically credit all contributors, I extend my appreciation to everyone who has shared their knowledge.
Create a GRUB2 Theme
Create a Directory
First, create an empty directory for the new theme files. It is recommended to create a subdirectory in
/boot/grub2/themes/.Create the directory:
1 sudo mkdir /boot/grub2/themes/test/Create the Theme File
Create
theme.txt:It is recommended to name the file
theme.txt.
1 sudo nano /boot/grub2/themes/test/theme.txtChange GRUB Parameters:
Edit the GRUB configuration file:
1 sudo nano /etc/default/grubFind the
GRUB_THEMEparameter in/etc/default/grub.To switch to your theme, set the parameter
GRUB_THEMEin the file/etc/default/grub.Add the following line, or modify it if it already exists:
1 GRUB_THEME=/boot/grub2/themes/test/theme.txtNotes:
- After the sign
"=", write the absolute path to the main theme file.- If the line
GRUB_THEME=...already exists in/etc/default/grub, rewrite it to matchGRUB_THEME=/boot/grub2/themes/test/theme.txt.- There should not be two lines for the same parameter.
Update Bootloader Configuration File:
Changes in this file won’t be applied until you regenerate the bootloader configuration file.
- Usually, there is a script
update-grub2in the system. It should be run with root rights.
1 sudo update-grub2
- If
update-grub2is not available, use the following command:
1 sudo grub2-mkconfig -o /boot/grub2/grub.cfgNotes:
- The command
update-grub2does the same asgrub2-mkconfig.update-grub2should be called each time you make changes to/etc/default/grub.GRUB2 Theme Options
Most GRUB2 theme options are described in
theme.txt. GRUB2 parses this file each time during the boot process. Reboot to see changes made totheme.txtor the theme directory.Common Concepts
Errors in global options and component names will show during boot. Errors in component options will not.
Types of Elements
- Root Element: Top level of
theme.txt, sets global options.- Boot Menu (boot_menu): Sets boot menu options.
- Progress Bar (progress_bar): Horizontal timeout indicator.
- Circular Progress (circular_progress): Circular timeout indicator.
- Label (label): Outputs a text string.
- Image (image): Outputs an image.
- Vertical Container (vbox): Outputs elements vertically.
- Horizontal Container (hbox): Outputs elements horizontally.
- Canvas (canvas): Coordinates set for each child element.
Root element is a canvas stretched to screen dimensions.
Images
Images are stretched to defined sizes, or their real sizes if smaller. Be mindful of different display aspect ratios (4:3 vs 16:9).
- Logotype Images: Use absolute width and height to avoid distortions.
- Gradients: Use a 1px width gradient image for various effects.
Graphical Decoration
Graphical decoration of an element consists of 9 zones: central, south, south-west, west, etc.
- Non-Stretched Zones: 4 diagonal zones.
- Stretched Zones: East/west zones by vertical, north/south zones by horizontal, central zone by both.
Ensure consistency in zone sizes to avoid distortions.
Fonts
Create fonts using the
grub2-mkfontutility. Refer to the “Font creating guide” for details.Test Scheme Description
- Increased 5x for clarity, alpha-channel at 75%.
- Dark red background for clarity.
Graphical elements include:
- Inbox:
inbox_*.png- Button:
button_*.png- Menu:
menu_*.png- Scrollbar:
scrollbar_frame_*.png,scrollbar_thumb_*.png- Progress Bar:
progress_*.png- Circular Progress Center Image:
center.png- Circular Progress Tick Image:
tick.pngCommon Properties
- Coordinates:
left, top, width, height- rectangle containing an element.- ID: Special value affects element display.
Root Element
Contains all theme elements and global options.
- Global Options: Use “-“ for names (e.g., “terminal-box”), “:” as a divider (e.g., “title_text: “GNU Grub””).
- Element Options: Use “_” for names (e.g., “item_height”), “=” as a divider (e.g., “item_height = 20”).
Desktop Color and Image
- desktop-color: Background color.
- desktop-image: Background image, placed in the theme’s directory and scaled to screen resolution.
Title Settings
- Title Text, Color, Font:
- Hide Title: Set
title-text="".- Center Aligned: Baseline at 40 + ascent from the top.
Example:
1
2
3 title-text: "Title Text"
title-color: "#FFFFFF"
desktop-image: "background.png"Console Decoration
- Decoration: Outside of console rectangle with a 3px padding. Default background is black, changeable via
/etc/default/grub.Console Font
- Font: Name of the console font. After adding a new font, run
update-grub2.Example:
1
2 terminal-font: "Droid Sans Mono Regular 11"
terminal-box: "inbox_*.png"Boot Menu
- Main Element: Lists installed operating systems.
- Coordinates:
left, top, width, height- rectangle coordinates.- Font and Color: Use for selected and non-active menu items.
Example:
1
2
3
4
5
6
7 + boot_menu {
left = 50%-300
top = 50%-100
width = 600
height = 300
selected_item_color = "#0000FF"
}Item Decoration
- Selected Item Decoration:
selected_item_pixmap_style- Boot Menu Decoration:
menu_pixmap_styleExample:
1
2
3
4 + boot_menu {
selected_item_pixmap_style = "button_*.png"
menu_pixmap_style = "inbox_*.png"
}Padding and Spacing
- Item Padding: Printing area padding from each side.
- Height Calculation: Use the given formula for height.
Example:
1
2
3 item_padding = 20
item_spacing = 20
item_height = 30Icons and Icon Space
- Default Values:
icon_width = 32icon_height = 32item_icon_space = 4- Free Space: Indented for icons regardless of their presence.
Example:
1
2 icon_width = 0
item_icon_space = 4For a complete understanding, refer to the GRUB2 Theme Reference.
Using Icons
To use icons, create a directory named
imagewithin your theme directory. Place your icons in PNG format into this directory. The icon filenames should correspond to specific predefined classes. The default classes are:
windows>osgnu-linux>gnu>ososx>darwin>oshurd>gnu>osThe most specific class takes precedence. For example, if
windows.pngis present, it will be used, andos.pngwill not. The main system class (which is the name of the system where theupdate-grub2script was called) is also available and takes precedence overgnu-linux. For instance, on a ROSA system, this class will be “rosa”. You can find classes in the/boot/grub2/grub.cfgfile.Scrollbar Settings
For detailed scrollbar settings, consider the following points:
- Do not use slices other than north, south, and center for
scrollbar_thumb.- Use all 9 slices for
scrollbar_frame.- Increase padding by adding transparent pixels to the corresponding slice.
- The scrollbar is drawn to the right of the center slice, and it is drawn only if needed.
- The width of
scrollbar_frameandscrollbar_thumbcan differ.- The
scrollbar_widthparameter specifies the width of the scrollbar’s central part. If the combined width ofscrollbar_widthand the east and west slices ofscrollbar_frameis less than the width of the east slice of the menu’s graphical decoration, the scrollbar won’t be fully drawn.- Set both
scrollbar_frameandscrollbar_thumb. If one is missing, the scrollbar won’t be visible.Here’s an example of a correctly displayed scrollbar:
1
2
3
4
5
6
7
8
9
10
11
12 + boot_menu {
left = 50%-300
top = 50%-100
width = 600
height = 289
selected_item_color = "#0000FF
"
scrollbar_thumb = "scrollbar_thumb_*.png"
scrollbar_frame = "scrollbar_frame_*.png"
}Remember that the content width of the vertical part of the menu should be at least the sum of the scrollbar width and the east slice of the
menu_pixmap_style.In the next section, we will explore the Progress Bar and Circular Progress indicators, which are crucial for enhancing your GRUB theme.
Progress Bar
A progress bar is a horizontal indicator showing the boot progress or timeout. Here are the essential properties and an example:
Basic Settings:
- Coordinates: Set the position and size of the progress bar.
- Pixmaps: Specify the images for the progress bar elements.
Element Properties:
- left, top, width, height: Define the rectangle containing the progress bar.
- pixmap: Image file used for the progress bar background.
- foreground: Image file used for the progress bar foreground.
Example:
1
2
3
4
5
6
7
8 + progress_bar {
left = 50%-150
top = 50%+150
width = 300
height = 30
pixmap = "progress_bg.png"
foreground = "progress_fg.png"
}Circular Progress
A circular progress indicator shows the boot progress or timeout in a circular format. Here are the essential properties and an example:
Basic Settings:
- Coordinates: Set the position and size of the circular progress indicator.
- Center Image: Image displayed at the center of the circle.
- Tick Image: Image used for the progress ticks.
Element Properties:
- left, top, width, height: Define the rectangle containing the circular progress indicator.
- center_image: Image file used for the center of the circular progress.
- tick_image: Image file used for the ticks.
Example:
1
2
3
4
5
6
7
8 + circular_progress {
left = 50%-50
top = 50%+50
width = 100
height = 100
center_image = "center.png"
tick_image = "tick.png"
}Labels
Labels are used to display text strings in your GRUB theme. Here are the essential properties and an example:
Basic Settings:
- Coordinates: Set the position and size of the label.
- Text, Font, Color: Specify the text, font, and color of the label.
Element Properties:
- left, top, width, height: Define the rectangle containing the label.
- text: Text string to be displayed.
- font: Font used for the text.
- color: Color of the text.
Example:
1
2
3
4
5
6
7
8
9 + label {
left = 50%-200
top = 50%-50
width = 400
height = 100
text = "Welcome to GRUB"
font = "Droid Sans Mono Regular 16"
color = "#FFFFFF"
}Images
Images are used to display graphical elements in your GRUB theme. Here are the essential properties and an example:
Basic Settings:
- Coordinates: Set the position and size of the image.
- File: Specify the image file.
Element Properties:
- left, top, width, height: Define the rectangle containing the image.
- file: Image file to be displayed.
Example:
1
2
3
4
5
6
7 + image {
left = 50%-100
top = 50%-100
width = 200
height = 200
file = "logo.png"
}Containers
Containers are used to group elements vertically (vbox) or horizontally (hbox). Here are the essential properties and an example for each type:
VBox (Vertical Container)
Basic Settings:
- Coordinates: Set the position and size of the container.
- Elements: List the elements contained within the vbox.
Element Properties:
- left, top, width, height: Define the rectangle containing the vbox.
- elements: List of elements within the vbox.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 + vbox {
left = 50%-150
top = 50%-150
width = 300
height = 400
elements = {
+ label {
text = "Option 1"
font = "Droid Sans Mono Regular 16"
color = "#FFFFFF"
}
+ label {
text = "Option 2"
font = "Droid Sans Mono Regular 16"
color = "#FFFFFF"
}
}
}HBox (Horizontal Container)
Basic Settings:
- Coordinates: Set the position and size of the container.
- Elements: List the elements contained within the hbox.
Element Properties:
- left, top, width, height: Define the rectangle containing the hbox.
- elements: List of elements within the hbox.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 + hbox {
left = 50%-150
top = 50%-50
width = 300
height = 100
elements = {
+ label {
text = "Option 1"
font = "Droid Sans Mono Regular 16"
color = "#FFFFFF"
}
+ label {
text = "Option 2"
font = "Droid Sans Mono Regular 16"
color = "#FFFFFF"
}
}
}Final Steps
Once you have created and configured your theme elements in the
theme.txtfile, you need to:
Save the
theme.txtfile: Ensure it is located in/boot/grub2/themes/test/.Update GRUB Configuration: Run the following command to apply your new theme:
1 sudo update-grub2Reboot: Restart your system to see the changes in effect.
Conclusion
Creating a custom GRUB theme involves organizing graphical elements, setting their properties, and configuring the overall look and feel of the bootloader. By following the examples and guidelines provided in this tutorial, you can create a unique and visually appealing GRUB theme that enhances your system’s boot experience.





