1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2016, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu build linux-modules)
21 #:use-module (guix elf)
22 #:use-module (guix glob)
23 #:use-module (guix build syscalls)
24 #:use-module ((guix build utils) #:select (find-files))
25 #:use-module (rnrs io ports)
26 #:use-module (rnrs bytevectors)
27 #:use-module (srfi srfi-1)
28 #:use-module (srfi srfi-11)
29 #:use-module (srfi srfi-26)
30 #:use-module (ice-9 vlist)
31 #:use-module (ice-9 match)
32 #:use-module (ice-9 rdelim)
37 module-soft-dependencies
39 file-name->module-name
41 recursive-module-dependencies
46 current-module-debugging-port
55 ;;; Tools to deal with Linux kernel modules.
59 (define current-module-debugging-port
60 (make-parameter (%make-void-port "w")))
62 (define (section-contents elf section)
63 "Return the contents of SECTION in ELF as a bytevector."
64 (let ((contents (make-bytevector (elf-section-size section))))
65 (bytevector-copy! (elf-bytes elf) (elf-section-offset section)
67 (elf-section-size section))
71 (char-set-complement (char-set #\nul)))
73 (define (nul-separated-string->list str)
74 "Split STR at occurrences of the NUL character and return the resulting
76 (string-tokenize str %not-nul))
78 (define (key=value->pair str)
79 "Assuming STR has the form \"KEY=VALUE\", return a pair like (KEY
81 (let ((= (string-index str #\=)))
82 (cons (string->symbol (string-take str =))
83 (string-drop str (+ 1 =)))))
85 (define (modinfo-section-contents file)
86 "Return the contents of the '.modinfo' section of FILE as a list of
88 (let* ((bv (call-with-input-file file get-bytevector-all))
90 (section (elf-section-by-name elf ".modinfo"))
91 (modinfo (section-contents elf section)))
93 (nul-separated-string->list (utf8->string modinfo)))))
96 (char-set-complement (char-set #\,)))
98 (define (module-dependencies file)
99 "Return the list of modules that FILE depends on. The returned list
100 contains module names, not actual file names."
101 (let ((info (modinfo-section-contents file)))
102 (match (assq 'depends info)
104 (string-tokenize what %not-comma)))))
106 (define not-softdep-whitespace
107 (char-set-complement (char-set #\space #\tab)))
109 (define (module-soft-dependencies file)
110 "Return the list of modules that can be preloaded, and then the list of
111 modules that can be postloaded, of the soft dependencies of module FILE."
112 ;; TEXT: "pre: baz blubb foo post: bax bar"
113 (define (parse-softdep text)
114 (let loop ((value '())
115 (tokens (string-tokenize text not-softdep-whitespace))
119 (if (string=? (string-take-right token 1) ":") ; section
120 (loop value rest (string-trim-both (string-drop-right token 1)))
121 (loop (cons (cons section token) value) rest section)))
125 ;; Note: Multiple 'softdep sections are allowed.
126 (let* ((info (modinfo-section-contents file))
127 (entries (concatenate
128 (filter-map (match-lambda
130 (parse-softdep value))
132 (modinfo-section-contents file)))))
133 (let-values (((pres posts)
134 (partition (match-lambda
138 (values (map (match-lambda
145 (define (module-aliases file)
146 "Return the list of aliases of module FILE."
147 (let ((info (modinfo-section-contents file)))
148 (filter-map (match-lambda
152 (modinfo-section-contents file))))
155 (cut string-append <> ".ko"))
157 (define (ensure-dot-ko name)
158 "Return NAME with a '.ko' prefix appended, unless it already has it."
159 (if (string-suffix? ".ko" name)
163 (define (normalize-module-name module)
164 "Return the \"canonical\" name for MODULE, replacing hyphens with
166 ;; See 'modname_normalize' in libkmod.
167 (string-map (lambda (chr)
173 (define (file-name->module-name file)
174 "Return the module name corresponding to FILE, stripping the trailing '.ko'
176 (normalize-module-name (basename file ".ko")))
178 (define (find-module-file directory module)
179 "Lookup module NAME under DIRECTORY, and return its absolute file name.
180 NAME can be a file name with or without '.ko', or it can be a module name.
181 Raise an error if it could not be found.
183 Module names can differ from file names in interesting ways; for instance,
184 module names usually (always?) use underscores as the inter-word separator,
185 whereas file names often, but not always, use hyphens. Examples:
186 \"usb-storage.ko\", \"serpent_generic.ko\"."
188 ;; List of possible file names. XXX: It would of course be cleaner to
189 ;; have a database that maps module names to file names and vice versa,
190 ;; but everyone seems to be doing hacks like this one. Oh well!
194 (normalize-module-name module)
195 (string-map (lambda (chr) ;converse of 'normalize-module-name'
201 (match (find-files directory
203 (member (basename file) names)))
207 (error "kernel module not found" module directory))
209 (error "several modules by that name" module directory))))
211 (define* (recursive-module-dependencies files
212 #:key (lookup-module dot-ko))
213 "Return the topologically-sorted list of file names of the modules depended
214 on by FILES, recursively. File names of modules are determined by applying
215 LOOKUP-MODULE to the module name."
216 (let loop ((files files)
218 (visited vlist-null))
221 (delete-duplicates (reverse result)))
223 (let* ((visited? (vhash-assoc head visited))
226 (map lookup-module (module-dependencies head))))
227 (visited (if visited?
229 (vhash-cons head #t visited))))
230 (loop (append deps tail)
231 (append result deps) visited))))))
234 (char-set-complement (char-set #\newline)))
236 (define (modules-loaded)
237 "Return the list of names of currently loaded Linux modules."
238 (let* ((contents (call-with-input-file "/proc/modules"
240 (lines (string-tokenize contents %not-newline)))
241 (match (map string-tokenize lines)
245 (define (module-black-list)
246 "Return the black list of modules that must not be loaded. This black list
247 is specified using 'modprobe.blacklist=MODULE1,MODULE2,...' on the kernel
248 command line; it is honored by libkmod for users that pass
249 'KMOD_PROBE_APPLY_BLACKLIST', which includes 'modprobe --use-blacklist' and
252 "modprobe.blacklist=")
254 (let ((command (call-with-input-file "/proc/cmdline"
256 (append-map (lambda (arg)
257 (if (string-prefix? parameter arg)
258 (string-tokenize (string-drop arg (string-length parameter))
261 (string-tokenize command))))
263 (define (module-loaded? module)
264 "Return #t if MODULE is already loaded. MODULE must be a Linux module name,
266 (member module (modules-loaded)))
268 (define* (load-linux-module* file
271 (lookup-module dot-ko)
272 (black-list (module-black-list)))
273 "Load Linux module from FILE, the name of a '.ko' file; return true on
274 success, false otherwise. When RECURSIVE? is true, load its dependencies
275 first (à la 'modprobe'.) The actual files containing modules depended on are
276 obtained by calling LOOKUP-MODULE with the module name. Modules whose name
277 appears in BLACK-LIST are not loaded."
278 (define (black-listed? module)
279 (let ((result (member module black-list)))
281 (format (current-module-debugging-port)
282 "not loading module '~a' because it's black-listed~%"
286 (define (load-dependencies file)
287 (let ((dependencies (module-dependencies file)))
288 (every (cut load-linux-module* <>
289 #:lookup-module lookup-module
290 #:black-list black-list)
291 (map lookup-module dependencies))))
293 (and (not (black-listed? (file-name->module-name file)))
295 (load-dependencies file))
297 (format (current-module-debugging-port)
298 "loading Linux module from '~a'...~%" file)
302 (set! fd (open-fdes file O_RDONLY))
303 (load-linux-module/fd fd)
307 ;; If this module was already loaded and we're in modprobe style, ignore
309 (when fd (close-fdes fd))
310 (or (and recursive? (= EEXIST (system-error-errno args)))
311 (apply throw args)))))))
318 ;; Copied from (guix utils). FIXME: Factorize.
319 (define (readlink* file)
320 "Call 'readlink' until the result is not a symlink."
321 (define %max-symlink-depth 50)
323 (let loop ((file file)
325 (define (absolute target)
326 (if (absolute-file-name? target)
328 (string-append (dirname file) "/" target)))
330 (if (>= depth %max-symlink-depth)
336 (values #t (readlink file)))
338 (let ((errno (system-error-errno args)))
339 (if (or (= errno EINVAL))
341 (apply throw args))))))
342 (lambda (success? target)
344 (loop (absolute target) (+ depth 1))
347 ;; See 'major' and 'minor' in <sys/sysmacros.h>.
349 (define (stat->device-major st)
350 (ash (logand #xfff00 (stat:rdev st)) -8))
352 (define (stat->device-minor st)
353 (logand #xff (stat:rdev st)))
356 (char-set-complement (char-set #\/)))
358 (define (read-uevent port)
359 "Read a /sys 'uevent' file from PORT and return an alist where each car is a
360 key such as 'MAJOR or 'DEVTYPE and each cdr is the corresponding value."
361 (let loop ((result '()))
362 (match (read-line port)
366 (loop (cons (key=value->pair line) result))))))
368 (define (device-module-aliases device)
369 "Return the list of module aliases required by DEVICE, a /dev file name, as
372 (device-module-aliases \"/dev/sda\")
373 => (\"scsi:t-0x00\" \"pci:v00008086d00009D03sv0000103Csd000080FAbc01sc06i01\")
375 The modules corresponding to these aliases can then be found using
377 ;; The approach is adapted from
378 ;; <https://unix.stackexchange.com/questions/97676/how-to-find-the-driver-module-associated-with-a-device-on-linux>.
379 (let* ((st (stat device))
380 (type (stat:type st))
381 (major (stat->device-major st))
382 (minor (stat->device-minor st))
383 (sys-name (string-append "/sys/dev/"
385 ((block-special) "block")
386 ((char-special) "char")
387 (else (symbol->string type)))
388 "/" (number->string major) ":"
389 (number->string minor)))
390 (directory (canonicalize-path (readlink* sys-name))))
391 (let loop ((components (string-tokenize directory %not-slash))
397 (let ((uevent (string-append (string-join components "/" 'prefix)
399 (if (file-exists? uevent)
400 (let ((props (call-with-input-file uevent read-uevent)))
401 (match (assq-ref props 'MODALIAS)
402 (#f (loop head aliases))
403 (alias (loop head (cons alias aliases)))))
404 (loop head aliases))))))))
406 (define (read-module-aliases port)
407 "Read from PORT data in the Linux 'modules.alias' file format. Return a
408 list of alias/module pairs where each alias is a glob pattern as like the
411 (string->compiled-sglob \"scsi:t-0x01*\")
413 and each module is a module name like \"snd_hda_intel\"."
414 (define (comment? str)
415 (string-prefix? "#" str))
417 (define (tokenize str)
418 ;; Lines have the form "alias ALIAS MODULE", where ALIAS can contain
419 ;; whitespace. This is why we don't use 'string-tokenize'.
420 (let* ((str (string-trim-both str))
421 (left (string-index str #\space))
422 (right (string-rindex str #\space)))
423 (list (string-take str left)
424 (string-trim-both (substring str left right))
425 (string-trim-both (string-drop str right)))))
427 (let loop ((aliases '()))
428 (match (read-line port)
434 (match (tokenize line)
435 (("alias" alias module)
436 (loop (alist-cons (string->compiled-sglob alias) module
441 (define (current-kernel-directory)
442 "Return the directory of the currently running Linux kernel."
443 (string-append (or (getenv "LINUX_MODULE_DIRECTORY")
444 "/run/booted-system/kernel/lib/modules")
445 "/" (utsname:release (uname))))
447 (define (current-alias-file)
448 "Return the absolute file name of the default 'modules.alias' file."
449 (string-append (current-kernel-directory) "/modules.alias"))
451 (define* (known-module-aliases #:optional (alias-file (current-alias-file)))
452 "Return the list of alias/module pairs read from ALIAS-FILE. Each alias is
454 (call-with-input-file alias-file read-module-aliases))
456 (define* (matching-modules alias
457 #:optional (known-aliases (known-module-aliases)))
458 "Return the list of modules that match ALIAS according to KNOWN-ALIASES.
459 ALIAS is a string like \"scsi:t-0x00\" as returned by
460 'device-module-aliases'."
461 (filter-map (match-lambda
463 (and (glob-match? pattern alias)
467 (define* (missing-modules device modules-provided)
468 "Assuming MODULES-PROVIDED lists kernel modules that are already
469 provided--e.g., in the initrd, return the list of missing kernel modules that
470 are required to access DEVICE."
472 ;; Attempt to load 'modules.alias' from the current kernel, assuming we're
473 ;; on Guix System, and assuming that corresponds to the kernel we'll be
475 (known-module-aliases))
478 (let* ((modules (delete-duplicates
479 (append-map (cut matching-modules <> aliases)
480 (device-module-aliases device))))
482 ;; Module names (not file names) are supposed to use underscores
483 ;; instead of hyphens. MODULES is a list of module names, whereas
484 ;; LINUX-MODULES is file names without '.ko', so normalize them.
485 (provided (map file-name->module-name modules-provided)))
486 (remove (cut member <> provided) modules))
489 ;;; linux-modules.scm ends here