From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34564) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dAwmL-0007vJ-Tv for guix-patches@gnu.org; Wed, 17 May 2017 07:06:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dAwmJ-0007m5-VK for guix-patches@gnu.org; Wed, 17 May 2017 07:06:05 -0400 Received: from debbugs.gnu.org ([208.118.235.43]:46935) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dAwmJ-0007m1-RR for guix-patches@gnu.org; Wed, 17 May 2017 07:06:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1dAwmJ-0006fk-Jb for guix-patches@gnu.org; Wed, 17 May 2017 07:06:03 -0400 Subject: bug#26815: [PATCH v4 3/3] vm: Add UEFI loader to disk images. Resent-Message-ID: From: Marius Bakke Date: Wed, 17 May 2017 13:05:22 +0200 Message-Id: <20170517110522.18106-3-mbakke@fastmail.com> In-Reply-To: <20170517110522.18106-1-mbakke@fastmail.com> References: <87r2zoygoi.fsf@gnu.org> <20170517110522.18106-1-mbakke@fastmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+kyle=kyleam.com@gnu.org Sender: "Guix-patches" To: 26815@debbugs.gnu.org * 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 | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ gnu/system/vm.scm | 19 +++++++++++++++--- 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/gnu/build/vm.scm b/gnu/build/vm.scm index 7ce1ec8e1..aca35999b 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,33 @@ 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) + "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 + (efi-directory (string-append esp "/EFI/BOOT")) + ;; Map grub target names to boot file names. + (efi-targets (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 requires a TMPDIR to prepare the firmware image. + (setenv "TMPDIR" esp) + + (mkdir-p efi-directory) + (unless (zero? (system* "grub-mkstandalone" "-O" (car efi-targets) + "-o" (string-append efi-directory "/" + (cdr efi-targets)) + ;; Graft the configuration file onto the image. + (string-append "boot/grub/grub.cfg=" config-file))) + (error "failed to create GRUB EFI image")))) + (define* (initialize-hard-disk device #:key bootloader-package @@ -332,8 +360,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 +378,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-package device target)) + (when esp + ;; Mount the ESP somewhere and install GRUB UEFI image. + (let ((mount-point (string-append target "/boot/efi")) + (grub-config (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. + (call-with-output-file grub-config + (lambda (port) + (format port + "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-config) + (display "done.\n") + + (delete-file grub-config) + (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 64770baf1..e0af90bee 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. ;;; @@ -203,7 +204,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) '()))) @@ -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 -- 2.13.0