]> git.ipfire.org Git - thirdparty/dracut.git/blobdiff - dracut-functions.sh
Merge pull request #90 from ldzhong/fix
[thirdparty/dracut.git] / dracut-functions.sh
index 7a12a7643162cad4b885c36e93ba94df348fac62..cb88078434d1663834c985b2a705ee177b26d3e9 100755 (executable)
 export LC_MESSAGES=C
 
 if [[ $DRACUT_KERNEL_LAZY ]] && ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
-    if [[ -d "$initdir/.kernelmodseen" ]]; then
-        mkdir -p "$initdir/.kernelmodseen"
+    if [[ -d "$initdir/.kernelmodseen" ]]; then
+        DRACUT_KERNEL_LAZY_HASHDIR="$initdir/.kernelmodseen"
     fi
-    DRACUT_KERNEL_LAZY_HASHDIR="$initdir/.kernelmodseen"
-fi
-
-if [[ $initdir ]] && ! [[ -d $initdir ]]; then
-    mkdir -p "$initdir"
 fi
 
 # Generic substring function.  If $2 is in $1, return 0.
@@ -36,6 +31,10 @@ strstr() { [[ $1 = *"$2"* ]]; }
 strglobin() { [[ $1 = *$2* ]]; }
 # Generic glob matching function. If glob pattern $2 matches all of $1, OK
 strglob() { [[ $1 = $2 ]]; }
+# returns OK if $1 contains literal string $2 at the beginning, and isn't empty
+str_starts() { [ "${1#"$2"*}" != "$1" ]; }
+# returns OK if $1 contains literal string $2 at the end, and isn't empty
+str_ends() { [ "${1%*"$2"}" != "$1" ]; }
 
 # helper function for check() in module-setup.sh
 # to check for required installed binaries
