From patchwork Thu Sep 26 10:09:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Herman Rimm X-Patchwork-Id: 31496 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 9FD9E27BBEA; Thu, 26 Sep 2024 11:12:29 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-6.4 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_VALIDITY_CERTIFIED, RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE,SPF_HELO_PASS, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 1463C27BBE2 for ; Thu, 26 Sep 2024 11:12:29 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1stlTL-000308-Vb; Thu, 26 Sep 2024 06:11:44 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1stlTI-0002yO-P1 for guix-patches@gnu.org; Thu, 26 Sep 2024 06:11:40 -0400 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1stlTI-0007QU-EY for guix-patches@gnu.org; Thu, 26 Sep 2024 06:11:40 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:References:In-Reply-To:Date:From:To:Subject; bh=IwjQq7hXMawzHIjFS9OpHeTFeaptK6HMgCh8qwZAyV8=; b=kMS6vfzqE06vuF2H3OWJ/+oRMP5C2fJV6bFidsXEeJIQZnge3mPZ4R7tjzXI7Kfga7J7I+DGlmPUm03pFoCcf/aHK7ecxqxdLcOY4Zn2K8GnMjc9Nes+fRMF+23d9wy7B7dzoHgZovuPd4vHo7kQ5xOxkvVMo1ISmDm4pUdEIbj3y7hgP0Wr7j1rgw6RNRpcwQh6UQzFmO6VH/VTBEVWRa+W4H9J21Syl5xHGNCslHnb0+tccuqkXGeX45WW3I6od1CuNZoYfAW+I/IM7qGOAskVWFZn5xVtBaTjZAGz3XPhfNfPedbbV/vCf36btfgHI0eIYEEpOiepS5wfe6cFDw==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1stlTj-0003VQ-D0 for guix-patches@gnu.org; Thu, 26 Sep 2024 06:12:07 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#73202] [PATCH v3 11/14] gnu: build: bootloader: Add efi-bootnums procedure. Resent-From: Herman Rimm Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 26 Sep 2024 10:12:07 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 73202 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 73202@debbugs.gnu.org Cc: Lilah Tascheter Received: via spool by 73202-submit@debbugs.gnu.org id=B73202.172734550713325 (code B ref 73202); Thu, 26 Sep 2024 10:12:07 +0000 Received: (at 73202) by debbugs.gnu.org; 26 Sep 2024 10:11:47 +0000 Received: from localhost ([127.0.0.1]:55504 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1stlTO-0003Sl-Ic for submit@debbugs.gnu.org; Thu, 26 Sep 2024 06:11:47 -0400 Received: from 81-205-150-117.fixed.kpn.net ([81.205.150.117]:39475 helo=email.rimm.ee) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1stlT1-0003PA-UU for 73202@debbugs.gnu.org; Thu, 26 Sep 2024 06:11:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rimm.ee; s=herman; t=1727345442; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ahYOiYejvGUtNAnSjkb6HMGrj1pd/GqjJ61kZBWbi9A=; b=kU3dDqEfP9KtYV9MUCOVtlUrxaVlbzJapexMpfSVIGQ0VhImnM4V7amipd9ZyXbz7wKk5B Y9xZWmgLx53/R5xplTki+yq0Gtp4xd056SnNydyML//gj/Ys7n5I+Et1fPShUuk8rASITK GxXSjOhA/mXCCXAOR+nthleF82d1QSd6vc0PweF9OIHnvmggTKtV4hLnAFupN73vMK5kMN 55tqzboXThqYyYtpKSE4pf/SU4qXRKHnmYFiE36QBkcO4rA7cxZXwJsmpdlFvuwPeL7JYE 1jymYI4l/Y8TmV9/cT1tEJ0f//8pXYtEvxkPSdsiajPg/S+EJIUUnXNttYw7wA== Received: by 81-205-150-117.fixed.kpn.net (OpenSMTPD) with ESMTPSA id fa5314c6 (TLSv1.3:TLS_CHACHA20_POLY1305_SHA256:256:NO); Thu, 26 Sep 2024 10:10:42 +0000 (UTC) Date: Thu, 26 Sep 2024 12:09:08 +0200 Message-ID: <3063da96c7b9dc3db63dbc96d2238f6ed742b857.1727345067.git.herman@rimm.ee> X-Mailer: git-send-email 2.45.2 In-Reply-To: <74c789e74594d538308d33633ed8540283dcde49.1727345067.git.herman@rimm.ee> References: <74c789e74594d538308d33633ed8540283dcde49.1727345067.git.herman@rimm.ee> 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: , Reply-to: Herman Rimm X-ACL-Warn: , Herman Rimm via Guix-patches X-Patchwork-Original-From: Herman Rimm via Guix-patches via From: Herman Rimm Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches From: Lilah Tascheter * gnu/build/bootloader.scm (atomic-copy, efi-bootnums): Add procedures. (in-temporary-directory): Add macro. Change-Id: I3654d160f7306bb45a78b82ea6b249ff4281f739 --- gnu/build/bootloader.scm | 48 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/gnu/build/bootloader.scm b/gnu/build/bootloader.scm index af6063a884..3934e03aee 100644 --- a/gnu/build/bootloader.scm +++ b/gnu/build/bootloader.scm @@ -3,6 +3,7 @@ ;;; Copyright © 2019 Ludovic Courtès ;;; Copyright © 2022 Denis 'GNUtoo' Carikli ;;; Copyright © 2022 Timothy Sample +;;; Copyright © 2024 Lilah Tascheter ;;; ;;; This file is part of GNU Guix. ;;; @@ -20,13 +21,25 @@ ;;; along with GNU Guix. If not, see . (define-module (gnu build bootloader) + #:autoload (guix build syscalls) (free-disk-space) #:use-module (guix build utils) #:use-module (guix utils) #:use-module (ice-9 binary-ports) + #:use-module (guix diagnostics) + #:use-module (guix i18n) #:use-module (ice-9 format) + #:use-module (ice-9 match) + #:use-module (ice-9 popen) + #:use-module (ice-9 receive) + #:use-module (ice-9 regex) #:use-module (rnrs io ports) #:use-module (rnrs io simple) - #:export (write-file-on-device + #:use-module (srfi srfi-1) + #:use-module (srfi srfi-26) + #:use-module (srfi srfi-35) + #:export (atomic-copy + in-temporary-directory + write-file-on-device install-efi-loader)) @@ -34,6 +47,21 @@ (define-module (gnu build bootloader) ;;; Writing utils. ;;; +(define (atomic-copy from to) + (let ((pivot (string-append to ".new"))) + (copy-file from pivot) + (rename-file pivot to))) + +(define-syntax-rule (in-temporary-directory blocks ...) + "Run BLOCKS while chdir'd into a temporary directory." + ;; Under POSIX.1-2008, mkdtemp must make the dir with 700 perms. + (let* ((tmp (or (getenv "TMPDIR") "/tmp")) + (dir (mkdtemp (string-append tmp "/guix-bootloader.XXXXXX"))) + (cwd (getcwd))) + (dynamic-wind (lambda () (chdir dir)) + (lambda () blocks ...) + (lambda () (chdir cwd) (delete-file-recursively dir))))) + (define (write-file-on-device file size device offset) "Write SIZE bytes from FILE to DEVICE starting at OFFSET." (call-with-input-file file @@ -56,6 +84,24 @@ (define (write-file-on-device file size device offset) ;;; EFI bootloader. ;;; +;; XXX: Parsing efibootmgr output may be kinda jank. A better way may exist. +(define (efi-bootnums efibootmgr) + "Returns '(path . bootnum) pairs for each EFI boot entry. bootnum is +a string, and path is backslash-deliminated and relative to the ESP." + (let* ((pipe (open-pipe* OPEN_READ efibootmgr)) + (text (get-string-all pipe)) + (status (status:exit-val (close-pipe pipe))) + (bootnum-pattern + "^Boot([0-9a-fA-F]+).*[^A-Za-z]File\\(([^)]+)\\)$")) + (unless (zero? status) + (raise-exception + (formatted-message (G_ "efibootmgr exited with error code ~a") status))) + (fold-matches (make-regexp bootnum-pattern regexp/newline) text '() + (lambda (match acc) + (let* ((path (match:substring match 2)) + (bootnum (match:substring match 1))) + (cons (cons path bootnum) acc)))))) + (define* (install-efi grub grub-config esp #:key targets) "Write a self-contained GRUB EFI loader to the mounted ESP using GRUB-CONFIG.