3 # functions used only by dracut and dracut modules
5 # Copyright 2005-2009 Red Hat, Inc. All rights reserved.
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.
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.
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/>.
23 # Check whether $1 is a function.
25 [[ "$(type -t "$1")" = "function" ]]
28 if ! [[ $dracutbasedir ]]; then
29 dracutbasedir
=${BASH_SOURCE[0]%/*}
30 [[ $dracutbasedir = dracut-functions
* ]] && dracutbasedir
="."
31 [[ $dracutbasedir ]] || dracutbasedir
="."
32 dracutbasedir
="$(readlink -f $dracutbasedir)"
35 if ! is_func dinfo
>/dev
/null
2>&1; then
36 .
"$dracutbasedir/dracut-logger.sh"
40 if ! [[ $initdir ]]; then
41 dfatal
"initdir not set"
45 if ! [[ -d $initdir ]]; then
49 if [[ $DRACUT_KERNEL_LAZY ]] && ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
50 if ! [[ -d "$initdir/.kernelmodseen" ]]; then
51 mkdir
-p "$initdir/.kernelmodseen"
53 DRACUT_KERNEL_LAZY_HASHDIR
="$initdir/.kernelmodseen"
56 if ! [[ $kernel ]]; then
61 srcmods
="/lib/modules/$kernel/"
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.'
68 srcmods
="$drivers_dir"
72 # export standard 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 "
81 .
$dracutbasedir/dracut-functions.sh
84 if ! [[ $libdirs ]] ; then
85 if [[ "$(ldd /bin/sh)" == */lib64
/* ]] &>/dev
/null \
86 && [[ -d /lib64
]]; then
88 [[ -d /usr
/lib64
]] && libdirs
+=" /usr/lib64"
91 [[ -d /usr
/lib
]] && libdirs
+=" /usr/lib"
94 libdirs
+=" $(ldconfig_paths)"
99 # helper function for check() in module-setup.sh
100 # to check for required installed binaries
101 # issues a standardized warning message
103 local _module_name
="${moddir##*/}"
106 if [[ "$1" = "-m" ]]; then
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!"
120 require_any_binary
() {
121 local _module_name
="${moddir##*/}"
124 if [[ "$1" = "-m" ]]; then
130 if find_binary
"$cmd" &>/dev
/null
; then
136 if (( $_ret != 0 )); then
137 dinfo
"$_module_name: Could not find any command of '$@'!"
144 dracut_need_initqueue
() {
145 >"$initdir/lib/dracut/need-initqueue"
148 dracut_module_included
() {
149 [[ " $mods_to_load $modules_loaded " == *\ $
*\
* ]]
152 if ! [[ $DRACUT_INSTALL ]]; then
153 DRACUT_INSTALL
=$
(find_binary dracut-install
)
156 if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install
]]; then
157 DRACUT_INSTALL
=$dracutbasedir/dracut-install
158 elif ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/install
/dracut-install
]]; then
159 DRACUT_INSTALL
=$dracutbasedir/install
/dracut-install
162 if ! [[ -x $DRACUT_INSTALL ]]; then
163 dfatal
"dracut-install not found!"
167 [[ $DRACUT_RESOLVE_LAZY ]] ||
export DRACUT_RESOLVE_DEPS
=1
169 [[ -e ${initdir}/"$1" ]] && return 0 # already there
170 $DRACUT_INSTALL ${initdir:+-D "$initdir"} -d "$@"
171 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir:+-D "$initdir"} -d "$@" ||
:
175 local _hostonly_install
176 if [[ "$1" == "-H" ]]; then
177 _hostonly_install
="-H"
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} "$@" ||
:
186 local _hostonly_install
187 if [[ "$1" == "-H" ]]; then
188 _hostonly_install
="-H"
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} "$@" ||
:
198 local _hostonly_install
199 if [[ "$1" == "-H" ]]; then
200 _hostonly_install
="-H"
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} "$@" ||
:
211 $DRACUT_INSTALL ${initdir:+-D "$initdir"} -a ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
213 (($_ret != 0)) && derror
$DRACUT_INSTALL ${initdir:+-D "$initdir"} -a ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" ||
:
222 local _hostonly_install
223 if [[ "$1" == "-H" ]]; then
224 _hostonly_install
="-H"
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} "$@" ||
:
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} "$@" ||
:
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} "$@" ||
:
245 echo "$i" >> "$initdir/lib/dracut/hostonly-files"
249 # find symlinks linked to given 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.
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.)
260 [[ ! $1 ]] && return 0
262 local fn
="$1" orig
="$(readlink -f "$1")" links
=''
264 [[ ${fn} == *.so.
* ]] ||
return 1
266 until [[ ${fn##*.} == so
]]; do
268 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
274 # attempt to install any programs specified in a udev rule
275 inst_rule_programs() {
278 if grep -qE 'PROGRAM==?"[^
"]+' "$1"; then
279 for _prog in $(grep -E 'PROGRAM==?"[^
"]+' "$1" | sed -r 's/.*PROGRAM==?"([^
"]+).*/\1/'); do
281 if [ -x ${udevdir}/$_prog ]; then
282 _bin=${udevdir}/$_prog
283 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
284 _bin=$(find_binary "$_prog") || {
285 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
290 [[ $_bin ]] && inst_binary "$_bin"
293 if grep -qE 'RUN[+=]=?"[^
"]+' "$1"; then
294 for _prog in $(grep -E 'RUN[+=]=?"[^
"]+' "$1" | sed -r 's/.*RUN[+=]=?"([^
"]+).*/\1/'); do
296 if [ -x ${udevdir}/$_prog ]; then
297 _bin=${udevdir}/$_prog
298 elif [[ "${_prog/\$env\{/}" == "$_prog" ]] && [[ "${_prog}" != "/sbin
/initqueue
" ]]; then
299 _bin=$(find_binary "$_prog") || {
300 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
305 [[ $_bin ]] && inst_binary "$_bin"
308 if grep -qE 'IMPORT\{program\}==?"[^
"]+' "$1"; then
309 for _prog in $(grep -E 'IMPORT\{program\}==?"[^
"]+' "$1" | sed -r 's/.*IMPORT\{program\}==?"([^
"]+).*/\1/'); do
311 if [ -x ${udevdir}/$_prog ]; then
312 _bin=${udevdir}/$_prog
313 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
314 _bin=$(find_binary "$_prog") || {
315 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
320 [[ $_bin ]] && dracut_install "$_bin"
325 # attempt to install any programs specified in a udev rule
326 inst_rule_group_owner() {
329 if grep -qE 'OWNER=?"[^
"]+' "$1"; then
330 for i in $(grep -E 'OWNER=?"[^
"]+' "$1" | sed -r 's/.*OWNER=?"([^
"]+).*/\1/'); do
331 if ! egrep -q "^
$i:" "$initdir/etc
/passwd
" 2>/dev/null; then
332 egrep "^
$i:" /etc/passwd 2>/dev/null >> "$initdir/etc
/passwd
"
336 if grep -qE 'GROUP=?"[^
"]+' "$1"; then
337 for i in $(grep -E 'GROUP=?"[^
"]+' "$1" | sed -r 's/.*GROUP=?"([^
"]+).*/\1/'); do
338 if ! egrep -q "^
$i:" "$initdir/etc
/group
" 2>/dev/null; then
339 egrep "^
$i:" /etc/group 2>/dev/null >> "$initdir/etc
/group
"
345 inst_rule_initqueue() {
346 if grep -q -F initqueue "$1"; then
347 dracut_need_initqueue
351 # udev rules always get installed in the same place, so
352 # create a function to install them to make life simpler.
354 local _target=/etc/udev/rules.d _rule _found
356 inst_dir "${udevdir}/rules.d
"
358 for _rule in "$@
"; do
359 if [ "${_rule#/}" = "$_rule" ]; then
360 for r in ${udevdir}/rules.d ${hostonly:+/etc/udev/rules.d}; do
361 [[ -e $r/$_rule ]] || continue
363 inst_rule_programs "$_found"
364 inst_rule_group_owner "$_found"
365 inst_rule_initqueue "$_found"
366 inst_simple "$_found"
369 for r in '' $dracutbasedir/rules.d/; do
370 # skip rules without an absolute path
371 [[ "${r}$_rule" != /* ]] && continue
372 [[ -f ${r}$_rule ]] || continue
374 inst_rule_programs "$_found"
375 inst_rule_group_owner "$_found"
376 inst_rule_initqueue "$_found"
377 inst_simple "$_found" "$_target/${_found##*/}"
379 [[ $_found ]] || dinfo "Skipping udev rule
: $_rule"
383 inst_rules_wildcard() {
384 local _target=/etc/udev/rules.d _rule _found
386 inst_dir "${udevdir}/rules.d
"
388 for _rule in ${udevdir}/rules.d/$1 ${dracutbasedir}/rules.d/$1 ; do
389 [[ -e $_rule ]] || continue
390 inst_rule_programs "$_rule"
391 inst_rule_group_owner "$_rule"
392 inst_rule_initqueue "$_rule"
396 if [[ -n ${hostonly} ]] ; then
397 for _rule in ${_target}/$1 ; do
398 [[ -f $_rule ]] || continue
399 inst_rule_programs "$_rule"
400 inst_rule_group_owner "$_rule"
401 inst_rule_initqueue "$_rule"
406 [[ $_found ]] || dinfo "Skipping udev rule
: $_rule"
409 prepare_udev_rules() {
410 [ -z "$UDEVVERSION" ] && export UDEVVERSION=$(udevadm --version)
413 f="${initdir}/etc
/udev
/rules.d
/$f"
414 [ -e "$f" ] || continue
415 while read line || [ -n "$line" ]; do
416 if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
417 if [ $UDEVVERSION -ge 174 ]; then
418 printf '%sIMPORT{builtin}="path_id
"\n' "${line%%IMPORT PATH_ID}"
420 printf '%sIMPORT{program}="path_id
%%p
"\n' "${line%%IMPORT PATH_ID}"
422 elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
423 if [ $UDEVVERSION -ge 176 ]; then
424 printf '%sIMPORT{builtin}="blkid
"\n' "${line%%IMPORT BLKID}"
426 printf '%sIMPORT{program}="/sbin
/blkid
-o udev
-p $tempnode"\n' "${line%%IMPORT BLKID}"
431 done < "${f}" > "${f}.new
"
436 # install function specialized for hooks
437 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
438 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
440 if ! [[ -f $3 ]]; then
441 dfatal "Cannot
install a hook
($3) that does not exist.
"
442 dfatal "Aborting initrd creation.
"
444 elif ! [[ "$hookdirs" == *$1* ]]; then
445 dfatal "No such hook
type $1. Aborting initrd creation.
"
448 inst_simple "$3" "/lib
/dracut
/hooks
/${1}/${2}-${3##*/}"
451 # install any of listed files
453 # If first argument is '-d' and second some destination path, first accessible
454 # source is installed into this path, otherwise it will installed in the same
455 # path as source. If none of listed files was installed, function return 1.
456 # On first successful installation it returns with 0 status.
460 # inst_any -d /bin/foo /bin/bar /bin/baz
462 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
467 [[ $1 = '-d' ]] && to="$2" && shift 2
470 [[ -e $f ]] || continue
471 [[ $to ]] && inst "$f" "$to" && return 0
472 inst "$f" && return 0
479 # inst_libdir_file [-n <pattern>] <file> [<file>...]
480 # Install a <file> located on a lib directory to the initramfs image
481 # -n <pattern> install matching files
484 if [[ "$1" == "-n" ]]; then
487 for _dir in $libdirs; do
489 for _f in "$_dir"/$_i; do
490 [[ "$_f" =~ $_pattern ]] || continue
491 [[ -e "$_f" ]] && _files+="$_f "
496 for _dir in $libdirs; do
498 for _f in "$_dir"/$_i; do
499 [[ -e "$_f" ]] && _files+="$_f "
504 [[ $_files ]] && inst_multiple $_files
508 # install function decompressing the target and handling symlinks
509 # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
511 # Function install targets in the same paths inside overlay but decompressed
512 # and without extensions (.gz, .bz2).
519 *.gz) _cmd='gzip -f -d' ;;
520 *.bz2) _cmd='bzip2 -d' ;;
524 # Decompress with chosen tool. We assume that tool changes name e.g.
525 # from 'name.gz' to 'name'.
526 ${_cmd} "${initdir}${_src}"
530 # It's similar to above, but if file is not compressed, performs standard
533 inst_opt_decompress
() {
537 inst_decompress
"${_src}" || inst
"${_src}"
541 # module_check <dracut module>
542 # execute the check() function of module-setup.sh of <dracut module>
543 # or the "check" script, if module-setup.sh is not found
544 # "check $hostonly" is called
546 local _moddir
=$
(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
549 local _hostonly
=$hostonly
550 [ $# -eq 2 ] && _forced
=$2
551 [[ -d $_moddir ]] ||
return 1
552 if [[ ! -f $_moddir/module-setup.sh
]]; then
553 # if we do not have a check script, we are unconditionally included
554 [[ -x $_moddir/check
]] ||
return 0
555 [ $_forced -ne 0 ] && unset hostonly
556 $_moddir/check
$hostonly
559 unset check depends cmdline
install installkernel
561 .
$_moddir/module-setup.sh
562 is_func check ||
return 0
563 [ $_forced -ne 0 ] && unset hostonly
564 moddir
=$_moddir check
$hostonly
566 unset check depends cmdline
install installkernel
572 # module_check_mount <dracut module>
573 # execute the check() function of module-setup.sh of <dracut module>
574 # or the "check" script, if module-setup.sh is not found
575 # "mount_needs=1 check 0" is called
576 module_check_mount
() {
577 local _moddir
=$
(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
580 [[ -d $_moddir ]] ||
return 1
581 if [[ ! -f $_moddir/module-setup.sh
]]; then
582 # if we do not have a check script, we are unconditionally included
583 [[ -x $_moddir/check
]] ||
return 0
584 mount_needs
=1 $_moddir/check
0
587 unset check depends cmdline
install installkernel
589 .
$_moddir/module-setup.sh
590 moddir
=$_moddir check
0
592 unset check depends cmdline
install installkernel
598 # module_depends <dracut module>
599 # execute the depends() function of module-setup.sh of <dracut module>
600 # or the "depends" script, if module-setup.sh is not found
602 local _moddir
=$
(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
604 [[ -d $_moddir ]] ||
return 1
605 if [[ ! -f $_moddir/module-setup.sh
]]; then
606 # if we do not have a check script, we have no deps
607 [[ -x $_moddir/check
]] ||
return 0
611 unset check depends cmdline
install installkernel
613 .
$_moddir/module-setup.sh
614 moddir
=$_moddir depends
616 unset check depends cmdline
install installkernel
621 # module_cmdline <dracut module>
622 # execute the cmdline() function of module-setup.sh of <dracut module>
623 # or the "cmdline" script, if module-setup.sh is not found
625 local _moddir
=$
(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
627 [[ -d $_moddir ]] ||
return 1
628 if [[ ! -f $_moddir/module-setup.sh
]]; then
629 [[ -x $_moddir/cmdline
]] && .
"$_moddir/cmdline"
632 unset check depends cmdline
install installkernel
634 .
$_moddir/module-setup.sh
635 moddir
=$_moddir cmdline
637 unset check depends cmdline
install installkernel
642 # module_install <dracut module>
643 # execute the install() function of module-setup.sh of <dracut module>
644 # or the "install" script, if module-setup.sh is not found
646 local _moddir
=$
(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
648 [[ -d $_moddir ]] ||
return 1
649 if [[ ! -f $_moddir/module-setup.sh
]]; then
650 [[ -x $_moddir/install ]] && .
"$_moddir/install"
653 unset check depends cmdline
install installkernel
655 .
$_moddir/module-setup.sh
656 moddir
=$_moddir install
658 unset check depends cmdline
install installkernel
663 # module_installkernel <dracut module>
664 # execute the installkernel() function of module-setup.sh of <dracut module>
665 # or the "installkernel" script, if module-setup.sh is not found
666 module_installkernel
() {
667 local _moddir
=$
(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
669 [[ -d $_moddir ]] ||
return 1
670 if [[ ! -f $_moddir/module-setup.sh
]]; then
671 [[ -x $_moddir/installkernel
]] && .
"$_moddir/installkernel"
674 unset check depends cmdline
install installkernel
675 installkernel
() { true
; }
676 .
$_moddir/module-setup.sh
677 moddir
=$_moddir installkernel
679 unset check depends cmdline
install installkernel
684 # check_mount <dracut module>
685 # check_mount checks, if a dracut module is needed for the given
686 # device and filesystem types in "${host_fs_types[@]}"
689 local _moddir
=$
(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
693 [ "${#host_fs_types[@]}" -le 0 ] && return 1
695 # If we are already scheduled to be loaded, no need to check again.
696 [[ " $mods_to_load " == *\
$_mod\
* ]] && return 0
697 [[ " $mods_checked_as_dep " == *\
$_mod\
* ]] && return 1
699 # This should never happen, but...
700 [[ -d $_moddir ]] ||
return 1
702 [[ $2 ]] || mods_checked_as_dep
+=" $_mod "
704 if [[ " $omit_dracutmodules " == *\
$_mod\
* ]]; then
708 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\
$_mod\
* ]]; then
709 module_check_mount
$_mod; ret
=$?
711 # explicit module, so also accept ret=255
712 [[ $ret = 0 ||
$ret = 255 ]] ||
return 1
714 # module not in our list
715 if [[ $dracutmodules = all
]]; then
716 # check, if we can and should install this module
717 module_check_mount
$_mod ||
return 1
724 for _moddep
in $
(module_depends
$_mod); do
725 # handle deps as if they were manually added
726 [[ " $dracutmodules " == *\
$_mod\
* ]] \
727 && [[ " $dracutmodules " != *\
$_moddep\
* ]] \
728 && dracutmodules
+=" $_moddep "
729 [[ " $add_dracutmodules " == *\
$_mod\
* ]] \
730 && [[ " $add_dracutmodules " != *\
$_moddep\
* ]] \
731 && add_dracutmodules
+=" $_moddep "
732 [[ " $force_add_dracutmodules " == *\
$_mod\
* ]] \
733 && [[ " $force_add_dracutmodules " != *\
$_moddep\
* ]] \
734 && force_add_dracutmodules
+=" $_moddep "
735 # if a module we depend on fail, fail also
736 if ! check_module
$_moddep; then
737 derror
"dracut module '$_mod' depends on '$_moddep', which can't be installed"
742 [[ " $mods_to_load " == *\
$_mod\
* ]] || \
743 mods_to_load
+=" $_mod "
748 # check_module <dracut module> [<use_as_dep>]
749 # check if a dracut module is to be used in the initramfs process
750 # if <use_as_dep> is set, then the process also keeps track
751 # that the modules were checked for the dependency tracking process
754 local _moddir
=$
(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
757 # If we are already scheduled to be loaded, no need to check again.
758 [[ " $mods_to_load " == *\
$_mod\
* ]] && return 0
759 [[ " $mods_checked_as_dep " == *\
$_mod\
* ]] && return 1
761 # This should never happen, but...
762 [[ -d $_moddir ]] ||
return 1
764 [[ $2 ]] || mods_checked_as_dep
+=" $_mod "
766 if [[ " $omit_dracutmodules " == *\
$_mod\
* ]]; then
767 dinfo
"dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
771 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\
$_mod\
* ]]; then
772 if [[ " $dracutmodules $force_add_dracutmodules " == *\
$_mod\
* ]]; then
773 module_check
$_mod 1; ret
=$?
775 module_check
$_mod 0; ret
=$?
777 # explicit module, so also accept ret=255
778 [[ $ret = 0 ||
$ret = 255 ]] ||
return 1
780 # module not in our list
781 if [[ $dracutmodules = all
]]; then
782 # check, if we can and should install this module
783 module_check
$_mod; ret
=$?
784 if [[ $ret != 0 ]]; then
786 [[ $ret != 255 ]] && return 1
794 for _moddep
in $
(module_depends
$_mod); do
795 # handle deps as if they were manually added
796 [[ " $dracutmodules " == *\
$_mod\
* ]] \
797 && [[ " $dracutmodules " != *\
$_moddep\
* ]] \
798 && dracutmodules
+=" $_moddep "
799 [[ " $add_dracutmodules " == *\
$_mod\
* ]] \
800 && [[ " $add_dracutmodules " != *\
$_moddep\
* ]] \
801 && add_dracutmodules
+=" $_moddep "
802 [[ " $force_add_dracutmodules " == *\
$_mod\
* ]] \
803 && [[ " $force_add_dracutmodules " != *\
$_moddep\
* ]] \
804 && force_add_dracutmodules
+=" $_moddep "
805 # if a module we depend on fail, fail also
806 if ! check_module
$_moddep; then
807 derror
"dracut module '$_mod' depends on '$_moddep', which can't be installed"
812 [[ " $mods_to_load " == *\
$_mod\
* ]] || \
813 mods_to_load
+=" $_mod "
818 # for_each_module_dir <func>
819 # execute "<func> <dracut module> 1"
820 for_each_module_dir
() {
826 for _moddir
in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
827 [[ -d $_moddir ]] ||
continue;
828 [[ -e $_moddir/install ||
-e $_moddir/installkernel || \
829 -e $_moddir/module-setup.sh
]] ||
continue
830 _mod
=${_moddir##*/}; _mod
=${_mod#[0-9][0-9]}
834 # Report any missing dracut modules, the user has specified
835 _modcheck
="$add_dracutmodules $force_add_dracutmodules"
836 [[ $dracutmodules != all
]] && _modcheck
="$_modcheck $dracutmodules"
837 for _mod
in $_modcheck; do
838 [[ " $mods_to_load " == *\
$_mod\
* ]] && continue
840 [[ " $force_add_dracutmodules " != *\
$_mod\
* ]] \
841 && [[ " $dracutmodules " != *\
$_mod\
* ]] \
842 && [[ " $omit_dracutmodules " == *\
$_mod\
* ]] \
845 derror
"dracut module '$_mod' cannot be found or installed."
846 [[ " $force_add_dracutmodules " == *\
$_mod\
* ]] && exit 1
847 [[ " $dracutmodules " == *\
$_mod\
* ]] && exit 1
848 [[ " $add_dracutmodules " == *\
$_mod\
* ]] && exit 1
852 # Install a single kernel module along with any firmware it may require.
853 # $1 = full path to kernel module to install
854 install_kmod_with_fw
() {
855 # no need to go further if the module is already installed
857 [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
860 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -e "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}" ]]; then
861 read ret
< "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
865 if [[ $omit_drivers ]]; then
869 if [[ "$_kmod" =~
$omit_drivers ]]; then
870 dinfo
"Omitting driver $_kmod"
873 if [[ "${1##*/lib/modules/$kernel/}" =~
$omit_drivers ]]; then
874 dinfo
"Omitting driver $_kmod"
879 if [[ $silent_omit_drivers ]]; then
883 [[ "$_kmod" =~
$silent_omit_drivers ]] && return 0
884 [[ "${1##*/lib/modules/$kernel/}" =~
$silent_omit_drivers ]] && return 0
887 inst_simple
"$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
889 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
890 [[ -d "$DRACUT_KERNEL_LAZY_HASHDIR" ]] && \
891 echo $ret > "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
892 (($ret != 0)) && return $ret
894 local _modname
=${1##*/} _fwdir _found _fw
895 _modname
=${_modname%.ko*}
896 for _fw
in $
(modinfo
-k $kernel -F firmware
$1 2>/dev
/null
); do
898 for _fwdir
in $fw_dir; do
899 [[ -d $_fwdir && -f $_fwdir/$_fw ]] ||
continue
900 inst_simple
"$_fwdir/$_fw" "/lib/firmware/$_fw"
903 if [[ $_found != yes ]]; then
904 if ! [[ -d $
(echo /sys
/module
/${_modname//-/_}|
{ read a b
; echo $a; }) ]]; then
905 dinfo
"Possible missing firmware \"${_fw}\" for kernel module" \
908 dwarn
"Possible missing firmware \"${_fw}\" for kernel module" \
916 # Do something with all the dependencies of a kernel module.
917 # Note that kernel modules depend on themselves using the technique we use
918 # $1 = function to call for each dependency we find
919 # It will be passed the full path to the found kernel module
920 # $2 = module to get dependencies for
921 # rest of args = arguments to modprobe
922 # _fderr specifies FD passed from surrounding scope
923 for_each_kmod_dep
() {
924 local _func
=$1 _kmod
=$2 _cmd _modpath _options
926 modprobe
"$@" --ignore-install --show-depends $_kmod 2>&${_fderr} |
(
927 while read _cmd _modpath _options ||
[ -n "$_cmd" ]; do
928 [[ $_cmd = insmod
]] ||
continue
929 $_func ${_modpath} ||
exit $?
934 dracut_kernel_post
() {
935 local _moddirname
=${srcmods%%/lib/modules/*}
938 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" ]]; then
939 xargs -r modprobe
-a ${_moddirname:+-d ${_moddirname}/} \
940 --ignore-install --show-depends --set-version $kernel \
941 < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" 2>/dev
/null \
943 |
while read _cmd _modpath _options ||
[ -n "$_cmd" ]; do
944 [[ $_cmd = insmod
]] ||
continue
946 done > "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
949 if [[ $DRACUT_INSTALL ]] && [[ -z $_moddirname ]]; then
950 xargs -r $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} -a < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
952 while read _modpath ||
[ -n "$_modpath" ]; do
953 local _destpath
=$_modpath
954 [[ $_moddirname ]] && _destpath
=${_destpath##$_moddirname/}
955 _destpath
=${_destpath##*/lib/modules/$kernel/}
956 inst_simple
"$_modpath" "/lib/modules/$kernel/${_destpath}" ||
exit $?
957 done < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
960 _pid
=$
(jobs -p |
while read a ||
[ -n "$a" ]; do printf ":$a";done)
963 if [[ $DRACUT_INSTALL ]]; then
964 xargs -r modinfo
-k $kernel -F firmware
< "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" \
965 |
while read line ||
[ -n "$line" ]; do
966 for _fwdir
in $fw_dir; do
969 done |
xargs -r $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} -a -o
971 for _fw
in $
(xargs -r modinfo
-k $kernel -F firmware
< "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"); do
972 for _fwdir
in $fw_dir; do
973 [[ -d $_fwdir && -f $_fwdir/$_fw ]] ||
continue
974 inst_simple
"$_fwdir/$_fw" "/lib/firmware/$_fw"
983 for _f
in modules.
builtin.bin modules.
builtin modules.order
; do
984 [[ $srcmods/$_f ]] && inst_simple
"$srcmods/$_f" "/lib/modules/$kernel/$_f"
987 # generate module dependencies for the initrd
988 if [[ -d $initdir/lib
/modules
/$kernel ]] && \
989 ! depmod
-a -b "$initdir" $kernel; then
990 dfatal
"\"depmod -a $kernel\" failed."
994 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && rm -fr -- "$DRACUT_KERNEL_LAZY_HASHDIR"
997 [[ "$kernel_current" ]] ||
export kernel_current
=$
(uname
-r)
999 module_is_host_only
() {
1001 local _modenc a i _k _s _v _aliases
1004 _modenc
=${_mod//-/_}
1006 [[ " $add_drivers " == *\
${_mod}\
* ]] && return 0
1008 # check if module is loaded
1009 [[ ${host_modules["$_modenc"]} ]] && return 0
1011 [[ "$kernel_current" ]] ||
export kernel_current
=$
(uname
-r)
1013 if [[ "$kernel_current" != "$kernel" ]]; then
1014 # check if module is loadable on the current kernel
1015 # this covers the case, where a new module is introduced
1016 # or a module was renamed
1017 # or a module changed from builtin to a module
1019 if [[ -d /lib
/modules
/$kernel_current ]]; then
1020 # if the modinfo can be parsed, but the module
1021 # is not loaded, then we can safely return 1
1022 modinfo
-F filename
"$_mod" &>/dev
/null
&& return 1
1025 # just install the module, better safe than sorry
1032 find_kernel_modules_by_path
() {
1035 [[ -f "$srcmods/modules.dep" ]] ||
return 0
1039 while read a rest ||
[ -n "$a" ]; do
1040 [[ $a = */$1/* ]] ||
[[ $a = updates
/* ]] ||
continue
1041 printf "%s\n" "$srcmods/$a"
1042 done < "$srcmods/modules.dep"
1047 find_kernel_modules
() {
1048 find_kernel_modules_by_path drivers
1051 # instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1052 # instmods [-c [-s]] <kernel subsystem>
1053 # install kernel modules along with all their dependencies.
1054 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1056 [[ $no_kernel = yes ]] && return
1057 # called [sub]functions inherit _fderr
1061 if [[ $1 = '-c' ]]; then
1066 if [[ $1 = '-s' ]]; then
1071 function inst1mod
() {
1072 local _ret
=0 _mod
="$1"
1075 ( [[ "$_mpargs" ]] && echo $_mpargs
1076 find_kernel_modules_by_path
"${_mod#=}" ) \
1080 --*) _mpargs
+=" $_mod" ;;
1083 # Check for aliased modules
1084 _modalias
=$
(modinfo
-k $kernel -F filename
$_mod 2> /dev
/null
)
1085 _modalias
=${_modalias%.ko*}
1086 if [[ $_modalias ]] && [ "${_modalias##*/}" != "${_mod%.ko*}" ] ; then
1087 _mod
=${_modalias##*/}
1090 # if we are already installed, skip this module and go on
1092 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1093 [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko*}" ]]; then
1094 read _ret
<"$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko*}"
1099 if [[ $omit_drivers ]] && [[ "$_mod" =~
$omit_drivers ]]; then
1100 dinfo
"Omitting driver ${_mod##$srcmods}"
1104 # If we are building a host-specific initramfs and this
1105 # module is not already loaded, move on to the next one.
1107 && ! module_is_host_only
"$_mod" \
1110 if [[ "$_check" = "yes" ]] ||
! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
1111 # We use '-d' option in modprobe only if modules prefix path
1112 # differs from default '/'. This allows us to use dracut with
1113 # old version of modprobe which doesn't have '-d' option.
1114 local _moddirname
=${srcmods%%/lib/modules/*}
1115 [[ -n ${_moddirname} ]] && _moddirname
="-d ${_moddirname}/"
1117 # ok, load the module, all its dependencies, and any firmware
1119 for_each_kmod_dep install_kmod_with_fw
$_mod \
1120 --set-version $kernel ${_moddirname} $_mpargs
1123 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1124 echo ${_mod%.ko*} >> "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist"
1131 function instmods_1
() {
1133 if (($# == 0)); then # filenames from stdin
1134 while read _mod ||
[ -n "$_mod" ]; do
1135 inst1mod
"${_mod%.ko*}" ||
{
1136 if [[ "$_check" == "yes" ]] && [[ "$_silent" == "no" ]]; then
1137 dfatal
"Failed to install module $_mod"
1142 while (($# > 0)); do # filenames as arguments
1143 inst1mod
${1%.ko*} ||
{
1144 if [[ "$_check" == "yes" ]] && [[ "$_silent" == "no" ]]; then
1145 dfatal
"Failed to install module $1"
1153 local _ret _filter_not_found
='FATAL: Module .* not found.'
1154 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1155 # redirections, but that would make dracut require bash4 at least.
1156 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1157 |
while read line ||
[ -n "$line" ]; do [[ "$line" =~
$_filter_not_found ]] ||
echo $line;done | derror