Grub Boot Procedure
Published:
Problem: Grub cannot boot
I moved my nvme ssd that contains several Linux OSes from one machine to another, and find out that it cannot correctly boot from grub. This note descibes how grub works and how to fix it.
How Grub works
The boot procedure is to
- Load grub, the bootloader, from
/boot/efi
- Load grub.cfg, the configuration to boot OS, from
/boot/
- Load linux kernel
/boot/vmlinuz
and initramfs/boot/initrd.img
, along with the boot file system at device eg./dev/nvme0n1p1
. - Boot
Therefore, if grub cannot boot, we can boot it manually using grub commandline and then fix it.
Manual boot in Grub
- Find the correct partition in the grub using
ls
, eg.ls (hd0, gpt0)/
, to locate the correct partition that contains your filesystem. Useroot=(hd?, gpt?)
to set root path; - Load kernel image and rootfs,
linux <path-to-vmlinuz> root=<root-fs-dev>
, eg.linux /boot/vmlinuz root=/dev/nvme0n1p2
; - Load initrd,
initrd <path-to-initrd.img>
, eg.initrd /boot/initrd.img
; - Ask grub to boot using the above configuration:
boot
.
Fixing Grub after a successfull boot
After boot into OS, remember to fix grub and efi partition.
- Fix efi partition using
grub-install
. eg.grub-install --target=x86_64-efi --efi-directory=/boot/efi /dev/nvme0n1p1
, where the/dev/*
should point to the efi partition. - Fix
grub.cfg
usingupdate-grub
orgrub-mkconfig -o /boot/grub/grub.cfg
.