使用 GRUB Legacy 制作可引导的 El-Torito CD

来源:OSDev Wiki· 阅读原文 ↗
文章封面
难度级别
Difficulty 1.png
初级

本教程将指导你制作一个使用 GRUB Legacy 的可引导 CD .iso 镜像。我们将创建一个 El-Torito “无模拟”可引导 CD,它不同于模拟软盘的可引导 CD 模拟软盘的可引导 CD。

要求

你需要以下内容:

  • mkisofs,它实际上已被 genisoimage 取代。
  • 一个 Multiboot(Multiboot 兼容的)内核,GRUB Legacy 可以引导它。
  • El-Torito GRUB Legacy stage2 文件,名为 stage2_eltorito.

Ubuntu/Debian

在 Ubuntu 或 Debian 中,你可以这样安装所需软件:

sudo apt-get install genisoimage grub

我们需要一个存放 CD 镜像中文件的位置:

cd                            # Go to your home directory.
mkdir -p isofiles/boot/grub

你现在在主文件夹中创建了一个 isofiles 目录,以及其中的 boot/grub 子文件夹。我们需要 stage2 文件。它由 grub 软件包安装。直接复制它:

cp /usr/lib/grub/i386-pc/stage2_eltorito  ~/isofiles/boot/grub/

Windows

请参阅 这篇关于如何安装和使用 mkisofs 的文章。我们需要一个存放 CD 镜像中文件的位置。创建一个文件夹 isofiles,其中包含 GRUB 所需的子文件夹 boot\grub。我们需要 stage2 文件。你可以通过 下载软件包 下载软件包并使用你喜欢的解包应用程序解包来获得它。找到文件 stage2_eltorito 并将其复制到 isofiles\boot\grub。

安装内核

获取一个你自行选择的内核,并将其复制到 isofiles 文件夹内的任意位置。最好将其放在 boot 子文件夹中。现在创建一个 menu.lst 文件。此文件控制 GRUB 应提供哪些菜单项以及内核如何引导。它必须放在 GRUB 文件夹 boot/grub 中,并包含类似以下内容:

default 0
#timeout 30

#title Boot from hard disk
#chainloader (hd0)+1

title My kernel
kernel /boot/kernel-file    # Edit it to the filename of your kernel.

在 GRUB 中有一个称为 root 的概念。它是你访问文件(如内核镜像和模块)所在的磁盘驱动器或分区。使用 stage2_eltorito 从 CD 引导时,你不需要设置 root,因为它已经设置为 "(cd)".

创建 .iso 镜像

下面我使用命令 genisoimage,但你可以将其改为 mkisofs,如果你的系统上是这样称呼它的。打开命令提示符/终端并进入 isofiles 文件夹所在的位置。在 Ubuntu 上它是你的主目录。执行命令:

genisoimage -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4
            -boot-info-table -o bootable.iso isofiles

现在你有一个名为 bootable.iso的文件。使用你喜欢的模拟器测试它,或者将其刻录到 CD 并在真实计算机上测试。我将解释我们为 genisoimage 使用的命令行参数:

-R 使用 Rock Ridge 协议,它允许 CD 上使用小写文件名。GRUB 需要此功能。
-b file 要引导的文件(文件名位于创建的 ISO 9660 文件系统中)。
-no-emul-boot 启用无模拟 El-Torito 引导。
-boot-load-size 4 指定要加载的 512 字节扇区的数量。四个 512 字节扇区(2048 字节)是一个 CD 扇区,也是大多数 BIOS 支持的数量。
-boot-info-table 修补引导文件以包含有关 CD 镜像的信息。GRUB 需要此功能。
-o bootable.iso 生成的 .iso 镜像的文件名。
isofiles 其他参数是要包含在 CD 上的文件和文件夹。在本例中只有 isofiles 的内容。

高级内容

为其添加标签

你可能想为 CD 镜像添加标签,以便在加载 CD 时能够识别它。只需向 genisoimage 传递一个 -V 命令行参数,后跟你想要的名称。

使其静默

某天你可能想直接在 Makefile 中创建 .iso 镜像。如果你不喜欢 genisoimage 产生的输出,只需传递命令行参数 -quiet。

有时你会收到关于所用输入字符集的警告。如果这是个问题,只需传递参数 -input-charset ascii (或者如果你的文件系统使用 utf8,则使用 utf8)。

从不同的文件和文件夹创建镜像

当你构建内核时,.iso 镜像上的各部分可能位于不同路径。假设你的项目文件如下:

project/
├── build/
│   ├── modules/
│   │   ├── iso9660.mod
│   │   └── kbdrv.mod
│   └── my-kernel.elf
├── src/
│   ├── grub/
│   │   └── menu.lst
│   └── ...
├── thirdparty/
│   └── grub/
│       └── stage2_eltorito
└── Makefile

你不想为了创建 .iso 镜像而复制这些文件。只需使用 -graft-points 参数,如下所示:

genisoimage -graft-points # ...other arguments here...
            boot/my-kernel.elf=build/my-kernel.elf
            boot/grub/menu.lst=src/grub/menu.lst
            boot/grub/stage2_eltorito=thirdparty/grub/stage2_eltorito
            modules/=build/modules/

你可以将文件和文件夹与 -graft-points 参数一起使用。

故障排除

如果在下面找不到问题的解决方案,请在 论坛 中提问。

mkisofs 报告 Uh oh, I cant find the boot image

mkisofs 的输出如下:

mkisofs: Uh oh, I cant find the boot image 'isofiles/boot/grub/stage2_eltorito' !

出现此问题是因为 mkisofs/genisoimage 会在 CD 上文件系统的子目录中查找其引导映像;请确保你指定的路径以 'boot/' 开头,而不是你的 ISO 目录名称(本例中为 'isofiles/')。

来源: 此主题

在 Linux 中遇到 permission denied 错误

genisoimage 的输出如下:

Size of boot image is 4 sectors -> No emulation
genisoimage: Permission denied. Error opening boot image file 'isofiles/boot/grub/stage2_eltorito' for update.

问题在于 -boot-info-table 命令行参数会修补引导文件以包含有关 CD 的信息,但 genisoimage 无法写入该引导文件。GRUB 需要修补引导文件才能正确访问 CD。解决方法是将其设为可写,如下所示:

chown myself isofiles/boot/grub/stage2_eltorito    # Make sure you are the owner of the file. Replace "myself"
                                                   # with your username. Maybe you need to use sudo.
chmod u+w isofiles/boot/grub/stage2_eltorito       # Make the file writable for the owner of the file (you).

另请参阅

文章

论坛

外部链接

来源与翻译说明

本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。

原文:Bootable El-Torito CD with GRUB Legacy;离线修订号:30412。

原作者:OSDev Wiki 社区贡献者。原文及引用资料的版权与许可声明继续适用:版权声明 许可说明

评论

暂无评论