]> git.ipfire.org Git - thirdparty/dracut.git/blobdiff - dracut-init.sh
move setting the "systemdutildir" variable before it's used
[thirdparty/dracut.git] / dracut-init.sh
index 8d1d9faa59637c09293c337896c0c9a17361bc81..e1ced79d91d1e365530f88dbce372a7cd2a5f7dd 100644 (file)
 #
 export LC_MESSAGES=C
 
+if [[ "$EUID" = "0" ]]; 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() {
@@ -46,13 +52,6 @@ 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
@@ -69,6 +68,8 @@ 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 "
@@ -149,6 +150,14 @@ dracut_module_included() {
     [[ " $mods_to_load $modules_loaded " == *\ $*\ * ]]
 }
 
+dracut_no_switch_root() {
+    >"$initdir/lib/dracut/no-switch-root"
+}
+
+dracut_module_path() {
+    echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; }
+}
+
 if ! [[ $DRACUT_INSTALL ]]; then
     DRACUT_INSTALL=$(find_binary dracut-install)
 fi
@@ -164,6 +173,13 @@ if ! [[ -x $DRACUT_INSTALL ]]; then
     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 ${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
@@ -219,10 +235,16 @@ dracut_install() {
 }
 
 dracut_instmods() {
+    local _silent=0;
+    local i;
     [[ $no_kernel = yes ]] && return
+    for i in "$@"; do
+        [[ $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)) && derror FAILED: $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 "$@" || :
 }
 
 inst_library() {
@@ -247,6 +269,24 @@ inst_script() {
     (($? != 0)) && derror FAILED: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l}  ${DRACUT_FIPS_MODE:+-f} "$@" || :
 }
 
+inst_fsck_help() {
+    local _helper="/run/dracut/fsck/fsck_help_$1.txt"
+    $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$2" $_helper
+    (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l}  ${DRACUT_FIPS_MODE:+-f} "$2" $_helper || :
+}
+
+# Use with form hostonly="$(optional_hostonly)" inst_xxxx <args>
+# If hosotnly 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() {
+    if [[ $hostonly_mode = "strict" ]]; then
+        printf -- "$hostonly"
+    else
+        printf ""
+    fi
+}
+
 mark_hostonly() {
     for i in "$@"; do
         echo "$i" >> "$initdir/lib/dracut/hostonly-files"
@@ -501,6 +541,14 @@ inst_libdir_file() {
     [[ $_files ]] && 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' ;;
+    esac
+}
 
 # install function decompressing the target and handling symlinks
 # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
@@ -512,11 +560,8 @@ inst_decompress() {
 
     for _src in $@
     do
-        case ${_src} in
-            *.gz) _cmd='gzip -f -d' ;;
-            *.bz2) _cmd='bzip2 -d' ;;
-            *) return 1 ;;
-        esac
+        _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'.
@@ -535,15 +580,16 @@ inst_opt_decompress() {
     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
+    [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
     [ $# -eq 2 ] && _forced=$2
     [[ -d $_moddir ]] || return 1
     if [[ ! -f $_moddir/module-setup.sh ]]; then
@@ -566,14 +612,15 @@ module_check() {
     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
+    [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
     [[ -d $_moddir ]] || return 1
     if [[ ! -f $_moddir/module-setup.sh ]]; then
         # if we do not have a check script, we are unconditionally included
@@ -592,12 +639,13 @@ module_check_mount() {
     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
+    [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
     [[ -d $_moddir ]] || return 1
     if [[ ! -f $_moddir/module-setup.sh ]]; then
         # if we do not have a check script, we have no deps
@@ -615,12 +663,13 @@ module_depends() {
     fi
 }
 
-# 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
+    [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
     [[ -d $_moddir ]] || return 1
     if [[ ! -f $_moddir/module-setup.sh ]]; then
         [[ -x $_moddir/cmdline ]] && . "$_moddir/cmdline"
@@ -636,12 +685,13 @@ module_cmdline() {
     fi
 }
 
-# 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
+    [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
     [[ -d $_moddir ]] || return 1
     if [[ ! -f $_moddir/module-setup.sh ]]; then
         [[ -x $_moddir/install ]] && . "$_moddir/install"
@@ -657,12 +707,13 @@ module_install() {
     fi
 }
 
-# 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
+    [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
     [[ -d $_moddir ]] || return 1
     if [[ ! -f $_moddir/module-setup.sh ]]; then
         [[ -x $_moddir/installkernel ]] && . "$_moddir/installkernel"
@@ -678,15 +729,16 @@ module_installkernel() {
     fi
 }
 
-# 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")
     [ "${#host_fs_types[@]}" -le 0 ] && return 1
 
     # If we are already scheduled to be loaded, no need to check again.
@@ -703,7 +755,7 @@ check_mount() {
     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
@@ -711,14 +763,14 @@ check_mount() {
         # module not in our list
         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\ * ]] \
@@ -742,15 +794,17 @@ check_mount() {
     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
@@ -767,9 +821,9 @@ check_module() {
 
     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
@@ -777,7 +831,7 @@ check_module() {
         # module not in our list
         if [[ $dracutmodules = all ]]; then
             # check, if we can and should install this module
-            module_check $_mod; ret=$?
+            module_check $_mod 0 $_moddir; ret=$?
             if [[ $ret != 0 ]]; then
                 [[ $2 ]] && return 1
                 [[ $ret != 255 ]] && return 1
@@ -788,7 +842,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\ * ]] \
@@ -813,7 +867,7 @@ check_module() {
 }
 
 # 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
@@ -825,7 +879,7 @@ for_each_module_dir() {
         [[ -e $_moddir/install || -e $_moddir/installkernel || \
             -e $_moddir/module-setup.sh ]] || continue
         _mod=${_moddir##*/}; _mod=${_mod#[0-9][0-9]}
-        $_func $_mod 1
+        $_func $_mod 1 $_moddir
     done
 
     # Report any missing dracut modules, the user has specified
@@ -879,14 +933,25 @@ install_kmod_with_fw() {
     inst_simple "$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
     ret=$?
     (($ret != 0)) && return $ret
+    local _cmd=$(get_decompress_cmd "$1")
+    [[ -n "${_cmd}" ]] && \
+        ${_cmd} "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
 
     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"
+            [[ -d $_fwdir ]] || continue
+            if [[ -f $_fwdir/$_fw ]]; then
+                inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
+            elif [[ -f $_fwdir/$_fw.xz ]]; then
+                inst_simple "$_fwdir/$_fw.xz" "/lib/firmware/$_fw.xz"
+                rm -f "${initdir}/lib/firmware/$_fw"
+                unxz -f "${initdir}/lib/firmware/$_fw.xz"
+            else
+               continue
+            fi
             _found=yes
         done
         if [[ $_found != yes ]]; then
@@ -921,11 +986,8 @@ for_each_kmod_dep() {
 }
 
 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"
+        [[ -e $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f"
     done
 
     # generate module dependencies for the initrd
@@ -937,60 +999,6 @@ dracut_kernel_post() {
 
 }
 
-[[ "$kernel_current" ]] || export kernel_current=$(uname -r)
-
-module_is_host_only() {
-    local _mod=$1
-    local _modenc a i _k _s _v _aliases
-    _mod=${_mod##*/}
-    _mod=${_mod%.ko*}
-    _modenc=${_mod//-/_}
-
-    [[ " $add_drivers " == *\ ${_mod}\ * ]] && return 0
-
-    # check if module is loaded
-    [[ ${host_modules["$_modenc"]} ]] && return 0
-
-    [[ "$kernel_current" ]] || export kernel_current=$(uname -r)
-
-    if [[ "$kernel_current" != "$kernel" ]]; then
-        # check if module is loadable on the current kernel
-        # this covers the case, where a new module is introduced
-        # or a module was renamed
-        # or a module changed from builtin to a module
-
-        if [[ -d /lib/modules/$kernel_current ]]; then
-            # if the modinfo can be parsed, but the module
-            # is not loaded, then we can safely return 1
-            modinfo -F filename "$_mod" &>/dev/null && return 1
-        fi
-
-        # just install the module, better safe than sorry
-        return 0
-    fi
-
-    return 1
-}
-
-find_kernel_modules_by_path () {
-    local _OLDIFS
-
-    [[ -f "$srcmods/modules.dep" ]] || return 0
-
-    _OLDIFS=$IFS
-    IFS=:
-    while read a rest || [ -n "$a" ]; do
-        [[ $a = */$1/* ]] || [[ $a = updates/* ]] || continue
-        printf "%s\n" "$srcmods/$a"
-    done < "$srcmods/modules.dep"
-    IFS=$_OLDIFS
-    return 0
-}
-
-find_kernel_modules () {
-    find_kernel_modules_by_path  drivers
-}
-
 instmods() {
     # instmods [-c [-s]] <kernel module> [<kernel module> ... ]
     # instmods [-c [-s]] <kernel subsystem>
@@ -1018,6 +1026,10 @@ instmods() {
         set -- "${args[@]}"
     fi
 
+    if (($# == 0)); then
+        return 0
+    fi
+
     $DRACUT_INSTALL \
         ${initdir:+-D "$initdir"} \
         ${loginstall:+-L "$loginstall"} \
@@ -1045,3 +1057,16 @@ instmods() {
     [[ "$optional" ]] && return 0
     return $_ret
 }
+
+if [[ "$(ln --help)" == *--relative* ]]; then
+    ln_r() {
+        ln -sfnr "${initdir}/$1" "${initdir}/$2"
+    }
+else
+    ln_r() {
+        local _source=$1
+        local _dest=$2
+        [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
+        ln -sfn -- "$(convert_abs_rel "${_dest}" "${_source}")" "${initdir}/${_dest}"
+    }
+fi