95fcoe: update fcoe interface check
[dracut.git] / dracut-functions.sh
blobb91d8dc8fff6690e3da6b808eaeb7ad9d7cae161
1 #!/bin/bash
3 # functions used by dracut and other tools.
5 # Copyright 2005-2009 Red Hat, Inc. All rights reserved.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 export LC_MESSAGES=C
22 if [[ $DRACUT_KERNEL_LAZY ]] && ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
23 if ! [[ -d "$initdir/.kernelmodseen" ]]; then
24 mkdir -p "$initdir/.kernelmodseen"
26 DRACUT_KERNEL_LAZY_HASHDIR="$initdir/.kernelmodseen"
29 if [[ $initdir ]] && ! [[ -d $initdir ]]; then
30 mkdir -p "$initdir"
33 # Generic substring function. If $2 is in $1, return 0.
34 strstr() { [[ $1 = *"$2"* ]]; }
35 # Generic glob matching function. If glob pattern $2 matches anywhere in $1, OK
36 strglobin() { [[ $1 = *$2* ]]; }
37 # Generic glob matching function. If glob pattern $2 matches all of $1, OK
38 strglob() { [[ $1 = $2 ]]; }
40 # helper function for check() in module-setup.sh
41 # to check for required installed binaries
42 # issues a standardized warning message
43 require_binaries() {
44 local _module_name="${moddir##*/}"
45 local _ret=0
47 if [[ "$1" = "-m" ]]; then
48 _module_name="$2"
49 shift 2
52 for cmd in "$@"; do
53 if ! find_binary "$cmd" &>/dev/null; then
54 dinfo "dracut module '${_module_name#[0-9][0-9]}' will not be installed, because command '$cmd' could not be found!"
55 ((_ret++))
57 done
58 return $_ret
61 require_any_binary() {
62 local _module_name="${moddir##*/}"
63 local _ret=1
65 if [[ "$1" = "-m" ]]; then
66 _module_name="$2"
67 shift 2
70 for cmd in "$@"; do
71 if find_binary "$cmd" &>/dev/null; then
72 _ret=0
73 break
75 done
77 if (( $_ret != 0 )); then
78 dinfo "$_module_name: Could not find any command of '$@'!"
79 return 1
82 return 0
85 # find a binary. If we were not passed the full path directly,
86 # search in the usual places to find the binary.
87 find_binary() {
88 if [[ -z ${1##/*} ]]; then
89 if [[ -x $1 ]] || { [[ "$1" == *.so* ]] && ldd "$1" &>/dev/null; }; then
90 printf "%s\n" "$1"
91 return 0
95 type -P "${1##*/}"
98 if ! [[ $dracutbasedir ]]; then
99 dracutbasedir=${BASH_SOURCE[0]%/*}
100 [[ $dracutbasedir = "dracut-functions" ]] && dracutbasedir="."
101 [[ $dracutbasedir ]] || dracutbasedir="."
102 dracutbasedir="$(readlink -f $dracutbasedir)"
105 ldconfig_paths()
107 local a i
108 declare -A a
109 for i in $(
110 ldconfig -pN 2>/dev/null | grep -F '=>' | grep -E -v '/(lib|lib64|usr/lib|usr/lib64)/[^/]*$' | while read a b c d; do
111 d=${d%/*}
112 printf "%s\n" "$d";
113 done
114 ); do
115 a["$i"]=1;
116 done;
117 printf "%s\n" ${!a[@]}
120 # Detect lib paths
121 if ! [[ $libdirs ]] ; then
122 if [[ "$(ldd /bin/sh)" == */lib64/* ]] &>/dev/null \
123 && [[ -d /lib64 ]]; then
124 libdirs+=" /lib64"
125 [[ -d /usr/lib64 ]] && libdirs+=" /usr/lib64"
126 else
127 libdirs+=" /lib"
128 [[ -d /usr/lib ]] && libdirs+=" /usr/lib"
131 libdirs+=" $(ldconfig_paths)"
133 export libdirs
136 if ! [[ $kernel ]]; then
137 kernel=$(uname -r)
138 export kernel
141 # Version comparision function. Assumes Linux style version scheme.
142 # $1 = version a
143 # $2 = comparision op (gt, ge, eq, le, lt, ne)
144 # $3 = version b
145 vercmp() {
146 local _n1=(${1//./ }) _op=$2 _n2=(${3//./ }) _i _res
148 for ((_i=0; ; _i++))
150 if [[ ! ${_n1[_i]}${_n2[_i]} ]]; then _res=0
151 elif ((${_n1[_i]:-0} > ${_n2[_i]:-0})); then _res=1
152 elif ((${_n1[_i]:-0} < ${_n2[_i]:-0})); then _res=2
153 else continue
155 break
156 done
158 case $_op in
159 gt) ((_res == 1));;
160 ge) ((_res != 2));;
161 eq) ((_res == 0));;
162 le) ((_res != 1));;
163 lt) ((_res == 2));;
164 ne) ((_res != 0));;
165 esac
168 srcmods="/lib/modules/$kernel/"
170 [[ $drivers_dir ]] && {
171 if ! command -v kmod &>/dev/null && vercmp "$(modprobe --version | cut -d' ' -f3)" lt 3.7; then
172 dfatal 'To use --kmoddir option module-init-tools >= 3.7 is required.'
173 exit 1
175 srcmods="$drivers_dir"
177 export srcmods
179 # is_func <command>
180 # Check whether $1 is a function.
181 is_func() {
182 [[ "$(type -t "$1")" = "function" ]]
185 if ! is_func dinfo >/dev/null 2>&1; then
186 . "$dracutbasedir/dracut-logger.sh"
187 dlog_init
190 if ! [[ $initdir ]]; then
191 dfatal "initdir not set"
192 exit 1
195 # export standard hookdirs
196 [[ $hookdirs ]] || {
197 hookdirs="cmdline pre-udev pre-trigger netroot "
198 hookdirs+="initqueue initqueue/settled initqueue/online initqueue/finished initqueue/timeout "
199 hookdirs+="pre-mount pre-pivot cleanup mount "
200 hookdirs+="emergency shutdown-emergency pre-shutdown shutdown "
201 export hookdirs
204 dracut_need_initqueue() {
205 >"$initdir/lib/dracut/need-initqueue"
208 dracut_module_included() {
209 [[ " $mods_to_load $modules_loaded " == *\ $*\ * ]]
212 # Create all subdirectories for given path without creating the last element.
213 # $1 = path
214 mksubdirs() {
215 [[ -e ${1%/*} ]] || mkdir -m 0755 -p -- "${1%/*}"
218 # Function prints global variables in format name=value line by line.
219 # $@ = list of global variables' name
220 print_vars() {
221 local _var _value
223 for _var in "$@"
225 eval printf -v _value "%s" \""\$$_var"\"
226 [[ ${_value} ]] && printf '%s="%s"\n' "$_var" "$_value"
227 done
230 # normalize_path <path>
231 # Prints the normalized path, where it removes any duplicated
232 # and trailing slashes.
233 # Example:
234 # $ normalize_path ///test/test//
235 # /test/test
236 normalize_path() {
237 shopt -q -s extglob
238 set -- "${1//+(\/)//}"
239 shopt -q -u extglob
240 printf "%s\n" "${1%/}"
243 # convert_abs_rel <from> <to>
244 # Prints the relative path, when creating a symlink to <to> from <from>.
245 # Example:
246 # $ convert_abs_rel /usr/bin/test /bin/test-2
247 # ../../bin/test-2
248 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
249 convert_abs_rel() {
250 local __current __absolute __abssize __cursize __newpath
251 local -i __i __level
253 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
255 # corner case #1 - self looping link
256 [[ "$1" == "$2" ]] && { printf "%s\n" "${1##*/}"; return; }
258 # corner case #2 - own dir link
259 [[ "${1%/*}" == "$2" ]] && { printf ".\n"; return; }
261 IFS="/" __current=($1)
262 IFS="/" __absolute=($2)
264 __abssize=${#__absolute[@]}
265 __cursize=${#__current[@]}
267 while [[ "${__absolute[__level]}" == "${__current[__level]}" ]]
269 (( __level++ ))
270 if (( __level > __abssize || __level > __cursize ))
271 then
272 break
274 done
276 for ((__i = __level; __i < __cursize-1; __i++))
278 if ((__i > __level))
279 then
280 __newpath=$__newpath"/"
282 __newpath=$__newpath".."
283 done
285 for ((__i = __level; __i < __abssize; __i++))
287 if [[ -n $__newpath ]]
288 then
289 __newpath=$__newpath"/"
291 __newpath=$__newpath${__absolute[__i]}
292 done
294 printf "%s\n" "$__newpath"
297 if [[ "$(ln --help)" == *--relative* ]]; then
298 ln_r() {
299 ln -sfnr "${initdir}/$1" "${initdir}/$2"
301 else
302 ln_r() {
303 local _source=$1
304 local _dest=$2
305 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
306 ln -sfn -- "$(convert_abs_rel "${_dest}" "${_source}")" "${initdir}/${_dest}"
310 # get_fs_env <device>
311 # Get and the ID_FS_TYPE variable from udev for a device.
312 # Example:
313 # $ get_fs_env /dev/sda2
314 # ext4
315 get_fs_env() {
316 local evalstr
317 local found
319 [[ $1 ]] || return
320 unset ID_FS_TYPE
321 ID_FS_TYPE=$(blkid -u filesystem -o export -- "$1" \
322 | while read line; do
323 if [[ "$line" == TYPE\=* ]]; then
324 printf "%s" "${line#TYPE=}";
325 exit 0;
327 done)
328 if [[ $ID_FS_TYPE ]]; then
329 printf "%s" "$ID_FS_TYPE"
330 return 0
332 return 1
335 # get_maj_min <device>
336 # Prints the major and minor of a device node.
337 # Example:
338 # $ get_maj_min /dev/sda2
339 # 8:2
340 get_maj_min() {
341 local _maj _min _majmin
342 _majmin="$(stat -L -c '%t:%T' "$1" 2>/dev/null)"
343 printf "%s" "$((0x${_majmin%:*})):$((0x${_majmin#*:}))"
347 # get_devpath_block <device>
348 # get the DEVPATH in /sys of a block device
349 get_devpath_block() {
350 local _majmin _i
351 _majmin=$(get_maj_min "$1")
353 for _i in /sys/block/*/dev /sys/block/*/*/dev; do
354 [[ -e "$_i" ]] || continue
355 if [[ "$_majmin" == "$(<"$_i")" ]]; then
356 printf "%s" "${_i%/dev}"
357 return 0
359 done
360 return 1
363 # get a persistent path from a device
364 get_persistent_dev() {
365 local i _tmp _dev
367 _dev=$(get_maj_min "$1")
368 [ -z "$_dev" ] && return
370 for i in \
371 /dev/mapper/* \
372 /dev/disk/${persistent_policy:-by-uuid}/* \
373 /dev/disk/by-uuid/* \
374 /dev/disk/by-label/* \
375 /dev/disk/by-partuuid/* \
376 /dev/disk/by-partlabel/* \
377 /dev/disk/by-id/* \
378 /dev/disk/by-path/* \
379 ; do
380 [[ -e "$i" ]] || continue
381 [[ $i == /dev/mapper/control ]] && continue
382 [[ $i == /dev/mapper/mpath* ]] && continue
383 _tmp=$(get_maj_min "$i")
384 if [ "$_tmp" = "$_dev" ]; then
385 printf -- "%s" "$i"
386 return
388 done
389 printf -- "%s" "$1"
392 expand_persistent_dev() {
393 local _dev=$1
395 case "$_dev" in
396 LABEL=*)
397 _dev="/dev/disk/by-label/${_dev#LABEL=}"
399 UUID=*)
400 _dev="${_dev#UUID=}"
401 _dev="${_dev,,}"
402 _dev="/dev/disk/by-uuid/${_dev}"
404 PARTUUID=*)
405 _dev="${_dev#PARTUUID=}"
406 _dev="${_dev,,}"
407 _dev="/dev/disk/by-partuuid/${_dev}"
409 PARTLABEL=*)
410 _dev="/dev/disk/by-partlabel/${_dev#PARTLABEL=}"
412 esac
413 printf "%s" "$_dev"
416 shorten_persistent_dev() {
417 local _dev="$1"
418 case "$_dev" in
419 /dev/disk/by-uuid/*)
420 printf "%s" "UUID=${_dev##*/}";;
421 /dev/disk/by-label/*)
422 printf "%s" "LABEL=${_dev##*/}";;
423 /dev/disk/by-partuuid/*)
424 printf "%s" "PARTUUID=${_dev##*/}";;
425 /dev/disk/by-partlabel/*)
426 printf "%s" "PARTLABEL=${_dev##*/}";;
428 printf "%s" "$_dev";;
429 esac
432 # find_block_device <mountpoint>
433 # Prints the major and minor number of the block device
434 # for a given mountpoint.
435 # Unless $use_fstab is set to "yes" the functions
436 # uses /proc/self/mountinfo as the primary source of the
437 # information and only falls back to /etc/fstab, if the mountpoint
438 # is not found there.
439 # Example:
440 # $ find_block_device /usr
441 # 8:4
442 find_block_device() {
443 local _majmin _dev _majmin _find_mpt
444 _find_mpt="$1"
445 if [[ $use_fstab != yes ]]; then
446 [[ -d $_find_mpt/. ]]
447 findmnt -e -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" | { \
448 while read _majmin _dev; do
449 if [[ -b $_dev ]]; then
450 if ! [[ $_majmin ]] || [[ $_majmin == 0:* ]]; then
451 _majmin=$(get_maj_min $_dev)
453 if [[ $_majmin ]]; then
454 printf "%s\n" "$_majmin"
455 else
456 printf "%s\n" "$_dev"
458 return 0
460 if [[ $_dev = *:* ]]; then
461 printf "%s\n" "$_dev"
462 return 0
464 done; return 1; } && return 0
466 # fall back to /etc/fstab
468 findmnt -e --fstab -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" | { \
469 while read _majmin _dev; do
470 if ! [[ $_dev ]]; then
471 _dev="$_majmin"
472 unset _majmin
474 if [[ -b $_dev ]]; then
475 [[ $_majmin ]] || _majmin=$(get_maj_min $_dev)
476 if [[ $_majmin ]]; then
477 printf "%s\n" "$_majmin"
478 else
479 printf "%s\n" "$_dev"
481 return 0
483 if [[ $_dev = *:* ]]; then
484 printf "%s\n" "$_dev"
485 return 0
487 done; return 1; } && return 0
489 return 1
492 # find_mp_fstype <mountpoint>
493 # Echo the filesystem type for a given mountpoint.
494 # /proc/self/mountinfo is taken as the primary source of information
495 # and /etc/fstab is used as a fallback.
496 # No newline is appended!
497 # Example:
498 # $ find_mp_fstype /;echo
499 # ext4
500 find_mp_fstype() {
501 local _fs
503 if [[ $use_fstab != yes ]]; then
504 findmnt -e -v -n -o 'FSTYPE' --target "$1" | { \
505 while read _fs; do
506 [[ $_fs ]] || continue
507 [[ $_fs = "autofs" ]] && continue
508 printf "%s" "$_fs"
509 return 0
510 done; return 1; } && return 0
513 findmnt --fstab -e -v -n -o 'FSTYPE' --target "$1" | { \
514 while read _fs; do
515 [[ $_fs ]] || continue
516 [[ $_fs = "autofs" ]] && continue
517 printf "%s" "$_fs"
518 return 0
519 done; return 1; } && return 0
521 return 1
524 # find_dev_fstype <device>
525 # Echo the filesystem type for a given device.
526 # /proc/self/mountinfo is taken as the primary source of information
527 # and /etc/fstab is used as a fallback.
528 # No newline is appended!
529 # Example:
530 # $ find_dev_fstype /dev/sda2;echo
531 # ext4
532 find_dev_fstype() {
533 local _find_dev _fs
534 _find_dev="$1"
535 if ! [[ "$_find_dev" = /dev* ]]; then
536 [[ -b "/dev/block/$_find_dev" ]] && _find_dev="/dev/block/$_find_dev"
539 if [[ $use_fstab != yes ]]; then
540 findmnt -e -v -n -o 'FSTYPE' --source "$_find_dev" | { \
541 while read _fs; do
542 [[ $_fs ]] || continue
543 [[ $_fs = "autofs" ]] && continue
544 printf "%s" "$_fs"
545 return 0
546 done; return 1; } && return 0
549 findmnt --fstab -e -v -n -o 'FSTYPE' --source "$_find_dev" | { \
550 while read _fs; do
551 [[ $_fs ]] || continue
552 [[ $_fs = "autofs" ]] && continue
553 printf "%s" "$_fs"
554 return 0
555 done; return 1; } && return 0
557 return 1
560 # find_mp_fsopts <mountpoint>
561 # Echo the filesystem options for a given mountpoint.
562 # /proc/self/mountinfo is taken as the primary source of information
563 # and /etc/fstab is used as a fallback.
564 # No newline is appended!
565 # Example:
566 # $ find_mp_fsopts /;echo
567 # rw,relatime,discard,data=ordered
568 find_mp_fsopts() {
569 if [[ $use_fstab != yes ]]; then
570 findmnt -e -v -n -o 'OPTIONS' --target "$1" 2>/dev/null && return 0
573 findmnt --fstab -e -v -n -o 'OPTIONS' --target "$1"
576 # find_dev_fsopts <device>
577 # Echo the filesystem options for a given device.
578 # /proc/self/mountinfo is taken as the primary source of information
579 # and /etc/fstab is used as a fallback.
580 # Example:
581 # $ find_dev_fsopts /dev/sda2
582 # rw,relatime,discard,data=ordered
583 find_dev_fsopts() {
584 local _find_dev _opts
585 _find_dev="$1"
586 if ! [[ "$_find_dev" = /dev* ]]; then
587 [[ -b "/dev/block/$_find_dev" ]] && _find_dev="/dev/block/$_find_dev"
590 if [[ $use_fstab != yes ]]; then
591 findmnt -e -v -n -o 'OPTIONS' --source "$_find_dev" 2>/dev/null && return 0
594 findmnt --fstab -e -v -n -o 'OPTIONS' --source "$_find_dev"
598 # finds the major:minor of the block device backing the root filesystem.
599 find_root_block_device() { find_block_device /; }
601 # for_each_host_dev_fs <func>
602 # Execute "<func> <dev> <filesystem>" for every "<dev> <fs>" pair found
603 # in ${host_fs_types[@]}
604 for_each_host_dev_fs()
606 local _func="$1"
607 local _dev
608 local _ret=1
610 [[ "${!host_fs_types[@]}" ]] || return 0
612 for _dev in "${!host_fs_types[@]}"; do
613 $_func "$_dev" "${host_fs_types[$_dev]}" && _ret=0
614 done
615 return $_ret
618 host_fs_all()
620 printf "%s\n" "${host_fs_types[@]}"
623 # Walk all the slave relationships for a given block device.
624 # Stop when our helper function returns success
625 # $1 = function to call on every found block device
626 # $2 = block device in major:minor format
627 check_block_and_slaves() {
628 local _x
629 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
630 if ! lvm_internal_dev $2; then "$1" $2 && return; fi
631 check_vol_slaves "$@" && return 0
632 if [[ -f /sys/dev/block/$2/../dev ]]; then
633 check_block_and_slaves $1 $(<"/sys/dev/block/$2/../dev") && return 0
635 [[ -d /sys/dev/block/$2/slaves ]] || return 1
636 for _x in /sys/dev/block/$2/slaves/*/dev; do
637 [[ -f $_x ]] || continue
638 check_block_and_slaves $1 $(<"$_x") && return 0
639 done
640 return 1
643 check_block_and_slaves_all() {
644 local _x _ret=1
645 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
646 if ! lvm_internal_dev $2 && "$1" $2; then
647 _ret=0
649 check_vol_slaves "$@" && return 0
650 if [[ -f /sys/dev/block/$2/../dev ]]; then
651 check_block_and_slaves_all $1 $(<"/sys/dev/block/$2/../dev") && _ret=0
653 [[ -d /sys/dev/block/$2/slaves ]] || return 1
654 for _x in /sys/dev/block/$2/slaves/*/dev; do
655 [[ -f $_x ]] || continue
656 check_block_and_slaves_all $1 $(<"$_x") && _ret=0
657 done
658 return $_ret
660 # for_each_host_dev_and_slaves <func>
661 # Execute "<func> <dev>" for every "<dev>" found
662 # in ${host_devs[@]} and their slaves
663 for_each_host_dev_and_slaves_all()
665 local _func="$1"
666 local _dev
667 local _ret=1
669 [[ "${host_devs[@]}" ]] || return 0
671 for _dev in ${host_devs[@]}; do
672 [[ -b "$_dev" ]] || continue
673 if check_block_and_slaves_all $_func $(get_maj_min $_dev); then
674 _ret=0
676 done
677 return $_ret
680 for_each_host_dev_and_slaves()
682 local _func="$1"
683 local _dev
685 [[ "${host_devs[@]}" ]] || return 0
687 for _dev in ${host_devs[@]}; do
688 [[ -b "$_dev" ]] || continue
689 check_block_and_slaves $_func $(get_maj_min $_dev) && return 0
690 done
691 return 1
694 # ugly workaround for the lvm design
695 # There is no volume group device,
696 # so, there are no slave devices for volume groups.
697 # Logical volumes only have the slave devices they really live on,
698 # but you cannot create the logical volume without the volume group.
699 # And the volume group might be bigger than the devices the LV needs.
700 check_vol_slaves() {
701 local _lv _vg _pv _dm
702 for i in /dev/mapper/*; do
703 [[ $i == /dev/mapper/control ]] && continue
704 _lv=$(get_maj_min $i)
705 _dm=/sys/dev/block/$_lv/dm
706 [[ -f $_dm/uuid && $(<$_dm/uuid) =~ LVM-* ]] || continue
707 if [[ $_lv = $2 ]]; then
708 _vg=$(lvm lvs --noheadings -o vg_name $i 2>/dev/null)
709 # strip space
710 _vg=$(printf "%s\n" "$_vg")
711 if [[ $_vg ]]; then
712 for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2>/dev/null)
714 check_block_and_slaves $1 $(get_maj_min $_pv) && return 0
715 done
718 done
719 return 1
722 # fs_get_option <filesystem options> <search for option>
723 # search for a specific option in a bunch of filesystem options
724 # and return the value
725 fs_get_option() {
726 local _fsopts=$1
727 local _option=$2
728 local OLDIFS="$IFS"
729 IFS=,
730 set -- $_fsopts
731 IFS="$OLDIFS"
732 while [ $# -gt 0 ]; do
733 case $1 in
734 $_option=*)
735 echo ${1#${_option}=}
736 break
737 esac
738 shift
739 done
742 if ! [[ $DRACUT_INSTALL ]]; then
743 DRACUT_INSTALL=$(find_binary dracut-install)
746 if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install ]]; then
747 DRACUT_INSTALL=$dracutbasedir/dracut-install
748 elif ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/install/dracut-install ]]; then
749 DRACUT_INSTALL=$dracutbasedir/install/dracut-install
752 if ! [[ -x $DRACUT_INSTALL ]]; then
753 dfatal "dracut-install not found!"
754 exit 10
757 [[ $DRACUT_RESOLVE_LAZY ]] || export DRACUT_RESOLVE_DEPS=1
758 inst_dir() {
759 [[ -e ${initdir}/"$1" ]] && return 0 # already there
760 $DRACUT_INSTALL ${initdir:+-D "$initdir"} -d "$@"
761 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} -d "$@" || :
764 inst() {
765 local _hostonly_install
766 if [[ "$1" == "-H" ]]; then
767 _hostonly_install="-H"
768 shift
770 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
771 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
772 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
775 inst_simple() {
776 local _hostonly_install
777 if [[ "$1" == "-H" ]]; then
778 _hostonly_install="-H"
779 shift
781 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
782 [[ -e $1 ]] || return 1 # no source
783 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${_hostonly_install:+-H} "$@"
784 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${_hostonly_install:+-H} "$@" || :
787 inst_symlink() {
788 local _hostonly_install
789 if [[ "$1" == "-H" ]]; then
790 _hostonly_install="-H"
791 shift
793 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
794 [[ -L $1 ]] || return 1
795 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
796 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
799 inst_multiple() {
800 local _ret
801 $DRACUT_INSTALL ${initdir:+-D "$initdir"} -a ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
802 _ret=$?
803 (($_ret != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} -a ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
804 return $_ret
807 dracut_install() {
808 inst_multiple "$@"
811 inst_library() {
812 local _hostonly_install
813 if [[ "$1" == "-H" ]]; then
814 _hostonly_install="-H"
815 shift
817 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
818 [[ -e $1 ]] || return 1 # no source
819 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
820 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
823 inst_binary() {
824 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
825 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@" || :
828 inst_script() {
829 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
830 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@" || :
833 mark_hostonly() {
834 for i in "$@"; do
835 echo "$i" >> "$initdir/lib/dracut/hostonly-files"
836 done
839 # find symlinks linked to given library file
840 # $1 = library file
841 # Function searches for symlinks by stripping version numbers appended to
842 # library filename, checks if it points to the same target and finally
843 # prints the list of symlinks to stdout.
845 # Example:
846 # rev_lib_symlinks libfoo.so.8.1
847 # output: libfoo.so.8 libfoo.so
848 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
849 rev_lib_symlinks() {
850 [[ ! $1 ]] && return 0
852 local fn="$1" orig="$(readlink -f "$1")" links=''
854 [[ ${fn} == *.so.* ]] || return 1
856 until [[ ${fn##*.} == so ]]; do
857 fn="${fn%.*}"
858 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
859 done
861 echo "${links}"
864 # attempt to install any programs specified in a udev rule
865 inst_rule_programs() {
866 local _prog _bin
868 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
869 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
870 _bin=""
871 if [ -x ${udevdir}/$_prog ]; then
872 _bin=${udevdir}/$_prog
873 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
874 _bin=$(find_binary "$_prog") || {
875 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
876 continue;
880 [[ $_bin ]] && inst_binary "$_bin"
881 done
883 if grep -qE 'RUN[+=]=?"[^ "]+' "$1"; then
884 for _prog in $(grep -E 'RUN[+=]=?"[^ "]+' "$1" | sed -r 's/.*RUN[+=]=?"([^ "]+).*/\1/'); do
885 _bin=""
886 if [ -x ${udevdir}/$_prog ]; then
887 _bin=${udevdir}/$_prog
888 elif [[ "${_prog/\$env\{/}" == "$_prog" ]] && [[ "${_prog}" != "/sbin/initqueue" ]]; then
889 _bin=$(find_binary "$_prog") || {
890 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
891 continue;
895 [[ $_bin ]] && inst_binary "$_bin"
896 done
898 if grep -qE 'IMPORT\{program\}==?"[^ "]+' "$1"; then
899 for _prog in $(grep -E 'IMPORT\{program\}==?"[^ "]+' "$1" | sed -r 's/.*IMPORT\{program\}==?"([^ "]+).*/\1/'); do
900 _bin=""
901 if [ -x ${udevdir}/$_prog ]; then
902 _bin=${udevdir}/$_prog
903 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
904 _bin=$(find_binary "$_prog") || {
905 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
906 continue;
910 [[ $_bin ]] && dracut_install "$_bin"
911 done
915 # attempt to install any programs specified in a udev rule
916 inst_rule_group_owner() {
917 local i
919 if grep -qE 'OWNER=?"[^ "]+' "$1"; then
920 for i in $(grep -E 'OWNER=?"[^ "]+' "$1" | sed -r 's/.*OWNER=?"([^ "]+).*/\1/'); do
921 if ! egrep -q "^$i:" "$initdir/etc/passwd" 2>/dev/null; then
922 egrep "^$i:" /etc/passwd 2>/dev/null >> "$initdir/etc/passwd"
924 done
926 if grep -qE 'GROUP=?"[^ "]+' "$1"; then
927 for i in $(grep -E 'GROUP=?"[^ "]+' "$1" | sed -r 's/.*GROUP=?"([^ "]+).*/\1/'); do
928 if ! egrep -q "^$i:" "$initdir/etc/group" 2>/dev/null; then
929 egrep "^$i:" /etc/group 2>/dev/null >> "$initdir/etc/group"
931 done
935 inst_rule_initqueue() {
936 if grep -q -F initqueue "$1"; then
937 dracut_need_initqueue
941 # udev rules always get installed in the same place, so
942 # create a function to install them to make life simpler.
943 inst_rules() {
944 local _target=/etc/udev/rules.d _rule _found
946 inst_dir "${udevdir}/rules.d"
947 inst_dir "$_target"
948 for _rule in "$@"; do
949 if [ "${_rule#/}" = "$_rule" ]; then
950 for r in ${udevdir}/rules.d ${hostonly:+/etc/udev/rules.d}; do
951 if [[ -e $r/$_rule ]]; then
952 _found="$r/$_rule"
953 inst_rule_programs "$_found"
954 inst_rule_group_owner "$_found"
955 inst_rule_initqueue "$_found"
956 inst_simple "$_found"
958 done
960 for r in '' $dracutbasedir/rules.d/; do
961 # skip rules without an absolute path
962 [[ "${r}$_rule" != /* ]] && continue
964 if [[ -f ${r}$_rule ]]; then
965 _found="${r}$_rule"
966 inst_rule_programs "$_found"
967 inst_rule_group_owner "$_found"
968 inst_rule_initqueue "$_found"
969 inst_simple "$_found" "$_target/${_found##*/}"
971 done
972 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
973 done
976 inst_rules_wildcard() {
977 local _target=/etc/udev/rules.d _rule _found
979 inst_dir "${udevdir}/rules.d"
980 inst_dir "$_target"
981 for _rule in ${udevdir}/rules.d/$1 ${dracutbasedir}/rules.d/$1 ; do
982 if [[ -e $_rule ]]; then
983 inst_rule_programs "$_rule"
984 inst_rule_group_owner "$_rule"
985 inst_rule_initqueue "$_rule"
986 inst_simple "$_rule"
987 _found=$_rule
989 done
990 if [ -n ${hostonly} ] ; then
991 for _rule in ${_target}/$1 ; do
992 if [[ -f $_rule ]]; then
993 inst_rule_programs "$_rule"
994 inst_rule_group_owner "$_rule"
995 inst_rule_initqueue "$_rule"
996 inst_simple "$_rule"
997 _found=$_rule
999 done
1001 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
1004 prepare_udev_rules() {
1005 [ -z "$UDEVVERSION" ] && export UDEVVERSION=$(udevadm --version)
1007 for f in "$@"; do
1008 f="${initdir}/etc/udev/rules.d/$f"
1009 [ -e "$f" ] || continue
1010 while read line; do
1011 if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
1012 if [ $UDEVVERSION -ge 174 ]; then
1013 printf '%sIMPORT{builtin}="path_id"\n' "${line%%IMPORT PATH_ID}"
1014 else
1015 printf '%sIMPORT{program}="path_id %%p"\n' "${line%%IMPORT PATH_ID}"
1017 elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
1018 if [ $UDEVVERSION -ge 176 ]; then
1019 printf '%sIMPORT{builtin}="blkid"\n' "${line%%IMPORT BLKID}"
1020 else
1021 printf '%sIMPORT{program}="/sbin/blkid -o udev -p $tempnode"\n' "${line%%IMPORT BLKID}"
1023 else
1024 echo "$line"
1026 done < "${f}" > "${f}.new"
1027 mv "${f}.new" "$f"
1028 done
1031 # install function specialized for hooks
1032 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
1033 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
1034 inst_hook() {
1035 if ! [[ -f $3 ]]; then
1036 dfatal "Cannot install a hook ($3) that does not exist."
1037 dfatal "Aborting initrd creation."
1038 exit 1
1039 elif ! [[ "$hookdirs" == *$1* ]]; then
1040 dfatal "No such hook type $1. Aborting initrd creation."
1041 exit 1
1043 inst_simple "$3" "/lib/dracut/hooks/${1}/${2}-${3##*/}"
1046 # install any of listed files
1048 # If first argument is '-d' and second some destination path, first accessible
1049 # source is installed into this path, otherwise it will installed in the same
1050 # path as source. If none of listed files was installed, function return 1.
1051 # On first successful installation it returns with 0 status.
1053 # Example:
1055 # inst_any -d /bin/foo /bin/bar /bin/baz
1057 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
1058 # initramfs.
1059 inst_any() {
1060 local to f
1062 [[ $1 = '-d' ]] && to="$2" && shift 2
1064 for f in "$@"; do
1065 if [[ -e $f ]]; then
1066 [[ $to ]] && inst "$f" "$to" && return 0
1067 inst "$f" && return 0
1069 done
1071 return 1
1075 # inst_libdir_file [-n <pattern>] <file> [<file>...]
1076 # Install a <file> located on a lib directory to the initramfs image
1077 # -n <pattern> install matching files
1078 inst_libdir_file() {
1079 local _files
1080 if [[ "$1" == "-n" ]]; then
1081 local _pattern=$2
1082 shift 2
1083 for _dir in $libdirs; do
1084 for _i in "$@"; do
1085 for _f in "$_dir"/$_i; do
1086 [[ "$_f" =~ $_pattern ]] || continue
1087 [[ -e "$_f" ]] && _files+="$_f "
1088 done
1089 done
1090 done
1091 else
1092 for _dir in $libdirs; do
1093 for _i in "$@"; do
1094 for _f in "$_dir"/$_i; do
1095 [[ -e "$_f" ]] && _files+="$_f "
1096 done
1097 done
1098 done
1100 [[ $_files ]] && inst_multiple $_files
1104 # install function decompressing the target and handling symlinks
1105 # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
1107 # Function install targets in the same paths inside overlay but decompressed
1108 # and without extensions (.gz, .bz2).
1109 inst_decompress() {
1110 local _src _cmd
1112 for _src in $@
1114 case ${_src} in
1115 *.gz) _cmd='gzip -f -d' ;;
1116 *.bz2) _cmd='bzip2 -d' ;;
1117 *) return 1 ;;
1118 esac
1119 inst_simple ${_src}
1120 # Decompress with chosen tool. We assume that tool changes name e.g.
1121 # from 'name.gz' to 'name'.
1122 ${_cmd} "${initdir}${_src}"
1123 done
1126 # It's similar to above, but if file is not compressed, performs standard
1127 # install.
1128 # $@ = list of files
1129 inst_opt_decompress() {
1130 local _src
1132 for _src in $@
1134 inst_decompress "${_src}" || inst "${_src}"
1135 done
1138 # module_check <dracut module>
1139 # execute the check() function of module-setup.sh of <dracut module>
1140 # or the "check" script, if module-setup.sh is not found
1141 # "check $hostonly" is called
1142 module_check() {
1143 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1144 local _ret
1145 local _forced=0
1146 local _hostonly=$hostonly
1147 [ $# -eq 2 ] && _forced=$2
1148 [[ -d $_moddir ]] || return 1
1149 if [[ ! -f $_moddir/module-setup.sh ]]; then
1150 # if we do not have a check script, we are unconditionally included
1151 [[ -x $_moddir/check ]] || return 0
1152 [ $_forced -ne 0 ] && unset hostonly
1153 $_moddir/check $hostonly
1154 _ret=$?
1155 else
1156 unset check depends cmdline install installkernel
1157 check() { true; }
1158 . $_moddir/module-setup.sh
1159 is_func check || return 0
1160 [ $_forced -ne 0 ] && unset hostonly
1161 moddir=$_moddir check $hostonly
1162 _ret=$?
1163 unset check depends cmdline install installkernel
1165 hostonly=$_hostonly
1166 return $_ret
1169 # module_check_mount <dracut module>
1170 # execute the check() function of module-setup.sh of <dracut module>
1171 # or the "check" script, if module-setup.sh is not found
1172 # "mount_needs=1 check 0" is called
1173 module_check_mount() {
1174 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1175 local _ret
1176 mount_needs=1
1177 [[ -d $_moddir ]] || return 1
1178 if [[ ! -f $_moddir/module-setup.sh ]]; then
1179 # if we do not have a check script, we are unconditionally included
1180 [[ -x $_moddir/check ]] || return 0
1181 mount_needs=1 $_moddir/check 0
1182 _ret=$?
1183 else
1184 unset check depends cmdline install installkernel
1185 check() { false; }
1186 . $_moddir/module-setup.sh
1187 moddir=$_moddir check 0
1188 _ret=$?
1189 unset check depends cmdline install installkernel
1191 unset mount_needs
1192 return $_ret
1195 # module_depends <dracut module>
1196 # execute the depends() function of module-setup.sh of <dracut module>
1197 # or the "depends" script, if module-setup.sh is not found
1198 module_depends() {
1199 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1200 local _ret
1201 [[ -d $_moddir ]] || return 1
1202 if [[ ! -f $_moddir/module-setup.sh ]]; then
1203 # if we do not have a check script, we have no deps
1204 [[ -x $_moddir/check ]] || return 0
1205 $_moddir/check -d
1206 return $?
1207 else
1208 unset check depends cmdline install installkernel
1209 depends() { true; }
1210 . $_moddir/module-setup.sh
1211 moddir=$_moddir depends
1212 _ret=$?
1213 unset check depends cmdline install installkernel
1214 return $_ret
1218 # module_cmdline <dracut module>
1219 # execute the cmdline() function of module-setup.sh of <dracut module>
1220 # or the "cmdline" script, if module-setup.sh is not found
1221 module_cmdline() {
1222 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1223 local _ret
1224 [[ -d $_moddir ]] || return 1
1225 if [[ ! -f $_moddir/module-setup.sh ]]; then
1226 [[ -x $_moddir/cmdline ]] && . "$_moddir/cmdline"
1227 return $?
1228 else
1229 unset check depends cmdline install installkernel
1230 cmdline() { true; }
1231 . $_moddir/module-setup.sh
1232 moddir=$_moddir cmdline
1233 _ret=$?
1234 unset check depends cmdline install installkernel
1235 return $_ret
1239 # module_install <dracut module>
1240 # execute the install() function of module-setup.sh of <dracut module>
1241 # or the "install" script, if module-setup.sh is not found
1242 module_install() {
1243 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1244 local _ret
1245 [[ -d $_moddir ]] || return 1
1246 if [[ ! -f $_moddir/module-setup.sh ]]; then
1247 [[ -x $_moddir/install ]] && . "$_moddir/install"
1248 return $?
1249 else
1250 unset check depends cmdline install installkernel
1251 install() { true; }
1252 . $_moddir/module-setup.sh
1253 moddir=$_moddir install
1254 _ret=$?
1255 unset check depends cmdline install installkernel
1256 return $_ret
1260 # module_installkernel <dracut module>
1261 # execute the installkernel() function of module-setup.sh of <dracut module>
1262 # or the "installkernel" script, if module-setup.sh is not found
1263 module_installkernel() {
1264 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1265 local _ret
1266 [[ -d $_moddir ]] || return 1
1267 if [[ ! -f $_moddir/module-setup.sh ]]; then
1268 [[ -x $_moddir/installkernel ]] && . "$_moddir/installkernel"
1269 return $?
1270 else
1271 unset check depends cmdline install installkernel
1272 installkernel() { true; }
1273 . $_moddir/module-setup.sh
1274 moddir=$_moddir installkernel
1275 _ret=$?
1276 unset check depends cmdline install installkernel
1277 return $_ret
1281 # check_mount <dracut module>
1282 # check_mount checks, if a dracut module is needed for the given
1283 # device and filesystem types in "${host_fs_types[@]}"
1284 check_mount() {
1285 local _mod=$1
1286 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1287 local _ret
1288 local _moddep
1290 [ "${#host_fs_types[*]}" -le 0 ] && return 1
1292 # If we are already scheduled to be loaded, no need to check again.
1293 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
1294 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
1296 # This should never happen, but...
1297 [[ -d $_moddir ]] || return 1
1299 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
1301 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
1302 return 1
1305 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
1306 module_check_mount $_mod; ret=$?
1308 # explicit module, so also accept ret=255
1309 [[ $ret = 0 || $ret = 255 ]] || return 1
1310 else
1311 # module not in our list
1312 if [[ $dracutmodules = all ]]; then
1313 # check, if we can and should install this module
1314 module_check_mount $_mod || return 1
1315 else
1316 # skip this module
1317 return 1
1321 for _moddep in $(module_depends $_mod); do
1322 # handle deps as if they were manually added
1323 [[ " $dracutmodules " == *\ $_mod\ * ]] \
1324 && [[ " $dracutmodules " != *\ $_moddep\ * ]] \
1325 && dracutmodules+=" $_moddep "
1326 [[ " $add_dracutmodules " == *\ $_mod\ * ]] \
1327 && [[ " $add_dracutmodules " != *\ $_moddep\ * ]] \
1328 && add_dracutmodules+=" $_moddep "
1329 [[ " $force_add_dracutmodules " == *\ $_mod\ * ]] \
1330 && [[ " $force_add_dracutmodules " != *\ $_moddep\ * ]] \
1331 && force_add_dracutmodules+=" $_moddep "
1332 # if a module we depend on fail, fail also
1333 if ! check_module $_moddep; then
1334 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
1335 return 1
1337 done
1339 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
1340 mods_to_load+=" $_mod "
1342 return 0
1345 # check_module <dracut module> [<use_as_dep>]
1346 # check if a dracut module is to be used in the initramfs process
1347 # if <use_as_dep> is set, then the process also keeps track
1348 # that the modules were checked for the dependency tracking process
1349 check_module() {
1350 local _mod=$1
1351 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1352 local _ret
1353 local _moddep
1354 # If we are already scheduled to be loaded, no need to check again.
1355 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
1356 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
1358 # This should never happen, but...
1359 [[ -d $_moddir ]] || return 1
1361 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
1363 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
1364 dinfo "dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
1365 return 1
1368 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
1369 if [[ " $dracutmodules $force_add_dracutmodules " == *\ $_mod\ * ]]; then
1370 module_check $_mod 1; ret=$?
1371 else
1372 module_check $_mod 0; ret=$?
1374 # explicit module, so also accept ret=255
1375 [[ $ret = 0 || $ret = 255 ]] || return 1
1376 else
1377 # module not in our list
1378 if [[ $dracutmodules = all ]]; then
1379 # check, if we can and should install this module
1380 module_check $_mod; ret=$?
1381 if [[ $ret != 0 ]]; then
1382 [[ $2 ]] && return 1
1383 [[ $ret != 255 ]] && return 1
1385 else
1386 # skip this module
1387 return 1
1391 for _moddep in $(module_depends $_mod); do
1392 # handle deps as if they were manually added
1393 [[ " $dracutmodules " == *\ $_mod\ * ]] \
1394 && [[ " $dracutmodules " != *\ $_moddep\ * ]] \
1395 && dracutmodules+=" $_moddep "
1396 [[ " $add_dracutmodules " == *\ $_mod\ * ]] \
1397 && [[ " $add_dracutmodules " != *\ $_moddep\ * ]] \
1398 && add_dracutmodules+=" $_moddep "
1399 [[ " $force_add_dracutmodules " == *\ $_mod\ * ]] \
1400 && [[ " $force_add_dracutmodules " != *\ $_moddep\ * ]] \
1401 && force_add_dracutmodules+=" $_moddep "
1402 # if a module we depend on fail, fail also
1403 if ! check_module $_moddep; then
1404 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
1405 return 1
1407 done
1409 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
1410 mods_to_load+=" $_mod "
1412 return 0
1415 # for_each_module_dir <func>
1416 # execute "<func> <dracut module> 1"
1417 for_each_module_dir() {
1418 local _modcheck
1419 local _mod
1420 local _moddir
1421 local _func
1422 _func=$1
1423 for _moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1424 [[ -d $_moddir ]] || continue;
1425 [[ -e $_moddir/install || -e $_moddir/installkernel || \
1426 -e $_moddir/module-setup.sh ]] || continue
1427 _mod=${_moddir##*/}; _mod=${_mod#[0-9][0-9]}
1428 $_func $_mod 1
1429 done
1431 # Report any missing dracut modules, the user has specified
1432 _modcheck="$add_dracutmodules $force_add_dracutmodules"
1433 [[ $dracutmodules != all ]] && _modcheck="$_modcheck $dracutmodules"
1434 for _mod in $_modcheck; do
1435 [[ " $mods_to_load " == *\ $_mod\ * ]] && continue
1437 [[ " $force_add_dracutmodules " != *\ $_mod\ * ]] \
1438 && [[ " $dracutmodules " != *\ $_mod\ * ]] \
1439 && [[ " $omit_dracutmodules " == *\ $_mod\ * ]] \
1440 && continue
1442 derror "dracut module '$_mod' cannot be found or installed."
1443 [[ " $force_add_dracutmodules " == *\ $_mod\ * ]] && exit 1
1444 [[ " $dracutmodules " == *\ $_mod\ * ]] && exit 1
1445 [[ " $add_dracutmodules " == *\ $_mod\ * ]] && exit 1
1446 done
1449 # Install a single kernel module along with any firmware it may require.
1450 # $1 = full path to kernel module to install
1451 install_kmod_with_fw() {
1452 # no need to go further if the module is already installed
1454 [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
1455 && return 0
1457 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -e "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}" ]]; then
1458 read ret < "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
1459 return $ret
1462 if [[ $omit_drivers ]]; then
1463 local _kmod=${1##*/}
1464 _kmod=${_kmod%.ko}
1465 _kmod=${_kmod/-/_}
1466 if [[ "$_kmod" =~ $omit_drivers ]]; then
1467 dinfo "Omitting driver $_kmod"
1468 return 0
1470 if [[ "${1##*/lib/modules/$kernel/}" =~ $omit_drivers ]]; then
1471 dinfo "Omitting driver $_kmod"
1472 return 0
1476 if [[ $silent_omit_drivers ]]; then
1477 local _kmod=${1##*/}
1478 _kmod=${_kmod%.ko}
1479 _kmod=${_kmod/-/_}
1480 [[ "$_kmod" =~ $silent_omit_drivers ]] && return 0
1481 [[ "${1##*/lib/modules/$kernel/}" =~ $silent_omit_drivers ]] && return 0
1484 inst_simple "$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
1485 ret=$?
1486 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1487 [[ -d "$DRACUT_KERNEL_LAZY_HASHDIR" ]] && \
1488 echo $ret > "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
1489 (($ret != 0)) && return $ret
1491 local _modname=${1##*/} _fwdir _found _fw
1492 _modname=${_modname%.ko*}
1493 for _fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do
1494 _found=''
1495 for _fwdir in $fw_dir; do
1496 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1497 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1498 _found=yes
1500 done
1501 if [[ $_found != yes ]]; then
1502 if ! [[ -d $(echo /sys/module/${_modname//-/_}|{ read a b; echo $a; }) ]]; then
1503 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
1504 "\"${_modname}.ko\""
1505 else
1506 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
1507 "\"${_modname}.ko\""
1510 done
1511 return 0
1514 # Do something with all the dependencies of a kernel module.
1515 # Note that kernel modules depend on themselves using the technique we use
1516 # $1 = function to call for each dependency we find
1517 # It will be passed the full path to the found kernel module
1518 # $2 = module to get dependencies for
1519 # rest of args = arguments to modprobe
1520 # _fderr specifies FD passed from surrounding scope
1521 for_each_kmod_dep() {
1522 local _func=$1 _kmod=$2 _cmd _modpath _options
1523 shift 2
1524 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
1525 while read _cmd _modpath _options; do
1526 [[ $_cmd = insmod ]] || continue
1527 $_func ${_modpath} || exit $?
1528 done
1532 dracut_kernel_post() {
1533 local _moddirname=${srcmods%%/lib/modules/*}
1534 local _pid
1536 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" ]]; then
1537 xargs -r modprobe -a ${_moddirname:+-d ${_moddirname}/} \
1538 --ignore-install --show-depends --set-version $kernel \
1539 < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" 2>/dev/null \
1540 | sort -u \
1541 | while read _cmd _modpath _options; do
1542 [[ $_cmd = insmod ]] || continue
1543 echo "$_modpath"
1544 done > "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1547 if [[ $DRACUT_INSTALL ]] && [[ -z $_moddirname ]]; then
1548 xargs -r $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} -a < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1549 else
1550 while read _modpath; do
1551 local _destpath=$_modpath
1552 [[ $_moddirname ]] && _destpath=${_destpath##$_moddirname/}
1553 _destpath=${_destpath##*/lib/modules/$kernel/}
1554 inst_simple "$_modpath" "/lib/modules/$kernel/${_destpath}" || exit $?
1555 done < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1558 _pid=$(jobs -p | while read a ; do printf ":$a";done)
1559 _pid=${_pid##*:}
1561 if [[ $DRACUT_INSTALL ]]; then
1562 xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" \
1563 | while read line; do
1564 for _fwdir in $fw_dir; do
1565 echo $_fwdir/$line;
1566 done;
1567 done | xargs -r $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} -a -o
1568 else
1569 for _fw in $(xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"); do
1570 for _fwdir in $fw_dir; do
1571 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1572 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1573 break
1575 done
1576 done
1579 wait $_pid
1582 for _f in modules.builtin.bin modules.builtin modules.order; do
1583 [[ $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f"
1584 done
1586 # generate module dependencies for the initrd
1587 if [[ -d $initdir/lib/modules/$kernel ]] && \
1588 ! depmod -a -b "$initdir" $kernel; then
1589 dfatal "\"depmod -a $kernel\" failed."
1590 exit 1
1593 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && rm -fr -- "$DRACUT_KERNEL_LAZY_HASHDIR"
1596 [[ "$kernel_current" ]] || export kernel_current=$(uname -r)
1598 module_is_host_only() {
1599 local _mod=$1
1600 local _modenc a i _k _s _v _aliases
1601 _mod=${_mod##*/}
1602 _mod=${_mod%.ko}
1603 _modenc=${_mod//-/_}
1605 [[ " $add_drivers " == *\ ${_mod}\ * ]] && return 0
1607 # check if module is loaded
1608 [[ ${host_modules["$_modenc"]} ]] && return 0
1610 [[ "$kernel_current" ]] || export kernel_current=$(uname -r)
1612 if [[ "$kernel_current" != "$kernel" ]]; then
1613 # check if module is loadable on the current kernel
1614 # this covers the case, where a new module is introduced
1615 # or a module was renamed
1616 # or a module changed from builtin to a module
1618 if [[ -d /lib/modules/$kernel_current ]]; then
1619 # if the modinfo can be parsed, but the module
1620 # is not loaded, then we can safely return 1
1621 modinfo -F filename "$_mod" &>/dev/null && return 1
1624 _aliases=$(modinfo -k $kernel -F alias $_mod 2>/dev/null)
1626 # if the module has no aliases, install it
1627 [[ $_aliases ]] || return 0
1629 # finally check all modalias
1630 for a in $_aliases; do
1631 for i in "${!host_modalias[@]}"; do
1632 [[ $i == $a ]] && return 0
1633 done
1634 done
1638 return 1
1641 find_kernel_modules_by_path () {
1642 local _OLDIFS
1644 [[ -f "$srcmods/modules.dep" ]] || return 0
1646 _OLDIFS=$IFS
1647 IFS=:
1648 while read a rest; do
1649 [[ $a = */$1/* ]] || [[ $a = updates/* ]] || continue
1650 printf "%s\n" "$srcmods/$a"
1651 done < "$srcmods/modules.dep"
1652 IFS=$_OLDIFS
1653 return 0
1656 find_kernel_modules () {
1657 find_kernel_modules_by_path drivers
1660 # instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1661 # instmods [-c [-s]] <kernel subsystem>
1662 # install kernel modules along with all their dependencies.
1663 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1664 instmods() {
1665 [[ $no_kernel = yes ]] && return
1666 # called [sub]functions inherit _fderr
1667 local _fderr=9
1668 local _check=no
1669 local _silent=no
1670 if [[ $1 = '-c' ]]; then
1671 _check=yes
1672 shift
1675 if [[ $1 = '-s' ]]; then
1676 _silent=yes
1677 shift
1680 function inst1mod() {
1681 local _ret=0 _mod="$1"
1682 case $_mod in
1684 ( [[ "$_mpargs" ]] && echo $_mpargs
1685 find_kernel_modules_by_path "${_mod#=}" ) \
1686 | instmods
1687 ((_ret+=$?))
1689 --*) _mpargs+=" $_mod" ;;
1691 _mod=${_mod##*/}
1692 # Check for aliased modules
1693 _modalias=$(modinfo -k $kernel -F filename $_mod 2> /dev/null)
1694 _modalias=${_modalias%.ko}
1695 if [ "${_modalias##*/}" != "$_mod" ] ; then
1696 _mod=${_modalias##*/}
1699 # if we are already installed, skip this module and go on
1700 # to the next one.
1701 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1702 [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko" ]]; then
1703 read _ret <"$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko"
1704 return $_ret
1707 _mod=${_mod/-/_}
1708 if [[ $omit_drivers ]] && [[ "$_mod" =~ $omit_drivers ]]; then
1709 dinfo "Omitting driver ${_mod##$srcmods}"
1710 return 0
1713 # If we are building a host-specific initramfs and this
1714 # module is not already loaded, move on to the next one.
1715 [[ $hostonly ]] \
1716 && ! module_is_host_only "$_mod" \
1717 && return 0
1719 if [[ "$_check" = "yes" ]] || ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
1720 # We use '-d' option in modprobe only if modules prefix path
1721 # differs from default '/'. This allows us to use dracut with
1722 # old version of modprobe which doesn't have '-d' option.
1723 local _moddirname=${srcmods%%/lib/modules/*}
1724 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1726 # ok, load the module, all its dependencies, and any firmware
1727 # it may require
1728 for_each_kmod_dep install_kmod_with_fw $_mod \
1729 --set-version $kernel ${_moddirname} $_mpargs
1730 ((_ret+=$?))
1731 else
1732 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1733 echo $_mod >> "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist"
1736 esac
1737 return $_ret
1740 function instmods_1() {
1741 local _mod _mpargs
1742 if (($# == 0)); then # filenames from stdin
1743 while read _mod; do
1744 inst1mod "${_mod%.ko*}" || {
1745 if [[ "$_check" == "yes" ]] && [[ "$_silent" == "no" ]]; then
1746 dfatal "Failed to install module $_mod"
1749 done
1751 while (($# > 0)); do # filenames as arguments
1752 inst1mod ${1%.ko*} || {
1753 if [[ "$_check" == "yes" ]] && [[ "$_silent" == "no" ]]; then
1754 dfatal "Failed to install module $1"
1757 shift
1758 done
1759 return 0
1762 local _ret _filter_not_found='FATAL: Module .* not found.'
1763 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1764 # redirections, but that would make dracut require bash4 at least.
1765 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1766 | while read line; do [[ "$line" =~ $_filter_not_found ]] || echo $line;done | derror
1767 _ret=$?
1768 return $_ret
1771 check_kernel_config()
1773 local _config_opt="$1"
1774 local _config_file
1775 [[ -f /boot/config-$kernel ]] \
1776 && _config_file="/boot/config-$kernel"
1777 [[ -f /lib/modules/$kernel/config ]] \
1778 && _config_file="/lib/modules/$kernel/config"
1780 # no kernel config file, so return true
1781 [[ $_config_file ]] || return 0
1783 grep -q -F "${_config_opt}=" "$_config_file" && return 0
1784 return 1
1788 # get_cpu_vendor
1789 # Only two values are returned: AMD or Intel
1790 get_cpu_vendor ()
1792 if grep -qE AMD /proc/cpuinfo; then
1793 printf "AMD"
1795 if grep -qE Intel /proc/cpuinfo; then
1796 printf "Intel"
1800 # get_host_ucode
1801 # Get the hosts' ucode file based on the /proc/cpuinfo
1802 get_ucode_file ()
1804 local family=`grep -E "cpu family" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1805 local model=`grep -E "model" /proc/cpuinfo |grep -v name | head -1 | sed s/.*:\ //`
1806 local stepping=`grep -E "stepping" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1808 if [[ "$(get_cpu_vendor)" == "AMD" ]]; then
1809 # If family greater or equal than 0x15
1810 if [[ $family -ge 21 ]]; then
1811 printf "microcode_amd_fam15h.bin"
1812 else
1813 printf "microcode_amd.bin"
1816 if [[ "$(get_cpu_vendor)" == "Intel" ]]; then
1817 # The /proc/cpuinfo are in decimal.
1818 printf "%02x-%02x-%02x" ${family} ${model} ${stepping}
1822 # Not every device in /dev/mapper should be examined.
1823 # If it is an LVM device, touch only devices which have /dev/VG/LV symlink.
1824 lvm_internal_dev() {
1825 local dev_dm_dir=/sys/dev/block/$1/dm
1826 [[ ! -f $dev_dm_dir/uuid || $(<$dev_dm_dir/uuid) != LVM-* ]] && return 1 # Not an LVM device
1827 local DM_VG_NAME DM_LV_NAME DM_LV_LAYER
1828 eval $(dmsetup splitname --nameprefixes --noheadings --rows "$(<$dev_dm_dir/name)" 2>/dev/null)
1829 [[ ${DM_VG_NAME} ]] && [[ ${DM_LV_NAME} ]] || return 0 # Better skip this!
1830 [[ ${DM_LV_LAYER} ]] || [[ ! -L /dev/${DM_VG_NAME}/${DM_LV_NAME} ]]