@@ -104,17 +103,7 @@ fi
 
 ldconfig_paths()
 {
-    local a i
-    declare -A a
-    for i in $(
-        ldconfig -pN 2>/dev/null | grep -F '=>' | grep -E -v '/(lib|lib64|usr/lib|usr/lib64)/[^/]*$' | while read a b c d; do
-            d=${d%/*}
-            printf "%s\n" "$d";
-        done
-    ); do
-        a["$i"]=1;
-    done;
-    printf "%s\n" ${!a[@]}
+    ldconfig -pN 2>/dev/null | grep -E -v '/(lib|lib64|usr/lib|usr/lib64)/[^/]*$' | sed -n 's,.* => \(.*\)/.*,\1,p' | sort | uniq
 }
 
 # Detect lib paths
@@ -319,7 +308,7 @@ get_fs_env() {
     [[ $1 ]] || return
     unset ID_FS_TYPE
     ID_FS_TYPE=$(blkid -u filesystem -o export -- "$1" \
-        | while read line; do
+        | while read line || [ -n "$line" ]; do
             if [[ "$line" == TYPE\=* ]]; then
                 printf "%s" "${line#TYPE=}";
                 exit 0;
@@ -440,12 +429,12 @@ shorten_persistent_dev() {
 # $ find_block_device /usr
 # 8:4
 find_block_device() {
-    local _majmin _dev _majmin _find_mpt
+    local _dev _majmin _find_mpt
     _find_mpt="$1"
     if [[ $use_fstab != yes ]]; then
         [[ -d $_find_mpt/. ]]
         findmnt -e -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" | { \
-            while read _majmin _dev; do
+            while read _majmin _dev || [ -n "$_dev" ]; do
                 if [[ -b $_dev ]]; then
                     if ! [[ $_majmin ]] || [[ $_majmin == 0:* ]]; then
                         _majmin=$(get_maj_min $_dev)
@@ -466,7 +455,7 @@ find_block_device() {
     # fall back to /etc/fstab
 
     findmnt -e --fstab -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" | { \
-        while read _majmin _dev; do
+        while read _majmin _dev || [ -n "$_dev" ]; do
             if ! [[ $_dev ]]; then
                 _dev="$_majmin"
                 unset _majmin
@@ -502,7 +491,7 @@ find_mp_fstype() {
 
     if [[ $use_fstab != yes ]]; then
         findmnt -e -v -n -o 'FSTYPE' --target "$1" | { \
-            while read _fs; do
+            while read _fs || [ -n "$_fs" ]; do
                 [[ $_fs ]] || continue
                 [[ $_fs = "autofs" ]] && continue
                 printf "%s" "$_fs"
@@ -511,7 +500,7 @@ find_mp_fstype() {
     fi
 
     findmnt --fstab -e -v -n -o 'FSTYPE' --target "$1" | { \
-        while read _fs; do
+        while read _fs || [ -n "$_fs" ]; do
             [[ $_fs ]] || continue
             [[ $_fs = "autofs" ]] && continue
             printf "%s" "$_fs"
@@ -538,7 +527,7 @@ find_dev_fstype() {
 
     if [[ $use_fstab != yes ]]; then
         findmnt -e -v -n -o 'FSTYPE' --source "$_find_dev" | { \
-            while read _fs; do
+            while read _fs || [ -n "$_fs" ]; do
                 [[ $_fs ]] || continue
                 [[ $_fs = "autofs" ]] && continue
                 printf "%s" "$_fs"
@@ -547,7 +536,7 @@ find_dev_fstype() {
     fi
 
     findmnt --fstab -e -v -n -o 'FSTYPE' --source "$_find_dev" | { \
-        while read _fs; do
+        while read _fs || [ -n "$_fs" ]; do
             [[ $_fs ]] || continue
             [[ $_fs = "autofs" ]] && continue
             printf "%s" "$_fs"
@@ -607,7 +596,7 @@ for_each_host_dev_fs()
     local _dev
     local _ret=1
 
-    [[ "${!host_fs_types[@]}" ]] || return 0
+    [[ "${#host_fs_types[@]}" ]] || return 0
 
     for _dev in "${!host_fs_types[@]}"; do
         $_func "$_dev" "${host_fs_types[$_dev]}" && _ret=0
@@ -668,7 +657,7 @@ for_each_host_dev_and_slaves_all()
 
     [[ "${host_devs[@]}" ]] || return 0
 
-    for _dev in ${host_devs[@]}; do
+    for _dev in "${host_devs[@]}"; do
         [[ -b "$_dev" ]] || continue
         if check_block_and_slaves_all $_func $(get_maj_min $_dev); then
             _ret=0
@@ -684,7 +673,7 @@ for_each_host_dev_and_slaves()
 
     [[ "${host_devs[@]}" ]] || return 0
 
-    for _dev in ${host_devs[@]}; do
+    for _dev in "${host_devs[@]}"; do
         [[ -b "$_dev" ]] || continue
         check_block_and_slaves $_func $(get_maj_min $_dev) && return 0
     done
@@ -948,26 +937,23 @@ inst_rules() {
     for _rule in "$@"; do
         if [ "${_rule#/}" = "$_rule" ]; then
             for r in ${udevdir}/rules.d ${hostonly:+/etc/udev/rules.d}; do
-                if [[ -e $r/$_rule ]]; then
-                    _found="$r/$_rule"
-                    inst_rule_programs "$_found"
-                    inst_rule_group_owner "$_found"
-                    inst_rule_initqueue "$_found"
-                    inst_simple "$_found"
-                fi
+                [[ -e $r/$_rule ]] || continue
+                _found="$r/$_rule"
+                inst_rule_programs "$_found"
+                inst_rule_group_owner "$_found"
+                inst_rule_initqueue "$_found"
+                inst_simple "$_found"
             done
         fi
         for r in '' $dracutbasedir/rules.d/; do
             # skip rules without an absolute path
             [[ "${r}$_rule" != /* ]] && continue
-
-            if [[ -f ${r}$_rule ]]; then
-                _found="${r}$_rule"
-                inst_rule_programs "$_found"
-                inst_rule_group_owner "$_found"
-                inst_rule_initqueue "$_found"
-                inst_simple "$_found" "$_target/${_found##*/}"
-            fi
+            [[ -f ${r}$_rule ]] || continue
+            _found="${r}$_rule"
+            inst_rule_programs "$_found"
+            inst_rule_group_owner "$_found"
+            inst_rule_initqueue "$_found"
+            inst_simple "$_found" "$_target/${_found##*/}"
         done
         [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
     done
@@ -979,23 +965,21 @@ inst_rules_wildcard() {
     inst_dir "${udevdir}/rules.d"
     inst_dir "$_target"
     for _rule in ${udevdir}/rules.d/$1 ${dracutbasedir}/rules.d/$1 ; do
-        if [[ -e $_rule ]]; then
+        [[ -e $_rule ]] || continue
+        inst_rule_programs "$_rule"
+        inst_rule_group_owner "$_rule"
+        inst_rule_initqueue "$_rule"
+        inst_simple "$_rule"
+        _found=$_rule
+    done
+    if [[ -n ${hostonly} ]] ; then
+        for _rule in ${_target}/$1 ; do
+            [[ -f $_rule ]] || continue
             inst_rule_programs "$_rule"
             inst_rule_group_owner "$_rule"
             inst_rule_initqueue "$_rule"
             inst_simple "$_rule"
             _found=$_rule
-        fi
-    done
-    if [ -n ${hostonly} ] ; then
-        for _rule in ${_target}/$1 ; do
-            if [[ -f $_rule ]]; then
-                inst_rule_programs "$_rule"
-                inst_rule_group_owner "$_rule"
-                inst_rule_initqueue "$_rule"
-                inst_simple "$_rule"
-                _found=$_rule
-            fi
         done
     fi
     [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
@@ -1007,7 +991,7 @@ prepare_udev_rules() {
     for f in "$@"; do
         f="${initdir}/etc/udev/rules.d/$f"
         [ -e "$f" ] || continue
-        while read line; do
+        while read line || [ -n "$line" ]; do
             if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
                 if [ $UDEVVERSION -ge 174 ]; then
                     printf '%sIMPORT{builtin}="path_id"\n' "${line%%IMPORT PATH_ID}"
@@ -1062,10 +1046,9 @@ inst_any() {
     [[ $1 = '-d' ]] && to="$2" && shift 2
 
     for f in "$@"; do
-        if [[ -e $f ]]; then
-            [[ $to ]] && inst "$f" "$to" && return 0
-            inst "$f" && return 0
-        fi
+        [[ -e $f ]] || continue
+        [[ $to ]] && inst "$f" "$to" && return 0
+        inst "$f" && return 0
     done
 
     return 1
@@ -1129,8 +1112,7 @@ inst_decompress() {
 inst_opt_decompress() {
     local _src
 
-    for _src in $@
-    do
+    for _src in $@; do
         inst_decompress "${_src}" || inst "${_src}"
     done
 }
@@ -1140,7 +1122,7 @@ inst_opt_decompress() {
 # or the "check" script, if module-setup.sh is not found
 # "check $hostonly" is called
 module_check() {
-    local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
+    local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
     local _ret
     local _forced=0
     local _hostonly=$hostonly
@@ -1171,7 +1153,7 @@ module_check() {
 # 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})
+    local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
     local _ret
     mount_needs=1
     [[ -d $_moddir ]] || return 1
@@ -1196,7 +1178,7 @@ module_check_mount() {
 # 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})
+    local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
     local _ret
     [[ -d $_moddir ]] || return 1
     if [[ ! -f $_moddir/module-setup.sh ]]; then
@@ -1219,7 +1201,7 @@ module_depends() {
 # 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})
+    local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
     local _ret
     [[ -d $_moddir ]] || return 1
     if [[ ! -f $_moddir/module-setup.sh ]]; then
@@ -1240,7 +1222,7 @@ module_cmdline() {
 # 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})
+    local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
     local _ret
     [[ -d $_moddir ]] || return 1
     if [[ ! -f $_moddir/module-setup.sh ]]; then
@@ -1261,7 +1243,7 @@ module_install() {
 # 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})
+    local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
     local _ret
     [[ -d $_moddir ]] || return 1
     if [[ ! -f $_moddir/module-setup.sh ]]; then
@@ -1283,11 +1265,11 @@ module_installkernel() {
 # device and filesystem types in "${host_fs_types[@]}"
 check_mount() {
     local _mod=$1
-    local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
+    local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
     local _ret
     local _moddep
 
-    [ "${#host_fs_types[*]}" -le 0 ] && return 1
+    [ "${#host_fs_types[@]}" -le 0 ] && return 1
 
     # If we are already scheduled to be loaded, no need to check again.
     [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
@@ -1348,7 +1330,7 @@ check_mount() {
 # that the modules were checked for the dependency tracking process
 check_module() {
     local _mod=$1
-    local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
+    local _moddir=$(echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; })
     local _ret
     local _moddep
     # If we are already scheduled to be loaded, no need to check again.
@@ -1461,7 +1443,7 @@ install_kmod_with_fw() {
 
     if [[ $omit_drivers ]]; then
         local _kmod=${1##*/}
-        _kmod=${_kmod%.ko}
+        _kmod=${_kmod%.ko*}
         _kmod=${_kmod/-/_}
         if [[ "$_kmod" =~ $omit_drivers ]]; then
             dinfo "Omitting driver $_kmod"
@@ -1475,7 +1457,7 @@ install_kmod_with_fw() {
 
     if [[ $silent_omit_drivers ]]; then
         local _kmod=${1##*/}
-        _kmod=${_kmod%.ko}
+        _kmod=${_kmod%.ko*}
         _kmod=${_kmod/-/_}
         [[ "$_kmod" =~ $silent_omit_drivers ]] && return 0
         [[ "${1##*/lib/modules/$kernel/}" =~ $silent_omit_drivers ]] && return 0
@@ -1493,10 +1475,9 @@ install_kmod_with_fw() {
     for _fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do
         _found=''
         for _fwdir in $fw_dir; do
-            if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
-                inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
-                _found=yes
-            fi
+            [[ -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
@@ -1522,7 +1503,7 @@ 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; do
+        while read _cmd _modpath _options || [ -n "$_cmd" ]; do
             [[ $_cmd = insmod ]] || continue
             $_func ${_modpath} || exit $?
         done
@@ -1538,7 +1519,7 @@ dracut_kernel_post() {
             --ignore-install --show-depends --set-version $kernel \
             < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" 2>/dev/null \
             | sort -u \
-            | while read _cmd _modpath _options; do
+            | while read _cmd _modpath _options || [ -n "$_cmd" ]; do
             [[ $_cmd = insmod ]] || continue
             echo "$_modpath"
         done > "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
@@ -1547,7 +1528,7 @@ dracut_kernel_post() {
             if [[ $DRACUT_INSTALL ]] && [[ -z $_moddirname ]]; then
                 xargs -r $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} -a < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
             else
-                while read _modpath; do
+                while read _modpath || [ -n "$_modpath" ]; do
                     local _destpath=$_modpath
                     [[ $_moddirname ]] && _destpath=${_destpath##$_moddirname/}
                     _destpath=${_destpath##*/lib/modules/$kernel/}
@@ -1555,12 +1536,12 @@ dracut_kernel_post() {
                 done < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
             fi
         ) &
-        _pid=$(jobs -p | while read a ; do printf ":$a";done)
+        _pid=$(jobs -p | while read a  || [ -n "$a" ]; do printf ":$a";done)
         _pid=${_pid##*:}
 
         if [[ $DRACUT_INSTALL ]]; then
             xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" \
-                | while read line; do
+                | while read line || [ -n "$line" ]; do
                 for _fwdir in $fw_dir; do
                     echo $_fwdir/$line;
                 done;
@@ -1568,10 +1549,9 @@ dracut_kernel_post() {
         else
             for _fw in $(xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"); do
                 for _fwdir in $fw_dir; do
-                    if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
-                        inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
-                        break
-                    fi
+                    [[ -d $_fwdir && -f $_fwdir/$_fw ]] || continue
+                    inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
+                    break
                 done
             done
         fi
@@ -1599,7 +1579,7 @@ module_is_host_only() {
     local _mod=$1
     local _modenc a i _k _s _v _aliases
     _mod=${_mod##*/}
-    _mod=${_mod%.ko}
+    _mod=${_mod%.ko*}
     _modenc=${_mod//-/_}
 
     [[ " $add_drivers " == *\ ${_mod}\ * ]] && return 0
@@ -1645,7 +1625,7 @@ find_kernel_modules_by_path () {
 
     _OLDIFS=$IFS
     IFS=:
-    while read a rest; do
+    while read a rest || [ -n "$a" ]; do
         [[ $a = */$1/* ]] || [[ $a = updates/* ]] || continue
         printf "%s\n" "$srcmods/$a"
     done < "$srcmods/modules.dep"
@@ -1691,16 +1671,16 @@ instmods() {
                 _mod=${_mod##*/}
                 # Check for aliased modules
                 _modalias=$(modinfo -k $kernel -F filename $_mod 2> /dev/null)
-                _modalias=${_modalias%.ko}
-                if [ "${_modalias##*/}" != "$_mod" ] ; then
+                _modalias=${_modalias%.ko*}
+                if [[ $_modalias ]] && [ "${_modalias##*/}" != "${_mod%.ko*}" ] ; then
                     _mod=${_modalias##*/}
                 fi
 
                 # if we are already installed, skip this module and go on
                 # to the next one.
                 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
-                    [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko" ]]; then
-                    read _ret <"$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko"
+                    [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko*}" ]]; then
+                    read _ret <"$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko*}"
                     return $_ret
                 fi
 
@@ -1730,7 +1710,7 @@ instmods() {
                     ((_ret+=$?))
                 else
                     [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
-                        echo $_mod >> "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist"
+                        echo ${_mod%.ko*} >> "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist"
                 fi
                 ;;
         esac
@@ -1740,20 +1720,18 @@ instmods() {
     function instmods_1() {
         local _mod _mpargs
         if (($# == 0)); then  # filenames from stdin
-            while read _mod; do
+            while read _mod || [ -n "$_mod" ]; do
                 inst1mod "${_mod%.ko*}" || {
-                    if [[ "$_check" == "yes" ]]; then
-                        [[ "$_silent" == "no" ]] && dfatal "Failed to install module $_mod"
-                        return 1
+                    if [[ "$_check" == "yes" ]] && [[ "$_silent" == "no" ]]; then
+                        dfatal "Failed to install module $_mod"
                     fi
                 }
             done
         fi
         while (($# > 0)); do  # filenames as arguments
             inst1mod ${1%.ko*} || {
-                if [[ "$_check" == "yes" ]]; then
-                    [[ "$_silent" == "no" ]] && dfatal "Failed to install module $1"
-                    return 1
+                if [[ "$_check" == "yes" ]] && [[ "$_silent" == "no" ]]; then
+                    dfatal "Failed to install module $1"
                 fi
             }
             shift
@@ -1765,7 +1743,7 @@ instmods() {
     # Capture all stderr from modprobe to _fderr. We could use {var}>...
     # redirections, but that would make dracut require bash4 at least.
     eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
-        | while read line; do [[ "$line" =~ $_filter_not_found ]] || echo $line;done | derror
+        | while read line || [ -n "$line" ]; do [[ "$line" =~ $_filter_not_found ]] || echo $line;done | derror
     _ret=$?
     return $_ret
 }
@@ -1832,3 +1810,12 @@ lvm_internal_dev() {
     [[ ${DM_LV_LAYER} ]] || [[ ! -L /dev/${DM_VG_NAME}/${DM_LV_NAME} ]]
 }
 
+btrfs_devs() {
+    local _mp="$1"
+    btrfs device usage "$_mp" \
+        | while read _dev _rest; do
+        str_starts "$_dev" "/" || continue
+        _dev=${_dev%,}
+        printf -- "%s\n" "$_dev"
+        done
+}