2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 sts=4 et filetype=sh
5 # functions used by dracut and other tools.
7 # Copyright 2005-2009 Red Hat, Inc. All rights reserved.
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 if [[ $DRACUT_KERNEL_LAZY ]] && ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
25 if ! [[ -d "$initdir/.kernelmodseen" ]]; then
26 mkdir
-p "$initdir/.kernelmodseen"
28 DRACUT_KERNEL_LAZY_HASHDIR
="$initdir/.kernelmodseen"
31 if [[ $initdir ]] && ! [[ -d $initdir ]]; then
35 # Generic substring function. If $2 is in $1, return 0.
36 strstr
() { [[ $1 = *$2* ]]; }
38 # find a binary. If we were not passed the full path directly,
39 # search in the usual places to find the binary.
41 if [[ -z ${1##/*} ]]; then
42 if [[ -x $1 ]] ||
{ [[ "$1" == *.so
* ]] && ldd
"$1" &>/dev
/null
; }; then
51 if ! [[ $dracutbasedir ]]; then
52 dracutbasedir
=${BASH_SOURCE[0]%/*}
53 [[ $dracutbasedir = "dracut-functions" ]] && dracutbasedir
="."
54 [[ $dracutbasedir ]] || dracutbasedir
="."
55 dracutbasedir
="$(readlink -f $dracutbasedir)"
63 ldconfig
-pN 2>/dev
/null |
while read a b c d
; do
64 [[ "$c" != "=>" ]] && continue
70 printf "%s\n" ${!a[@]}
74 if ! [[ $libdirs ]] ; then
75 if [[ "$(ldd /bin/sh)" == */lib64
/* ]] &>/dev
/null \
76 && [[ -d /lib64
]]; then
78 [[ -d /usr
/lib64
]] && libdirs
+=" /usr/lib64"
81 [[ -d /usr
/lib
]] && libdirs
+=" /usr/lib"
84 libdirs
+="$(ldconfig_paths)"
89 if ! [[ $kernel ]]; then
94 # Version comparision function. Assumes Linux style version scheme.
96 # $2 = comparision op (gt, ge, eq, le, lt, ne)
99 local _n1
=(${1//./ }) _op
=$2 _n2
=(${3//./ }) _i _res
103 if [[ ! ${_n1[_i]}${_n2[_i]} ]]; then _res
=0
104 elif ((${_n1[_i]:-0} > ${_n2[_i]:-0})); then _res
=1
105 elif ((${_n1[_i]:-0} < ${_n2[_i]:-0})); then _res
=2
121 srcmods
="/lib/modules/$kernel/"
123 [[ $drivers_dir ]] && {
124 if ! command -v kmod
&>/dev
/null
&& vercmp
"$(modprobe --version | cut -d' ' -f3)" lt
3.7; then
125 dfatal
'To use --kmoddir option module-init-tools >= 3.7 is required.'
128 srcmods
="$drivers_dir"
132 if ! type dinfo
>/dev
/null
2>&1; then
133 .
"$dracutbasedir/dracut-logger.sh"
137 if ! [[ $initdir ]]; then
138 dfatal
"initdir not set"
142 # export standard hookdirs
144 hookdirs
="cmdline pre-udev pre-trigger netroot "
145 hookdirs
+="initqueue initqueue/settled initqueue/online initqueue/finished initqueue/timeout "
146 hookdirs
+="pre-mount pre-pivot cleanup mount "
147 hookdirs
+="emergency shutdown-emergency pre-shutdown shutdown "
151 dracut_need_initqueue
() {
152 >"$initdir/lib/dracut/need-initqueue"
155 dracut_module_included
() {
156 [[ "$mods_to_load $modules_loaded" == *$@
* ]]
159 # Create all subdirectories for given path without creating the last element.
162 [[ -e ${1%/*} ]] || mkdir
-m 0755 -p -- "${1%/*}"
166 # Check whether $1 is a function.
168 [[ "$(type -t "$1")" = "function" ]]
171 # Function prints global variables in format name=value line by line.
172 # $@ = list of global variables' name
178 eval printf -v _value
"%s" "\$$_var"
179 [[ ${_value} ]] && printf '%s="%s"\n' "$_var" "$_value"
183 # normalize_path <path>
184 # Prints the normalized path, where it removes any duplicated
185 # and trailing slashes.
187 # $ normalize_path ///test/test//
191 set -- "${1//+(\/)//}"
196 # convert_abs_rel <from> <to>
197 # Prints the relative path, when creating a symlink to <to> from <from>.
199 # $ convert_abs_rel /usr/bin/test /bin/test-2
201 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
203 local __current __absolute __abssize __cursize __newpath
206 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
208 # corner case #1 - self looping link
209 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
211 # corner case #2 - own dir link
212 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
214 IFS
="/" __current
=($1)
215 IFS
="/" __absolute
=($2)
217 __abssize
=${#__absolute[@]}
218 __cursize
=${#__current[@]}
220 while [[ "${__absolute[__level]}" == "${__current[__level]}" ]]
223 if (( __level
> __abssize || __level
> __cursize
))
229 for ((__i
= __level
; __i
< __cursize-1
; __i
++))
233 __newpath
=$__newpath"/"
235 __newpath
=$__newpath".."
238 for ((__i
= __level
; __i
< __abssize
; __i
++))
240 if [[ -n $__newpath ]]
242 __newpath
=$__newpath"/"
244 __newpath
=$__newpath${__absolute[__i]}
250 if [[ "$(ln --help)" == *--relative* ]]; then
252 ln -sfnr "${initdir}/$1" "${initdir}/$2"
258 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
259 ln -sfn -- "$
(convert_abs_rel
"${_dest}" "${_source}")" "${initdir}/${_dest}"
263 # get_fs_env <device>
264 # Get and set the ID_FS_TYPE variable from udev for a device.
266 # $ get_fs_env /dev/sda2; echo $ID_FS_TYPE
274 ID_FS_TYPE
=$
(blkid
-u filesystem
-o export -- "$1" \
275 |
while read line
; do
276 if [[ "$line" == TYPE\
=* ]]; then
277 printf "%s" "${line#TYPE=}";
281 if [[ $ID_FS_TYPE ]]; then
282 printf "%s" "$ID_FS_TYPE"
288 # get_maj_min <device>
289 # Prints the major and minor of a device node.
291 # $ get_maj_min /dev/sda2
294 local _maj _min _majmin
295 _majmin
="$(stat -L -c '%t:%T' "$1" 2>/dev/null)"
296 printf "%s" "$((0x${_majmin%:*})):$((0x${_majmin#*:}))"
299 # get a persistent path from a device
300 get_persistent_dev
() {
303 _dev
=$
(get_maj_min
"$1")
304 [ -z "$_dev" ] && return
306 for i
in /dev
/mapper
/* /dev
/disk
/by-uuid
/* /dev
/disk
/by-id
/*; do
307 [[ $i == /dev
/mapper
/mpath
* ]] && continue
308 _tmp
=$
(get_maj_min
"$i")
309 if [ "$_tmp" = "$_dev" ]; then
316 # find_block_device <mountpoint>
317 # Prints the major and minor number of the block device
318 # for a given mountpoint.
319 # Unless $use_fstab is set to "yes" the functions
320 # uses /proc/self/mountinfo as the primary source of the
321 # information and only falls back to /etc/fstab, if the mountpoint
322 # is not found there.
324 # $ find_block_device /usr
326 find_block_device
() {
327 local _majmin _dev _majmin _find_mpt
329 if [[ $use_fstab != yes ]]; then
330 [[ -d $_find_mpt/.
]]
331 findmnt
-e -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" |
{ \
332 while read _majmin _dev
; do
333 if [[ -b $_dev ]]; then
334 if ! [[ $_majmin ]] ||
[[ $_majmin == 0:* ]]; then
335 _majmin
=$
(get_maj_min
$_dev)
337 if [[ $_majmin ]]; then
344 if [[ $_dev = *:* ]]; then
348 done; return 1; } && return 0
350 # fall back to /etc/fstab
352 findmnt
-e --fstab -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" |
{ \
353 while read _majmin _dev
; do
354 if ! [[ $_dev ]]; then
358 if [[ -b $_dev ]]; then
359 [[ $_majmin ]] || _majmin
=$
(get_maj_min
$_dev)
360 if [[ $_majmin ]]; then
367 if [[ $_dev = *:* ]]; then
371 done; return 1; } && return 0
376 # find_mp_fstype <mountpoint>
377 # Echo the filesystem type for a given mountpoint.
378 # /proc/self/mountinfo is taken as the primary source of information
379 # and /etc/fstab is used as a fallback.
380 # No newline is appended!
382 # $ find_mp_fstype /;echo
387 if [[ $use_fstab != yes ]]; then
388 findmnt
-e -v -n -o 'FSTYPE' --target "$1" |
{ \
390 [[ $_fs ]] ||
continue
391 [[ $_fs = "autofs" ]] && continue
394 done; return 1; } && return 0
397 findmnt
--fstab -e -v -n -o 'FSTYPE' --target "$1" |
{ \
399 [[ $_fs ]] ||
continue
400 [[ $_fs = "autofs" ]] && continue
403 done; return 1; } && return 0
408 # find_dev_fstype <device>
409 # Echo the filesystem type for a given device.
410 # /proc/self/mountinfo is taken as the primary source of information
411 # and /etc/fstab is used as a fallback.
412 # No newline is appended!
414 # $ find_dev_fstype /dev/sda2;echo
419 if ! [[ "$_find_dev" = /dev
* ]]; then
420 [[ -b "/dev/block/$_find_dev" ]] && _find_dev
="/dev/block/$_find_dev"
423 if [[ $use_fstab != yes ]]; then
424 findmnt
-e -v -n -o 'FSTYPE' --source "$_find_dev" |
{ \
426 [[ $_fs ]] ||
continue
427 [[ $_fs = "autofs" ]] && continue
430 done; return 1; } && return 0
433 findmnt
--fstab -e -v -n -o 'FSTYPE' --source "$_find_dev" |
{ \
435 [[ $_fs ]] ||
continue
436 [[ $_fs = "autofs" ]] && continue
439 done; return 1; } && return 0
445 # finds the major:minor of the block device backing the root filesystem.
446 find_root_block_device
() { find_block_device
/; }
448 # for_each_host_dev_fs <func>
449 # Execute "<func> <dev> <filesystem>" for every "<dev> <fs>" pair found
450 # in ${host_fs_types[@]}
451 for_each_host_dev_fs
()
457 [[ "${!host_fs_types[@]}" ]] ||
return 0
459 for _dev
in "${!host_fs_types[@]}"; do
460 $_func "$_dev" "${host_fs_types[$_dev]}" && _ret
=0
467 echo "${host_fs_types[@]}"
470 # Walk all the slave relationships for a given block device.
471 # Stop when our helper function returns success
472 # $1 = function to call on every found block device
473 # $2 = block device in major:minor format
474 check_block_and_slaves
() {
476 [[ -b /dev
/block
/$2 ]] ||
return 1 # Not a block device? So sorry.
478 check_vol_slaves
"$@" && return 0
479 if [[ -f /sys
/dev
/block
/$2/..
/dev
]]; then
480 check_block_and_slaves
$1 $
(<"/sys/dev/block/$2/../dev") && return 0
482 [[ -d /sys
/dev
/block
/$2/slaves
]] ||
return 1
483 for _x
in /sys
/dev
/block
/$2/slaves
/*/dev
; do
484 [[ -f $_x ]] ||
continue
485 check_block_and_slaves
$1 $
(<"$_x") && return 0
490 check_block_and_slaves_all
() {
492 [[ -b /dev
/block
/$2 ]] ||
return 1 # Not a block device? So sorry.
496 check_vol_slaves
"$@" && return 0
497 if [[ -f /sys
/dev
/block
/$2/..
/dev
]]; then
498 check_block_and_slaves_all
$1 $
(<"/sys/dev/block/$2/../dev") && _ret
=0
500 [[ -d /sys
/dev
/block
/$2/slaves
]] ||
return 1
501 for _x
in /sys
/dev
/block
/$2/slaves
/*/dev
; do
502 [[ -f $_x ]] ||
continue
503 check_block_and_slaves_all
$1 $
(<"$_x") && _ret
=0
507 # for_each_host_dev_and_slaves <func>
508 # Execute "<func> <dev>" for every "<dev>" found
509 # in ${host_devs[@]} and their slaves
510 for_each_host_dev_and_slaves_all
()
516 [[ "${host_devs[@]}" ]] ||
return 0
518 for _dev
in ${host_devs[@]}; do
519 [[ -b "$_dev" ]] ||
continue
520 if check_block_and_slaves_all
$_func $
(get_maj_min
$_dev); then
527 for_each_host_dev_and_slaves
()
532 [[ "${host_devs[@]}" ]] ||
return 0
534 for _dev
in ${host_devs[@]}; do
535 [[ -b "$_dev" ]] ||
continue
536 check_block_and_slaves
$_func $
(get_maj_min
$_dev) && return 0
541 # ugly workaround for the lvm design
542 # There is no volume group device,
543 # so, there are no slave devices for volume groups.
544 # Logical volumes only have the slave devices they really live on,
545 # but you cannot create the logical volume without the volume group.
546 # And the volume group might be bigger than the devices the LV needs.
549 for i
in /dev
/mapper
/*; do
550 _lv
=$
(get_maj_min
$i)
551 if [[ $_lv = $2 ]]; then
552 _vg
=$
(lvm lvs
--noheadings -o vg_name
$i 2>/dev
/null
)
556 for _pv
in $
(lvm vgs
--noheadings -o pv_name
"$_vg" 2>/dev
/null
)
558 check_block_and_slaves
$1 $
(get_maj_min
$_pv) && return 0
566 # fs_get_option <filesystem options> <search for option>
567 # search for a specific option in a bunch of filesystem options
568 # and return the value
576 while [ $# -gt 0 ]; do
579 echo ${1#${_option}=}
587 if ! [[ $DRACUT_INSTALL ]]; then
588 DRACUT_INSTALL
=$
(find_binary dracut-install
)
591 if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install
]]; then
592 DRACUT_INSTALL
=$dracutbasedir/dracut-install
595 if ! [[ -x $DRACUT_INSTALL ]]; then
596 dfatal
"dracut-install not found!"
600 [[ $DRACUT_RESOLVE_LAZY ]] ||
export DRACUT_RESOLVE_DEPS
=1
602 [[ -e ${initdir}/"$1" ]] && return 0 # already there
603 $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@"
604 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@" ||
:
608 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
609 #dinfo "$DRACUT_INSTALL -l $@"
610 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
611 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" ||
:
615 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
616 [[ -e $1 ]] ||
return 1 # no source
617 $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@"
618 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} "$@" ||
:
622 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
623 [[ -L $1 ]] ||
return 1
624 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
625 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" ||
:
630 #dinfo "initdir=$initdir $DRACUT_INSTALL -l $@"
631 $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
633 (($ret != 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" ||
:
642 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
643 [[ -e $1 ]] ||
return 1 # no source
644 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
645 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" ||
:
649 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
650 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" ||
:
654 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
655 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" ||
:
658 # find symlinks linked to given library file
660 # Function searches for symlinks by stripping version numbers appended to
661 # library filename, checks if it points to the same target and finally
662 # prints the list of symlinks to stdout.
665 # rev_lib_symlinks libfoo.so.8.1
666 # output: libfoo.so.8 libfoo.so
667 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
669 [[ ! $1 ]] && return 0
671 local fn
="$1" orig
="$(readlink -f "$1")" links
=''
673 [[ ${fn} == *.so.
* ]] ||
return 1
675 until [[ ${fn##*.} == so
]]; do
677 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
683 # attempt to install any programs specified in a udev rule
684 inst_rule_programs() {
687 if grep -qE 'PROGRAM==?"[^
"]+' "$1"; then
688 for _prog in $(grep -E 'PROGRAM==?"[^
"]+' "$1" | sed -r 's/.*PROGRAM==?"([^
"]+).*/\1/'); do
690 if [ -x ${udevdir}/$_prog ]; then
691 _bin=${udevdir}/$_prog
692 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
693 _bin=$(find_binary "$_prog") || {
694 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
699 [[ $_bin ]] && inst_binary "$_bin"
702 if grep -qE 'RUN[+=]=?"[^
"]+' "$1"; then
703 for _prog in $(grep -E 'RUN[+=]=?"[^
"]+' "$1" | sed -r 's/.*RUN[+=]=?"([^
"]+).*/\1/'); do
705 if [ -x ${udevdir}/$_prog ]; then
706 _bin=${udevdir}/$_prog
707 elif [[ "${_prog/\$env\{/}" == "$_prog" ]] && [[ "${_prog}" != "/sbin
/initqueue
" ]]; then
708 _bin=$(find_binary "$_prog") || {
709 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
714 [[ $_bin ]] && inst_binary "$_bin"
717 if grep -qE 'IMPORT\{program\}==?"[^
"]+' "$1"; then
718 for _prog in $(grep -E 'IMPORT\{program\}==?"[^
"]+' "$1" | sed -r 's/.*IMPORT\{program\}==?"([^
"]+).*/\1/'); do
720 if [ -x ${udevdir}/$_prog ]; then
721 _bin=${udevdir}/$_prog
722 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
723 _bin=$(find_binary "$_prog") || {
724 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
729 [[ $_bin ]] && dracut_install "$_bin"
734 # attempt to install any programs specified in a udev rule
735 inst_rule_group_owner() {
738 if grep -qE 'OWNER=?"[^
"]+' "$1"; then
739 for i in $(grep -E 'OWNER=?"[^
"]+' "$1" | sed -r 's/.*OWNER=?"([^
"]+).*/\1/'); do
740 if ! egrep -q "^
$i:" "$initdir/etc
/passwd
" 2>/dev/null; then
741 egrep "^
$i:" /etc/passwd 2>/dev/null >> "$initdir/etc
/passwd
"
745 if grep -qE 'GROUP=?"[^
"]+' "$1"; then
746 for i in $(grep -E 'GROUP=?"[^
"]+' "$1" | sed -r 's/.*GROUP=?"([^
"]+).*/\1/'); do
747 if ! egrep -q "^
$i:" "$initdir/etc
/group
" 2>/dev/null; then
748 egrep "^
$i:" /etc/group 2>/dev/null >> "$initdir/etc
/group
"
754 inst_rule_initqueue() {
755 if grep -q -F initqueue "$1"; then
756 dracut_need_initqueue
760 # udev rules always get installed in the same place, so
761 # create a function to install them to make life simpler.
763 local _target=/etc/udev/rules.d _rule _found
765 inst_dir "${udevdir}/rules.d
"
767 for _rule in "$@
"; do
768 if [ "${_rule#/}" = "$_rule" ]; then
769 for r in ${udevdir}/rules.d /etc/udev/rules.d; do
770 if [[ -f $r/$_rule ]]; then
772 inst_rule_programs "$_found"
773 inst_rule_group_owner "$_found"
774 inst_rule_initqueue "$_found"
775 inst_simple "$_found"
779 for r in '' ./ $dracutbasedir/rules.d/; do
780 if [[ -f ${r}$_rule ]]; then
782 inst_rule_programs "$_found"
783 inst_rule_group_owner "$_found"
784 inst_rule_initqueue "$_found"
785 inst_simple "$_found" "$_target/${_found##*/}"
788 [[ $_found ]] || dinfo "Skipping udev rule
: $_rule"
792 prepare_udev_rules() {
793 [ -z "$UDEVVERSION" ] && export UDEVVERSION=$(udevadm --version)
796 f="${initdir}/etc
/udev
/rules.d
/$f"
797 [ -e "$f" ] || continue
799 if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
800 if [ $UDEVVERSION -ge 174 ]; then
801 printf '%sIMPORT{builtin}="path_id
"\n' "${line%%IMPORT PATH_ID}"
803 printf '%sIMPORT{program}="path_id
%%p
"\n' "${line%%IMPORT PATH_ID}"
805 elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
806 if [ $UDEVVERSION -ge 176 ]; then
807 printf '%sIMPORT{builtin}="blkid
"\n' "${line%%IMPORT BLKID}"
809 printf '%sIMPORT{program}="/sbin
/blkid
-o udev
-p $tempnode"\n' "${line%%IMPORT BLKID}"
814 done < "${f}" > "${f}.new
"
819 # install function specialized for hooks
820 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
821 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
823 if ! [[ -f $3 ]]; then
824 dfatal "Cannot
install a hook
($3) that does not exist.
"
825 dfatal "Aborting initrd creation.
"
827 elif ! [[ "$hookdirs" == *$1* ]]; then
828 dfatal "No such hook
type $1. Aborting initrd creation.
"
831 inst_simple "$3" "/lib
/dracut
/hooks
/${1}/${2}-${3##*/}"
834 # install any of listed files
836 # If first argument is '-d' and second some destination path, first accessible
837 # source is installed into this path, otherwise it will installed in the same
838 # path as source. If none of listed files was installed, function return 1.
839 # On first successful installation it returns with 0 status.
843 # inst_any -d /bin/foo /bin/bar /bin/baz
845 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
850 [[ $1 = '-d' ]] && to="$2" && shift 2
854 [[ $to ]] && inst "$f" "$to" && return 0
855 inst "$f" && return 0
863 # inst_libdir_file [-n <pattern>] <file> [<file>...]
864 # Install a <file> located on a lib directory to the initramfs image
865 # -n <pattern> install matching files
868 if [[ "$1" == "-n" ]]; then
871 for _dir in $libdirs; do
873 for _f in "$_dir"/$_i; do
874 [[ "$_f" =~ $_pattern ]] || continue
875 [[ -e "$_f" ]] && _files+="$_f "
880 for _dir in $libdirs; do
882 for _f in "$_dir"/$_i; do
883 [[ -e "$_f" ]] && _files+="$_f "
888 [[ $_files ]] && inst_multiple $_files
892 # install function decompressing the target and handling symlinks
893 # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
895 # Function install targets in the same paths inside overlay but decompressed
896 # and without extensions (.gz, .bz2).
903 *.gz) _cmd='gzip -f -d' ;;
904 *.bz2) _cmd='bzip2 -d' ;;
908 # Decompress with chosen tool. We assume that tool changes name e.g.
909 # from 'name.gz' to 'name'.
910 ${_cmd} "${initdir}${_src}"
914 # It's similar to above, but if file is not compressed, performs standard
917 inst_opt_decompress
() {
922 inst_decompress
"${_src}" || inst
"${_src}"
926 # module_check <dracut module>
927 # execute the check() function of module-setup.sh of <dracut module>
928 # or the "check" script, if module-setup.sh is not found
929 # "check $hostonly" is called
931 local _moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
934 local _hostonly
=$hostonly
935 [ $# -eq 2 ] && _forced
=$2
936 [[ -d $_moddir ]] ||
return 1
937 if [[ ! -f $_moddir/module-setup.sh
]]; then
938 # if we do not have a check script, we are unconditionally included
939 [[ -x $_moddir/check
]] ||
return 0
940 [ $_forced -ne 0 ] && unset hostonly
941 $_moddir/check
$hostonly
944 unset check depends
install installkernel
946 .
$_moddir/module-setup.sh
947 is_func check ||
return 0
948 [ $_forced -ne 0 ] && unset hostonly
951 unset check depends
install installkernel
957 # module_check_mount <dracut module>
958 # execute the check() function of module-setup.sh of <dracut module>
959 # or the "check" script, if module-setup.sh is not found
960 # "mount_needs=1 check 0" is called
961 module_check_mount
() {
962 local _moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
965 [[ -d $_moddir ]] ||
return 1
966 if [[ ! -f $_moddir/module-setup.sh
]]; then
967 # if we do not have a check script, we are unconditionally included
968 [[ -x $_moddir/check
]] ||
return 0
969 mount_needs
=1 $_moddir/check
0
972 unset check depends
install installkernel
974 .
$_moddir/module-setup.sh
977 unset check depends
install installkernel
983 # module_depends <dracut module>
984 # execute the depends() function of module-setup.sh of <dracut module>
985 # or the "depends" script, if module-setup.sh is not found
987 local _moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
989 [[ -d $_moddir ]] ||
return 1
990 if [[ ! -f $_moddir/module-setup.sh
]]; then
991 # if we do not have a check script, we have no deps
992 [[ -x $_moddir/check
]] ||
return 0
996 unset check depends
install installkernel
998 .
$_moddir/module-setup.sh
1001 unset check depends
install installkernel
1006 # module_install <dracut module>
1007 # execute the install() function of module-setup.sh of <dracut module>
1008 # or the "install" script, if module-setup.sh is not found
1010 local _moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
1012 [[ -d $_moddir ]] ||
return 1
1013 if [[ ! -f $_moddir/module-setup.sh
]]; then
1014 [[ -x $_moddir/install ]] && .
"$_moddir/install"
1017 unset check depends
install installkernel
1019 .
$_moddir/module-setup.sh
1022 unset check depends
install installkernel
1027 # module_installkernel <dracut module>
1028 # execute the installkernel() function of module-setup.sh of <dracut module>
1029 # or the "installkernel" script, if module-setup.sh is not found
1030 module_installkernel
() {
1031 local _moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
1033 [[ -d $_moddir ]] ||
return 1
1034 if [[ ! -f $_moddir/module-setup.sh
]]; then
1035 [[ -x $_moddir/installkernel
]] && .
"$_moddir/installkernel"
1038 unset check depends
install installkernel
1039 installkernel
() { true
; }
1040 .
$_moddir/module-setup.sh
1043 unset check depends
install installkernel
1048 # check_mount <dracut module>
1049 # check_mount checks, if a dracut module is needed for the given
1050 # device and filesystem types in "${host_fs_types[@]}"
1053 local _moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
1057 [ "${#host_fs_types[*]}" -le 0 ] && return 1
1059 # If we are already scheduled to be loaded, no need to check again.
1060 [[ " $mods_to_load " == *\
$_mod\
* ]] && return 0
1061 [[ " $mods_checked_as_dep " == *\
$_mod\
* ]] && return 1
1063 # This should never happen, but...
1064 [[ -d $_moddir ]] ||
return 1
1066 [[ $2 ]] || mods_checked_as_dep
+=" $_mod "
1068 if [[ " $omit_dracutmodules " == *\
$_mod\
* ]]; then
1072 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\
$_mod\
* ]]; then
1073 module_check_mount
$_mod; ret
=$?
1075 # explicit module, so also accept ret=255
1076 [[ $ret = 0 ||
$ret = 255 ]] ||
return 1
1078 # module not in our list
1079 if [[ $dracutmodules = all
]]; then
1080 # check, if we can and should install this module
1081 module_check_mount
$_mod ||
return 1
1089 for _moddep
in $
(module_depends
$_mod); do
1090 # handle deps as if they were manually added
1091 [[ " $add_dracutmodules " == *\
$_moddep\
* ]] || \
1092 add_dracutmodules
+=" $_moddep "
1093 [[ " $force_add_dracutmodules " == *\
$_moddep\
* ]] || \
1094 force_add_dracutmodules
+=" $_moddep "
1095 # if a module we depend on fail, fail also
1096 if ! check_module
$_moddep; then
1097 derror
"dracut module '$_mod' depends on '$_moddep', which can't be installed"
1102 [[ " $mods_to_load " == *\
$_mod\
* ]] || \
1103 mods_to_load
+=" $_mod "
1108 # check_module <dracut module> [<use_as_dep>]
1109 # check if a dracut module is to be used in the initramfs process
1110 # if <use_as_dep> is set, then the process also keeps track
1111 # that the modules were checked for the dependency tracking process
1114 local _moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
1117 # If we are already scheduled to be loaded, no need to check again.
1118 [[ " $mods_to_load " == *\
$_mod\
* ]] && return 0
1119 [[ " $mods_checked_as_dep " == *\
$_mod\
* ]] && return 1
1121 # This should never happen, but...
1122 [[ -d $_moddir ]] ||
return 1
1124 [[ $2 ]] || mods_checked_as_dep
+=" $_mod "
1126 if [[ " $omit_dracutmodules " == *\
$_mod\
* ]]; then
1127 dinfo
"dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
1131 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\
$_mod\
* ]]; then
1132 if [[ " $force_add_dracutmodules " == *\
$_mod\
* ]]; then
1133 module_check
$_mod 1; ret
=$?
1135 module_check
$_mod 0; ret
=$?
1137 # explicit module, so also accept ret=255
1138 [[ $ret = 0 ||
$ret = 255 ]] ||
return 1
1140 # module not in our list
1141 if [[ $dracutmodules = all
]]; then
1142 # check, if we can and should install this module
1143 module_check
$_mod ||
return 1
1150 for _moddep
in $
(module_depends
$_mod); do
1151 # handle deps as if they were manually added
1152 [[ " $add_dracutmodules " == *\
$_moddep\
* ]] || \
1153 add_dracutmodules
+=" $_moddep "
1154 [[ " $force_add_dracutmodules " == *\
$_moddep\
* ]] || \
1155 force_add_dracutmodules
+=" $_moddep "
1156 # if a module we depend on fail, fail also
1157 if ! check_module
$_moddep; then
1158 derror
"dracut module '$_mod' depends on '$_moddep', which can't be installed"
1163 [[ " $mods_to_load " == *\
$_mod\
* ]] || \
1164 mods_to_load
+=" $_mod "
1169 # for_each_module_dir <func>
1170 # execute "<func> <dracut module> 1"
1171 for_each_module_dir
() {
1177 for _moddir
in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1178 _mod
=${_moddir##*/}; _mod
=${_mod#[0-9][0-9]}
1182 # Report any missing dracut modules, the user has specified
1183 _modcheck
="$add_dracutmodules $force_add_dracutmodules"
1184 [[ $dracutmodules != all
]] && _modcheck
="$m $dracutmodules"
1185 for _mod
in $_modcheck; do
1186 [[ " $mods_to_load " == *\
$_mod\
* ]] && continue
1187 [[ " $omit_dracutmodules " == *\
$_mod\
* ]] && continue
1188 derror
"dracut module '$_mod' cannot be found or installed."
1192 # Install a single kernel module along with any firmware it may require.
1193 # $1 = full path to kernel module to install
1194 install_kmod_with_fw
() {
1195 # no need to go further if the module is already installed
1197 [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
1200 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -e "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}" ]]; then
1201 read ret
< "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
1205 if [[ $omit_drivers ]]; then
1206 local _kmod
=${1##*/}
1209 if [[ "$_kmod" =~
$omit_drivers ]]; then
1210 dinfo
"Omitting driver $_kmod"
1213 if [[ "${1##*/lib/modules/$kernel/}" =~
$omit_drivers ]]; then
1214 dinfo
"Omitting driver $_kmod"
1219 if [[ $silent_omit_drivers ]]; then
1220 local _kmod
=${1##*/}
1223 [[ "$_kmod" =~
$silent_omit_drivers ]] && return 0
1224 [[ "${1##*/lib/modules/$kernel/}" =~
$silent_omit_drivers ]] && return 0
1227 inst_simple
"$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
1229 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1230 [[ -d "$DRACUT_KERNEL_LAZY_HASHDIR" ]] && \
1231 echo $ret > "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
1232 (($ret != 0)) && return $ret
1234 local _modname
=${1##*/} _fwdir _found _fw
1235 _modname
=${_modname%.ko*}
1236 for _fw
in $
(modinfo
-k $kernel -F firmware
$1 2>/dev
/null
); do
1238 for _fwdir
in $fw_dir; do
1239 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1240 inst_simple
"$_fwdir/$_fw" "/lib/firmware/$_fw"
1244 if [[ $_found != yes ]]; then
1245 if ! [[ -d $
(echo /sys
/module
/${_modname//-/_}|
{ read a b
; echo $a; }) ]]; then
1246 dinfo
"Possible missing firmware \"${_fw}\" for kernel module" \
1247 "\"${_modname}.ko\""
1249 dwarn
"Possible missing firmware \"${_fw}\" for kernel module" \
1250 "\"${_modname}.ko\""
1257 # Do something with all the dependencies of a kernel module.
1258 # Note that kernel modules depend on themselves using the technique we use
1259 # $1 = function to call for each dependency we find
1260 # It will be passed the full path to the found kernel module
1261 # $2 = module to get dependencies for
1262 # rest of args = arguments to modprobe
1263 # _fderr specifies FD passed from surrounding scope
1264 for_each_kmod_dep
() {
1265 local _func
=$1 _kmod
=$2 _cmd _modpath _options
1267 modprobe
"$@" --ignore-install --show-depends $_kmod 2>&${_fderr} |
(
1268 while read _cmd _modpath _options
; do
1269 [[ $_cmd = insmod
]] ||
continue
1270 $_func ${_modpath} ||
exit $?
1275 dracut_kernel_post
() {
1276 local _moddirname
=${srcmods%%/lib/modules/*}
1279 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" ]]; then
1280 xargs -r modprobe
-a ${_moddirname+-d ${_moddirname}/} \
1281 --ignore-install --show-depends --set-version $kernel \
1282 < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" 2>/dev
/null \
1284 |
while read _cmd _modpath _options
; do
1285 [[ $_cmd = insmod
]] ||
continue
1287 done > "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1290 if [[ $DRACUT_INSTALL ]] && [[ -z $_moddirname ]]; then
1291 xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1293 while read _modpath
; do
1294 local _destpath
=$_modpath
1295 [[ $_moddirname ]] && _destpath
=${_destpath##$_moddirname/}
1296 _destpath
=${_destpath##*/lib/modules/$kernel/}
1297 inst_simple
"$_modpath" "/lib/modules/$kernel/${_destpath}" ||
exit $?
1298 done < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1301 _pid
=$
(jobs -p |
while read a
; do printf ":$a";done)
1304 if [[ $DRACUT_INSTALL ]]; then
1305 xargs -r modinfo
-k $kernel -F firmware
< "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" \
1306 |
while read line
; do
1307 for _fwdir
in $fw_dir; do
1310 done |
xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a -o
1312 for _fw
in $
(xargs -r modinfo
-k $kernel -F firmware
< "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"); do
1313 for _fwdir
in $fw_dir; do
1314 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1315 inst_simple
"$_fwdir/$_fw" "/lib/firmware/$_fw"
1325 for _f
in modules.
builtin.bin modules.
builtin; do
1326 [[ $srcmods/$_f ]] && break
1328 dfatal
"No modules.builtin.bin and modules.builtin found!"
1332 for _f
in modules.
builtin.bin modules.
builtin modules.order
; do
1333 [[ $srcmods/$_f ]] && inst_simple
"$srcmods/$_f" "/lib/modules/$kernel/$_f"
1336 # generate module dependencies for the initrd
1337 if [[ -d $initdir/lib
/modules
/$kernel ]] && \
1338 ! depmod
-a -b "$initdir" $kernel; then
1339 dfatal
"\"depmod -a $kernel\" failed."
1343 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && rm -fr -- "$DRACUT_KERNEL_LAZY_HASHDIR"
1346 module_is_host_only
() {
1351 [[ " $add_drivers " == *\
${_mod}\
* ]] && return 0
1353 # check if module is loaded
1354 for i
in /sys
/module
/${_mod//-/_}; do
1355 [[ -d $i ]] && return 0
1358 # check if module is loadable on the current kernel
1359 # this covers the case, where a new module is introduced
1360 # or a module was renamed
1361 # or a module changed from builtin to a module
1362 modinfo
-F filename
"$_mod" &>/dev
/null ||
return 0
1367 find_kernel_modules_by_path
() {
1370 [[ -f "$srcmods/modules.dep" ]] ||
return 0
1374 while read a rest
; do
1375 [[ $a = */$1/* ]] ||
continue
1376 printf "%s\n" "$srcmods/$a"
1377 done < "$srcmods/modules.dep"
1382 find_kernel_modules
() {
1383 find_kernel_modules_by_path drivers
1386 # instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1387 # instmods [-c [-s]] <kernel subsystem>
1388 # install kernel modules along with all their dependencies.
1389 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1391 [[ $no_kernel = yes ]] && return
1392 # called [sub]functions inherit _fderr
1396 if [[ $1 = '-c' ]]; then
1401 if [[ $1 = '-s' ]]; then
1406 function inst1mod
() {
1407 local _ret
=0 _mod
="$1"
1410 ( [[ "$_mpargs" ]] && echo $_mpargs
1411 find_kernel_modules_by_path
"${_mod#=}" ) \
1415 --*) _mpargs
+=" $_mod" ;;
1418 # if we are already installed, skip this module and go on
1420 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1421 [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko" ]]; then
1422 read _ret
<"$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko"
1427 if [[ $omit_drivers ]] && [[ "$_mod" =~
$omit_drivers ]]; then
1428 dinfo
"Omitting driver ${_mod##$srcmods}"
1432 # If we are building a host-specific initramfs and this
1433 # module is not already loaded, move on to the next one.
1435 && ! module_is_host_only
"$_mod" \
1438 if [[ "$_check" = "yes" ]] ||
! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
1439 # We use '-d' option in modprobe only if modules prefix path
1440 # differs from default '/'. This allows us to use dracut with
1441 # old version of modprobe which doesn't have '-d' option.
1442 local _moddirname
=${srcmods%%/lib/modules/*}
1443 [[ -n ${_moddirname} ]] && _moddirname
="-d ${_moddirname}/"
1445 # ok, load the module, all its dependencies, and any firmware
1447 for_each_kmod_dep install_kmod_with_fw
$_mod \
1448 --set-version $kernel ${_moddirname} $_mpargs
1451 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1452 echo $_mod >> "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist"
1459 function instmods_1
() {
1461 if (($# == 0)); then # filenames from stdin
1463 inst1mod
"${_mod%.ko*}" ||
{
1464 if [[ "$_check" == "yes" ]]; then
1465 [[ "$_silent" == "no" ]] && dfatal
"Failed to install module $_mod"
1471 while (($# > 0)); do # filenames as arguments
1472 inst1mod
${1%.ko*} ||
{
1473 if [[ "$_check" == "yes" ]]; then
1474 [[ "$_silent" == "no" ]] && dfatal
"Failed to install module $1"
1483 local _ret _filter_not_found
='FATAL: Module .* not found.'
1484 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1485 # redirections, but that would make dracut require bash4 at least.
1486 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1487 |
while read line
; do [[ "$line" =~
$_filter_not_found ]] ||
echo $line;done | derror
1492 # Only two values are returned: AMD or Intel
1495 if grep -qE AMD
/proc
/cpuinfo
; then
1498 if grep -qE Intel
/proc
/cpuinfo
; then
1504 # Get the hosts' ucode file based on the /proc/cpuinfo
1507 local family
=`grep -E "cpu family" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1508 local model
=`grep -E "model" /proc/cpuinfo |grep -v name | head -1 | sed s/.*:\ //`
1509 local stepping
=`grep -E "stepping" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1511 if [[ "$(get_cpu_vendor)" == "AMD" ]]; then
1512 # If family greater or equal than 0x15
1513 if [[ $family -ge 21 ]]; then
1514 printf "microcode_amd_fam15h.bin"
1516 printf "microcode_amd.bin"
1519 if [[ "$(get_cpu_vendor)" == "Intel" ]]; then
1520 # The /proc/cpuinfo are in decimal.
1521 printf "%02x-%02x-%02x" ${family} ${model} ${stepping}