1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2016, 2018 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-26)
29 #:use-module (ice-9 vlist)
30 #:use-module (ice-9 match)
31 #:use-module (ice-9 rdelim)
37 file-name->module-name
39 recursive-module-dependencies
44 current-module-debugging-port
52 ;;; Tools to deal with Linux kernel modules.
56 (define current-module-debugging-port
57 (make-parameter (%make-void-port "w")))
59 (define (section-contents elf section)
60 "Return the contents of SECTION in ELF as a bytevector."
61 (let* ((modinfo (elf-section-by-name elf ".modinfo"))
62 (contents (make-bytevector (elf-section-size modinfo))))
63 (bytevector-copy! (elf-bytes elf) (elf-section-offset modinfo)
65 (elf-section-size modinfo))
69 (char-set-complement (char-set #\nul)))
71 (define (nul-separated-string->list str)
72 "Split STR at occurrences of the NUL character and return the resulting
74 (string-tokenize str %not-nul))
76 (define (key=value->pair str)
77 "Assuming STR has the form \"KEY=VALUE\", return a pair like (KEY
79 (let ((= (string-index str #\=)))
80 (cons (string->symbol (string-take str =))
81 (string-drop str (+ 1 =)))))
83 (define (modinfo-section-contents file)
84 "Return the contents of the '.modinfo' section of FILE as a list of
86 (let* ((bv (call-with-input-file file get-bytevector-all))
88 (modinfo (section-contents elf ".modinfo")))
90 (nul-separated-string->list (utf8->string modinfo)))))
93 (char-set-complement (char-set #\,)))
95 (define (module-dependencies file)
96 "Return the list of modules that FILE depends on. The returned list
97 contains module names, not actual file names."
98 (let ((info (modinfo-section-contents file)))
99 (match (assq 'depends info)
101 (string-tokenize what %not-comma)))))
103 (define (module-aliases file)
104 "Return the list of aliases of module FILE."
105 (let ((info (modinfo-section-contents file)))
106 (filter-map (match-lambda
110 (modinfo-section-contents file))))
113 (cut string-append <> ".ko"))
115 (define (ensure-dot-ko name)
116 "Return NAME with a '.ko' prefix appended, unless it already has it."
117 (if (string-suffix? ".ko" name)
121 (define (normalize-module-name module)
122 "Return the \"canonical\" name for MODULE, replacing hyphens with
124 ;; See 'modname_normalize' in libkmod.
125 (string-map (lambda (chr)
131 (define (file-name->module-name file)
132 "Return the module name corresponding to FILE, stripping the trailing '.ko'
134 (normalize-module-name (basename file ".ko")))
136 (define (find-module-file directory module)
137 "Lookup module NAME under DIRECTORY, and return its absolute file name.
138 NAME can be a file name with or without '.ko', or it can be a module name.
139 Return #f if it could not be found.
141 Module names can differ from file names in interesting ways; for instance,
142 module names usually (always?) use underscores as the inter-word separator,
143 whereas file names often, but not always, use hyphens. Examples:
144 \"usb-storage.ko\", \"serpent_generic.ko\"."
146 ;; List of possible file names. XXX: It would of course be cleaner to
147 ;; have a database that maps module names to file names and vice versa,
148 ;; but everyone seems to be doing hacks like this one. Oh well!
152 (normalize-module-name module)
153 (string-map (lambda (chr) ;converse of 'normalize-module-name'
159 (match (find-files directory
161 (member (basename file) names)))
167 (error "several modules by that name" module directory))))
169 (define* (recursive-module-dependencies files
170 #:key (lookup-module dot-ko))
171 "Return the topologically-sorted list of file names of the modules depended
172 on by FILES, recursively. File names of modules are determined by applying
173 LOOKUP-MODULE to the module name."
174 (let loop ((files files)
176 (visited vlist-null))
179 (delete-duplicates (reverse result)))
181 (let* ((visited? (vhash-assoc head visited))
184 (map lookup-module (module-dependencies head))))
185 (visited (if visited?
187 (vhash-cons head #t visited))))
188 (loop (append deps tail)
189 (append result deps) visited))))))
192 (char-set-complement (char-set #\newline)))
194 (define (modules-loaded)
195 "Return the list of names of currently loaded Linux modules."
196 (let* ((contents (call-with-input-file "/proc/modules"
198 (lines (string-tokenize contents %not-newline)))
199 (match (map string-tokenize lines)
203 (define (module-black-list)
204 "Return the black list of modules that must not be loaded. This black list
205 is specified using 'modprobe.blacklist=MODULE1,MODULE2,...' on the kernel
206 command line; it is honored by libkmod for users that pass
207 'KMOD_PROBE_APPLY_BLACKLIST', which includes 'modprobe --use-blacklist' and
210 "modprobe.blacklist=")
212 (let ((command (call-with-input-file "/proc/cmdline"
214 (append-map (lambda (arg)
215 (if (string-prefix? parameter arg)
216 (string-tokenize (string-drop arg (string-length parameter))
219 (string-tokenize command))))
221 (define (module-loaded? module)
222 "Return #t if MODULE is already loaded. MODULE must be a Linux module name,
224 (member module (modules-loaded)))
226 (define* (load-linux-module* file
229 (lookup-module dot-ko)
230 (black-list (module-black-list)))
231 "Load Linux module from FILE, the name of a '.ko' file; return true on
232 success, false otherwise. When RECURSIVE? is true, load its dependencies
233 first (à la 'modprobe'.) The actual files containing modules depended on are
234 obtained by calling LOOKUP-MODULE with the module name. Modules whose name
235 appears in BLACK-LIST are not loaded."
236 (define (black-listed? module)
237 (let ((result (member module black-list)))
239 (format (current-module-debugging-port)
240 "not loading module '~a' because it's black-listed~%"
244 (define (load-dependencies file)
245 (let ((dependencies (module-dependencies file)))
246 (every (cut load-linux-module* <>
247 #:lookup-module lookup-module
248 #:black-list black-list)
249 (map lookup-module dependencies))))
251 (and (not (black-listed? (file-name->module-name file)))
253 (load-dependencies file))
255 (format (current-module-debugging-port)
256 "loading Linux module from '~a'...~%" file)
260 (set! fd (open-fdes file O_RDONLY))
261 (load-linux-module/fd fd)
265 ;; If this module was already loaded and we're in modprobe style, ignore
267 (when fd (close-fdes fd))
268 (or (and recursive? (= EEXIST (system-error-errno args)))
269 (apply throw args)))))))
276 ;; Copied from (guix utils). FIXME: Factorize.
277 (define (readlink* file)
278 "Call 'readlink' until the result is not a symlink."
279 (define %max-symlink-depth 50)
281 (let loop ((file file)
283 (define (absolute target)
284 (if (absolute-file-name? target)
286 (string-append (dirname file) "/" target)))
288 (if (>= depth %max-symlink-depth)
294 (values #t (readlink file)))
296 (let ((errno (system-error-errno args)))
297 (if (or (= errno EINVAL))
299 (apply throw args))))))
300 (lambda (success? target)
302 (loop (absolute target) (+ depth 1))
305 ;; See 'major' and 'minor' in <sys/sysmacros.h>.
307 (define (stat->device-major st)
308 (ash (logand #xfff00 (stat:rdev st)) -8))
310 (define (stat->device-minor st)
311 (logand #xff (stat:rdev st)))
314 (char-set-complement (char-set #\/)))
316 (define (read-uevent port)
317 "Read a /sys 'uevent' file from PORT and return an alist where each car is a
318 key such as 'MAJOR or 'DEVTYPE and each cdr is the corresponding value."
319 (let loop ((result '()))
320 (match (read-line port)
324 (loop (cons (key=value->pair line) result))))))
326 (define (device-module-aliases device)
327 "Return the list of module aliases required by DEVICE, a /dev file name, as
330 (device-module-aliases \"/dev/sda\")
331 => (\"scsi:t-0x00\" \"pci:v00008086d00009D03sv0000103Csd000080FAbc01sc06i01\")
333 The modules corresponding to these aliases can then be found using
335 ;; The approach is adapted from
336 ;; <https://unix.stackexchange.com/questions/97676/how-to-find-the-driver-module-associated-with-a-device-on-linux>.
337 (let* ((st (stat device))
338 (type (stat:type st))
339 (major (stat->device-major st))
340 (minor (stat->device-minor st))
341 (sys-name (string-append "/sys/dev/"
343 ((block-special) "block")
344 ((char-special) "char")
345 (else (symbol->string type)))
346 "/" (number->string major) ":"
347 (number->string minor)))
348 (directory (canonicalize-path (readlink* sys-name))))
349 (let loop ((components (string-tokenize directory %not-slash))
355 (let ((uevent (string-append (string-join components "/" 'prefix)
357 (if (file-exists? uevent)
358 (let ((props (call-with-input-file uevent read-uevent)))
359 (match (assq-ref props 'MODALIAS)
360 (#f (loop head aliases))
361 (alias (loop head (cons alias aliases)))))
362 (loop head aliases))))))))
364 (define (read-module-aliases port)
365 "Read from PORT data in the Linux 'modules.alias' file format. Return a
366 list of alias/module pairs where each alias is a glob pattern as like the
369 (string->compiled-sglob \"scsi:t-0x01*\")
371 and each module is a module name like \"snd_hda_intel\"."
372 (define (comment? str)
373 (string-prefix? "#" str))
375 (define (tokenize str)
376 ;; Lines have the form "alias ALIAS MODULE", where ALIAS can contain
377 ;; whitespace. This is why we don't use 'string-tokenize'.
378 (let* ((str (string-trim-both str))
379 (left (string-index str #\space))
380 (right (string-rindex str #\space)))
381 (list (string-take str left)
382 (string-trim-both (substring str left right))
383 (string-trim-both (string-drop str right)))))
385 (let loop ((aliases '()))
386 (match (read-line port)
392 (match (tokenize line)
393 (("alias" alias module)
394 (loop (alist-cons (string->compiled-sglob alias) module
399 (define (current-kernel-directory)
400 "Return the directory of the currently running Linux kernel."
401 (string-append (or (getenv "LINUX_MODULE_DIRECTORY")
402 "/run/booted-system/kernel/lib/modules")
403 "/" (utsname:release (uname))))
405 (define (current-alias-file)
406 "Return the absolute file name of the default 'modules.alias' file."
407 (string-append (current-kernel-directory) "/modules.alias"))
409 (define* (known-module-aliases #:optional (alias-file (current-alias-file)))
410 "Return the list of alias/module pairs read from ALIAS-FILE. Each alias is
412 (call-with-input-file alias-file read-module-aliases))
414 (define* (matching-modules alias
415 #:optional (known-aliases (known-module-aliases)))
416 "Return the list of modules that match ALIAS according to KNOWN-ALIASES.
417 ALIAS is a string like \"scsi:t-0x00\" as returned by
418 'device-module-aliases'."
419 (filter-map (match-lambda
421 (and (glob-match? pattern alias)
425 ;;; linux-modules.scm ends here