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 ]] ||
{ strstr
"$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)"
58 if ! [[ $DRACUT_INSTALL ]]; then
59 DRACUT_INSTALL
=$
(find_binary dracut-install
)
62 if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install
]]; then
63 DRACUT_INSTALL
=$dracutbasedir/dracut-install
67 if ! [[ $libdirs ]] ; then
68 if strstr
"$(ldd /bin/sh)" "/lib64/" &>/dev
/null \
69 && [[ -d /lib64
]]; then
71 [[ -d /usr
/lib64
]] && libdirs
+=" /usr/lib64"
74 [[ -d /usr
/lib
]] && libdirs
+=" /usr/lib"
79 if ! [[ $kernel ]]; then
84 srcmods
="/lib/modules/$kernel/"
85 [[ $drivers_dir ]] && {
86 if vercmp $
(modprobe
--version | cut
-d' ' -f3) lt
3.7; then
87 dfatal
'To use --kmoddir option module-init-tools >= 3.7 is required.'
90 srcmods
="$drivers_dir"
94 if ! type dinfo
>/dev
/null
2>&1; then
95 .
"$dracutbasedir/dracut-logger.sh"
99 # export standard hookdirs
101 hookdirs
="cmdline pre-udev pre-trigger netroot "
102 hookdirs
+="initqueue initqueue/settled initqueue/online initqueue/finished initqueue/timeout "
103 hookdirs
+="pre-mount pre-pivot cleanup mount "
104 hookdirs
+="emergency shutdown-emergency shutdown "
108 # Create all subdirectories for given path without creating the last element.
110 mksubdirs
() { [[ -e ${1%/*} ]] || mkdir -m 0755 -p ${1%/*}; }
112 # Version comparision function. Assumes Linux style version scheme.
114 # $2 = comparision op (gt, ge, eq, le, lt, ne)
117 local _n1
=(${1//./ }) _op
=$2 _n2
=(${3//./ }) _i _res
121 if [[ ! ${_n1[_i]}${_n2[_i]} ]]; then _res
=0
122 elif ((${_n1[_i]:-0} > ${_n2[_i]:-0})); then _res
=1
123 elif ((${_n1[_i]:-0} < ${_n2[_i]:-0})); then _res
=2
140 # Check whether $1 is a function.
142 [[ $
(type -t $1) = "function" ]]
145 # Function prints global variables in format name=value line by line.
146 # $@ = list of global variables' name
152 _value
=$
(eval echo \$
$_var)
153 [[ ${_value} ]] && echo "${_var}=\"${_value}\""
157 # normalize_path <path>
158 # Prints the normalized path, where it removes any duplicated
159 # and trailing slashes.
161 # $ normalize_path ///test/test//
165 set -- "${1//+(\/)//}"
170 # convert_abs_rel <from> <to>
171 # Prints the relative path, when creating a symlink to <to> from <from>.
173 # $ convert_abs_rel /usr/bin/test /bin/test-2
175 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
177 local __current __absolute __abssize __cursize __newpath
180 set -- "$
(normalize_path
"$1")" "$
(normalize_path
"$2")"
182 # corner case #1 - self looping link
183 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
185 # corner case #2 - own dir link
186 [[ "${1%/*}" == "$2" ]] && { echo ".
"; return; }
188 IFS="/" __current=($1)
189 IFS="/" __absolute=($2)
191 __abssize=${#__absolute[@]}
192 __cursize=${#__current[@]}
194 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
197 if (( __level > __abssize || __level > __cursize ))
203 for ((__i = __level; __i < __cursize-1; __i++))
207 __newpath=$__newpath"/"
209 __newpath=$__newpath"..
"
212 for ((__i = __level; __i < __abssize; __i++))
214 if [[ -n $__newpath ]]
216 __newpath=$__newpath"/"
218 __newpath=$__newpath${__absolute[__i]}
224 if strstr "$
(ln --help)" "--relative"; then
226 ln -sfnr "${initdir}/$1" "${initdir}/$2"
232 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
233 ln -sfn $
(convert_abs_rel
"${_dest}" "${_source}") "${initdir}/${_dest}"
237 # get_fs_env <device>
238 # Get and set the ID_FS_TYPE and ID_FS_UUID variable from udev for a device.
240 # $ get_fs_env /dev/sda2; echo $ID_FS_TYPE; echo $ID_FS_UUID
242 # 551a39aa-4ae9-4e70-a262-ef665cadb574
250 if evalstr
=$
(udevadm info
--query=env
--name=$1 \
251 |
{ while read line
; do
252 strstr
"$line" "DEVPATH" && found
=1;
253 strstr
"$line" "ID_FS_TYPE=" && { echo $line; exit 0;}
254 done; [[ $found ]] && exit 0; exit 1; }) ; then
256 [[ $ID_FS_TYPE ]] && return 0
260 # Fallback, if we don't have udev information
261 if find_binary blkid
>/dev
/null
; then
262 eval $
(blkid
-o udev
$1 \
263 |
while read line
; do
264 strstr
"$line" "ID_FS_TYPE=" && echo $line;
266 [[ $ID_FS_TYPE ]] && return 0
271 # get_maj_min <device>
272 # Prints the major and minor of a device node.
274 # $ get_maj_min /dev/sda2
278 _dev
=$
(stat
-L -c '$((0x%t)):$((0x%T))' "$1" 2>/dev
/null
)
279 _dev
=$
(eval "echo $_dev")
283 # find_block_device <mountpoint>
284 # Prints the major and minor number of the block device
285 # for a given mountpoint.
286 # Unless $use_fstab is set to "yes" the functions
287 # uses /proc/self/mountinfo as the primary source of the
288 # information and only falls back to /etc/fstab, if the mountpoint
289 # is not found there.
291 # $ find_block_device /usr
293 find_block_device
() {
294 local _x _mpt _majmin _dev _fs _maj _min
295 if [[ $use_fstab != yes ]]; then
296 while read _x _x _majmin _x _mpt _x _x _fs _dev _x
; do
297 [[ $_mpt = $1 ]] ||
continue
298 [[ $_fs = nfs
]] && { echo $_dev; return 0;}
299 [[ $_fs = nfs3
]] && { echo $_dev; return 0;}
300 [[ $_fs = nfs4
]] && { echo $_dev; return 0;}
301 [[ $_fs = btrfs
]] && {
305 if [[ ${_majmin#0:} = $_majmin ]]; then
307 return 0 # we have a winner!
309 done < /proc
/self
/mountinfo
311 # fall back to /etc/fstab
312 while read _dev _mpt _fs _x
; do
313 [ "${_dev%%#*}" != "$_dev" ] && continue
315 if [[ $_mpt = $1 ]]; then
316 [[ $_fs = nfs
]] && { echo $_dev; return 0;}
317 [[ $_fs = nfs3
]] && { echo $_dev; return 0;}
318 [[ $_fs = nfs4
]] && { echo $_dev; return 0;}
319 [[ $_dev != ${_dev#UUID=} ]] && _dev
=/dev
/disk
/by-uuid
/${_dev#UUID=}
320 [[ $_dev != ${_dev#LABEL=} ]] && _dev
=/dev
/disk
/by-label
/${_dev#LABEL=}
321 [[ -b $_dev ]] ||
return 1 # oops, not a block device.
322 get_maj_min
"$_dev" && return 0
329 # find_dev_fstype <device>
330 # Echo the filesystem type for a given device.
331 # /proc/self/mountinfo is taken as the primary source of information
332 # and /etc/fstab is used as a fallback.
333 # No newline is appended!
335 # $ find_dev_fstype /dev/sda2;echo
338 local _x _mpt _majmin _dev _fs _maj _min
339 while read _x _x _majmin _x _mpt _x _x _fs _dev _x
; do
340 [[ $_dev = $1 ]] ||
continue
343 done < /proc
/self
/mountinfo
345 # fall back to /etc/fstab
346 while read _dev _mpt _fs _x
; do
347 [[ $_dev = $1 ]] ||
continue
355 # finds the major:minor of the block device backing the root filesystem.
356 find_root_block_device
() { find_block_device
/; }
358 # for_each_host_dev_fs <func>
359 # Execute "<func> <dev> <filesystem>" for every "<dev>|<fs>" pair found
360 # in ${host_fs_types[@]}
361 for_each_host_dev_fs
()
367 for f
in ${host_fs_types[@]}; do
373 [[ -b "$_dev" ]] ||
continue
375 $_func $_dev $_fs && _ret
=0
380 # Walk all the slave relationships for a given block device.
381 # Stop when our helper function returns success
382 # $1 = function to call on every found block device
383 # $2 = block device in major:minor format
384 check_block_and_slaves
() {
386 [[ -b /dev
/block
/$2 ]] ||
return 1 # Not a block device? So sorry.
388 check_vol_slaves
"$@" && return 0
389 if [[ -f /sys
/dev
/block
/$2/..
/dev
]]; then
390 check_block_and_slaves
$1 $
(cat "/sys/dev/block/$2/../dev") && return 0
392 [[ -d /sys
/dev
/block
/$2/slaves
]] ||
return 1
393 for _x
in /sys
/dev
/block
/$2/slaves
/*/dev
; do
394 [[ -f $_x ]] ||
continue
395 check_block_and_slaves
$1 $
(cat "$_x") && return 0
400 # ugly workaround for the lvm design
401 # There is no volume group device,
402 # so, there are no slave devices for volume groups.
403 # Logical volumes only have the slave devices they really live on,
404 # but you cannot create the logical volume without the volume group.
405 # And the volume group might be bigger than the devices the LV needs.
408 for i
in /dev
/mapper
/*; do
409 _lv
=$
(get_maj_min
$i)
410 if [[ $_lv = $2 ]]; then
411 _vg
=$
(lvm lvs
--noheadings -o vg_name
$i 2>/dev
/null
)
415 for _pv
in $
(lvm vgs
--noheadings -o pv_name
"$_vg" 2>/dev
/null
)
417 check_block_and_slaves
$1 $
(get_maj_min
$_pv) && return 0
425 if [[ $DRACUT_INSTALL ]]; then
426 [[ $DRACUT_RESOLVE_LAZY ]] ||
export DRACUT_RESOLVE_DEPS
=1
428 [[ -e ${initdir}/"$1" ]] && return 0 # already there
429 $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@"
430 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@" ||
:
434 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
435 #dinfo "$DRACUT_INSTALL -l $@"
436 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
437 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" ||
:
441 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
442 [[ -e $1 ]] ||
return 1 # no source
443 $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@"
444 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} "$@" ||
:
448 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
449 [[ -L $1 ]] ||
return 1
450 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
451 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" ||
:
455 #dinfo "initdir=$initdir $DRACUT_INSTALL -l $@"
456 $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
457 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" ||
:
461 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
462 [[ -e $1 ]] ||
return 1 # no source
463 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
464 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" ||
:
468 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
469 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" ||
:
473 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
474 (($?
!= 0)) && derror
$DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" ||
:
479 # Install a directory, keeping symlinks as on the original system.
480 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
481 # will create ${initdir}/lib64, ${initdir}/lib64/file,
482 # and a symlink ${initdir}/lib -> lib64.
484 [[ -e ${initdir}/"$1" ]] && return 0 # already there
486 local _dir
="$1" _part
="${1%/*}" _file
487 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
492 # iterate over parent directories
493 for _file
in $_dir; do
494 [[ -e "${initdir}/$_file" ]] && continue
495 if [[ -L $_file ]]; then
496 inst_symlink
"$_file"
499 mkdir
-m 0755 -p "${initdir}/$_file" ||
return 1
500 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
501 chmod u
+w
"${initdir}/$_file"
506 # $1 = file to copy to ramdisk
507 # $2 (optional) Name for the file on the ramdisk
508 # Location of the image dir is assumed to be $initdir
509 # We never overwrite the target if it exists.
511 [[ -f "$1" ]] ||
return 1
512 strstr
"$1" "/" ||
return 1
513 local _src
=$1 _target
="${2:-$1}"
515 [[ -L $_src ]] && { inst_symlink
$_src $_target; return $?
; }
517 if ! [[ -d ${initdir}/$_target ]]; then
518 [[ -e ${initdir}/$_target ]] && return 0
519 [[ -L ${initdir}/$_target ]] && return 0
520 [[ -d "${initdir}/${_target%/*}" ]] || inst_dir "${_target%/*}"
522 if [[ $DRACUT_FIPS_MODE ]]; then
523 # install checksum files also
524 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
525 inst
"${_src%/*}/.${_src##*/}.hmac" "${_target%/*}/.${_target##*/}.hmac"
528 ddebug
"Installing $_src"
529 cp --reflink=auto
--sparse=auto
-pfL "$_src" "${initdir}/$_target"
532 # same as above, but specialized for symlinks
534 local _src
=$1 _target
=${2:-$1} _realsrc
535 strstr
"$1" "/" ||
return 1
536 [[ -L $1 ]] ||
return 1
537 [[ -L $initdir/$_target ]] && return 0
538 _realsrc
=$
(readlink
-f "$_src")
539 if ! [[ -e $initdir/$_realsrc ]]; then
540 if [[ -d $_realsrc ]]; then
546 [[ ! -e $initdir/${_target%/*} ]] && inst_dir
"${_target%/*}"
548 ln_r
"${_realsrc}" "${_target}"
551 # Same as above, but specialized to handle dynamic libraries.
552 # It handles making symlinks according to how the original library
555 local _src
="$1" _dest
=${2:-$1} _lib _reallib _symlink
556 strstr
"$1" "/" ||
return 1
557 [[ -e $initdir/$_dest ]] && return 0
558 if [[ -L $_src ]]; then
559 if [[ $DRACUT_FIPS_MODE ]]; then
560 # install checksum files also
561 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
562 inst
"${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
565 _reallib
=$
(readlink
-f "$_src")
566 inst_simple
"$_reallib" "$_reallib"
567 inst_dir
"${_dest%/*}"
568 ln_r
"${_reallib}" "${_dest}"
570 inst_simple
"$_src" "$_dest"
573 # Create additional symlinks. See rev_symlinks description.
574 for _symlink
in $
(rev_lib_symlinks
$_src) $
(rev_lib_symlinks
$_reallib); do
575 [[ ! -e $initdir/$_symlink ]] && {
576 ddebug
"Creating extra symlink: $_symlink"
577 inst_symlink
$_symlink
582 # Same as above, but specialized to install binary executables.
583 # Install binary executable, and all shared library dependencies, if any.
586 _bin
=$
(find_binary
"$1") ||
return 1
588 [[ -e $initdir/$_target ]] && return 0
590 local _so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
592 LC_ALL
=C ldd
"$_bin" 2>/dev
/null |
while read _line
; do
593 [[ $_line = 'not a dynamic executable' ]] && break
595 if [[ $_line =~
$_so_regex ]]; then
596 _file
=${BASH_REMATCH[1]}
597 [[ -e ${initdir}/$_file ]] && continue
598 inst_library
"$_file"
602 if [[ $_line =~ not\ found
]]; then
603 dfatal
"Missing a shared library required by $_bin."
604 dfatal
"Run \"ldd $_bin\" to find out what it is."
606 dfatal
"dracut cannot create an initrd."
610 inst_simple
"$_bin" "$_target"
613 # same as above, except for shell scripts.
614 # If your shell script does not start with shebang, it is not a shell script.
617 _bin
=$
(find_binary
"$1") ||
return 1
619 local _line _shebang_regex
620 read -r -n 80 _line
<"$_bin"
621 # If debug is set, clean unprintable chars to prevent messing up the term
622 [[ $debug ]] && _line
=$
(echo -n "$_line" |
tr -c -d '[:print:][:space:]')
623 _shebang_regex
='(#! *)(/[^ ]+).*'
624 [[ $_line =~
$_shebang_regex ]] ||
return 1
625 inst
"${BASH_REMATCH[2]}" && inst_simple
"$_bin" "$@"
628 # general purpose installation function
629 # Same args as above.
635 2) [[ ! $initdir && -d $2 ]] && export initdir
=$2
636 [[ $initdir = $2 ]] && set $1;;
637 3) [[ -z $initdir ]] && export initdir
=$2
639 *) dfatal
"inst only takes 1 or 2 or 3 arguments"
642 for _x
in inst_symlink inst_script inst_binary inst_simple
; do
648 # dracut_install [-o ] <file> [<file> ... ]
649 # Install <file> to the initramfs image
650 # -o optionally install the <file> and don't fail, if it is not there
653 if [[ $1 = '-o' ]]; then
658 if ! inst
"$1" ; then
659 if [[ $_optional = yes ]]; then
660 dinfo
"Skipping program $1 as it cannot be found and is" \
661 "flagged to be optional"
663 dfatal
"Failed to install $1"
673 # find symlinks linked to given library file
675 # Function searches for symlinks by stripping version numbers appended to
676 # library filename, checks if it points to the same target and finally
677 # prints the list of symlinks to stdout.
680 # rev_lib_symlinks libfoo.so.8.1
681 # output: libfoo.so.8 libfoo.so
682 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
684 [[ ! $1 ]] && return 0
686 local fn
="$1" orig
="$(readlink -f "$1")" links
=''
688 [[ ${fn} =~ .
*\.so\..
* ]] ||
return 1
690 until [[ ${fn##*.} == so
]]; do
692 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
698 # attempt to install any programs specified in a udev rule
699 inst_rule_programs() {
702 if grep -qE 'PROGRAM==?"[^
"]+' "$1"; then
703 for _prog in $(grep -E 'PROGRAM==?"[^
"]+' "$1" | sed -r 's/.*PROGRAM==?"([^
"]+).*/\1/'); do
704 if [ -x ${udevdir}/$_prog ]; then
705 _bin=${udevdir}/$_prog
707 _bin=$(find_binary "$_prog") || {
708 dinfo "Skipping program
$_prog using
in udev rule $
(basename $1) as it cannot be found
"
713 #dinfo "Installing
$_bin due to it
's use in the udev rule $(basename $1)"
714 dracut_install "$_bin"
717 if grep -qE 'RUN
==?
"[^ "]+' "$1"; then
718 for _prog in $(grep -E 'RUN
==?
"[^ "]+' "$1" | sed -r 's
/.
*RUN
==?
"([^ "]+).
*/\
1/'); do
719 if [ -x ${udevdir}/$_prog ]; then
720 _bin=${udevdir}/$_prog
722 _bin=$(find_binary "$_prog") || {
723 dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
728 #dinfo "Installing $_bin due to it's use
in the udev rule $
(basename $1)"
729 dracut_install "$_bin"
732 if grep -qE 'PROGRAM==?"[^
"]+' "$1"; then
733 for _prog in $(grep -E 'IMPORT==?"[^
"]+' "$1" | sed -r 's/.*IMPORT==?"([^
"]+).*/\1/'); do
734 if [ -x ${udevdir}/$_prog ]; then
735 _bin=${udevdir}/$_prog
737 _bin=$(find_binary "$_prog") || {
738 dinfo "Skipping program
$_prog using
in udev rule $
(basename $1) as it cannot be found
"
743 #dinfo "Installing
$_bin due to it
's use in the udev rule $(basename $1)"
744 dracut_install "$_bin"
749 # udev rules always get installed in the same place, so
750 # create a function to install them to make life simpler.
752 local _target=/etc/udev/rules.d _rule _found
754 inst_dir "${udevdir}/rules.d"
756 for _rule in "$@"; do
757 if [ "${rule#/}" = "$rule" ]; then
758 for r in ${udevdir}/rules.d /etc/udev/rules.d; do
759 if [[ -f $r/$_rule ]]; then
761 inst_rule_programs "$_found"
762 inst_simple "$_found"
766 for r in '' ./ $dracutbasedir/rules.d/; do
767 if [[ -f ${r}$_rule ]]; then
769 inst_rule_programs "$_found"
770 inst_simple "$_found" "$_target/${_found##*/}"
773 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
777 # install function specialized for hooks
778 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
779 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
781 if ! [[ -f $3 ]]; then
782 dfatal "Cannot install a hook ($3) that does not exist."
783 dfatal "Aborting initrd creation."
785 elif ! strstr "$hookdirs" "$1"; then
786 dfatal "No such hook type $1. Aborting initrd creation."
789 inst_simple "$3" "/lib/dracut/hooks/${1}/${2}${3##*/}"
792 # install any of listed files
794 # If first argument is '-d' and second some destination path, first accessible
795 # source is installed into this path, otherwise it will installed in the same
796 # path as source. If none of listed files was installed, function return 1.
797 # On first successful installation it returns with 0 status.
801 # inst_any -d /bin/foo /bin/bar /bin/baz
803 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
808 [[ $1 = '-d' ]] && to="$2" && shift 2
812 [[ $to ]] && inst "$f" "$to" && return 0
813 inst "$f" && return 0
821 # inst_libdir_file [-n <pattern>] <file> [<file>...]
822 # Install a <file> located on a lib directory to the initramfs image
823 # -n <pattern> install matching files
826 if [[ "$1" == "-n" ]]; then
829 for _dir in $libdirs; do
831 for _f in "$_dir"/$_i; do
832 [[ "$_f" =~ $_pattern ]] || continue
833 [[ -e "$_f" ]] && _files+="$_f "
838 for _dir in $libdirs; do
840 for _f in "$_dir"/$_i; do
841 [[ -e "$_f" ]] && _files+="$_f "
846 [[ $_files ]] && dracut_install $_files
850 # install function decompressing the target and handling symlinks
851 # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
853 # Function install targets in the same paths inside overlay but decompressed
854 # and without extensions (.gz, .bz2).
861 *.gz) _cmd='gzip -d' ;;
862 *.bz2) _cmd='bzip2 -d' ;;
866 # Decompress with chosen tool. We assume that tool changes name e.g.
867 # from 'name.gz
' to 'name
'.
868 ${_cmd} "${initdir}${_src}"
872 # It's similar to above
, but
if file is not compressed
, performs standard
875 inst_opt_decompress
() {
880 inst_decompress
"${_src}" || inst
"${_src}"
884 # module_check <dracut module>
885 # execute the check() function of module-setup.sh of <dracut module>
886 # or the "check" script, if module-setup.sh is not found
887 # "check $hostonly" is called
889 local _moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
892 local _hostonly
=$hostonly
893 [ $# -eq 2 ] && _forced
=$2
894 [[ -d $_moddir ]] ||
return 1
895 if [[ ! -f $_moddir/module-setup.sh
]]; then
896 # if we do not have a check script, we are unconditionally included
897 [[ -x $_moddir/check
]] ||
return 0
898 [ $_forced -ne 0 ] && unset hostonly
899 $_moddir/check
$hostonly
902 unset check depends
install installkernel
903 .
$_moddir/module-setup.sh
904 is_func check ||
return 0
905 [ $_forced -ne 0 ] && unset hostonly
908 unset check depends
install installkernel
914 # module_check_mount <dracut module>
915 # execute the check() function of module-setup.sh of <dracut module>
916 # or the "check" script, if module-setup.sh is not found
917 # "mount_needs=1 check 0" is called
918 module_check_mount
() {
919 local _moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
922 [[ -d $_moddir ]] ||
return 1
923 if [[ ! -f $_moddir/module-setup.sh
]]; then
924 # if we do not have a check script, we are unconditionally included
925 [[ -x $_moddir/check
]] ||
return 0
926 mount_needs
=1 $_moddir/check
0
929 unset check depends
install installkernel
930 .
$_moddir/module-setup.sh
931 is_func check ||
return 1
934 unset check depends
install installkernel
940 # module_depends <dracut module>
941 # execute the depends() function of module-setup.sh of <dracut module>
942 # or the "depends" script, if module-setup.sh is not found
944 local _moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
946 [[ -d $_moddir ]] ||
return 1
947 if [[ ! -f $_moddir/module-setup.sh
]]; then
948 # if we do not have a check script, we have no deps
949 [[ -x $_moddir/check
]] ||
return 0
953 unset check depends
install installkernel
954 .
$_moddir/module-setup.sh
955 is_func depends ||
return 0
958 unset check depends
install installkernel
963 # module_install <dracut module>
964 # execute the install() function of module-setup.sh of <dracut module>
965 # or the "install" script, if module-setup.sh is not found
967 local _moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
969 [[ -d $_moddir ]] ||
return 1
970 if [[ ! -f $_moddir/module-setup.sh
]]; then
971 [[ -x $_moddir/install ]] && .
"$_moddir/install"
974 unset check depends
install installkernel
975 .
$_moddir/module-setup.sh
976 is_func
install ||
return 0
979 unset check depends
install installkernel
984 # module_installkernel <dracut module>
985 # execute the installkernel() function of module-setup.sh of <dracut module>
986 # or the "installkernel" script, if module-setup.sh is not found
987 module_installkernel
() {
988 local _moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
990 [[ -d $_moddir ]] ||
return 1
991 if [[ ! -f $_moddir/module-setup.sh
]]; then
992 [[ -x $_moddir/installkernel
]] && .
"$_moddir/installkernel"
995 unset check depends
install installkernel
996 .
$_moddir/module-setup.sh
997 is_func installkernel ||
return 0
1000 unset check depends
install installkernel
1005 # check_mount <dracut module>
1006 # check_mount checks, if a dracut module is needed for the given
1007 # device and filesystem types in "${host_fs_types[@]}"
1010 local _moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
1013 # If we are already scheduled to be loaded, no need to check again.
1014 strstr
" $mods_to_load " " $_mod " && return 0
1015 strstr
" $mods_checked_as_dep " " $_mod " && return 1
1017 # This should never happen, but...
1018 [[ -d $_moddir ]] ||
return 1
1020 [[ $2 ]] || mods_checked_as_dep
+=" $_mod "
1022 strstr
" $omit_dracutmodules " " $_mod " && return 1
1024 if [ "${#host_fs_types[*]}" -gt 0 ]; then
1025 module_check_mount
$_mod ||
return 1
1031 for _moddep
in $
(module_depends
$_mod); do
1032 # handle deps as if they were manually added
1033 strstr
" $add_dracutmodules " " $_moddep " || \
1034 add_dracutmodules
+=" $_moddep "
1035 strstr
" $force_add_dracutmodules " " $_moddep " || \
1036 force_add_dracutmodules
+=" $_moddep "
1037 # if a module we depend on fail, fail also
1038 check_module
$_moddep ||
return 1
1041 strstr
" $mods_to_load " " $_mod " || \
1042 mods_to_load
+=" $_mod "
1047 # check_module <dracut module> [<use_as_dep>]
1048 # check if a dracut module is to be used in the initramfs process
1049 # if <use_as_dep> is set, then the process also keeps track
1050 # that the modules were checked for the dependency tracking process
1053 local _moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
1056 # If we are already scheduled to be loaded, no need to check again.
1057 strstr
" $mods_to_load " " $_mod " && return 0
1058 strstr
" $mods_checked_as_dep " " $_mod " && return 1
1060 # This should never happen, but...
1061 [[ -d $_moddir ]] ||
return 1
1063 [[ $2 ]] || mods_checked_as_dep
+=" $_mod "
1065 strstr
" $omit_dracutmodules " " $_mod " && return 1
1067 if strstr
" $dracutmodules $add_dracutmodules $force_add_dracutmodules" " $_mod "; then
1068 if strstr
" $force_add_dracutmodules" " $_mod"; then
1069 module_check
$_mod 1; ret
=$?
1071 module_check
$_mod 0; ret
=$?
1073 # explicit module, so also accept ret=255
1074 [[ $ret = 0 ||
$ret = 255 ]] ||
return 1
1076 # module not in our list
1077 if [[ $dracutmodules = all
]]; then
1078 # check, if we can and should install this module
1079 module_check
$_mod ||
return 1
1086 for _moddep
in $
(module_depends
$_mod); do
1087 # handle deps as if they were manually added
1088 strstr
" $add_dracutmodules " " $_moddep " || \
1089 add_dracutmodules
+=" $_moddep "
1090 strstr
" $force_add_dracutmodules " " $_moddep " || \
1091 force_add_dracutmodules
+=" $_moddep "
1092 # if a module we depend on fail, fail also
1093 check_module
$_moddep ||
return 1
1096 strstr
" $mods_to_load " " $_mod " || \
1097 mods_to_load
+=" $_mod "
1102 # for_each_module_dir <func>
1103 # execute "<func> <dracut module> 1"
1104 for_each_module_dir
() {
1110 for _moddir
in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1111 _mod
=${_moddir##*/}; _mod
=${_mod#[0-9][0-9]}
1115 # Report any missing dracut modules, the user has specified
1116 _modcheck
="$add_dracutmodules $force_add_dracutmodules"
1117 [[ $dracutmodules != all
]] && _modcheck
="$m $dracutmodules"
1118 for _mod
in $_modcheck; do
1119 strstr
"$mods_to_load" "$_mod" && continue
1120 strstr
"$omit_dracutmodules" "$_mod" && continue
1121 derror
"Dracut module \"$_mod\" cannot be found or installed."
1125 # Install a single kernel module along with any firmware it may require.
1126 # $1 = full path to kernel module to install
1127 install_kmod_with_fw
() {
1128 # no need to go further if the module is already installed
1130 [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
1133 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -e "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}" ]]; then
1134 read ret
< "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
1138 if [[ $omit_drivers ]]; then
1139 local _kmod
=${1##*/}
1142 if [[ "$_kmod" =~
$omit_drivers ]]; then
1143 dinfo
"Omitting driver $_kmod"
1146 if [[ "${1##*/lib/modules/$kernel/}" =~
$omit_drivers ]]; then
1147 dinfo
"Omitting driver $_kmod"
1152 inst_simple
"$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
1154 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1155 [[ -d "$DRACUT_KERNEL_LAZY_HASHDIR" ]] && \
1156 echo $ret > "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
1157 (($ret != 0)) && return $ret
1159 local _modname
=${1##*/} _fwdir _found _fw
1160 _modname
=${_modname%.ko*}
1161 for _fw
in $
(modinfo
-k $kernel -F firmware
$1 2>/dev
/null
); do
1163 for _fwdir
in $fw_dir; do
1164 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1165 inst_simple
"$_fwdir/$_fw" "/lib/firmware/$_fw"
1169 if [[ $_found != yes ]]; then
1170 if ! [[ -d $
(echo /sys
/module
/${_modname//-/_}|
{ read a b
; echo $a; }) ]]; then
1171 dinfo
"Possible missing firmware \"${_fw}\" for kernel module" \
1172 "\"${_modname}.ko\""
1174 dwarn
"Possible missing firmware \"${_fw}\" for kernel module" \
1175 "\"${_modname}.ko\""
1182 # Do something with all the dependencies of a kernel module.
1183 # Note that kernel modules depend on themselves using the technique we use
1184 # $1 = function to call for each dependency we find
1185 # It will be passed the full path to the found kernel module
1186 # $2 = module to get dependencies for
1187 # rest of args = arguments to modprobe
1188 # _fderr specifies FD passed from surrounding scope
1189 for_each_kmod_dep
() {
1190 local _func
=$1 _kmod
=$2 _cmd _modpath _options
1192 modprobe
"$@" --ignore-install --show-depends $_kmod 2>&${_fderr} |
(
1193 while read _cmd _modpath _options
; do
1194 [[ $_cmd = insmod
]] ||
continue
1195 $_func ${_modpath} ||
exit $?
1200 dracut_kernel_post
() {
1201 local _moddirname
=${srcmods%%/lib/modules/*}
1203 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" ]]; then
1204 xargs -r modprobe
-a ${_moddirname+-d ${_moddirname}/} \
1205 --ignore-install --show-depends --set-version $kernel \
1206 < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" 2>/dev
/null \
1208 |
while read _cmd _modpath _options
; do
1209 [[ $_cmd = insmod
]] ||
continue
1211 done > "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1214 if [[ $DRACUT_INSTALL ]] && [[ -z $_moddirname ]]; then
1215 xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1217 while read _modpath
; do
1218 local _destpath
=$_modpath
1219 [[ $_moddirname ]] && _destpath
=${_destpath##$_moddirname/}
1220 _destpath
=${_destpath##*/lib/modules/$kernel/}
1221 inst_simple
"$_modpath" "/lib/modules/$kernel/${_destpath}" ||
exit $?
1222 done < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1226 if [[ $DRACUT_INSTALL ]]; then
1227 xargs -r modinfo
-k $kernel -F firmware
< "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" \
1228 |
while read line
; do
1229 for _fwdir
in $fw_dir; do
1232 done |
xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a -o
1234 for _fw
in $
(xargs -r modinfo
-k $kernel -F firmware
< "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"); do
1235 for _fwdir
in $fw_dir; do
1236 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1237 inst_simple
"$_fwdir/$_fw" "/lib/firmware/$_fw"
1247 for _f
in modules.
builtin.bin modules.
builtin; do
1248 [[ $srcmods/$_f ]] && break
1250 dfatal
"No modules.builtin.bin and modules.builtin found!"
1254 for _f
in modules.
builtin.bin modules.
builtin modules.order
; do
1255 [[ $srcmods/$_f ]] && inst_simple
"$srcmods/$_f" "/lib/modules/$kernel/$_f"
1258 # generate module dependencies for the initrd
1259 if [[ -d $initdir/lib
/modules
/$kernel ]] && \
1260 ! depmod
-a -b "$initdir" $kernel; then
1261 dfatal
"\"depmod -a $kernel\" failed."
1265 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && rm -fr "$DRACUT_KERNEL_LAZY_HASHDIR"
1268 find_kernel_modules_by_path
() (
1270 if ! [[ $hostonly ]]; then
1273 while read a rest
; do
1274 [[ $a = kernel
*/$1/* ]] ||
continue
1276 done < $srcmods/modules.dep
1279 ( cd /sys
/module
; echo *; ) \
1280 |
xargs -r modinfo
-F filename
-k $kernel 2>/dev
/null
1285 find_kernel_modules
() {
1286 find_kernel_modules_by_path drivers
1289 # instmods [-c] <kernel module> [<kernel module> ... ]
1290 # instmods [-c] <kernel subsystem>
1291 # install kernel modules along with all their dependencies.
1292 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1294 [[ $no_kernel = yes ]] && return
1295 # called [sub]functions inherit _fderr
1298 if [[ $1 = '-c' ]]; then
1303 function inst1mod
() {
1304 local _ret
=0 _mod
="$1"
1307 ( [[ "$_mpargs" ]] && echo $_mpargs
1308 find_kernel_modules_by_path
"${_mod#=}" ) \
1312 --*) _mpargs
+=" $_mod" ;;
1313 i2o_scsi
) return 0;; # Do not load this diagnostic-only module
1316 # if we are already installed, skip this module and go on
1318 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1319 [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko" ]]; then
1320 read _ret
<"$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko"
1324 if [[ $omit_drivers ]] && [[ "$1" =~
$omit_drivers ]]; then
1325 dinfo
"Omitting driver ${_mod##$srcmods}"
1328 # If we are building a host-specific initramfs and this
1329 # module is not already loaded, move on to the next one.
1331 && ! [[ -d $
(echo /sys
/module
/${_mod//-/_}|
{ read a b
; echo $a; }) ]] \
1332 && ! [[ "$add_drivers" =~
" ${_mod} " ]] \
1335 if [[ "$_check" = "yes" ]] ||
! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
1336 # We use '-d' option in modprobe only if modules prefix path
1337 # differs from default '/'. This allows us to use Dracut with
1338 # old version of modprobe which doesn't have '-d' option.
1339 local _moddirname
=${srcmods%%/lib/modules/*}
1340 [[ -n ${_moddirname} ]] && _moddirname
="-d ${_moddirname}/"
1342 # ok, load the module, all its dependencies, and any firmware
1344 for_each_kmod_dep install_kmod_with_fw
$_mod \
1345 --set-version $kernel ${_moddirname} $_mpargs
1348 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1349 echo $_mod >> "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist"
1356 function instmods_1
() {
1358 if (($# == 0)); then # filenames from stdin
1360 inst1mod
"${_mod%.ko*}" ||
{
1361 if [ "$_check" = "yes" ]; then
1362 dfatal
"Failed to install $_mod"
1368 while (($# > 0)); do # filenames as arguments
1369 inst1mod
${1%.ko*} ||
{
1370 if [ "$_check" = "yes" ]; then
1371 dfatal
"Failed to install $1"
1380 local _ret _filter_not_found
='FATAL: Module .* not found.'
1381 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1382 # redirections, but that would make dracut require bash4 at least.
1383 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1384 |
while read line
; do [[ "$line" =~
$_filter_not_found ]] ||
echo $line;done | derror