GRUB是GNU Grand Unified Bootloader的首字母缩写。它是实际上所有Linux发行版中使用的引导加载程序。
在启动阶段的早期,Bootloader由机器固件加载,BIOS或者UEFI(GRUB支持两个),并加载其中一个可用的内核。
GRUB编译后清理源代码目录
编译完代码后,我们可能希望从以前配置的剩余内容中清除源代码目录,以防重复该过程。
我们可以使用
- make clean
- make stuclean
两者有什么区别?
第一个导致程序二进制文件和对象被删除;后者也会执行相同的操作,但还会删除由“configure”脚本生成的文件。
编译GRUB代码
提取压缩包:
$tar -xvzf grub-2.06.tar.gz
进入提取的新目录grub-2.06
$cd grub-2.06
默认的安装目录是/usr/local。
可以使用“--Prefix”选项更改。
为特定平台配置GRUB编译
我们可以使用的另一个重要选项是--with-platform
。
需要此选项来指定应编译源代码的平台。
例如,为EFI明确编译GRUB,我们会写:
$./configure --with-platform=efi
还有许多其他选项,可用于启用或者禁用GRUB功能(启用更多功能,可能需要安装其他构建依赖项)。
有关它们的详细描述,我们可以运行:
$./configure -h
本教程,我们将使用默认选项编译GRUB,因此我们只需运行配置脚本而不指定任何内容:
$./configure
如果一切都在预期,脚本将完成其作业时,将在屏幕上打印编译GRUB的摘要。
GRUB2 will be compiled with following components: Platform: i386-pc With devmapper support: No (need libdevmapper header) With memory debugging: No With disk cache statistics: No With boot time statistics: No efiemu runtime: Yes grub-mkfont: No (need freetype2 library) grub-mount: No (need FUSE library) starfield theme: No (No build-time grub-mkfont) With libzfs support: No (need zfs library) Build-time grub-mkfont: No (need freetype2 library) Without unifont (no build-time grub-mkfont) Without liblzma (no support for XZ-compressed mips images) (need lzma library) With stack smashing protector: No
要真正编译代码,我们现在必须使用“make”。或者,我们可以使用-j
选项(简写为--jobs
)调用它,以指定应同时运行多少个命令。通常传递给此选项的值是可用处理单元的数量(我们可以使用'nproc'命令获得此值)。如果在没有参数的情况下提供了“-j”选项,则不会施加任何限制:
$make -j$(nproc)
一旦我们运行上面的命令,编译就会开始。过程完成后,我们可以继续安装。正如我们所看到的,默认前缀是/usr/local
,因此我们需要以root权限启动'makeinstall'命令。在这种情况下,我们将使用sudo获得:
$sudo make install
获取GRUB源代码
使用“curl”下载:
$curl -O ftp.gnu.org/gnu/grub/grub-2.06.tar.gz
同时下载关联的.sig
以验证tar包签名:
$curl -O ftp.gnu.org/gnu/grub/grub-2.06.tar.gz.sig
要验证GPG的tar包签名,我们必须导入用于签署包的公钥:
$gpg --keyserver keyserver.ubuntu.com --receive-keys BE5C23209ACDDACEB20DB0A28C8189F1988C2166
运行以下命令来验证tar包签名:
$gpg --verify grub-2.06.tar.gz.sig
我们应该收到一条Good signature的信息,如下所示:
gpg: assuming signed data in 'grub-2.06.tar.gz' gpg: Signature made Tue 08 Jun 2021 05:11:03 PM CEST gpg: using RSA key BE5C23209ACDDACEB20DB0A28C8189F1988C2166 gpg: Good signature from "Daniel Kiper <dkiper@net-space.pl>" [unknown] gpg: WARNING: This key is not certified with a trusted signature! gpg: There is no indication that the signature belongs to the owner. Primary key fingerprint: BE5C 2320 9ACD DACE B20D B0A2 8C81 89F1 988C 2166
安装GRUB编译依赖项
我们需要安装一些软件依赖项。
在Fedora上,我们可以使用DNF包管理器并运行:
$sudo dnf install \ make \ binutils \ bison \ gcc \ gettext-devel \ flex
在Debian上,我们可以执行以下命令:
$sudo apt-get update && sudo apt-get install \ make \ binutils \ bison \ gcc \ gettext \ flex
在ArchLinux上,我们使用Pacman安装包:
$sudo pacman -Sy \ make \ diffutils \ python \ binutils \ bison \ gcc \ gettext \ flex