unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
From: Marius Bakke <mbakke@fastmail.com>
To: 26815@debbugs.gnu.org
Subject: bug#26815: [PATCH 3/3] vm: Add UEFI loader to disk images.
Date: Wed, 10 May 2017 21:52:27 +0200	[thread overview]
Message-ID: <20170510195227.13461-3-mbakke@fastmail.com> (raw)
In-Reply-To: <20170510195227.13461-1-mbakke@fastmail.com>

* gnu/system/vm.scm (qemu-image): Add GRUB-EFI to inputs. Append 40MiB EFI
System Partition.
* gnu/build/vm.scm (install-efi): New procedure.
(initialize-hard-disk): Generate grub EFI blob when ESP is present.
---
 gnu/build/vm.scm  | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gnu/system/vm.scm | 19 +++++++++++++++---
 2 files changed, 76 insertions(+), 3 deletions(-)

diff --git a/gnu/build/vm.scm b/gnu/build/vm.scm
index 935a174fc..258c0b8ae 100644
--- a/gnu/build/vm.scm
+++ b/gnu/build/vm.scm
@@ -27,6 +27,7 @@
   #:use-module (gnu build linux-boot)
   #:use-module (gnu build install)
   #:use-module (guix records)
+  #:use-module (ice-9 format)
   #:use-module (ice-9 match)
   #:use-module (ice-9 regex)
   #:use-module (srfi srfi-1)
