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

From: Maxim Cournoyer <[email-masked]>
Date: Sat, 07 Nov 2020 02:09:30 -0500

Hello!

Jesse Gibbons <[email-masked]> writes:

> To replicate:
> cd to a git checkout of the source code, gnu/system/examples
> The hash for my current checkout is
> d7e033b9a153a9e60f52ff64f4eb355c1c3d0a6e which is also the hash for my
> current version of guix.
> guix system disk-image -t raw lightweight-desktop.tmpl
>
> lightweight-desktop.tmpl defines an operating-system that uses
> grub-efi-bootloader, and is embedded in the info pages as an example
> operating system. Since it is lightweight, it doesn't take as long to
> build as desktop.tmpl.
>
> Expected results: guix builds the raw image just fine, and that image
> can be copied to an SD card and run on a computer with an efi
> bootloader, just like the counterpart with grub-bootloader. There is
> nothing in the documentation suggesting that this should not work by
> design.
>
> Actual results: guix fails, see attached log.
>
> -Jesse

Thank you for the report!

I'm CC'ing Mathieu as he knows this part of the code much better than I
do, having overhauled the generation of disk images recently!

Below are two patches I made while investigating this.  Mathieu, does
that look like a proper fix?  It allows images to be generated, but I'm
unsure whether they'd be bootable (I couldn't successfully boot either
EFI or MBR GRUB-based disk images in QEMU).

Anyway, attached are the patches.

Thank you!

Maxim

>From 557a938633ed55c41aed8b41a0f93b6151f00943 Mon Sep 17 00:00:00 2001
From: Maxim Cournoyer <[email-masked]>
Date: Fri, 6 Nov 2020 21:37:33 -0500
Subject: [PATCH 1/2] build: image: Ensure required arguments are present.

* gnu/build/image.scm (initialize-root-partition): Validate arguments.
[system-directory, references-graph]: Change from keyword to positional
arguments.
[doc]: Add space between sentences.
* gnu/system/image.scm (system-iso9660-image): Adjust.
---
 gnu/build/image.scm | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/gnu/build/image.scm b/gnu/build/image.scm
index 640a784204..053972166b 100644
--- a/gnu/build/image.scm
+++ b/gnu/build/image.scm
@@ -5,6 +5,7 @@
 ;;; Copyright © 2017 Marius Bakke <[email-masked]>
 ;;; Copyright © 2020 Tobias Geerinckx-Rice <[email-masked]>
 ;;; Copyright © 2020 Mathieu Othacehe <[email-masked]>
+;;; Copyright © 2020 Maxim Cournoyer <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -179,7 +180,7 @@ to call-with-database."
                                     make-device-nodes
                                     (wal-mode? #t)
                                     #:allow-other-keys)
-  "Initialize the given ROOT directory. Use BOOTCFG and BOOTCFG-LOCATION to
+  "Initialize the given ROOT directory.  Use BOOTCFG and BOOTCFG-LOCATION to
 install the bootloader configuration.
 
 If REGISTER-CLOSURES? is true, register REFERENCES-GRAPHS in the store.  If
@@ -187,6 +188,20 @@ DEDUPLICATE? is true, then also deduplicate files common to CLOSURES and the
 rest of the store when registering the closures.  SYSTEM-DIRECTORY is the name
 of the directory of the 'system' derivation.  Pass WAL-MODE? to
 register-closure."
+
+  (define (validate-arguments)
+    (when bootcfg
+      (unless bootcfg-location
+        (error "Missing 'bootcfg-location' argument")))
+    (when bootloader-installer
+      (unless bootloader-package
+        (error "Missing 'bootloader-package' argument")))
+    (unless references-graphs
+      (error "Missing 'references-graphs' argument"))
+    (unless system-directory
+      (error "Missing 'system-directory' argument")))
+
+  (validate-arguments)
   (populate-root-file-system system-directory root)
   (populate-store references-graphs root)
 
-- 
2.28.0

>From c642a6984d4961256f50ca5133d1eec5ca1af4e9 Mon Sep 17 00:00:00 2001
From: Maxim Cournoyer <[email-masked]>
Date: Sat, 7 Nov 2020 00:20:49 -0500
Subject: [PATCH 2/2] image: Only attempt bootloader installation when
 supported.

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

The initialize-root-partition in (guix build image) attempts to install a
bootloader when BOOTLOADER-INSTALLER is defined.  Unfortunately, it relies on
a special #f value for the second argument (device), which only the
INSTALL-GRUB procedure from (gnu bootloader grub) supports.

* gnu/system/image.scm (system-disk-image): Only pass the bootloader installer
argument when the bootloader name is 'grub.  Add comment.
---
 gnu/system/image.scm | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/gnu/system/image.scm b/gnu/system/image.scm
index 4075a26552..d4932466e5 100644
--- a/gnu/system/image.scm
+++ b/gnu/system/image.scm
@@ -345,8 +345,16 @@ used in the image."
                               #:grub-efi #+grub-efi
                               #:bootloader-package
                               #+(bootloader-package bootloader)
+                              ;; 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)
                               #:bootcfg #$bootcfg
                               #:bootcfg-location
                               #$(bootloader-configuration-file bootloader))
-- 
2.28.0