记录一下工作中遇到的制作grub的流程
制作环境为ubuntu arm环境、制作需要安装grub-install。
(1)使用dd命令生成空镜像文件
dd命令中【1k=1024b;1KB=1000b;1M=1024kb;1MB=1000kb】
$sudo dd if=/dev/zero of=grub.img bs=1M count=1024
(2)格式化U盘为GPT格式
$ sudo gdisk -l grub.img
GPT fdisk (gdisk) version 0.8.8
Partition table scan:
MBR: protective # <============== 保护性的MBR,这个是GPT兼容MBR的一种设计
BSD: not present
APM: not present
GPT: present # <============== 看这里,已经是GPT了
Found valid GPT with protective MBR; using GPT. # <========== 这里的显示也说明是GPT分区
Disk /dev/sdb: 30736384 sectors, 14.7 GiB
Logical sector size: 512 bytes
Disk identifier (GUID):
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 30736350
Partitions will be aligned on 2048-sector boundaries
Total free space is 4029 sectors (2.0 MiB)
(3)标记分区
没有gdisk,可以使用fdisk或者parted代替,或者apt安装gdisk
#gdisk grub.img
GPT fdisk (gdisk) version 0.8.6
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): p
Disk grub.img: 5601280 sectors, 1.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID):
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 5601246
Partitions will be aligned on 2048-sector boundaries
Total free space is 5601213 sectors (1.0 GiB)
Number Start (sector) End (sector) Size Code Name
Command (? for help): n
Partition number (1-128, default 1): 1
First sector (34-5601246, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-5601246, default = 5601246) or {+-}size{KMGTP}: +1G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): EF00
Changed type of partition to 'EFI System'
Command (? for help): p
Disk grub.img: 5601280 sectors, 1.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID):
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 5601246
Partitions will be aligned on 2048-sector boundaries
Total free space is 1406909 sectors (687.0 MiB)
Number Start (sector) End (sector) Size Code Name
1 2048 1024000 1.0 GiB EF00 EFI System
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to grub2_alios.img.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot.
The operation has completed successfully.
确认分区状况
#fdisk -l grub.img
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk grub.img: 2867 MB, 2867855360 bytes, 5601280 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: gpt
Disk identifier:
# Start End Size Type Name
1 2048 1024000 1G EFI System EFI System
(4)挂载虚拟磁盘
losetup:用来连接 img 镜像文件和 loopX回环设备
kpartx:挂载虚拟磁盘
#确认loop起始位置
#losetup -f
/dev/loop0
#链接img镜像和loopx回环设备
#losetup /dev/loop0 grub.img
#挂载镜像
#kpartx -av /dev/loop0
#打印信息如下,添加映射分区
add map loop0p1 (252:0): 0 4194304 linear /dev/loop0 2048
(5)修改uuid,这一步不是必须的,但我们使用的定制bios应该是写死了某些设置,不修改uuid无法识别到对应的启动项,ext4格式的修改uuid的用常见的方法就可以。这里放一种fat32格式下修改uuid的方式
UUID="12345678"
printf "\x${UUID:7:2}\x${UUID:5:2}\x${UUID:2:2}\x${UUID:0:2}" \
| dd bs=1 seek=67 count=4 conv=notrunc of=/dev/mapper/loop0p1
(6)创建grub
#退回到根目录
cd /
#创建挂载文件夹
#mkdir test
#格式化分区
#mkfs.fat -F 32 /dev/loop0p1
#挂载分区
#mount /dev/mapper/loop0p1 /test/
#制作grub
#grub-install --target arm64-efi --efi-directory /test --boot-directory=/test /dev/sda
#无报错,制作完成
Installing for arm64-efi platform.
Installation finished. No error reported
(7)卸载分区和链接
#卸载分区
umount: /dev/mapper/loop0p1
#卸载关联
#kpartx -d grub.img
(8)制作完成,可使用qemu进行测试,查看成功与否