]> git.ipfire.org Git - thirdparty/dracut.git/blobdiff - dracut-init.sh
fix: codespell
[thirdparty/dracut.git] / dracut-init.sh
old mode 100644 (file)
new mode 100755 (executable)
index e8100a4..654207d
 #
 export LC_MESSAGES=C
 
-export DRACUT_CP="cp --reflink=auto --sparse=auto --preserve=mode,timestamps,xattr,links -dfr"
+if [[ $EUID == "0" ]] && ! [[ $DRACUT_NO_XATTR ]]; then
+    export DRACUT_CP="cp --reflink=auto --sparse=auto --preserve=mode,timestamps,xattr,links -dfr"
+else
+    export DRACUT_CP="cp --reflink=auto --sparse=auto --preserve=mode,timestamps,links -dfr"
+fi
 
 # is_func <command>
 # Check whether $1 is a function.
 is_func() {
-    [[ "$(type -t "$1")" = "function" ]]
+    [[ "$(type -t "$1")" == "function" ]]
 }
 
 if ! [[ $dracutbasedir ]]; then
     dracutbasedir=${BASH_SOURCE[0]%/*}
-    [[ $dracutbasedir = dracut-functions* ]] && dracutbasedir="."
+    [[ $dracutbasedir == dracut-functions* ]] && dracutbasedir="."
     [[ $dracutbasedir ]] || dracutbasedir="."
     dracutbasedir="$(readlink -f $dracutbasedir)"
 fi
 
-if ! is_func dinfo >/dev/null 2>&1; then
+if ! is_func dinfo > /dev/null 2>&1; then
+    # shellcheck source=./dracut-logger.sh
     . "$dracutbasedir/dracut-logger.sh"
     dlog_init
 fi
 
+# shellcheck disable=SC2154
 if ! [[ $initdir ]]; then
     dfatal "initdir not set"
     exit 1
@@ -48,22 +54,17 @@ if ! [[ -d $initdir ]]; then
     mkdir -p "$initdir"
 fi
 
-if [[ $DRACUT_KERNEL_LAZY ]] && ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
-    if ! [[ -d "$initdir/.kernelmodseen" ]]; then
-        mkdir -p "$initdir/.kernelmodseen"
-    fi
-    DRACUT_KERNEL_LAZY_HASHDIR="$initdir/.kernelmodseen"
-fi
-
 if ! [[ $kernel ]]; then
     kernel=$(uname -r)
     export kernel
 fi
 
-srcmods="/lib/modules/$kernel/"
+# shellcheck disable=SC2154
+srcmods="$dracutsysrootdir/lib/modules/$kernel/"
 
+# shellcheck disable=SC2154
 [[ $drivers_dir ]] && {
-    if ! command -v kmod &>/dev/null && vercmp "$(modprobe --version | cut -d' ' -f3)" lt 3.7; then
+    if ! command -v kmod &> /dev/null && vercmp "$(modprobe --version | cut -d' ' -f3)" lt 3.7; then
         dfatal 'To use --kmoddir option module-init-tools >= 3.7 is required.'
         exit 1
     fi
@@ -71,8 +72,6 @@ srcmods="/lib/modules/$kernel/"
 }
 export srcmods
 
-[[ $DRACUT_FIRMWARE_PATH ]] || export DRACUT_FIRMWARE_PATH="/lib/firmware/updates:/lib/firmware:/lib/firmware/$kernel"
-
 # export standard hookdirs
 [[ $hookdirs ]] || {
     hookdirs="cmdline pre-udev pre-trigger netroot "
@@ -82,17 +81,22 @@ export srcmods
     export hookdirs
 }
 
-. $dracutbasedir/dracut-functions.sh
+DRACUT_LDD=${DRACUT_LDD:-ldd}
+DRACUT_TESTBIN=${DRACUT_TESTBIN:-/bin/sh}
+DRACUT_LDCONFIG=${DRACUT_LDCONFIG:-ldconfig}
+
+# shellcheck source=./dracut-functions.sh
+. "$dracutbasedir"/dracut-functions.sh
 
 # Detect lib paths
-if ! [[ $libdirs ]] ; then
-    if [[ "$(ldd /bin/sh)" == */lib64/* ]] &>/dev/null \
-        && [[ -d /lib64 ]]; then
+if ! [[ $libdirs ]]; then
+    if [[ $("$DRACUT_LDD" "$dracutsysrootdir$DRACUT_TESTBIN") == */lib64/* ]] &> /dev/null \
+        && [[ -d $dracutsysrootdir/lib64 ]]; then
         libdirs+=" /lib64"
-        [[ -d /usr/lib64 ]] && libdirs+=" /usr/lib64"
+        [[ -d $dracutsysrootdir/usr/lib64 ]] && libdirs+=" /usr/lib64"
     else
         libdirs+=" /lib"
-        [[ -d /usr/lib ]] && libdirs+=" /usr/lib"
+        [[ -d $dracutsysrootdir/usr/lib ]] && libdirs+=" /usr/lib"
     fi
 
     libdirs+=" $(ldconfig_paths)"
@@ -104,118 +108,206 @@ fi
 # to check for required installed binaries
 # issues a standardized warning message
 require_binaries() {
+    # shellcheck disable=SC2154
     local _module_name="${moddir##*/}"
     local _ret=0
 
-    if [[ "$1" = "-m" ]]; then
+    if [[ $1 == "-m" ]]; then
         _module_name="$2"
         shift 2
     fi
 
     for cmd in "$@"; do
-        if ! find_binary "$cmd" &>/dev/null; then
-            dinfo "dracut module '${_module_name#[0-9][0-9]}' will not be installed, because command '$cmd' could not be found!"
+        if ! find_binary "$cmd" &> /dev/null; then
+            dinfo "Module '${_module_name#[0-9][0-9]}' will not be installed, because command '$cmd' could not be found!"
             ((_ret++))
         fi
     done
-    return $_ret
+    return "$_ret"
 }
 
 require_any_binary() {
     local _module_name="${moddir##*/}"
     local _ret=1
 
-    if [[ "$1" = "-m" ]]; then
+    if [[ $1 == "-m" ]]; then
         _module_name="$2"
         shift 2
     fi
 
     for cmd in "$@"; do
-        if find_binary "$cmd" &>/dev/null; then
+        if find_binary "$cmd" &> /dev/null; then
             _ret=0
             break
         fi
     done
 
-    if (( $_ret != 0 )); then
-        dinfo "$_module_name: Could not find any command of '$@'!"
+    if ((_ret != 0)); then
+        dinfo "$_module_name: Could not find any command of '$*'!"
         return 1
     fi
 
     return 0
 }
 
