~ruther/ruthless-guix

ref: cee190286b47d39d7bb056aab296ebbd1f1fe833 ruthless-guix/modules/ruthless/image.scm -rw-r--r-- 16.3 KiB
cee19028 — Rutherther feat: add test for LVM root 3 days ago
                                                                                
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
;;; Generic disk image builder for ruthless-guix
;;; Copyright © 2025
;;;
;;; This module provides a generic, reusable wrapper for building disk images
;;; that can work with different partition schemes (LVM, RAID, plain, etc.).

(define-module (ruthless image)
  #:use-module (gnu bootloader)
  #:use-module (gnu bootloader grub)
  #:use-module (gnu build marionette)
  #:use-module (gnu packages base)
  #:use-module (gnu packages bootloaders)
  #:use-module (gnu packages cryptsetup)
  #:use-module (gnu packages disk)
  #:use-module (gnu packages file-systems)
  #:use-module (gnu packages firmware)
  #:use-module (gnu packages gnupg)
  #:use-module (gnu packages guile)
  #:use-module (gnu packages linux)
  #:use-module (gnu packages virtualization)
  #:use-module (gnu services)
  #:use-module (gnu services base)
  #:use-module (gnu system)
  #:use-module (gnu system vm)
  #:use-module (gnu tests)
  #:use-module (gnu tests base)
  #:use-module (guix gexp)
  #:use-module (guix modules)
  #:use-module (guix monads)
  #:use-module (guix packages)
  #:use-module (guix records)
  #:use-module (guix store)
  #:use-module (guix scripts system reconfigure)
  #:use-module ((guix self) #:select (make-config.scm))
  #:use-module (srfi srfi-1)
  #:use-module (ice-9 match)
  #:export (<disk-initializer>
            disk-initializer
            disk-initializer?
            disk-initializer-name
            disk-initializer-packages
            disk-initializer-setup-proc
            disk-initializer-cleanup-proc

            build-disk-image

            make-lvm-disk-initializer
            lvm-disk-initializer
            make-simple-gpt-disk-initializer
            simple-gpt-disk-initializer))

;;;
;;; Disk initializer record
;;;

