unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob 2db5c258f0a8d73c64562dd12a47fbe994817872 15014 bytes (raw)
name: gnu/system/boot.scm 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2013-2022 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
;;; Copyright © 2017 David Craven <david@craven.ch>
;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
;;; Copyright © 2019, 2020 Miguel Ángel Arruga Vivas <rosen644835@gmail.com>
;;; Copyright © 2020 Danny Milosavljevic <dannym@scratchpost.org>
;;; Copyright © 2020 Stefan <stefan-guix@vodafonemail.de>
;;; Copyright © 2020, 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com>
;;; Copyright © 2020 Janneke Nieuwenhuizen <jannek@gnu.org>
;;; Copyright © 2020, 2022 Efraim Flashner <efraim@flashner.co.il>
;;; Copyright © 2024 Nicolas Graves <ngraves@ngraves.fr>
;;; Copyright © 2024 Felix Lechner <felix.lechner@lease-up.com>
;;; Copyright © 2024 Herman Rimm <herman@rimm.ee>
;;; Copyright © 2024 Lilah Tascheter <lilah@lunabee.space>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu system boot)
  #:use-module (guix gexp)
  #:use-module (guix diagnostics)
  #:use-module (guix i18n)
  #:use-module (guix records)
  #:use-module (guix store)
  #:use-module (guix utils)
  #:use-module (gnu bootloader)
  #:use-module (gnu system file-systems)
  #:use-module (gnu system uuid)
  #:use-module (ice-9 format)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-19)
  #:use-module (srfi srfi-34)
  #:use-module (srfi srfi-35)
  #:use-module (rnrs bytevectors)
  #:export (boot-parameters
            boot-parameters?
            boot-parameters-label
            boot-parameters-root-device
            boot-parameters-bootloader-name
            boot-parameters-store-crypto-devices
            boot-parameters-store-device
            boot-parameters-store-directory-prefix
            boot-parameters-store-mount-point
            boot-parameters-locale
            boot-parameters-kernel
            boot-parameters-kernel-arguments
            boot-parameters-initrd
            boot-parameters-multiboot-modules
            boot-parameters-version
            %boot-parameters-version

            read-boot-parameters
            read-boot-parameters-file
            bootable-kernel-arguments

            boot-alternative
            boot-alternative?
            boot-alternative-generation
            boot-alternative-system-path
            boot-alternative-epoch
            boot-alternative-parameters

            epoch->date-string
            decorated-boot-label
            boot-parameters->menu-entry
            boot-alternative->menu-entry

            ensure-not-/dev
            system-linux-image-file-name))

;;;
;;; Boot parameters
;;;

;;; Version 1 was introduced early 2022 to mark the departure from long option
;;; names such as '--load' to the more conventional initrd option names like
;;; 'gnu.load'.
;;;
;;; When bumping the boot-parameters version, increment it by one (1).
(define %boot-parameters-version 1)

