grub-install的坑
从宿主操作系统单独安装grub.efi到其他挂载的目录下需要加上--boot-directory参数:
Note:
Make sure to install the packages and run the
grub-install
command from the system in which GRUB will be installed as the boot loader. That means if you are booting from the live installation environment, you need to be inside the chroot when runninggrub-install
. If for some reason it is necessary to rungrub-install
from outside of the installed system, append the--boot-directory=
option with the path to the mounted/boot
directory, e.g--boot-directory=/mnt/boot
.Some motherboards cannot handle
bootloader-id
with spaces in it.
加上--removable可以使所有UEFI都可以检测到.efi文件(UEFI首先检查NVRAM,如果没有注册的.efi文件位置就回退到EFI分区的/BOOT/BOOTX64.EFI)
完整指令(假设./boot是EFI分区挂载的位置):
sudo grub-install --target=x86_64-efi --efi-directory=./boot --boot-directory=./boot --bootloader-id=GRUB --removable --recheck
grub.cfg的menuentry里首先使用insmod加载grub自带的必要模块(part_gpt, ext2, fat, normal),然后使用
search --no-floppy --fs-uuid --set=root [UUID]
其中UUID为内核所在分区的UUID
最后使用linux /bzImage .... 启动内核