From 9555239cfc9362a15cc3f255040c410395d49e04 Mon Sep 17 00:00:00 2001 From: Marius Bakke Date: Sun, 7 May 2017 15:31:30 +0200 Subject: [PATCH] vm: Support EFI boot in base image. * gnu/system/vm.scm (qemu-image): Add GRUB-EFI to inputs. Append 40MB EFI System Partition. * gnu/build/vm.scm (initialize-hard-disk): Generate grub EFI blob when ESP is present. --- gnu/build/vm.scm | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- gnu/system/vm.scm | 14 +++++++++++--- 2 files changed, 68 insertions(+), 4 deletions(-) diff --git a/gnu/build/vm.scm b/gnu/build/vm.scm index 70e66fb35..57056fced 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) @@ -334,9 +335,16 @@ passing it a directory name where it is mounted." "Return the first partition found with the boot flag set." (member 'boot (partition-flags partitions))) + (define (partition-esp? partitions) + "Return the first EFI System Partition." + (member 'esp (partition-flags partitions))) + (let* ((partitions (initialize-partition-table device partitions)) (root (find partition-bootable? partitions)) - (target "/fs")) + (esp (find partition-esp? partitions)) + (target "/fs") + ;; Grub expects to find an EFI System Partition here. + (efi-directory (string-append target "/boot/efi"))) (unless root (error "no bootable partition specified" partitions)) @@ -346,6 +354,54 @@ passing it a directory name where it is mounted." (mkdir-p target) (mount (partition-device root) target (partition-file-system root)) (install-boot-config bootcfg bootcfg-location target) + + ;; If we have an ESP partition, generate a self-contained grub EFI + ;; image and write it to a well-known location. + (when esp + (let* ((system %host-type) + (efi-payload-directory (string-append efi-directory "/EFI/BOOT")) + ;; Map the grub targets to the boot file names expected by + ;; UEFI compliant firmware. See "Removable Media Boot Behavior": + ;; http://www.uefi.org/sites/default/files/resources/UEFI%20Spec%202_6.pdf + (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-tmp (string-append target "/tmp")) + (grub.cfg (string-append grub-tmp "/grub.cfg"))) + (display "mounting EFI system partition...\n") + (mkdir-p efi-directory) + (mount (partition-device esp) efi-directory + (partition-file-system esp)) + (mkdir-p efi-payload-directory) + + ;; Grub needs a tmpdir to prepare the image. + (setenv "TMPDIR" grub-tmp) + ;; We also need a tiny configuration file telling the EFI blob where + ;; to find 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 grub firmware image...\n") + (unless (zero? (system* "grub-mkstandalone" "-O" (car efi-target-map) + "-o" (string-append efi-payload-directory "/" + (cdr efi-target-map)) + ;; Graft the contents of our configuration file + ;; into the image. See grub-mkstandalone(1). + (string-append "boot/grub/grub.cfg=" grub.cfg))) + (error "failed to create grub EFI image")) + + (delete-file grub.cfg) + (umount efi-directory))) + (when bootloader-installer (bootloader-installer bootloader device target)) diff --git a/gnu/system/vm.scm b/gnu/system/vm.scm index 099e3fac3..04ccc3650 100644 --- a/gnu/system/vm.scm +++ b/gnu/system/vm.scm @@ -3,6 +3,7 @@ ;;; Copyright © 2016 Christopher Allan Webber ;;; Copyright © 2016 Leo Famulari ;;; Copyright © 2017 Mathieu Othacehe +;;; Copyright © 2017 Marius Bakke ;;; ;;; 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,18 @@ 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)) + (partition + ;; Append a small FAT32 partition for + ;; use with UEFI bootloaders. + (size (* 40 (expt 2 20))) + (label "gnu-esp") + (file-system "vfat") + (flags '(esp)))))) (initialize-hard-disk "/dev/vda" #:partitions partitions #:bootloader -- 2.12.2