From patchwork Wed May 27 07:24:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mathieu Othacehe X-Patchwork-Id: 22402 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 6659127BBE4; Wed, 27 May 2020 08:25:10 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL,T_DKIM_INVALID,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTP id 6B0D927BBE1 for ; Wed, 27 May 2020 08:25:09 +0100 (BST) Received: from localhost ([::1]:60742 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jdqR2-0004zm-S1 for patchwork@mira.cbaines.net; Wed, 27 May 2020 03:25:08 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41858) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jdqQw-0004zE-Nd for guix-patches@gnu.org; Wed, 27 May 2020 03:25:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:36545) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jdqQw-00019s-Du for guix-patches@gnu.org; Wed, 27 May 2020 03:25:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1jdqQw-0003hk-91 for guix-patches@gnu.org; Wed, 27 May 2020 03:25:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#41560] [PATCH 1/8] bootloader: Add 'disk-image-installer'. References: <20200527072219.25576-1-othacehe@gnu.org> In-Reply-To: <20200527072219.25576-1-othacehe@gnu.org> Resent-From: Mathieu Othacehe Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Wed, 27 May 2020 07:25:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 41560 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 41560@debbugs.gnu.org Cc: Mathieu Othacehe Received: via spool by 41560-submit@debbugs.gnu.org id=B41560.159056427814128 (code B ref 41560); Wed, 27 May 2020 07:25:02 +0000 Received: (at 41560) by debbugs.gnu.org; 27 May 2020 07:24:38 +0000 Received: from localhost ([127.0.0.1]:48073 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jdqQY-0003fi-0G for submit@debbugs.gnu.org; Wed, 27 May 2020 03:24:38 -0400 Received: from mail-wr1-f48.google.com ([209.85.221.48]:44209) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jdqQV-0003fB-Qz for 41560@debbugs.gnu.org; Wed, 27 May 2020 03:24:36 -0400 Received: by mail-wr1-f48.google.com with SMTP id y17so14532536wrn.11 for <41560@debbugs.gnu.org>; Wed, 27 May 2020 00:24:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=5S2+4M+/c3csQQztFclcjyx2E1AOmX0FPz6iYoz78Fw=; b=IUU5FctSnXjV5qeUlxdg1XVQhd18rr0KzVNn9aBTPjphOSSDOAaMwDamRXIeqlopCG D+D4IxIbdxlRWHXShHkyvApEPyz/Xhuz5f46mCvq2zMiiXkRsDOUAzkJy3KNsDbEE6SD E0BnOUYvqdUkEW2fJPNnTQ9KXGiiO0HdPxPqkmppdwNmIr4fdtYUIrK1rHnp7/O8aJjs 3Pt6usD9P8zTvxfmvA2U5/ageYRCYyRM3dhfnnmKMUmZgXWMJWBm85sz6jaWdnD+rzqR fDvyEatOKS4VL32KohcZ7dJJkz4tP9Hnqfagelxu/8o5FXaLS2U3CU72cb7fG5O+99s3 4aUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=5S2+4M+/c3csQQztFclcjyx2E1AOmX0FPz6iYoz78Fw=; b=N9ezcKpvI7zdhUYBm6U3Mn4rtIcdJCqBT7fMALDLp2xzmrM2i7/JXaQPlbc2k7QoJA WFODhlC7kC3sQY1DqtyAW7fvfEzfVIuuODLquAb5oboSLOa6aXH3vw3u069eWcNM1Dvx PCW2p0doE8A1WJ0njsuFEM2UgaYtaoRVpMSepA0TBPSJO5Xwq2dxMyRSpBMDZxF58p/U 26L8deixMkQHrYVtUSVpwS11LpZnToIx0yxStWJ8R9aq5OlmIH2Yj949WS1AShExfgr1 cg9trDglLdF8Vl+eyS79TxvVL1dbDu+If0DA80U3OEOGsfsE5R49SyvL17CM23m62uuL HFEA== X-Gm-Message-State: AOAM532FPLQ/qzxuupTti7wVbNDX7HEaEr7zJFw6mJX4Efo9DuUS7gPj KoFun5K9Q2AkPyy6CBexm/6joDQ0ECs= X-Google-Smtp-Source: ABdhPJzSTRVR+Q+en/ZGk/e97RtRc0N4rumikUXCeTPL4qLx2OBQmr9faNEn0Jh+3M4x7UWop6rouQ== X-Received: by 2002:a5d:4f09:: with SMTP id c9mr5496824wru.3.1590564269174; Wed, 27 May 2020 00:24:29 -0700 (PDT) Received: from meru.fronius.com ([2a01:e0a:fa:a50:e5a2:3f70:d249:6ce9]) by smtp.gmail.com with ESMTPSA id v28sm2002845wra.77.2020.05.27.00.24.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 May 2020 00:24:28 -0700 (PDT) From: Mathieu Othacehe X-Google-Original-From: Mathieu Othacehe Date: Wed, 27 May 2020 09:24:13 +0200 Message-Id: <20200527072420.26140-1-othacehe@gnu.org> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * gnu/bootloader.scm ()[disk-image-installer]: New field, (bootloader-disk-image-installer): export it. * gnu/bootloader/grub.scm (install-grub-disk-image): New procedure ... (grub-bootloader): ... used as "disk-image-installer" here. (grub-efi-bootloader): set "disk-image-installer" to #f. * gnu/system/image.scm (root-partition?, find-root-partition): Move to "Helpers" section. (root-partition-index): New procedure. (system-disk-image): Honor disk-image-installer, and use it to install the bootloader directly on the disk-image, if supported. --- gnu/bootloader.scm | 5 ++++- gnu/bootloader/grub.scm | 45 ++++++++++++++++++++++++++++++++++++++++- gnu/system/image.scm | 33 +++++++++++++++++++++--------- 3 files changed, 71 insertions(+), 12 deletions(-) diff --git a/gnu/bootloader.scm b/gnu/bootloader.scm index 01bdd4acaa..668caa7fc3 100644 --- a/gnu/bootloader.scm +++ b/gnu/bootloader.scm @@ -1,6 +1,6 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2017 David Craven -;;; Copyright © 2017 Mathieu Othacehe +;;; Copyright © 2017, 2020 Mathieu Othacehe ;;; Copyright © 2017 Leo Famulari ;;; Copyright © 2019 Ludovic Courtès ;;; @@ -42,6 +42,7 @@ bootloader-name bootloader-package bootloader-installer + bootloader-disk-image-installer bootloader-configuration-file bootloader-configuration-file-generator @@ -125,6 +126,8 @@ record." (name bootloader-name) (package bootloader-package) (installer bootloader-installer) + (disk-image-installer bootloader-disk-image-installer + (default #f)) (configuration-file bootloader-configuration-file) (configuration-file-generator bootloader-configuration-file-generator)) diff --git a/gnu/bootloader/grub.scm b/gnu/bootloader/grub.scm index bb40c551a7..74dc00480f 100644 --- a/gnu/bootloader/grub.scm +++ b/gnu/bootloader/grub.scm @@ -2,7 +2,7 @@ ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès ;;; Copyright © 2016 Chris Marusich ;;; Copyright © 2017 Leo Famulari -;;; Copyright © 2017 Mathieu Othacehe +;;; Copyright © 2017, 2020 Mathieu Othacehe ;;; Copyright © 2019, 2020 Jan (janneke) Nieuwenhuizen ;;; Copyright © 2020 Maxim Cournoyer ;;; @@ -436,6 +436,47 @@ fi~%")))) "--boot-directory" install-dir device)))) +(define install-grub-disk-image + #~(lambda (bootloader root-index image) + ;; Install GRUB on the given IMAGE. The root partition index is + ;; ROOT-INDEX. + (let ((grub-mkimage + (string-append bootloader "/bin/grub-mkimage")) + (modules '("biosdisk" "part_msdos" "fat" "ext2")) + (grub-bios-setup + (string-append bootloader "/sbin/grub-bios-setup")) + (root-device (format #f "hd0,msdos~a" root-index)) + (boot-img (string-append bootloader "/lib/grub/i386-pc/boot.img")) + (device-map "device.map")) + + ;; Create a minimal, standalone Grub image that will be written + ;; directly in the MBR-GAP (space between the end of the MBR and the + ;; first partition). + (apply invoke grub-mkimage + "-O" "i386-pc" + "-o" "core.img" + "-p" (format #f "(~a)/boot/grub" root-device) + modules) + + ;; Create a device mapping file. + (call-with-output-file device-map + (lambda (port) + (format port "(hd0) ~a~%" image))) + + ;; Copy the default boot.img, that will be written on the MBR sector + ;; by GRUB-BIOS-SETUP. + (copy-file boot-img "boot.img") + + ;; Install both the "boot.img" and the "core.img" files on the given + ;; IMAGE. On boot, the MBR sector will execute the minimal Grub + ;; written in the MBR-GAP. Grub configuration and missing modules will + ;; be read from ROOT-DEVICE. + (invoke grub-bios-setup + "-m" device-map + "-r" root-device + "-d" "." + image)))) + (define install-grub-efi #~(lambda (bootloader efi-dir mount-point) ;; Install GRUB onto the EFI partition mounted at EFI-DIR, for the @@ -465,6 +506,7 @@ fi~%")))) (name 'grub) (package grub) (installer install-grub) + (disk-image-installer install-grub-disk-image) (configuration-file "/boot/grub/grub.cfg") (configuration-file-generator grub-configuration-file))) @@ -480,6 +522,7 @@ fi~%")))) (bootloader (inherit grub-bootloader) (installer install-grub-efi) + (disk-image-installer #f) (name 'grub-efi) (package grub-efi))) diff --git a/gnu/system/image.scm b/gnu/system/image.scm index a1214dd20a..ffc746fcf5 100644 --- a/gnu/system/image.scm +++ b/gnu/system/image.scm @@ -147,6 +147,19 @@ (guix build utils)) gexp* ...)))) +(define (root-partition? partition) + "Return true if PARTITION is the root partition, false otherwise." + (member 'boot (partition-flags partition))) + +(define (find-root-partition image) + "Return the root partition of the given IMAGE." + (srfi-1:find root-partition? (image-partitions image))) + +(define (root-partition-index image) + "Return the index of the root partition of the given IMAGE." + (1+ (srfi-1:list-index identity + (map root-partition? (image-partitions image))))) + ;; ;; Disk image. @@ -276,9 +289,17 @@ image ~a { (let* ((substitutable? (image-substitutable? image)) (builder (with-imported-modules* - (let ((inputs '#+(list genimage coreutils findutils))) + (let ((inputs '#+(list genimage coreutils findutils)) + (bootloader-installer + #+(bootloader-disk-image-installer bootloader))) (set-path-environment-variable "PATH" '("bin" "sbin") inputs) - (genimage #$(image->genimage-cfg image) #$output)))) + (genimage #$(image->genimage-cfg image) #$output) + ;; Install the bootloader directly on the disk-image. + (when bootloader-installer + (bootloader-installer + #+(bootloader-package bootloader) + #$(root-partition-index image) + (string-append #$output "/" #$genimage-name)))))) (image-dir (computed-file "image-dir" builder))) (computed-file name #~(symlink @@ -371,14 +392,6 @@ used in the image. " ;; Image creation. ;; -(define (root-partition? partition) - "Return true if PARTITION is the root partition, false otherwise." - (member 'boot (partition-flags partition))) - -(define (find-root-partition image) - "Return the root partition of the given IMAGE." - (srfi-1:find root-partition? (image-partitions image))) - (define (image->root-file-system image) "Return the IMAGE root partition file-system type." (let ((format (image-format image)))