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" ]] && ! [[ $DRACUT_NO_XATTR ]]; 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 # shellcheck source=./dracut-logger.sh
43 .
"$dracutbasedir/dracut-logger.sh"
47 # shellcheck disable=SC2154
48 if ! [[ $initdir ]]; then
49 dfatal
"initdir not set"
53 if ! [[ -d $initdir ]]; then
57 if ! [[ $kernel ]]; then
62 # shellcheck disable=SC2154
63 srcmods
="$dracutsysrootdir/lib/modules/$kernel/"
65 # shellcheck disable=SC2154
66 [[ $drivers_dir ]] && {
67 if ! command -v kmod
&> /dev
/null
&& vercmp
"$(modprobe --version | cut -d' ' -f3)" lt
3.7; then
68 dfatal
'To use --kmoddir option module-init-tools >= 3.7 is required.'
71 srcmods
="$drivers_dir"
75 # export standard hookdirs
77 hookdirs
="cmdline pre-udev pre-trigger netroot "
78 hookdirs
+="initqueue initqueue/settled initqueue/online initqueue/finished initqueue/timeout "
79 hookdirs
+="pre-mount pre-pivot cleanup mount "
80 hookdirs
+="emergency shutdown-emergency pre-shutdown shutdown "
84 DRACUT_LDD
=${DRACUT_LDD:-ldd}
85 DRACUT_TESTBIN
=${DRACUT_TESTBIN:-/bin/sh}
86 DRACUT_LDCONFIG
=${DRACUT_LDCONFIG:-ldconfig}
88 # shellcheck source=./dracut-functions.sh
89 .
"$dracutbasedir"/dracut-functions.sh
92 if ! [[ $libdirs ]]; then
93 if [[ $
("$DRACUT_LDD" "$dracutsysrootdir$DRACUT_TESTBIN") == */lib64
/* ]] &> /dev
/null \
94 && [[ -d $dracutsysrootdir/lib64
]]; then
96 [[ -d $dracutsysrootdir/usr
/lib64
]] && libdirs
+=" /usr/lib64"
99 [[ -d $dracutsysrootdir/usr
/lib
]] && libdirs
+=" /usr/lib"
102 libdirs
+=" $(ldconfig_paths)"
107 # helper function for check() in module-setup.sh
108 # to check for required installed binaries
109 # issues a standardized warning message
111 # shellcheck disable=SC2154
112 local _module_name
="${moddir##*/}"
115 if [[ $1 == "-m" ]]; then
121 if ! find_binary
"$cmd" &> /dev
/null
; then
122 dinfo
"dracut module '${_module_name#[0-9][0-9]}' will not be installed, because command '$cmd' could not be found!"
129 require_any_binary
() {
130 local _module_name
="${moddir##*/}"
133 if [[ $1 == "-m" ]]; then
139 if find_binary
"$cmd" &> /dev
/null
; then
145 if ((_ret
!= 0)); then
146 dinfo
"$_module_name: Could not find any command of '$*'!"
153 dracut_need_initqueue
() {
154 : > "$initdir/lib/dracut/need-initqueue"
157 dracut_module_included
() {
158 # shellcheck disable=SC2154
159 [[ " $mods_to_load $modules_loaded " == *\ $
*\
* ]]
162 dracut_no_switch_root
() {
163 : > "$initdir/lib/dracut/no-switch-root"
166 dracut_module_path
() {
169 # shellcheck disable=SC2231
170 for _dir
in "${dracutbasedir}"/modules.d
/??
${1}; do
177 if ! [[ $DRACUT_INSTALL ]]; then
178 DRACUT_INSTALL
=$
(find_binary dracut-install
)
181 if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install
]]; then
182 DRACUT_INSTALL
=$dracutbasedir/dracut-install
183 elif ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/install
/dracut-install
]]; then
184 DRACUT_INSTALL
=$dracutbasedir/install
/dracut-install
187 # Test if dracut-install is a standalone executable with no options.
188 # E.g. DRACUT_INSTALL may be set externally as:
189 # DRACUT_INSTALL="valgrind dracut-install"
191 # DRACUT_INSTALL="dracut-install --debug"
192 # in which case the string cannot be tested for being executable.
194 for i
in $DRACUT_INSTALL; do
195 DRINSTALLPARTS
=$
((DRINSTALLPARTS
+ 1))
198 if [[ $DRINSTALLPARTS == 1 ]] && ! command -v "$DRACUT_INSTALL" > /dev
/null
2>&1; then
199 dfatal
"dracut-install not found!"
203 if [[ $hostonly == "-h" ]]; then
204 if ! [[ $DRACUT_KERNEL_MODALIASES ]] ||
! [[ -f $DRACUT_KERNEL_MODALIASES ]]; then
205 export DRACUT_KERNEL_MODALIASES
="${DRACUT_TMPDIR}/modaliases"
206 "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${srcmods:+--kerneldir "$srcmods"} --modalias > "$DRACUT_KERNEL_MODALIASES"
210 [[ $DRACUT_RESOLVE_LAZY ]] ||
export DRACUT_RESOLVE_DEPS
=1
213 [[ -e ${initdir}/"$1" ]] && return 0 # already there
214 if "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} -d "$@"; then
218 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} -d "$@"
224 local _ret _hostonly_install
225 if [[ $1 == "-H" ]]; then
226 _hostonly_install
="-H"
229 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
230 if "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"; then
234 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
240 local _ret _hostonly_install
241 if [[ $1 == "-H" ]]; then
242 _hostonly_install
="-H"
245 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
246 [[ -e $1 ]] ||
return 1 # no source
247 if "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${_hostonly_install:+-H} "$@"; then
251 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${_hostonly_install:+-H} "$@"
257 local _ret _hostonly_install
258 if [[ $1 == "-H" ]]; then
259 _hostonly_install
="-H"
262 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
263 [[ -L $1 ]] ||
return 1
264 if "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"; then
268 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
275 if "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} -a ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"; then
279 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} -a ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
291 # shellcheck disable=SC2154
292 [[ $no_kernel == yes ]] && return
294 [[ $i == "--silent" ]] && _silent
=1
297 if "$DRACUT_INSTALL" \
298 ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
299 ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${hostonly:+-H} ${omit_drivers:+-N "$omit_drivers"} ${srcmods:+--kerneldir "$srcmods"} -m "$@"; then
303 if ((_silent
== 0)); then
304 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${hostonly:+-H} ${omit_drivers:+-N "$omit_drivers"} ${srcmods:+--kerneldir "$srcmods"} -m "$@"
311 local _ret _hostonly_install
312 if [[ $1 == "-H" ]]; then
313 _hostonly_install
="-H"
316 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
317 [[ -e $1 ]] ||
return 1 # no source
318 if "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"; then
322 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
329 if "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"; then
333 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
340 if "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"; then
344 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
350 local _ret _helper
="/run/dracut/fsck/fsck_help_$1.txt"
351 if "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$2" "$_helper"; then
355 derror
"$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$2" "$_helper"
360 # Use with form hostonly="$(optional_hostonly)" inst_xxxx <args>
361 # If hosotnly mode is set to "strict", hostonly restrictions will still
362 # be applied, else will ignore hostonly mode and try to install all
364 optional_hostonly
() {
365 # shellcheck disable=SC2154
366 if [[ $hostonly_mode == "strict" ]]; then
367 printf -- "%s" "$hostonly"
375 echo "$i" >> "$initdir/lib/dracut/hostonly-files"
379 # find symlinks linked to given library file
381 # Function searches for symlinks by stripping version numbers appended to
382 # library filename, checks if it points to the same target and finally
383 # prints the list of symlinks to stdout.
386 # rev_lib_symlinks libfoo.so.8.1
387 # output: libfoo.so.8 libfoo.so
388 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
394 [[ ! $1 ]] && return 0
397 _orig
="$(readlink -f "$1")"
400 [[ ${_fn} == *.so.
* ]] ||
return 1
402 until [[ ${_fn##*.} == so
]]; do
404 [[ -L ${_fn} ]] && [[ $(readlink -f "${_fn}") == "${_orig}" ]] && _links+=("${_fn}")
410 # attempt to install any programs specified in a udev rule
411 inst_rule_programs() {
414 # shellcheck disable=SC2013
415 for _prog in $(sed -nr 's/.*PROGRAM==?"([^
"]+).*/\1/p' "$1"); do
417 # shellcheck disable=SC2154
418 if [[ -x ${udevdir}/$_prog ]]; then
419 _bin="${udevdir}"/$_prog
420 elif [[ ${_prog/\$env\{/} == "$_prog" ]]; then
421 _bin=$(find_binary "$_prog") || {
422 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
427 [[ $_bin ]] && inst_binary "$_bin"
430 # shellcheck disable=SC2013
431 for _prog in $(sed -nr 's/.*RUN[+=]=?"([^
"]+).*/\1/p' "$1"); do
433 if [[ -x ${udevdir}/$_prog ]]; then
434 _bin=${udevdir}/$_prog
435 elif [[ ${_prog/\$env\{/} == "$_prog" ]] && [[ ${_prog} != "/sbin
/initqueue
" ]]; then
436 _bin=$(find_binary "$_prog") || {
437 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
442 [[ $_bin ]] && inst_binary "$_bin"
445 # shellcheck disable=SC2013
446 for _prog in $(sed -nr 's/.*IMPORT\{program\}==?"([^
"]+).*/\1/p' "$1"); do
448 if [[ -x ${udevdir}/$_prog ]]; then
449 _bin=${udevdir}/$_prog
450 elif [[ ${_prog/\$env\{/} == "$_prog" ]]; then
451 _bin=$(find_binary "$_prog") || {
452 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
457 [[ $_bin ]] && dracut_install "$_bin"
461 # attempt to create any groups and users specified in a udev rule
462 inst_rule_group_owner() {
465 # shellcheck disable=SC2013
466 for i in $(sed -nr 's/.*OWNER=?"([^
"]+).*/\1/p' "$1"); do
467 if ! grep -Eq "^
$i:" "$initdir/etc
/passwd
" 2> /dev/null; then
468 grep -E "^
$i:" "$dracutsysrootdir"/etc/passwd 2> /dev/null >> "$initdir/etc
/passwd
"
472 # shellcheck disable=SC2013
473 for i in $(sed -nr 's/.*GROUP=?"([^
"]+).*/\1/p' "$1"); do
474 if ! grep -Eq "^
$i:" "$initdir/etc
/group
" 2> /dev/null; then
475 grep -E "^
$i:" "$dracutsysrootdir"/etc/group 2> /dev/null >> "$initdir/etc
/group
"
480 inst_rule_initqueue() {
481 if grep -q -F initqueue "$1"; then
482 dracut_need_initqueue
486 # udev rules always get installed in the same place, so
487 # create a function to install them to make life simpler.
489 local _target=/etc/udev/rules.d _rule _found
491 inst_dir "${udevdir}/rules.d
"
493 for _rule in "$@
"; do
494 if [ "${_rule#/}" = "$_rule" ]; then
495 for r in "$dracutsysrootdir${udevdir}/rules.d
" ${hostonly:+"$dracutsysrootdir"/etc/udev/rules.d}; do
496 [[ -e $r/$_rule ]] || continue
498 inst_rule_programs "$_found"
499 inst_rule_group_owner "$_found"
500 inst_rule_initqueue "$_found"
501 inst_simple "$_found"
504 for r in '' "$dracutsysrootdir$dracutbasedir/rules.d
/"; do
505 # skip rules without an absolute path
506 [[ "${r}$_rule" != /* ]] && continue
507 [[ -f ${r}$_rule ]] || continue
509 inst_rule_programs "$_found"
510 inst_rule_group_owner "$_found"
511 inst_rule_initqueue "$_found"
512 inst_simple "$_found" "$_target/${_found##*/}"
514 [[ $_found ]] || dinfo "Skipping udev rule
: $_rule"
518 inst_rules_wildcard() {
519 local _target=/etc/udev/rules.d _rule _found
521 inst_dir "${udevdir}/rules.d
"
523 for _rule in ${udevdir}/rules.d/$1 ${dracutbasedir}/rules.d/$1; do
524 [[ -e $_rule ]] || continue
525 inst_rule_programs "$_rule"
526 inst_rule_group_owner "$_rule"
527 inst_rule_initqueue "$_rule"
531 if [[ -n ${hostonly} ]]; then
532 for _rule in ${_target}/$1; do
533 [[ -f $_rule ]] || continue
534 inst_rule_programs "$_rule"
535 inst_rule_group_owner "$_rule"
536 inst_rule_initqueue "$_rule"
541 [[ $_found ]] || dinfo "Skipping udev rule
: $_rule"
544 prepare_udev_rules() {
545 if [ -z "$UDEVVERSION" ]; then
546 UDEVVERSION=$(udevadm --version)
550 if [ -z "$UDEVVERSION" ]; then
551 derror "Failed to detect udev version
!"
554 if [ -z "${UDEVVERSION##*[!0-9]*}" ]; then
555 derror "udevadm
--version did not report an integer
, udev version cannot be determined
!"
560 f="${initdir}/etc
/udev
/rules.d
/$f"
561 [ -e "$f" ] || continue
562 while read -r line || [ -n "$line" ]; do
563 if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
564 if ((UDEVVERSION >= 174)); then
565 printf '%sIMPORT{builtin}="path_id
"\n' "${line%%IMPORT PATH_ID}"
567 printf '%sIMPORT{program}="path_id
%%p
"\n' "${line%%IMPORT PATH_ID}"
569 elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
570 if ((UDEVVERSION >= 176)); then
571 printf '%sIMPORT{builtin}="blkid
"\n' "${line%%IMPORT BLKID}"
573 # shellcheck disable=SC2016
574 printf '%sIMPORT{program}="/sbin
/blkid
-o udev
-p $tempnode"\n' "${line%%IMPORT BLKID}"
579 done < "${f}" > "${f}.new
"
584 # install function specialized for hooks
585 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
586 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
589 if ! [[ -f $3 ]]; then
590 dfatal "Cannot
install a hook
($3) that does not exist.
"
591 dfatal "Aborting initrd creation.
"
593 elif ! [[ $hookdirs == *$1* ]]; then
594 dfatal "No such hook
type $1. Aborting initrd creation.
"
597 hook="/lib
/dracut
/hooks
/${1}/${2}-${3##*/}"
598 inst_simple "$3" "$hook"
599 chmod u+x "$initdir/$hook"
602 # install any of listed files
604 # If first argument is '-d' and second some destination path, first accessible
605 # source is installed into this path, otherwise it will installed in the same
606 # path as source. If none of listed files was installed, function return 1.
607 # On first successful installation it returns with 0 status.
611 # inst_any -d /bin/foo /bin/bar /bin/baz
613 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
618 [[ $1 == '-d' ]] && to="$2" && shift 2
621 [[ -e $f ]] || continue
622 [[ $to ]] && inst "$f" "$to" && return 0
623 inst "$f" && return 0
629 # inst_libdir_file [-n <pattern>] <file> [<file>...]
630 # Install a <file> located on a lib directory to the initramfs image
631 # -n <pattern> install matching files
634 if [[ $1 == "-n" ]]; then
637 for _dir in $libdirs; do
639 for _f in "$dracutsysrootdir$_dir"/$_i; do
640 [[ ${_f#$dracutsysrootdir} =~ $_pattern ]] || continue
641 [[ -e $_f ]] && _files+=("${_f#$dracutsysrootdir}")
646 for _dir in $libdirs; do
648 for _f in "$dracutsysrootdir$_dir"/$_i; do
649 [[ -e $_f ]] && _files+=("${_f#$dracutsysrootdir}")
654 [[ ${#_files[@]} -gt 0 ]] && inst_multiple "${_files[@]}"
657 # get a command to decompress the given file
658 get_decompress_cmd() {
660 *.gz) echo 'gzip -f -d' ;;
661 *.bz2) echo 'bzip2 -d' ;;
662 *.xz) echo 'xz -f -d' ;;
663 *.zst) echo 'zstd -f -d ' ;;
667 # install function decompressing the target and handling symlinks
668 # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
670 # Function install targets in the same paths inside overlay but decompressed
671 # and without extensions (.gz, .bz2).
676 _cmd=$(get_decompress_cmd "${_src}")
677 [[ -z ${_cmd} ]] && return 1
678 inst_simple "${_src}"
679 # Decompress with chosen tool. We assume that tool changes name e.g.
680 # from 'name.gz' to 'name'.
681 ${_cmd} "${initdir}${_src}"
685 # It's similar to above, but if file is not compressed, performs standard
688 inst_opt_decompress
() {
692 inst_decompress
"${_src}" || inst
"${_src}"
696 # module_check <dracut module> [<forced>] [<module path>]
697 # execute the check() function of module-setup.sh of <dracut module>
698 # or the "check" script, if module-setup.sh is not found
699 # "check $hostonly" is called
704 local _hostonly
=$hostonly
705 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
706 [ $# -eq 2 ] && _forced
=$2
707 [[ -d $_moddir ]] ||
return 1
708 if [[ ! -f $_moddir/module-setup.sh
]]; then
709 # if we do not have a check script, we are unconditionally included
710 [[ -x $_moddir/check
]] ||
return 0
711 [[ $_forced != 0 ]] && unset hostonly
712 # don't quote $hostonly to leave argument empty
713 # shellcheck disable=SC2086
714 "$_moddir"/check
$hostonly
717 unset check depends cmdline
install installkernel
719 # shellcheck disable=SC1090
720 .
"$_moddir"/module-setup.sh
721 is_func check ||
return 0
722 [[ $_forced != 0 ]] && unset hostonly
723 # don't quote $hostonly to leave argument empty
724 # shellcheck disable=SC2086
725 moddir
="$_moddir" check
$hostonly
727 unset check depends cmdline
install installkernel
733 # module_check_mount <dracut module> [<module path>]
734 # execute the check() function of module-setup.sh of <dracut module>
735 # or the "check" script, if module-setup.sh is not found
736 # "mount_needs=1 check 0" is called
737 module_check_mount
() {
741 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
742 [[ -d $_moddir ]] ||
return 1
743 if [[ ! -f $_moddir/module-setup.sh
]]; then
744 # if we do not have a check script, we are unconditionally included
745 [[ -x $_moddir/check
]] ||
return 0
746 mount_needs
=1 "$_moddir"/check
0
749 unset check depends cmdline
install installkernel
751 # shellcheck disable=SC1090
752 .
"$_moddir"/module-setup.sh
753 moddir
=$_moddir check
0
755 unset check depends cmdline
install installkernel
761 # module_depends <dracut module> [<module path>]
762 # execute the depends() function of module-setup.sh of <dracut module>
763 # or the "depends" script, if module-setup.sh is not found
767 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
768 [[ -d $_moddir ]] ||
return 1
769 if [[ ! -f $_moddir/module-setup.sh
]]; then
770 # if we do not have a check script, we have no deps
771 [[ -x $_moddir/check
]] ||
return 0
775 unset check depends cmdline
install installkernel
777 # shellcheck disable=SC1090
778 .
"$_moddir"/module-setup.sh
779 moddir
=$_moddir depends
781 unset check depends cmdline
install installkernel
786 # module_cmdline <dracut module> [<module path>]
787 # execute the cmdline() function of module-setup.sh of <dracut module>
788 # or the "cmdline" script, if module-setup.sh is not found
792 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
793 [[ -d $_moddir ]] ||
return 1
794 if [[ ! -f $_moddir/module-setup.sh
]]; then
795 # shellcheck disable=SC1090
796 [[ -x $_moddir/cmdline
]] && .
"$_moddir/cmdline"
799 unset check depends cmdline
install installkernel
801 # shellcheck disable=SC1090
802 .
"$_moddir"/module-setup.sh
803 moddir
="$_moddir" cmdline
805 unset check depends cmdline
install installkernel
810 # module_install <dracut module> [<module path>]
811 # execute the install() function of module-setup.sh of <dracut module>
812 # or the "install" script, if module-setup.sh is not found
816 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
817 [[ -d $_moddir ]] ||
return 1
818 if [[ ! -f $_moddir/module-setup.sh
]]; then
819 # shellcheck disable=SC1090
820 [[ -x $_moddir/install ]] && .
"$_moddir/install"
823 unset check depends cmdline
install installkernel
825 # shellcheck disable=SC1090
826 .
"$_moddir"/module-setup.sh
827 moddir
="$_moddir" install
829 unset check depends cmdline
install installkernel
834 # module_installkernel <dracut module> [<module path>]
835 # execute the installkernel() function of module-setup.sh of <dracut module>
836 # or the "installkernel" script, if module-setup.sh is not found
837 module_installkernel
() {
840 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
841 [[ -d $_moddir ]] ||
return 1
842 if [[ ! -f $_moddir/module-setup.sh
]]; then
843 # shellcheck disable=SC1090
844 [[ -x $_moddir/installkernel
]] && .
"$_moddir/installkernel"
847 unset check depends cmdline
install installkernel
848 installkernel
() { true
; }
849 # shellcheck disable=SC1090
850 .
"$_moddir"/module-setup.sh
851 moddir
="$_moddir" installkernel
853 unset check depends cmdline
install installkernel
858 # check_mount <dracut module> [<use_as_dep>] [<module path>]
859 # check_mount checks, if a dracut module is needed for the given
860 # device and filesystem types in "${host_fs_types[@]}"
867 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
868 # shellcheck disable=SC2154
869 [ "${#host_fs_types[@]}" -le 0 ] && return 1
871 # If we are already scheduled to be loaded, no need to check again.
872 [[ " $mods_to_load " == *\
$_mod\
* ]] && return 0
873 [[ " $mods_checked_as_dep " == *\
$_mod\
* ]] && return 1
875 # This should never happen, but...
876 [[ -d $_moddir ]] ||
return 1
878 [[ $2 ]] || mods_checked_as_dep
+=" $_mod "
880 # shellcheck disable=SC2154
881 if [[ " $omit_dracutmodules " == *\
$_mod\
* ]]; then
885 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\
$_mod\
* ]]; then
886 module_check_mount
"$_mod" "$_moddir"
889 # explicit module, so also accept ret=255
890 [[ $ret == 0 ||
$ret == 255 ]] ||
return 1
892 # module not in our list
893 if [[ $dracutmodules == all
]]; then
894 # check, if we can and should install this module
895 module_check_mount
"$_mod" "$_moddir" ||
return 1
902 for _moddep
in $
(module_depends
"$_mod" "$_moddir"); do
903 # handle deps as if they were manually added
904 [[ " $dracutmodules " == *\
$_mod\
* ]] \
905 && [[ " $dracutmodules " != *\
$_moddep\
* ]] \
906 && dracutmodules
+=" $_moddep "
907 [[ " $add_dracutmodules " == *\
$_mod\
* ]] \
908 && [[ " $add_dracutmodules " != *\
$_moddep\
* ]] \
909 && add_dracutmodules
+=" $_moddep "
910 [[ " $force_add_dracutmodules " == *\
$_mod\
* ]] \
911 && [[ " $force_add_dracutmodules " != *\
$_moddep\
* ]] \
912 && force_add_dracutmodules
+=" $_moddep "
913 # if a module we depend on fail, fail also
914 if ! check_module
"$_moddep"; then
915 derror
"dracut module '$_mod' depends on '$_moddep', which can't be installed"
920 [[ " $mods_to_load " == *\
$_mod\
* ]] \
921 || mods_to_load
+=" $_mod "
926 # check_module <dracut module> [<use_as_dep>] [<module path>]
927 # check if a dracut module is to be used in the initramfs process
928 # if <use_as_dep> is set, then the process also keeps track
929 # that the modules were checked for the dependency tracking process
936 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
937 # If we are already scheduled to be loaded, no need to check again.
938 [[ " $mods_to_load " == *\
$_mod\
* ]] && return 0
939 [[ " $mods_checked_as_dep " == *\
$_mod\
* ]] && return 1
941 # This should never happen, but...
942 [[ -d $_moddir ]] ||
return 1
944 [[ $2 ]] || mods_checked_as_dep
+=" $_mod "
946 if [[ " $omit_dracutmodules " == *\
$_mod\
* ]]; then
947 ddebug
"dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
951 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\
$_mod\
* ]]; then
952 if [[ " $dracutmodules $force_add_dracutmodules " == *\
$_mod\
* ]]; then
953 module_check
"$_mod" 1 "$_moddir"
956 module_check
"$_mod" 0 "$_moddir"
959 # explicit module, so also accept ret=255
960 [[ $ret == 0 ||
$ret == 255 ]] ||
return 1
962 # module not in our list
963 if [[ $dracutmodules == all
]]; then
964 # check, if we can and should install this module
965 module_check
"$_mod" 0 "$_moddir"
967 if [[ $ret != 0 ]]; then
969 [[ $ret != 255 ]] && return 1
977 for _moddep
in $
(module_depends
"$_mod" "$_moddir"); do
978 # handle deps as if they were manually added
979 [[ " $dracutmodules " == *\
$_mod\
* ]] \
980 && [[ " $dracutmodules " != *\
$_moddep\
* ]] \
981 && dracutmodules
+=" $_moddep "
982 [[ " $add_dracutmodules " == *\
$_mod\
* ]] \
983 && [[ " $add_dracutmodules " != *\
$_moddep\
* ]] \
984 && add_dracutmodules
+=" $_moddep "
985 [[ " $force_add_dracutmodules " == *\
$_mod\
* ]] \
986 && [[ " $force_add_dracutmodules " != *\
$_moddep\
* ]] \
987 && force_add_dracutmodules
+=" $_moddep "
988 # if a module we depend on fail, fail also
989 if ! check_module
"$_moddep"; then
990 derror
"dracut module '$_mod' depends on '$_moddep', which can't be installed"
995 [[ " $mods_to_load " == *\
$_mod\
* ]] \
996 || mods_to_load
+=" $_mod "
1001 # for_each_module_dir <func>
1002 # execute "<func> <dracut module> 1 <module path>"
1003 for_each_module_dir
() {
1009 for _moddir
in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1010 [[ -d $_moddir ]] ||
continue
1011 [[ -e $_moddir/install ||
-e $_moddir/installkernel ||
-e \
1012 $_moddir/module-setup.sh
]] ||
continue
1014 _mod
=${_mod#[0-9][0-9]}
1015 $_func "$_mod" 1 "$_moddir"
1018 # Report any missing dracut modules, the user has specified
1019 _modcheck
="$add_dracutmodules $force_add_dracutmodules"
1020 [[ $dracutmodules != all
]] && _modcheck
="$_modcheck $dracutmodules"
1021 for _mod
in $_modcheck; do
1022 [[ " $mods_to_load " == *\
$_mod\
* ]] && continue
1024 [[ " $force_add_dracutmodules " != *\
$_mod\
* ]] \
1025 && [[ " $dracutmodules " != *\
$_mod\
* ]] \
1026 && [[ " $omit_dracutmodules " == *\
$_mod\
* ]] \
1029 derror
"dracut module '$_mod' cannot be found or installed."
1030 [[ " $force_add_dracutmodules " == *\
$_mod\
* ]] && exit 1
1031 [[ " $dracutmodules " == *\
$_mod\
* ]] && exit 1
1032 [[ " $add_dracutmodules " == *\
$_mod\
* ]] && exit 1
1036 dracut_kernel_post
() {
1037 for _f
in modules.
builtin modules.
builtin.
alias modules.
builtin.modinfo modules.order
; do
1038 [[ -e $srcmods/$_f ]] && inst_simple
"$srcmods/$_f" "/lib/modules/$kernel/$_f"
1041 # generate module dependencies for the initrd
1042 if [[ -d $initdir/lib
/modules
/$kernel ]] \
1043 && ! depmod
-a -b "$initdir" "$kernel"; then
1044 dfatal
"\"depmod -a $kernel\" failed."
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"
1057 local _optional
="-o"
1061 [[ $no_kernel == yes ]] && return
1063 if [[ $1 == '-c' ]]; then
1067 if [[ $1 == '-s' ]]; then
1072 if (($# == 0)); then
1073 read -r -d '' -a args
1077 if (($# == 0)); then
1082 ${initdir:+-D "$initdir"} \
1083 ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
1084 ${loginstall:+-L "$loginstall"} \
1086 ${omit_drivers:+-N "$omit_drivers"} \
1087 ${srcmods:+--kerneldir "$srcmods"} \
1089 ${_silent:+--silent} \
1093 if ((_ret
!= 0)) && [[ -z $_silent ]]; then
1096 ${initdir:+-D "$initdir"} \
1097 ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
1098 ${loginstall:+-L "$loginstall"} \
1100 ${omit_drivers:+-N "$omit_drivers"} \
1101 ${srcmods:+--kerneldir "$srcmods"} \
1103 ${_silent:+--silent} \
1107 [[ "$optional" ]] && return 0
1111 if [[ "$(ln --help)" == *--relative* ]]; then
1113 ln -sfnr "${initdir}/$1" "${initdir}/$2"
1119 [[ -d ${_dest%/*} ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
1120 ln -sfn -- "$(convert_abs_rel "${_dest}" "${_source}")" "${initdir}/${_dest}"
1124 is_qemu_virtualized() {
1125 # 0 if a virt environment was detected
1126 # 1 if a virt environment could not be detected
1127 # 255 if any error was encountered
1128 if type -P systemd-detect-virt > /dev/null 2>&1; then
1129 if ! vm=$(systemd-detect-virt --vm > /dev/null 2>&1); then
1132 [[ $vm == "qemu
" ]] && return 0
1133 [[ $vm == "kvm
" ]] && return 0
1134 [[ $vm == "bochs
" ]] && return 0
1137 for i in /sys/class/dmi/id/*_vendor; do
1138 [[ -f $i ]] || continue
1139 read -r vendor < "$i"
1140 [[ $vendor == "QEMU
" ]] && return 0
1141 [[ $vendor == "Red Hat
" ]] && return 0
1142 [[ $vendor == "Bochs
" ]] && return 0