(define-record-type* <disk-initializer> disk-initializer
  make-disk-initializer
  disk-initializer?
  (name          disk-initializer-name)           ; symbol
  (packages      disk-initializer-packages        ; list of packages
                 (default '()))
  (setup-proc    disk-initializer-setup-proc)     ; gexp: sets up disk, mounts to /mnt
  (cleanup-proc  disk-initializer-cleanup-proc))  ; gexp: unmounts, deactivates

;;;
;;; Helper utilities
;;;

;; Guile-Gcrypt, Guile-SQLite3, and their propagated inputs.
(define gcrypt-sqlite3&co
  (append-map
   (lambda (package)
     (cons package
           (match (package-transitive-propagated-inputs package)
             (((labels packages) ...)
              packages))))
   (list guile-gcrypt guile-sqlite3)))

(define neither-config-nor-git?
  ;; Select (guix …) and (gnu …) modules, except (guix config) and (guix git).
  (match-lambda
    (('guix 'config) #f)
    (('guix 'git) #f)
    (('guix rest ...) #t)
    (('gnu rest ...) #t)
    (rest #f)))

(define-syntax-rule (with-imported-modules* gexp* ...)
  "Import necessary modules for image building operations."
  (with-extensions gcrypt-sqlite3&co
    (with-imported-modules `(,@(source-module-closure
                                '((gnu build image)
                                  (gnu build bootloader)
                                  (gnu build hurd-boot)
                                  (gnu build linux-boot)
                                  (guix store database))
                                #:select? neither-config-nor-git?)
                             ((guix config) => ,(make-config.scm)))
      #~(begin
          (use-modules (gnu build image)
                       (gnu build bootloader)
                       (gnu build hurd-boot)
                       (gnu build linux-boot)
                       (guix store database)
                       (guix build utils))
          gexp* ...))))

;;;
;;; Main disk image builder
;;;

(define* (build-disk-image os
                           #:key
                           disk-initializer
                           (disk-size (* 2048 1024 1024))  ; 2 GiB
                           (memory-size 1024)
                           (uefi? #t)
                           (image-format 'qcow2)
                           (name "disk-image"))
  "Return a derivation that builds a disk image with OS installed.

DISK-INITIALIZER is a <disk-initializer> record that defines:
  - Packages needed for disk setup
  - How to partition and format the disk
  - How to mount filesystems to /mnt
  - How to cleanup/unmount after initialization

The procedure handles:
  - Creating a builder VM with necessary packages
  - Creating the target disk
  - Calling the disk initializer's setup procedure
  - Running initialize-root-partition on the mounted filesystem
  - Installing the bootloader
  - Calling the cleanup procedure
  - Converting to the requested image format"

  (define builder-os
    ;; The helper OS that prepares the disk image.
    (marionette-operating-system
     (operating-system
       (inherit %simple-os)
       (packages
        (append (disk-initializer-packages disk-initializer)
                (list parted e2fsprogs dosfstools)
                (operating-system-packages %simple-os))))
     #:imported-modules '((gnu services herd)
                          (guix combinators))))

  (define builder-vm
    (virtual-machine
     (operating-system builder-os)
     (memory-size memory-size)
     (disk-image-size (* 1024 1024 1024))))  ; 1 GiB for builder

  (define bootcfg
    (operating-system-bootcfg os))

  (define bootloader-config
    (operating-system-bootloader os))

  (define bootloader
    (bootloader-configuration-bootloader bootloader-config))

  (define graph "system-graph")

  (define schema
    (local-file (search-path %load-path "guix/store/schema.sql")))

  ;; Computed file that contains the references graph for the OS closure.
  ;; This is needed because program-file doesn't support #:references-graphs.
  (define references-graph
    (computed-file
     "references-graph"
     #~(copy-file #$graph #$output)
     #:options `(#:references-graphs ((,graph ,os)))))

  ;; Program that initializes the root partition.
  ;; This will be loaded inside the VM via primitive-load.
  ;; Note: bootloader installation is handled separately after this step.
  (define initialize-root-program
    (program-file
     "initialize-root.scm"
     (with-imported-modules*
      (parameterize ((sql-schema #$schema))
        (initialize-root-partition
         "/mnt"
         #:references-graphs (list #$references-graph)
         #:copy-closures? #t
         #:register-closures? #t
         #:deduplicate? #f
         #:system-directory #$os
         #:bootcfg #$bootcfg
         #:bootcfg-location #$(bootloader-configuration-file bootloader)
         #:bootloader-package #$(bootloader-package bootloader)
         ;; Bootloader installer is called separately via install-bootloader
         #:bootloader-installer #f)))))

  (define setup-proc (disk-initializer-setup-proc disk-initializer))
  (define cleanup-proc (disk-initializer-cleanup-proc disk-initializer))

  ;; Main build procedure
  (define build
    (with-imported-modules (source-module-closure
                            '((guix build utils)
                              (gnu build marionette)))
      #~(begin
          (use-modules (guix build utils)
                       (gnu build marionette)
                       (ice-9 format))

          (define target-disk
            (string-append #$output ".raw"))

          ;; Create the target disk image
          (invoke #$(file-append qemu-minimal "/bin/qemu-img")
                  "create" "-f" "qcow2" target-disk
                  (number->string #$disk-size))

          ;; Boot the builder VM with UEFI if requested
          (let ((marionette
                 (make-marionette
                  (list #$builder-vm
                        #$@(if uefi?
                               #~("-bios" #$(file-append ovmf-x86-64
                                                         "/share/firmware/ovmf_x64.bin"))
                               #~())
                        "-drive"
                        (format #f "file=~a,if=virtio,cache=writeback,werror=report"
                                target-disk)))))

            ;; Wait for the system to be ready
            (unless (marionette-eval
                     '(begin
                        (use-modules (gnu services herd))
                        (start-service 'term-tty1)
                        #t)
                     marionette)
              (error "Failed to start term-tty1 service in VM"))

            ;; Step 1: Run disk setup (partition, format, mount to /mnt)
            (display "Setting up disk...\n")
            (unless (marionette-eval
                     '(begin
                        #$setup-proc
                        #t)
                     marionette)
              (error "Could not set up disks."))

            ;; Step 2: Initialize root partition (copy store, create structure, etc.)
            (display "Initializing root filesystem...\n")
            (unless (marionette-eval
                     '(begin
                        (primitive-load #$initialize-root-program)
                        #t)
                     marionette)
              (error "Failed to initialize root partition in VM"))

            ;; Step 3: Install bootloader
            (display "Installing bootloader...\n")
            (unless (#$(install-bootloader
                        (lambda (exp)
                          #~(lambda (marionette)
                              (marionette-eval '(begin #$exp #t) marionette)))
                        bootloader-config
                        bootcfg
                        #:target "/mnt")
                     marionette)
              (error "Failed to install bootloader"))

            ;; Step 4: Cleanup (unmount, deactivate LVM, etc.)
            (display "Cleaning up...\n")
            (unless (marionette-eval
                     '(begin
                        #$cleanup-proc
                        #t)
                     marionette)
              (error "Cleanup failed"))

            ;; Shutdown builder VM
            (display "Shutting down builder VM...\n")
            (marionette-eval
             '(system* "/run/current-system/profile/sbin/halt")
             marionette)

            ;; Wait for VM to terminate
            (false-if-exception
             (let loop ()
               (let ((status (waitpid (marionette-pid marionette) WNOHANG)))
                 (when (zero? (car status))
                   (sleep 1)
                   (loop))))))

          ;; Convert to final format or rename
          #$(match image-format
              ('qcow2
               #~(rename-file target-disk #$output))
              ('raw
               #~(begin
                   (invoke #$(file-append qemu-minimal "/bin/qemu-img")
                           "convert" "-f" "qcow2" "-O" "raw"
                           target-disk #$output)
                   (delete-file target-disk)))))))

  (computed-file (string-append name "." (symbol->string image-format))
                 build))

;;;
;;; Pre-built disk initializers
;;;

(define* (make-lvm-disk-initializer #:key
                                    (volume-group "vg0")
                                    (root-volume "root")
                                    (esp-size "259MiB")
                                    (esp-label "ESP")
                                    (root-label "root"))
  "Create a disk-initializer for LVM root with UEFI boot.

The partition layout is:
  - 1-3 MiB: BIOS boot partition (for legacy GRUB)
  - 3-ESP-SIZE: EFI System Partition
  - ESP-SIZE-100%: LVM physical volume containing VOLUME-GROUP/ROOT-VOLUME"
  (disk-initializer
   (name 'lvm-uefi)
   (packages (list lvm2))
   (setup-proc
    #~(begin
        (use-modules (guix build utils))

        ;; Partition: GPT with BIOS boot, ESP, and LVM PV
        (unless (zero?
                 (system* #$(file-append parted "/sbin/parted")
                          "-s" "/dev/vdb"
                          "mklabel" "gpt"
                          "mkpart" "bios_grub" "1MiB" "3MiB"
                          "set" "1" "bios_grub" "on"
                          "mkpart" "ESP" "fat32" "3MiB" #$esp-size
                          "set" "2" "esp" "on"
                          "mkpart" "lvm" #$esp-size "100%"
                          "set" "3" "lvm" "on"))
          (error "Failed to partition disk"))

        ;; Create ESP filesystem
        (unless (zero?
                 (system* #$(file-append dosfstools "/sbin/mkfs.fat")
                          "-F" "32" "-n" #$esp-label "/dev/vdb2"))
          (error "Failed to create ESP filesystem"))

        ;; Setup LVM
        (unless (zero?
                 (system* #$(file-append lvm2 "/sbin/pvcreate") "-ff" "-y" "/dev/vdb3"))
          (error "Failed to create LVM physical volume"))
        (unless (zero?
                 (system* #$(file-append lvm2 "/sbin/vgcreate") #$volume-group "/dev/vdb3"))
          (error "Failed to create LVM volume group"))
        (unless (zero?
                 (system* #$(file-append lvm2 "/sbin/lvcreate")
                          "-l" "100%FREE" "-n" #$root-volume #$volume-group))
          (error "Failed to create LVM root volume"))

        ;; Format root
        (unless (zero?
                 (system* #$(file-append e2fsprogs "/sbin/mkfs.ext4")
                          "-L" #$root-label
                          #$(string-append "/dev/mapper/" volume-group "-" root-volume)))
          (error "Failed to format root filesystem"))

        ;; Mount filesystems
        (mkdir-p "/mnt")
        (unless (zero?
                 (system* "mount"
                          #$(string-append "/dev/mapper/" volume-group "-" root-volume)
                          "/mnt"))
          (error "Failed to mount root filesystem"))
        (mkdir-p "/mnt/boot/efi")
        (unless (zero? (system* "mount" "/dev/vdb2" "/mnt/boot/efi"))
          (error "Failed to mount ESP"))))

   (cleanup-proc
    #~(begin
        (sync)
        (unless (zero? (system* "umount" "/mnt/boot/efi"))
          (error "Failed to unmount ESP"))
        (unless (zero? (system* "umount" "/mnt"))
          (error "Failed to unmount root"))
        (unless (zero? (system* #$(file-append lvm2 "/sbin/vgchange") "-an" #$volume-group))
          (error "Failed to deactivate LVM volume group"))))))

(define lvm-disk-initializer
  (make-lvm-disk-initializer))

(define* (make-simple-gpt-disk-initializer #:key
                                           (esp-size "259MiB")
                                           (esp-label "ESP")
                                           (root-label "root"))
  "Create a disk-initializer for simple GPT with UEFI boot (no LVM).

The partition layout is:
  - 1-3 MiB: BIOS boot partition (for legacy GRUB)
  - 3-ESP-SIZE: EFI System Partition
  - ESP-SIZE-100%: Root partition (ext4)"
  (disk-initializer
   (name 'simple-gpt-uefi)
   (packages '())
   (setup-proc
    #~(begin
        (use-modules (guix build utils))

        ;; Partition: GPT with BIOS boot, ESP, and root
        (unless (zero?
                 (system* #$(file-append parted "/sbin/parted")
                          "-s" "/dev/vdb"
                          "mklabel" "gpt"
                          "mkpart" "bios_grub" "1MiB" "3MiB"
                          "set" "1" "bios_grub" "on"
                          "mkpart" "ESP" "fat32" "3MiB" #$esp-size
                          "set" "2" "esp" "on"
                          "mkpart" "root" "ext4" #$esp-size "100%"))
          (error "Failed to partition disk"))

        ;; Create filesystems
        (unless (zero?
                 (system* #$(file-append dosfstools "/sbin/mkfs.fat")
                          "-F" "32" "-n" #$esp-label "/dev/vdb2"))
          (error "Failed to create ESP filesystem"))
        (unless (zero?
                 (system* #$(file-append e2fsprogs "/sbin/mkfs.ext4")
                          "-L" #$root-label "/dev/vdb3"))
          (error "Failed to format root filesystem"))

        ;; Mount filesystems
        (mkdir-p "/mnt")
        (unless (zero? (system* "mount" "/dev/vdb3" "/mnt"))
          (error "Failed to mount root filesystem"))
        (mkdir-p "/mnt/boot/efi")
        (unless (zero? (system* "mount" "/dev/vdb2" "/mnt/boot/efi"))
          (error "Failed to mount ESP"))))

   (cleanup-proc
    #~(begin
        (sync)
        (unless (zero? (system* "umount" "/mnt/boot/efi"))
          (error "Failed to unmount ESP"))
        (unless (zero? (system* "umount" "/mnt"))
          (error "Failed to unmount root"))))))

(define simple-gpt-disk-initializer
  (make-simple-gpt-disk-initializer))