Subject: bug#26815: [PATCH v4 3/3] vm: Add UEFI loader to disk images.

From: Marius Bakke <[email-masked]>
Date: Thu, 18 May 2017 18:21:35 +0200

Ludovic Courtès <[email-masked]> writes:

>> +++ b/gnu/system/vm.scm
>> @@ -228,11 +229,23 @@ the image."
>>                                 #:system-directory #$os-drv))
>>                    (partitions (list (partition
>>                                       (size #$(- disk-image-size
>> -                                                (* 10 (expt 2 20))))
>> +                                                (* 50 (expt 2 20))))
>>                                       (label #$file-system-label)
>>                                       (file-system #$file-system-type)
>>                                       (flags '(boot))
>> -                                     (initializer initialize)))))
>> +                                     (initializer initialize))
>> +                                    ;; Append a small EFI System Partition for
>> +                                    ;; use with UEFI bootloaders.
>> +                                    (partition
>> +                                     ;; The standalone grub image is about 10MiB, but
>> +                                     ;; leave some room for custom or multiple images.
>> +                                     (size (* 40 (expt 2 20)))
>> +                                     (label "GNU-ESP")             ;cosmetic only
>> +                                     ;; Use "vfat" here since this property is used
>> +                                     ;; when mounting. The actual FAT-ness is based
>> +                                     ;; on filesystem size (16 in this case).
>> +                                     (file-system "vfat")
>> +                                     (flags '(esp))))))
>>               (initialize-hard-disk "/dev/vda"
>>                                     #:partitions partitions
>>                                     #:bootloader-package
>
> I thought we’d pass something like
>
>   (initialize-hard-disk … #:grub-efi #$grub-eif)
>
> to avoid the ambiguity?

Thanks for the hint. With the attached patch, grub-efi is now passed
through to "install-efi". I haven't tested it on "version-0.13.0" but
assume it will work.

Now, I would like to instead implement an #:efi-loader keyword, and pass
it a derivation for the standalone EFI blob, but I'm not sure if I can
do that before the weekend. What do you think, is this "good enough" for
the time being?

> Once we’re done with that, we’ll have to update one of the
> ‘operating-system’ declaration examples to show UEFI configuration, and
> to update the doc to explain the installation process for UEFI.

I'll get on that.

From b2236aa915bfb32e974546790a1e87ef1b268403 Mon Sep 17 00:00:00 2001
From: Marius Bakke <[email-masked]>
Date: Thu, 18 May 2017 18:10:31 +0200
Subject: [PATCH] vm: Initialize-hard-disk now takes a grub-efi parameter.

* gnu/build/vm.scm (initialize-hard-disk): Learn #:grub-efi.
(install-efi): Use it.
* gnu/system/vm.scm (qemu-image): Pass it.
---
 gnu/build/vm.scm  | 8 +++++---
 gnu/system/vm.scm | 3 ++-
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/gnu/build/vm.scm b/gnu/build/vm.scm
index c0c4c17fb..57619764c 100644
--- a/gnu/build/vm.scm
+++ b/gnu/build/vm.scm
@@ -316,12 +316,13 @@ SYSTEM-DIRECTORY is the name of the directory of the 'system' derivation."
     (mkdir-p directory)
     (symlink bootcfg (string-append directory "/bootcfg"))))
 
-(define (install-efi esp config-file)
+(define (install-efi grub esp config-file)
   "Write a self-contained GRUB EFI loader to the mounted ESP using CONFIG-FILE."
   (let* ((system %host-type)
          ;; Hard code the output location to a well-known path recognized by
          ;; compliant firmware. See "3.5.1.1 Removable Media Boot Behaviour":
          ;; http://www.uefi.org/sites/default/files/resources/UEFI%20Spec%202_6.pdf
+         (grub-mkstandalone (string-append grub "/bin/grub-mkstandalone"))
          (efi-directory (string-append esp "/EFI/BOOT"))
          ;; Map grub target names to boot file names.
          (efi-targets (cond ((string-prefix? "x86_64" system)
@@ -336,7 +337,7 @@ SYSTEM-DIRECTORY is the name of the directory of the 'system' derivation."
     (setenv "TMPDIR" esp)
 
     (mkdir-p efi-directory)
-    (unless (zero? (system* "grub-mkstandalone" "-O" (car efi-targets)
+    (unless (zero? (system* grub-mkstandalone "-O" (car efi-targets)
                             "-o" (string-append efi-directory "/"
                                                 (cdr efi-targets))
                             ;; Graft the configuration file onto the image.
@@ -349,6 +350,7 @@ SYSTEM-DIRECTORY is the name of the directory of the 'system' derivation."
                                bootcfg
                                bootcfg-location
                                bootloader-installer
+                               (grub-efi #f)
                                (partitions '()))
   "Initialize DEVICE as a disk containing all the <partition> objects listed
 in PARTITIONS, and using BOOTCFG as its bootloader configuration file.
@@ -400,7 +402,7 @@ passing it a directory name where it is mounted."
                     configfile /boot/grub/grub.cfg~%")))
 
         (display "creating EFI firmware image...")
-        (install-efi mount-point grub-config)
+        (install-efi grub-efi mount-point grub-config)
         (display "done.\n")
 
         (delete-file grub-config)
diff --git a/gnu/system/vm.scm b/gnu/system/vm.scm
index e0af90bee..d282ba557 100644
--- a/gnu/system/vm.scm
+++ b/gnu/system/vm.scm
@@ -204,7 +204,7 @@ the image."
                       (guix build utils))
 
          (let ((inputs
-                '#$(append (list qemu parted e2fsprogs dosfstools grub-efi)
+                '#$(append (list qemu parted e2fsprogs dosfstools)
                            (map canonical-package
                                 (list sed grep coreutils findutils gawk))
                            (if register-closures? (list guix) '())))
@@ -248,6 +248,7 @@ the image."
                                      (flags '(esp))))))
              (initialize-hard-disk "/dev/vda"
                                    #:partitions partitions
+                                   #:grub-efi #$grub-efi
                                    #:bootloader-package
                                    #$(bootloader-package bootloader)
                                    #:bootcfg #$bootcfg-drv
-- 
2.13.0