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
65 printf "%s\n" ${d%/*};
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//+(\/)//}"
193 printf "%s\n" "${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" ]] && { printf "%s\n" "${1##*/}"; return; }
211 # corner case #2 - own dir link
212 [[ "${1%/*}" == "$2" ]] && { printf ".\n"; 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]}
247 printf "%s\n" "$__newpath"
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
308 /dev/disk/${persistent_policy:-by-uuid}/* \
309 /dev/disk/by-uuid/* \
310 /dev/disk/by-label/* \
311 /dev/disk/by-partuuid/* \
312 /dev/disk/by-partlabel/* \
314 /dev/disk/by-path/* \
316 [[ $i == /dev/mapper/control ]] && continue
317 [[ $i == /dev/mapper/mpath* ]] && continue
318 _tmp=$(get_maj_min "$i")
319 if [ "$_tmp" = "$_dev" ]; then
326 shorten_persistent_dev() {
330 printf "%s" "UUID=${dev##*/}";;
331 /dev/disk/by-label/*)
332 printf "%s" "LABEL=${dev##*/}";;
333 /dev/disk/by-partuuid/*)
334 printf "%s" "PARTUUID=${dev##*/}";;
335 /dev/disk/by-partlabel/*)
336 printf "%s" "PARTLABEL=${dev##*/}";;
342 # find_block_device <mountpoint>
343 # Prints the major and minor number of the block device
344 # for a given mountpoint.
345 # Unless $use_fstab is set to "yes" the functions
346 # uses /proc/self/mountinfo as the primary source of the
347 # information and only falls back to /etc/fstab, if the mountpoint
348 # is not found there.
350 # $ find_block_device /usr
352 find_block_device() {
353 local _majmin _dev _majmin _find_mpt
355 if [[ $use_fstab != yes ]]; then
356 [[ -d $_find_mpt/. ]]
357 findmnt -e -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" | { \
358 while read _majmin _dev; do
359 if [[ -b $_dev ]]; then
360 if ! [[ $_majmin ]] || [[ $_majmin == 0:* ]]; then
361 _majmin=$(get_maj_min $_dev)
363 if [[ $_majmin ]]; then
364 printf "%s\n" "$_majmin"
366 printf "%s\n" "$_dev"
370 if [[ $_dev = *:* ]]; then
371 printf "%s\n" "$_dev"
374 done; return 1; } && return 0
376 # fall back to /etc/fstab
378 findmnt -e --fstab -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" | { \
379 while read _majmin _dev; do
380 if ! [[ $_dev ]]; then
384 if [[ -b $_dev ]]; then
385 [[ $_majmin ]] || _majmin=$(get_maj_min $_dev)
386 if [[ $_majmin ]]; then
387 printf "%s\n" "$_majmin"
389 printf "%s\n" "$_dev"
393 if [[ $_dev = *:* ]]; then
394 printf "%s\n" "$_dev"
397 done; return 1; } && return 0
402 # find_mp_fstype <mountpoint>
403 # Echo the filesystem type for a given mountpoint.
404 # /proc/self/mountinfo is taken as the primary source of information
405 # and /etc/fstab is used as a fallback.
406 # No newline is appended!
408 # $ find_mp_fstype /;echo
413 if [[ $use_fstab != yes ]]; then
414 findmnt -e -v -n -o 'FSTYPE' --target "$1" | { \
416 [[ $_fs ]] || continue
417 [[ $_fs = "autofs" ]] && continue
420 done; return 1; } && return 0
423 findmnt --fstab -e -v -n -o 'FSTYPE' --target "$1" | { \
425 [[ $_fs ]] || continue
426 [[ $_fs = "autofs" ]] && continue
429 done; return 1; } && return 0
434 # find_dev_fstype <device>
435 # Echo the filesystem type for a given device.
436 # /proc/self/mountinfo is taken as the primary source of information
437 # and /etc/fstab is used as a fallback.
438 # No newline is appended!
440 # $ find_dev_fstype /dev/sda2;echo
445 if ! [[ "$_find_dev" = /dev* ]]; then
446 [[ -b "/dev/block/$_find_dev" ]] && _find_dev="/dev/block/$_find_dev"
449 if [[ $use_fstab != yes ]]; then
450 findmnt -e -v -n -o 'FSTYPE' --source "$_find_dev" | { \
452 [[ $_fs ]] || continue
453 [[ $_fs = "autofs" ]] && continue
456 done; return 1; } && return 0
459 findmnt --fstab -e -v -n -o 'FSTYPE' --source "$_find_dev" | { \
461 [[ $_fs ]] || continue
462 [[ $_fs = "autofs" ]] && continue
465 done; return 1; } && return 0
470 # find_mp_fsopts <mountpoint>
471 # Echo the filesystem options for a given mountpoint.
472 # /proc/self/mountinfo is taken as the primary source of information
473 # and /etc/fstab is used as a fallback.
474 # No newline is appended!
476 # $ find_mp_fsopts /;echo
477 # rw,relatime,discard,data=ordered
479 if [[ $use_fstab != yes ]]; then
480 findmnt -e -v -n -o 'OPTIONS' --target "$1" 2>/dev/null && return 0
483 findmnt --fstab -e -v -n -o 'OPTIONS' --target "$1"
486 # find_dev_fsopts <device>
487 # Echo the filesystem options for a given device.
488 # /proc/self/mountinfo is taken as the primary source of information
489 # and /etc/fstab is used as a fallback.
491 # $ find_dev_fsopts /dev/sda2
492 # rw,relatime,discard,data=ordered
494 local _find_dev _opts
496 if ! [[ "$_find_dev" = /dev* ]]; then
497 [[ -b "/dev/block/$_find_dev" ]] && _find_dev="/dev/block/$_find_dev"
500 if [[ $use_fstab != yes ]]; then
501 findmnt -e -v -n -o 'OPTIONS' --source "$_find_dev" 2>/dev/null && return 0
504 findmnt --fstab -e -v -n -o 'OPTIONS' --source "$_find_dev"
508 # finds the major:minor of the block device backing the root filesystem.
509 find_root_block_device() { find_block_device /; }
511 # for_each_host_dev_fs <func>
512 # Execute "<func> <dev> <filesystem>" for every "<dev> <fs>" pair found
513 # in ${host_fs_types[@]}
514 for_each_host_dev_fs()
520 [[ "${!host_fs_types[@]}" ]] || return 0
522 for _dev in "${!host_fs_types[@]}"; do
523 $_func "$_dev" "${host_fs_types[$_dev]}" && _ret=0
530 printf "%s\n" "${host_fs_types[@]}"
533 # Walk all the slave relationships for a given block device.
534 # Stop when our helper function returns success
535 # $1 = function to call on every found block device
536 # $2 = block device in major:minor format
537 check_block_and_slaves() {
539 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
541 check_vol_slaves "$@" && return 0
542 if [[ -f /sys/dev/block/$2/../dev ]]; then
543 check_block_and_slaves $1 $(<"/sys/dev/block/$2/../dev") && return 0
545 [[ -d /sys/dev/block/$2/slaves ]] || return 1
546 for _x in /sys/dev/block/$2/slaves/*/dev; do
547 [[ -f $_x ]] || continue
548 check_block_and_slaves $1 $(<"$_x") && return 0
553 check_block_and_slaves_all() {
555 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
559 check_vol_slaves "$@" && return 0
560 if [[ -f /sys/dev/block/$2/../dev ]]; then
561 check_block_and_slaves_all $1 $(<"/sys/dev/block/$2/../dev") && _ret=0
563 [[ -d /sys/dev/block/$2/slaves ]] || return 1
564 for _x in /sys/dev/block/$2/slaves/*/dev; do
565 [[ -f $_x ]] || continue
566 check_block_and_slaves_all $1 $(<"$_x") && _ret=0
570 # for_each_host_dev_and_slaves <func>
571 # Execute "<func> <dev>" for every "<dev>" found
572 # in ${host_devs[@]} and their slaves
573 for_each_host_dev_and_slaves_all()
579 [[ "${host_devs[@]}" ]] || return 0
581 for _dev in ${host_devs[@]}; do
582 [[ -b "$_dev" ]] || continue
583 if check_block_and_slaves_all $_func $(get_maj_min $_dev); then
590 for_each_host_dev_and_slaves()
595 [[ "${host_devs[@]}" ]] || return 0
597 for _dev in ${host_devs[@]}; do
598 [[ -b "$_dev" ]] || continue
599 check_block_and_slaves $_func $(get_maj_min $_dev) && return 0
604 # ugly workaround for the lvm design
605 # There is no volume group device,
606 # so, there are no slave devices for volume groups.
607 # Logical volumes only have the slave devices they really live on,
608 # but you cannot create the logical volume without the volume group.
609 # And the volume group might be bigger than the devices the LV needs.
612 for i in /dev/mapper/*; do
613 [[ $i == /dev/mapper/control ]] && continue
614 _lv=$(get_maj_min $i)
615 if [[ $_lv = $2 ]]; then
616 _vg=$(lvm lvs --noheadings -o vg_name $i 2>/dev/null)
618 _vg=$(printf "%s\n" "$_vg")
620 for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2>/dev/null)
622 check_block_and_slaves $1 $(get_maj_min $_pv) && return 0
630 # fs_get_option <filesystem options> <search for option>
631 # search for a specific option in a bunch of filesystem options
632 # and return the value
640 while [ $# -gt 0 ]; do
643 echo ${1#${_option}=}
651 if ! [[ $DRACUT_INSTALL ]]; then
652 DRACUT_INSTALL=$(find_binary dracut-install)
655 if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install ]]; then
656 DRACUT_INSTALL=$dracutbasedir/dracut-install
659 if ! [[ -x $DRACUT_INSTALL ]]; then
660 dfatal "dracut-install not found!"
664 [[ $DRACUT_RESOLVE_LAZY ]] || export DRACUT_RESOLVE_DEPS=1
666 [[ -e ${initdir}/"$1" ]] && return 0 # already there
667 $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@"
668 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@" || :
672 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
673 #dinfo "$DRACUT_INSTALL -l $@"
674 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
675 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
679 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
680 [[ -e $1 ]] || return 1 # no source
681 $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@"
682 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@" || :
686 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
687 [[ -L $1 ]] || return 1
688 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
689 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
694 #dinfo "initdir=$initdir $DRACUT_INSTALL -l $@"
695 $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
697 (($ret != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
706 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
707 [[ -e $1 ]] || return 1 # no source
708 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
709 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
713 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
714 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
718 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
719 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
722 # find symlinks linked to given library file
724 # Function searches for symlinks by stripping version numbers appended to
725 # library filename, checks if it points to the same target and finally
726 # prints the list of symlinks to stdout.
729 # rev_lib_symlinks libfoo.so.8.1
730 # output: libfoo.so.8 libfoo.so
731 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
733 [[ ! $1 ]] && return 0
735 local fn="$1" orig="$(readlink -f "$1")" links=''
737 [[ ${fn} == *.so.* ]] || return 1
739 until [[ ${fn##*.} == so ]]; do
741 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
747 # attempt to install any programs specified in a udev rule
748 inst_rule_programs() {
751 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
752 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
754 if [ -x ${udevdir}/$_prog ]; then
755 _bin=${udevdir}/$_prog
756 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
757 _bin=$(find_binary "$_prog") || {
758 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
763 [[ $_bin ]] && inst_binary "$_bin"
766 if grep -qE 'RUN[+=]=?"[^ "]+' "$1"; then
767 for _prog in $(grep -E 'RUN[+=]=?"[^ "]+' "$1" | sed -r 's/.*RUN[+=]=?"([^ "]+).*/\1/'); do
769 if [ -x ${udevdir}/$_prog ]; then
770 _bin=${udevdir}/$_prog
771 elif [[ "${_prog/\$env\{/}" == "$_prog" ]] && [[ "${_prog}" != "/sbin/initqueue" ]]; then
772 _bin=$(find_binary "$_prog") || {
773 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
778 [[ $_bin ]] && inst_binary "$_bin"
781 if grep -qE 'IMPORT\{program\}==?"[^ "]+' "$1"; then
782 for _prog in $(grep -E 'IMPORT\{program\}==?"[^ "]+' "$1" | sed -r 's/.*IMPORT\{program\}==?"([^ "]+).*/\1/'); do
784 if [ -x ${udevdir}/$_prog ]; then
785 _bin=${udevdir}/$_prog
786 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
787 _bin=$(find_binary "$_prog") || {
788 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
793 [[ $_bin ]] && dracut_install "$_bin"
798 # attempt to install any programs specified in a udev rule
799 inst_rule_group_owner() {
802 if grep -qE 'OWNER=?"[^ "]+' "$1"; then
803 for i in $(grep -E 'OWNER=?"[^ "]+' "$1" | sed -r 's/.*OWNER=?"([^ "]+).*/\1/'); do
804 if ! egrep -q "^$i:" "$initdir/etc/passwd" 2>/dev/null; then
805 egrep "^$i:" /etc/passwd 2>/dev/null >> "$initdir/etc/passwd"
809 if grep -qE 'GROUP=?"[^ "]+' "$1"; then
810 for i in $(grep -E 'GROUP=?"[^ "]+' "$1" | sed -r 's/.*GROUP=?"([^ "]+).*/\1/'); do
811 if ! egrep -q "^$i:" "$initdir/etc/group" 2>/dev/null; then
812 egrep "^$i:" /etc/group 2>/dev/null >> "$initdir/etc/group"
818 inst_rule_initqueue() {
819 if grep -q -F initqueue "$1"; then
820 dracut_need_initqueue
824 # udev rules always get installed in the same place, so
825 # create a function to install them to make life simpler.
827 local _target=/etc/udev/rules.d _rule _found
829 inst_dir "${udevdir}/rules.d"
831 for _rule in "$@"; do
832 if [ "${_rule#/}" = "$_rule" ]; then
833 for r in ${udevdir}/rules.d /etc/udev/rules.d; do
834 if [[ -f $r/$_rule ]]; then
836 inst_rule_programs "$_found"
837 inst_rule_group_owner "$_found"
838 inst_rule_initqueue "$_found"
839 inst_simple "$_found"
843 for r in '' ./ $dracutbasedir/rules.d/; do
844 if [[ -f ${r}$_rule ]]; then
846 inst_rule_programs "$_found"
847 inst_rule_group_owner "$_found"
848 inst_rule_initqueue "$_found"
849 inst_simple "$_found" "$_target/${_found##*/}"
852 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
856 prepare_udev_rules() {
857 [ -z "$UDEVVERSION" ] && export UDEVVERSION=$(udevadm --version)
860 f="${initdir}/etc/udev/rules.d/$f"
861 [ -e "$f" ] || continue
863 if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
864 if [ $UDEVVERSION -ge 174 ]; then
865 printf '%sIMPORT{builtin}="path_id"\n' "${line%%IMPORT PATH_ID}"
867 printf '%sIMPORT{program}="path_id %%p"\n' "${line%%IMPORT PATH_ID}"
869 elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
870 if [ $UDEVVERSION -ge 176 ]; then
871 printf '%sIMPORT{builtin}="blkid"\n' "${line%%IMPORT BLKID}"
873 printf '%sIMPORT{program}="/sbin/blkid -o udev -p $tempnode"\n' "${line%%IMPORT BLKID}"
878 done < "${f}" > "${f}.new"
883 # install function specialized for hooks
884 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
885 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
887 if ! [[ -f $3 ]]; then
888 dfatal "Cannot install a hook ($3) that does not exist."
889 dfatal "Aborting initrd creation."
891 elif ! [[ "$hookdirs" == *$1* ]]; then
892 dfatal "No such hook type $1. Aborting initrd creation."
895 inst_simple "$3" "/lib/dracut/hooks/${1}/${2}-${3##*/}"
898 # install any of listed files
900 # If first argument is '-d' and second some destination path, first accessible
901 # source is installed into this path, otherwise it will installed in the same
902 # path as source. If none of listed files was installed, function return 1.
903 # On first successful installation it returns with 0 status.
907 # inst_any -d /bin/foo /bin/bar /bin/baz
909 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
914 [[ $1 = '-d' ]] && to="$2" && shift 2
918 [[ $to ]] && inst "$f" "$to" && return 0
919 inst "$f" && return 0
927 # inst_libdir_file [-n <pattern>] <file> [<file>...]
928 # Install a <file> located on a lib directory to the initramfs image
929 # -n <pattern> install matching files
932 if [[ "$1" == "-n" ]]; then
935 for _dir in $libdirs; do
937 for _f in "$_dir"/$_i; do
938 [[ "$_f" =~ $_pattern ]] || continue
939 [[ -e "$_f" ]] && _files+="$_f "
944 for _dir in $libdirs; do
946 for _f in "$_dir"/$_i; do
947 [[ -e "$_f" ]] && _files+="$_f "
952 [[ $_files ]] && inst_multiple $_files
956 # install function decompressing the target and handling symlinks
957 # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
959 # Function install targets in the same paths inside overlay but decompressed
960 # and without extensions (.gz, .bz2).
967 *.gz) _cmd='gzip -f -d' ;;
968 *.bz2) _cmd='bzip2 -d' ;;
972 # Decompress with chosen tool. We assume that tool changes name e.g.
973 # from 'name.gz' to 'name'.
974 ${_cmd} "${initdir}${_src}"
978 # It's similar to above, but if file is not compressed, performs standard
981 inst_opt_decompress() {
986 inst_decompress "${_src}" || inst "${_src}"
990 # module_check <dracut module>
991 # execute the check() function of module-setup.sh of <dracut module>
992 # or the "check" script, if module-setup.sh is not found
993 # "check $hostonly" is called
995 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
998 local _hostonly=$hostonly
999 [ $# -eq 2 ] && _forced=$2
1000 [[ -d $_moddir ]] || return 1
1001 if [[ ! -f $_moddir/module-setup.sh ]]; then
1002 # if we do not have a check script, we are unconditionally included
1003 [[ -x $_moddir/check ]] || return 0
1004 [ $_forced -ne 0 ] && unset hostonly
1005 $_moddir/check $hostonly
1008 unset check depends cmdline install installkernel
1010 . $_moddir/module-setup.sh
1011 is_func check || return 0
1012 [ $_forced -ne 0 ] && unset hostonly
1015 unset check depends cmdline install installkernel
1021 # module_check_mount <dracut module>
1022 # execute the check() function of module-setup.sh of <dracut module>
1023 # or the "check" script, if module-setup.sh is not found
1024 # "mount_needs=1 check 0" is called
1025 module_check_mount() {
1026 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1029 [[ -d $_moddir ]] || return 1
1030 if [[ ! -f $_moddir/module-setup.sh ]]; then
1031 # if we do not have a check script, we are unconditionally included
1032 [[ -x $_moddir/check ]] || return 0
1033 mount_needs=1 $_moddir/check 0
1036 unset check depends cmdline install installkernel
1038 . $_moddir/module-setup.sh
1041 unset check depends cmdline install installkernel
1047 # module_depends <dracut module>
1048 # execute the depends() function of module-setup.sh of <dracut module>
1049 # or the "depends" script, if module-setup.sh is not found
1051 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1053 [[ -d $_moddir ]] || return 1
1054 if [[ ! -f $_moddir/module-setup.sh ]]; then
1055 # if we do not have a check script, we have no deps
1056 [[ -x $_moddir/check ]] || return 0
1060 unset check depends cmdline install installkernel
1062 . $_moddir/module-setup.sh
1065 unset check depends cmdline install installkernel
1070 # module_cmdline <dracut module>
1071 # execute the cmdline() function of module-setup.sh of <dracut module>
1072 # or the "cmdline" script, if module-setup.sh is not found
1074 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1076 [[ -d $_moddir ]] || return 1
1077 if [[ ! -f $_moddir/module-setup.sh ]]; then
1078 [[ -x $_moddir/cmdline ]] && . "$_moddir/cmdline"
1081 unset check depends cmdline install installkernel
1083 . $_moddir/module-setup.sh
1086 unset check depends cmdline install installkernel
1091 # module_install <dracut module>
1092 # execute the install() function of module-setup.sh of <dracut module>
1093 # or the "install" script, if module-setup.sh is not found
1095 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1097 [[ -d $_moddir ]] || return 1
1098 if [[ ! -f $_moddir/module-setup.sh ]]; then
1099 [[ -x $_moddir/install ]] && . "$_moddir/install"
1102 unset check depends cmdline install installkernel
1104 . $_moddir/module-setup.sh
1107 unset check depends cmdline install installkernel
1112 # module_installkernel <dracut module>
1113 # execute the installkernel() function of module-setup.sh of <dracut module>
1114 # or the "installkernel" script, if module-setup.sh is not found
1115 module_installkernel() {
1116 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1118 [[ -d $_moddir ]] || return 1
1119 if [[ ! -f $_moddir/module-setup.sh ]]; then
1120 [[ -x $_moddir/installkernel ]] && . "$_moddir/installkernel"
1123 unset check depends cmdline install installkernel
1124 installkernel() { true; }
1125 . $_moddir/module-setup.sh
1128 unset check depends cmdline install installkernel
1133 # check_mount <dracut module>
1134 # check_mount checks, if a dracut module is needed for the given
1135 # device and filesystem types in "${host_fs_types[@]}"
1138 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1142 [ "${#host_fs_types[*]}" -le 0 ] && return 1
1144 # If we are already scheduled to be loaded, no need to check again.
1145 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
1146 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
1148 # This should never happen, but...
1149 [[ -d $_moddir ]] || return 1
1151 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
1153 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
1157 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
1158 module_check_mount $_mod; ret=$?
1160 # explicit module, so also accept ret=255
1161 [[ $ret = 0 || $ret = 255 ]] || return 1
1163 # module not in our list
1164 if [[ $dracutmodules = all ]]; then
1165 # check, if we can and should install this module
1166 module_check_mount $_mod || return 1
1174 for _moddep in $(module_depends $_mod); do
1175 # handle deps as if they were manually added
1176 [[ " $add_dracutmodules " == *\ $_moddep\ * ]] || \
1177 add_dracutmodules+=" $_moddep "
1178 [[ " $force_add_dracutmodules " == *\ $_moddep\ * ]] || \
1179 force_add_dracutmodules+=" $_moddep "
1180 # if a module we depend on fail, fail also
1181 if ! check_module $_moddep; then
1182 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
1187 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
1188 mods_to_load+=" $_mod "
1193 # check_module <dracut module> [<use_as_dep>]
1194 # check if a dracut module is to be used in the initramfs process
1195 # if <use_as_dep> is set, then the process also keeps track
1196 # that the modules were checked for the dependency tracking process
1199 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1202 # If we are already scheduled to be loaded, no need to check again.
1203 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
1204 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
1206 # This should never happen, but...
1207 [[ -d $_moddir ]] || return 1
1209 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
1211 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
1212 dinfo "dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
1216 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
1217 if [[ " $force_add_dracutmodules " == *\ $_mod\ * ]]; then
1218 module_check $_mod 1; ret=$?
1220 module_check $_mod 0; ret=$?
1222 # explicit module, so also accept ret=255
1223 [[ $ret = 0 || $ret = 255 ]] || return 1
1225 # module not in our list
1226 if [[ $dracutmodules = all ]]; then
1227 # check, if we can and should install this module
1228 module_check $_mod || return 1
1235 for _moddep in $(module_depends $_mod); do
1236 # handle deps as if they were manually added
1237 [[ " $add_dracutmodules " == *\ $_moddep\ * ]] || \
1238 add_dracutmodules+=" $_moddep "
1239 [[ " $force_add_dracutmodules " == *\ $_moddep\ * ]] || \
1240 force_add_dracutmodules+=" $_moddep "
1241 # if a module we depend on fail, fail also
1242 if ! check_module $_moddep; then
1243 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
1248 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
1249 mods_to_load+=" $_mod "
1254 # for_each_module_dir <func>
1255 # execute "<func> <dracut module> 1"
1256 for_each_module_dir() {
1262 for _moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1263 _mod=${_moddir##*/}; _mod=${_mod#[0-9][0-9]}
1267 # Report any missing dracut modules, the user has specified
1268 _modcheck="$add_dracutmodules $force_add_dracutmodules"
1269 [[ $dracutmodules != all ]] && _modcheck="$m $dracutmodules"
1270 for _mod in $_modcheck; do
1271 [[ " $mods_to_load " == *\ $_mod\ * ]] && continue
1272 [[ " $omit_dracutmodules " == *\ $_mod\ * ]] && continue
1273 derror "dracut module '$_mod' cannot be found or installed."
1277 # Install a single kernel module along with any firmware it may require.
1278 # $1 = full path to kernel module to install
1279 install_kmod_with_fw() {
1280 # no need to go further if the module is already installed
1282 [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
1285 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -e "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}" ]]; then
1286 read ret < "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
1290 if [[ $omit_drivers ]]; then
1291 local _kmod=${1##*/}
1294 if [[ "$_kmod" =~ $omit_drivers ]]; then
1295 dinfo "Omitting driver $_kmod"
1298 if [[ "${1##*/lib/modules/$kernel/}" =~ $omit_drivers ]]; then
1299 dinfo "Omitting driver $_kmod"
1304 if [[ $silent_omit_drivers ]]; then
1305 local _kmod=${1##*/}
1308 [[ "$_kmod" =~ $silent_omit_drivers ]] && return 0
1309 [[ "${1##*/lib/modules/$kernel/}" =~ $silent_omit_drivers ]] && return 0
1312 inst_simple "$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
1314 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1315 [[ -d "$DRACUT_KERNEL_LAZY_HASHDIR" ]] && \
1316 echo $ret > "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
1317 (($ret != 0)) && return $ret
1319 local _modname=${1##*/} _fwdir _found _fw
1320 _modname=${_modname%.ko*}
1321 for _fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do
1323 for _fwdir in $fw_dir; do
1324 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1325 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1329 if [[ $_found != yes ]]; then
1330 if ! [[ -d $(echo /sys/module/${_modname//-/_}|{ read a b; echo $a; }) ]]; then
1331 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
1332 "\"${_modname}.ko\""
1334 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
1335 "\"${_modname}.ko\""
1342 # Do something with all the dependencies of a kernel module.
1343 # Note that kernel modules depend on themselves using the technique we use
1344 # $1 = function to call for each dependency we find
1345 # It will be passed the full path to the found kernel module
1346 # $2 = module to get dependencies for
1347 # rest of args = arguments to modprobe
1348 # _fderr specifies FD passed from surrounding scope
1349 for_each_kmod_dep() {
1350 local _func=$1 _kmod=$2 _cmd _modpath _options
1352 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
1353 while read _cmd _modpath _options; do
1354 [[ $_cmd = insmod ]] || continue
1355 $_func ${_modpath} || exit $?
1360 dracut_kernel_post() {
1361 local _moddirname=${srcmods%%/lib/modules/*}
1364 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" ]]; then
1365 xargs -r modprobe -a ${_moddirname+-d ${_moddirname}/} \
1366 --ignore-install --show-depends --set-version $kernel \
1367 < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" 2>/dev/null \
1369 | while read _cmd _modpath _options; do
1370 [[ $_cmd = insmod ]] || continue
1372 done > "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1375 if [[ $DRACUT_INSTALL ]] && [[ -z $_moddirname ]]; then
1376 xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1378 while read _modpath; do
1379 local _destpath=$_modpath
1380 [[ $_moddirname ]] && _destpath=${_destpath##$_moddirname/}
1381 _destpath=${_destpath##*/lib/modules/$kernel/}
1382 inst_simple "$_modpath" "/lib/modules/$kernel/${_destpath}" || exit $?
1383 done < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1386 _pid=$(jobs -p | while read a ; do printf ":$a";done)
1389 if [[ $DRACUT_INSTALL ]]; then
1390 xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" \
1391 | while read line; do
1392 for _fwdir in $fw_dir; do
1395 done | xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a -o
1397 for _fw in $(xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"); do
1398 for _fwdir in $fw_dir; do
1399 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1400 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1410 for _f in modules.builtin.bin modules.builtin; do
1411 [[ $srcmods/$_f ]] && break
1413 dfatal "No modules.builtin.bin and modules.builtin found!"
1417 for _f in modules.builtin.bin modules.builtin modules.order; do
1418 [[ $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f"
1421 # generate module dependencies for the initrd
1422 if [[ -d $initdir/lib/modules/$kernel ]] && \
1423 ! depmod -a -b "$initdir" $kernel; then
1424 dfatal "\"depmod -a $kernel\" failed."
1428 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && rm -fr -- "$DRACUT_KERNEL_LAZY_HASHDIR"
1431 module_is_host_only() {
1436 [[ " $add_drivers " == *\ ${_mod}\ * ]] && return 0
1438 # check if module is loaded
1439 for i in /sys/module/${_mod//-/_}; do
1440 [[ -d $i ]] && return 0
1443 # check if module is loadable on the current kernel
1444 # this covers the case, where a new module is introduced
1445 # or a module was renamed
1446 # or a module changed from builtin to a module
1447 modinfo -F filename "$_mod" &>/dev/null || return 0
1452 find_kernel_modules_by_path () {
1455 [[ -f "$srcmods/modules.dep" ]] || return 0
1459 while read a rest; do
1460 [[ $a = */$1/* ]] || continue
1461 printf "%s\n" "$srcmods/$a"
1462 done < "$srcmods/modules.dep"
1467 find_kernel_modules () {
1468 find_kernel_modules_by_path drivers
1471 # instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1472 # instmods [-c [-s]] <kernel subsystem>
1473 # install kernel modules along with all their dependencies.
1474 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1476 [[ $no_kernel = yes ]] && return
1477 # called [sub]functions inherit _fderr
1481 if [[ $1 = '-c' ]]; then
1486 if [[ $1 = '-s' ]]; then
1491 function inst1mod() {
1492 local _ret=0 _mod="$1"
1495 ( [[ "$_mpargs" ]] && echo $_mpargs
1496 find_kernel_modules_by_path "${_mod#=}" ) \
1500 --*) _mpargs+=" $_mod" ;;
1503 # if we are already installed, skip this module and go on
1505 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1506 [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko" ]]; then
1507 read _ret <"$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko"
1512 if [[ $omit_drivers ]] && [[ "$_mod" =~ $omit_drivers ]]; then
1513 dinfo "Omitting driver ${_mod##$srcmods}"
1517 # If we are building a host-specific initramfs and this
1518 # module is not already loaded, move on to the next one.
1520 && ! module_is_host_only "$_mod" \
1523 if [[ "$_check" = "yes" ]] || ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
1524 # We use '-d' option in modprobe only if modules prefix path
1525 # differs from default '/'. This allows us to use dracut with
1526 # old version of modprobe which doesn't have '-d' option.
1527 local _moddirname=${srcmods%%/lib/modules/*}
1528 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1530 # ok, load the module, all its dependencies, and any firmware
1532 for_each_kmod_dep install_kmod_with_fw $_mod \
1533 --set-version $kernel ${_moddirname} $_mpargs
1536 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1537 echo $_mod >> "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist"
1544 function instmods_1() {
1546 if (($# == 0)); then # filenames from stdin
1548 inst1mod "${_mod%.ko*}" || {
1549 if [[ "$_check" == "yes" ]]; then
1550 [[ "$_silent" == "no" ]] && dfatal "Failed to install module $_mod"
1556 while (($# > 0)); do # filenames as arguments
1557 inst1mod ${1%.ko*} || {
1558 if [[ "$_check" == "yes" ]]; then
1559 [[ "$_silent" == "no" ]] && dfatal "Failed to install module $1"
1568 local _ret _filter_not_found='FATAL: Module .* not found.'
1569 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1570 # redirections, but that would make dracut require bash4 at least.
1571 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1572 | while read line; do [[ "$line" =~ $_filter_not_found ]] || echo $line;done | derror
1577 # Only two values are returned: AMD or Intel
1580 if grep -qE AMD /proc/cpuinfo; then
1583 if grep -qE Intel /proc/cpuinfo; then
1589 # Get the hosts' ucode file based on the /proc/cpuinfo
1592 local family=`grep -E "cpu family" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1593 local model=`grep -E "model" /proc/cpuinfo |grep -v name | head -1 | sed s/.*:\ //`
1594 local stepping=`grep -E "stepping" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1596 if [[ "$(get_cpu_vendor)" == "AMD" ]]; then
1597 # If family greater or equal than 0x15
1598 if [[ $family -ge 21 ]]; then
1599 printf "microcode_amd_fam15h.bin"
1601 printf "microcode_amd.bin"
1604 if [[ "$(get_cpu_vendor)" == "Intel" ]]; then
1605 # The /proc/cpuinfo are in decimal.
1606 printf "%02x-%02x-%02x" ${family} ${model} ${stepping}