可引导磁盘:用 Bochs 与 GRUB 创建 BIOS/UEFI 可引导磁盘镜像教程
文章目录
本简短教程介绍如何创建自己的可引导磁盘镜像。有关分区的信息,请参阅 GPT 和 FAT.
| 难度级别 |
|---|
初级 |
BIOS 磁盘
Bochs 可以是一个强大的调试工具,尤其是在操作系统加载序列的早期阶段。然而,BOCHS 只能通过 BIOS 引导,并且其模拟的内容有些过时。为了创建一个基本的可引导磁盘,你需要根据柱面数、磁头数(最大 16)和扇区数(最大 63)来计算大小。然后,你需要创建磁盘并在其上安装 grub 和你的文件。
以下使用 80*16*63 的磁盘布局,这需要在 BOCHS 中显式配置。
$ dd if=/dev/zero of=disk.img bs=512 count=80640 # create an empty null-filled disk. You can use a random device to get a facsimile of a real disk if desired $ LOOPBACK_DEVICE=$(losetup --find --show disk.img ) # load the disk as a loopback device $ parted $LOOPBACK_DEVICE mklabel msdos # Create a MBR partition label $ parted $LOOPBACK_DEVICE mkpart primary fat32 1MiB 100% # Create a partition starting at 1 MiB that spans the rest of the disk with label fat32 $ losetup -d $LOOPBACK_DEVICE # Delete the loopback device $ LOOPBACK_DEVICE=$(losetup --find --show --partscan disk.img ) # Reload, the partitions will now be loaded and be accessible as well $ mkfs.vfat -F 32 ${LOOPBACK_DEVICE}p1 # Create a FAT32 partition $ DISK_NAME="/mnt/disk" $ mkdir -p $DISK_NAME # Create a mount point $ mount -t vfat ${LOOPBACK_DEVICE}p1 $DISK_NAME # Mount the image $ cd $DISK_NAME $ mkdir boot $ grub-install --no-nvram --directory /usr/lib/grub/i386-pc/ --modules="normal part_msdos multiboot2 fat" $LOOPBACK_DEVICE --no-floppy --target=i386-multiboot # Install grub
现在你可以将内核镜像复制到 /mnt/disk/boot。
$ umount $DISK_NAME # unmount the disk image $ losetup -d $LOOPBACK_DEVICE # remove the loopback device entries (similar to ejecting a USB)
现在你可以使用支持 BIOS 引导的虚拟机(例如 Bochs)从该磁盘引导。当 grub shell 出现时,使用以下命令引导(假设你使用的是 multiboot2)
multiboot2 (hd0,msdos1)/boot/<yourkernelimage> boot
如果你使用的是 EFI 设备,你可能需要获取 grub bios 文件。在 debian 上,这些文件位于 grub-pc-bin 软件包中。 不要安装此软件包,而是:
mkdir grub-bios sudo apt download grub-pc-bin # Download the package to the current working directory dpkg-deb -R grub-pc-bin_<version>.deb grub-bios/ # Unpack the deb file to grub-bios
你必须将 grub-install 中的 --directory 改为指向:
$(pwd)/grub-bios/usr/lib/grub/i386-pc
要求
你需要以下内容:
所需步骤
创建空镜像
首先,让我们创建一个空的磁盘镜像文件,比如说大小为 128 兆字节。
$ dd if=/dev/zero of=diskimage.dd bs=1048576 count=128
创建分区表
由于 GPT 比 MBR 分区表更复杂,我们将使用 fdisk,它具有交互式界面(在 macOS 上你需要使用 gnu fdisk)。输入(即你需要键入的内容)位于 ":" 冒号之后。
$ fdisk diskimage.dd Welcome to fdisk (util-linux 2.30.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table. Created a new DOS disklabel with disk identifier 0xfa00b86e. Command (m for help): g Created a new GPT disklabel (GUID: E6B4945A-8308-448B-9ACA-0E656854CF66). Command (m for help): n p Partition number (1-128, default 1): 1 First sector (2048-262110, default 2048): 2048 Last sector, +sectors or +size{K,M,G,T,P} (2048-262110, default 262110): +8M Created a new partition 1 of type 'Linux filesystem' and of size 8 MiB. Command (m for help): t 1 Selected partition 1 Partition type (type L to list all types): 1 Changed type of partition 'Linux filesystem' to 'EFI System'. Command (m for help): w The partition table has been altered.Syncing disks. $
使用的命令如下:
- g - 创建新的 GPT 分区表
- n p - 新建主分区
- 1 2048 +8M - 第一个分区,起始扇区,分区大小
- t 1 - 更改第一个分区的类型
- 1 - 类型 1 是 EFI 系统分区 (简称 ESP)
- w - 写入更改
在第一个分区上创建文件系统
现在这并不像看起来那么简单,因为我们必须在文件中间的某个位置创建文件系统。为此,我们将创建一个 loop 设备.
$ losetup -o $((2048*512)) --sizelimit $((8*1024*1024)) -f diskimage.dd
这里的参数是:
- -o $((2048*512)) - 告诉 losetup 我们的分区从第 2048 个扇区开始(一个扇区为 512 字节)
- --sizelimit $((8*1024*1024)) - 指定限制为 8 兆字节
- -f - 告诉它查找合适的 loopback 设备
- 最后一个参数是我们的磁盘镜像文件名。
要查看你的镜像使用了哪个设备,你可以执行
$ losetup -a
这将列出所有 loopback 设备。
现在我们有了一个精确指向镜像文件内分区的设备,我们可以在其上创建 FAT 文件系统:
$ mkfs.vfat -F 16 -n "EFI System" /dev/loop0
这里
- -F 16 - 告诉它创建 FAT16
- -n "EFI System" - 设置分区的标签(不要更改,某些固件会检查此项)
- /dev/loop0 - 是 loopback 设备(改为你在 losetup -a 输出中看到的那个)
向分区添加文件
下一步是挂载我们的 loopback 设备,以便我们可以将文件复制到分区。
$ mkdir somedir $ mount /dev/loop0 somedir
为简单起见,让我们将内核命名为 MSDOS.SYS,配置文件命名为 CONFIG.SYS:
$ cp MSDOS.SYS CONFIG.SYS somedir/
某些引导加载程序是所谓的两阶段加载程序。这意味着它们有两个部分:引导扇区和第二阶段文件。你必须将该第二阶段复制到分区:
$ cp 2NDSTAGE.BIN somedir/
UEFI 需要一个特殊的 PE 可执行文件,名为 EFI/BOOT/BOOT(arch).EFI,你可以使用 GNU-EFI:
$ mkdir somedir/EFI somedir/EFI/BOOT $ cp BOOTX64.EFI somedir/EFI/BOOT
完成复制后,卸载分区非常重要:
$ umount somedir $ rmdir somedir
添加 BIOS 引导加载程序
如果你打算创建带有 PMBR 的混合镜像(这样你也可以在传统 BIOS 机器上引导),那么你需要添加传统 BIOS 引导加载程序代码:
$ dd if=boot.bin of=diskimage.dd conv=notrunc bs=446 count=1 $ dd if=boot.bin of=diskimage.dd conv=notrunc bs=1 count=2 skip=510 seek=510
第一个命令复制代码,第二个命令复制扇区末尾的两个魔数字节。这样,字节 446 - 510(分区表所在的位置)就不会被覆盖。保留 UEFI 所需的 ESP 条目非常重要。
这里的参数含义是:
- if=boot.bin - 引导加载程序的名称,应为 512 字节
- of=diskimage.dd - 磁盘镜像
- conv=notrunc - 告诉 dd 更新现有镜像文件而不是创建新文件
- bs=446 - 块大小(扇区大小为 512 字节,但我们想写入更少)
- skip=510 - 跳过那么多块(在第二行中 bs 为 1,因此跳过 510 字节)
- count=1 - 告诉 dd 仅更新一个扇区
另请参阅
来源与翻译说明
本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。
原文:Bootable Disk;离线修订号:30564。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论