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/src
/install
/dracut-install
]]; then
184 DRACUT_INSTALL
=$dracutbasedir/src
/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} "$@"
274 local _ret _hostonly_install
275 if [[ $1 == "-H" ]]; then
276 _hostonly_install
="-H"
279 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
283 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} "$@"
295 # shellcheck disable=SC2154
296 [[ $no_kernel == yes ]] && return
298 [[ $i == "--silent" ]] && _silent
=1
301 if "$DRACUT_INSTALL" \
302 ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
303 ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${hostonly:+-H} ${omit_drivers:+-N "$omit_drivers"} ${srcmods:+--kerneldir "$srcmods"} -m "$@"; then
307 if ((_silent
== 0)); then
308 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${hostonly:+-H} ${omit_drivers:+-N "$omit_drivers"} ${srcmods:+--kerneldir "$srcmods"} -m "$@"
315 local _ret _hostonly_install
316 if [[ $1 == "-H" ]]; then
317 _hostonly_install
="-H"
320 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
321 [[ -e $1 ]] ||
return 1 # no source
322 if "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"; then
326 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
333 if "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"; then
337 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
344 if "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"; then
348 derror FAILED
: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
354 local _ret _helper
="/run/dracut/fsck/fsck_help_$1.txt"
355 if "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$2" "$_helper"; then
359 derror
"$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$2" "$_helper"
364 # Use with form hostonly="$(optional_hostonly)" inst_xxxx <args>
365 # If hosotnly mode is set to "strict", hostonly restrictions will still
366 # be applied, else will ignore hostonly mode and try to install all
368 optional_hostonly
() {
369 # shellcheck disable=SC2154
370 if [[ $hostonly_mode == "strict" ]]; then
371 printf -- "%s" "$hostonly"
379 echo "$i" >> "$initdir/lib/dracut/hostonly-files"
383 # find symlinks linked to given library file
385 # Function searches for symlinks by stripping version numbers appended to
386 # library filename, checks if it points to the same target and finally
387 # prints the list of symlinks to stdout.
390 # rev_lib_symlinks libfoo.so.8.1
391 # output: libfoo.so.8 libfoo.so
392 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
398 [[ ! $1 ]] && return 0
401 _orig
="$(readlink -f "$1")"
404 [[ ${_fn} == *.so.
* ]] ||
return 1
406 until [[ ${_fn##*.} == so
]]; do
408 [[ -L ${_fn} ]] && [[ $(readlink -f "${_fn}") == "${_orig}" ]] && _links+=("${_fn}")
414 # attempt to install any programs specified in a udev rule
415 inst_rule_programs() {
418 # shellcheck disable=SC2013
419 for _prog in $(sed -nr 's/.*PROGRAM==?"([^
"]+).*/\1/p' "$1"); do
421 # shellcheck disable=SC2154
422 if [[ -x ${udevdir}/$_prog ]]; then
423 _bin="${udevdir}"/$_prog
424 elif [[ ${_prog/\$env\{/} == "$_prog" ]]; then
425 _bin=$(find_binary "$_prog") || {
426 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
431 [[ $_bin ]] && inst_binary "$_bin"
434 # shellcheck disable=SC2013
435 for _prog in $(sed -nr 's/.*RUN[+=]=?"([^
"]+).*/\1/p' "$1"); do
437 if [[ -x ${udevdir}/$_prog ]]; then
438 _bin=${udevdir}/$_prog
439 elif [[ ${_prog/\$env\{/} == "$_prog" ]] && [[ ${_prog} != "/sbin
/initqueue
" ]]; then
440 _bin=$(find_binary "$_prog") || {
441 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
446 [[ $_bin ]] && inst_binary "$_bin"
449 # shellcheck disable=SC2013
450 for _prog in $(sed -nr 's/.*IMPORT\{program\}==?"([^
"]+).*/\1/p' "$1"); do
452 if [[ -x ${udevdir}/$_prog ]]; then
453 _bin=${udevdir}/$_prog
454 elif [[ ${_prog/\$env\{/} == "$_prog" ]]; then
455 _bin=$(find_binary "$_prog") || {
456 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
461 [[ $_bin ]] && dracut_install "$_bin"
465 # attempt to create any groups and users specified in a udev rule
466 inst_rule_group_owner() {
469 # shellcheck disable=SC2013
470 for i in $(sed -nr 's/.*OWNER=?"([^
"]+).*/\1/p' "$1"); do
471 if ! grep -Eq "^
$i:" "$initdir/etc
/passwd
" 2> /dev/null; then
472 grep -E "^
$i:" "$dracutsysrootdir"/etc/passwd 2> /dev/null >> "$initdir/etc
/passwd
"
476 # shellcheck disable=SC2013
477 for i in $(sed -nr 's/.*GROUP=?"([^
"]+).*/\1/p' "$1"); do
478 if ! grep -Eq "^
$i:" "$initdir/etc
/group
" 2> /dev/null; then
479 grep -E "^
$i:" "$dracutsysrootdir"/etc/group 2> /dev/null >> "$initdir/etc
/group
"
484 inst_rule_initqueue() {
485 if grep -q -F initqueue "$1"; then
486 dracut_need_initqueue
490 # udev rules always get installed in the same place, so
491 # create a function to install them to make life simpler.
493 local _target=/etc/udev/rules.d _rule _found
495 inst_dir "${udevdir}/rules.d
"
497 for _rule in "$@
"; do
498 if [ "${_rule#/}" = "$_rule" ]; then
499 for r in "$dracutsysrootdir${udevdir}/rules.d
" ${hostonly:+"$dracutsysrootdir"/etc/udev/rules.d}; do
500 [[ -e $r/$_rule ]] || continue
502 inst_rule_programs "$_found"
503 inst_rule_group_owner "$_found"
504 inst_rule_initqueue "$_found"
505 inst_simple "$_found"
508 for r in '' "$dracutsysrootdir$dracutbasedir/rules.d
/"; do
509 # skip rules without an absolute path
510 [[ "${r}$_rule" != /* ]] && continue
511 [[ -f ${r}$_rule ]] || continue
513 inst_rule_programs "$_found"
514 inst_rule_group_owner "$_found"
515 inst_rule_initqueue "$_found"
516 inst_simple "$_found" "$_target/${_found##*/}"
518 [[ $_found ]] || dinfo "Skipping udev rule
: $_rule"
522 inst_rules_wildcard() {
523 local _target=/etc/udev/rules.d _rule _found
525 inst_dir "${udevdir}/rules.d
"
527 for _rule in ${udevdir}/rules.d/$1 ${dracutbasedir}/rules.d/$1; do
528 [[ -e $_rule ]] || continue
529 inst_rule_programs "$_rule"
530 inst_rule_group_owner "$_rule"
531 inst_rule_initqueue "$_rule"
535 if [[ -n ${hostonly} ]]; then
536 for _rule in ${_target}/$1; do
537 [[ -f $_rule ]] || continue
538 inst_rule_programs "$_rule"
539 inst_rule_group_owner "$_rule"
540 inst_rule_initqueue "$_rule"
545 [[ $_found ]] || dinfo "Skipping udev rule
: $_rule"
548 # make sure that library links are correct and up to date
550 for f in "$dracutsysrootdir"/etc/ld.so.conf "$dracutsysrootdir"/etc/ld.so.conf.d/*; do
551 [[ -f $f ]] && inst_simple "${f#$dracutsysrootdir}"
553 if ! $DRACUT_LDCONFIG -r "$initdir" -f /etc/ld.so.conf; then
554 if [[ $EUID == 0 ]]; then
555 derror "ldconfig exited ungracefully
"
557 derror "ldconfig might need uid
=0 (root
) for chroot
()"
562 prepare_udev_rules() {
563 if [ -z "$UDEVVERSION" ]; then
564 UDEVVERSION=$(udevadm --version)
568 if [ -z "$UDEVVERSION" ]; then
569 derror "Failed to detect udev version
!"
572 if [ -z "${UDEVVERSION##*[!0-9]*}" ]; then
573 derror "udevadm
--version did not report an integer
, udev version cannot be determined
!"
578 f="${initdir}/etc
/udev
/rules.d
/$f"
579 [ -e "$f" ] || continue
580 while read -r line || [ -n "$line" ]; do
581 if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
582 if ((UDEVVERSION >= 174)); then
583 printf '%sIMPORT{builtin}="path_id
"\n' "${line%%IMPORT PATH_ID}"
585 printf '%sIMPORT{program}="path_id
%%p
"\n' "${line%%IMPORT PATH_ID}"
587 elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
588 if ((UDEVVERSION >= 176)); then
589 printf '%sIMPORT{builtin}="blkid
"\n' "${line%%IMPORT BLKID}"
591 # shellcheck disable=SC2016
592 printf '%sIMPORT{program}="/sbin
/blkid
-o udev
-p $tempnode"\n' "${line%%IMPORT BLKID}"
597 done < "${f}" > "${f}.new
"
602 # install function specialized for hooks
603 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
604 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
607 if ! [[ -f $3 ]]; then
608 dfatal "Cannot
install a hook
($3) that does not exist.
"
609 dfatal "Aborting initrd creation.
"
611 elif ! [[ $hookdirs == *$1* ]]; then
612 dfatal "No such hook
type $1. Aborting initrd creation.
"
615 hook="/lib
/dracut
/hooks
/${1}/${2}-${3##*/}"
616 inst_simple "$3" "$hook"
617 chmod u+x "$initdir/$hook"
620 # install any of listed files
622 # If first argument is '-d' and second some destination path, first accessible
623 # source is installed into this path, otherwise it will installed in the same
624 # path as source. If none of listed files was installed, function return 1.
625 # On first successful installation it returns with 0 status.
629 # inst_any -d /bin/foo /bin/bar /bin/baz
631 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
636 [[ $1 == '-d' ]] && to="$2" && shift 2
639 [[ -e $f ]] || continue
640 [[ $to ]] && inst "$f" "$to" && return 0
641 inst "$f" && return 0
647 # inst_libdir_file [-n <pattern>] <file> [<file>...]
648 # Install a <file> located on a lib directory to the initramfs image
649 # -n <pattern> install matching files
652 if [[ $1 == "-n" ]]; then
655 for _dir in $libdirs; do
657 for _f in "$dracutsysrootdir$_dir"/$_i; do
658 [[ ${_f#$dracutsysrootdir} =~ $_pattern ]] || continue
659 [[ -e $_f ]] && _files+=("${_f#$dracutsysrootdir}")
664 for _dir in $libdirs; do
666 for _f in "$dracutsysrootdir$_dir"/$_i; do
667 [[ -e $_f ]] && _files+=("${_f#$dracutsysrootdir}")
672 [[ ${#_files[@]} -gt 0 ]] && inst_multiple "${_files[@]}"
675 # get a command to decompress the given file
676 get_decompress_cmd() {
678 *.gz) echo 'gzip -f -d' ;;
679 *.bz2) echo 'bzip2 -d' ;;
680 *.xz) echo 'xz -f -d' ;;
681 *.zst) echo 'zstd -f -d ' ;;
685 # install function decompressing the target and handling symlinks
686 # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
688 # Function install targets in the same paths inside overlay but decompressed
689 # and without extensions (.gz, .bz2).
694 _cmd=$(get_decompress_cmd "${_src}")
695 [[ -z ${_cmd} ]] && return 1
696 inst_simple "${_src}"
697 # Decompress with chosen tool. We assume that tool changes name e.g.
698 # from 'name.gz' to 'name'.
699 ${_cmd} "${initdir}${_src}"
703 # It's similar to above, but if file is not compressed, performs standard
706 inst_opt_decompress
() {
710 inst_decompress
"${_src}" || inst
"${_src}"
714 # module_check <dracut module> [<forced>] [<module path>]
715 # execute the check() function of module-setup.sh of <dracut module>
716 # or the "check" script, if module-setup.sh is not found
717 # "check $hostonly" is called
722 local _hostonly
=$hostonly
723 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
724 [ $# -eq 2 ] && _forced
=$2
725 [[ -d $_moddir ]] ||
return 1
726 if [[ ! -f $_moddir/module-setup.sh
]]; then
727 # if we do not have a check script, we are unconditionally included
728 [[ -x $_moddir/check
]] ||
return 0
729 [[ $_forced != 0 ]] && unset hostonly
730 # don't quote $hostonly to leave argument empty
731 # shellcheck disable=SC2086
732 "$_moddir"/check
$hostonly
735 unset check depends cmdline
install installkernel
737 # shellcheck disable=SC1090
738 .
"$_moddir"/module-setup.sh
739 is_func check ||
return 0
740 [[ $_forced != 0 ]] && unset hostonly
741 # don't quote $hostonly to leave argument empty
742 # shellcheck disable=SC2086
743 moddir
="$_moddir" check
$hostonly
745 unset check depends cmdline
install installkernel
751 # module_check_mount <dracut module> [<module path>]
752 # execute the check() function of module-setup.sh of <dracut module>
753 # or the "check" script, if module-setup.sh is not found
754 # "mount_needs=1 check 0" is called
755 module_check_mount
() {
759 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
760 [[ -d $_moddir ]] ||
return 1
761 if [[ ! -f $_moddir/module-setup.sh
]]; then
762 # if we do not have a check script, we are unconditionally included
763 [[ -x $_moddir/check
]] ||
return 0
764 mount_needs
=1 "$_moddir"/check
0
767 unset check depends cmdline
install installkernel
769 # shellcheck disable=SC1090
770 .
"$_moddir"/module-setup.sh
771 moddir
=$_moddir check
0
773 unset check depends cmdline
install installkernel
779 # module_depends <dracut module> [<module path>]
780 # execute the depends() function of module-setup.sh of <dracut module>
781 # or the "depends" script, if module-setup.sh is not found
785 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
786 [[ -d $_moddir ]] ||
return 1
787 if [[ ! -f $_moddir/module-setup.sh
]]; then
788 # if we do not have a check script, we have no deps
789 [[ -x $_moddir/check
]] ||
return 0
793 unset check depends cmdline
install installkernel
795 # shellcheck disable=SC1090
796 .
"$_moddir"/module-setup.sh
797 moddir
=$_moddir depends
799 unset check depends cmdline
install installkernel
804 # module_cmdline <dracut module> [<module path>]
805 # execute the cmdline() function of module-setup.sh of <dracut module>
806 # or the "cmdline" script, if module-setup.sh is not found
810 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
811 [[ -d $_moddir ]] ||
return 1
812 if [[ ! -f $_moddir/module-setup.sh
]]; then
813 # shellcheck disable=SC1090
814 [[ -x $_moddir/cmdline
]] && .
"$_moddir/cmdline"
817 unset check depends cmdline
install installkernel
819 # shellcheck disable=SC1090
820 .
"$_moddir"/module-setup.sh
821 moddir
="$_moddir" cmdline
823 unset check depends cmdline
install installkernel
828 # module_install <dracut module> [<module path>]
829 # execute the install() function of module-setup.sh of <dracut module>
830 # or the "install" script, if module-setup.sh is not found
834 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
835 [[ -d $_moddir ]] ||
return 1
836 if [[ ! -f $_moddir/module-setup.sh
]]; then
837 # shellcheck disable=SC1090
838 [[ -x $_moddir/install ]] && .
"$_moddir/install"
841 unset check depends cmdline
install installkernel
843 # shellcheck disable=SC1090
844 .
"$_moddir"/module-setup.sh
845 moddir
="$_moddir" install
847 unset check depends cmdline
install installkernel
852 # module_installkernel <dracut module> [<module path>]
853 # execute the installkernel() function of module-setup.sh of <dracut module>
854 # or the "installkernel" script, if module-setup.sh is not found
855 module_installkernel
() {
858 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
859 [[ -d $_moddir ]] ||
return 1
860 if [[ ! -f $_moddir/module-setup.sh
]]; then
861 # shellcheck disable=SC1090
862 [[ -x $_moddir/installkernel
]] && .
"$_moddir/installkernel"
865 unset check depends cmdline
install installkernel
866 installkernel
() { true
; }
867 # shellcheck disable=SC1090
868 .
"$_moddir"/module-setup.sh
869 moddir
="$_moddir" installkernel
871 unset check depends cmdline
install installkernel
876 # check_mount <dracut module> [<use_as_dep>] [<module path>]
877 # check_mount checks, if a dracut module is needed for the given
878 # device and filesystem types in "${host_fs_types[@]}"
885 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
886 # shellcheck disable=SC2154
887 [ "${#host_fs_types[@]}" -le 0 ] && return 1
889 # If we are already scheduled to be loaded, no need to check again.
890 [[ " $mods_to_load " == *\
$_mod\
* ]] && return 0
891 [[ " $mods_checked_as_dep " == *\
$_mod\
* ]] && return 1
893 # This should never happen, but...
894 [[ -d $_moddir ]] ||
return 1
896 [[ $2 ]] || mods_checked_as_dep
+=" $_mod "
898 # shellcheck disable=SC2154
899 if [[ " $omit_dracutmodules " == *\
$_mod\
* ]]; then
903 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\
$_mod\
* ]]; then
904 module_check_mount
"$_mod" "$_moddir"
907 # explicit module, so also accept ret=255
908 [[ $ret == 0 ||
$ret == 255 ]] ||
return 1
910 # module not in our list
911 if [[ $dracutmodules == all
]]; then
912 # check, if we can and should install this module
913 module_check_mount
"$_mod" "$_moddir" ||
return 1
920 for _moddep
in $
(module_depends
"$_mod" "$_moddir"); do
921 # handle deps as if they were manually added
922 [[ " $dracutmodules " == *\
$_mod\
* ]] \
923 && [[ " $dracutmodules " != *\
$_moddep\
* ]] \
924 && dracutmodules
+=" $_moddep "
925 [[ " $add_dracutmodules " == *\
$_mod\
* ]] \
926 && [[ " $add_dracutmodules " != *\
$_moddep\
* ]] \
927 && add_dracutmodules
+=" $_moddep "
928 [[ " $force_add_dracutmodules " == *\
$_mod\
* ]] \
929 && [[ " $force_add_dracutmodules " != *\
$_moddep\
* ]] \
930 && force_add_dracutmodules
+=" $_moddep "
931 # if a module we depend on fail, fail also
932 if ! check_module
"$_moddep"; then
933 derror
"dracut module '$_mod' depends on '$_moddep', which can't be installed"
938 [[ " $mods_to_load " == *\
$_mod\
* ]] \
939 || mods_to_load
+=" $_mod "
944 # check_module <dracut module> [<use_as_dep>] [<module path>]
945 # check if a dracut module is to be used in the initramfs process
946 # if <use_as_dep> is set, then the process also keeps track
947 # that the modules were checked for the dependency tracking process
954 [[ -z $_moddir ]] && _moddir
=$
(dracut_module_path
"$1")
955 # If we are already scheduled to be loaded, no need to check again.
956 [[ " $mods_to_load " == *\
$_mod\
* ]] && return 0
957 [[ " $mods_checked_as_dep " == *\
$_mod\
* ]] && return 1
959 # This should never happen, but...
960 [[ -d $_moddir ]] ||
return 1
962 [[ $2 ]] || mods_checked_as_dep
+=" $_mod "
964 if [[ " $omit_dracutmodules " == *\
$_mod\
* ]]; then
965 ddebug
"dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
969 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\
$_mod\
* ]]; then
970 if [[ " $dracutmodules $force_add_dracutmodules " == *\
$_mod\
* ]]; then
971 module_check
"$_mod" 1 "$_moddir"
974 module_check
"$_mod" 0 "$_moddir"
977 # explicit module, so also accept ret=255
978 [[ $ret == 0 ||
$ret == 255 ]] ||
return 1
980 # module not in our list
981 if [[ $dracutmodules == all
]]; then
982 # check, if we can and should install this module
983 module_check
"$_mod" 0 "$_moddir"
985 if [[ $ret != 0 ]]; then
987 [[ $ret != 255 ]] && return 1
995 for _moddep
in $
(module_depends
"$_mod" "$_moddir"); do
996 # handle deps as if they were manually added
997 [[ " $dracutmodules " == *\
$_mod\
* ]] \
998 && [[ " $dracutmodules " != *\
$_moddep\
* ]] \
999 && dracutmodules
+=" $_moddep "
1000 [[ " $add_dracutmodules " == *\
$_mod\
* ]] \
1001 && [[ " $add_dracutmodules " != *\
$_moddep\
* ]] \
1002 && add_dracutmodules
+=" $_moddep "
1003 [[ " $force_add_dracutmodules " == *\
$_mod\
* ]] \
1004 && [[ " $force_add_dracutmodules " != *\
$_moddep\
* ]] \
1005 && force_add_dracutmodules
+=" $_moddep "
1006 # if a module we depend on fail, fail also
1007 if ! check_module
"$_moddep"; then
1008 derror
"dracut module '$_mod' depends on '$_moddep', which can't be installed"
1013 [[ " $mods_to_load " == *\
$_mod\
* ]] \
1014 || mods_to_load
+=" $_mod "
1019 # for_each_module_dir <func>
1020 # execute "<func> <dracut module> 1 <module path>"
1021 for_each_module_dir
() {
1027 for _moddir
in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1028 [[ -d $_moddir ]] ||
continue
1029 [[ -e $_moddir/install ||
-e $_moddir/installkernel ||
-e \
1030 $_moddir/module-setup.sh
]] ||
continue
1032 _mod
=${_mod#[0-9][0-9]}
1033 $_func "$_mod" 1 "$_moddir"
1036 # Report any missing dracut modules, the user has specified
1037 _modcheck
="$add_dracutmodules $force_add_dracutmodules"
1038 [[ $dracutmodules != all
]] && _modcheck
="$_modcheck $dracutmodules"
1039 for _mod
in $_modcheck; do
1040 [[ " $mods_to_load " == *\
$_mod\
* ]] && continue
1042 [[ " $force_add_dracutmodules " != *\
$_mod\
* ]] \
1043 && [[ " $dracutmodules " != *\
$_mod\
* ]] \
1044 && [[ " $omit_dracutmodules " == *\
$_mod\
* ]] \
1047 derror
"dracut module '$_mod' cannot be found or installed."
1048 [[ " $force_add_dracutmodules " == *\
$_mod\
* ]] && exit 1
1049 [[ " $dracutmodules " == *\
$_mod\
* ]] && exit 1
1050 [[ " $add_dracutmodules " == *\
$_mod\
* ]] && exit 1
1054 dracut_kernel_post
() {
1055 for _f
in modules.
builtin modules.
builtin.
alias modules.
builtin.modinfo modules.order
; do
1056 [[ -e $srcmods/$_f ]] && inst_simple
"$srcmods/$_f" "/lib/modules/$kernel/$_f"
1059 # generate module dependencies for the initrd
1060 if [[ -d $initdir/lib
/modules
/$kernel ]] \
1061 && ! depmod
-a -b "$initdir" "$kernel"; then
1062 dfatal
"\"depmod -a $kernel\" failed."
1069 # instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1070 # instmods [-c [-s]] <kernel subsystem>
1071 # install kernel modules along with all their dependencies.
1072 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1075 local _optional
="-o"
1079 [[ $no_kernel == yes ]] && return
1081 if [[ $1 == '-c' ]]; then
1085 if [[ $1 == '-s' ]]; then
1090 if (($# == 0)); then
1091 read -r -d '' -a args
1095 if (($# == 0)); then
1100 ${initdir:+-D "$initdir"} \
1101 ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
1102 ${loginstall:+-L "$loginstall"} \
1104 ${omit_drivers:+-N "$omit_drivers"} \
1105 ${srcmods:+--kerneldir "$srcmods"} \
1107 ${_silent:+--silent} \
1111 if ((_ret
!= 0)) && [[ -z $_silent ]]; then
1114 ${initdir:+-D "$initdir"} \
1115 ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
1116 ${loginstall:+-L "$loginstall"} \
1118 ${omit_drivers:+-N "$omit_drivers"} \
1119 ${srcmods:+--kerneldir "$srcmods"} \
1121 ${_silent:+--silent} \
1125 [[ "$optional" ]] && return 0
1129 if [[ "$(ln --help)" == *--relative* ]]; then
1131 ln -sfnr "${initdir}/$1" "${initdir}/$2"
1137 [[ -d ${_dest%/*} ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
1138 ln -sfn -- "$(convert_abs_rel "${_dest}" "${_source}")" "${initdir}/${_dest}"
1142 is_qemu_virtualized() {
1143 # 0 if a virt environment was detected
1144 # 1 if a virt environment could not be detected
1145 # 255 if any error was encountered
1146 if type -P systemd-detect-virt > /dev/null 2>&1; then
1147 if ! vm=$(systemd-detect-virt --vm > /dev/null 2>&1); then
1150 [[ $vm == "qemu
" ]] && return 0
1151 [[ $vm == "kvm
" ]] && return 0
1152 [[ $vm == "bochs
" ]] && return 0
1155 for i in /sys/class/dmi/id/*_vendor; do
1156 [[ -f $i ]] || continue
1157 read -r vendor < "$i"
1158 [[ $vendor == "QEMU
" ]] && return 0
1159 [[ $vendor == "Red Hat
" ]] && return 0
1160 [[ $vendor == "Bochs
" ]] && return 0