@@ -315,6 +316,34 @@ 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 grub.cfg)
+  "Write a self-contained Grub UEFI blob to the mounted ESP using GRUB.CFG."
+  (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
+         (efi-directory (string-append esp "/EFI/BOOT"))
+         ;; Map grub target names to boot file names.
+         (efi-target-map (cond
+                          ((string-prefix? "x86_64" system)
+                           '("x86_64-efi" . "BOOTX64.EFI"))
+                          ((string-prefix? "i686" system)
+                           '("i386-efi" . "BOOTIA32.EFI"))
+                          ((string-prefix? "armhf" system)
+                           '("arm-efi" . "BOOTARM.EFI"))
+                          ((string-prefix? "aarch64" system)
+                           '("arm64-efi" . "BOOTAA64.EFI")))))
+    ;; Grub needs a TMPDIR to prepare the firmware image.
+    (setenv "TMPDIR" esp)
+
+    (mkdir-p efi-directory)
+    (unless (zero? (system* "grub-mkstandalone" "-O" (car efi-target-map)
+                            "-o" (string-append efi-directory "/"
+                                                (cdr efi-target-map))
+                            ;; Graft the configuration file onto the image.
+                            (string-append "boot/grub/grub.cfg=" grub.cfg)))
+      (error "failed to create grub EFI image"))))
+
 (define* (initialize-hard-disk device
                                #:key
                                bootloader
@@ -332,8 +361,13 @@ passing it a directory name where it is mounted."
     "Return the first partition found with the boot flag set."
     (member 'boot (partition-flags partition)))
 
+  (define (partition-esp? partition)
+    "Return the first EFI System Partition."
+    (member 'esp (partition-flags partition)))
+
   (let* ((partitions (initialize-partition-table device partitions))
          (root       (find partition-bootable? partitions))
+         (esp        (find partition-esp? partitions))
          (target     "/fs"))
     (unless root
       (error "no bootable partition specified" partitions))
@@ -345,8 +379,34 @@ passing it a directory name where it is mounted."
     (mount (partition-device root) target (partition-file-system root))
     (install-boot-config bootcfg bootcfg-location target)
     (when bootloader-installer
+      (display "installing bootloader...\n")
       (bootloader-installer bootloader device target))
 
+    (when esp
+      ;; Mount the ESP somewhere and install Grub UEFI image.
+      (let ((mount-point (string-append target "/boot/efi"))
+            (grub.cfg    (string-append target "/tmp/grub-standalone.cfg")))
+        (display "mounting EFI system partition...\n")
+        (mkdir-p mount-point)
+        (mount (partition-device esp) mount-point
+               (partition-file-system esp))
+
+        ;; Create a tiny configuration file telling the embedded grub
+        ;; where to load the real thing.
+        (with-output-to-file grub.cfg
+          (lambda _
+            (format #t
+                    "insmod part_msdos~@
+                    search --set=root --label gnu-disk-image~@
+                    configfile /boot/grub/grub.cfg~%")))
+
+        (display "creating EFI firmware image...")
+        (install-efi mount-point grub.cfg)
+        (display "done.\n")
+
+        (delete-file grub.cfg)
+        (umount mount-point)))
+
     ;; Register BOOTCFG as a GC root.
     (register-bootcfg-root target bootcfg)
 
diff --git a/gnu/system/vm.scm b/gnu/system/vm.scm
index 099e3fac3..fbabcf6c3 100644
--- a/gnu/system/vm.scm
+++ b/gnu/system/vm.scm
@@ -3,6 +3,7 @@
 ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
 ;;; Copyright © 2016 Leo Famulari <leo@famulari.name>
 ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
+;;; Copyright © 2017 Marius Bakke <mbakke@fastmail.com>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -202,7 +203,7 @@ the image."
                       (guix build utils))
 
          (let ((inputs
-                '#$(append (list qemu parted e2fsprogs dosfstools)
+                '#$(append (list qemu parted e2fsprogs dosfstools grub-efi)
                            (map canonical-package
                                 (list sed grep coreutils findutils gawk))
                            (if register-closures? (list guix) '())))
@@ -227,11 +228,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
-- 
2.12.2

  parent reply	other threads:[~2017-05-10 19:53 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-07 14:35 bug#26815: [PATCH 0/3] Hybrid UEFI disk image Marius Bakke
2017-05-07 14:36 ` bug#26815: [PATCH 1/3] vm: Add support for arbitrary partition flags Marius Bakke
2017-05-07 14:36   ` bug#26815: [PATCH 2/3] vm: Support creating FAT partitions Marius Bakke
2017-05-07 15:26     ` Danny Milosavljevic
2017-05-07 15:52       ` Marius Bakke
2017-05-07 16:32         ` bug#26815: [PATCH v2 " Marius Bakke
2017-05-07 17:06         ` bug#26815: [PATCH " Danny Milosavljevic
2017-05-07 19:15           ` Marius Bakke
2017-05-07 20:07             ` Danny Milosavljevic
2017-05-08 14:45               ` Ludovic Courtès
2017-05-08 15:59     ` Maxim Cournoyer
2017-05-07 14:36   ` bug#26815: [PATCH 3/3] vm: Support EFI boot in base image Marius Bakke
2017-05-07 15:18     ` Danny Milosavljevic
2017-05-07 15:41       ` Marius Bakke
2017-05-07 19:17       ` Marius Bakke
2017-05-08  9:06         ` Marius Bakke
2017-05-08 14:50           ` Ludovic Courtès
2017-05-10 19:52             ` bug#26815: [PATCH 1/3] vm: Support arbitrary partition flags Marius Bakke
2017-05-10 19:52               ` bug#26815: [PATCH 2/3] vm: Support creating FAT partitions Marius Bakke
2017-05-10 19:52               ` Marius Bakke [this message]
2017-05-10 21:05                 ` bug#26815: [PATCH 3/3] vm: Add UEFI loader to disk images Ludovic Courtès
2017-05-10 21:21                   ` Marius Bakke
2017-05-10 19:58             ` bug#26815: [PATCH 3/3] vm: Support EFI boot in base image Marius Bakke
2017-05-12 22:06               ` Ludovic Courtès
2017-05-12 23:12                 ` Marius Bakke
2017-05-13  9:17                   ` Mathieu Othacehe
2017-05-13 13:11                     ` Ludovic Courtès
2017-05-13 14:13                       ` Marius Bakke
2017-05-13 19:23                         ` Ludovic Courtès
2017-05-16 15:17                         ` Ludovic Courtès
2017-05-17 11:05                           ` Marius Bakke
2017-05-17 12:36                             ` Marius Bakke
2017-05-17 13:42                               ` Ricardo Wurmus
2017-05-17 19:47                               ` Ludovic Courtès
2017-05-17 11:05                           ` bug#26815: [PATCH v4 1/3] vm: Support arbitrary partition flags Marius Bakke
2017-05-17 11:05                             ` bug#26815: [PATCH v4 2/3] vm: Support creating FAT partitions Marius Bakke
2017-05-17 11:05                             ` bug#26815: [PATCH v4 3/3] vm: Add UEFI loader to disk images Marius Bakke
2017-05-17 21:28                               ` Ludovic Courtès
2017-05-18 16:21                                 ` Marius Bakke
2017-05-18 17:34                                   ` Marius Bakke
2017-05-18 20:59                                     ` Ludovic Courtès
2017-05-19 16:15                                       ` Marius Bakke
2017-05-19 17:37                                         ` Mathieu Othacehe
2017-05-19 18:06                                           ` Marius Bakke
2017-05-20  8:25                                             ` Ludovic Courtès
2017-05-20  8:55                                               ` Mathieu Othacehe
2017-05-20  9:23                                               ` Marius Bakke
2017-05-20  9:36                                                 ` Ludovic Courtès
2017-05-20  9:36                                             ` Mathieu Othacehe
2017-05-20 10:05                                               ` Marius Bakke
2017-05-19 21:21                                         ` Ludovic Courtès
2017-05-18 20:50                                   ` Ludovic Courtès
2017-05-18 22:52                                     ` Marius Bakke
2017-05-19  7:00                                       ` Ludovic Courtès
2017-05-17 21:21                             ` bug#26815: [PATCH v4 1/3] vm: Support arbitrary partition flags Ludovic Courtès
2017-05-07 15:28   ` bug#26815: [PATCH 1/3] vm: Add support for " Danny Milosavljevic
2017-05-08 14:43   ` Ludovic Courtès
2017-05-08 15:55   ` Maxim Cournoyer
2017-05-08 21:41   ` Danny Milosavljevic
2017-05-07 15:02 ` bug#26815: [PATCH 0/3] Hybrid UEFI disk image Marius Bakke

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170510195227.13461-3-mbakke@fastmail.com \
    --to=mbakke@fastmail.com \
    --cc=26815@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).