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 if ! [[ $initdir ]]; then
48 dfatal
"initdir not set"
52 if ! [[ -d $initdir ]]; then
56 if ! [[ $kernel ]]; then
61 srcmods
="$dracutsysrootdir/lib/modules/$kernel/"
63 [[ $drivers_dir ]] && {
64 if ! command -v kmod
&> /dev
/null
&& vercmp
"$(modprobe --version | cut -d' ' -f3)" lt
3.7; then
65 dfatal
'To use --kmoddir option module-init-tools >= 3.7 is required.'
68 srcmods
="$drivers_dir"
72 # export standard hookdirs
74 hookdirs
="cmdline pre-udev pre-trigger netroot "
75 hookdirs
+="initqueue initqueue/settled initqueue/online initqueue/finished initqueue/timeout "
76 hookdirs
+="pre-mount pre-pivot cleanup mount "
77 hookdirs
+="emergency shutdown-emergency pre-shutdown shutdown "
81 DRACUT_LDD
=${DRACUT_LDD:-ldd}
82 DRACUT_TESTBIN
=${DRACUT_TESTBIN:-/bin/sh}
83 DRACUT_LDCONFIG
=${DRACUT_LDCONFIG:-ldconfig}
85 # shellcheck source=./dracut-functions.sh
86 .
"$dracutbasedir"/dracut-functions.sh
89 if ! [[ $libdirs ]]; then
90 if [[ $
("$DRACUT_LDD" "$dracutsysrootdir$DRACUT_TESTBIN") == */lib64
/* ]] &> /dev
/null \
91 && [[ -d $dracutsysrootdir/lib64
]]; then
93 [[ -d $dracutsysrootdir/usr
/lib64
]] && libdirs
+=" /usr/lib64"
96 [[ -d $dracutsysrootdir/usr
/lib
]] && libdirs
+=" /usr/lib"
99 libdirs
+=" $(ldconfig_paths)"
104 # helper function for check() in module-setup.sh
105 # to check for required installed binaries
106 # issues a standardized warning message
108 local _module_name
="${moddir##*/}"
111 if [[ $1 == "-m" ]]; then
117 if ! find_binary
"$cmd" &> /dev
/null
; then
118 dinfo
"Module '${_module_name#[0-9][0-9]}' will not be installed, because command '$cmd' could not be found!"
125 require_any_binary
() {
126 local _module_name
="${moddir##*/}"
129 if [[ $1 == "-m" ]]; then
135 if find_binary
"$cmd" &> /dev
/null
; then
141 if ((_ret
!= 0)); then
142 dinfo
"$_module_name: Could not find any command of '$*'!"
149 # helper function for check() in module-setup.sh
150 # to check for required kernel modules
151 # issues a standardized warning message
152 require_kernel_modules
() {
153 local _module_name
="${moddir##*/}"
156 # Ignore kernel module requirement for no-kernel build
157 [[ $no_kernel == yes ]] && return 0
159 if [[ $1 == "-m" ]]; then
165 if ! check_kernel_module
"$mod" &> /dev
/null
; then
166 dinfo
"Module '${_module_name#[0-9][0-9]}' will not be installed, because kernel module '$mod' is not available!"
173 dracut_need_initqueue
() {
174 : > "$initdir/lib/dracut/need-initqueue"
177 dracut_module_included
() {
178 [[ " $mods_to_load $modules_loaded " == *\ $
*\
* ]]
181 dracut_no_switch_root
() {
182 : > "$initdir/lib/dracut/no-switch-root"
185 dracut_module_path
() {
188 # shellcheck disable=SC2231
189 for _dir
in "${dracutbasedir}"/modules.d
/??
${1}; do
196 if ! [[ $DRACUT_INSTALL ]]; then
197 DRACUT_INSTALL
=$
(find_binary dracut-install
)
200 if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install
]]; then
201 DRACUT_INSTALL
=$dracutbasedir/dracut-install
202 elif ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/src
/install
/dracut-install
]]; then
203 DRACUT_INSTALL
=$dracutbasedir/src
/install
/dracut-install
206 # Test if dracut-install is a standalone executable with no options.
207 # E.g. DRACUT_INSTALL may be set externally as:
208 # DRACUT_INSTALL="valgrind dracut-install"
210 # DRACUT_INSTALL="dracut-install --debug"
211 # in which case the string cannot be tested for being executable.
213 for i
in $DRACUT_INSTALL; do
214 DRINSTALLPARTS
=$
((DRINSTALLPARTS
+ 1))
217 if [[ $DRINSTALLPARTS == 1 ]] && ! command -v "$DRACUT_INSTALL" > /dev
/null
2>&1; then
218 dfatal
"dracut-install not found!"
222 if [[ $hostonly == "-h" ]]; then
223 if ! [[ $DRACUT_KERNEL_MODALIASES ]] ||
! [[ -f $DRACUT_KERNEL_MODALIASES ]]; then
224 export DRACUT_KERNEL_MODALIASES
="${DRACUT_TMPDIR}/modaliases"
225 $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${srcmods:+--kerneldir "$srcmods"} --modalias > "$DRACUT_KERNEL_MODALIASES"
229 [[ $DRACUT_RESOLVE_LAZY ]] ||
export DRACUT_RESOLVE_DEPS
=1
232 [[ -e ${initdir}/"$1" ]] && return 0 # already there
233 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} -d "$@"; then
237 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} -d "$@"
243 local _ret _hostonly_install
244 if [[ $1 == "-H" ]]; then
245 _hostonly_install
="-H"
248 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
249 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"; then
253 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
259 local _ret _hostonly_install
260 if [[ $1 == "-H" ]]; then
261 _hostonly_install
="-H"
264 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
265 [[ -e $1 ]] ||
return 1 # no source
266 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${_hostonly_install:+-H} "$@"; then
270 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${_hostonly_install:+-H} "$@"
276 local _ret _hostonly_install
277 if [[ $1 == "-H" ]]; then
278 _hostonly_install
="-H"
281 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
282 [[ -L $1 ]] ||
return 1
283 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"; then
287 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
293 local _ret _hostonly_install
294 if [[ $1 == "-H" ]]; then
295 _hostonly_install
="-H"
298 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
302 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} "$@"
314 [[ $no_kernel == yes ]] && return
316 [[ $i == "--silent" ]] && _silent
=1
320 ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
321 ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${hostonly:+-H} ${omit_drivers:+-N "$omit_drivers"} ${srcmods:+--kerneldir "$srcmods"} -m "$@"; then
325 if ((_silent
== 0)); then
326 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${hostonly:+-H} ${omit_drivers:+-N "$omit_drivers"} ${srcmods:+--kerneldir "$srcmods"} -m "$@"
333 local _ret _hostonly_install
334 if [[ $1 == "-H" ]]; then
335 _hostonly_install
="-H"
338 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
339 [[ -e $1 ]] ||
return 1 # no source
340 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"; then
344 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
351 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"; then
355 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
362 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"; then
366 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
372 local _ret _helper
="/run/dracut/fsck/fsck_help_$1.txt"
373 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$2" "$_helper"; then
377 derror
"$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$2" "$_helper"
382 # Use with form hostonly="$(optional_hostonly)" inst_xxxx <args>
383 # If hostonly mode is set to "strict", hostonly restrictions will still
384 # be applied, else will ignore hostonly mode and try to install all
386 optional_hostonly
() {
387 if [[ $hostonly_mode == "strict" ]]; then
388 printf -- "%s" "$hostonly"
396 echo "$i" >> "$initdir/lib/dracut/hostonly-files"
400 # find symlinks linked to given library file
402 # Function searches for symlinks by stripping version numbers appended to
403 # library filename, checks if it points to the same target and finally
404 # prints the list of symlinks to stdout.
407 # rev_lib_symlinks libfoo.so.8.1
408 # output: libfoo.so.8 libfoo.so
409 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
415 [[ ! $1 ]] && return 0
418 _orig
="$(readlink -f "$1")"
421 [[ ${_fn} == *.so.
* ]] ||
return 1
423 until [[ ${_fn##*.} == so
]]; do
425 [[ -L ${_fn} ]] && [[ $(readlink -f "${_fn}") == "${_orig}" ]] && _links+=("${_fn}")
431 # attempt to install any programs specified in a udev rule
432 inst_rule_programs() {
435 # shellcheck disable=SC2013
436 for _prog in $(sed -nr 's/.*PROGRAM==?"([^
"]+).*/\1/p' "$1"); do
438 if [[ -x ${udevdir}/$_prog ]]; then
439 _bin="${udevdir}"/$_prog
440 elif [[ ${_prog/\$env\{/} == "$_prog" ]]; then
441 _bin=$(find_binary "$_prog") || {
442 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
447 [[ $_bin ]] && inst_binary "$_bin"
450 # shellcheck disable=SC2013
451 for _prog in $(sed -nr 's/.*RUN[+=]=?"([^
"]+).*/\1/p' "$1"); do
453 if [[ -x ${udevdir}/$_prog ]]; then
454 _bin=${udevdir}/$_prog
455 elif [[ ${_prog/\$env\{/} == "$_prog" ]] && [[ ${_prog} != "/sbin
/initqueue
" ]]; then
456 _bin=$(find_binary "$_prog") || {
457 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
462 [[ $_bin ]] && inst_binary "$_bin"
465 # shellcheck disable=SC2013
466 for _prog in $(sed -nr 's/.*IMPORT\{program\}==?"([^
"]+).*/\1/p' "$1"); do
468 if [[ -x ${udevdir}/$_prog ]]; then
469 _bin=${udevdir}/$_prog
470 elif [[ ${_prog/\$env\{/} == "$_prog" ]]; then
471 _bin=$(find_binary "$_prog") || {
472 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
477 [[ $_bin ]] && dracut_install "$_bin"
481 # attempt to create any groups and users specified in a udev rule
482 inst_rule_group_owner() {
485 # shellcheck disable=SC2013
486 for i in $(sed -nr 's/.*OWNER=?"([^
"]+).*/\1/p' "$1"); do
487 if ! grep -Eq "^
$i:" "$initdir/etc
/passwd
" 2> /dev/null; then
488 grep -E "^
$i:" "$dracutsysrootdir"/etc/passwd 2> /dev/null >> "$initdir/etc
/passwd
"
492 # shellcheck disable=SC2013
493 for i in $(sed -nr 's/.*GROUP=?"([^
"]+).*/\1/p' "$1"); do
494 if ! grep -Eq "^
$i:" "$initdir/etc
/group
" 2> /dev/null; then
495 grep -E "^
$i:" "$dracutsysrootdir"/etc/group 2> /dev/null >> "$initdir/etc
/group
"
500 inst_rule_initqueue() {
501 if grep -q -F initqueue "$1"; then
502 dracut_need_initqueue
506 # udev rules always get installed in the same place, so
507 # create a function to install them to make life simpler.
509 local _target=/etc/udev/rules.d _rule _found
511 inst_dir "${udevdir}/rules.d
"
513 for _rule in "$@
"; do
514 if [ "${_rule#/}" = "$_rule" ]; then
515 for r in "$dracutsysrootdir${udevdir}/rules.d
" ${hostonly:+"$dracutsysrootdir"/etc/udev/rules.d}; do
516 [[ -e $r/$_rule ]] || continue
518 inst_rule_programs "$_found"
519 inst_rule_group_owner "$_found"
520 inst_rule_initqueue "$_found"
521 inst_simple "$_found"
524 for r in '' "$dracutsysrootdir$dracutbasedir/rules.d
/"; do
525 # skip rules without an absolute path
526 [[ "${r}$_rule" != /* ]] && continue
527 [[ -f ${r}$_rule ]] || continue
529 inst_rule_programs "$_found"
530 inst_rule_group_owner "$_found"
531 inst_rule_initqueue "$_found"
532 inst_simple "$_found" "$_target/${_found##*/}"
534 [[ $_found ]] || ddebug "Skipping udev rule
: $_rule"
538 inst_rules_wildcard() {
539 local _target=/etc/udev/rules.d _rule _found
541 inst_dir "${udevdir}/rules.d
"
543 for _rule in ${udevdir}/rules.d/$1 ${dracutbasedir}/rules.d/$1; do
544 [[ -e $_rule ]] || continue
545 inst_rule_programs "$_rule"
546 inst_rule_group_owner "$_rule"
547 inst_rule_initqueue "$_rule"
551 if [[ -n ${hostonly} ]]; then
552 for _rule in ${_target}/$1; do
553 [[ -f $_rule ]] || continue
554 inst_rule_programs "$_rule"
555 inst_rule_group_owner "$_rule"
556 inst_rule_initqueue "$_rule"
561 [[ $_found ]] || ddebug "Skipping udev rule
: $_rule"
564 # make sure that library links are correct and up to date
566 for f in "$dracutsysrootdir"/etc/ld.so.conf "$dracutsysrootdir"/etc/ld.so.conf.d/*; do
567 [[ -f $f ]] && inst_simple "${f#"$dracutsysrootdir"}"
569 if ! $DRACUT_LDCONFIG -r "$initdir" -f /etc/ld.so.conf; then
570 if [[ $EUID == 0 ]]; then
571 derror "ldconfig exited ungracefully
"
573 derror "ldconfig might need uid
=0 (root
) for chroot
()"
578 prepare_udev_rules() {
579 dwarn "prepare_udev_rules
: deprecated and will be removed
"
581 if [ -z "$UDEVVERSION" ]; then
582 UDEVVERSION=$(udevadm --version)
586 if [ -z "$UDEVVERSION" ]; then
587 derror "Failed to detect udev version
!"
590 if [ -z "${UDEVVERSION##*[!0-9]*}" ]; then
591 derror "udevadm
--version did not report an integer
, udev version cannot be determined
!"
596 f="${initdir}/etc
/udev
/rules.d
/$f"
597 [ -e "$f" ] || continue
598 while read -r line || [ -n "$line" ]; do
599 if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
600 if ((UDEVVERSION >= 174)); then
601 printf '%sIMPORT{builtin}="path_id
"\n' "${line%%IMPORT PATH_ID}"
603 printf '%sIMPORT{program}="path_id
%%p
"\n' "${line%%IMPORT PATH_ID}"
605 elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
606 if ((UDEVVERSION >= 176)); then
607 printf '%sIMPORT{builtin}="blkid
"\n' "${line%%IMPORT BLKID}"
609 # shellcheck disable=SC2016
610 printf '%sIMPORT{program}="/sbin
/blkid
-o udev
-p $tempnode"\n' "${line%%IMPORT BLKID}"
615 done < "${f}" > "${f}.new
"
620 # install function specialized for hooks
621 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
622 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
625 if ! [[ -f $3 ]]; then
626 dfatal "Cannot
install a hook
($3) that does not exist.
"
627 dfatal "Aborting initrd creation.
"
629 elif ! [[ $hookdirs == *$1* ]]; then
630 dfatal "No such hook
type $1. Aborting initrd creation.
"
633 hook="/lib
/dracut
/hooks
/${1}/${2}-${3##*/}"
634 inst_simple "$3" "$hook"
635 chmod u+x "$initdir/$hook"
638 # install any of listed files
640 # If first argument is '-d' and second some destination path, first accessible
641 # source is installed into this path, otherwise it will installed in the same
642 # path as source. If none of listed files was installed, function return 1.
643 # On first successful installation it returns with 0 status.
647 # inst_any -d /bin/foo /bin/bar /bin/baz
649 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
654 [[ $1 == '-d' ]] && to="$2" && shift 2
657 [[ -e $f ]] || continue
658 [[ $to ]] && inst "$f" "$to" && return 0
659 inst "$f" && return 0
665 # inst_libdir_dir <dir> [<dir>...]
666 # Install a <dir> located on a lib directory to the initramfs image
669 for _dir in $libdirs; do
671 for _d in "$dracutsysrootdir$_dir"/$_i; do
672 [[ -d $_d ]] && _dirs+=("${_d#"$dracutsysrootdir"}")
676 for _dir in "${_dirs[@]}"; do
681 # inst_libdir_file [-n <pattern>] <file> [<file>...]
682 # Install a <file> located on a lib directory to the initramfs image
683 # -n <pattern> install matching files
686 if [[ $1 == "-n" ]]; then
689 for _dir in $libdirs; do
691 for _f in "$dracutsysrootdir$_dir"/$_i; do
692 [[ ${_f#"$dracutsysrootdir"} =~ $_pattern ]] || continue
693 [[ -e $_f ]] && _files+=("${_f#"$dracutsysrootdir"}")
698 for _dir in $libdirs; do
700 for _f in "$dracutsysrootdir$_dir"/$_i; do
701 [[ -e $_f ]] && _files+=("${_f#"$dracutsysrootdir"}")
706 [[ ${#_files[@]} -gt 0 ]] && inst_multiple "${_files[@]}"
709 # get a command to decompress the given file
710 get_decompress_cmd() {
712 *.gz) echo 'gzip -f -d' ;;
713 *.bz2) echo 'bzip2 -d' ;;
714 *.xz) echo 'xz -f -d' ;;
715 *.zst) echo 'zstd -f -d ' ;;
719 # install function decompressing the target and handling symlinks
720 # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
722 # Function install targets in the same paths inside overlay but decompressed
723 # and without extensions (.gz, .bz2).
728 _cmd=$(get_decompress_cmd "${_src}")
729 [[ -z ${_cmd} ]] && return 1
730 inst_simple "${_src}"
731 # Decompress with chosen tool. We assume that tool changes name e.g.
732 # from 'name.gz' to 'name'.
733 ${_cmd} "${initdir}${_src}"
737 # It's similar to above, but if file is not compressed, performs standard
740 inst_opt_decompress
() {
744 inst_decompress
"${_src}" || inst
"${_src}"
748 # module_check <dracut module> [<forced>] [<module path>]
749 # execute the check() function of module-setup.sh of <dracut module>
750 # or the "check" script, if module-setup.sh is not found
751 # "check $hostonly" is called
756 local _hostonly
=$hostonly
757 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
758 [ $# -ge 2 ] && _forced
=$2
759 [[ -f $_moddir/module-setup.sh
]] ||
return 1
760 unset check depends cmdline
install installkernel
762 # shellcheck disable=SC1090
763 .
"$_moddir"/module-setup.sh
764 is_func check ||
return 0
765 [[ $_forced != 0 ]] && unset hostonly
766 # don't quote $hostonly to leave argument empty
767 # shellcheck disable=SC2086
768 moddir
="$_moddir" check
$hostonly
770 unset check depends cmdline
install installkernel
775 # module_check_mount <dracut module> [<module path>]
776 # execute the check() function of module-setup.sh of <dracut module>
777 # or the "check" script, if module-setup.sh is not found
778 # "mount_needs=1 check 0" is called
779 module_check_mount
() {
783 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
784 [[ -f $_moddir/module-setup.sh
]] ||
return 1
785 unset check depends cmdline
install installkernel
787 # shellcheck disable=SC1090
788 .
"$_moddir"/module-setup.sh
789 moddir
=$_moddir check
0
791 unset check depends cmdline
install installkernel
796 # module_depends <dracut module> [<module path>]
797 # execute the depends() function of module-setup.sh of <dracut module>
798 # or the "depends" script, if module-setup.sh is not found
802 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
803 [[ -f $_moddir/module-setup.sh
]] ||
return 1
804 unset check depends cmdline
install installkernel
806 # shellcheck disable=SC1090
807 .
"$_moddir"/module-setup.sh
808 moddir
=$_moddir depends
810 unset check depends cmdline
install installkernel
814 # module_cmdline <dracut module> [<module path>]
815 # execute the cmdline() function of module-setup.sh of <dracut module>
816 # or the "cmdline" script, if module-setup.sh is not found
820 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
821 [[ -f $_moddir/module-setup.sh
]] ||
return 1
822 unset check depends cmdline
install installkernel
824 # shellcheck disable=SC1090
825 .
"$_moddir"/module-setup.sh
826 moddir
="$_moddir" cmdline
828 unset check depends cmdline
install installkernel
832 # module_install <dracut module> [<module path>]
833 # execute the install() function of module-setup.sh of <dracut module>
834 # or the "install" script, if module-setup.sh is not found
838 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
839 [[ -f $_moddir/module-setup.sh
]] ||
return 1
840 unset check depends cmdline
install installkernel
842 # shellcheck disable=SC1090
843 .
"$_moddir"/module-setup.sh
844 moddir
="$_moddir" install
846 unset check depends cmdline
install installkernel
850 # module_installkernel <dracut module> [<module path>]
851 # execute the installkernel() function of module-setup.sh of <dracut module>
852 # or the "installkernel" script, if module-setup.sh is not found
853 module_installkernel
() {
856 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
857 [[ -f $_moddir/module-setup.sh
]] ||
return 1
858 unset check depends cmdline
install installkernel
859 installkernel
() { true
; }
860 # shellcheck disable=SC1090
861 .
"$_moddir"/module-setup.sh
862 moddir
="$_moddir" installkernel
864 unset check depends cmdline
install installkernel
868 # check_mount <dracut module> [<use_as_dep>] [<module path>]
869 # check_mount checks, if a dracut module is needed for the given
870 # device and filesystem types in "${host_fs_types[@]}"
877 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
878 [ "${#host_fs_types[@]}" -le 0 ] && return 1
880 # If we are already scheduled to be loaded, no need to check again.
881 [[ " $mods_to_load " == *\
$_mod\
* ]] && return 0
882 [[ " $mods_checked_as_dep " == *\
$_mod\
* ]] && return 1
884 # This should never happen, but...
885 [[ -d $_moddir ]] ||
return 1
887 [[ $2 ]] || mods_checked_as_dep
+=" $_mod "
889 if [[ " $omit_dracutmodules " == *\
$_mod\
* ]]; then
893 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\
$_mod\
* ]]; then
894 module_check_mount
"$_mod" "$_moddir"
897 # explicit module, so also accept _ret=255
898 [[ $_ret == 0 ||
$_ret == 255 ]] ||
return 1
900 # module not in our list
901 if [[ $dracutmodules == all
]]; then
902 # check, if we can and should install this module
903 module_check_mount
"$_mod" "$_moddir" ||
return 1
910 for _moddep
in $
(module_depends
"$_mod" "$_moddir"); do
911 # handle deps as if they were manually added
912 [[ " $dracutmodules " == *\
$_mod\
* ]] \
913 && [[ " $dracutmodules " != *\
$_moddep\
* ]] \
914 && dracutmodules
+=" $_moddep "
915 [[ " $add_dracutmodules " == *\
$_mod\
* ]] \
916 && [[ " $add_dracutmodules " != *\
$_moddep\
* ]] \
917 && add_dracutmodules
+=" $_moddep "
918 [[ " $force_add_dracutmodules " == *\
$_mod\
* ]] \
919 && [[ " $force_add_dracutmodules " != *\
$_moddep\
* ]] \
920 && force_add_dracutmodules
+=" $_moddep "
921 # if a module we depend on fail, fail also
922 if ! check_module
"$_moddep"; then
923 derror
"Module '$_mod' depends on '$_moddep', which can't be installed"
928 [[ " $mods_to_load " == *\
$_mod\
* ]] \
929 || mods_to_load
+=" $_mod "
934 # check_module <dracut module> [<use_as_dep>] [<module path>]
935 # check if a dracut module is to be used in the initramfs process
936 # if <use_as_dep> is set, then the process also keeps track
937 # that the modules were checked for the dependency tracking process
944 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
945 # If we are already scheduled to be loaded, no need to check again.
946 [[ " $mods_to_load " == *\
$_mod\
* ]] && return 0
947 [[ " $mods_checked_as_dep " == *\
$_mod\
* ]] && return 1
949 # This should never happen, but...
950 [[ -d $_moddir ]] ||
return 1
952 [[ $2 ]] || mods_checked_as_dep
+=" $_mod "
954 if [[ " $omit_dracutmodules " == *\
$_mod\
* ]]; then
955 ddebug
"Module '$_mod' will not be installed, because it's in the list to be omitted!"
959 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\
$_mod\
* ]]; then
960 if [[ " $dracutmodules $force_add_dracutmodules " == *\
$_mod\
* ]]; then
961 module_check
"$_mod" 1 "$_moddir"
964 module_check
"$_mod" 0 "$_moddir"
967 # explicit module, so also accept _ret=255
968 [[ $_ret == 0 ||
$_ret == 255 ]] ||
return 1
970 # module not in our list
971 if [[ $dracutmodules == all
]]; then
972 # check, if we can and should install this module
973 module_check
"$_mod" 0 "$_moddir"
975 if [[ $_ret != 0 ]]; then
977 [[ $_ret != 255 ]] && return 1
985 for _moddep
in $
(module_depends
"$_mod" "$_moddir"); do
986 # handle deps as if they were manually added
987 [[ " $dracutmodules " == *\
$_mod\
* ]] \
988 && [[ " $dracutmodules " != *\
$_moddep\
* ]] \
989 && dracutmodules
+=" $_moddep "
990 [[ " $add_dracutmodules " == *\
$_mod\
* ]] \
991 && [[ " $add_dracutmodules " != *\
$_moddep\
* ]] \
992 && add_dracutmodules
+=" $_moddep "
993 [[ " $force_add_dracutmodules " == *\
$_mod\
* ]] \
994 && [[ " $force_add_dracutmodules " != *\
$_moddep\
* ]] \
995 && force_add_dracutmodules
+=" $_moddep "
996 # if a module we depend on fail, fail also
997 if ! check_module
"$_moddep"; then
998 derror
"Module '$_mod' depends on '$_moddep', which can't be installed"
1003 [[ " $mods_to_load " == *\
$_mod\
* ]] \
1004 || mods_to_load
+=" $_mod "
1009 # for_each_module_dir <func>
1010 # execute "<func> <dracut module> 1 <module path>"
1011 for_each_module_dir
() {
1018 for _moddir
in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1019 [[ -e $_moddir/module-setup.sh
]] ||
continue
1021 _mod
=${_mod#[0-9][0-9]}
1022 $_func "$_mod" 1 "$_moddir"
1025 # Report any missing dracut modules, the user has specified
1026 _modcheck
="$add_dracutmodules $force_add_dracutmodules"
1027 [[ $dracutmodules != all
]] && _modcheck
="$_modcheck $dracutmodules"
1028 for _mod
in $_modcheck; do
1029 [[ " $mods_to_load " == *\
$_mod\
* ]] && continue
1031 [[ " $force_add_dracutmodules " != *\
$_mod\
* ]] \
1032 && [[ " $dracutmodules " != *\
$_mod\
* ]] \
1033 && [[ " $omit_dracutmodules " == *\
$_mod\
* ]] \
1036 [[ -d $
(echo "$dracutbasedir/modules.d"/[0-9][0-9]"$_mod") ]] \
1037 && _reason
="installed" \
1039 derror
"Module '$_mod' cannot be $_reason."
1040 [[ " $force_add_dracutmodules " == *\
$_mod\
* ]] && exit 1
1041 [[ " $dracutmodules " == *\
$_mod\
* ]] && exit 1
1042 [[ " $add_dracutmodules " == *\
$_mod\
* ]] && exit 1
1046 dracut_kernel_post
() {
1047 for _f
in modules.
builtin modules.
builtin.
alias modules.
builtin.modinfo modules.order
; do
1048 [[ -e $srcmods/$_f ]] && inst_simple
"$srcmods/$_f" "/lib/modules/$kernel/$_f"
1051 # generate module dependencies for the initrd
1052 if [[ -d $initdir/lib
/modules
/$kernel ]] \
1053 && ! depmod
-a -b "$initdir" "$kernel"; then
1054 dfatal
"\"depmod -a $kernel\" failed."
1061 # instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1062 # instmods [-c [-s]] <kernel subsystem>
1063 # install kernel modules along with all their dependencies.
1064 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1067 local _optional
="-o"
1071 [[ $no_kernel == yes ]] && return
1073 if [[ $1 == '-c' ]]; then
1077 if [[ $1 == '-s' ]]; then
1082 if (($# == 0)); then
1083 read -r -d '' -a args
1087 if (($# == 0)); then
1092 ${initdir:+-D "$initdir"} \
1093 ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
1094 ${loginstall:+-L "$loginstall"} \
1096 ${omit_drivers:+-N "$omit_drivers"} \
1097 ${srcmods:+--kerneldir "$srcmods"} \
1099 ${_silent:+--silent} \
1103 if ((_ret
!= 0)) && [[ -z $_silent ]]; then
1106 ${initdir:+-D "$initdir"} \
1107 ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
1108 ${loginstall:+-L "$loginstall"} \
1110 ${omit_drivers:+-N "$omit_drivers"} \
1111 ${srcmods:+--kerneldir "$srcmods"} \
1113 ${_silent:+--silent} \
1117 [[ "$_optional" ]] && return 0
1121 if [[ "$(ln --help)" == *--relative* ]]; then
1123 ln -sfnr "${initdir}/$1" "${initdir}/$2"
1129 [[ -d ${_dest%/*} ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
1130 ln -sfn -- "$(convert_abs_rel "${_dest}" "${_source}")" "${initdir}/${_dest}"
1134 is_qemu_virtualized() {
1135 # 0 if a virt environment was detected
1136 # 1 if a virt environment could not be detected
1137 # 255 if any error was encountered
1138 if type -P systemd-detect-virt > /dev/null 2>&1; then
1139 if ! vm=$(systemd-detect-virt --vm 2> /dev/null); then
1142 [[ $vm == "qemu
" ]] && return 0
1143 [[ $vm == "kvm
" ]] && return 0
1144 [[ $vm == "bochs
" ]] && return 0
1147 for i in /sys/class/dmi/id/*_vendor; do
1148 [[ -f $i ]] || continue
1149 read -r vendor < "$i"
1150 [[ $vendor == "QEMU
" ]] && return 0
1151 [[ $vendor == "Red Hat
" ]] && return 0
1152 [[ $vendor == "Bochs
" ]] && return 0