09.03.2023 13:12

Boot disk for both BIOS and UEFI mode

For many years I was trying to avoid UEFI and used the legacy BIOS boot mode. I consider the BIOS boot mode more stright forward and simpler to use, also I do not need the features of UEFI.  Nevertheless, years are passing and my debian based rescue-disk that was partitioned with MBR with only a legacy BIOS boot support was not helpful with some new systems. For that reason I was looking for a way to "enhance" it with the EFI boot support. After all it was easy, at least to some degree.

I created a new EFI partition in the MBR (no GPT needed), this is generally OK to have the partition anywhere on the disk, the size can be small (512MB). The partition table is a bit mess in following example but is OK:

Device     Boot   Start      End Sectors  Size Id Type
/dev/sdb1          2048  7813119 7811072  3.7G 83 Linux
/dev/sdb2       7815167  9766911 1951745  953M  f W95 Ext'd (LBA)
/dev/sdb3       9766912 10815487 1048576  512M ef EFI (FAT-12/16/32)
/dev/sdb5       7815168  9766911 1951744  953M 82 Linux swap / Solaris

Formated with mkfs.vfat /dev/sdb3.

Now I took a look on debian USB install disk, the content of it is very simple, there is the iso9660 partition (install image) and a small EFI partition with just:

# ls -R
.:
efi

./efi:
boot  debian

./efi/boot:
bootx64.efi  grubx64.efi

./efi/debian:
grub.cfg

It is that simple, because it uses the EFI "fallback" boot option to use /efi/boot/bootx64.efi to chain load. I just copied the content of it to my new EFI partition.

search --file --set=root /.disk/info
set prefix=($root)/boot/grub
source $prefix/x86_64-efi/grub.cfg
linux /vmlinuz root=UUID=1d4b916e-500f-443c-8b21-361da082ca15 ro
initrd /initrd.img
boot

then the config for the grub is just a sequence of the grub commands to load a linux kernel and initrd.

The only hick up was that my install of the debian did not contain the /boot/grub/x86_64-efi/ - I had to copy that from grub-efi-amd64-bin package to /boot.Also you may use a different way to search for root in the first command and avoid to use /.disk/info file on the root partition. You may also use the UUID as usual in search command.

Downside may be, that the grub copied to EFI is "fixed" version, it will not be updated from packages, but at least it will work for some time (at least until /boot/grub/x86_64-efi/ content gets incompatible.

Note: There are many implementations of UEFI vs legacy BIOS behavior from various manufacturers, that may cause you to have issue with setup like this. YMMV.

Email comment