From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms13.migadu.com with LMTPS id WCgjIKEz9WbzGwAAe85BDQ:P1 (envelope-from ) for ; Thu, 26 Sep 2024 10:12:49 +0000 Received: from aspmx1.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id WCgjIKEz9WbzGwAAe85BDQ (envelope-from ) for ; Thu, 26 Sep 2024 12:12:49 +0200 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=debbugs.gnu.org header.s=debbugs-gnu-org header.b="u/s7wcso"; dkim=fail ("headers rsa verify failed") header.d=rimm.ee header.s=herman header.b=nqtLLVHw; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1727345569; h=from:from:sender:sender:reply-to: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:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=uo7S4ucruDPB9WMbXGJxtKmL+muhmw/dHd4Iz8LonXI=; b=IwiouPAid8oaIqubPDQ8LUUkrzF1T2By7gJfQkIRSbC2tlIcXOqizc8EotmGR2XfSOUUt5 bkW23kLxrY+vAK1+u1Lx/ZUVuJpf1qN7ONYhIZk/r1hzvq1NdlEVskkpGocS3bceYe1IPw qrveKe5trjJ9SdibsWMeg6FnbmBNUapz6g3KevnwLgyW8aQYluI3VWBVVeU+vYJzm1GO/G DapoIsIzr/t93vzPKvlY87bSOjqT+IxBqlMYUHRByjpcFn06iaNKZqzrjzUJFmZ5tFN1El ZX6H81LOencmeW9gkISFvGfytoUZn+EJoZyxgIl7vYo/RcaSqrLAHOCXYj/POw== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1727345569; a=rsa-sha256; cv=none; b=c6oVpMJ1GmN7MrMs6Scsdy8oSkU72LVVa4Ng02zpHBouz/yTG6a2GThV1VBNqt+4YHung/ ogQEgTGIKaUrHwQ6JXp8Z/XVwcmC4rsiW0eMAdb+6kitvtN/XKmAmf0H8ZIc35qEaqe2HQ K2sTwCh1g//sfETU5PNqDpckQuaaMxYVpYf1gT+MXG+6GR57JAA0Tf/JDpNm94qVOlykMl AH089h74iKKKLiK6gMbOHvjejIUlcc/yxzh7l7XU0dQZgoEFJkWF+ugN5I3GB13TNTBzpt qY6Fw6g9CLnkkmg/NyuaA1xS+ZmkGtnB1BzJhRM+bzkc3fHavQqRx250JErvhg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=debbugs.gnu.org header.s=debbugs-gnu-org header.b="u/s7wcso"; dkim=fail ("headers rsa verify failed") header.d=rimm.ee header.s=herman header.b=nqtLLVHw; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 2C43B11DB9 for ; Thu, 26 Sep 2024 12:12:49 +0200 (CEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1stlTI-0002xr-EC; Thu, 26 Sep 2024 06:11:40 -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 1stlTH-0002xI-5i for guix-patches@gnu.org; Thu, 26 Sep 2024 06:11:39 -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 1stlTG-0007PT-SS; Thu, 26 Sep 2024 06:11:38 -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=l5rNuJOD6zluhgdl6fBRO8pUfoQpElxldvIzeyjEXkY=; b=u/s7wcsoPSoQaFN2V1/6hbgFyRuOkX1jmUuTbRqdwFNNMkOv3gL0J3BDUu3mvOVvPBdZxPlH4V/EPxctFSCtgJhxGIISJ62J2Fa7zDHNbIUZ9DtO3qCs0ioIMfOu5oIauFUCin88qsOQ6OcAfmYWDafmOMxzH1B+0XqEuivPChZ5+k3px1nU3EgE765IUHgixFbF7nXvncQ4bMDABND1Ibj5vd2ZZUQnxnRT15Yq01gC/fKWSboGeUqO03xa4m+YdQQomUmr1HAE0Fp2YKFsB9DWNdGE0yFFG4qTclHUOiXGArAHWOy6XHziPd35+n8IpJMIgXdUMkzbBWqLffYRvQ==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1stlTh-0003Un-Px; Thu, 26 Sep 2024 06:12:05 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#73202] [PATCH v3 08/14] gnu: bootloader: Add bootloader-target record and infastructure. Resent-From: Herman Rimm Original-Sender: "Debbugs-submit" Resent-CC: guix@cbaines.net, dev@jpoiret.xyz, lilah@lunabee.space, ludo@gnu.org, othacehe@gnu.org, zimon.toutoune@gmail.com, me@tobias.gr, guix-patches@gnu.org Resent-Date: Thu, 26 Sep 2024 10:12:05 +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 , Christopher Baines , Josselin Poiret , Lilah Tascheter , Ludovic =?UTF-8?Q?Court=C3=A8s?= , Mathieu Othacehe , Simon Tournier , Tobias Geerinckx-Rice X-Debbugs-Original-Xcc: Christopher Baines , Josselin Poiret , Lilah Tascheter , Ludovic =?UTF-8?Q?Court=C3=A8s?= , Mathieu Othacehe , Simon Tournier , Tobias Geerinckx-Rice Received: via spool by 73202-submit@debbugs.gnu.org id=B73202.172734550513303 (code B ref 73202); Thu, 26 Sep 2024 10:12:05 +0000 Received: (at 73202) by debbugs.gnu.org; 26 Sep 2024 10:11:45 +0000 Received: from localhost ([127.0.0.1]:55498 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1stlTM-0003ST-GT for submit@debbugs.gnu.org; Thu, 26 Sep 2024 06:11:45 -0400 Received: from 81-205-150-117.fixed.kpn.net ([81.205.150.117]:53171 helo=email.rimm.ee) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1stlSz-0003PN-3Q for 73202@debbugs.gnu.org; Thu, 26 Sep 2024 06:11:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rimm.ee; s=herman; t=1727345440; 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=uo7S4ucruDPB9WMbXGJxtKmL+muhmw/dHd4Iz8LonXI=; b=nqtLLVHw7MEsJMktcC1FVDCFandxYnFiAOcNsej9p5aiEkRmaevEj8g7TlPwIyY8m8Ufep HXrEpJef54ToX3BNw07dHJaC3zrrTsSAbFpRQ03x2I2BjrVZaPNpceePFGBk6a9CyYAMMI Q6T1rWhDXcjp8Y4dv2r0iW58wUTeAKI1eSVjozcnPxZDdPLg3EMf77XEifviJL7VDRAsPf MaqJQyAbc0XmWbrx3ToWmbW4qcXoGy8hjsizVUR21jGm/dHIHRHt9rxUOSv1TmOYEtyyDy sLydgDcWspMUmb3D5A8l/Cu4NthQFvqFXTKCA8+FC8A4ccujl+thLHd3xuld0A== Received: by 81-205-150-117.fixed.kpn.net (OpenSMTPD) with ESMTPSA id b097df94 (TLSv1.3:TLS_CHACHA20_POLY1305_SHA256:256:NO); Thu, 26 Sep 2024 10:10:40 +0000 (UTC) Date: Thu, 26 Sep 2024 12:09:05 +0200 Message-ID: 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 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 From: Herman Rimm via Guix-patches via Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: guix-patches-bounces+larch=yhetil.org@gnu.org X-Migadu-Country: US X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -3.41 X-Spam-Score: -3.41 X-Migadu-Scanner: mx13.migadu.com X-Migadu-Queue-Id: 2C43B11DB9 X-TUID: 1AiPHLxM1ka0 From: Lilah Tascheter * gnu/bootloader.scm (bootloader-target): New record. (&target-error): New condition. (pathcat, get-target-of-type, parent-of, unfold-pathcat, target-base?, type-major?, ensure, ensure-target-types, ensure-majors, gbegin): New procedures. (define-literal, with-targets, :path, :devpath, :device, :fs, :label, :uuid): New macros. * guix/ui.scm (call-with-error-handling)[target-error?]: Handle target-errors. Change-Id: I3f07c9096dd8b91c04449b6360b3b7d21640da14 --- gnu/bootloader.scm | 229 ++++++++++++++++++++++++++++++++++++++++++++- guix/ui.scm | 9 ++ 2 files changed, 233 insertions(+), 5 deletions(-) diff --git a/gnu/bootloader.scm b/gnu/bootloader.scm index 3ea50a4004..0c24996205 100644 --- a/gnu/bootloader.scm +++ b/gnu/bootloader.scm @@ -7,6 +7,7 @@ ;;; Copyright © 2022 Josselin Poiret ;;; Copyright © 2022 Reza Alizadeh Majd ;;; Copyright © 2024 Tomas Volf <~@wolfsden.cz> +;;; Copyright © 2024 Lilah Tascheter ;;; ;;; This file is part of GNU Guix. ;;; @@ -24,19 +25,28 @@ ;;; along with GNU Guix. If not, see . (define-module (gnu bootloader) + #:autoload (gnu build file-systems) + (read-partition-label read-partition-uuid + find-partition-by-label find-partition-by-uuid) #:use-module (gnu system file-systems) #:use-module (gnu system uuid) - #:use-module (guix gexp) - #:use-module (guix profiles) - #:use-module (guix records) + #:autoload (guix build syscalls) + (mounts mount-source mount-point mount-type) #:use-module (guix deprecation) - #:use-module ((guix ui) #:select (warn-about-load-error)) #:use-module (guix diagnostics) + #:use-module (guix gexp) #:use-module (guix i18n) + #:use-module (guix modules) + #:use-module (guix profiles) + #:use-module (guix records) + #:use-module (guix utils) + #:use-module (ice-9 match) + #:use-module (ice-9 receive) + #:use-module (rnrs bytevectors) #:use-module (srfi srfi-1) + #:use-module (srfi srfi-26) #:use-module (srfi srfi-34) #:use-module (srfi srfi-35) - #:use-module (ice-9 match) #:export (menu-entry menu-entry? menu-entry-label @@ -62,6 +72,25 @@ (define-module (gnu bootloader) bootloader-configuration-file bootloader-configuration-file-generator + bootloader-target + bootloader-target? + bootloader-target-type + bootloader-target-expected? + bootloader-target-path + bootloader-target-offset + bootloader-target-device + bootloader-target-file-system + bootloader-target-label + bootloader-target-uuid + + target-error? + target-error-type + target-error-targets + + gbegin + :path :devpath :device :fs :label :uuid + with-targets + bootloader-configuration bootloader-configuration? bootloader-configuration-bootloader @@ -232,6 +261,196 @@ (define-record-type* (configuration-file bootloader-configuration-file) (configuration-file-generator bootloader-configuration-file-generator)) + +;;; +;;; Bootloader target record. +;;; + +;; represents different kinds of targets in a +;; normalized form. + +(define-record-type* + bootloader-target make-bootloader-target bootloader-target? + (type bootloader-target-type) ; symbol + (expected? bootloader-target-expected? (default #f)) ; bool + + (path bootloader-target-path (default #f)) ; string|#f + (offset bootloader-target-offset (thunked) ; symbol|#f + (default (and (bootloader-target-path this-record) + (not (eq? (bootloader-target-type this-record) 'root)) + 'root))) + (device bootloader-target-device (default #f)) ; string|#f + (file-system bootloader-target-file-system (default #f)) ; string|#f + (label bootloader-target-label (default #f)) ; string|#f + (uuid bootloader-target-uuid (default #f))) ; uuid|#f + +(define-condition-type &target-error &error target-error? + (type target-error-type) + (targets target-error-targets)) + +(define (pathcat p1 p2) + (string-append (string-trim-right p1 #\/) "/" (string-trim p2 #\/))) + +(define* (get-target-of-type type targets #:optional require?) + "Finds a target in TARGETS of type TYPE, returns REQUIRE? if #false, +or provides an error otherwise." + (define (type? target) + (eq? type (bootloader-target-type target))) + (match (filter type? targets) + ((target _ ...) target) + (_ (and require? + (raise + (condition + (&message (message (G_ "required, but not provided"))) + (&target-error (type type) (targets targets)))))))) + +(define (parent-of target targets) + "Resolve the parent of TARGET in TARGETS, return #f if orphan." + (and=> (bootloader-target-offset target) + (cut get-target-of-type <> targets #t))) + +(define (unfold-pathcat target targets) + "Find the full VFS path of TARGET." + (let ((quit (lambda (t) (not (and=> t bootloader-target-path)))) + (parent-of (cut parent-of <> targets))) + (reduce pathcat #f + (unfold quit bootloader-target-path parent-of target)))) + +(define (target-base? t) + (or (not t) (match-record t + (expected? offset device label uuid) + (or device label uuid (not offset) expected?)))) + +(define (type-major? target) (memq target '(root esp disk))) + +(define (ensure types targets end) + (let* ((used-in (cute unfold end identity (cut parent-of <> targets) <>)) + (cons-in (lambda (t) (cons t (used-in t)))) + (ensure (map (cut get-target-of-type <> targets #t) types))) + (filter identity (apply append (map cons-in ensure))))) + +(define* (ensure-target-types types targets #:optional (base? #f)) + "Ensures all TYPES are provided in TARGETS. Returns #t iff every ensured +target and its requirements are fully provided. Errors out when a required TYPE +isn't provided. When BASE?, only ensure path requirements up to a device." + (not (any bootloader-target-expected? + (ensure types targets (if base? target-base? not))))) + +(define (ensure-majors types targets) + "Errors out when a required TYPE isn't provided, or when use of multiple major +targets is detected." + (let* ((all (map bootloader-target-type (ensure types targets target-base?))) + (majors (delete-duplicates (filter type-major? all) eq?))) + (if (< (length majors) 2) #t + (raise (condition (&message (message (G_ "multiple major targets used"))) + (&target-error (type majors) (targets targets))))))) + + + +(define (gbegin . gex) + "Sequence provided g-expressions." + (case (length gex) ((0) #f) ((1) (car gex)) (else #~(begin #$@gex)))) + +;; syntax matching on free literals breaks easily, so bind them +(define-syntax-rule (define-literal id) (define-syntax id (syntax-rules ()))) +(define-literal :path) +(define-literal :devpath) +(define-literal :device) +(define-literal :fs) +(define-literal :label) +(define-literal :uuid) + +(define-syntax with-targets + (cut syntax-case <> () + ((_ targets-expr block ...) + (let* ((genvars (compose generate-temporaries make-list)) + (targets (car (genvars 1)))) + (define (resolve in target base) + (with-syntax ((target target) (base base) (targets targets)) + (syntax-case in + (:path :devpath :device :fs :label :uuid) + ((name _) (not (identifier? #'name)) + #`(_ (syntax-error "binds must be to identifiers" #,in))) + ((name :device) #'(name (bootloader-target-device base))) + ((name :label) #'(name (bootloader-target-label base))) + ((name :uuid) #'(name (bootloader-target-uuid base))) + ((name :fs) #'(name (bootloader-target-file-system base))) + ((name :path) #'(name (unfold-pathcat target targets))) + ((name :devpath) + #'(name (if (target-base? target) + "/" + (pathcat "/" (bootloader-target-path target))))) + (_ #`(_ (syntax-error "invalid binding spec" #,in)))))) + + (define (binds spec) + (syntax-case spec (=>) + ((type => binds ...) + (with-syntax (((target base) (genvars 2)) (targets targets)) + (append + #`((get (lambda (t) (get-target-of-type t targets #t))) + (target (get type)) + (base (if (target-base? target) + target + (get (bootloader-target-offset target))))) + (map (cut resolve <> #'target #'base) #'(binds ...))))) + (_ #f))) + + (define blocks + (cut syntax-case <> () + ((spec ... expr) + (let* ((path? (cut syntax-case <> (:path) ((_ :path) #t) (_ #f))) + (qualified? (cut syntax-case <> (=>) + ((_ => spec ...) (any path? #'(spec ...))) + (_ #f))) + (specs #'(spec ...)) + (lets (apply append (filter-map binds specs))) + (type (cut syntax-case <> (=>) + ((t => _ ...) #'t) (t #'t)))) + (receive (full part) (partition qualified? specs) + #`(and (ensure-majors (list #,@(map type specs)) #,targets) + (ensure-target-types (list #,@(map type part)) + #,targets #t) + (ensure-target-types (list #,@(map type full)) + #,targets #f) + (let* #,lets expr))))) + (bad #'(syntax-error "malformed block" bad)))) + "Using the list TARGETS, evaluate and sequence each BLOCK to produce a +gexp. BLOCK is a set of SPECs followed by an EXPR (evaluating to a gexp). +Each SPEC denotes a type of target to guard EXPR on their existance and +full-qualification. This procedure is linear in regard to BLOCKs. + +SPEC may be of the following forms: +@itemize +@item 'TYPE Requires TYPE to be fully present or promised. Errors otherwise. +@item ('TYPE => (VAR COMPONENT) ...): As type, but also binds variables. TYPE's + COMPONENT is bound to the variable VAR as described below. +@end itemize + +Available COMPONENTs are: +@itemize +@item :path (fully-qualified) +@item :devpath (relative from device) +@item :device (auto-detected from uuid and label if not user-provided) +@item :fs +@item :label +@item :uuid +@end itemize + +Note that installers may be called multiple times with different targets being +fully-qualified. To ensure that targets aren't installed multiple times, make sure +that each BLOCK ensures at least one major target, either directly or indirectly. +Likewise, at most one major target should be ensured per BLOCK, under the same +conditions. Major targets originate from disk image handling, and are currently: +@itemize +@item disk +@item root +@item esp +@end itemize" + #`(let ((#,targets targets-expr)) + (apply gbegin (filter identity + (list #,@(map blocks #'(block ...)))))))) + (bad #'(syntax-error "must provide targets" bad)))) + ;;; ;;; Bootloader configuration record. diff --git a/guix/ui.scm b/guix/ui.scm index fe059ba089..663b814da6 100644 --- a/guix/ui.scm +++ b/guix/ui.scm @@ -19,6 +19,7 @@ ;;; Copyright © 2018 Steve Sprang ;;; Copyright © 2022 Taiju HIGASHI ;;; Copyright © 2022 Liliana Marie Prikler +;;; Copyright © 2024 Lilah Tascheter ;;; ;;; This file is part of GNU Guix. ;;; @@ -36,6 +37,8 @@ ;;; along with GNU Guix. If not, see . (define-module (guix ui) ;import in user interfaces only + #:use-module ((gnu bootloader) + #:select (target-error? target-error-type target-error-targets)) #:use-module (guix i18n) #:use-module (guix colors) #:use-module (guix diagnostics) @@ -862,6 +865,12 @@ (define (call-with-error-handling thunk) (invoke-error-stop-signal c) (cons (invoke-error-program c) (invoke-error-arguments c)))) + ((target-error? c) + (leave (G_ "bootloader-target '~a'~@[: ~a~] ~ + among the following targets:~%~{~y~}") + (target-error-type c) + (and (message-condition? c) (condition-message c)) + (target-error-targets c))) ((formatted-message? c) (apply report-error -- 2.45.2