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
544 # get a command to decompress the given file
545 get_decompress_cmd() {
547 *.gz) echo 'gzip -f -d' ;;
548 *.bz2) echo 'bzip2 -d' ;;
549 *.xz) echo 'xz -f -d' ;;
553 # install function decompressing the target and handling symlinks
554 # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
556 # Function install targets in the same paths inside overlay but decompressed
557 # and without extensions (.gz, .bz2).
563 _cmd=$(get_decompress_cmd ${_src})
564 [[ -z "${_cmd}" ]] && return 1
566 # Decompress with chosen tool. We assume that tool changes name e.g.
567 # from 'name.gz' to 'name'.
568 ${_cmd} "${initdir}${_src}"
572 # It's similar to above, but if file is not compressed, performs standard
575 inst_opt_decompress
() {
579 inst_decompress
"${_src}" || inst
"${_src}"
583 # module_check <dracut module> [<forced>] [<module path>]
584 # execute the check() function of module-setup.sh of <dracut module>
585 # or the "check" script, if module-setup.sh is not found
586 # "check $hostonly" is called
591 local _hostonly
=$hostonly
592 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
593 [ $# -eq 2 ] && _forced
=$2
594 [[ -d $_moddir ]] ||
return 1
595 if [[ ! -f $_moddir/module-setup.sh
]]; then
596 # if we do not have a check script, we are unconditionally included
597 [[ -x $_moddir/check
]] ||
return 0
598 [ $_forced -ne 0 ] && unset hostonly
599 $_moddir/check
$hostonly
602 unset check depends cmdline
install installkernel
604 .
$_moddir/module-setup.sh
605 is_func check ||
return 0
606 [ $_forced -ne 0 ] && unset hostonly
607 moddir
=$_moddir check
$hostonly
609 unset check depends cmdline
install installkernel
615 # module_check_mount <dracut module> [<module path>]
616 # execute the check() function of module-setup.sh of <dracut module>
617 # or the "check" script, if module-setup.sh is not found
618 # "mount_needs=1 check 0" is called
619 module_check_mount
() {
623 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
624 [[ -d $_moddir ]] ||
return 1
625 if [[ ! -f $_moddir/module-setup.sh
]]; then
626 # if we do not have a check script, we are unconditionally included
627 [[ -x $_moddir/check
]] ||
return 0
628 mount_needs
=1 $_moddir/check
0
631 unset check depends cmdline
install installkernel
633 .
$_moddir/module-setup.sh
634 moddir
=$_moddir check
0
636 unset check depends cmdline
install installkernel
642 # module_depends <dracut module> [<module path>]
643 # execute the depends() function of module-setup.sh of <dracut module>
644 # or the "depends" script, if module-setup.sh is not found
648 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
649 [[ -d $_moddir ]] ||
return 1
650 if [[ ! -f $_moddir/module-setup.sh
]]; then
651 # if we do not have a check script, we have no deps
652 [[ -x $_moddir/check
]] ||
return 0
656 unset check depends cmdline
install installkernel
658 .
$_moddir/module-setup.sh
659 moddir
=$_moddir depends
661 unset check depends cmdline
install installkernel
666 # module_cmdline <dracut module> [<module path>]
667 # execute the cmdline() function of module-setup.sh of <dracut module>
668 # or the "cmdline" script, if module-setup.sh is not found
672 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
673 [[ -d $_moddir ]] ||
return 1
674 if [[ ! -f $_moddir/module-setup.sh
]]; then
675 [[ -x $_moddir/cmdline
]] && .
"$_moddir/cmdline"
678 unset check depends cmdline
install installkernel
680 .
$_moddir/module-setup.sh
681 moddir
=$_moddir cmdline
683 unset check depends cmdline
install installkernel
688 # module_install <dracut module> [<module path>]
689 # execute the install() function of module-setup.sh of <dracut module>
690 # or the "install" script, if module-setup.sh is not found
694 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
695 [[ -d $_moddir ]] ||
return 1
696 if [[ ! -f $_moddir/module-setup.sh
]]; then
697 [[ -x $_moddir/install ]] && .
"$_moddir/install"
700 unset check depends cmdline
install installkernel
702 .
$_moddir/module-setup.sh
703 moddir
=$_moddir install
705 unset check depends cmdline
install installkernel
710 # module_installkernel <dracut module> [<module path>]
711 # execute the installkernel() function of module-setup.sh of <dracut module>
712 # or the "installkernel" script, if module-setup.sh is not found
713 module_installkernel
() {
716 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
717 [[ -d $_moddir ]] ||
return 1
718 if [[ ! -f $_moddir/module-setup.sh
]]; then
719 [[ -x $_moddir/installkernel
]] && .
"$_moddir/installkernel"
722 unset check depends cmdline
install installkernel
723 installkernel
() { true
; }
724 .
$_moddir/module-setup.sh
725 moddir
=$_moddir installkernel
727 unset check depends cmdline
install installkernel
732 # check_mount <dracut module> [<use_as_dep>] [<module path>]
733 # check_mount checks, if a dracut module is needed for the given
734 # device and filesystem types in "${host_fs_types[@]}"
741 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
742 [ "${#host_fs_types[@]}" -le 0 ] && return 1
744 # If we are already scheduled to be loaded, no need to check again.
745 [[ " $mods_to_load " == *\
$_mod\
* ]] && return 0
746 [[ " $mods_checked_as_dep " == *\
$_mod\
* ]] && return 1
748 # This should never happen, but...
749 [[ -d $_moddir ]] ||
return 1
751 [[ $2 ]] || mods_checked_as_dep
+=" $_mod "
753 if [[ " $omit_dracutmodules " == *\
$_mod\
* ]]; then
757 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\
$_mod\
* ]]; then
758 module_check_mount
$_mod $_moddir; ret
=$?
760 # explicit module, so also accept ret=255
761 [[ $ret = 0 ||
$ret = 255 ]] ||
return 1
763 # module not in our list
764 if [[ $dracutmodules = all
]]; then
765 # check, if we can and should install this module
766 module_check_mount
$_mod $_moddir ||
return 1
773 for _moddep
in $
(module_depends
$_mod $_moddir); do
774 # handle deps as if they were manually added
775 [[ " $dracutmodules " == *\
$_mod\
* ]] \
776 && [[ " $dracutmodules " != *\
$_moddep\
* ]] \
777 && dracutmodules
+=" $_moddep "
778 [[ " $add_dracutmodules " == *\
$_mod\
* ]] \
779 && [[ " $add_dracutmodules " != *\
$_moddep\
* ]] \
780 && add_dracutmodules
+=" $_moddep "
781 [[ " $force_add_dracutmodules " == *\
$_mod\
* ]] \
782 && [[ " $force_add_dracutmodules " != *\
$_moddep\
* ]] \
783 && force_add_dracutmodules
+=" $_moddep "
784 # if a module we depend on fail, fail also
785 if ! check_module
$_moddep; then
786 derror
"dracut module '$_mod' depends on '$_moddep', which can't be installed"
791 [[ " $mods_to_load " == *\
$_mod\
* ]] || \
792 mods_to_load
+=" $_mod "
797 # check_module <dracut module> [<use_as_dep>] [<module path>]
798 # check if a dracut module is to be used in the initramfs process
799 # if <use_as_dep> is set, then the process also keeps track
800 # that the modules were checked for the dependency tracking process
807 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
808 # If we are already scheduled to be loaded, no need to check again.
809 [[ " $mods_to_load " == *\
$_mod\
* ]] && return 0
810 [[ " $mods_checked_as_dep " == *\
$_mod\
* ]] && return 1
812 # This should never happen, but...
813 [[ -d $_moddir ]] ||
return 1
815 [[ $2 ]] || mods_checked_as_dep
+=" $_mod "
817 if [[ " $omit_dracutmodules " == *\
$_mod\
* ]]; then
818 dinfo
"dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
822 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\
$_mod\
* ]]; then
823 if [[ " $dracutmodules $force_add_dracutmodules " == *\
$_mod\
* ]]; then
824 module_check
$_mod 1 $_moddir; ret
=$?
826 module_check
$_mod 0 $_moddir; ret
=$?
828 # explicit module, so also accept ret=255
829 [[ $ret = 0 ||
$ret = 255 ]] ||
return 1
831 # module not in our list
832 if [[ $dracutmodules = all
]]; then
833 # check, if we can and should install this module
834 module_check
$_mod 0 $_moddir; ret
=$?
835 if [[ $ret != 0 ]]; then
837 [[ $ret != 255 ]] && return 1
845 for _moddep
in $
(module_depends
$_mod $_moddir); do
846 # handle deps as if they were manually added
847 [[ " $dracutmodules " == *\
$_mod\
* ]] \
848 && [[ " $dracutmodules " != *\
$_moddep\
* ]] \
849 && dracutmodules
+=" $_moddep "
850 [[ " $add_dracutmodules " == *\
$_mod\
* ]] \
851 && [[ " $add_dracutmodules " != *\
$_moddep\
* ]] \
852 && add_dracutmodules
+=" $_moddep "
853 [[ " $force_add_dracutmodules " == *\
$_mod\
* ]] \
854 && [[ " $force_add_dracutmodules " != *\
$_moddep\
* ]] \
855 && force_add_dracutmodules
+=" $_moddep "
856 # if a module we depend on fail, fail also
857 if ! check_module
$_moddep; then
858 derror
"dracut module '$_mod' depends on '$_moddep', which can't be installed"
863 [[ " $mods_to_load " == *\
$_mod\
* ]] || \
864 mods_to_load
+=" $_mod "
869 # for_each_module_dir <func>
870 # execute "<func> <dracut module> 1 <module path>"
871 for_each_module_dir
() {
877 for _moddir
in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
878 [[ -d $_moddir ]] ||
continue;
879 [[ -e $_moddir/install ||
-e $_moddir/installkernel || \
880 -e $_moddir/module-setup.sh
]] ||
continue
881 _mod
=${_moddir##*/}; _mod
=${_mod#[0-9][0-9]}
882 $_func $_mod 1 $_moddir
885 # Report any missing dracut modules, the user has specified
886 _modcheck
="$add_dracutmodules $force_add_dracutmodules"
887 [[ $dracutmodules != all
]] && _modcheck
="$_modcheck $dracutmodules"
888 for _mod
in $_modcheck; do
889 [[ " $mods_to_load " == *\
$_mod\
* ]] && continue
891 [[ " $force_add_dracutmodules " != *\
$_mod\
* ]] \
892 && [[ " $dracutmodules " != *\
$_mod\
* ]] \
893 && [[ " $omit_dracutmodules " == *\
$_mod\
* ]] \
896 derror
"dracut module '$_mod' cannot be found or installed."
897 [[ " $force_add_dracutmodules " == *\
$_mod\
* ]] && exit 1
898 [[ " $dracutmodules " == *\
$_mod\
* ]] && exit 1
899 [[ " $add_dracutmodules " == *\
$_mod\
* ]] && exit 1
903 # Install a single kernel module along with any firmware it may require.
904 # $1 = full path to kernel module to install
905 install_kmod_with_fw
() {
906 # no need to go further if the module is already installed
908 [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
911 if [[ $omit_drivers ]]; then
915 if [[ "$_kmod" =~
$omit_drivers ]]; then
916 dinfo
"Omitting driver $_kmod"
919 if [[ "${1##*/lib/modules/$kernel/}" =~
$omit_drivers ]]; then
920 dinfo
"Omitting driver $_kmod"
925 if [[ $silent_omit_drivers ]]; then
929 [[ "$_kmod" =~
$silent_omit_drivers ]] && return 0
930 [[ "${1##*/lib/modules/$kernel/}" =~
$silent_omit_drivers ]] && return 0
933 inst_simple
"$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
935 (($ret != 0)) && return $ret
936 local _cmd
=$
(get_decompress_cmd
"$1")
937 [[ -n "${_cmd}" ]] && \
938 ${_cmd} "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
940 local _modname=${1##*/} _fwdir _found _fw
941 _modname=${_modname%.ko*}
942 for _fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do
944 for _fwdir in $fw_dir; do
945 [[ -d $_fwdir ]] || continue
946 if [[ -f $_fwdir/$_fw ]]; then
947 inst_simple "$_fwdir/$_fw" "/lib
/firmware
/$_fw"
948 elif [[ -f $_fwdir/$_fw.xz ]]; then
949 inst_simple "$_fwdir/$_fw.xz
" "/lib
/firmware
/$_fw.xz
"
950 rm -f "${initdir}/lib
/firmware
/$_fw"
951 unxz -f "${initdir}/lib
/firmware
/$_fw.xz
"
957 if [[ $_found != yes ]]; then
958 if ! [[ -d $(echo /sys/module/${_modname//-/_}|{ read a b; echo $a; }) ]]; then
959 dinfo "Possible missing firmware
\"${_fw}\" for kernel module
" \
962 dwarn "Possible missing firmware
\"${_fw}\" for kernel module
" \
970 # Do something with all the dependencies of a kernel module.
971 # Note that kernel modules depend on themselves using the technique we use
972 # $1 = function to call for each dependency we find
973 # It will be passed the full path to the found kernel module
974 # $2 = module to get dependencies for
975 # rest of args = arguments to modprobe
976 # _fderr specifies FD passed from surrounding scope
977 for_each_kmod_dep() {
978 local _func=$1 _kmod=$2 _cmd _modpath _options
980 modprobe "$@
" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
981 while read _cmd _modpath _options || [ -n "$_cmd" ]; do
982 [[ $_cmd = insmod ]] || continue
983 $_func ${_modpath} || exit $?
988 dracut_kernel_post() {
989 for _f in modules.builtin.bin modules.builtin modules.order; do
990 [[ -e $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib
/modules
/$kernel/$_f"
993 # generate module dependencies for the initrd
994 if [[ -d $initdir/lib/modules/$kernel ]] && \
995 ! depmod -a -b "$initdir" $kernel; then
996 dfatal "\"depmod
-a $kernel\" failed.
"
1003 # instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1004 # instmods [-c [-s]] <kernel subsystem>
1005 # install kernel modules along with all their dependencies.
1006 # <kernel subsystem> can be e.g. "=block
" or "=drivers
/usb
/storage
"
1009 local _optional="-o"
1013 [[ $no_kernel = yes ]] && return
1015 if [[ $1 = '-c' ]]; then
1019 if [[ $1 = '-s' ]]; then
1024 if (($# == 0)); then
1025 read -r -d '' -a args
1029 if (($# == 0)); then
1034 ${initdir:+-D "$initdir"} \
1035 ${loginstall:+-L "$loginstall"} \
1037 ${omit_drivers:+-N "$omit_drivers"} \
1038 ${srcmods:+--kerneldir "$srcmods"} \
1040 ${_silent:+--silent} \
1044 if (($_ret != 0)) && [[ -z "$_silent" ]]; then
1047 ${initdir:+-D "$initdir"} \
1048 ${loginstall:+-L "$loginstall"} \
1050 ${omit_drivers:+-N "$omit_drivers"} \
1051 ${srcmods:+--kerneldir "$srcmods"} \
1053 ${_silent:+--silent} \
1057 [[ "$optional" ]] && return 0
1061 if [[ "$
(ln --help)" == *--relative* ]]; then
1063 ln -sfnr "${initdir}/$1" "${initdir}/$2"
1069 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
1070 ln -sfn -- "$(convert_abs_rel "${_dest}" "${_source}")" "${initdir}/${_dest}"