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 # helper function for check() in module-setup.sh
154 # to check for required kernel modules
155 # issues a standardized warning message
156 require_kernel_modules
() {
157 # shellcheck disable=SC2154
158 local _module_name
="${moddir##*/}"
161 # Ignore kernel module requirement for no-kernel build
162 [[ $no_kernel == yes ]] && return 0
164 if [[ $1 == "-m" ]]; then
170 if ! check_kernel_module
"$mod" &> /dev
/null
; then
171 dinfo
"dracut module '${_module_name#[0-9][0-9]}' will not be installed, because kernel module '$mod' is not available!"
178 dracut_need_initqueue
() {
179 : > "$initdir/lib/dracut/need-initqueue"
182 dracut_module_included
() {
183 # shellcheck disable=SC2154
184 [[ " $mods_to_load $modules_loaded " == *\ $
*\
* ]]
187 dracut_no_switch_root
() {
188 : > "$initdir/lib/dracut/no-switch-root"
191 dracut_module_path
() {
194 # shellcheck disable=SC2231
195 for _dir
in "${dracutbasedir}"/modules.d
/??
${1}; do
202 if ! [[ $DRACUT_INSTALL ]]; then
203 DRACUT_INSTALL
=$
(find_binary dracut-install
)
206 if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install
]]; then
207 DRACUT_INSTALL
=$dracutbasedir/dracut-install
208 elif ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/src
/install
/dracut-install
]]; then
209 DRACUT_INSTALL
=$dracutbasedir/src
/install
/dracut-install
212 # Test if dracut-install is a standalone executable with no options.
213 # E.g. DRACUT_INSTALL may be set externally as:
214 # DRACUT_INSTALL="valgrind dracut-install"
216 # DRACUT_INSTALL="dracut-install --debug"
217 # in which case the string cannot be tested for being executable.
219 for i
in $DRACUT_INSTALL; do
220 DRINSTALLPARTS
=$
((DRINSTALLPARTS
+ 1))
223 if [[ $DRINSTALLPARTS == 1 ]] && ! command -v "$DRACUT_INSTALL" > /dev
/null
2>&1; then
224 dfatal
"dracut-install not found!"
228 if [[ $hostonly == "-h" ]]; then
229 if ! [[ $DRACUT_KERNEL_MODALIASES ]] ||
! [[ -f $DRACUT_KERNEL_MODALIASES ]]; then
230 export DRACUT_KERNEL_MODALIASES
="${DRACUT_TMPDIR}/modaliases"
231 $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${srcmods:+--kerneldir "$srcmods"} --modalias > "$DRACUT_KERNEL_MODALIASES"
235 [[ $DRACUT_RESOLVE_LAZY ]] ||
export DRACUT_RESOLVE_DEPS
=1
238 [[ -e ${initdir}/"$1" ]] && return 0 # already there
239 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} -d "$@"; then
243 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} -d "$@"
249 local _ret _hostonly_install
250 if [[ $1 == "-H" ]]; then
251 _hostonly_install
="-H"
254 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
255 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"; then
259 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
265 local _ret _hostonly_install
266 if [[ $1 == "-H" ]]; then
267 _hostonly_install
="-H"
270 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
271 [[ -e $1 ]] ||
return 1 # no source
272 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${_hostonly_install:+-H} "$@"; then
276 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${_hostonly_install:+-H} "$@"
282 local _ret _hostonly_install
283 if [[ $1 == "-H" ]]; then
284 _hostonly_install
="-H"
287 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
288 [[ -L $1 ]] ||
return 1
289 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"; then
293 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
299 local _ret _hostonly_install
300 if [[ $1 == "-H" ]]; then
301 _hostonly_install
="-H"
304 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} -a ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"; then
308 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} "$@"
320 # shellcheck disable=SC2154
321 [[ $no_kernel == yes ]] && return
323 [[ $i == "--silent" ]] && _silent
=1
327 ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
328 ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${hostonly:+-H} ${omit_drivers:+-N "$omit_drivers"} ${srcmods:+--kerneldir "$srcmods"} -m "$@"; then
332 if ((_silent
== 0)); then
333 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${hostonly:+-H} ${omit_drivers:+-N "$omit_drivers"} ${srcmods:+--kerneldir "$srcmods"} -m "$@"
340 local _ret _hostonly_install
341 if [[ $1 == "-H" ]]; then
342 _hostonly_install
="-H"
345 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
346 [[ -e $1 ]] ||
return 1 # no source
347 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"; then
351 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
358 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"; then
362 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
369 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"; then
373 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
379 local _ret _helper
="/run/dracut/fsck/fsck_help_$1.txt"
380 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$2" "$_helper"; then
384 derror
"$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$2" "$_helper"
389 # Use with form hostonly="$(optional_hostonly)" inst_xxxx <args>
390 # If hostonly mode is set to "strict", hostonly restrictions will still
391 # be applied, else will ignore hostonly mode and try to install all
393 optional_hostonly
() {
394 # shellcheck disable=SC2154
395 if [[ $hostonly_mode == "strict" ]]; then
396 printf -- "%s" "$hostonly"
404 echo "$i" >> "$initdir/lib/dracut/hostonly-files"
408 # find symlinks linked to given library file
410 # Function searches for symlinks by stripping version numbers appended to
411 # library filename, checks if it points to the same target and finally
412 # prints the list of symlinks to stdout.
415 # rev_lib_symlinks libfoo.so.8.1
416 # output: libfoo.so.8 libfoo.so
417 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
423 [[ ! $1 ]] && return 0
426 _orig
="$(readlink -f "$1")"
429 [[ ${_fn} == *.so.
* ]] ||
return 1
431 until [[ ${_fn##*.} == so
]]; do
433 [[ -L ${_fn} ]] && [[ $(readlink -f "${_fn}") == "${_orig}" ]] && _links+=("${_fn}")
439 # attempt to install any programs specified in a udev rule
440 inst_rule_programs() {
443 # shellcheck disable=SC2013
444 for _prog in $(sed -nr 's/.*PROGRAM==?"([^
"]+).*/\1/p' "$1"); do
446 # shellcheck disable=SC2154
447 if [[ -x ${udevdir}/$_prog ]]; then
448 _bin="${udevdir}"/$_prog
449 elif [[ ${_prog/\$env\{/} == "$_prog" ]]; then
450 _bin=$(find_binary "$_prog") || {
451 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
456 [[ $_bin ]] && inst_binary "$_bin"
459 # shellcheck disable=SC2013
460 for _prog in $(sed -nr 's/.*RUN[+=]=?"([^
"]+).*/\1/p' "$1"); do
462 if [[ -x ${udevdir}/$_prog ]]; then
463 _bin=${udevdir}/$_prog
464 elif [[ ${_prog/\$env\{/} == "$_prog" ]] && [[ ${_prog} != "/sbin
/initqueue
" ]]; then
465 _bin=$(find_binary "$_prog") || {
466 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
471 [[ $_bin ]] && inst_binary "$_bin"
474 # shellcheck disable=SC2013
475 for _prog in $(sed -nr 's/.*IMPORT\{program\}==?"([^
"]+).*/\1/p' "$1"); do
477 if [[ -x ${udevdir}/$_prog ]]; then
478 _bin=${udevdir}/$_prog
479 elif [[ ${_prog/\$env\{/} == "$_prog" ]]; then
480 _bin=$(find_binary "$_prog") || {
481 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
486 [[ $_bin ]] && dracut_install "$_bin"
490 # attempt to create any groups and users specified in a udev rule
491 inst_rule_group_owner() {
494 # shellcheck disable=SC2013
495 for i in $(sed -nr 's/.*OWNER=?"([^
"]+).*/\1/p' "$1"); do
496 if ! grep -Eq "^
$i:" "$initdir/etc
/passwd
" 2> /dev/null; then
497 grep -E "^
$i:" "$dracutsysrootdir"/etc/passwd 2> /dev/null >> "$initdir/etc
/passwd
"
501 # shellcheck disable=SC2013
502 for i in $(sed -nr 's/.*GROUP=?"([^
"]+).*/\1/p' "$1"); do
503 if ! grep -Eq "^
$i:" "$initdir/etc
/group
" 2> /dev/null; then
504 grep -E "^
$i:" "$dracutsysrootdir"/etc/group 2> /dev/null >> "$initdir/etc
/group
"
509 inst_rule_initqueue() {
510 if grep -q -F initqueue "$1"; then
511 dracut_need_initqueue
515 # udev rules always get installed in the same place, so
516 # create a function to install them to make life simpler.
518 local _target=/etc/udev/rules.d _rule _found
520 inst_dir "${udevdir}/rules.d
"
522 for _rule in "$@
"; do
523 if [ "${_rule#/}" = "$_rule" ]; then
524 for r in "$dracutsysrootdir${udevdir}/rules.d
" ${hostonly:+"$dracutsysrootdir"/etc/udev/rules.d}; do
525 [[ -e $r/$_rule ]] || continue
527 inst_rule_programs "$_found"
528 inst_rule_group_owner "$_found"
529 inst_rule_initqueue "$_found"
530 inst_simple "$_found"
533 for r in '' "$dracutsysrootdir$dracutbasedir/rules.d
/"; do
534 # skip rules without an absolute path
535 [[ "${r}$_rule" != /* ]] && continue
536 [[ -f ${r}$_rule ]] || continue
538 inst_rule_programs "$_found"
539 inst_rule_group_owner "$_found"
540 inst_rule_initqueue "$_found"
541 inst_simple "$_found" "$_target/${_found##*/}"
543 [[ $_found ]] || dinfo "Skipping udev rule
: $_rule"
547 inst_rules_wildcard() {
548 local _target=/etc/udev/rules.d _rule _found
550 inst_dir "${udevdir}/rules.d
"
552 for _rule in ${udevdir}/rules.d/$1 ${dracutbasedir}/rules.d/$1; do
553 [[ -e $_rule ]] || continue
554 inst_rule_programs "$_rule"
555 inst_rule_group_owner "$_rule"
556 inst_rule_initqueue "$_rule"
560 if [[ -n ${hostonly} ]]; then
561 for _rule in ${_target}/$1; do
562 [[ -f $_rule ]] || continue
563 inst_rule_programs "$_rule"
564 inst_rule_group_owner "$_rule"
565 inst_rule_initqueue "$_rule"
570 [[ $_found ]] || dinfo "Skipping udev rule
: $_rule"
573 # make sure that library links are correct and up to date
575 for f in "$dracutsysrootdir"/etc/ld.so.conf "$dracutsysrootdir"/etc/ld.so.conf.d/*; do
576 [[ -f $f ]] && inst_simple "${f#"$dracutsysrootdir"}"
578 if ! $DRACUT_LDCONFIG -r "$initdir" -f /etc/ld.so.conf; then
579 if [[ $EUID == 0 ]]; then
580 derror "ldconfig exited ungracefully
"
582 derror "ldconfig might need uid
=0 (root
) for chroot
()"
587 prepare_udev_rules() {
588 if [ -z "$UDEVVERSION" ]; then
589 UDEVVERSION=$(udevadm --version)
593 if [ -z "$UDEVVERSION" ]; then
594 derror "Failed to detect udev version
!"
597 if [ -z "${UDEVVERSION##*[!0-9]*}" ]; then
598 derror "udevadm
--version did not report an integer
, udev version cannot be determined
!"
603 f="${initdir}/etc
/udev
/rules.d
/$f"
604 [ -e "$f" ] || continue
605 while read -r line || [ -n "$line" ]; do
606 if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
607 if ((UDEVVERSION >= 174)); then
608 printf '%sIMPORT{builtin}="path_id
"\n' "${line%%IMPORT PATH_ID}"
610 printf '%sIMPORT{program}="path_id
%%p
"\n' "${line%%IMPORT PATH_ID}"
612 elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
613 if ((UDEVVERSION >= 176)); then
614 printf '%sIMPORT{builtin}="blkid
"\n' "${line%%IMPORT BLKID}"
616 # shellcheck disable=SC2016
617 printf '%sIMPORT{program}="/sbin
/blkid
-o udev
-p $tempnode"\n' "${line%%IMPORT BLKID}"
622 done < "${f}" > "${f}.new
"
627 # install function specialized for hooks
628 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
629 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
632 if ! [[ -f $3 ]]; then
633 dfatal "Cannot
install a hook
($3) that does not exist.
"
634 dfatal "Aborting initrd creation.
"
636 elif ! [[ $hookdirs == *$1* ]]; then
637 dfatal "No such hook
type $1. Aborting initrd creation.
"
640 hook="/lib
/dracut
/hooks
/${1}/${2}-${3##*/}"
641 inst_simple "$3" "$hook"
642 chmod u+x "$initdir/$hook"
645 # install any of listed files
647 # If first argument is '-d' and second some destination path, first accessible
648 # source is installed into this path, otherwise it will installed in the same
649 # path as source. If none of listed files was installed, function return 1.
650 # On first successful installation it returns with 0 status.
654 # inst_any -d /bin/foo /bin/bar /bin/baz
656 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
661 [[ $1 == '-d' ]] && to="$2" && shift 2
664 [[ -e $f ]] || continue
665 [[ $to ]] && inst "$f" "$to" && return 0
666 inst "$f" && return 0
672 # inst_libdir_dir <dir> [<dir>...]
673 # Install a <dir> located on a lib directory to the initramfs image
676 for _dir in $libdirs; do
678 for _d in "$dracutsysrootdir$_dir"/$_i; do
679 [[ -d $_d ]] && _dirs+=("${_d#"$dracutsysrootdir"}")
683 for _dir in "${_dirs[@]}"; do
688 # inst_libdir_file [-n <pattern>] <file> [<file>...]
689 # Install a <file> located on a lib directory to the initramfs image
690 # -n <pattern> install matching files
693 if [[ $1 == "-n" ]]; then
696 for _dir in $libdirs; do
698 for _f in "$dracutsysrootdir$_dir"/$_i; do
699 [[ ${_f#"$dracutsysrootdir"} =~ $_pattern ]] || continue
700 [[ -e $_f ]] && _files+=("${_f#"$dracutsysrootdir"}")
705 for _dir in $libdirs; do
707 for _f in "$dracutsysrootdir$_dir"/$_i; do
708 [[ -e $_f ]] && _files+=("${_f#"$dracutsysrootdir"}")
713 [[ ${#_files[@]} -gt 0 ]] && inst_multiple "${_files[@]}"
716 # get a command to decompress the given file
717 get_decompress_cmd() {
719 *.gz) echo 'gzip -f -d' ;;
720 *.bz2) echo 'bzip2 -d' ;;
721 *.xz) echo 'xz -f -d' ;;
722 *.zst) echo 'zstd -f -d ' ;;
726 # install function decompressing the target and handling symlinks
727 # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
729 # Function install targets in the same paths inside overlay but decompressed
730 # and without extensions (.gz, .bz2).
735 _cmd=$(get_decompress_cmd "${_src}")
736 [[ -z ${_cmd} ]] && return 1
737 inst_simple "${_src}"
738 # Decompress with chosen tool. We assume that tool changes name e.g.
739 # from 'name.gz' to 'name'.
740 ${_cmd} "${initdir}${_src}"
744 # It's similar to above, but if file is not compressed, performs standard
747 inst_opt_decompress
() {
751 inst_decompress
"${_src}" || inst
"${_src}"
755 # module_check <dracut module> [<forced>] [<module path>]
756 # execute the check() function of module-setup.sh of <dracut module>
757 # or the "check" script, if module-setup.sh is not found
758 # "check $hostonly" is called
763 local _hostonly
=$hostonly
764 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
765 [ $# -eq 2 ] && _forced
=$2
766 [[ -d $_moddir ]] ||
return 1
767 if [[ ! -f $_moddir/module-setup.sh
]]; then
768 # if we do not have a check script, we are unconditionally included
769 [[ -x $_moddir/check
]] ||
return 0
770 [[ $_forced != 0 ]] && unset hostonly
771 # don't quote $hostonly to leave argument empty
772 # shellcheck disable=SC2086
773 "$_moddir"/check
$hostonly
776 unset check depends cmdline
install installkernel
778 # shellcheck disable=SC1090
779 .
"$_moddir"/module-setup.sh
780 is_func check ||
return 0
781 [[ $_forced != 0 ]] && unset hostonly
782 # don't quote $hostonly to leave argument empty
783 # shellcheck disable=SC2086
784 moddir
="$_moddir" check
$hostonly
786 unset check depends cmdline
install installkernel
792 # module_check_mount <dracut module> [<module path>]
793 # execute the check() function of module-setup.sh of <dracut module>
794 # or the "check" script, if module-setup.sh is not found
795 # "mount_needs=1 check 0" is called
796 module_check_mount
() {
800 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
801 [[ -d $_moddir ]] ||
return 1
802 if [[ ! -f $_moddir/module-setup.sh
]]; then
803 # if we do not have a check script, we are unconditionally included
804 [[ -x $_moddir/check
]] ||
return 0
805 mount_needs
=1 "$_moddir"/check
0
808 unset check depends cmdline
install installkernel
810 # shellcheck disable=SC1090
811 .
"$_moddir"/module-setup.sh
812 moddir
=$_moddir check
0
814 unset check depends cmdline
install installkernel
820 # module_depends <dracut module> [<module path>]
821 # execute the depends() function of module-setup.sh of <dracut module>
822 # or the "depends" script, if module-setup.sh is not found
826 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
827 [[ -d $_moddir ]] ||
return 1
828 if [[ ! -f $_moddir/module-setup.sh
]]; then
829 # if we do not have a check script, we have no deps
830 [[ -x $_moddir/check
]] ||
return 0
834 unset check depends cmdline
install installkernel
836 # shellcheck disable=SC1090
837 .
"$_moddir"/module-setup.sh
838 moddir
=$_moddir depends
840 unset check depends cmdline
install installkernel
845 # module_cmdline <dracut module> [<module path>]
846 # execute the cmdline() function of module-setup.sh of <dracut module>
847 # or the "cmdline" script, if module-setup.sh is not found
851 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
852 [[ -d $_moddir ]] ||
return 1
853 if [[ ! -f $_moddir/module-setup.sh
]]; then
854 # shellcheck disable=SC1090
855 [[ -x $_moddir/cmdline
]] && .
"$_moddir/cmdline"
858 unset check depends cmdline
install installkernel
860 # shellcheck disable=SC1090
861 .
"$_moddir"/module-setup.sh
862 moddir
="$_moddir" cmdline
864 unset check depends cmdline
install installkernel
869 # module_install <dracut module> [<module path>]
870 # execute the install() function of module-setup.sh of <dracut module>
871 # or the "install" script, if module-setup.sh is not found
875 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
876 [[ -d $_moddir ]] ||
return 1
877 if [[ ! -f $_moddir/module-setup.sh
]]; then
878 # shellcheck disable=SC1090
879 [[ -x $_moddir/install ]] && .
"$_moddir/install"
882 unset check depends cmdline
install installkernel
884 # shellcheck disable=SC1090
885 .
"$_moddir"/module-setup.sh
886 moddir
="$_moddir" install
888 unset check depends cmdline
install installkernel
893 # module_installkernel <dracut module> [<module path>]
894 # execute the installkernel() function of module-setup.sh of <dracut module>
895 # or the "installkernel" script, if module-setup.sh is not found
896 module_installkernel
() {
899 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
900 [[ -d $_moddir ]] ||
return 1
901 if [[ ! -f $_moddir/module-setup.sh
]]; then
902 # shellcheck disable=SC1090
903 [[ -x $_moddir/installkernel
]] && .
"$_moddir/installkernel"
906 unset check depends cmdline
install installkernel
907 installkernel
() { true
; }
908 # shellcheck disable=SC1090
909 .
"$_moddir"/module-setup.sh
910 moddir
="$_moddir" installkernel
912 unset check depends cmdline
install installkernel
917 # check_mount <dracut module> [<use_as_dep>] [<module path>]
918 # check_mount checks, if a dracut module is needed for the given
919 # device and filesystem types in "${host_fs_types[@]}"
926 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
927 # shellcheck disable=SC2154
928 [ "${#host_fs_types[@]}" -le 0 ] && return 1
930 # If we are already scheduled to be loaded, no need to check again.
931 [[ " $mods_to_load " == *\
$_mod\
* ]] && return 0
932 [[ " $mods_checked_as_dep " == *\
$_mod\
* ]] && return 1
934 # This should never happen, but...
935 [[ -d $_moddir ]] ||
return 1
937 [[ $2 ]] || mods_checked_as_dep
+=" $_mod "
939 # shellcheck disable=SC2154
940 if [[ " $omit_dracutmodules " == *\
$_mod\
* ]]; then
944 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\
$_mod\
* ]]; then
945 module_check_mount
"$_mod" "$_moddir"
948 # explicit module, so also accept ret=255
949 [[ $ret == 0 ||
$ret == 255 ]] ||
return 1
951 # module not in our list
952 if [[ $dracutmodules == all
]]; then
953 # check, if we can and should install this module
954 module_check_mount
"$_mod" "$_moddir" ||
return 1
961 for _moddep
in $
(module_depends
"$_mod" "$_moddir"); do
962 # handle deps as if they were manually added
963 [[ " $dracutmodules " == *\
$_mod\
* ]] \
964 && [[ " $dracutmodules " != *\
$_moddep\
* ]] \
965 && dracutmodules
+=" $_moddep "
966 [[ " $add_dracutmodules " == *\
$_mod\
* ]] \
967 && [[ " $add_dracutmodules " != *\
$_moddep\
* ]] \
968 && add_dracutmodules
+=" $_moddep "
969 [[ " $force_add_dracutmodules " == *\
$_mod\
* ]] \
970 && [[ " $force_add_dracutmodules " != *\
$_moddep\
* ]] \
971 && force_add_dracutmodules
+=" $_moddep "
972 # if a module we depend on fail, fail also
973 if ! check_module
"$_moddep"; then
974 derror
"dracut module '$_mod' depends on '$_moddep', which can't be installed"
979 [[ " $mods_to_load " == *\
$_mod\
* ]] \
980 || mods_to_load
+=" $_mod "
985 # check_module <dracut module> [<use_as_dep>] [<module path>]
986 # check if a dracut module is to be used in the initramfs process
987 # if <use_as_dep> is set, then the process also keeps track
988 # that the modules were checked for the dependency tracking process
995 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
996 # If we are already scheduled to be loaded, no need to check again.
997 [[ " $mods_to_load " == *\
$_mod\
* ]] && return 0
998 [[ " $mods_checked_as_dep " == *\
$_mod\
* ]] && return 1
1000 # This should never happen, but...
1001 [[ -d $_moddir ]] ||
return 1
1003 [[ $2 ]] || mods_checked_as_dep
+=" $_mod "
1005 if [[ " $omit_dracutmodules " == *\
$_mod\
* ]]; then
1006 ddebug
"dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
1010 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\
$_mod\
* ]]; then
1011 if [[ " $dracutmodules $force_add_dracutmodules " == *\
$_mod\
* ]]; then
1012 module_check
"$_mod" 1 "$_moddir"
1015 module_check
"$_mod" 0 "$_moddir"
1018 # explicit module, so also accept ret=255
1019 [[ $ret == 0 ||
$ret == 255 ]] ||
return 1
1021 # module not in our list
1022 if [[ $dracutmodules == all
]]; then
1023 # check, if we can and should install this module
1024 module_check
"$_mod" 0 "$_moddir"
1026 if [[ $ret != 0 ]]; then
1027 [[ $2 ]] && return 1
1028 [[ $ret != 255 ]] && return 1
1036 for _moddep
in $
(module_depends
"$_mod" "$_moddir"); do
1037 # handle deps as if they were manually added
1038 [[ " $dracutmodules " == *\
$_mod\
* ]] \
1039 && [[ " $dracutmodules " != *\
$_moddep\
* ]] \
1040 && dracutmodules
+=" $_moddep "
1041 [[ " $add_dracutmodules " == *\
$_mod\
* ]] \
1042 && [[ " $add_dracutmodules " != *\
$_moddep\
* ]] \
1043 && add_dracutmodules
+=" $_moddep "
1044 [[ " $force_add_dracutmodules " == *\
$_mod\
* ]] \
1045 && [[ " $force_add_dracutmodules " != *\
$_moddep\
* ]] \
1046 && force_add_dracutmodules
+=" $_moddep "
1047 # if a module we depend on fail, fail also
1048 if ! check_module
"$_moddep"; then
1049 derror
"dracut module '$_mod' depends on '$_moddep', which can't be installed"
1054 [[ " $mods_to_load " == *\
$_mod\
* ]] \
1055 || mods_to_load
+=" $_mod "
1060 # for_each_module_dir <func>
1061 # execute "<func> <dracut module> 1 <module path>"
1062 for_each_module_dir
() {
1068 for _moddir
in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1069 [[ -d $_moddir ]] ||
continue
1070 [[ -e $_moddir/install ||
-e $_moddir/installkernel ||
-e \
1071 $_moddir/module-setup.sh
]] ||
continue
1073 _mod
=${_mod#[0-9][0-9]}
1074 $_func "$_mod" 1 "$_moddir"
1077 # Report any missing dracut modules, the user has specified
1078 _modcheck
="$add_dracutmodules $force_add_dracutmodules"
1079 [[ $dracutmodules != all
]] && _modcheck
="$_modcheck $dracutmodules"
1080 for _mod
in $_modcheck; do
1081 [[ " $mods_to_load " == *\
$_mod\
* ]] && continue
1083 [[ " $force_add_dracutmodules " != *\
$_mod\
* ]] \
1084 && [[ " $dracutmodules " != *\
$_mod\
* ]] \
1085 && [[ " $omit_dracutmodules " == *\
$_mod\
* ]] \
1088 derror
"dracut module '$_mod' cannot be found or installed."
1089 [[ " $force_add_dracutmodules " == *\
$_mod\
* ]] && exit 1
1090 [[ " $dracutmodules " == *\
$_mod\
* ]] && exit 1
1091 [[ " $add_dracutmodules " == *\
$_mod\
* ]] && exit 1
1095 dracut_kernel_post
() {
1096 for _f
in modules.
builtin modules.
builtin.
alias modules.
builtin.modinfo modules.order
; do
1097 [[ -e $srcmods/$_f ]] && inst_simple
"$srcmods/$_f" "/lib/modules/$kernel/$_f"
1100 # generate module dependencies for the initrd
1101 if [[ -d $initdir/lib
/modules
/$kernel ]] \
1102 && ! depmod
-a -b "$initdir" "$kernel"; then
1103 dfatal
"\"depmod -a $kernel\" failed."
1110 # instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1111 # instmods [-c [-s]] <kernel subsystem>
1112 # install kernel modules along with all their dependencies.
1113 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1116 local _optional
="-o"
1120 [[ $no_kernel == yes ]] && return
1122 if [[ $1 == '-c' ]]; then
1126 if [[ $1 == '-s' ]]; then
1131 if (($# == 0)); then
1132 read -r -d '' -a args
1136 if (($# == 0)); then
1141 ${initdir:+-D "$initdir"} \
1142 ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
1143 ${loginstall:+-L "$loginstall"} \
1145 ${omit_drivers:+-N "$omit_drivers"} \
1146 ${srcmods:+--kerneldir "$srcmods"} \
1148 ${_silent:+--silent} \
1152 if ((_ret
!= 0)) && [[ -z $_silent ]]; then
1155 ${initdir:+-D "$initdir"} \
1156 ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
1157 ${loginstall:+-L "$loginstall"} \
1159 ${omit_drivers:+-N "$omit_drivers"} \
1160 ${srcmods:+--kerneldir "$srcmods"} \
1162 ${_silent:+--silent} \
1166 [[ "$_optional" ]] && return 0
1170 if [[ "$(ln --help)" == *--relative* ]]; then
1172 ln -sfnr "${initdir}/$1" "${initdir}/$2"
1178 [[ -d ${_dest%/*} ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
1179 ln -sfn -- "$(convert_abs_rel "${_dest}" "${_source}")" "${initdir}/${_dest}"
1183 is_qemu_virtualized() {
1184 # 0 if a virt environment was detected
1185 # 1 if a virt environment could not be detected
1186 # 255 if any error was encountered
1187 if type -P systemd-detect-virt > /dev/null 2>&1; then
1188 if ! vm=$(systemd-detect-virt --vm > /dev/null 2>&1); then
1191 [[ $vm == "qemu
" ]] && return 0
1192 [[ $vm == "kvm
" ]] && return 0
1193 [[ $vm == "bochs
" ]] && return 0
1196 for i in /sys/class/dmi/id/*_vendor; do
1197 [[ -f $i ]] || continue
1198 read -r vendor < "$i"
1199 [[ $vendor == "QEMU
" ]] && return 0
1200 [[ $vendor == "Red Hat
" ]] && return 0
1201 [[ $vendor == "Bochs
" ]] && return 0