guixsd-config

GuixSD configs for my systems
Log | Files | Refs

commit dfb5abee7e50f008c79ee76c752b752837e42b23
parent ab121cf5645f5a6bd7639cc95fe13abfa2dcec2a
Author: vin <git@vineetk.net>
Date:   Mon, 22 Dec 2025 12:33:48 -0500

work on zfs grub config and initrd

Diffstat:
Aepistemia/bootloader.scm | 24++++++++++++++++++++++++
Mepistemia/systems/demiurge.scm | 60++++++++++++++++++++++++++++++++----------------------------
Mepistemia/systems/linux-initrd.scm | 104+++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
3 files changed, 120 insertions(+), 68 deletions(-)

diff --git a/epistemia/bootloader.scm b/epistemia/bootloader.scm @@ -0,0 +1,24 @@ +;;; SPDX-License-Identifier: GPL-3.0-or-later +(define-module (epistemia bootloader) + #:use-module (guix gexp) + #:use-module (guix utils) + #:use-module (gnu bootloader) + #:use-module (gnu bootloader grub) + #:export (grub-zfs)) + +(define grub-zfs + (bootloader + (inherit grub-efi-bootloader) + (name 'grub-zfs) + (installer + #~(lambda (bootloader targets mount-point) + (use-modules (guix build utils)) + (let ((standard-installer #$(bootloader-installer grub-efi-bootloader))) + (standard-installer bootloader targets mount-point)) + (let ((grub-cfg (string-append mount-point "/boot/grub/grub.cfg"))) + (chmod grub-cfg #o644) + (substitute* grub-cfg + (("search --label --set [^ ]+") + "set root=(hd0,gpt2)") + ((" /store/") + " /gnu/@/store/"))))))) diff --git a/epistemia/systems/demiurge.scm b/epistemia/systems/demiurge.scm @@ -1,5 +1,7 @@ (define-module (epistemia systems demiurge) #:use-module (gnu) + #:use-module (gnu bootloader) + #:use-module (gnu bootloader grub) #:use-module (gnu packages audio) #:use-module (gnu packages containers) #:use-module (gnu services containers) @@ -9,11 +11,13 @@ #:use-module (gnu services shepherd) #:use-module (gnu system accounts) #:use-module (guix) + #:use-module (guix gexp) #:use-module (guix utils) #:use-module (nongnu packages linux) - #:use-module (nongnu system linux-initrd) ; for microcode + #:use-module (nongnu system linux-initrd) + #:use-module (epistemia bootloader) #:use-module (epistemia systems base-system) - #:use-module (epistemia systems linux-initrd) ; for zfs-on-root + #:use-module (epistemia systems linux-initrd) #:use-module (epistemia services zfs) #:use-module (epistemia packages zfs)) @@ -24,7 +28,7 @@ (inherit base-system) (host-name "demiurge") - (kernel linux-6.16) + (kernel linux-6.17) (kernel-arguments (list "net.ifnames=0")) (kernel-loadable-modules (list (list zfs-linux "module"))) @@ -36,24 +40,24 @@ (firmware (cons* amdgpu-firmware %base-firmware)) (bootloader (bootloader-configuration - (bootloader grub-efi-bootloader) + (bootloader grub-zfs) (targets '("/boot/efi")))) (file-systems (append (list (file-system - (device (file-system-label "zroot/root_crux")) + (device "zroot/root") (mount-point "/") (type "zfs") (check? #f) (needed-for-boot? #t)) (file-system - (device (file-system-label "zroot/gnu")) + (device "zroot/gnu") (mount-point "/gnu") (type "zfs") (check? #f) (needed-for-boot? #t)) (file-system - (device (uuid "EC1D-4A47" 'fat)) + (device (uuid "3EF4-E408" 'fat)) (mount-point "/boot/efi") (type "vfat"))) %base-file-systems)) @@ -62,33 +66,33 @@ %base-packages)) (services (append (list (simple-service 'zfs-loader - kernel-module-loader-service-type - '("zfs")) + kernel-module-loader-service-type + '("zfs")) (simple-service 'zfs-shepherd-services - shepherd-root-service-type - zfs-shepherd-services) + shepherd-root-service-type + zfs-shepherd-services) (simple-service 'zfs-shepherd-services-user-processes - user-processes-service-type - '(zfs-automount)) + user-processes-service-type + '(zfs-automount)) (service seatd-service-type) (service dbus-root-service-type) (service iptables-service-type) (service rootless-podman-service-type - (rootless-podman-configuration - (subgids - (list (subid-range (name "vin")))) - (subuids - (list (subid-range (name "vin")))))) + (rootless-podman-configuration + (subgids + (list (subid-range (name "vin")))) + (subuids + (list (subid-range (name "vin")))))) (service static-networking-service-type - (list (static-networking - (addresses - (list (network-address - (device "eth0") - (value "192.168.1.2/24")))) - (routes - (list (network-route - (destination "default") - (gateway "192.168.1.1")))) - (name-servers '("192.168.1.1"))))) + (list (static-networking + (addresses + (list (network-address + (device "eth0") + (value "192.168.1.2/24")))) + (routes + (list (network-route + (destination "default") + (gateway "192.168.1.1")))) + (name-servers '("192.168.1.1"))))) (service openssh-service-type)) %base-services))) diff --git a/epistemia/systems/linux-initrd.scm b/epistemia/systems/linux-initrd.scm @@ -1,5 +1,4 @@ ;;; SPDX-License-Identifier: GPL-3.0-or-later -;;; Adapted from GNU Guix gnu/system/linux-initrd.scm and Hako's Rosenthal zfs-on-root patches (define-module (epistemia systems linux-initrd) #:use-module (guix gexp) #:use-module (guix utils) @@ -14,7 +13,7 @@ #:use-module (gnu system file-systems) #:use-module (gnu system mapped-devices) #:use-module (gnu system keyboard) - #:use-module (gnu system linux-initrd) ;; upstream module imported for reusing helpers + #:use-module (gnu system linux-initrd) #:use-module (ice-9 match) #:use-module (ice-9 regex) #:use-module (ice-9 vlist) @@ -22,14 +21,9 @@ #:use-module (srfi srfi-26) #:export (epistemia-zfs-initrd)) -;;; -;;; Custom Module Builder (Solves the "Out-of-Tree" problem) -;;; - (define (flat-linux-module-directory* linux extra-packages modules) "Return a flat directory containing the Linux kernel modules listed in -MODULES. Unlike the upstream version, this searches both LINUX and EXTRA-PACKAGES -for the requested modules." +MODULES." (define imported-modules (source-module-closure '((gnu build linux-modules) (guix build utils)))) @@ -45,12 +39,10 @@ for the requested modules." (srfi srfi-26) (ice-9 match)) - ;; Define search paths: Kernel + Extra Packages (ZFS) (define module-dirs (map (lambda (pkg) (string-append pkg "/lib/modules")) (cons #$linux '#$extra-packages))) - ;; Helper to find the builtin modules in the main kernel (define builtin-modules (match (find-files (string-append #$linux "/lib/modules") (lambda (file stat) @@ -65,20 +57,27 @@ for the requested modules." (define modules-to-lookup (lset-difference string=? '#$modules builtin-modules)) - ;; Custom lookup function that iterates over all module-dirs (define (lookup-in-dirs name) (let loop ((dirs module-dirs)) (match dirs ((dir . rest) - (or (find-module-file dir name) - (loop 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 ((modules (map lookup-in-dirs modules-to-lookup))) - (append modules + (let ((found-modules (map lookup-in-dirs modules-to-lookup))) + (append (filter identity found-modules) (recursive-module-dependencies - modules + (filter identity found-modules) #:lookup-module lookup-in-dirs)))) (define (maybe-uncompress file) @@ -90,7 +89,7 @@ for the requested modules." (mkdir #$output) (for-each (lambda (module) - (when module ;; check if module was found + (when module (let ((out-module (string-append #$output "/" (basename module)))) @@ -103,14 +102,10 @@ for the requested modules." (computed-file "linux-modules-combined" build-exp)) -;;; -;;; Custom Initrd Builder (Solves the "Import Pool" problem) -;;; - (define* (zfs-raw-initrd file-systems #:key (linux linux-libre) - (zfs-package zfs) ;; New argument + (zfs-package zfs) (linux-modules '()) (pre-mount #t) (mapped-devices '()) @@ -118,8 +113,9 @@ for the requested modules." (helper-packages '()) qemu-networking? volatile-root? - (on-error 'debug)) - "A wrapper around expression->initrd that injects ZFS support." + (on-error 'debug) + (monkey-patch? #t) + #:allow-other-keys) (define device-mapping-commands (map (lambda (md) @@ -138,22 +134,23 @@ for the requested modules." "device" "scan")) #~()))) - ;; Custom ZFS Import Logic (define zfs-import-commands #~(begin (format #t "Epistemia: Importing ZFS pools...~%") - ;; We use the zpool binary from the provided package. - ;; -N: Do not mount (Guix handles mounting) - ;; -f: Force import (if previously not exported cleanly) - ;; -d: Look in /dev/disk/by-id for stable names - (system* (string-append #$zfs-package "/sbin/zpool") - "import" "-a" "-N" "-f" "-d" "/dev/disk/by-id") - ;; Sleep briefly to allow device nodes (like /dev/zvol/...) to settle - (sleep 2))) - - ;; Use our custom module directory builder + (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 zfs-package) linux-modules)) + (flat-linux-module-directory* linux + (list (gexp-input zfs-package "module")) + linux-modules)) (expression->initrd (with-imported-modules (source-module-closure @@ -168,7 +165,34 @@ for the requested modules." ((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) + (format #t "Epistemia: Checking spec: ~s~%" spec) + (if (and (string? spec) + (or (string-contains spec "zroot/") + (string-contains spec "zfs"))) + (begin + (format #t "Epistemia: ZFS bypass for ~s~%" spec) + spec) ;; Return string immediately + (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 () @@ -204,11 +228,10 @@ for the requested modules." volatile-root? (extra-modules '()) (on-error 'debug) - #:allow-other-keys) - "Return a ZFS-capable initrd." + (monkey-patch? #t) + #:allow-other-keys) (define linux-modules* - ;; Ensure ZFS is added to the module list (cons "zfs" `(,@linux-modules ,@(file-system-modules file-systems) @@ -232,4 +255,5 @@ for the requested modules." #:keyboard-layout keyboard-layout #:qemu-networking? qemu-networking? #:volatile-root? volatile-root? - #:on-error on-error)) + #:on-error on-error + #:monkey-patch? monkey-patch?))