Create the following disk partition table (GPT, EFI, Linux)
$ fdisk -l disk.img
Disklabel type: gpt
Disk identifier: 68CF21FF-A7D8-EA4F-84BF-2C1395E85E87
Device Start End Sectors Size Type
disk.img1 2048 43007 40960 20M EFI System
disk.img2 43008 409566 366559 179M Linux filesystem
Format and mount partitions
LOOPDEV=$(sudo losetup -P --find --show disk.img)
MOUNTDIR=a
ROOTFS=b
mkfs.xfs -L rootfs ${LOOPDEV}p2
mkfs.fat -F32 ${LOOPDEV}p1
mount ${LOOPDEV}p1 $MOUNTDIR
mount ${LOOPDEV}p2 $ROOTFS
Create the grub stage2 file
sudo grub-mkimage -d /usr/lib/grub/x86_64-efi -o ${MOUNTDIR}/EFI/BOOT/bootx64.efi -p /efi/boot -O x86_64-efi \
fat part_gpt normal boot linux configfile efifwsetup efi_gop efi_uga search search_label xfs echo
Configure both the real grub file and the disk loader
$ cat $ROOTFS/boot/grub/grub.cfg
set timeout=5
menuentry "regular_startup" {
linux ($prefix)/boot/vmlinuz
initrd ($prefix)/boot/initramfs.igz
}
$ cat $MOUNTDIR/EFI/BOOT/grub.cfg
search --label rootfs --set prefix
configfile ($prefix)/boot/grub/grub.cfg
Then copy a filesystem (with kernel and initramfs as specified in grub.cfg) onto $ROOTFS