]> git.ipfire.org Git - thirdparty/dracut.git/blame - dracut-init.sh
dracut.sh: call dracut-install with "-f" in FIPS mode
[thirdparty/dracut.git] / dracut-init.sh
CommitLineData
777f2db0
HH
1#!/bin/bash
2#
561eb42f 3# functions used only by dracut and dracut modules
777f2db0
HH
4#
5# Copyright 2005-2009 Red Hat, Inc. All rights reserved.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but 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.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19#
20export LC_MESSAGES=C
21
561eb42f
HH
22# is_func <command>
23# Check whether $1 is a function.
24is_func() {
25 [[ "$(type -t "$1")" = "function" ]]
26}
27
28if ! [[ $dracutbasedir ]]; then
29 dracutbasedir=${BASH_SOURCE[0]%/*}
30 [[ $dracutbasedir = dracut-functions* ]] && dracutbasedir="."
31 [[ $dracutbasedir ]] || dracutbasedir="."
32 dracutbasedir="$(readlink -f $dracutbasedir)"
33fi
34
35if ! is_func dinfo >/dev/null 2>&1; then
36 . "$dracutbasedir/dracut-logger.sh"
37 dlog_init
38fi
39
40if ! [[ $initdir ]]; then
41 dfatal "initdir not set"
42 exit 1
43fi
44
45if ! [[ -d $initdir ]]; then
46 mkdir -p "$initdir"
47fi
48
777f2db0
HH
49if [[ $DRACUT_KERNEL_LAZY ]] && ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
50 if ! [[ -d "$initdir/.kernelmodseen" ]]; then
51 mkdir -p "$initdir/.kernelmodseen"
52 fi
53 DRACUT_KERNEL_LAZY_HASHDIR="$initdir/.kernelmodseen"
54fi
55
561eb42f
HH
56if ! [[ $kernel ]]; then
57 kernel=$(uname -r)
58 export kernel
59fi
60
61srcmods="/lib/modules/$kernel/"
62
63[[ $drivers_dir ]] && {
64 if ! command -v kmod &>/dev/null && vercmp "$(modprobe --version | cut -d' ' -f3)" lt 3.7; then
65 dfatal 'To use --kmoddir option module-init-tools >= 3.7 is required.'
66 exit 1
67 fi
68 srcmods="$drivers_dir"
69}
70export srcmods
71
72# export standard hookdirs
73[[ $hookdirs ]] || {
74 hookdirs="cmdline pre-udev pre-trigger netroot "
75 hookdirs+="initqueue initqueue/settled initqueue/online initqueue/finished initqueue/timeout "
76 hookdirs+="pre-mount pre-pivot cleanup mount "
77 hookdirs+="emergency shutdown-emergency pre-shutdown shutdown "
78 export hookdirs
79}
80
456f299a
HH
81. $dracutbasedir/dracut-functions.sh
82
561eb42f
HH
83# Detect lib paths
84if ! [[ $libdirs ]] ; then
85 if [[ "$(ldd /bin/sh)" == */lib64/* ]] &>/dev/null \
86 && [[ -d /lib64 ]]; then
87 libdirs+=" /lib64"
88 [[ -d /usr/lib64 ]] && libdirs+=" /usr/lib64"
89 else
90 libdirs+=" /lib"
91 [[ -d /usr/lib ]] && libdirs+=" /usr/lib"
92 fi
93
94 libdirs+=" $(ldconfig_paths)"
95
96 export libdirs
777f2db0
HH
97fi
98
561eb42f
HH
99# helper function for check() in module-setup.sh
100# to check for required installed binaries
101# issues a standardized warning message
102require_binaries() {
103 local _module_name="${moddir##*/}"
104 local _ret=0
105
106 if [[ "$1" = "-m" ]]; then
107 _module_name="$2"
108 shift 2
109 fi
110
111 for cmd in "$@"; do
112 if ! find_binary "$cmd" &>/dev/null; then
113 dinfo "dracut module '${_module_name#[0-9][0-9]}' will not be installed, because command '$cmd' could not be found!"
114 ((_ret++))
115 fi
116 done
117 return $_ret
118}
119
120require_any_binary() {
121 local _module_name="${moddir##*/}"
122 local _ret=1
123
124 if [[ "$1" = "-m" ]]; then
125 _module_name="$2"
126 shift 2
127 fi
128
129 for cmd in "$@"; do
130 if find_binary "$cmd" &>/dev/null; then
131 _ret=0
132 break
133 fi
134 done
135
136 if (( $_ret != 0 )); then
137 dinfo "$_module_name: Could not find any command of '$@'!"
138 return 1
139 fi
140
141 return 0
142}
143
144dracut_need_initqueue() {
145 >"$initdir/lib/dracut/need-initqueue"
146}
147
148dracut_module_included() {
149 [[ " $mods_to_load $modules_loaded " == *\ $*\ * ]]
150}
151
152if ! [[ $DRACUT_INSTALL ]]; then
153 DRACUT_INSTALL=$(find_binary dracut-install)
154fi
155
156if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install ]]; then
157 DRACUT_INSTALL=$dracutbasedir/dracut-install
158elif ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/install/dracut-install ]]; then
159 DRACUT_INSTALL=$dracutbasedir/install/dracut-install
160fi
161
162if ! [[ -x $DRACUT_INSTALL ]]; then
163 dfatal "dracut-install not found!"
164 exit 10
165fi
166
167[[ $DRACUT_RESOLVE_LAZY ]] || export DRACUT_RESOLVE_DEPS=1
168inst_dir() {
169 [[ -e ${initdir}/"$1" ]] && return 0 # already there
170 $DRACUT_INSTALL ${initdir:+-D "$initdir"} -d "$@"
171 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} -d "$@" || :
172}
173
174inst() {
175 local _hostonly_install
176 if [[ "$1" == "-H" ]]; then
177 _hostonly_install="-H"
178 shift
179 fi
180 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
181 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
182 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
183}
184
185inst_simple() {
186 local _hostonly_install
187 if [[ "$1" == "-H" ]]; then
188 _hostonly_install="-H"
189 shift
190 fi
191 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
192 [[ -e $1 ]] || return 1 # no source
193 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${_hostonly_install:+-H} "$@"
194 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${_hostonly_install:+-H} "$@" || :
195}
196
197inst_symlink() {
198 local _hostonly_install
199 if [[ "$1" == "-H" ]]; then
200 _hostonly_install="-H"
201 shift
202 fi
203 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
204 [[ -L $1 ]] || return 1
205 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
206 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
207}
208
209inst_multiple() {
210 local _ret
211 $DRACUT_INSTALL ${initdir:+-D "$initdir"} -a ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
212 _ret=$?
213 (($_ret != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} -a ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
214 return $_ret
215}
216
217dracut_install() {
218 inst_multiple "$@"
219}
220
221inst_library() {
222 local _hostonly_install
223 if [[ "$1" == "-H" ]]; then
224 _hostonly_install="-H"
225 shift
226 fi
227 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
228 [[ -e $1 ]] || return 1 # no source
229 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
230 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
231}
232
233inst_binary() {
234 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
235 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@" || :
236}
237
238inst_script() {
239 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
240 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@" || :
241}
242
243mark_hostonly() {
244 for i in "$@"; do
245 echo "$i" >> "$initdir/lib/dracut/hostonly-files"
246 done
247}
248
249# find symlinks linked to given library file
250# $1 = library file
251# Function searches for symlinks by stripping version numbers appended to
252# library filename, checks if it points to the same target and finally
253# prints the list of symlinks to stdout.
254#
255# Example:
256# rev_lib_symlinks libfoo.so.8.1
257# output: libfoo.so.8 libfoo.so
258# (Only if libfoo.so.8 and libfoo.so exists on host system.)
259rev_lib_symlinks() {
260 [[ ! $1 ]] && return 0
261
262 local fn="$1" orig="$(readlink -f "$1")" links=''
263
264 [[ ${fn} == *.so.* ]] || return 1
265
266 until [[ ${fn##*.} == so ]]; do
267 fn="${fn%.*}"
268 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
269 done
270
271 echo "${links}"
272}
273
274# attempt to install any programs specified in a udev rule
275inst_rule_programs() {
276 local _prog _bin
277
06a1d076
VS
278 for _prog in $(sed -nr 's/.*PROGRAM==?"([^ "]+).*/\1/p'); do
279 _bin=""
280 if [ -x ${udevdir}/$_prog ]; then
281 _bin=${udevdir}/$_prog
282 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
283 _bin=$(find_binary "$_prog") || {
284 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
285 continue;
286 }
287 fi
288
289 [[ $_bin ]] && inst_binary "$_bin"
290 done
291 for _prog in $(sed -nr 's/.*RUN[+=]=?"([^ "]+).*/\1/p'); do
292 _bin=""
293 if [ -x ${udevdir}/$_prog ]; then
294 _bin=${udevdir}/$_prog
295 elif [[ "${_prog/\$env\{/}" == "$_prog" ]] && [[ "${_prog}" != "/sbin/initqueue" ]]; then
296 _bin=$(find_binary "$_prog") || {
297 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
298 continue;
299 }
300 fi
301
302 [[ $_bin ]] && inst_binary "$_bin"
303 done
304 for _prog in $(sed -nr 's/.*IMPORT\{program\}==?"([^ "]+).*/\1/p'); do
305 _bin=""
306 if [ -x ${udevdir}/$_prog ]; then
307 _bin=${udevdir}/$_prog
308 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
309 _bin=$(find_binary "$_prog") || {
310 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
311 continue;
312 }
313 fi
314
315 [[ $_bin ]] && dracut_install "$_bin"
316 done
561eb42f
HH
317}
318
319# attempt to install any programs specified in a udev rule
320inst_rule_group_owner() {
321 local i
322
06a1d076
VS
323 for i in $(sed -nr 's/.*OWNER=?"([^ "]+).*/\1/p'); do
324 if ! grep -Eq "^$i:" "$initdir/etc/passwd" 2>/dev/null; then
325 grep -E "^$i:" /etc/passwd 2>/dev/null >> "$initdir/etc/passwd"
326 fi
327 done
328 for i in $(sed -nr 's/.*GROUP=?"([^ "]+).*/\1/p' "$1"); do
329 if ! grep -Eq "^$i:" "$initdir/etc/group" 2>/dev/null; then
330 grep -E "^$i:" /etc/group 2>/dev/null >> "$initdir/etc/group"
331 fi
332 done
561eb42f
HH
333}
334
335inst_rule_initqueue() {
336 if grep -q -F initqueue "$1"; then
337 dracut_need_initqueue
338 fi
339}
340
341# udev rules always get installed in the same place, so
342# create a function to install them to make life simpler.
343inst_rules() {
344 local _target=/etc/udev/rules.d _rule _found
345
346 inst_dir "${udevdir}/rules.d"
347 inst_dir "$_target"
348 for _rule in "$@"; do
349 if [ "${_rule#/}" = "$_rule" ]; then
350 for r in ${udevdir}/rules.d ${hostonly:+/etc/udev/rules.d}; do
351 [[ -e $r/$_rule ]] || continue
352 _found="$r/$_rule"
353 inst_rule_programs "$_found"
354 inst_rule_group_owner "$_found"
355 inst_rule_initqueue "$_found"
356 inst_simple "$_found"
357 done
358 fi
359 for r in '' $dracutbasedir/rules.d/; do
360 # skip rules without an absolute path
361 [[ "${r}$_rule" != /* ]] && continue
362 [[ -f ${r}$_rule ]] || continue
363 _found="${r}$_rule"
364 inst_rule_programs "$_found"
365 inst_rule_group_owner "$_found"
366 inst_rule_initqueue "$_found"
367 inst_simple "$_found" "$_target/${_found##*/}"
368 done
369 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
370 done
371}
372
373inst_rules_wildcard() {
374 local _target=/etc/udev/rules.d _rule _found
375
376 inst_dir "${udevdir}/rules.d"
377 inst_dir "$_target"
378 for _rule in ${udevdir}/rules.d/$1 ${dracutbasedir}/rules.d/$1 ; do
379 [[ -e $_rule ]] || continue
380 inst_rule_programs "$_rule"
381 inst_rule_group_owner "$_rule"
382 inst_rule_initqueue "$_rule"
383 inst_simple "$_rule"
384 _found=$_rule
385 done
386 if [[ -n ${hostonly} ]] ; then
387 for _rule in ${_target}/$1 ; do
388 [[ -f $_rule ]] || continue
389 inst_rule_programs "$_rule"
390 inst_rule_group_owner "$_rule"
391 inst_rule_initqueue "$_rule"
392 inst_simple "$_rule"
393 _found=$_rule
394 done
395 fi
396 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
397}
398
399prepare_udev_rules() {
400 [ -z "$UDEVVERSION" ] && export UDEVVERSION=$(udevadm --version)
401
402 for f in "$@"; do
403 f="${initdir}/etc/udev/rules.d/$f"
404 [ -e "$f" ] || continue
405 while read line || [ -n "$line" ]; do
406 if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
407 if [ $UDEVVERSION -ge 174 ]; then
408 printf '%sIMPORT{builtin}="path_id"\n' "${line%%IMPORT PATH_ID}"
409 else
410 printf '%sIMPORT{program}="path_id %%p"\n' "${line%%IMPORT PATH_ID}"
411 fi
412 elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
413 if [ $UDEVVERSION -ge 176 ]; then
414 printf '%sIMPORT{builtin}="blkid"\n' "${line%%IMPORT BLKID}"
415 else
416 printf '%sIMPORT{program}="/sbin/blkid -o udev -p $tempnode"\n' "${line%%IMPORT BLKID}"
417 fi
418 else
419 echo "$line"
420 fi
421 done < "${f}" > "${f}.new"
422 mv "${f}.new" "$f"
423 done
424}
425
426# install function specialized for hooks
427# $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
428# All hooks should be POSIX/SuS compliant, they will be sourced by init.
429inst_hook() {
430 if ! [[ -f $3 ]]; then
431 dfatal "Cannot install a hook ($3) that does not exist."
432 dfatal "Aborting initrd creation."
433 exit 1
434 elif ! [[ "$hookdirs" == *$1* ]]; then
435 dfatal "No such hook type $1. Aborting initrd creation."
436 exit 1
437 fi
438 inst_simple "$3" "/lib/dracut/hooks/${1}/${2}-${3##*/}"
439}
440
441# install any of listed files
442#
443# If first argument is '-d' and second some destination path, first accessible
444# source is installed into this path, otherwise it will installed in the same
445# path as source. If none of listed files was installed, function return 1.
446# On first successful installation it returns with 0 status.
447#
448# Example:
449#
450# inst_any -d /bin/foo /bin/bar /bin/baz
451#
452# Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
453# initramfs.
454inst_any() {
455 local to f
456
457 [[ $1 = '-d' ]] && to="$2" && shift 2
458
459 for f in "$@"; do
460 [[ -e $f ]] || continue
461 [[ $to ]] && inst "$f" "$to" && return 0
462 inst "$f" && return 0
463 done
464
465 return 1
466}
467
468
469# inst_libdir_file [-n <pattern>] <file> [<file>...]
470# Install a <file> located on a lib directory to the initramfs image
471# -n <pattern> install matching files
472inst_libdir_file() {
473 local _files
474 if [[ "$1" == "-n" ]]; then
475 local _pattern=$2
476 shift 2
477 for _dir in $libdirs; do
478 for _i in "$@"; do
479 for _f in "$_dir"/$_i; do
480 [[ "$_f" =~ $_pattern ]] || continue
481 [[ -e "$_f" ]] && _files+="$_f "
482 done
483 done
484 done
485 else
486 for _dir in $libdirs; do
487 for _i in "$@"; do
488 for _f in "$_dir"/$_i; do
489 [[ -e "$_f" ]] && _files+="$_f "
490 done
491 done
492 done
493 fi
494 [[ $_files ]] && inst_multiple $_files
495}
496
497
498# install function decompressing the target and handling symlinks
499# $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
500#
501# Function install targets in the same paths inside overlay but decompressed
502# and without extensions (.gz, .bz2).
503inst_decompress() {
504 local _src _cmd
505
506 for _src in $@
507 do
508 case ${_src} in
509 *.gz) _cmd='gzip -f -d' ;;
510 *.bz2) _cmd='bzip2 -d' ;;
511 *) return 1 ;;
512 esac
513 inst_simple ${_src}
514 # Decompress with chosen tool. We assume that tool changes name e.g.
515 # from 'name.gz' to 'name'.
516 ${_cmd} "${initdir}${_src}"
517 done
518}
519
520# It's similar to above, but if file is not compressed, performs standard
521# install.
522# $@ = list of files
523inst_opt_decompress() {
524 local _src
525
526 for _src in $@; do
527 inst_decompress "${_src}" || inst "${_src}"
528 done
529}
530
531# module_check <dracut module>
532# execute the check() function of module-setup.sh of <dracut module>
533# or the "check" script, if module-setup.sh is not found
534# "check $hostonly" is called
535module_check() {
536 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
537 local _ret
538 local _forced=0
539 local _hostonly=$hostonly
540 [ $# -eq 2 ] && _forced=$2
541 [[ -d $_moddir ]] || return 1
542 if [[ ! -f $_moddir/module-setup.sh ]]; then
543 # if we do not have a check script, we are unconditionally included
544 [[ -x $_moddir/check ]] || return 0
545 [ $_forced -ne 0 ] && unset hostonly
546 $_moddir/check $hostonly
547 _ret=$?
548 else
549 unset check depends cmdline install installkernel
550 check() { true; }
551 . $_moddir/module-setup.sh
552 is_func check || return 0
553 [ $_forced -ne 0 ] && unset hostonly
554 moddir=$_moddir check $hostonly
555 _ret=$?
556 unset check depends cmdline install installkernel
557 fi
558 hostonly=$_hostonly
559 return $_ret
560}
561
562# module_check_mount <dracut module>
563# execute the check() function of module-setup.sh of <dracut module>
564# or the "check" script, if module-setup.sh is not found
565# "mount_needs=1 check 0" is called
566module_check_mount() {
567 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
568 local _ret
569 mount_needs=1
570 [[ -d $_moddir ]] || return 1
571 if [[ ! -f $_moddir/module-setup.sh ]]; then
572 # if we do not have a check script, we are unconditionally included
573 [[ -x $_moddir/check ]] || return 0
574 mount_needs=1 $_moddir/check 0
575 _ret=$?
576 else
577 unset check depends cmdline install installkernel
578 check() { false; }
579 . $_moddir/module-setup.sh
580 moddir=$_moddir check 0
581 _ret=$?
582 unset check depends cmdline install installkernel
583 fi
584 unset mount_needs
585 return $_ret
586}
587
588# module_depends <dracut module>
589# execute the depends() function of module-setup.sh of <dracut module>
590# or the "depends" script, if module-setup.sh is not found
591module_depends() {
592 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
593 local _ret
594 [[ -d $_moddir ]] || return 1
595 if [[ ! -f $_moddir/module-setup.sh ]]; then
596 # if we do not have a check script, we have no deps
597 [[ -x $_moddir/check ]] || return 0
598 $_moddir/check -d
599 return $?
600 else
601 unset check depends cmdline install installkernel
602 depends() { true; }
603 . $_moddir/module-setup.sh
604 moddir=$_moddir depends
605 _ret=$?
606 unset check depends cmdline install installkernel
607 return $_ret
608 fi
609}
610
611# module_cmdline <dracut module>
612# execute the cmdline() function of module-setup.sh of <dracut module>
613# or the "cmdline" script, if module-setup.sh is not found
614module_cmdline() {
615 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
616 local _ret
617 [[ -d $_moddir ]] || return 1
618 if [[ ! -f $_moddir/module-setup.sh ]]; then
619 [[ -x $_moddir/cmdline ]] && . "$_moddir/cmdline"
620 return $?
621 else
622 unset check depends cmdline install installkernel
623 cmdline() { true; }
624 . $_moddir/module-setup.sh
625 moddir=$_moddir cmdline
626 _ret=$?
627 unset check depends cmdline install installkernel
628 return $_ret
629 fi
630}
631
632# module_install <dracut module>
633# execute the install() function of module-setup.sh of <dracut module>
634# or the "install" script, if module-setup.sh is not found
635module_install() {
636 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
637 local _ret
638 [[ -d $_moddir ]] || return 1
639 if [[ ! -f $_moddir/module-setup.sh ]]; then
640 [[ -x $_moddir/install ]] && . "$_moddir/install"
641 return $?
642 else
643 unset check depends cmdline install installkernel
644 install() { true; }
645 . $_moddir/module-setup.sh
646 moddir=$_moddir install
647 _ret=$?
648 unset check depends cmdline install installkernel
649 return $_ret
650 fi
651}
652
653# module_installkernel <dracut module>
654# execute the installkernel() function of module-setup.sh of <dracut module>
655# or the "installkernel" script, if module-setup.sh is not found
656module_installkernel() {
657 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
658 local _ret
659 [[ -d $_moddir ]] || return 1
660 if [[ ! -f $_moddir/module-setup.sh ]]; then
661 [[ -x $_moddir/installkernel ]] && . "$_moddir/installkernel"
662 return $?
663 else
664 unset check depends cmdline install installkernel
665 installkernel() { true; }
666 . $_moddir/module-setup.sh
667 moddir=$_moddir installkernel
668 _ret=$?
669 unset check depends cmdline install installkernel
670 return $_ret
671 fi
672}
673
674# check_mount <dracut module>
675# check_mount checks, if a dracut module is needed for the given
676# device and filesystem types in "${host_fs_types[@]}"
677check_mount() {
678 local _mod=$1
679 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
680 local _ret
681 local _moddep
682
683 [ "${#host_fs_types[@]}" -le 0 ] && return 1
684
685 # If we are already scheduled to be loaded, no need to check again.
686 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
687 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
688
689 # This should never happen, but...
690 [[ -d $_moddir ]] || return 1
691
692 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
693
694 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
695 return 1
696 fi
697
698 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
699 module_check_mount $_mod; ret=$?
700
701 # explicit module, so also accept ret=255
702 [[ $ret = 0 || $ret = 255 ]] || return 1
703 else
704 # module not in our list
705 if [[ $dracutmodules = all ]]; then
706 # check, if we can and should install this module
707 module_check_mount $_mod || return 1
708 else
709 # skip this module
710 return 1
711 fi
712 fi
713
714 for _moddep in $(module_depends $_mod); do
715 # handle deps as if they were manually added
716 [[ " $dracutmodules " == *\ $_mod\ * ]] \
717 && [[ " $dracutmodules " != *\ $_moddep\ * ]] \
718 && dracutmodules+=" $_moddep "
719 [[ " $add_dracutmodules " == *\ $_mod\ * ]] \
720 && [[ " $add_dracutmodules " != *\ $_moddep\ * ]] \
721 && add_dracutmodules+=" $_moddep "
722 [[ " $force_add_dracutmodules " == *\ $_mod\ * ]] \
723 && [[ " $force_add_dracutmodules " != *\ $_moddep\ * ]] \
724 && force_add_dracutmodules+=" $_moddep "
725 # if a module we depend on fail, fail also
726 if ! check_module $_moddep; then
727 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
728 return 1
729 fi
730 done
731
732 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
733 mods_to_load+=" $_mod "
734
735 return 0
736}
737
738# check_module <dracut module> [<use_as_dep>]
739# check if a dracut module is to be used in the initramfs process
740# if <use_as_dep> is set, then the process also keeps track
741# that the modules were checked for the dependency tracking process
742check_module() {
743 local _mod=$1
744 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
745 local _ret
746 local _moddep
747 # If we are already scheduled to be loaded, no need to check again.
748 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
749 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
750
751 # This should never happen, but...
752 [[ -d $_moddir ]] || return 1
753
754 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
755
756 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
757 dinfo "dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
758 return 1
759 fi
760
761 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
762 if [[ " $dracutmodules $force_add_dracutmodules " == *\ $_mod\ * ]]; then
763 module_check $_mod 1; ret=$?
764 else
765 module_check $_mod 0; ret=$?
766 fi
767 # explicit module, so also accept ret=255
768 [[ $ret = 0 || $ret = 255 ]] || return 1
769 else
770 # module not in our list
771 if [[ $dracutmodules = all ]]; then
772 # check, if we can and should install this module
773 module_check $_mod; ret=$?
774 if [[ $ret != 0 ]]; then
775 [[ $2 ]] && return 1
776 [[ $ret != 255 ]] && return 1
777 fi
778 else
779 # skip this module
780 return 1
781 fi
782 fi
783
784 for _moddep in $(module_depends $_mod); do
785 # handle deps as if they were manually added
786 [[ " $dracutmodules " == *\ $_mod\ * ]] \
787 && [[ " $dracutmodules " != *\ $_moddep\ * ]] \
788 && dracutmodules+=" $_moddep "
789 [[ " $add_dracutmodules " == *\ $_mod\ * ]] \
790 && [[ " $add_dracutmodules " != *\ $_moddep\ * ]] \
791 && add_dracutmodules+=" $_moddep "
792 [[ " $force_add_dracutmodules " == *\ $_mod\ * ]] \
793 && [[ " $force_add_dracutmodules " != *\ $_moddep\ * ]] \
794 && force_add_dracutmodules+=" $_moddep "
795 # if a module we depend on fail, fail also
796 if ! check_module $_moddep; then
797 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
798 return 1
799 fi
800 done
801
802 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
803 mods_to_load+=" $_mod "
804
805 return 0
806}
807
808# for_each_module_dir <func>
809# execute "<func> <dracut module> 1"
810for_each_module_dir() {
811 local _modcheck
812 local _mod
813 local _moddir
814 local _func
815 _func=$1
816 for _moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
817 [[ -d $_moddir ]] || continue;
818 [[ -e $_moddir/install || -e $_moddir/installkernel || \
819 -e $_moddir/module-setup.sh ]] || continue
820 _mod=${_moddir##*/}; _mod=${_mod#[0-9][0-9]}
821 $_func $_mod 1
822 done
823
824 # Report any missing dracut modules, the user has specified
825 _modcheck="$add_dracutmodules $force_add_dracutmodules"
826 [[ $dracutmodules != all ]] && _modcheck="$_modcheck $dracutmodules"
827 for _mod in $_modcheck; do
828 [[ " $mods_to_load " == *\ $_mod\ * ]] && continue
829
830 [[ " $force_add_dracutmodules " != *\ $_mod\ * ]] \
831 && [[ " $dracutmodules " != *\ $_mod\ * ]] \
832 && [[ " $omit_dracutmodules " == *\ $_mod\ * ]] \
833 && continue
834
835 derror "dracut module '$_mod' cannot be found or installed."
836 [[ " $force_add_dracutmodules " == *\ $_mod\ * ]] && exit 1
837 [[ " $dracutmodules " == *\ $_mod\ * ]] && exit 1
838 [[ " $add_dracutmodules " == *\ $_mod\ * ]] && exit 1
839 done
840}
841
842# Install a single kernel module along with any firmware it may require.
843# $1 = full path to kernel module to install
844install_kmod_with_fw() {
845 # no need to go further if the module is already installed
846
847 [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
848 && return 0
849
850 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -e "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}" ]]; then
851 read ret < "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
852 return $ret
853 fi
854
855 if [[ $omit_drivers ]]; then
856 local _kmod=${1##*/}
857 _kmod=${_kmod%.ko*}
858 _kmod=${_kmod/-/_}
859 if [[ "$_kmod" =~ $omit_drivers ]]; then
860 dinfo "Omitting driver $_kmod"
861 return 0
862 fi
863 if [[ "${1##*/lib/modules/$kernel/}" =~ $omit_drivers ]]; then
864 dinfo "Omitting driver $_kmod"
865 return 0
866 fi
867 fi
868
869 if [[ $silent_omit_drivers ]]; then
870 local _kmod=${1##*/}
871 _kmod=${_kmod%.ko*}
872 _kmod=${_kmod/-/_}
873 [[ "$_kmod" =~ $silent_omit_drivers ]] && return 0
874 [[ "${1##*/lib/modules/$kernel/}" =~ $silent_omit_drivers ]] && return 0
875 fi
876
877 inst_simple "$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
878 ret=$?
879 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
880 [[ -d "$DRACUT_KERNEL_LAZY_HASHDIR" ]] && \
881 echo $ret > "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
882 (($ret != 0)) && return $ret
883
884 local _modname=${1##*/} _fwdir _found _fw
885 _modname=${_modname%.ko*}
886 for _fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do
887 _found=''
888 for _fwdir in $fw_dir; do
889 [[ -d $_fwdir && -f $_fwdir/$_fw ]] || continue
890 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
891 _found=yes
892 done
893 if [[ $_found != yes ]]; then
894 if ! [[ -d $(echo /sys/module/${_modname//-/_}|{ read a b; echo $a; }) ]]; then
895 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
896 "\"${_modname}.ko\""
897 else
898 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
899 "\"${_modname}.ko\""
900 fi
901 fi
902 done
903 return 0
904}
905
906# Do something with all the dependencies of a kernel module.
907# Note that kernel modules depend on themselves using the technique we use
908# $1 = function to call for each dependency we find
909# It will be passed the full path to the found kernel module
910# $2 = module to get dependencies for
911# rest of args = arguments to modprobe
912# _fderr specifies FD passed from surrounding scope
913for_each_kmod_dep() {
914 local _func=$1 _kmod=$2 _cmd _modpath _options
915 shift 2
916 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
917 while read _cmd _modpath _options || [ -n "$_cmd" ]; do
918 [[ $_cmd = insmod ]] || continue
919 $_func ${_modpath} || exit $?
920 done
921 )
922}
923
924dracut_kernel_post() {
925 local _moddirname=${srcmods%%/lib/modules/*}
926 local _pid
927
928 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" ]]; then
929 xargs -r modprobe -a ${_moddirname:+-d ${_moddirname}/} \
930 --ignore-install --show-depends --set-version $kernel \
931 < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" 2>/dev/null \
932 | sort -u \
933 | while read _cmd _modpath _options || [ -n "$_cmd" ]; do
934 [[ $_cmd = insmod ]] || continue
935 echo "$_modpath"
936 done > "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
937
938 (
939 if [[ $DRACUT_INSTALL ]] && [[ -z $_moddirname ]]; then
940 xargs -r $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} -a < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
941 else
942 while read _modpath || [ -n "$_modpath" ]; do
943 local _destpath=$_modpath
944 [[ $_moddirname ]] && _destpath=${_destpath##$_moddirname/}
945 _destpath=${_destpath##*/lib/modules/$kernel/}
946 inst_simple "$_modpath" "/lib/modules/$kernel/${_destpath}" || exit $?
947 done < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
948 fi
949 ) &
950 _pid=$(jobs -p | while read a || [ -n "$a" ]; do printf ":$a";done)
951 _pid=${_pid##*:}
952
953 if [[ $DRACUT_INSTALL ]]; then
954 xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" \
955 | while read line || [ -n "$line" ]; do
956 for _fwdir in $fw_dir; do
957 echo $_fwdir/$line;
958 done;
959 done | xargs -r $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} -a -o
960 else
961 for _fw in $(xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"); do
962 for _fwdir in $fw_dir; do
963 [[ -d $_fwdir && -f $_fwdir/$_fw ]] || continue
964 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
965 break
966 done
967 done
968 fi
969
970 wait $_pid
971 fi
972
973 for _f in modules.builtin.bin modules.builtin modules.order; do
974 [[ $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f"
975 done
976
977 # generate module dependencies for the initrd
978 if [[ -d $initdir/lib/modules/$kernel ]] && \
979 ! depmod -a -b "$initdir" $kernel; then
980 dfatal "\"depmod -a $kernel\" failed."
981 exit 1
982 fi
983
984 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && rm -fr -- "$DRACUT_KERNEL_LAZY_HASHDIR"
985}
986
987[[ "$kernel_current" ]] || export kernel_current=$(uname -r)
988
989module_is_host_only() {
990 local _mod=$1
991 local _modenc a i _k _s _v _aliases
992 _mod=${_mod##*/}
993 _mod=${_mod%.ko*}
994 _modenc=${_mod//-/_}
995
996 [[ " $add_drivers " == *\ ${_mod}\ * ]] && return 0
997
998 # check if module is loaded
999 [[ ${host_modules["$_modenc"]} ]] && return 0
1000
1001 [[ "$kernel_current" ]] || export kernel_current=$(uname -r)
1002
1003 if [[ "$kernel_current" != "$kernel" ]]; then
1004 # check if module is loadable on the current kernel
1005 # this covers the case, where a new module is introduced
1006 # or a module was renamed
1007 # or a module changed from builtin to a module
1008
1009 if [[ -d /lib/modules/$kernel_current ]]; then
1010 # if the modinfo can be parsed, but the module
1011 # is not loaded, then we can safely return 1
1012 modinfo -F filename "$_mod" &>/dev/null && return 1
1013 fi
1014
07a081f3
HH
1015 # just install the module, better safe than sorry
1016 return 0
561eb42f
HH
1017 fi
1018
1019 return 1
1020}
1021
1022find_kernel_modules_by_path () {
1023 local _OLDIFS
1024
1025 [[ -f "$srcmods/modules.dep" ]] || return 0
1026
1027 _OLDIFS=$IFS
1028 IFS=:
1029 while read a rest || [ -n "$a" ]; do
1030 [[ $a = */$1/* ]] || [[ $a = updates/* ]] || continue
1031 printf "%s\n" "$srcmods/$a"
1032 done < "$srcmods/modules.dep"
1033 IFS=$_OLDIFS
1034 return 0
1035}
1036
1037find_kernel_modules () {
1038 find_kernel_modules_by_path drivers
1039}
1040
1041# instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1042# instmods [-c [-s]] <kernel subsystem>
1043# install kernel modules along with all their dependencies.
1044# <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1045instmods() {
1046 [[ $no_kernel = yes ]] && return
1047 # called [sub]functions inherit _fderr
1048 local _fderr=9
1049 local _check=no
1050 local _silent=no
1051 if [[ $1 = '-c' ]]; then
1052 _check=yes
1053 shift
1054 fi
1055
1056 if [[ $1 = '-s' ]]; then
1057 _silent=yes
1058 shift
1059 fi
1060
1061 function inst1mod() {
1062 local _ret=0 _mod="$1"
1063 case $_mod in
1064 =*)
1065 ( [[ "$_mpargs" ]] && echo $_mpargs
1066 find_kernel_modules_by_path "${_mod#=}" ) \
1067 | instmods
1068 ((_ret+=$?))
1069 ;;
1070 --*) _mpargs+=" $_mod" ;;
1071 *)
1072 _mod=${_mod##*/}
1073 # Check for aliased modules
1074 _modalias=$(modinfo -k $kernel -F filename $_mod 2> /dev/null)
1075 _modalias=${_modalias%.ko*}
1076 if [[ $_modalias ]] && [ "${_modalias##*/}" != "${_mod%.ko*}" ] ; then
1077 _mod=${_modalias##*/}
1078 fi
1079
1080 # if we are already installed, skip this module and go on
1081 # to the next one.
1082 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1083 [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko*}" ]]; then
1084 read _ret <"$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko*}"
1085 return $_ret
1086 fi
1087
1088 _mod=${_mod/-/_}
1089 if [[ $omit_drivers ]] && [[ "$_mod" =~ $omit_drivers ]]; then
1090 dinfo "Omitting driver ${_mod##$srcmods}"
1091 return 0
1092 fi
1093
1094 # If we are building a host-specific initramfs and this
1095 # module is not already loaded, move on to the next one.
1096 [[ $hostonly ]] \
1097 && ! module_is_host_only "$_mod" \
1098 && return 0
1099
1100 if [[ "$_check" = "yes" ]] || ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
1101 # We use '-d' option in modprobe only if modules prefix path
1102 # differs from default '/'. This allows us to use dracut with
1103 # old version of modprobe which doesn't have '-d' option.
1104 local _moddirname=${srcmods%%/lib/modules/*}
1105 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1106
1107 # ok, load the module, all its dependencies, and any firmware
1108 # it may require
1109 for_each_kmod_dep install_kmod_with_fw $_mod \
1110 --set-version $kernel ${_moddirname} $_mpargs
1111 ((_ret+=$?))
1112 else
1113 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1114 echo ${_mod%.ko*} >> "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist"
1115 fi
1116 ;;
1117 esac
1118 return $_ret
1119 }
1120
1121 function instmods_1() {
1122 local _mod _mpargs
1123 if (($# == 0)); then # filenames from stdin
1124 while read _mod || [ -n "$_mod" ]; do
1125 inst1mod "${_mod%.ko*}" || {
1126 if [[ "$_check" == "yes" ]] && [[ "$_silent" == "no" ]]; then
1127 dfatal "Failed to install module $_mod"
1128 fi
1129 }
1130 done
1131 fi
1132 while (($# > 0)); do # filenames as arguments
1133 inst1mod ${1%.ko*} || {
1134 if [[ "$_check" == "yes" ]] && [[ "$_silent" == "no" ]]; then
1135 dfatal "Failed to install module $1"
1136 fi
1137 }
1138 shift
1139 done
1140 return 0
1141 }
1142
1143 local _ret _filter_not_found='FATAL: Module .* not found.'
1144 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1145 # redirections, but that would make dracut require bash4 at least.
1146 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1147 | while read line || [ -n "$line" ]; do [[ "$line" =~ $_filter_not_found ]] || echo $line;done | derror
1148 _ret=$?
1149 return $_ret
1150}