all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Herman Rimm via Guix-patches via <guix-patches@gnu.org>
To: 73202@debbugs.gnu.org
Cc: Lilah Tascheter <lilah@lunabee.space>
Subject: [bug#73202] [PATCH v2 09/15] gnu: bootloader: Add bootloader-configurations->gexp.
Date: Fri, 20 Sep 2024 12:37:54 +0200	[thread overview]
Message-ID: <36b06c055689a23a29e1ad8cc0e2617a1f57f900.1726827025.git.herman@rimm.ee> (raw)
In-Reply-To: <cover.1726827025.git.herman@rimm.ee>

From: Lilah Tascheter <lilah@lunabee.space>

* gnu/bootloader.scm (bootloader)[default-targets]: Add field.
(target-overrides, normalize, bootloader-configuration->gexp,
bootloader-configurations->gexp): New procedures.

Change-Id: I3654d160f7306bb45a78b82ea6b249ff4281f739
---
 gnu/bootloader.scm | 108 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 108 insertions(+)

diff --git a/gnu/bootloader.scm b/gnu/bootloader.scm
index 0c24996205..c77de6f55e 100644
--- a/gnu/bootloader.scm
+++ b/gnu/bootloader.scm
@@ -67,6 +67,7 @@ (define-module (gnu bootloader)
             bootloader?
             bootloader-name
             bootloader-package
+            bootloader-default-targets
             bootloader-installer
             bootloader-disk-image-installer
             bootloader-configuration-file
@@ -107,6 +108,8 @@ (define-module (gnu bootloader)
             bootloader-configuration-device-tree-support?
             bootloader-configuration-extra-initrd
 
+            bootloader-configuration->gexp
+            bootloader-configurations->gexp
 
             efi-bootloader-chain))
 
@@ -255,6 +258,7 @@ (define-record-type* <bootloader>
   bootloader?
   (name                            bootloader-name)
   (package                         bootloader-package)
+  (default-targets bootloader-default-targets (default '()))
   (installer                       bootloader-installer)
   (disk-image-installer            bootloader-disk-image-installer
                                    (default #f))
@@ -498,6 +502,110 @@ (define (bootloader-configuration-targets config)
       ;; hence the default value of '(#f) rather than '().
       (list #f)))
 
+\f
+;;;
+;;; Bootloader installation paths.
+;;;
+
+(define (target-overrides . layers)
+  (let* ((types (flat-map (cute map bootloader-target-type <>) layers))
+         ;; TODO: use loop instead of fold for early termination.
+         (pred (lambda (type layer found)
+                 (or found (get-target-of-type type layer))))
+         (find (lambda (type) (fold (cute pred type <> <>) #f layers))))
+    (filter identity (map find (delete-duplicates types)))))
+
+(define (normalize targets)
+  "Augments TARGETS with filesystem information at runtime, allowing
+users to specify a lot less information.  Puts TARGETS into a normal
+form, where each path is fully specified up to a device offset."
+  (define (mass m)
+    `((,(mount-source m) . ,m)
+      (,(mount-point m) . ,m)))
+
+  (define (accessible=> d f)
+    (and d (access? d R_OK) (f d)))
+
+  (define (fixuuid target)
+    (match-record target <bootloader-target> (uuid file-system)
+      (let ((type (cond ((not file-system) 'dce)
+                        ((member file-system '("vfat" "fat32")) 'fat)
+                        ((string=? file-system "ntfs") 'ntfs)
+                        ((string=? file-system "iso9660") 'iso9660)
+                        (else 'dce))))
+        (bootloader-target (inherit target)
+          (uuid (cond ((uuid? uuid) uuid)
+                  ((bytevector? uuid) (bytevector->uuid uuid type))
+                  ((string? uuid) (string->uuid uuid type))
+                  (else #f)))))))
+
+  (define (arborify target targets)
+    (let* ((up (lambda (t) (and t (parent-of t targets))))
+           (proto (unfold target-base? identity up (up target) list))
+           (chain (reverse (cons target proto))))
+      (bootloader-target
+        (inherit target)
+        (offset (and=> (car chain) bootloader-target-type))
+        (path (reduce pathcat #f (map bootloader-target-path (cdr chain)))))))
+
+  (let ((amounts (delay (apply append (map mass (mounts))))))
+    (define (assoc-mnt f)
+      (lambda (v) (and=> (assoc-ref (force amounts) v) f)))
+
+    (define (scrape target)
+      (match-record target <bootloader-target>
+        (expected? path offset device label uuid file-system)
+        (if expected? target
+          (bootloader-target
+            (inherit target)
+            (device (or device
+                        (false-if-exception
+                          (or (and=> uuid find-partition-by-uuid)
+                              (and=> label find-partition-by-label)))
+                        (and path ((assoc-mnt mount-source)
+                                   (unfold-pathcat target targets)))))
+            (label (or label (accessible=> device read-partition-label)))
+            (uuid (or uuid (accessible=> device read-partition-uuid)))
+            (file-system (or file-system (and=> device (assoc-mnt mount-type))))
+            (offset (and path offset))
+            (path (or path (and=> device (assoc-mnt mount-point))))))))
+
+    (let ((mid (map (compose fixuuid scrape) targets)))
+      (map (cut arborify <> mid) mid))))
+
+(define* (bootloader-configuration->gexp bootloader-config args #:key
+                                          (root-offset "/") (overrides '()))
+  "Returns a gexp to install BOOTLOADER-CONFIG to its targets, passing ARGS
+to each installer alongside the additional #:bootloader-config keyword
+arguments.  Target OVERRIDES are applied and all path targets have ROOT-OFFSET
+applied.  The following keyword arguments are expected in ARGS:
+@enumerate
+@item current-boot-alternative
+@item old-boot-alternatives
+@item locale (from bootmeta)
+@item store-directory-prefix (from bootmeta)
+@item store-crypto-devices (from bootmeta)
+@end enumerate"
+  (let* ((bootloader (bootloader-configuration-bootloader bootloader-config))
+         (installer (bootloader-installer bootloader))
+         (auto-targets (list (bootloader-target
+                               (type 'root)
+                               (path root-offset)
+                               (offset #f))))
+         (targets (target-overrides
+                    overrides
+                    (bootloader-configuration-targets bootloader-config)
+                    auto-targets
+                    (bootloader-default-targets bootloader)))
+         (conf (bootloader-configuration
+                 (inherit bootloader-config)
+                 (targets (normalize targets)))))
+    (apply installer #:bootloader-config conf args)))
+
+(define (bootloader-configurations->gexp bootloader-configs . rest)
+  (apply gbegin (filter-map (cut apply bootloader-configuration->gexp <> rest)
+                            bootloader-configs)))
+
 \f
 ;;;
 ;;; Bootloaders.
-- 
2.45.2





  parent reply	other threads:[~2024-09-20 10:40 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-12 16:58 [bug#73202] [PATCH] guix: scripts: Rewrite reinstall-bootloader to use provenance data Herman Rimm via Guix-patches via
2024-09-20 10:37 ` [bug#73202] [PATCH v2 00/15] Preparation for bootloader rewrite Herman Rimm via Guix-patches via
2024-09-20 10:37   ` [bug#73202] [PATCH v2 01/15] gnu: bootloader: Remove deprecated bootloader-configuration field Herman Rimm via Guix-patches via
2024-09-20 10:37   ` [bug#73202] [PATCH v2 02/15] gnu: system: Remove useless boot parameters Herman Rimm via Guix-patches via
2024-09-20 10:37   ` [bug#73202] [PATCH v2 03/15] gnu: tests: reconfigure: Remove bootloader install test Herman Rimm via Guix-patches via
2024-09-20 10:37   ` [bug#73202] [PATCH v2 04/15] guix: scripts: Remove unused code Herman Rimm via Guix-patches via
2024-09-20 10:37   ` [bug#73202] [PATCH v2 05/15] guix: scripts: Rewrite reinstall-bootloader to use provenance data Herman Rimm via Guix-patches via
2024-09-20 10:37   ` [bug#73202] [PATCH v2 06/15] guix: utils: Add flatten and flat-map from haunt Herman Rimm via Guix-patches via
2024-09-20 10:37   ` [bug#73202] [PATCH v2 07/15] guix: records: Add wrap-element procedure Herman Rimm via Guix-patches via
2024-09-20 10:37   ` [bug#73202] [PATCH v2 08/15] gnu: bootloader: Add bootloader-target record and infastructure Herman Rimm via Guix-patches via
2024-09-20 10:37   ` Herman Rimm via Guix-patches via [this message]
2024-09-20 10:37   ` [bug#73202] [PATCH v2 10/15] gnu: bootloader: Add device-subvol field to menu-entry record Herman Rimm via Guix-patches via
2024-09-20 10:37   ` [bug#73202] [PATCH v2 11/15] gnu: build: bootloader: Add efi-bootnums procedure Herman Rimm via Guix-patches via
2024-09-20 10:37   ` [bug#73202] [PATCH v2 12/15] gnu: bootloader: Install any bootloader to ESP Herman Rimm via Guix-patches via
2024-09-20 10:37   ` [bug#73202] [PATCH v2 13/15] gnu: bootloader: Match records outside the module Herman Rimm via Guix-patches via
2024-09-20 10:37   ` [bug#73202] [PATCH v2 14/15] gnu: system: boot: Add procedure Herman Rimm via Guix-patches via
2024-09-20 10:38   ` [bug#73202] [PATCH v2 15/15] teams: Add bootloading team Herman Rimm via Guix-patches via
2024-09-21 10:57 ` [bug#73202] [PATCH v2 00/15] Preparation for bootloader rewrite Herman Rimm via Guix-patches via
2024-09-25 20:58   ` Lilah Tascheter via Guix-patches
2024-09-26 10:08 ` [bug#73202] [PATCH v3 01/14] gnu: bootloader: Remove deprecated bootloader-configuration field Herman Rimm via Guix-patches via
2024-09-26 10:08   ` [bug#73202] [PATCH v3 02/14] gnu: system: Remove useless boot parameters Herman Rimm via Guix-patches via
2024-09-26 10:09   ` [bug#73202] [PATCH v3 03/14] gnu: tests: reconfigure: Remove bootloader install test Herman Rimm via Guix-patches via
2024-09-26 10:09   ` [bug#73202] [PATCH v3 04/14] guix: scripts: Remove unused code Herman Rimm via Guix-patches via
2024-09-26 10:09   ` [bug#73202] [PATCH v3 05/14] guix: scripts: Rewrite reinstall-bootloader to use provenance data Herman Rimm via Guix-patches via
2024-09-26 10:09   ` [bug#73202] [PATCH v3 06/14] guix: utils: Add flatten and flat-map from haunt Herman Rimm via Guix-patches via
2024-09-26 10:09   ` [bug#73202] [PATCH v3 07/14] guix: records: Add wrap-element procedure Herman Rimm via Guix-patches via
2024-09-26 10:09   ` [bug#73202] [PATCH v3 08/14] gnu: bootloader: Add bootloader-target record and infastructure Herman Rimm via Guix-patches via
2024-09-26 10:09   ` [bug#73202] [PATCH v3 09/14] gnu: bootloader: Add bootloader-configurations->gexp Herman Rimm via Guix-patches via
2024-09-26 10:09   ` [bug#73202] [PATCH v3 10/14] gnu: bootloader: Add device-subvol field to menu-entry record Herman Rimm via Guix-patches via
2024-09-26 10:09   ` [bug#73202] [PATCH v3 11/14] gnu: build: bootloader: Add efi-bootnums procedure Herman Rimm via Guix-patches via
2024-09-26 10:09   ` [bug#73202] [PATCH v3 12/14] gnu: bootloader: Install any bootloader to ESP Herman Rimm via Guix-patches via
2024-09-26 10:09   ` [bug#73202] [PATCH v3 13/14] gnu: bootloader: Match records outside the module Herman Rimm via Guix-patches via
2024-09-26 10:09   ` [bug#73202] [PATCH v3 14/14] teams: Add bootloading team Herman Rimm via Guix-patches via
2024-10-03 20:32 ` [bug#73202] [PATCH] Preparation for bootloader rewrite Herman Rimm via Guix-patches via
2024-10-04  5:07   ` Lilah Tascheter via Guix-patches
2024-10-04 13:55     ` Herman Rimm via Guix-patches via
2024-10-07 16:59       ` Ryan via Guix-patches via
2024-10-07 19:23         ` Herman Rimm via Guix-patches via
2024-10-08 14:37         ` Ryan via Guix-patches via
2024-10-08 17:23           ` Lilah Tascheter via Guix-patches
2024-10-08 18:05       ` Lilah Tascheter via Guix-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=36b06c055689a23a29e1ad8cc0e2617a1f57f900.1726827025.git.herman@rimm.ee \
    --to=guix-patches@gnu.org \
    --cc=73202@debbugs.gnu.org \
    --cc=herman@rimm.ee \
    --cc=lilah@lunabee.space \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.