+# helper function for check() in module-setup.sh
+# to check for required kernel modules
+# issues a standardized warning message
+require_kernel_modules() {
+    # shellcheck disable=SC2154
+    local _module_name="${moddir##*/}"
+    local _ret=0
+
+    # Ignore kernel module requirement for no-kernel build
+    [[ $no_kernel == yes ]] && return 0
+
+    if [[ $1 == "-m" ]]; then
+        _module_name="$2"
+        shift 2
+    fi
+
+    for mod in "$@"; do
+        if ! check_kernel_module "$mod" &> /dev/null; then
+            dinfo "Module '${_module_name#[0-9][0-9]}' will not be installed, because kernel module '$mod' is not available!"
+            ((_ret++))
+        fi
+    done
+    return "$_ret"
+}
+
 dracut_need_initqueue() {
-    >"$initdir/lib/dracut/need-initqueue"
+    : > "$initdir/lib/dracut/need-initqueue"
 }
 
 dracut_module_included() {
+    # shellcheck disable=SC2154
     [[ " $mods_to_load $modules_loaded " == *\ $*\ * ]]
 }
 
+dracut_no_switch_root() {
+    : > "$initdir/lib/dracut/no-switch-root"
+}
+
+dracut_module_path() {
+    local _dir
+
+    # shellcheck disable=SC2231
+    for _dir in "${dracutbasedir}"/modules.d/??${1}; do
+        echo "$_dir"
+        return 0
+    done
+    return 1
+}
+
 if ! [[ $DRACUT_INSTALL ]]; then
     DRACUT_INSTALL=$(find_binary dracut-install)
 fi
 
 if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install ]]; then
     DRACUT_INSTALL=$dracutbasedir/dracut-install
-elif ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/install/dracut-install ]]; then
-    DRACUT_INSTALL=$dracutbasedir/install/dracut-install
+elif ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/src/install/dracut-install ]]; then
+    DRACUT_INSTALL=$dracutbasedir/src/install/dracut-install
 fi
 
-if ! [[ -x $DRACUT_INSTALL ]]; then
+# Test if dracut-install is a standalone executable with no options.
+# E.g. DRACUT_INSTALL may be set externally as:
+# DRACUT_INSTALL="valgrind dracut-install"
+# or
+# DRACUT_INSTALL="dracut-install --debug"
+# in which case the string cannot be tested for being executable.
+DRINSTALLPARTS=0
+for i in $DRACUT_INSTALL; do
+    DRINSTALLPARTS=$((DRINSTALLPARTS + 1))
+done
+
+if [[ $DRINSTALLPARTS == 1 ]] && ! command -v "$DRACUT_INSTALL" > /dev/null 2>&1; then
     dfatal "dracut-install not found!"
     exit 10
 fi
 
