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/>.
22 if [[ "$EUID" = "0" ]]; then
23 export DRACUT_CP
="cp --reflink=auto --sparse=auto --preserve=mode,timestamps,xattr,links -dfr"
25 export DRACUT_CP
="cp --reflink=auto --sparse=auto --preserve=mode,timestamps,links -dfr"
29 # Check whether $1 is a function.
31 [[ "$(type -t "$1")" = "function" ]]
34 if ! [[ $dracutbasedir ]]; then
35 dracutbasedir
=${BASH_SOURCE[0]%/*}
36 [[ $dracutbasedir = dracut-functions
* ]] && dracutbasedir
="."
37 [[ $dracutbasedir ]] || dracutbasedir
="."
38 dracutbasedir
="$(readlink -f $dracutbasedir)"
41 if ! is_func dinfo
>/dev
/null
2>&1; then
42 .
"$dracutbasedir/dracut-logger.sh"
46 if ! [[ $initdir ]]; then
47 dfatal
"initdir not set"
51 if ! [[ -d $initdir ]]; then
55 if ! [[ $kernel ]]; then
60 srcmods
="/lib/modules/$kernel/"
62 [[ $drivers_dir ]] && {
63 if ! command -v kmod
&>/dev
/null
&& vercmp
"$(modprobe --version | cut -d' ' -f3)" lt
3.7; then
64 dfatal
'To use --kmoddir option module-init-tools >= 3.7 is required.'
67 srcmods
="$drivers_dir"
71 [[ $DRACUT_FIRMWARE_PATH ]] ||
export DRACUT_FIRMWARE_PATH
="/lib/firmware/updates:/lib/firmware:/lib/firmware/$kernel"
73 # export standard hookdirs
75 hookdirs
="cmdline pre-udev pre-trigger netroot "
76 hookdirs
+="initqueue initqueue/settled initqueue/online initqueue/finished initqueue/timeout "
77 hookdirs
+="pre-mount pre-pivot cleanup mount "
78 hookdirs
+="emergency shutdown-emergency pre-shutdown shutdown "
82 .
$dracutbasedir/dracut-functions.sh
85 if ! [[ $libdirs ]] ; then
86 if [[ "$(ldd /bin/sh)" == */lib64
/* ]] &>/dev
/null \
87 && [[ -d /lib64
]]; then
89 [[ -d /usr
/lib64
]] && libdirs
+=" /usr/lib64"
92 [[ -d /usr
/lib
]] && libdirs
+=" /usr/lib"
95 libdirs
+=" $(ldconfig_paths)"
100 # helper function for check() in module-setup.sh
101 # to check for required installed binaries
102 # issues a standardized warning message
104 local _module_name
="${moddir##*/}"
107 if [[ "$1" = "-m" ]]; then
113 if ! find_binary
"$cmd" &>/dev
/null
; then
114 dinfo
"dracut module '${_module_name#[0-9][0-9]}' will not be installed, because command '$cmd' could not be found!"
121 require_any_binary
() {
122 local _module_name
="${moddir##*/}"
125 if [[ "$1" = "-m" ]]; then
131 if find_binary
"$cmd" &>/dev
/null
; then
137 if (( $_ret != 0 )); then
138 dinfo
"$_module_name: Could not find any command of '$@'!"
145 dracut_need_initqueue
() {
146 >"$initdir/lib/dracut/need-initqueue"
149 dracut_module_included
() {
150 [[ " $mods_to_load $modules_loaded " == *\ $
*\
* ]]
153 dracut_no_switch_root
() {
154 >"$initdir/lib/dracut/no-switch-root"
157 dracut_module_path
() {
158 echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; }
161 if ! [[ $DRACUT_INSTALL ]]; then
162 DRACUT_INSTALL
=$
(find_binary dracut-install
)
165 if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install
]]; then
166 DRACUT_INSTALL
=$dracutbasedir/dracut-install
167 elif ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/install
/dracut-install
]]; then
168 DRACUT_INSTALL
=$dracutbasedir/install
/dracut-install
171 if ! [[ -x $DRACUT_INSTALL ]]; then
172 dfatal
"dracut-install not found!"
176 if [[ $hostonly == "-h" ]]; then
177 if ! [[ $DRACUT_KERNEL_MODALIASES ]] ||
! [[ -f "$DRACUT_KERNEL_MODALIASES" ]]; then
178 export DRACUT_KERNEL_MODALIASES
="${DRACUT_TMPDIR}/modaliases"
179 $DRACUT_INSTALL ${srcmods:+--kerneldir "$srcmods"} --modalias > "$DRACUT_KERNEL_MODALIASES"
183 [[ $DRACUT_RESOLVE_LAZY ]] ||
export DRACUT_RESOLVE_DEPS
=1
185 [[ -e ${initdir}/"$1" ]] && return 0 # already there
186 $DRACUT_INSTALL ${initdir:+-D "$initdir"} -d "$@"
187 (($?
!= 0)) && derror FAILED
: $DRACUT_INSTALL ${initdir:+-D "$initdir"} -d "$@" ||
:
191 local _hostonly_install
192 if [[ "$1" == "-H" ]]; then
193 _hostonly_install
="-H"
196 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
197 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
198 (($?
!= 0)) && derror FAILED
: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" ||
:
202 local _hostonly_install
203 if [[ "$1" == "-H" ]]; then
204 _hostonly_install
="-H"
207 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
208 [[ -e $1 ]] ||
return 1 # no source
209 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${_hostonly_install:+-H} "$@"
210 (($?
!= 0)) && derror FAILED
: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${_hostonly_install:+-H} "$@" ||
:
214 local _hostonly_install
215 if [[ "$1" == "-H" ]]; then
216 _hostonly_install
="-H"
219 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
220 [[ -L $1 ]] ||
return 1
221 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
222 (($?
!= 0)) && derror FAILED
: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" ||
:
227 $DRACUT_INSTALL ${initdir:+-D "$initdir"} -a ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
229 (($_ret != 0)) && derror FAILED
: $DRACUT_INSTALL ${initdir:+-D "$initdir"} -a ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" ||
:
240 [[ $no_kernel = yes ]] && return
242 [[ $i == "--silent" ]] && _silent
=1
246 ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${hostonly:+-H} ${omit_drivers:+-N "$omit_drivers"} ${srcmods:+--kerneldir "$srcmods"} -m "$@"
247 (($?
!= 0)) && (($_silent == 0)) && derror FAILED
: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${hostonly:+-H} ${omit_drivers:+-N "$omit_drivers"} ${srcmods:+--kerneldir "$srcmods"} -m "$@" ||
:
251 local _hostonly_install
252 if [[ "$1" == "-H" ]]; then
253 _hostonly_install
="-H"
256 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
257 [[ -e $1 ]] ||
return 1 # no source
258 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
259 (($?
!= 0)) && derror FAILED
: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" ||
:
263 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
264 (($?
!= 0)) && derror FAILED
: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@" ||
:
268 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
269 (($?
!= 0)) && derror FAILED
: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@" ||
:
273 local _helper
="/run/dracut/fsck/fsck_help_$1.txt"
274 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$2" $_helper
275 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$2" $_helper ||
:
278 # Use with form hostonly="$(optional_hostonly)" inst_xxxx <args>
279 # If hosotnly mode is set to "strict", hostonly restrictions will still
280 # be applied, else will ignore hostonly mode and try to install all
282 optional_hostonly
() {
283 if [[ $hostonly_mode = "strict" ]]; then
284 printf -- "$hostonly"
292 echo "$i" >> "$initdir/lib/dracut/hostonly-files"
296 # find symlinks linked to given library file
298 # Function searches for symlinks by stripping version numbers appended to
299 # library filename, checks if it points to the same target and finally
300 # prints the list of symlinks to stdout.
303 # rev_lib_symlinks libfoo.so.8.1
304 # output: libfoo.so.8 libfoo.so
305 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
307 [[ ! $1 ]] && return 0
309 local fn
="$1" orig
="$(readlink -f "$1")" links
=''
311 [[ ${fn} == *.so.
* ]] ||
return 1
313 until [[ ${fn##*.} == so
]]; do
315 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
321 # attempt to install any programs specified in a udev rule
322 inst_rule_programs() {
325 for _prog in $(sed -nr 's/.*PROGRAM==?"([^
"]+).*/\1/p' "$1"); do
327 if [ -x ${udevdir}/$_prog ]; then
328 _bin=${udevdir}/$_prog
329 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
330 _bin=$(find_binary "$_prog") || {
331 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
336 [[ $_bin ]] && inst_binary "$_bin"
338 for _prog in $(sed -nr 's/.*RUN[+=]=?"([^
"]+).*/\1/p' "$1"); do
340 if [ -x ${udevdir}/$_prog ]; then
341 _bin=${udevdir}/$_prog
342 elif [[ "${_prog/\$env\{/}" == "$_prog" ]] && [[ "${_prog}" != "/sbin
/initqueue
" ]]; then
343 _bin=$(find_binary "$_prog") || {
344 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
349 [[ $_bin ]] && inst_binary "$_bin"
351 for _prog in $(sed -nr 's/.*IMPORT\{program\}==?"([^
"]+).*/\1/p' "$1"); do
353 if [ -x ${udevdir}/$_prog ]; then
354 _bin=${udevdir}/$_prog
355 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
356 _bin=$(find_binary "$_prog") || {
357 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
362 [[ $_bin ]] && dracut_install "$_bin"
366 # attempt to install any programs specified in a udev rule
367 inst_rule_group_owner() {
370 for i in $(sed -nr 's/.*OWNER=?"([^
"]+).*/\1/p' "$1"); do
371 if ! grep -Eq "^
$i:" "$initdir/etc
/passwd
" 2>/dev/null; then
372 grep -E "^
$i:" /etc/passwd 2>/dev/null >> "$initdir/etc
/passwd
"
375 for i in $(sed -nr 's/.*GROUP=?"([^
"]+).*/\1/p' "$1"); do
376 if ! grep -Eq "^
$i:" "$initdir/etc
/group
" 2>/dev/null; then
377 grep -E "^
$i:" /etc/group 2>/dev/null >> "$initdir/etc
/group
"
382 inst_rule_initqueue() {
383 if grep -q -F initqueue "$1"; then
384 dracut_need_initqueue
388 # udev rules always get installed in the same place, so
389 # create a function to install them to make life simpler.
391 local _target=/etc/udev/rules.d _rule _found
393 inst_dir "${udevdir}/rules.d
"
395 for _rule in "$@
"; do
396 if [ "${_rule#/}" = "$_rule" ]; then
397 for r in ${udevdir}/rules.d ${hostonly:+/etc/udev/rules.d}; do
398 [[ -e $r/$_rule ]] || continue
400 inst_rule_programs "$_found"
401 inst_rule_group_owner "$_found"
402 inst_rule_initqueue "$_found"
403 inst_simple "$_found"
406 for r in '' $dracutbasedir/rules.d/; do
407 # skip rules without an absolute path
408 [[ "${r}$_rule" != /* ]] && continue
409 [[ -f ${r}$_rule ]] || continue
411 inst_rule_programs "$_found"
412 inst_rule_group_owner "$_found"
413 inst_rule_initqueue "$_found"
414 inst_simple "$_found" "$_target/${_found##*/}"
416 [[ $_found ]] || dinfo "Skipping udev rule
: $_rule"
420 inst_rules_wildcard() {
421 local _target=/etc/udev/rules.d _rule _found
423 inst_dir "${udevdir}/rules.d
"
425 for _rule in ${udevdir}/rules.d/$1 ${dracutbasedir}/rules.d/$1 ; do
426 [[ -e $_rule ]] || continue
427 inst_rule_programs "$_rule"
428 inst_rule_group_owner "$_rule"
429 inst_rule_initqueue "$_rule"
433 if [[ -n ${hostonly} ]] ; then
434 for _rule in ${_target}/$1 ; do
435 [[ -f $_rule ]] || continue
436 inst_rule_programs "$_rule"
437 inst_rule_group_owner "$_rule"
438 inst_rule_initqueue "$_rule"
443 [[ $_found ]] || dinfo "Skipping udev rule
: $_rule"
446 prepare_udev_rules() {
447 [ -z "$UDEVVERSION" ] && export UDEVVERSION=$(udevadm --version)
450 f="${initdir}/etc
/udev
/rules.d
/$f"
451 [ -e "$f" ] || continue
452 while read line || [ -n "$line" ]; do
453 if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
454 if [ $UDEVVERSION -ge 174 ]; then
455 printf '%sIMPORT{builtin}="path_id
"\n' "${line%%IMPORT PATH_ID}"
457 printf '%sIMPORT{program}="path_id
%%p
"\n' "${line%%IMPORT PATH_ID}"
459 elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
460 if [ $UDEVVERSION -ge 176 ]; then
461 printf '%sIMPORT{builtin}="blkid
"\n' "${line%%IMPORT BLKID}"
463 printf '%sIMPORT{program}="/sbin
/blkid
-o udev
-p $tempnode"\n' "${line%%IMPORT BLKID}"
468 done < "${f}" > "${f}.new
"
473 # install function specialized for hooks
474 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
475 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
477 if ! [[ -f $3 ]]; then
478 dfatal "Cannot
install a hook
($3) that does not exist.
"
479 dfatal "Aborting initrd creation.
"
481 elif ! [[ "$hookdirs" == *$1* ]]; then
482 dfatal "No such hook
type $1. Aborting initrd creation.
"
485 inst_simple "$3" "/lib
/dracut
/hooks
/${1}/${2}-${3##*/}"
488 # install any of listed files
490 # If first argument is '-d' and second some destination path, first accessible
491 # source is installed into this path, otherwise it will installed in the same
492 # path as source. If none of listed files was installed, function return 1.
493 # On first successful installation it returns with 0 status.
497 # inst_any -d /bin/foo /bin/bar /bin/baz
499 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
504 [[ $1 = '-d' ]] && to="$2" && shift 2
507 [[ -e $f ]] || continue
508 [[ $to ]] && inst "$f" "$to" && return 0
509 inst "$f" && return 0
516 # inst_libdir_file [-n <pattern>] <file> [<file>...]
517 # Install a <file> located on a lib directory to the initramfs image
518 # -n <pattern> install matching files
521 if [[ "$1" == "-n" ]]; then
524 for _dir in $libdirs; do
526 for _f in "$_dir"/$_i; do
527 [[ "$_f" =~ $_pattern ]] || continue
528 [[ -e "$_f" ]] && _files+="$_f "
533 for _dir in $libdirs; do
535 for _f in "$_dir"/$_i; do
536 [[ -e "$_f" ]] && _files+="$_f "
541 [[ $_files ]] && inst_multiple $_files
545 # install function decompressing the target and handling symlinks
546 # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
548 # Function install targets in the same paths inside overlay but decompressed
549 # and without extensions (.gz, .bz2).
556 *.gz) _cmd='gzip -f -d' ;;
557 *.bz2) _cmd='bzip2 -d' ;;
561 # Decompress with chosen tool. We assume that tool changes name e.g.
562 # from 'name.gz' to 'name'.
563 ${_cmd} "${initdir}${_src}"
567 # It's similar to above, but if file is not compressed, performs standard
570 inst_opt_decompress
() {
574 inst_decompress
"${_src}" || inst
"${_src}"
578 # module_check <dracut module> [<forced>] [<module path>]
579 # execute the check() function of module-setup.sh of <dracut module>
580 # or the "check" script, if module-setup.sh is not found
581 # "check $hostonly" is called
586 local _hostonly
=$hostonly
587 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
588 [ $# -eq 2 ] && _forced
=$2
589 [[ -d $_moddir ]] ||
return 1
590 if [[ ! -f $_moddir/module-setup.sh
]]; then
591 # if we do not have a check script, we are unconditionally included
592 [[ -x $_moddir/check
]] ||
return 0
593 [ $_forced -ne 0 ] && unset hostonly
594 $_moddir/check
$hostonly
597 unset check depends cmdline
install installkernel
599 .
$_moddir/module-setup.sh
600 is_func check ||
return 0
601 [ $_forced -ne 0 ] && unset hostonly
602 moddir
=$_moddir check
$hostonly
604 unset check depends cmdline
install installkernel
610 # module_check_mount <dracut module> [<module path>]
611 # execute the check() function of module-setup.sh of <dracut module>
612 # or the "check" script, if module-setup.sh is not found
613 # "mount_needs=1 check 0" is called
614 module_check_mount
() {
618 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
619 [[ -d $_moddir ]] ||
return 1
620 if [[ ! -f $_moddir/module-setup.sh
]]; then
621 # if we do not have a check script, we are unconditionally included
622 [[ -x $_moddir/check
]] ||
return 0
623 mount_needs
=1 $_moddir/check
0
626 unset check depends cmdline
install installkernel
628 .
$_moddir/module-setup.sh
629 moddir
=$_moddir check
0
631 unset check depends cmdline
install installkernel
637 # module_depends <dracut module> [<module path>]
638 # execute the depends() function of module-setup.sh of <dracut module>
639 # or the "depends" script, if module-setup.sh is not found
643 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
644 [[ -d $_moddir ]] ||
return 1
645 if [[ ! -f $_moddir/module-setup.sh
]]; then
646 # if we do not have a check script, we have no deps
647 [[ -x $_moddir/check
]] ||
return 0
651 unset check depends cmdline
install installkernel
653 .
$_moddir/module-setup.sh
654 moddir
=$_moddir depends
656 unset check depends cmdline
install installkernel
661 # module_cmdline <dracut module> [<module path>]
662 # execute the cmdline() function of module-setup.sh of <dracut module>
663 # or the "cmdline" script, if module-setup.sh is not found
667 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
668 [[ -d $_moddir ]] ||
return 1
669 if [[ ! -f $_moddir/module-setup.sh
]]; then
670 [[ -x $_moddir/cmdline
]] && .
"$_moddir/cmdline"
673 unset check depends cmdline
install installkernel
675 .
$_moddir/module-setup.sh
676 moddir
=$_moddir cmdline
678 unset check depends cmdline
install installkernel
683 # module_install <dracut module> [<module path>]
684 # execute the install() function of module-setup.sh of <dracut module>
685 # or the "install" script, if module-setup.sh is not found
689 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
690 [[ -d $_moddir ]] ||
return 1
691 if [[ ! -f $_moddir/module-setup.sh
]]; then
692 [[ -x $_moddir/install ]] && .
"$_moddir/install"
695 unset check depends cmdline
install installkernel
697 .
$_moddir/module-setup.sh
698 moddir
=$_moddir install
700 unset check depends cmdline
install installkernel
705 # module_installkernel <dracut module> [<module path>]
706 # execute the installkernel() function of module-setup.sh of <dracut module>
707 # or the "installkernel" script, if module-setup.sh is not found
708 module_installkernel
() {
711 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
712 [[ -d $_moddir ]] ||
return 1
713 if [[ ! -f $_moddir/module-setup.sh
]]; then
714 [[ -x $_moddir/installkernel
]] && .
"$_moddir/installkernel"
717 unset check depends cmdline
install installkernel
718 installkernel
() { true
; }
719 .
$_moddir/module-setup.sh
720 moddir
=$_moddir installkernel
722 unset check depends cmdline
install installkernel
727 # check_mount <dracut module> [<use_as_dep>] [<module path>]
728 # check_mount checks, if a dracut module is needed for the given
729 # device and filesystem types in "${host_fs_types[@]}"
736 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
737 [ "${#host_fs_types[@]}" -le 0 ] && return 1
739 # If we are already scheduled to be loaded, no need to check again.
740 [[ " $mods_to_load " == *\
$_mod\
* ]] && return 0
741 [[ " $mods_checked_as_dep " == *\
$_mod\
* ]] && return 1
743 # This should never happen, but...
744 [[ -d $_moddir ]] ||
return 1
746 [[ $2 ]] || mods_checked_as_dep
+=" $_mod "
748 if [[ " $omit_dracutmodules " == *\
$_mod\
* ]]; then
752 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\
$_mod\
* ]]; then
753 module_check_mount
$_mod $_moddir; ret
=$?
755 # explicit module, so also accept ret=255
756 [[ $ret = 0 ||
$ret = 255 ]] ||
return 1
758 # module not in our list
759 if [[ $dracutmodules = all
]]; then
760 # check, if we can and should install this module
761 module_check_mount
$_mod $_moddir ||
return 1
768 for _moddep
in $
(module_depends
$_mod $_moddir); do
769 # handle deps as if they were manually added
770 [[ " $dracutmodules " == *\
$_mod\
* ]] \
771 && [[ " $dracutmodules " != *\
$_moddep\
* ]] \
772 && dracutmodules
+=" $_moddep "
773 [[ " $add_dracutmodules " == *\
$_mod\
* ]] \
774 && [[ " $add_dracutmodules " != *\
$_moddep\
* ]] \
775 && add_dracutmodules
+=" $_moddep "
776 [[ " $force_add_dracutmodules " == *\
$_mod\
* ]] \
777 && [[ " $force_add_dracutmodules " != *\
$_moddep\
* ]] \
778 && force_add_dracutmodules
+=" $_moddep "
779 # if a module we depend on fail, fail also
780 if ! check_module
$_moddep; then
781 derror
"dracut module '$_mod' depends on '$_moddep', which can't be installed"
786 [[ " $mods_to_load " == *\
$_mod\
* ]] || \
787 mods_to_load
+=" $_mod "
792 # check_module <dracut module> [<use_as_dep>] [<module path>]
793 # check if a dracut module is to be used in the initramfs process
794 # if <use_as_dep> is set, then the process also keeps track
795 # that the modules were checked for the dependency tracking process
802 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
803 # If we are already scheduled to be loaded, no need to check again.
804 [[ " $mods_to_load " == *\
$_mod\
* ]] && return 0
805 [[ " $mods_checked_as_dep " == *\
$_mod\
* ]] && return 1
807 # This should never happen, but...
808 [[ -d $_moddir ]] ||
return 1
810 [[ $2 ]] || mods_checked_as_dep
+=" $_mod "
812 if [[ " $omit_dracutmodules " == *\
$_mod\
* ]]; then
813 dinfo
"dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
817 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\
$_mod\
* ]]; then
818 if [[ " $dracutmodules $force_add_dracutmodules " == *\
$_mod\
* ]]; then
819 module_check
$_mod 1 $_moddir; ret
=$?
821 module_check
$_mod 0 $_moddir; ret
=$?
823 # explicit module, so also accept ret=255
824 [[ $ret = 0 ||
$ret = 255 ]] ||
return 1
826 # module not in our list
827 if [[ $dracutmodules = all
]]; then
828 # check, if we can and should install this module
829 module_check
$_mod 0 $_moddir; ret
=$?
830 if [[ $ret != 0 ]]; then
832 [[ $ret != 255 ]] && return 1
840 for _moddep
in $
(module_depends
$_mod $_moddir); do
841 # handle deps as if they were manually added
842 [[ " $dracutmodules " == *\
$_mod\
* ]] \
843 && [[ " $dracutmodules " != *\
$_moddep\
* ]] \
844 && dracutmodules
+=" $_moddep "
845 [[ " $add_dracutmodules " == *\
$_mod\
* ]] \
846 && [[ " $add_dracutmodules " != *\
$_moddep\
* ]] \
847 && add_dracutmodules
+=" $_moddep "
848 [[ " $force_add_dracutmodules " == *\
$_mod\
* ]] \
849 && [[ " $force_add_dracutmodules " != *\
$_moddep\
* ]] \
850 && force_add_dracutmodules
+=" $_moddep "
851 # if a module we depend on fail, fail also
852 if ! check_module
$_moddep; then
853 derror
"dracut module '$_mod' depends on '$_moddep', which can't be installed"
858 [[ " $mods_to_load " == *\
$_mod\
* ]] || \
859 mods_to_load
+=" $_mod "
864 # for_each_module_dir <func>
865 # execute "<func> <dracut module> 1 <module path>"
866 for_each_module_dir
() {
872 for _moddir
in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
873 [[ -d $_moddir ]] ||
continue;
874 [[ -e $_moddir/install ||
-e $_moddir/installkernel || \
875 -e $_moddir/module-setup.sh
]] ||
continue
876 _mod
=${_moddir##*/}; _mod
=${_mod#[0-9][0-9]}
877 $_func $_mod 1 $_moddir
880 # Report any missing dracut modules, the user has specified
881 _modcheck
="$add_dracutmodules $force_add_dracutmodules"
882 [[ $dracutmodules != all
]] && _modcheck
="$_modcheck $dracutmodules"
883 for _mod
in $_modcheck; do
884 [[ " $mods_to_load " == *\
$_mod\
* ]] && continue
886 [[ " $force_add_dracutmodules " != *\
$_mod\
* ]] \
887 && [[ " $dracutmodules " != *\
$_mod\
* ]] \
888 && [[ " $omit_dracutmodules " == *\
$_mod\
* ]] \
891 derror
"dracut module '$_mod' cannot be found or installed."
892 [[ " $force_add_dracutmodules " == *\
$_mod\
* ]] && exit 1
893 [[ " $dracutmodules " == *\
$_mod\
* ]] && exit 1
894 [[ " $add_dracutmodules " == *\
$_mod\
* ]] && exit 1
898 # Install a single kernel module along with any firmware it may require.
899 # $1 = full path to kernel module to install
900 install_kmod_with_fw
() {
901 # no need to go further if the module is already installed
903 [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
906 if [[ $omit_drivers ]]; then
910 if [[ "$_kmod" =~
$omit_drivers ]]; then
911 dinfo
"Omitting driver $_kmod"
914 if [[ "${1##*/lib/modules/$kernel/}" =~
$omit_drivers ]]; then
915 dinfo
"Omitting driver $_kmod"
920 if [[ $silent_omit_drivers ]]; then
924 [[ "$_kmod" =~
$silent_omit_drivers ]] && return 0
925 [[ "${1##*/lib/modules/$kernel/}" =~
$silent_omit_drivers ]] && return 0
928 inst_simple
"$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
930 (($ret != 0)) && return $ret
932 local _modname
=${1##*/} _fwdir _found _fw
933 _modname
=${_modname%.ko*}
934 for _fw
in $
(modinfo
-k $kernel -F firmware
$1 2>/dev
/null
); do
936 for _fwdir
in $fw_dir; do
937 [[ -d $_fwdir ]] ||
continue
938 if [[ -f $_fwdir/$_fw ]]; then
939 inst_simple
"$_fwdir/$_fw" "/lib/firmware/$_fw"
940 elif [[ -f $_fwdir/$_fw.xz
]]; then
941 inst_simple
"$_fwdir/$_fw.xz" "/lib/firmware/$_fw.xz"
942 rm -f "${initdir}/lib/firmware/$_fw"
943 unxz
-f "${initdir}/lib/firmware/$_fw.xz"
949 if [[ $_found != yes ]]; then
950 if ! [[ -d $
(echo /sys
/module
/${_modname//-/_}|
{ read a b
; echo $a; }) ]]; then
951 dinfo
"Possible missing firmware \"${_fw}\" for kernel module" \
954 dwarn
"Possible missing firmware \"${_fw}\" for kernel module" \
962 # Do something with all the dependencies of a kernel module.
963 # Note that kernel modules depend on themselves using the technique we use
964 # $1 = function to call for each dependency we find
965 # It will be passed the full path to the found kernel module
966 # $2 = module to get dependencies for
967 # rest of args = arguments to modprobe
968 # _fderr specifies FD passed from surrounding scope
969 for_each_kmod_dep
() {
970 local _func
=$1 _kmod
=$2 _cmd _modpath _options
972 modprobe
"$@" --ignore-install --show-depends $_kmod 2>&${_fderr} |
(
973 while read _cmd _modpath _options ||
[ -n "$_cmd" ]; do
974 [[ $_cmd = insmod
]] ||
continue
975 $_func ${_modpath} ||
exit $?
980 dracut_kernel_post
() {
981 for _f
in modules.
builtin.bin modules.
builtin modules.order
; do
982 [[ -e $srcmods/$_f ]] && inst_simple
"$srcmods/$_f" "/lib/modules/$kernel/$_f"
985 # generate module dependencies for the initrd
986 if [[ -d $initdir/lib
/modules
/$kernel ]] && \
987 ! depmod
-a -b "$initdir" $kernel; then
988 dfatal
"\"depmod -a $kernel\" failed."
995 # instmods [-c [-s]] <kernel module> [<kernel module> ... ]
996 # instmods [-c [-s]] <kernel subsystem>
997 # install kernel modules along with all their dependencies.
998 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1001 local _optional
="-o"
1005 [[ $no_kernel = yes ]] && return
1007 if [[ $1 = '-c' ]]; then
1011 if [[ $1 = '-s' ]]; then
1016 if (($# == 0)); then
1017 read -r -d '' -a args
1021 if (($# == 0)); then
1026 ${initdir:+-D "$initdir"} \
1027 ${loginstall:+-L "$loginstall"} \
1029 ${omit_drivers:+-N "$omit_drivers"} \
1030 ${srcmods:+--kerneldir "$srcmods"} \
1032 ${_silent:+--silent} \
1036 if (($_ret != 0)) && [[ -z "$_silent" ]]; then
1039 ${initdir:+-D "$initdir"} \
1040 ${loginstall:+-L "$loginstall"} \
1042 ${omit_drivers:+-N "$omit_drivers"} \
1043 ${srcmods:+--kerneldir "$srcmods"} \
1045 ${_silent:+--silent} \
1049 [[ "$optional" ]] && return 0
1053 if [[ "$(ln --help)" == *--relative* ]]; then
1055 ln -sfnr "${initdir}/$1" "${initdir}/$2"
1061 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
1062 ln -sfn -- "$
(convert_abs_rel
"${_dest}" "${_source}")" "${initdir}/${_dest}"