Subject: [bug#41924] [PATCH] profiles: Make linux-module-database skip inappropriate inputs

From: Ivan Kozlov <[email-masked]>
Date: Fri, 19 Jun 2020 11:20:36 +0300

Hi,

> What I would do is try to find a file that's only in the kernel when there's
> module support (/lib/modules doesn't count since there could be a kernel with
> module support but no modules compiled), and then try to ascertain whether
> CONFIG_MODULES=y that way, so
> (define config-modules? (any (append-map directory-entries with marker-file)))
> or something.

The only way to do this sort of thing is to have the config. Otherwise the kernel will always sort of exist in a separate world. You can always compile it in a way that breaks the rest of the Guix system’s assumptions, e.g. by disabling the necessary system calls or cgroups controllers, and get no warnings/build failures. This applies to any extra kernel modules, too. I don’t think LKM support stands out. I actually don’t think it is a big problem that using an unofficial Linux package puts some burden on the maintainer, I merely want such setups to be possible.

The problem in question really applies to all cases when there aren’t any modules to deal with, as you correctly point out, not necessary having to do with CONFIG_MODULES; e.g. building a system with CONFIG_MODULES=y and no loadable modules, and no extra modules, is perfectly legitimate.

> That would silently skip packages that don't contain kernel modules (but for
> example supertux or something), too, right?
> (so misconfigured guix wouldn't be detected)

It is possible to rely on the way operating-system-directory-base-entries builds the manifest to avoid this specific issue: (car #$(manifest-inputs manifest)) should be the kernel.

I haven’t really looked into the test suite, here is my guess:

diff --git a/gnu/tests/linux-modules.scm b/gnu/tests/linux-modules.scm
index 953b132ef7..2fc8f52b90 100644
--- a/gnu/tests/linux-modules.scm
+++ b/gnu/tests/linux-modules.scm
@@ -25,6 +25,7 @@
   #:use-module (gnu system)
   #:use-module (gnu system vm)
   #:use-module (gnu tests)
+  #:use-module (gnu tests base)
   #:use-module (guix derivations)
   #:use-module (guix gexp)
   #:use-module (guix modules)
@@ -129,3 +130,37 @@ with two extra modules.")
                                                  (package-arguments
                                                   ddcci-driver-linux))))))
            '("acpi_call" "ddcci")))))
+
+(define %test-no-loadable-kernel-modules
+  (let* ((kernel (package
+                   (inherit linux-libre)
+                   (arguments
+                    (substitute-keyword-arguments (package-arguments linux-libre)
+                      ((#:phases phases)
+                       `(modify-phases ,phases
+                          (add-before 'build 'no-lkm-config
+                            (lambda _
+                              (substitute* ".config"
+                                (("=m") "=y"))
+                              (let ((port (open-file ".config" "a")))
+                                (display "CONFIG_MODULES=n" port)
+                                (close-port port))))
+                          (replace 'install
+                            (lambda* (#:key outputs #:allow-other-keys)
+                              (for-each
+                               (let ((out (assoc-ref outputs "out")))
+                                 (lambda (file) (install-file file out)))
+                               (find-files "." "^(\\.config|bzImage|zImage|Image|vmlinuz|System\\.map|Module\\.symvers)$"))))))))))
+         (os (marionette-operating-system
+              (operating-system
+                (inherit (simple-operating-system))
+                (kernel kernel)
+                (initrd-modules '()))
+              #:imported-modules '((gnu services herd)
+                                   (guix combinators))))
+         (vm (virtual-machine os)))
+    (system-test
+     (name "no-loadable-kernel-modules")
+     (description "Build and run a basic system without loadable kernel module support")
+     (value (run-basic-test (virtualized-operating-system os '())
+                            #~(list #$vm))))))
diff --git a/guix/profiles.scm b/guix/profiles.scm
index 25ff146bdf..abf0cf7f27 100644
--- a/guix/profiles.scm
+++ b/guix/profiles.scm
@@ -1201,6 +1201,8 @@ for both major versions of GTK+."
   "Return a derivation that unites all the kernel modules of the manifest
 and creates the dependency graph of all these kernel modules.

+The first entry in the manifest must be a Linux kernel package.
+
 This is meant to be used as a profile hook."
   (define kmod  ; lazy reference
     (module-ref (resolve-interface '(gnu packages linux)) 'kmod))
@@ -1226,14 +1228,22 @@ This is meant to be used as a profile hook."
                  ;; Note: Should usually result in one entry.
                  (versions (delete-duplicates
                             (append-map directory-entries
-                                        module-directories))))
-              (match versions
-               ((version)
-                (let ((old-path (getenv "PATH")))
-                  (setenv "PATH" #+(file-append kmod "/bin"))
-                  (make-linux-module-directory inputs version #$output)
-                  (setenv "PATH" old-path)))
-               (_ (error "Specified Linux kernel and Linux kernel modules
+                                        (if (file-exists? (car module-directories))
+                                            module-directories
+                                            (cdr module-directories))))))
+            (match versions
+              ((version)
+               (let ((old-path (getenv "PATH")))
+                 (setenv "PATH" #+(file-append kmod "/bin"))
+                 (make-linux-module-directory inputs version #$output)
+                 (setenv "PATH" old-path)))
+              ;; Do nothing when there is nothing to do
+              (() (mkdir #$output))
+              ;; This might not catch a version incompatibility
+              ;; if all kernel modules reside in extra packages
+              ;; Would checking the kernel package version have
+              ;; undesirable effects?
+              (_ (error "Specified Linux kernel and Linux kernel modules
 are not all of the same version")))))))
   (gexp->derivation "linux-module-database" build
                     #:local-build? #t