+if [[ $hostonly == "-h" ]]; then
+    if ! [[ $DRACUT_KERNEL_MODALIASES ]] || ! [[ -f $DRACUT_KERNEL_MODALIASES ]]; then
+        export DRACUT_KERNEL_MODALIASES="${DRACUT_TMPDIR}/modaliases"
+        $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${srcmods:+--kerneldir "$srcmods"} --modalias > "$DRACUT_KERNEL_MODALIASES"
+    fi
+fi
+
 [[ $DRACUT_RESOLVE_LAZY ]] || export DRACUT_RESOLVE_DEPS=1
 inst_dir() {
-    [[ -e ${initdir}/"$1" ]] && return 0  # already there
-    $DRACUT_INSTALL ${initdir:+-D "$initdir"} -d "$@"
-    (($? != 0)) && derror FAILED: $DRACUT_INSTALL ${initdir:+-D "$initdir"} -d "$@" || :
+    local _ret
+    [[ -e ${initdir}/"$1" ]] && return 0 # already there
+    if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} -d "$@"; then
+        return 0
+    else
+        _ret=$?
+        derror FAILED: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} -d "$@"
+        return $_ret
+    fi
 }
 
 inst() {
-    local _hostonly_install
-    if [[ "$1" == "-H" ]]; then
+    local _ret _hostonly_install
+    if [[ $1 == "-H" ]]; then
         _hostonly_install="-H"
         shift
     fi
-    [[ -e ${initdir}/"${2:-$1}" ]] && return 0  # already there
-    $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
-    (($? != 0)) && derror FAILED: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
+    [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
+    if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"; then
+        return 0
+    else
+        _ret=$?
+        derror FAILED: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
+        return $_ret
+    fi
 }
 
 inst_simple() {
-    local _hostonly_install
-    if [[ "$1" == "-H" ]]; then
+    local _ret _hostonly_install
+    if [[ $1 == "-H" ]]; then
         _hostonly_install="-H"
         shift
     fi
-    [[ -e ${initdir}/"${2:-$1}" ]] && return 0  # already there
-    [[ -e $1 ]] || return 1  # no source
-    $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${_hostonly_install:+-H} "$@"
-    (($? != 0)) && derror FAILED: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${_hostonly_install:+-H} "$@" || :
+    [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
+    [[ -e $1 ]] || return 1                    # no source
+    if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${_hostonly_install:+-H} "$@"; then
+        return 0
+    else
+        _ret=$?
+        derror FAILED: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${_hostonly_install:+-H} "$@"
+        return $_ret
+    fi
 }
 
 inst_symlink() {
-    local _hostonly_install
-    if [[ "$1" == "-H" ]]; then
+    local _ret _hostonly_install
+    if [[ $1 == "-H" ]]; then
         _hostonly_install="-H"
         shift
     fi
-    [[ -e ${initdir}/"${2:-$1}" ]] && return 0  # already there
+    [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
     [[ -L $1 ]] || return 1
-    $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l}  ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
-    (($? != 0)) && derror FAILED: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l}  ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
+    if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"; then
+        return 0
+    else
+        _ret=$?
+        derror FAILED: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
+        return $_ret
+    fi
 }
 
 inst_multiple() {
-    local _ret
-    $DRACUT_INSTALL ${initdir:+-D "$initdir"} -a ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l}  ${DRACUT_FIPS_MODE:+-f} "$@"
-    _ret=$?
-    (($_ret != 0)) && derror FAILED: $DRACUT_INSTALL ${initdir:+-D "$initdir"} -a ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l}  ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
-    return $_ret
+    local _ret _hostonly_install
+    if [[ $1 == "-H" ]]; then
+        _hostonly_install="-H"
+        shift
+    fi
+    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
+        return 0
+    else
+        _ret=$?
+        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} "$@"
+        return $_ret
+    fi
 }
 
 dracut_install() {
@@ -223,38 +315,88 @@ dracut_install() {
 }
 
 dracut_instmods() {
-    local _silent=0;
-    local i;
-    [[ $no_kernel = yes ]] && return
+    local _ret _silent=0
+    local i
+    # shellcheck disable=SC2154
+    [[ $no_kernel == yes ]] && return
     for i in "$@"; do
-        [[ $i == "--silent" ]] && silent=1
+        [[ $i == "--silent" ]] && _silent=1
     done
 
-    $DRACUT_INSTALL \
-        ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${hostonly:+-H} ${omit_drivers:+-N "$omit_drivers"} ${srcmods:+--kerneldir "$srcmods"} -m "$@"
-    (($? != 0)) && (($silent == 0)) && derror FAILED: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${hostonly:+-H} ${omit_drivers:+-N "$omit_drivers"} ${srcmods:+--kerneldir "$srcmods"} -m "$@" || :
+    if $DRACUT_INSTALL \
+        ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
+        ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${hostonly:+-H} ${omit_drivers:+-N "$omit_drivers"} ${srcmods:+--kerneldir "$srcmods"} -m "$@"; then
+        return 0
+    else
+        _ret=$?
+        if ((_silent == 0)); then
+            derror FAILED: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${hostonly:+-H} ${omit_drivers:+-N "$omit_drivers"} ${srcmods:+--kerneldir "$srcmods"} -m "$@"
+        fi
+        return $_ret
+    fi
 }
 
 inst_library() {
-    local _hostonly_install
-    if [[ "$1" == "-H" ]]; then
+    local _ret _hostonly_install
+    if [[ $1 == "-H" ]]; then
         _hostonly_install="-H"
         shift
     fi
-    [[ -e ${initdir}/"${2:-$1}" ]] && return 0  # already there
-    [[ -e $1 ]] || return 1  # no source
-    $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l}  ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
-    (($? != 0)) && derror FAILED: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l}  ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
+    [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
+    [[ -e $1 ]] || return 1                    # no source
+    if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"; then
+        return 0
+    else
+        _ret=$?
+        derror FAILED: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
+        return $_ret
+    fi
 }
 
 inst_binary() {
-    $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l}  ${DRACUT_FIPS_MODE:+-f} "$@"
-    (($? != 0)) && derror FAILED: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l}  ${DRACUT_FIPS_MODE:+-f} "$@" || :
+    local _ret
+    if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"; then
+        return 0
+    else
+        _ret=$?
+        derror FAILED: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
+        return $_ret
+    fi
 }
 
 inst_script() {
-    $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
-    (($? != 0)) && derror FAILED: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l}  ${DRACUT_FIPS_MODE:+-f} "$@" || :
+    local _ret
+    if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"; then
+        return 0
+    else
+        _ret=$?
+        derror FAILED: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
+        return $_ret
+    fi
+}
+
+inst_fsck_help() {
+    local _ret _helper="/run/dracut/fsck/fsck_help_$1.txt"
+    if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$2" "$_helper"; then
+        return 0
+    else
+        _ret=$?
+        derror "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$2" "$_helper"
+        return $_ret
+    fi
+}
+
+# Use with form hostonly="$(optional_hostonly)" inst_xxxx <args>
+# If hostonly mode is set to "strict", hostonly restrictions will still
+# be applied, else will ignore hostonly mode and try to install all
+# given modules.
+optional_hostonly() {
+    # shellcheck disable=SC2154
+    if [[ $hostonly_mode == "strict" ]]; then
+        printf -- "%s" "$hostonly"
+    else
+        printf ""
+    fi
 }
 
 mark_hostonly() {
@@ -274,58 +416,70 @@ mark_hostonly() {
 # output: libfoo.so.8 libfoo.so
 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
 rev_lib_symlinks() {
+    local _fn
+    local _orig
+    local _links
+
     [[ ! $1 ]] && return 0
 
-    local fn="$1" orig="$(readlink -f "$1")" links=''
+    _fn="$1"
+    _orig="$(readlink -f "$1")"
+    _links=()
 
-    [[ ${fn} == *.so.* ]] || return 1
+    [[ ${_fn} == *.so.* ]] || return 1
 
-    until [[ ${fn##*.} == so ]]; do
-        fn="${fn%.*}"
-        [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
+    until [[ ${_fn##*.} == so ]]; do
+        _fn="${_fn%.*}"
+        [[ -L ${_fn} ]] && [[ $(readlink -f "${_fn}") == "${_orig}" ]] && _links+=("${_fn}")
     done
 
-    echo "${links}"
+    echo "${_links[*]}}"
 }
 
 # attempt to install any programs specified in a udev rule
 inst_rule_programs() {
     local _prog _bin
 
+    # shellcheck disable=SC2013
     for _prog in $(sed -nr 's/.*PROGRAM==?"([^ "]+).*/\1/p' "$1"); do
         _bin=""
-        if [ -x ${udevdir}/$_prog ]; then
-            _bin=${udevdir}/$_prog
-        elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
+        # shellcheck disable=SC2154
+        if [[ -x ${udevdir}/$_prog ]]; then
+            _bin="${udevdir}"/$_prog
+        elif [[ ${_prog/\$env\{/} == "$_prog" ]]; then
             _bin=$(find_binary "$_prog") || {
                 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
-                continue;
+                continue
             }
         fi
 
         [[ $_bin ]] && inst_binary "$_bin"
     done
+
+    # shellcheck disable=SC2013
     for _prog in $(sed -nr 's/.*RUN[+=]=?"([^ "]+).*/\1/p' "$1"); do
         _bin=""
-        if [ -x ${udevdir}/$_prog ]; then
+        if [[ -x ${udevdir}/$_prog ]]; then
             _bin=${udevdir}/$_prog
-        elif [[ "${_prog/\$env\{/}" == "$_prog" ]] && [[ "${_prog}" != "/sbin/initqueue" ]]; then
+        elif [[ ${_prog/\$env\{/} == "$_prog" ]] && [[ ${_prog} != "/sbin/initqueue" ]]; then
             _bin=$(find_binary "$_prog") || {
                 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
-                continue;
+                continue
             }
         fi
 
         [[ $_bin ]] && inst_binary "$_bin"
     done
+
+    # shellcheck disable=SC2013
     for _prog in $(sed -nr 's/.*IMPORT\{program\}==?"([^ "]+).*/\1/p' "$1"); do
         _bin=""
-        if [ -x ${udevdir}/$_prog ]; then
+        if [[ -x ${udevdir}/$_prog ]]; then
             _bin=${udevdir}/$_prog
-        elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
+        elif [[ ${_prog/\$env\{/} == "$_prog" ]]; then
             _bin=$(find_binary "$_prog") || {
                 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
-                continue;
+                continue
             }
         fi
 
@@ -333,18 +487,21 @@ inst_rule_programs() {
     done
 }
 
-# attempt to install any programs specified in a udev rule
+# attempt to create any groups and users specified in a udev rule
 inst_rule_group_owner() {
     local i
 
+    # shellcheck disable=SC2013
     for i in $(sed -nr 's/.*OWNER=?"([^ "]+).*/\1/p' "$1"); do
-        if ! grep -Eq "^$i:" "$initdir/etc/passwd" 2>/dev/null; then
-            grep -E "^$i:" /etc/passwd 2>/dev/null >> "$initdir/etc/passwd"
+        if ! grep -Eq "^$i:" "$initdir/etc/passwd" 2> /dev/null; then
+            grep -E "^$i:" "$dracutsysrootdir"/etc/passwd 2> /dev/null >> "$initdir/etc/passwd"
         fi
     done
+
+    # shellcheck disable=SC2013
     for i in $(sed -nr 's/.*GROUP=?"([^ "]+).*/\1/p' "$1"); do
-        if ! grep -Eq "^$i:" "$initdir/etc/group" 2>/dev/null; then
-            grep -E "^$i:" /etc/group 2>/dev/null >> "$initdir/etc/group"
+        if ! grep -Eq "^$i:" "$initdir/etc/group" 2> /dev/null; then
+            grep -E "^$i:" "$dracutsysrootdir"/etc/group 2> /dev/null >> "$initdir/etc/group"
         fi
     done
 }
@@ -364,7 +521,7 @@ inst_rules() {
     inst_dir "$_target"
     for _rule in "$@"; do
         if [ "${_rule#/}" = "$_rule" ]; then
-            for r in ${udevdir}/rules.d ${hostonly:+/etc/udev/rules.d}; do
+            for r in "$dracutsysrootdir${udevdir}/rules.d" ${hostonly:+"$dracutsysrootdir"/etc/udev/rules.d}; do
                 [[ -e $r/$_rule ]] || continue
                 _found="$r/$_rule"
                 inst_rule_programs "$_found"
@@ -373,7 +530,7 @@ inst_rules() {
                 inst_simple "$_found"
             done
         fi
-        for r in '' $dracutbasedir/rules.d/; do
+        for r in '' "$dracutsysrootdir$dracutbasedir/rules.d/"; do
             # skip rules without an absolute path
             [[ "${r}$_rule" != /* ]] && continue
             [[ -f ${r}$_rule ]] || continue
@@ -383,7 +540,7 @@ inst_rules() {
             inst_rule_initqueue "$_found"
             inst_simple "$_found" "$_target/${_found##*/}"
         done
-        [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
+        [[ $_found ]] || ddebug "Skipping udev rule: $_rule"
     done
 }
 
@@ -392,7 +549,7 @@ inst_rules_wildcard() {
 
     inst_dir "${udevdir}/rules.d"
     inst_dir "$_target"
-    for _rule in ${udevdir}/rules.d/$1 ${dracutbasedir}/rules.d/$1 ; do
+    for _rule in ${udevdir}/rules.d/$1 ${dracutbasedir}/rules.d/$1; do
         [[ -e $_rule ]] || continue
         inst_rule_programs "$_rule"
         inst_rule_group_owner "$_rule"
@@ -400,8 +557,8 @@ inst_rules_wildcard() {
         inst_simple "$_rule"
         _found=$_rule
     done
-    if [[ -n ${hostonly} ]] ; then
-        for _rule in ${_target}/$1 ; do
+    if [[ -n ${hostonly} ]]; then
+        for _rule in ${_target}/$1; do
             [[ -f $_rule ]] || continue
             inst_rule_programs "$_rule"
             inst_rule_group_owner "$_rule"
@@ -410,26 +567,55 @@ inst_rules_wildcard() {
             _found=$_rule
         done
     fi
-    [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
+    [[ $_found ]] || ddebug "Skipping udev rule: $_rule"
+}
+
+# make sure that library links are correct and up to date
+build_ld_cache() {
+    for f in "$dracutsysrootdir"/etc/ld.so.conf "$dracutsysrootdir"/etc/ld.so.conf.d/*; do
+        [[ -f $f ]] && inst_simple "${f#"$dracutsysrootdir"}"
+    done
+    if ! $DRACUT_LDCONFIG -r "$initdir" -f /etc/ld.so.conf; then
+        if [[ $EUID == 0 ]]; then
+            derror "ldconfig exited ungracefully"
+        else
+            derror "ldconfig might need uid=0 (root) for chroot()"
+        fi
+    fi
 }
 
 prepare_udev_rules() {
-    [ -z "$UDEVVERSION" ] && export UDEVVERSION=$(udevadm --version)
+    dwarn "prepare_udev_rules: deprecated and will be removed"
+
+    if [ -z "$UDEVVERSION" ]; then
+        UDEVVERSION=$(udevadm --version)
+        export UDEVVERSION
+    fi
+
+    if [ -z "$UDEVVERSION" ]; then
+        derror "Failed to detect udev version!"
+        return 1
+    fi
+    if [ -z "${UDEVVERSION##*[!0-9]*}" ]; then
+        derror "udevadm --version did not report an integer, udev version cannot be determined!"
+        return 1
+    fi
 
     for f in "$@"; do
         f="${initdir}/etc/udev/rules.d/$f"
         [ -e "$f" ] || continue
-        while read line || [ -n "$line" ]; do
+        while read -r line || [ -n "$line" ]; do
             if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
-                if [ $UDEVVERSION -ge 174 ]; then
+                if ((UDEVVERSION >= 174)); then
                     printf '%sIMPORT{builtin}="path_id"\n' "${line%%IMPORT PATH_ID}"
                 else
                     printf '%sIMPORT{program}="path_id %%p"\n' "${line%%IMPORT PATH_ID}"
                 fi
             elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
-                if [ $UDEVVERSION -ge 176 ]; then
+                if ((UDEVVERSION >= 176)); then
                     printf '%sIMPORT{builtin}="blkid"\n' "${line%%IMPORT BLKID}"
                 else
+                    # shellcheck disable=SC2016
                     printf '%sIMPORT{program}="/sbin/blkid -o udev -p $tempnode"\n' "${line%%IMPORT BLKID}"
                 fi
             else
@@ -444,15 +630,18 @@ prepare_udev_rules() {
 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
 inst_hook() {
+    local hook
     if ! [[ -f $3 ]]; then
         dfatal "Cannot install a hook ($3) that does not exist."
         dfatal "Aborting initrd creation."
         exit 1
-    elif ! [[ "$hookdirs" == *$1* ]]; then
+    elif ! [[ $hookdirs == *$1* ]]; then
         dfatal "No such hook type $1. Aborting initrd creation."
         exit 1
     fi
-    inst_simple "$3" "/lib/dracut/hooks/${1}/${2}-${3##*/}"
+    hook="/lib/dracut/hooks/${1}/${2}-${3##*/}"
+    inst_simple "$3" "$hook"
+    chmod u+x "$initdir/$hook"
 }
 
 # install any of listed files
@@ -471,7 +660,7 @@ inst_hook() {
 inst_any() {
     local to f
 
-    [[ $1 = '-d' ]] && to="$2" && shift 2
+    [[ $1 == '-d' ]] && to="$2" && shift 2
 
     for f in "$@"; do
         [[ -e $f ]] || continue
@@ -482,35 +671,59 @@ inst_any() {
     return 1
 }
 
+# inst_libdir_dir <dir> [<dir>...]
+# Install a <dir> located on a lib directory to the initramfs image
+inst_libdir_dir() {
+    local -a _dirs
+    for _dir in $libdirs; do
+        for _i in "$@"; do
+            for _d in "$dracutsysrootdir$_dir"/$_i; do
+                [[ -d $_d ]] && _dirs+=("${_d#"$dracutsysrootdir"}")
+            done
+        done
+    done
+    for _dir in "${_dirs[@]}"; do
+        inst_dir "$_dir"
+    done
+}
 
 # inst_libdir_file [-n <pattern>] <file> [<file>...]
 # Install a <file> located on a lib directory to the initramfs image
 # -n <pattern> install matching files
 inst_libdir_file() {
-    local _files
-    if [[ "$1" == "-n" ]]; then
+    local -a _files
+    if [[ $1 == "-n" ]]; then
         local _pattern=$2
         shift 2
         for _dir in $libdirs; do
             for _i in "$@"; do
-                for _f in "$_dir"/$_i; do
-                    [[ "$_f" =~ $_pattern ]] || continue
-                    [[ -e "$_f" ]] && _files+="$_f "
+                for _f in "$dracutsysrootdir$_dir"/$_i; do
+                    [[ ${_f#"$dracutsysrootdir"} =~ $_pattern ]] || continue
+                    [[ -e $_f ]] && _files+=("${_f#"$dracutsysrootdir"}")
                 done
             done
         done
     else
         for _dir in $libdirs; do
             for _i in "$@"; do
-                for _f in "$_dir"/$_i; do
-                    [[ -e "$_f" ]] && _files+="$_f "
+                for _f in "$dracutsysrootdir$_dir"/$_i; do
+                    [[ -e $_f ]] && _files+=("${_f#"$dracutsysrootdir"}")
                 done
             done
         done
     fi
-    [[ $_files ]] && inst_multiple $_files
+    [[ ${#_files[@]} -gt 0 ]] && inst_multiple "${_files[@]}"
 }
 
+# get a command to decompress the given file
+get_decompress_cmd() {
+    case "$1" in
+        *.gz) echo 'gzip -f -d' ;;
+        *.bz2) echo 'bzip2 -d' ;;
+        *.xz) echo 'xz -f -d' ;;
+        *.zst) echo 'zstd -f -d ' ;;
+    esac
+}
 
 # install function decompressing the target and handling symlinks
 # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
@@ -520,14 +733,10 @@ inst_libdir_file() {
 inst_decompress() {
     local _src _cmd
 
-    for _src in $@
-    do
-        case ${_src} in
-            *.gz) _cmd='gzip -f -d' ;;
-            *.bz2) _cmd='bzip2 -d' ;;
-            *) return 1 ;;
-        esac
-        inst_simple ${_src}
+    for _src in "$@"; do
+        _cmd=$(get_decompress_cmd "${_src}")
+        [[ -z ${_cmd} ]] && return 1
+        inst_simple "${_src}"
         # Decompress with chosen tool.  We assume that tool changes name e.g.
         # from 'name.gz' to 'name'.
         ${_cmd} "${initdir}${_src}"
@@ -540,163 +749,142 @@ inst_decompress() {
 inst_opt_decompress() {
     local _src
 
-    for _src in $@; do
+    for _src in "$@"; do
         inst_decompress "${_src}" || inst "${_src}"
     done
 }
 
-# module_check <dracut module>
+# module_check <dracut module> [<forced>] [<module path>]
 # execute the check() function of module-setup.sh of <dracut module>
 # or the "check" script, if module-setup.sh is not found
 # "check $hostonly" is called
 module_check() {
-    local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
+    local _moddir=$3
     local _ret
     local _forced=0
     local _hostonly=$hostonly
-    [ $# -eq 2 ] && _forced=$2
-    [[ -d $_moddir ]] || return 1
-    if [[ ! -f $_moddir/module-setup.sh ]]; then
-        # if we do not have a check script, we are unconditionally included
-        [[ -x $_moddir/check ]] || return 0
-        [ $_forced -ne 0 ] && unset hostonly
-        $_moddir/check $hostonly
-        _ret=$?
-    else
-        unset check depends cmdline install installkernel
-        check() { true; }
-        . $_moddir/module-setup.sh
-        is_func check || return 0
-        [ $_forced -ne 0 ] && unset hostonly
-        moddir=$_moddir check $hostonly
-        _ret=$?
-        unset check depends cmdline install installkernel
-    fi
+    [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
+    [ $# -ge 2 ] && _forced=$2
+    [[ -f $_moddir/module-setup.sh ]] || return 1
+    unset check depends cmdline install installkernel
+    check() { true; }
+    # shellcheck disable=SC1090
+    . "$_moddir"/module-setup.sh
+    is_func check || return 0
+    [[ $_forced != 0 ]] && unset hostonly
+    # don't quote $hostonly to leave argument empty
+    # shellcheck disable=SC2086
+    moddir="$_moddir" check $hostonly
+    _ret=$?
+    unset check depends cmdline install installkernel
     hostonly=$_hostonly
     return $_ret
 }
 
-# module_check_mount <dracut module>
+# module_check_mount <dracut module> [<module path>]
 # execute the check() function of module-setup.sh of <dracut module>
 # or the "check" script, if module-setup.sh is not found
 # "mount_needs=1 check 0" is called
 module_check_mount() {
-    local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
+    local _moddir=$2
     local _ret
-    mount_needs=1
-    [[ -d $_moddir ]] || return 1
-    if [[ ! -f $_moddir/module-setup.sh ]]; then
-        # if we do not have a check script, we are unconditionally included
-        [[ -x $_moddir/check ]] || return 0
-        mount_needs=1 $_moddir/check 0
-        _ret=$?
-    else
-        unset check depends cmdline install installkernel
-        check() { false; }
-        . $_moddir/module-setup.sh
-        moddir=$_moddir check 0
-        _ret=$?
-        unset check depends cmdline install installkernel
-    fi
+    export mount_needs=1
+    [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
+    [[ -f $_moddir/module-setup.sh ]] || return 1
+    unset check depends cmdline install installkernel
+    check() { false; }
+    # shellcheck disable=SC1090
+    . "$_moddir"/module-setup.sh
+    moddir=$_moddir check 0
+    _ret=$?
+    unset check depends cmdline install installkernel
     unset mount_needs
-    return $_ret
+    return "$_ret"
 }
 
-# module_depends <dracut module>
+# module_depends <dracut module> [<module path>]
 # execute the depends() function of module-setup.sh of <dracut module>
 # or the "depends" script, if module-setup.sh is not found
 module_depends() {
-    local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
+    local _moddir=$2
     local _ret
-    [[ -d $_moddir ]] || return 1
-    if [[ ! -f $_moddir/module-setup.sh ]]; then
-        # if we do not have a check script, we have no deps
-        [[ -x $_moddir/check ]] || return 0
-        $_moddir/check -d
-        return $?
-    else
-        unset check depends cmdline install installkernel
-        depends() { true; }
-        . $_moddir/module-setup.sh
-        moddir=$_moddir depends
-        _ret=$?
-        unset check depends cmdline install installkernel
-        return $_ret
-    fi
+    [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
+    [[ -f $_moddir/module-setup.sh ]] || return 1
+    unset check depends cmdline install installkernel
+    depends() { true; }
+    # shellcheck disable=SC1090
+    . "$_moddir"/module-setup.sh
+    moddir=$_moddir depends
+    _ret=$?
+    unset check depends cmdline install installkernel
+    return $_ret
 }
 
-# module_cmdline <dracut module>
+# module_cmdline <dracut module> [<module path>]
 # execute the cmdline() function of module-setup.sh of <dracut module>
 # or the "cmdline" script, if module-setup.sh is not found
 module_cmdline() {
-    local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
+    local _moddir=$2
     local _ret
-    [[ -d $_moddir ]] || return 1
-    if [[ ! -f $_moddir/module-setup.sh ]]; then
-        [[ -x $_moddir/cmdline ]] && . "$_moddir/cmdline"
-        return $?
-    else
-        unset check depends cmdline install installkernel
-        cmdline() { true; }
-        . $_moddir/module-setup.sh
-        moddir=$_moddir cmdline
-        _ret=$?
-        unset check depends cmdline install installkernel
-        return $_ret
-    fi
+    [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
+    [[ -f $_moddir/module-setup.sh ]] || return 1
+    unset check depends cmdline install installkernel
+    cmdline() { true; }
+    # shellcheck disable=SC1090
+    . "$_moddir"/module-setup.sh
+    moddir="$_moddir" cmdline
+    _ret=$?
+    unset check depends cmdline install installkernel
+    return $_ret
 }
 
-# module_install <dracut module>
+# module_install <dracut module> [<module path>]
 # execute the install() function of module-setup.sh of <dracut module>
 # or the "install" script, if module-setup.sh is not found
 module_install() {
-    local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
+    local _moddir=$2
     local _ret
-    [[ -d $_moddir ]] || return 1
-    if [[ ! -f $_moddir/module-setup.sh ]]; then
-        [[ -x $_moddir/install ]] && . "$_moddir/install"
-        return $?
-    else
-        unset check depends cmdline install installkernel
-        install() { true; }
-        . $_moddir/module-setup.sh
-        moddir=$_moddir install
-        _ret=$?
-        unset check depends cmdline install installkernel
-        return $_ret
-    fi
+    [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
+    [[ -f $_moddir/module-setup.sh ]] || return 1
+    unset check depends cmdline install installkernel
+    install() { true; }
+    # shellcheck disable=SC1090
+    . "$_moddir"/module-setup.sh
+    moddir="$_moddir" install
+    _ret=$?
+    unset check depends cmdline install installkernel
+    return $_ret
 }
 
-# module_installkernel <dracut module>
+# module_installkernel <dracut module> [<module path>]
 # execute the installkernel() function of module-setup.sh of <dracut module>
 # or the "installkernel" script, if module-setup.sh is not found
 module_installkernel() {
-    local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
+    local _moddir=$2
     local _ret
-    [[ -d $_moddir ]] || return 1
-    if [[ ! -f $_moddir/module-setup.sh ]]; then
-        [[ -x $_moddir/installkernel ]] && . "$_moddir/installkernel"
-        return $?
-    else
-        unset check depends cmdline install installkernel
-        installkernel() { true; }
-        . $_moddir/module-setup.sh
-        moddir=$_moddir installkernel
-        _ret=$?
-        unset check depends cmdline install installkernel
-        return $_ret
-    fi
+    [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
+    [[ -f $_moddir/module-setup.sh ]] || return 1
+    unset check depends cmdline install installkernel
+    installkernel() { true; }
+    # shellcheck disable=SC1090
+    . "$_moddir"/module-setup.sh
+    moddir="$_moddir" installkernel
+    _ret=$?
+    unset check depends cmdline install installkernel
+    return $_ret
 }
 
-# check_mount <dracut module>
+# check_mount <dracut module> [<use_as_dep>] [<module path>]
 # check_mount checks, if a dracut module is needed for the given
 # device and filesystem types in "${host_fs_types[@]}"
 check_mount() {
     local _mod=$1
-    local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
+    local _moddir=$3
     local _ret
     local _moddep
 
+    [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
+    # shellcheck disable=SC2154
     [ "${#host_fs_types[@]}" -le 0 ] && return 1
 
     # If we are already scheduled to be loaded, no need to check again.
@@ -708,27 +896,29 @@ check_mount() {
 
     [[ $2 ]] || mods_checked_as_dep+=" $_mod "
 
+    # shellcheck disable=SC2154
     if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
         return 1
     fi
 
     if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
-        module_check_mount $_mod; ret=$?
+        module_check_mount "$_mod" "$_moddir"
+        _ret=$?
 
-        # explicit module, so also accept ret=255
-        [[ $ret = 0 || $ret = 255 ]] || return 1
+        # explicit module, so also accept _ret=255
+        [[ $_ret == 0 || $_ret == 255 ]] || return 1
     else
         # module not in our list
-        if [[ $dracutmodules = all ]]; then
+        if [[ $dracutmodules == all ]]; then
             # check, if we can and should install this module
-            module_check_mount $_mod || return 1
+            module_check_mount "$_mod" "$_moddir" || return 1
         else
             # skip this module
             return 1
         fi
     fi
 
-    for _moddep in $(module_depends $_mod); do
+    for _moddep in $(module_depends "$_mod" "$_moddir"); do
         # handle deps as if they were manually added
         [[ " $dracutmodules " == *\ $_mod\ * ]] \
             && [[ " $dracutmodules " != *\ $_moddep\ * ]] \
@@ -740,27 +930,29 @@ check_mount() {
             && [[ " $force_add_dracutmodules " != *\ $_moddep\ * ]] \
             && force_add_dracutmodules+=" $_moddep "
         # if a module we depend on fail, fail also
-        if ! check_module $_moddep; then
-            derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
+        if ! check_module "$_moddep"; then
+            derror "Module '$_mod' depends on '$_moddep', which can't be installed"
             return 1
         fi
     done
 
-    [[ " $mods_to_load " == *\ $_mod\ * ]] || \
-        mods_to_load+=" $_mod "
+    [[ " $mods_to_load " == *\ $_mod\ * ]] \
+        || mods_to_load+=" $_mod "
 
     return 0
 }
 
-# check_module <dracut module> [<use_as_dep>]
+# check_module <dracut module> [<use_as_dep>] [<module path>]
 # check if a dracut module is to be used in the initramfs process
 # if <use_as_dep> is set, then the process also keeps track
 # that the modules were checked for the dependency tracking process
 check_module() {
     local _mod=$1
-    local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
+    local _moddir=$3
     local _ret
     local _moddep
+
+    [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
     # If we are already scheduled to be loaded, no need to check again.
     [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
     [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
@@ -771,26 +963,29 @@ check_module() {
     [[ $2 ]] || mods_checked_as_dep+=" $_mod "
 
     if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
-        dinfo "dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
+        ddebug "Module '$_mod' will not be installed, because it's in the list to be omitted!"
         return 1
     fi
 
     if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
         if [[ " $dracutmodules $force_add_dracutmodules " == *\ $_mod\ * ]]; then
-            module_check $_mod 1; ret=$?
+            module_check "$_mod" 1 "$_moddir"
+            _ret=$?
         else
-            module_check $_mod 0; ret=$?
+            module_check "$_mod" 0 "$_moddir"
+            _ret=$?
         fi
-        # explicit module, so also accept ret=255
-        [[ $ret = 0 || $ret = 255 ]] || return 1
+        # explicit module, so also accept _ret=255
+        [[ $_ret == 0 || $_ret == 255 ]] || return 1
     else
         # module not in our list
-        if [[ $dracutmodules = all ]]; then
+        if [[ $dracutmodules == all ]]; then
             # check, if we can and should install this module
-            module_check $_mod; ret=$?
-            if [[ $ret != 0 ]]; then
+            module_check "$_mod" 0 "$_moddir"
+            _ret=$?
+            if [[ $_ret != 0 ]]; then
                 [[ $2 ]] && return 1
-                [[ $ret != 255 ]] && return 1
+                [[ $_ret != 255 ]] && return 1
             fi
         else
             # skip this module
@@ -798,7 +993,7 @@ check_module() {
         fi
     fi
 
-    for _moddep in $(module_depends $_mod); do
+    for _moddep in $(module_depends "$_mod" "$_moddir"); do
         # handle deps as if they were manually added
         [[ " $dracutmodules " == *\ $_mod\ * ]] \
             && [[ " $dracutmodules " != *\ $_moddep\ * ]] \
@@ -810,32 +1005,32 @@ check_module() {
             && [[ " $force_add_dracutmodules " != *\ $_moddep\ * ]] \
             && force_add_dracutmodules+=" $_moddep "
         # if a module we depend on fail, fail also
-        if ! check_module $_moddep; then
-            derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
+        if ! check_module "$_moddep"; then
+            derror "Module '$_mod' depends on '$_moddep', which can't be installed"
             return 1
         fi
     done
 
-    [[ " $mods_to_load " == *\ $_mod\ * ]] || \
-        mods_to_load+=" $_mod "
+    [[ " $mods_to_load " == *\ $_mod\ * ]] \
+        || mods_to_load+=" $_mod "
 
     return 0
 }
 
 # for_each_module_dir <func>
-# execute "<func> <dracut module> 1"
+# execute "<func> <dracut module> 1 <module path>"
 for_each_module_dir() {
     local _modcheck
     local _mod
     local _moddir
     local _func
+    local _reason
     _func=$1
     for _moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
-        [[ -d $_moddir ]] || continue;
-        [[ -e $_moddir/install || -e $_moddir/installkernel || \
-            -e $_moddir/module-setup.sh ]] || continue
-        _mod=${_moddir##*/}; _mod=${_mod#[0-9][0-9]}
-        $_func $_mod 1
+        [[ -e $_moddir/module-setup.sh ]] || continue
+        _mod=${_moddir##*/}
+        _mod=${_mod#[0-9][0-9]}
+        $_func "$_mod" 1 "$_moddir"
     done
 
     # Report any missing dracut modules, the user has specified
@@ -849,98 +1044,24 @@ for_each_module_dir() {
             && [[ " $omit_dracutmodules " == *\ $_mod\ * ]] \
             && continue
 
-        derror "dracut module '$_mod' cannot be found or installed."
+        [[ -d $(echo "$dracutbasedir/modules.d"/[0-9][0-9]"$_mod") ]] \
+            && _reason="installed" \
+            || _reason="found"
+        derror "Module '$_mod' cannot be $_reason."
         [[ " $force_add_dracutmodules " == *\ $_mod\ * ]] && exit 1
         [[ " $dracutmodules " == *\ $_mod\ * ]] && exit 1
         [[ " $add_dracutmodules " == *\ $_mod\ * ]] && exit 1
     done
 }
 
-# Install a single kernel module along with any firmware it may require.
-# $1 = full path to kernel module to install
-install_kmod_with_fw() {
-    # no need to go further if the module is already installed
-
-    [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
-        && return 0
-
-    if [[ $omit_drivers ]]; then
-        local _kmod=${1##*/}
-        _kmod=${_kmod%.ko*}
-        _kmod=${_kmod/-/_}
-        if [[ "$_kmod" =~ $omit_drivers ]]; then
-            dinfo "Omitting driver $_kmod"
-            return 0
-        fi
-        if [[ "${1##*/lib/modules/$kernel/}" =~ $omit_drivers ]]; then
-            dinfo "Omitting driver $_kmod"
-            return 0
-        fi
-    fi
-
-    if [[ $silent_omit_drivers ]]; then
-        local _kmod=${1##*/}
-        _kmod=${_kmod%.ko*}
-        _kmod=${_kmod/-/_}
-        [[ "$_kmod" =~ $silent_omit_drivers ]] && return 0
-        [[ "${1##*/lib/modules/$kernel/}" =~ $silent_omit_drivers ]] && return 0
-    fi
-
-    inst_simple "$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
-    ret=$?
-    (($ret != 0)) && return $ret
-
-    local _modname=${1##*/} _fwdir _found _fw
-    _modname=${_modname%.ko*}
-    for _fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do
-        _found=''
-        for _fwdir in $fw_dir; do
-            [[ -d $_fwdir && -f $_fwdir/$_fw ]] || continue
-            inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
-            _found=yes
-        done
-        if [[ $_found != yes ]]; then
-            if ! [[ -d $(echo /sys/module/${_modname//-/_}|{ read a b; echo $a; }) ]]; then
-                dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
-                    "\"${_modname}.ko\""
-            else
-                dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
-                    "\"${_modname}.ko\""
-            fi
-        fi
-    done
-    return 0
-}
-
-# Do something with all the dependencies of a kernel module.
-# Note that kernel modules depend on themselves using the technique we use
-# $1 = function to call for each dependency we find
-#      It will be passed the full path to the found kernel module
-# $2 = module to get dependencies for
-# rest of args = arguments to modprobe
-# _fderr specifies FD passed from surrounding scope
-for_each_kmod_dep() {
-    local _func=$1 _kmod=$2 _cmd _modpath _options
-    shift 2
-    modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
-        while read _cmd _modpath _options || [ -n "$_cmd" ]; do
-            [[ $_cmd = insmod ]] || continue
-            $_func ${_modpath} || exit $?
-        done
-    )
-}
-
 dracut_kernel_post() {
-    local _moddirname=${srcmods%%/lib/modules/*}
-    local _pid
-
-    for _f in modules.builtin.bin modules.builtin modules.order; do
-        [[ $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f"
+    for _f in modules.builtin modules.builtin.alias modules.builtin.modinfo modules.order; do
+        [[ -e $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f"
     done
 
     # generate module dependencies for the initrd
-    if [[ -d $initdir/lib/modules/$kernel ]] && \
-        ! depmod -a -b "$initdir" $kernel; then
+    if [[ -d $initdir/lib/modules/$kernel ]] \
+        && ! depmod -a -b "$initdir" "$kernel"; then
         dfatal "\"depmod -a $kernel\" failed."
         exit 1
     fi
@@ -958,13 +1079,13 @@ instmods() {
     local _silent
     local _ret
 
-    [[ $no_kernel = yes ]] && return
+    [[ $no_kernel == yes ]] && return
 
-    if [[ $1 = '-c' ]]; then
+    if [[ $1 == '-c' ]]; then
         unset _optional
         shift
     fi
-    if [[ $1 = '-s' ]]; then
+    if [[ $1 == '-s' ]]; then
         _silent=1
         shift
     fi
@@ -974,8 +1095,13 @@ instmods() {
         set -- "${args[@]}"
     fi
 
+    if (($# == 0)); then
+        return 0
+    fi
+
     $DRACUT_INSTALL \
         ${initdir:+-D "$initdir"} \
+        ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
         ${loginstall:+-L "$loginstall"} \
         ${hostonly:+-H} \
         ${omit_drivers:+-N "$omit_drivers"} \
@@ -985,20 +1111,21 @@ instmods() {
         -m "$@"
     _ret=$?
 
-    if (($_ret != 0)) && [[ -z "$_silent" ]]; then
+    if ((_ret != 0)) && [[ -z $_silent ]]; then
         derror "FAILED: " \
-            $DRACUT_INSTALL \
-                ${initdir:+-D "$initdir"} \
-                ${loginstall:+-L "$loginstall"} \
-                ${hostonly:+-H} \
-                ${omit_drivers:+-N "$omit_drivers"} \
-                ${srcmods:+--kerneldir "$srcmods"} \
-                ${_optional:+-o} \
-                ${_silent:+--silent} \
-                -m "$@"
-    fi
-
-    [[ "$optional" ]] && return 0
+            "$DRACUT_INSTALL" \
+            ${initdir:+-D "$initdir"} \
+            ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
+            ${loginstall:+-L "$loginstall"} \
+            ${hostonly:+-H} \
+            ${omit_drivers:+-N "$omit_drivers"} \
+            ${srcmods:+--kerneldir "$srcmods"} \
+            ${_optional:+-o} \
+            ${_silent:+--silent} \
+            -m "$@"
+    fi
+
+    [[ "$_optional" ]] && return 0
     return $_ret
 }
 
@@ -1010,7 +1137,30 @@ else
     ln_r() {
         local _source=$1
         local _dest=$2
-        [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
+        [[ -d ${_dest%/*} ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
         ln -sfn -- "$(convert_abs_rel "${_dest}" "${_source}")" "${initdir}/${_dest}"
     }
 fi
+
+is_qemu_virtualized() {
+    # 0 if a virt environment was detected
+    # 1 if a virt environment could not be detected
+    # 255 if any error was encountered
+    if type -P systemd-detect-virt > /dev/null 2>&1; then
+        if ! vm=$(systemd-detect-virt --vm 2> /dev/null); then
+            return 255
+        fi
+        [[ $vm == "qemu" ]] && return 0
+        [[ $vm == "kvm" ]] && return 0
+        [[ $vm == "bochs" ]] && return 0
+    fi
+
+    for i in /sys/class/dmi/id/*_vendor; do
+        [[ -f $i ]] || continue
+        read -r vendor < "$i"
+        [[ $vendor == "QEMU" ]] && return 0
+        [[ $vendor == "Red Hat" ]] && return 0
+        [[ $vendor == "Bochs" ]] && return 0
+    done
+    return 1
+}