summaryrefslogtreecommitdiff
path: root/epistemia/systems/linux-initrd.scm
blob: c727428c7dfd35659f58e51bb889bd8a7ff04bb8 (plain)
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
;;; SPDX-License-Identifier: GPL-3.0-or-later
;; AI disclosure: LLM slop, but it works. This was made before I got more familiar with Guile Scheme and took many retries to get working.
;; TODO: rewrite this later by deleting this and rewriting by reading the main initrd and my previous pre-guix initrd script. could maybe be deduplicated a lot via inherits.
(define-module (epistemia systems linux-initrd)
  #:use-module (guix gexp)
  #:use-module (guix utils)
  #:use-module ((guix store) #:select (%store-prefix))
  #:use-module ((guix derivations) #:select (derivation->output-path))
  #:use-module (guix modules)
  #:use-module (gnu packages compression)
  #:use-module (gnu packages disk)
  #:use-module (gnu packages guile)
  #:use-module (gnu packages linux)
  #:use-module (gnu packages file-systems)
  #:use-module (gnu system file-systems)
  #:use-module (gnu system mapped-devices)
  #:use-module (gnu system keyboard)
  #:use-module (gnu system linux-initrd)
  #:use-module (ice-9 match)
  #:use-module (ice-9 regex)
  #:use-module (ice-9 vlist)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:export (epistemia-zfs-initrd))

(define (flat-linux-module-directory* linux extra-packages modules)
  "Return a flat directory containing the Linux kernel modules listed in
MODULES."
  (define imported-modules
    (source-module-closure '((gnu build linux-modules)
                             (guix build utils))))

  (define build-exp
    (with-imported-modules imported-modules
      (with-extensions (list guile-zlib guile-zstd)
        #~(begin
            (use-modules (gnu build linux-modules)
                         (guix build utils)
                         (rnrs io ports)
                         (srfi srfi-1)
                         (srfi srfi-26)
                         (ice-9 match))

            (define module-dirs
              (map (lambda (pkg) (string-append pkg "/lib/modules"))
                   (cons #$linux '#$extra-packages)))

            (define builtin-modules
              (match (find-files (string-append #$linux "/lib/modules")
                                 (lambda (file stat)
                                   (string=? (basename file) "modules.builtin")))
                ((file . _)
                 (call-with-input-file file
                   (lambda (port)
                     (map file-name->module-name
                          (string-tokenize (get-string-all port))))))
                (_ '())))

            (define modules-to-lookup
              (lset-difference string=? '#$modules builtin-modules))

            (define (lookup-in-dirs name)
              (let loop ((dirs module-dirs))
                (match dirs
                  ((dir . rest)
                   (let ((candidates 
                          (find-files dir 
                                      (lambda (file stat)
                                        (let ((base (basename file)))
                                          (or (string=? base (string-append name ".ko"))
                                              (string=? base (string-append name ".ko.gz"))
                                              (string=? base (string-append name ".ko.zst"))))))))
                     (match candidates
                       ((first . _) first)
                       (() (loop rest)))))
                  (() #f))))

            (define modules
              (let ((found-modules (map lookup-in-dirs modules-to-lookup)))
                (append (filter identity found-modules)
                        (recursive-module-dependencies
                         (filter identity found-modules)
                         #:lookup-module lookup-in-dirs))))

            (define (maybe-uncompress file)
              (cond
               ((string-contains file ".ko.gz")
                (invoke #+(file-append gzip "/bin/gunzip") file))
               ((string-contains file ".ko.zst")
                (invoke #+(file-append zstd "/bin/zstd") "-d" file))))

            (mkdir #$output)
            (for-each (lambda (module)
                        (when module
                          (let ((out-module
                                 (string-append #$output "/"
                                                (basename module))))
                            (format #t "copying '~a'...~%" module)
                            (copy-file module out-module)
                            (maybe-uncompress out-module))))
                      (delete-duplicates modules))

            (write-module-name-database #$output)))))

  (computed-file "linux-modules-combined" build-exp))

(define* (zfs-raw-initrd file-systems
                         #:key
                         (linux linux-libre)
                         (zfs-package zfs)
                         (linux-modules '())
                         (pre-mount #t)
                         (mapped-devices '())
                         (keyboard-layout #f)
                         (helper-packages '())
                         qemu-networking?
                         volatile-root?
                         (on-error 'debug)
                         (monkey-patch? #t)
                         #:allow-other-keys)
  
  (define device-mapping-commands
    (map (lambda (md)
           (let* ((source  (mapped-device-source md))
                  (targets (mapped-device-targets md))
                  (type    (mapped-device-type md))
                  (open    (mapped-device-kind-open type)))
             (apply open source targets
                    (mapped-device-arguments md))))
         mapped-devices))

  (define file-system-scan-commands
    (let ((file-system-types (map file-system-type file-systems)))
      (if (member "btrfs" file-system-types)
          #~((system* (string-append #$btrfs-progs/static "/bin/btrfs")
                      "device" "scan"))
          #~())))

  (define zfs-import-commands
    #~(begin
        (format #t "Epistemia: Importing ZFS pools...~%")
        (false-if-exception 
         (system* (string-append #$zfs-package "/sbin/zpool")
                  "import" "-a" "-N" "-f" "-d" "/dev"))
        (format #t "Epistemia: Imported ZFS pools:~%")
        (false-if-exception
         (system* (string-append #$zfs-package "/sbin/zpool")
                  "status"))
        (sleep 2)
        #t))

  (define kodir
    (flat-linux-module-directory* linux 
				  (list (gexp-input zfs-package "module")) 
				  linux-modules))

  (expression->initrd
   (with-imported-modules (source-module-closure
                           '((gnu build linux-boot)
                             (guix build utils)
                             (guix build bournish)
                             (gnu system file-systems)
                             (gnu build file-systems)))
     #~(begin
         (use-modules (gnu build linux-boot)
                      (gnu system file-systems)
                      ((guix build utils) #:hide (delete))
                      (guix build bournish)
                      (srfi srfi-1)
                      (srfi srfi-13)
                      (srfi srfi-26))
         
         ;; We must patch BOTH modules because linux-boot likely already
         ;; imported the function.
         #$(if monkey-patch?
               #~(let* ((fs-mod   (resolve-module '(gnu build file-systems)))
                        (boot-mod (resolve-module '(gnu build linux-boot)))
                        (orig-canon (module-ref fs-mod 'canonicalize-device-spec))
                        (new-canon
                         (lambda (spec)
                           ;; ZFS datasets in Guix config are either strings or file-system-labels.
                           ;; If it's a label record, the first field is the label string.
                           (let ((device (cond
                                          ((string? spec) spec)
                                          ((and (struct? spec) 
                                                (string? (struct-ref spec 0)))
                                           (struct-ref spec 0))
                                          (else #f))))
                             (if (and device
                                      (or (string-contains device "zroot/")
                                          (string-contains device "zfs")))
                                 (begin
                                   (format #t "Epistemia: ZFS bypass for ~s~%" device)
                                   device)
                                 (orig-canon spec))))))
                   
                   ;; 1. Overwrite the definition source
                   (module-set! fs-mod 'canonicalize-device-spec new-canon)
                   
                   ;; 2. Overwrite the consumer's binding (Crucial for boot-system)
                   (module-set! boot-mod 'canonicalize-device-spec new-canon)
                   
                   (format #t "Epistemia: ZFS Monkey Patch Applied.~%"))
               #~#t)

         (with-output-to-port (%make-void-port "w")
           (lambda ()
             (set-path-environment-variable "PATH" '("bin" "sbin")
                                            '#$helper-packages)))

         (parameterize ((current-warning-port (%make-void-port "w")))
           (boot-system #:mounts
                        (map spec->file-system
                             '#$(map file-system->spec file-systems))
                        #:pre-mount (lambda ()
                                      (and #$pre-mount
                                           #$@device-mapping-commands
                                           #$@file-system-scan-commands
                                           #$zfs-import-commands))
                        #:linux-modules '#$linux-modules
                        #:linux-module-directory '#$kodir
                        #:keymap-file #+(and=> keyboard-layout
                                               keyboard-layout->console-keymap)
                        #:qemu-guest-networking? #$qemu-networking?
                        #:volatile-root? '#$volatile-root?
                        #:on-error '#$on-error))))
   #:name "zfs-initrd"))

(define* (epistemia-zfs-initrd file-systems
                               #:key
                               (linux linux-libre)
                               (zfs-package zfs)
                               (linux-modules '())
                               (mapped-devices '())
                               (keyboard-layout #f)
                               qemu-networking?
                               volatile-root?
                               (extra-modules '())
                               (on-error 'debug)
                               (monkey-patch? #t)
                               #:allow-other-keys)
  
  (define linux-modules*
    (cons "zfs"
          `(,@linux-modules
            ,@(file-system-modules file-systems)
            ,@(if volatile-root? '("overlay") '())
            ,@extra-modules)))

  (define helper-packages
    (cons zfs-package
          (append (file-system-packages file-systems
                                        #:volatile-root? volatile-root?)
                  (if keyboard-layout
                      (list loadkeys-static)
                      '()))))

  (zfs-raw-initrd file-systems
                  #:linux linux
                  #:zfs-package zfs-package
                  #:linux-modules linux-modules*
                  #:mapped-devices mapped-devices
                  #:helper-packages helper-packages
                  #:keyboard-layout keyboard-layout
                  #:qemu-networking? qemu-networking?
                  #:volatile-root? volatile-root?
                  #:on-error on-error
                  #:monkey-patch? monkey-patch?))