(define-record-type* <boot-parameters>
  boot-parameters make-boot-parameters boot-parameters?
  (label            boot-parameters-label)
  ;; Because we will use the 'store-device' to create the GRUB search command,
  ;; the 'store-device' has slightly different semantics than 'root-device'.
  ;; The 'store-device' can be a file system uuid, a file system label, or #f,
  ;; but it cannot be a device file name such as "/dev/sda3", since GRUB would
  ;; not understand that.  The 'root-device', on the other hand, corresponds
  ;; exactly to the device field of the <file-system> object representing the
  ;; OS's root file system, so it might be a device file name like
  ;; "/dev/sda3".  The 'store-directory-prefix' field contains #f or the store
  ;; file name inside the 'store-device' as it is seen by GRUB, e.g. it would
  ;; contain "/storefs" if the store is located in that subvolume of a btrfs
  ;; partition.
  (root-device      boot-parameters-root-device)
  (bootloader-name  boot-parameters-bootloader-name)
  (store-device     boot-parameters-store-device)
  (store-mount-point boot-parameters-store-mount-point)
  (store-directory-prefix boot-parameters-store-directory-prefix)
  (store-crypto-devices boot-parameters-store-crypto-devices
                        (default '()))
  (locale           boot-parameters-locale)
  (kernel           boot-parameters-kernel)
  (kernel-arguments boot-parameters-kernel-arguments)
  (initrd           boot-parameters-initrd)
  (multiboot-modules boot-parameters-multiboot-modules)
  (version          boot-parameters-version  ;positive integer
                    (default %boot-parameters-version)))

(define (read-boot-parameters port)
  "Read boot parameters from PORT and return the corresponding
<boot-parameters> object.  Raise an error if the format is unrecognized."
  (define device-sexp->device
    (match-lambda
      (('uuid (? symbol? type) (? bytevector? bv))
       (bytevector->uuid bv type))
      (('file-system-label (? string? label))
       (file-system-label label))
      ((? bytevector? bv)                         ;old format
       (bytevector->uuid bv 'dce))
      ((? string? device)
       (if (string-contains device ":/")
           device ; nfs-root
           ;; It used to be that we would not distinguish between labels and
           ;; device names.  Try to infer the right thing here.
           (if (string-prefix? "/" device)
               device
               (file-system-label device))))))
  (define uuid-sexp->uuid
    (match-lambda
      (('uuid (? symbol? type) (? bytevector? bv))
       (bytevector->uuid bv type))
      (x
       (warning (G_ "unrecognized uuid ~a at '~a'~%") x (port-filename port))
       #f)))

  ;; New versions are not backward-compatible, so only accept past and current
  ;; versions, not future ones.
  (define (version? n)
    (member n (iota (1+ %boot-parameters-version))))

  (match (read port)
    (('boot-parameters ('version (? version? version))
                       ('label label) ('root-device root)
                       ('kernel kernel)
                       rest ...)
     (boot-parameters
      (version version)
      (label label)
      (root-device (device-sexp->device root))

      (bootloader-name
       (match (assq 'bootloader-name rest)
         ((_ (args ...)) args)
         ((_ args) (list args))
         (#f       'grub))) ; for compatibility reasons.

      ;; In the past, we would store the directory name of linux instead of
      ;; the absolute file name of its image.  Detect that and correct it.
      (kernel (if (string=? kernel (direct-store-path kernel))
                  (string-append kernel "/"
                                 (system-linux-image-file-name))
                  kernel))

      (kernel-arguments
       (match (assq 'kernel-arguments rest)
         ((_ args) args)
         (#f       '())))                         ;the old format

      (initrd
       (match (assq 'initrd rest)
         (('initrd ('string-append directory file)) ;the old format
          (string-append directory file))
         (('initrd (? string? file))
          file)
         (#f #f)))

      (multiboot-modules
       (match (assq 'multiboot-modules rest)
         ((_ args) args)
         (#f       '())))

      (locale
       (match (assq 'locale rest)
         ((_ locale) locale)
         (#f         #f)))

      (store-device
       ;; Linux device names like "/dev/sda1" are not suitable GRUB device
       ;; identifiers, so we just filter them out.
       (ensure-not-/dev
        (match (assq 'store rest)
          (('store ('device #f) _ ...)
           root-device)
          (('store ('device device) _ ...)
           (device-sexp->device device))
          (_                                      ;the old format
           root-device))))

      (store-directory-prefix
       (match (assq 'store rest)
         (('store . store-data)
          (match (assq 'directory-prefix store-data)
            (('directory-prefix prefix) prefix)
            ;; No directory-prefix found.
            (_ #f)))
         (_
          ;; No store found, old format.
          #f)))

      (store-crypto-devices
       (match (assq 'store rest)
         (('store . store-data)
          (match (assq 'crypto-devices store-data)
            (('crypto-devices (devices ...))
             (map uuid-sexp->uuid devices))
            (('crypto-devices dev)
             (warning (G_ "unrecognized crypto-devices ~S at '~a'~%")
                      dev (port-filename port))
             '())
            (_
             ;; No crypto-devices found.
             '())))
         (_
          ;; No store found, old format.
          '())))

      (store-mount-point
       (match (assq 'store rest)
         (('store ('device _) ('mount-point mount-point) _ ...)
          mount-point)
         (_                                       ;the old format
          "/")))))
    (x                                            ;unsupported format
     (raise
      (make-compound-condition
       (formatted-message
        (G_ "unrecognized boot parameters at '~a'~%")
        (port-filename port))
       (condition
        (&fix-hint (hint (format #f (G_ "This probably means that this version
of Guix is older than the one that created @file{~a}.  To address this, you
need to update Guix:

@example
guix pull
@end example")
                                 (port-filename port))))))))))

(define (read-boot-parameters-file system)
  "Read boot parameters from SYSTEM's (system or generation) \"parameters\"
file and returns the corresponding <boot-parameters> object or #f if the
format is unrecognized.
The object has its kernel-arguments extended in order to make it bootable."
  (let* ((file (string-append system "/parameters"))
         (params (call-with-input-file file read-boot-parameters))
         (root (boot-parameters-root-device params))
         (version (boot-parameters-version params)))
    (boot-parameters
     (inherit params)
     (kernel-arguments (append (bootable-kernel-arguments system root version)
                               (boot-parameters-kernel-arguments params))))))

(define* (bootable-kernel-arguments system root-device version)
  "Return a list of kernel arguments (gexps) to boot SYSTEM from ROOT-DEVICE.
VERSION is the target version of the boot-parameters record."
  ;; If the version is newer than 0, we use the new style initrd parameter
  ;; names, otherwise we use the legacy ones.  This is to maintain backward
  ;; compatibility when producing bootloader configurations for older
  ;; generations.
  (define version>0? (> version 0))
  (let ((root (file-system-device->string root-device
                                          #:uuid-type 'dce)))
    (append
     (if (string=? root "none")
         '() ;  Ignore the case where the root is "none" (typically tmpfs).
         ;; Note: Always use the DCE format because that's what
         ;; (gnu build linux-boot) expects for the 'root'
         ;; kernel command-line option.
         (list (string-append (if version>0? "root=" "--root=") root)))
     (list #~(string-append (if #$version>0? "gnu.system=" "--system=") #$system)
           #~(string-append (if #$version>0? "gnu.load=" "--load=")
                            #$system "/boot")))))

(define-record-type* <boot-alternative>
  boot-alternative make-boot-alternative boot-alternative?
  (generation  boot-alternative-generation)
  (system-path boot-alternative-system-path)
  (epoch       boot-alternative-epoch)
  (parameters  boot-alternative-parameters)) ; <boot-parameters>

(define (epoch->date-string epoch)
  "Return a string representing the date for EPOCH seconds."
  (let ((time (make-time time-utc 0 epoch)))
    (date->string (time-utc->date time)
                  "~Y-~m-~d ~H:~M")))

(define (decorated-boot-label text generation epoch)
  "Return a string for a nice boot label that includes TEXT, a numbered
GENERATION, and a timestamp derived from EPOCH seconds."
  (let ((count (and generation
                    (string-append "#" (number->string generation))))
        (timestamp (and=> epoch epoch->date-string)))
    (match (filter identity (list count timestamp))
      (() text)
      (extras (string-append text "(" (string-join extras ", ") ")")))))

(define (boot-parameters->menu-entry conf)
  "Return a <menu-entry> instance given CONF, a <boot-parameters> instance."
  (let* ((kernel (boot-parameters-kernel conf))
         (multiboot-modules (boot-parameters-multiboot-modules conf))
         (multiboot? (pair? multiboot-modules)))
    (menu-entry
     (label (boot-parameters-label conf))
     (device (boot-parameters-store-device conf))
     (device-mount-point (boot-parameters-store-mount-point conf))
     (device-subvol (boot-parameters-store-directory-prefix conf))
     (linux (and (not multiboot?) kernel))
     (linux-arguments (if (not multiboot?)
                          (boot-parameters-kernel-arguments conf)
                          '()))
     (initrd (boot-parameters-initrd conf))
     (multiboot-kernel (and multiboot? kernel))
     (multiboot-arguments (if multiboot?
                              (boot-parameters-kernel-arguments conf)
                              '()))
     (multiboot-modules (if multiboot?
                            (boot-parameters-multiboot-modules conf)
                            '())))))

(define boot-alternative->menu-entry
  (compose boot-parameters->menu-entry boot-alternative-parameters))

(define (ensure-not-/dev device)
  "If DEVICE starts with a slash, return #f.  This is meant to filter out
Linux device names such as /dev/sda, and to preserve GRUB device names and
file system labels."
  (if (and (string? device) (string-prefix? "/" device))
      #f
      device))

;; XXX: defined here instead of (gnu system) to prevent dependency loop
(define* (system-linux-image-file-name #:optional
                                       (target (or (%current-target-system)
                                                   (%current-system))))
  "Return the basename of the kernel image file for TARGET."
  (cond
   ((string-prefix? "arm" target) "zImage")
   ((string-prefix? "mips" target) "vmlinuz")
   ((string-prefix? "aarch64" target) "Image")
   ((string-prefix? "riscv64" target) "Image")
   (else "bzImage")))

;;; boot.scm ends here

debug log:

solving 2db5c258f0 ...
found 2db5c258f0 in https://yhetil.org/guix-patches/dca9304c33b9a7a767a22582aa63b504a8dc034b.1727201267.git.herman@rimm.ee/
found 98fcd2b3a0 in https://yhetil.org/guix-patches/33b51456dc709d17c8be3776471e0599f83eaec1.1727345067.git.herman@rimm.ee/
found 54e5673a54 in https://yhetil.org/guix-patches/c0d6bcb2d8c56569b96ac9674de337188dd77c0b.1727345067.git.herman@rimm.ee/
found de312c7208 in https://yhetil.org/guix-patches/8edc2882011d47a2263f3d8069defb30427f6d96.1726913452.git.herman@rimm.ee/
found 1e0d75212f in https://yhetil.org/guix-patches/f734db8e278920da59c704357a6bd0372ef43c6d.1726913452.git.herman@rimm.ee/
found c1d58a2743 in https://yhetil.org/guix-patches/c9dace7f7e80907dddcac5d050fc2c7152c23207.1726913452.git.herman@rimm.ee/
found 2c531e4ad5 in https://yhetil.org/guix-patches/cee6388d584da07748378256a133492c0be0a70d.1726913452.git.herman@rimm.ee/

applying [1/7] https://yhetil.org/guix-patches/cee6388d584da07748378256a133492c0be0a70d.1726913452.git.herman@rimm.ee/
diff --git a/gnu/system/boot.scm b/gnu/system/boot.scm
new file mode 100644
index 0000000000..2c531e4ad5


applying [2/7] https://yhetil.org/guix-patches/c9dace7f7e80907dddcac5d050fc2c7152c23207.1726913452.git.herman@rimm.ee/
diff --git a/gnu/system/boot.scm b/gnu/system/boot.scm
index 2c531e4ad5..c1d58a2743 100644


applying [3/7] https://yhetil.org/guix-patches/f734db8e278920da59c704357a6bd0372ef43c6d.1726913452.git.herman@rimm.ee/
diff --git a/gnu/system/boot.scm b/gnu/system/boot.scm
index c1d58a2743..1e0d75212f 100644


applying [4/7] https://yhetil.org/guix-patches/8edc2882011d47a2263f3d8069defb30427f6d96.1726913452.git.herman@rimm.ee/
diff --git a/gnu/system/boot.scm b/gnu/system/boot.scm
index 1e0d75212f..de312c7208 100644


applying [5/7] https://yhetil.org/guix-patches/c0d6bcb2d8c56569b96ac9674de337188dd77c0b.1727345067.git.herman@rimm.ee/
diff --git a/gnu/system/boot.scm b/gnu/system/boot.scm
index de312c7208..54e5673a54 100644


applying [6/7] https://yhetil.org/guix-patches/33b51456dc709d17c8be3776471e0599f83eaec1.1727345067.git.herman@rimm.ee/
diff --git a/gnu/system/boot.scm b/gnu/system/boot.scm
index 54e5673a54..98fcd2b3a0 100644


applying [7/7] https://yhetil.org/guix-patches/dca9304c33b9a7a767a22582aa63b504a8dc034b.1727201267.git.herman@rimm.ee/
diff --git a/gnu/system/boot.scm b/gnu/system/boot.scm
index 98fcd2b3a0..2db5c258f0 100644

Checking patch gnu/system/boot.scm...
Applied patch gnu/system/boot.scm cleanly.
Checking patch gnu/system/boot.scm...
Applied patch gnu/system/boot.scm cleanly.
Checking patch gnu/system/boot.scm...
Applied patch gnu/system/boot.scm cleanly.
Checking patch gnu/system/boot.scm...
Applied patch gnu/system/boot.scm cleanly.
Checking patch gnu/system/boot.scm...
Applied patch gnu/system/boot.scm cleanly.
Checking patch gnu/system/boot.scm...
Applied patch gnu/system/boot.scm cleanly.
Checking patch gnu/system/boot.scm...
Applied patch gnu/system/boot.scm cleanly.

index at:
100644 2db5c258f0a8d73c64562dd12a47fbe994817872	gnu/system/boot.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).