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 the ID_FS_TYPE variable from udev for a device.
266 # $ get_fs_env /dev/sda2
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
444 # find_dev_fsopts <device>
445 # Echo the filesystem options for a given device.
446 # /proc/self/mountinfo is taken as the primary source of information
447 # and /etc/fstab is used as a fallback.
449 # $ find_dev_fsopts /dev/sda2
450 # rw,relatime,discard,data=ordered
452 local _find_dev _opts
454 if ! [[ "$_find_dev" = /dev
* ]]; then
455 [[ -b "/dev/block/$_find_dev" ]] && _find_dev
="/dev/block/$_find_dev"
458 if [[ $use_fstab != yes ]]; then
459 findmnt
-e -v -n -o 'OPTIONS' --source "$_find_dev" 2>/dev
/null
&& return 0
462 findmnt
--fstab -e -v -n -o 'OPTIONS' --source "$_find_dev"
466 # finds the major:minor of the block device backing the root filesystem.
467 find_root_block_device
() { find_block_device
/; }
469 # for_each_host_dev_fs <func>
470 # Execute "<func> <dev> <filesystem>" for every "<dev> <fs>" pair found
471 # in ${host_fs_types[@]}
472 for_each_host_dev_fs
()
478 [[ "${!host_fs_types[@]}" ]] ||
return 0
480 for _dev
in "${!host_fs_types[@]}"; do
481 $_func "$_dev" "${host_fs_types[$_dev]}" && _ret
=0
488 echo "${host_fs_types[@]}"
491 # Walk all the slave relationships for a given block device.
492 # Stop when our helper function returns success
493 # $1 = function to call on every found block device
494 # $2 = block device in major:minor format
495 check_block_and_slaves
() {
497 [[ -b /dev
/block
/$2 ]] ||
return 1 # Not a block device? So sorry.
499 check_vol_slaves
"$@" && return 0
500 if [[ -f /sys
/dev
/block
/$2/..
/dev
]]; then
501 check_block_and_slaves
$1 $
(<"/sys/dev/block/$2/../dev") && return 0
503 [[ -d /sys
/dev
/block
/$2/slaves
]] ||
return 1
504 for _x
in /sys
/dev
/block
/$2/slaves
/*/dev
; do
505 [[ -f $_x ]] ||
continue
506 check_block_and_slaves
$1 $
(<"$_x") && return 0
511 check_block_and_slaves_all
() {
513 [[ -b /dev
/block
/$2 ]] ||
return 1 # Not a block device? So sorry.
517 check_vol_slaves
"$@" && return 0
518 if [[ -f /sys
/dev
/block
/$2/..
/dev
]]; then
519 check_block_and_slaves_all
$1 $
(<"/sys/dev/block/$2/../dev") && _ret
=0
521 [[ -d /sys
/dev
/block
/$2/slaves
]] ||
return 1
522 for _x
in /sys
/dev
/block
/$2/slaves
/*/dev
; do
523 [[ -f $_x ]] ||
continue
524 check_block_and_slaves_all
$1 $
(<"$_x") && _ret
=0
528 # for_each_host_dev_and_slaves <func>
529 # Execute "<func> <dev>" for every "<dev>" found
530 # in ${host_devs[@]} and their slaves
531 for_each_host_dev_and_slaves_all
()
537 [[ "${host_devs[@]}" ]] ||
return 0
539 for _dev
in ${host_devs[@]}; do
540 [[ -b "$_dev" ]] ||
continue
541 if check_block_and_slaves_all
$_func $
(get_maj_min
$_dev); then
548 for_each_host_dev_and_slaves
()
553 [[ "${host_devs[@]}" ]] ||
return 0
555 for _dev
in ${host_devs[@]}; do
556 [[ -b "$_dev" ]] ||
continue
557 check_block_and_slaves
$_func $
(get_maj_min
$_dev) && return 0
562 # ugly workaround for the lvm design
563 # There is no volume group device,
564 # so, there are no slave devices for volume groups.
565 # Logical volumes only have the slave devices they really live on,
566 # but you cannot create the logical volume without the volume group.
567 # And the volume group might be bigger than the devices the LV needs.
570 for i
in /dev
/mapper
/*; do
571 _lv
=$
(get_maj_min
$i)
572 if [[ $_lv = $2 ]]; then
573 _vg
=$
(lvm lvs
--noheadings -o vg_name
$i 2>/dev
/null
)
577 for _pv
in $
(lvm vgs
--noheadings -o pv_name
"$_vg" 2>/dev
/null
)
579 check_block_and_slaves
$1 $
(get_maj_min
$_pv) && return 0
587 # fs_get_option <filesystem options> <search for option>
588 # search for a specific option in a bunch of filesystem options
589 # and return the value
597 while [ $# -gt 0 ]; do
600 echo ${1#${_option}=}
608 if ! [[ $DRACUT_INSTALL ]]; then
609 DRACUT_INSTALL
=$
(find_binary dracut-install
)
612 if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install
]]; then
613 DRACUT_INSTALL
=$dracutbasedir/dracut-install
616 if ! [[ -x $DRACUT_INSTALL ]]; then
617 dfatal
"dracut-install not found!"
621 [[ $DRACUT_RESOLVE_LAZY ]] ||
export DRACUT_RESOLVE_DEPS
=1
623 [[ -e ${initdir}/"$1" ]] && return 0 # already there
624 $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@"
625 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@" ||
:
629 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
630 #dinfo "$DRACUT_INSTALL -l $@"
631 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
632 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" ||
:
636 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
637 [[ -e $1 ]] ||
return 1 # no source
638 $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@"
639 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} "$@" ||
:
643 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
644 [[ -L $1 ]] ||
return 1
645 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
646 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" ||
:
651 #dinfo "initdir=$initdir $DRACUT_INSTALL -l $@"
652 $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
654 (($ret != 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" ||
:
663 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
664 [[ -e $1 ]] ||
return 1 # no source
665 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
666 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" ||
:
670 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
671 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" ||
:
675 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
676 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" ||
:
679 # find symlinks linked to given library file
681 # Function searches for symlinks by stripping version numbers appended to
682 # library filename, checks if it points to the same target and finally
683 # prints the list of symlinks to stdout.
686 # rev_lib_symlinks libfoo.so.8.1
687 # output: libfoo.so.8 libfoo.so
688 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
690 [[ ! $1 ]] && return 0
692 local fn
="$1" orig
="$(readlink -f "$1")" links
=''
694 [[ ${fn} == *.so.
* ]] ||
return 1
696 until [[ ${fn##*.} == so
]]; do
698 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
704 # attempt to install any programs specified in a udev rule
705 inst_rule_programs() {
708 if grep -qE 'PROGRAM==?"[^
"]+' "$1"; then
709 for _prog in $(grep -E 'PROGRAM==?"[^
"]+' "$1" | sed -r 's/.*PROGRAM==?"([^
"]+).*/\1/'); do
711 if [ -x ${udevdir}/$_prog ]; then
712 _bin=${udevdir}/$_prog
713 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
714 _bin=$(find_binary "$_prog") || {
715 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
720 [[ $_bin ]] && inst_binary "$_bin"
723 if grep -qE 'RUN[+=]=?"[^
"]+' "$1"; then
724 for _prog in $(grep -E 'RUN[+=]=?"[^
"]+' "$1" | sed -r 's/.*RUN[+=]=?"([^
"]+).*/\1/'); do
726 if [ -x ${udevdir}/$_prog ]; then
727 _bin=${udevdir}/$_prog
728 elif [[ "${_prog/\$env\{/}" == "$_prog" ]] && [[ "${_prog}" != "/sbin
/initqueue
" ]]; then
729 _bin=$(find_binary "$_prog") || {
730 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
735 [[ $_bin ]] && inst_binary "$_bin"
738 if grep -qE 'IMPORT\{program\}==?"[^
"]+' "$1"; then
739 for _prog in $(grep -E 'IMPORT\{program\}==?"[^
"]+' "$1" | sed -r 's/.*IMPORT\{program\}==?"([^
"]+).*/\1/'); do
741 if [ -x ${udevdir}/$_prog ]; then
742 _bin=${udevdir}/$_prog
743 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
744 _bin=$(find_binary "$_prog") || {
745 dinfo "Skipping program
$_prog using
in udev rule
${1##*/} as it cannot be found
"
750 [[ $_bin ]] && dracut_install "$_bin"
755 # attempt to install any programs specified in a udev rule
756 inst_rule_group_owner() {
759 if grep -qE 'OWNER=?"[^
"]+' "$1"; then
760 for i in $(grep -E 'OWNER=?"[^
"]+' "$1" | sed -r 's/.*OWNER=?"([^
"]+).*/\1/'); do
761 if ! egrep -q "^
$i:" "$initdir/etc
/passwd
" 2>/dev/null; then
762 egrep "^
$i:" /etc/passwd 2>/dev/null >> "$initdir/etc
/passwd
"
766 if grep -qE 'GROUP=?"[^
"]+' "$1"; then
767 for i in $(grep -E 'GROUP=?"[^
"]+' "$1" | sed -r 's/.*GROUP=?"([^
"]+).*/\1/'); do
768 if ! egrep -q "^
$i:" "$initdir/etc
/group
" 2>/dev/null; then
769 egrep "^
$i:" /etc/group 2>/dev/null >> "$initdir/etc
/group
"
775 inst_rule_initqueue() {
776 if grep -q -F initqueue "$1"; then
777 dracut_need_initqueue
781 # udev rules always get installed in the same place, so
782 # create a function to install them to make life simpler.
784 local _target=/etc/udev/rules.d _rule _found
786 inst_dir "${udevdir}/rules.d
"
788 for _rule in "$@
"; do
789 if [ "${_rule#/}" = "$_rule" ]; then
790 for r in ${udevdir}/rules.d /etc/udev/rules.d; do
791 if [[ -f $r/$_rule ]]; then
793 inst_rule_programs "$_found"
794 inst_rule_group_owner "$_found"
795 inst_rule_initqueue "$_found"
796 inst_simple "$_found"
800 for r in '' ./ $dracutbasedir/rules.d/; do
801 if [[ -f ${r}$_rule ]]; then
803 inst_rule_programs "$_found"
804 inst_rule_group_owner "$_found"
805 inst_rule_initqueue "$_found"
806 inst_simple "$_found" "$_target/${_found##*/}"
809 [[ $_found ]] || dinfo "Skipping udev rule
: $_rule"
813 prepare_udev_rules() {
814 [ -z "$UDEVVERSION" ] && export UDEVVERSION=$(udevadm --version)
817 f="${initdir}/etc
/udev
/rules.d
/$f"
818 [ -e "$f" ] || continue
820 if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
821 if [ $UDEVVERSION -ge 174 ]; then
822 printf '%sIMPORT{builtin}="path_id
"\n' "${line%%IMPORT PATH_ID}"
824 printf '%sIMPORT{program}="path_id
%%p
"\n' "${line%%IMPORT PATH_ID}"
826 elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
827 if [ $UDEVVERSION -ge 176 ]; then
828 printf '%sIMPORT{builtin}="blkid
"\n' "${line%%IMPORT BLKID}"
830 printf '%sIMPORT{program}="/sbin
/blkid
-o udev
-p $tempnode"\n' "${line%%IMPORT BLKID}"
835 done < "${f}" > "${f}.new
"
840 # install function specialized for hooks
841 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
842 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
844 if ! [[ -f $3 ]]; then
845 dfatal "Cannot
install a hook
($3) that does not exist.
"
846 dfatal "Aborting initrd creation.
"
848 elif ! [[ "$hookdirs" == *$1* ]]; then
849 dfatal "No such hook
type $1. Aborting initrd creation.
"
852 inst_simple "$3" "/lib
/dracut
/hooks
/${1}/${2}-${3##*/}"
855 # install any of listed files
857 # If first argument is '-d' and second some destination path, first accessible
858 # source is installed into this path, otherwise it will installed in the same
859 # path as source. If none of listed files was installed, function return 1.
860 # On first successful installation it returns with 0 status.
864 # inst_any -d /bin/foo /bin/bar /bin/baz
866 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
871 [[ $1 = '-d' ]] && to="$2" && shift 2
875 [[ $to ]] && inst "$f" "$to" && return 0
876 inst "$f" && return 0
884 # inst_libdir_file [-n <pattern>] <file> [<file>...]
885 # Install a <file> located on a lib directory to the initramfs image
886 # -n <pattern> install matching files
889 if [[ "$1" == "-n" ]]; then
892 for _dir in $libdirs; do
894 for _f in "$_dir"/$_i; do
895 [[ "$_f" =~ $_pattern ]] || continue
896 [[ -e "$_f" ]] && _files+="$_f "
901 for _dir in $libdirs; do
903 for _f in "$_dir"/$_i; do
904 [[ -e "$_f" ]] && _files+="$_f "
909 [[ $_files ]] && inst_multiple $_files
913 # install function decompressing the target and handling symlinks
914 # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
916 # Function install targets in the same paths inside overlay but decompressed
917 # and without extensions (.gz, .bz2).
924 *.gz) _cmd='gzip -f -d' ;;
925 *.bz2) _cmd='bzip2 -d' ;;
929 # Decompress with chosen tool. We assume that tool changes name e.g.
930 # from 'name.gz' to 'name'.
931 ${_cmd} "${initdir}${_src}"
935 # It's similar to above, but if file is not compressed, performs standard
938 inst_opt_decompress
() {
943 inst_decompress
"${_src}" || inst
"${_src}"
947 # module_check <dracut module>
948 # execute the check() function of module-setup.sh of <dracut module>
949 # or the "check" script, if module-setup.sh is not found
950 # "check $hostonly" is called
952 local _moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
955 local _hostonly
=$hostonly
956 [ $# -eq 2 ] && _forced
=$2
957 [[ -d $_moddir ]] ||
return 1
958 if [[ ! -f $_moddir/module-setup.sh
]]; then
959 # if we do not have a check script, we are unconditionally included
960 [[ -x $_moddir/check
]] ||
return 0
961 [ $_forced -ne 0 ] && unset hostonly
962 $_moddir/check
$hostonly
965 unset check depends
install installkernel
967 .
$_moddir/module-setup.sh
968 is_func check ||
return 0
969 [ $_forced -ne 0 ] && unset hostonly
972 unset check depends
install installkernel
978 # module_check_mount <dracut module>
979 # execute the check() function of module-setup.sh of <dracut module>
980 # or the "check" script, if module-setup.sh is not found
981 # "mount_needs=1 check 0" is called
982 module_check_mount
() {
983 local _moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
986 [[ -d $_moddir ]] ||
return 1
987 if [[ ! -f $_moddir/module-setup.sh
]]; then
988 # if we do not have a check script, we are unconditionally included
989 [[ -x $_moddir/check
]] ||
return 0
990 mount_needs
=1 $_moddir/check
0
993 unset check depends
install installkernel
995 .
$_moddir/module-setup.sh
998 unset check depends
install installkernel
1004 # module_depends <dracut module>
1005 # execute the depends() function of module-setup.sh of <dracut module>
1006 # or the "depends" script, if module-setup.sh is not found
1008 local _moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
1010 [[ -d $_moddir ]] ||
return 1
1011 if [[ ! -f $_moddir/module-setup.sh
]]; then
1012 # if we do not have a check script, we have no deps
1013 [[ -x $_moddir/check
]] ||
return 0
1017 unset check depends
install installkernel
1019 .
$_moddir/module-setup.sh
1022 unset check depends
install installkernel
1027 # module_install <dracut module>
1028 # execute the install() function of module-setup.sh of <dracut module>
1029 # or the "install" script, if module-setup.sh is not found
1031 local _moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
1033 [[ -d $_moddir ]] ||
return 1
1034 if [[ ! -f $_moddir/module-setup.sh
]]; then
1035 [[ -x $_moddir/install ]] && .
"$_moddir/install"
1038 unset check depends
install installkernel
1040 .
$_moddir/module-setup.sh
1043 unset check depends
install installkernel
1048 # module_installkernel <dracut module>
1049 # execute the installkernel() function of module-setup.sh of <dracut module>
1050 # or the "installkernel" script, if module-setup.sh is not found
1051 module_installkernel
() {
1052 local _moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
1054 [[ -d $_moddir ]] ||
return 1
1055 if [[ ! -f $_moddir/module-setup.sh
]]; then
1056 [[ -x $_moddir/installkernel
]] && .
"$_moddir/installkernel"
1059 unset check depends
install installkernel
1060 installkernel
() { true
; }
1061 .
$_moddir/module-setup.sh
1064 unset check depends
install installkernel
1069 # check_mount <dracut module>
1070 # check_mount checks, if a dracut module is needed for the given
1071 # device and filesystem types in "${host_fs_types[@]}"
1074 local _moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
1078 [ "${#host_fs_types[*]}" -le 0 ] && return 1
1080 # If we are already scheduled to be loaded, no need to check again.
1081 [[ " $mods_to_load " == *\
$_mod\
* ]] && return 0
1082 [[ " $mods_checked_as_dep " == *\
$_mod\
* ]] && return 1
1084 # This should never happen, but...
1085 [[ -d $_moddir ]] ||
return 1
1087 [[ $2 ]] || mods_checked_as_dep
+=" $_mod "
1089 if [[ " $omit_dracutmodules " == *\
$_mod\
* ]]; then
1093 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\
$_mod\
* ]]; then
1094 module_check_mount
$_mod; ret
=$?
1096 # explicit module, so also accept ret=255
1097 [[ $ret = 0 ||
$ret = 255 ]] ||
return 1
1099 # module not in our list
1100 if [[ $dracutmodules = all
]]; then
1101 # check, if we can and should install this module
1102 module_check_mount
$_mod ||
return 1
1110 for _moddep
in $
(module_depends
$_mod); do
1111 # handle deps as if they were manually added
1112 [[ " $add_dracutmodules " == *\
$_moddep\
* ]] || \
1113 add_dracutmodules
+=" $_moddep "
1114 [[ " $force_add_dracutmodules " == *\
$_moddep\
* ]] || \
1115 force_add_dracutmodules
+=" $_moddep "
1116 # if a module we depend on fail, fail also
1117 if ! check_module
$_moddep; then
1118 derror
"dracut module '$_mod' depends on '$_moddep', which can't be installed"
1123 [[ " $mods_to_load " == *\
$_mod\
* ]] || \
1124 mods_to_load
+=" $_mod "
1129 # check_module <dracut module> [<use_as_dep>]
1130 # check if a dracut module is to be used in the initramfs process
1131 # if <use_as_dep> is set, then the process also keeps track
1132 # that the modules were checked for the dependency tracking process
1135 local _moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
1138 # If we are already scheduled to be loaded, no need to check again.
1139 [[ " $mods_to_load " == *\
$_mod\
* ]] && return 0
1140 [[ " $mods_checked_as_dep " == *\
$_mod\
* ]] && return 1
1142 # This should never happen, but...
1143 [[ -d $_moddir ]] ||
return 1
1145 [[ $2 ]] || mods_checked_as_dep
+=" $_mod "
1147 if [[ " $omit_dracutmodules " == *\
$_mod\
* ]]; then
1148 dinfo
"dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
1152 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\
$_mod\
* ]]; then
1153 if [[ " $force_add_dracutmodules " == *\
$_mod\
* ]]; then
1154 module_check
$_mod 1; ret
=$?
1156 module_check
$_mod 0; ret
=$?
1158 # explicit module, so also accept ret=255
1159 [[ $ret = 0 ||
$ret = 255 ]] ||
return 1
1161 # module not in our list
1162 if [[ $dracutmodules = all
]]; then
1163 # check, if we can and should install this module
1164 module_check
$_mod ||
return 1
1171 for _moddep
in $
(module_depends
$_mod); do
1172 # handle deps as if they were manually added
1173 [[ " $add_dracutmodules " == *\
$_moddep\
* ]] || \
1174 add_dracutmodules
+=" $_moddep "
1175 [[ " $force_add_dracutmodules " == *\
$_moddep\
* ]] || \
1176 force_add_dracutmodules
+=" $_moddep "
1177 # if a module we depend on fail, fail also
1178 if ! check_module
$_moddep; then
1179 derror
"dracut module '$_mod' depends on '$_moddep', which can't be installed"
1184 [[ " $mods_to_load " == *\
$_mod\
* ]] || \
1185 mods_to_load
+=" $_mod "
1190 # for_each_module_dir <func>
1191 # execute "<func> <dracut module> 1"
1192 for_each_module_dir
() {
1198 for _moddir
in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1199 _mod
=${_moddir##*/}; _mod
=${_mod#[0-9][0-9]}
1203 # Report any missing dracut modules, the user has specified
1204 _modcheck
="$add_dracutmodules $force_add_dracutmodules"
1205 [[ $dracutmodules != all
]] && _modcheck
="$m $dracutmodules"
1206 for _mod
in $_modcheck; do
1207 [[ " $mods_to_load " == *\
$_mod\
* ]] && continue
1208 [[ " $omit_dracutmodules " == *\
$_mod\
* ]] && continue
1209 derror
"dracut module '$_mod' cannot be found or installed."
1213 # Install a single kernel module along with any firmware it may require.
1214 # $1 = full path to kernel module to install
1215 install_kmod_with_fw
() {
1216 # no need to go further if the module is already installed
1218 [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
1221 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -e "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}" ]]; then
1222 read ret
< "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
1226 if [[ $omit_drivers ]]; then
1227 local _kmod
=${1##*/}
1230 if [[ "$_kmod" =~
$omit_drivers ]]; then
1231 dinfo
"Omitting driver $_kmod"
1234 if [[ "${1##*/lib/modules/$kernel/}" =~
$omit_drivers ]]; then
1235 dinfo
"Omitting driver $_kmod"
1240 if [[ $silent_omit_drivers ]]; then
1241 local _kmod
=${1##*/}
1244 [[ "$_kmod" =~
$silent_omit_drivers ]] && return 0
1245 [[ "${1##*/lib/modules/$kernel/}" =~
$silent_omit_drivers ]] && return 0
1248 inst_simple
"$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
1250 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1251 [[ -d "$DRACUT_KERNEL_LAZY_HASHDIR" ]] && \
1252 echo $ret > "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
1253 (($ret != 0)) && return $ret
1255 local _modname
=${1##*/} _fwdir _found _fw
1256 _modname
=${_modname%.ko*}
1257 for _fw
in $
(modinfo
-k $kernel -F firmware
$1 2>/dev
/null
); do
1259 for _fwdir
in $fw_dir; do
1260 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1261 inst_simple
"$_fwdir/$_fw" "/lib/firmware/$_fw"
1265 if [[ $_found != yes ]]; then
1266 if ! [[ -d $
(echo /sys
/module
/${_modname//-/_}|
{ read a b
; echo $a; }) ]]; then
1267 dinfo
"Possible missing firmware \"${_fw}\" for kernel module" \
1268 "\"${_modname}.ko\""
1270 dwarn
"Possible missing firmware \"${_fw}\" for kernel module" \
1271 "\"${_modname}.ko\""
1278 # Do something with all the dependencies of a kernel module.
1279 # Note that kernel modules depend on themselves using the technique we use
1280 # $1 = function to call for each dependency we find
1281 # It will be passed the full path to the found kernel module
1282 # $2 = module to get dependencies for
1283 # rest of args = arguments to modprobe
1284 # _fderr specifies FD passed from surrounding scope
1285 for_each_kmod_dep
() {
1286 local _func
=$1 _kmod
=$2 _cmd _modpath _options
1288 modprobe
"$@" --ignore-install --show-depends $_kmod 2>&${_fderr} |
(
1289 while read _cmd _modpath _options
; do
1290 [[ $_cmd = insmod
]] ||
continue
1291 $_func ${_modpath} ||
exit $?
1296 dracut_kernel_post
() {
1297 local _moddirname
=${srcmods%%/lib/modules/*}
1300 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" ]]; then
1301 xargs -r modprobe
-a ${_moddirname+-d ${_moddirname}/} \
1302 --ignore-install --show-depends --set-version $kernel \
1303 < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" 2>/dev
/null \
1305 |
while read _cmd _modpath _options
; do
1306 [[ $_cmd = insmod
]] ||
continue
1308 done > "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1311 if [[ $DRACUT_INSTALL ]] && [[ -z $_moddirname ]]; then
1312 xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1314 while read _modpath
; do
1315 local _destpath
=$_modpath
1316 [[ $_moddirname ]] && _destpath
=${_destpath##$_moddirname/}
1317 _destpath
=${_destpath##*/lib/modules/$kernel/}
1318 inst_simple
"$_modpath" "/lib/modules/$kernel/${_destpath}" ||
exit $?
1319 done < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1322 _pid
=$
(jobs -p |
while read a
; do printf ":$a";done)
1325 if [[ $DRACUT_INSTALL ]]; then
1326 xargs -r modinfo
-k $kernel -F firmware
< "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" \
1327 |
while read line
; do
1328 for _fwdir
in $fw_dir; do
1331 done |
xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a -o
1333 for _fw
in $
(xargs -r modinfo
-k $kernel -F firmware
< "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"); do
1334 for _fwdir
in $fw_dir; do
1335 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1336 inst_simple
"$_fwdir/$_fw" "/lib/firmware/$_fw"
1346 for _f
in modules.
builtin.bin modules.
builtin; do
1347 [[ $srcmods/$_f ]] && break
1349 dfatal
"No modules.builtin.bin and modules.builtin found!"
1353 for _f
in modules.
builtin.bin modules.
builtin modules.order
; do
1354 [[ $srcmods/$_f ]] && inst_simple
"$srcmods/$_f" "/lib/modules/$kernel/$_f"
1357 # generate module dependencies for the initrd
1358 if [[ -d $initdir/lib
/modules
/$kernel ]] && \
1359 ! depmod
-a -b "$initdir" $kernel; then
1360 dfatal
"\"depmod -a $kernel\" failed."
1364 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && rm -fr -- "$DRACUT_KERNEL_LAZY_HASHDIR"
1367 module_is_host_only
() {
1372 [[ " $add_drivers " == *\
${_mod}\
* ]] && return 0
1374 # check if module is loaded
1375 for i
in /sys
/module
/${_mod//-/_}; do
1376 [[ -d $i ]] && return 0
1379 # check if module is loadable on the current kernel
1380 # this covers the case, where a new module is introduced
1381 # or a module was renamed
1382 # or a module changed from builtin to a module
1383 modinfo
-F filename
"$_mod" &>/dev
/null ||
return 0
1388 find_kernel_modules_by_path
() {
1391 [[ -f "$srcmods/modules.dep" ]] ||
return 0
1395 while read a rest
; do
1396 [[ $a = */$1/* ]] ||
continue
1397 printf "%s\n" "$srcmods/$a"
1398 done < "$srcmods/modules.dep"
1403 find_kernel_modules
() {
1404 find_kernel_modules_by_path drivers
1407 # instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1408 # instmods [-c [-s]] <kernel subsystem>
1409 # install kernel modules along with all their dependencies.
1410 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1412 [[ $no_kernel = yes ]] && return
1413 # called [sub]functions inherit _fderr
1417 if [[ $1 = '-c' ]]; then
1422 if [[ $1 = '-s' ]]; then
1427 function inst1mod
() {
1428 local _ret
=0 _mod
="$1"
1431 ( [[ "$_mpargs" ]] && echo $_mpargs
1432 find_kernel_modules_by_path
"${_mod#=}" ) \
1436 --*) _mpargs
+=" $_mod" ;;
1439 # if we are already installed, skip this module and go on
1441 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1442 [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko" ]]; then
1443 read _ret
<"$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko"
1448 if [[ $omit_drivers ]] && [[ "$_mod" =~
$omit_drivers ]]; then
1449 dinfo
"Omitting driver ${_mod##$srcmods}"
1453 # If we are building a host-specific initramfs and this
1454 # module is not already loaded, move on to the next one.
1456 && ! module_is_host_only
"$_mod" \
1459 if [[ "$_check" = "yes" ]] ||
! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
1460 # We use '-d' option in modprobe only if modules prefix path
1461 # differs from default '/'. This allows us to use dracut with
1462 # old version of modprobe which doesn't have '-d' option.
1463 local _moddirname
=${srcmods%%/lib/modules/*}
1464 [[ -n ${_moddirname} ]] && _moddirname
="-d ${_moddirname}/"
1466 # ok, load the module, all its dependencies, and any firmware
1468 for_each_kmod_dep install_kmod_with_fw
$_mod \
1469 --set-version $kernel ${_moddirname} $_mpargs
1472 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1473 echo $_mod >> "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist"
1480 function instmods_1
() {
1482 if (($# == 0)); then # filenames from stdin
1484 inst1mod
"${_mod%.ko*}" ||
{
1485 if [[ "$_check" == "yes" ]]; then
1486 [[ "$_silent" == "no" ]] && dfatal
"Failed to install module $_mod"
1492 while (($# > 0)); do # filenames as arguments
1493 inst1mod
${1%.ko*} ||
{
1494 if [[ "$_check" == "yes" ]]; then
1495 [[ "$_silent" == "no" ]] && dfatal
"Failed to install module $1"
1504 local _ret _filter_not_found
='FATAL: Module .* not found.'
1505 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1506 # redirections, but that would make dracut require bash4 at least.
1507 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1508 |
while read line
; do [[ "$line" =~
$_filter_not_found ]] ||
echo $line;done | derror
1513 # Only two values are returned: AMD or Intel
1516 if grep -qE AMD
/proc
/cpuinfo
; then
1519 if grep -qE Intel
/proc
/cpuinfo
; then
1525 # Get the hosts' ucode file based on the /proc/cpuinfo
1528 local family
=`grep -E "cpu family" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1529 local model
=`grep -E "model" /proc/cpuinfo |grep -v name | head -1 | sed s/.*:\ //`
1530 local stepping
=`grep -E "stepping" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1532 if [[ "$(get_cpu_vendor)" == "AMD" ]]; then
1533 # If family greater or equal than 0x15
1534 if [[ $family -ge 21 ]]; then
1535 printf "microcode_amd_fam15h.bin"
1537 printf "microcode_amd.bin"
1540 if [[ "$(get_cpu_vendor)" == "Intel" ]]; then
1541 # The /proc/cpuinfo are in decimal.
1542 printf "%02x-%02x-%02x" ${family} ${model} ${stepping}