Subject: Re: bug#44353: guix system disk-image -t raw fails with grub-efi-bootloader

From: Mathieu Othacehe <[email-masked]>
Date: Sat, 07 Nov 2020 10:08:36 +0100

Hello Maxim,

Thanks for your patch! It's hard to provide a reliable abstraction on
top of all the exotic bootloader installation methods existing.

Currently, each bootloader implements two methods:

* bootloader-installer
* bootloader-disk-image-installer

The first one is dedicated to the installation of the bootloader on a
mounted directory, while the second one is meant to work on a disk
device such as "/dev/sda" or directly on a disk-image.

When producing a disk-image with the "raw" type, we are always creating
and populating an ESP partition (see raw-image-type), regardless of the
selected bootloader. In fact, "raw" should be renamed to "hybrid-efi".

The produced image can work on machines with legacy mbr boot or with EFI
boot. Hence, calling "install-grub-efi" like it's done while building
the lightweight-desktop operating-system is useless and fails. The
attached patch fixes it.

You can test it with:

--8<---------------cut here---------------start------------->8---
qemu-system-x86_64 -m 1024 -bios $(guix build ovmf)/share/firmware/ovmf_x64.bin -hda disk.img
--8<---------------cut here---------------end--------------->8---

> +                              ;; Special case: most bootloaders can be copied
> +                              ;; directly at some fixed location on the image
> +                              ;; disk, but when installed to the master boot
> +                              ;; record (MBR), GRUB requires support files
> +                              ;; present under /boot/grub, which is handled by
> +                              ;; its 'installer' procedure.
>                                #:bootloader-installer
> -                              #+(bootloader-installer bootloader)
> +                              #+(if (eq? 'grub (bootloader-name bootloader))
> +                                    (bootloader-installer bootloader)
> +                                    #f)

That would prevent other bootloaders relying on this procedure to work,
such as extlinux.

Thanks,

Mathieu
>From 2145fc40d3eb949885ce94883b09c7291c607be6 Mon Sep 17 00:00:00 2001
From: Mathieu Othacehe <[email-masked]>
Date: Sat, 7 Nov 2020 10:03:53 +0100
Subject: [PATCH] bootloader: grub: Fix EFI installation.

When producing a disk-image, "install-grub-efi" will be called with EFI-DIR
set to false.  The EFI bootloader is already installed by
"initialize-efi-partition". In that case, do not proceed to bootloader
installation.

Fixes: <https://issues.guix.gnu.org/44353>.

* gnu/bootloader/grub.scm (install-grub-efi): Do not install the bootloader if
EFI-DIR is false.
---
 gnu/bootloader/grub.scm | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)

diff --git a/gnu/bootloader/grub.scm b/gnu/bootloader/grub.scm
index 0899fab61f..3404456df9 100644
--- a/gnu/bootloader/grub.scm
+++ b/gnu/bootloader/grub.scm
@@ -574,20 +574,24 @@ fi~%"))))
 (define install-grub-efi
   #~(lambda (bootloader efi-dir mount-point)
       ;; Install GRUB onto the EFI partition mounted at EFI-DIR, for the
-      ;; system whose root is mounted at MOUNT-POINT.
-      (let ((grub-install (string-append bootloader "/sbin/grub-install"))
-            (install-dir (string-append mount-point "/boot"))
-            ;; When installing Guix, it's common to mount EFI-DIR below
-            ;; MOUNT-POINT rather than /boot/efi on the live image.
-            (target-esp (if (file-exists? (string-append mount-point efi-dir))
-                            (string-append mount-point efi-dir)
-                            efi-dir)))
-        ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
-        ;; root partition.
-        (setenv "GRUB_ENABLE_CRYPTODISK" "y")
-        (invoke/quiet grub-install "--boot-directory" install-dir
-                      "--bootloader-id=Guix"
-                      "--efi-directory" target-esp))))
+      ;; system whose root is mounted at MOUNT-POINT.  When producing a
+      ;; disk-image, EFI-DIR is false and the EFI bootloader is already
+      ;; installed using "initialize-efi-partition".
+      (when efi-dir
+        (let ((grub-install (string-append bootloader "/sbin/grub-install"))
+              (install-dir (string-append mount-point "/boot"))
+              ;; When installing Guix, it's common to mount EFI-DIR below
+              ;; MOUNT-POINT rather than /boot/efi on the live image.
+              (target-esp (if (file-exists?
+                               (string-append mount-point efi-dir))
+                              (string-append mount-point efi-dir)
+                              efi-dir)))
+          ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
+          ;; root partition.
+          (setenv "GRUB_ENABLE_CRYPTODISK" "y")
+          (invoke/quiet grub-install "--boot-directory" install-dir
+                        "--bootloader-id=Guix"
+                        "--efi-directory" target-esp)))))
 
 (define (install-grub-efi-netboot subdir)
   "Define a grub-efi-netboot bootloader installer for installation in SUBDIR,
-- 
2.29.2