add "--install-optional" and install_optional_items
[dracut.git] / dracut-functions.sh
blob6cdfea0f5d900d7823fb7d900d6b9b7d8f848349
1 #!/bin/bash
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/>.
22 export LC_MESSAGES=C
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
32 mkdir -p "$initdir"
35 # Generic substring function. If $2 is in $1, return 0.
36 strstr() { [[ $1 = *"$2"* ]]; }
37 # Generic glob matching function. If glob pattern $2 matches anywhere in $1, OK
38 strglobin() { [[ $1 = *$2* ]]; }
39 # Generic glob matching function. If glob pattern $2 matches all of $1, OK
40 strglob() { [[ $1 = $2 ]]; }
42 # helper function for check() in module-setup.sh
43 # to check for required installed binaries
44 # issues a standardized warning message
45 require_binaries() {
46 local _module_name="${moddir##*/}"
47 local _ret=0
49 if [[ "$1" = "-m" ]]; then
50 _module_name="$2"
51 shift 2
54 for cmd in "$@"; do
55 if ! find_binary "$cmd" &>/dev/null; then
56 dinfo "dracut module '${_module_name#[0-9][0-9]}' will not be installed, because command '$cmd' could not be found!"
57 ((_ret++))
59 done
60 return $_ret
63 require_any_binary() {
64 local _module_name="${moddir##*/}"
65 local _ret=1
67 if [[ "$1" = "-m" ]]; then
68 _module_name="$2"
69 shift 2
72 for cmd in "$@"; do
73 if find_binary "$cmd" &>/dev/null; then
74 _ret=0
75 break
77 done
79 if (( $_ret != 0 )); then
80 dinfo "$_module_name: Could not find any command of '$@'!"
81 return 1
84 return 0
87 # find a binary. If we were not passed the full path directly,
88 # search in the usual places to find the binary.
89 find_binary() {
90 if [[ -z ${1##/*} ]]; then
91 if [[ -x $1 ]] || { [[ "$1" == *.so* ]] && ldd "$1" &>/dev/null; }; then
92 printf "%s\n" "$1"
93 return 0
97 type -P "${1##*/}"
100 if ! [[ $dracutbasedir ]]; then
101 dracutbasedir=${BASH_SOURCE[0]%/*}
102 [[ $dracutbasedir = "dracut-functions" ]] && dracutbasedir="."
103 [[ $dracutbasedir ]] || dracutbasedir="."
104 dracutbasedir="$(readlink -f $dracutbasedir)"
107 ldconfig_paths()
109 local a i
110 declare -A a
111 for i in $(
112 ldconfig -pN 2>/dev/null | grep -F '=>' | grep -E -v '/(lib|lib64|usr/lib|usr/lib64)/[^/]*$' | while read a b c d; do
113 d=${d%/*}
114 printf "%s\n" "$d";
115 done
116 ); do
117 a["$i"]=1;
118 done;
119 printf "%s\n" ${!a[@]}
122 # Detect lib paths
123 if ! [[ $libdirs ]] ; then
124 if [[ "$(ldd /bin/sh)" == */lib64/* ]] &>/dev/null \
125 && [[ -d /lib64 ]]; then
126 libdirs+=" /lib64"
127 [[ -d /usr/lib64 ]] && libdirs+=" /usr/lib64"
128 else
129 libdirs+=" /lib"
130 [[ -d /usr/lib ]] && libdirs+=" /usr/lib"
133 libdirs+=" $(ldconfig_paths)"
135 export libdirs
138 if ! [[ $kernel ]]; then
139 kernel=$(uname -r)
140 export kernel
143 # Version comparision function. Assumes Linux style version scheme.
144 # $1 = version a
145 # $2 = comparision op (gt, ge, eq, le, lt, ne)
146 # $3 = version b
147 vercmp() {
148 local _n1=(${1//./ }) _op=$2 _n2=(${3//./ }) _i _res
150 for ((_i=0; ; _i++))
152 if [[ ! ${_n1[_i]}${_n2[_i]} ]]; then _res=0
153 elif ((${_n1[_i]:-0} > ${_n2[_i]:-0})); then _res=1
154 elif ((${_n1[_i]:-0} < ${_n2[_i]:-0})); then _res=2
155 else continue
157 break
158 done
160 case $_op in
161 gt) ((_res == 1));;
162 ge) ((_res != 2));;
163 eq) ((_res == 0));;
164 le) ((_res != 1));;
165 lt) ((_res == 2));;
166 ne) ((_res != 0));;
167 esac
170 srcmods="/lib/modules/$kernel/"
172 [[ $drivers_dir ]] && {
173 if ! command -v kmod &>/dev/null && vercmp "$(modprobe --version | cut -d' ' -f3)" lt 3.7; then
174 dfatal 'To use --kmoddir option module-init-tools >= 3.7 is required.'
175 exit 1
177 srcmods="$drivers_dir"
179 export srcmods
181 if ! type dinfo >/dev/null 2>&1; then
182 . "$dracutbasedir/dracut-logger.sh"
183 dlog_init
186 if ! [[ $initdir ]]; then
187 dfatal "initdir not set"
188 exit 1
191 # export standard hookdirs
192 [[ $hookdirs ]] || {
193 hookdirs="cmdline pre-udev pre-trigger netroot "
194 hookdirs+="initqueue initqueue/settled initqueue/online initqueue/finished initqueue/timeout "
195 hookdirs+="pre-mount pre-pivot cleanup mount "
196 hookdirs+="emergency shutdown-emergency pre-shutdown shutdown "
197 export hookdirs
200 dracut_need_initqueue() {
201 >"$initdir/lib/dracut/need-initqueue"
204 dracut_module_included() {
205 [[ " $mods_to_load $modules_loaded " == *\ $*\ * ]]
208 # Create all subdirectories for given path without creating the last element.
209 # $1 = path
210 mksubdirs() {
211 [[ -e ${1%/*} ]] || mkdir -m 0755 -p -- "${1%/*}"
214 # is_func <command>
215 # Check whether $1 is a function.
216 is_func() {
217 [[ "$(type -t "$1")" = "function" ]]
220 # Function prints global variables in format name=value line by line.
221 # $@ = list of global variables' name
222 print_vars() {
223 local _var _value
225 for _var in "$@"
227 eval printf -v _value "%s" \""\$$_var"\"
228 [[ ${_value} ]] && printf '%s="%s"\n' "$_var" "$_value"
229 done
232 # normalize_path <path>
233 # Prints the normalized path, where it removes any duplicated
234 # and trailing slashes.
235 # Example:
236 # $ normalize_path ///test/test//
237 # /test/test
238 normalize_path() {
239 shopt -q -s extglob
240 set -- "${1//+(\/)//}"
241 shopt -q -u extglob
242 printf "%s\n" "${1%/}"
245 # convert_abs_rel <from> <to>
246 # Prints the relative path, when creating a symlink to <to> from <from>.
247 # Example:
248 # $ convert_abs_rel /usr/bin/test /bin/test-2
249 # ../../bin/test-2
250 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
251 convert_abs_rel() {
252 local __current __absolute __abssize __cursize __newpath
253 local -i __i __level
255 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
257 # corner case #1 - self looping link
258 [[ "$1" == "$2" ]] && { printf "%s\n" "${1##*/}"; return; }
260 # corner case #2 - own dir link
261 [[ "${1%/*}" == "$2" ]] && { printf ".\n"; return; }
263 IFS="/" __current=($1)
264 IFS="/" __absolute=($2)
266 __abssize=${#__absolute[@]}
267 __cursize=${#__current[@]}
269 while [[ "${__absolute[__level]}" == "${__current[__level]}" ]]
271 (( __level++ ))
272 if (( __level > __abssize || __level > __cursize ))
273 then
274 break
276 done
278 for ((__i = __level; __i < __cursize-1; __i++))
280 if ((__i > __level))
281 then
282 __newpath=$__newpath"/"
284 __newpath=$__newpath".."
285 done
287 for ((__i = __level; __i < __abssize; __i++))
289 if [[ -n $__newpath ]]
290 then
291 __newpath=$__newpath"/"
293 __newpath=$__newpath${__absolute[__i]}
294 done
296 printf "%s\n" "$__newpath"
299 if [[ "$(ln --help)" == *--relative* ]]; then
300 ln_r() {
301 ln -sfnr "${initdir}/$1" "${initdir}/$2"
303 else
304 ln_r() {
305 local _source=$1
306 local _dest=$2
307 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
308 ln -sfn -- "$(convert_abs_rel "${_dest}" "${_source}")" "${initdir}/${_dest}"
312 # get_fs_env <device>
313 # Get and the ID_FS_TYPE variable from udev for a device.
314 # Example:
315 # $ get_fs_env /dev/sda2
316 # ext4
317 get_fs_env() {
318 local evalstr
319 local found
321 [[ $1 ]] || return
322 unset ID_FS_TYPE
323 ID_FS_TYPE=$(blkid -u filesystem -o export -- "$1" \
324 | while read line; do
325 if [[ "$line" == TYPE\=* ]]; then
326 printf "%s" "${line#TYPE=}";
327 exit 0;
329 done)
330 if [[ $ID_FS_TYPE ]]; then
331 printf "%s" "$ID_FS_TYPE"
332 return 0
334 return 1
337 # get_maj_min <device>
338 # Prints the major and minor of a device node.
339 # Example:
340 # $ get_maj_min /dev/sda2
341 # 8:2
342 get_maj_min() {
343 local _maj _min _majmin
344 _majmin="$(stat -L -c '%t:%T' "$1" 2>/dev/null)"
345 printf "%s" "$((0x${_majmin%:*})):$((0x${_majmin#*:}))"
349 # get_devpath_block <device>
350 # get the DEVPATH in /sys of a block device
351 get_devpath_block() {
352 local _majmin _i
353 _majmin=$(get_maj_min "$1")
355 for _i in /sys/block/*/dev /sys/block/*/*/dev; do
356 [[ -e "$_i" ]] || continue
357 if [[ "$_majmin" == "$(<"$_i")" ]]; then
358 printf "%s" "${_i%/dev}"
359 return 0
361 done
362 return 1
365 # get a persistent path from a device
366 get_persistent_dev() {
367 local i _tmp _dev
369 _dev=$(get_maj_min "$1")
370 [ -z "$_dev" ] && return
372 for i in \
373 /dev/mapper/* \
374 /dev/disk/${persistent_policy:-by-uuid}/* \
375 /dev/disk/by-uuid/* \
376 /dev/disk/by-label/* \
377 /dev/disk/by-partuuid/* \
378 /dev/disk/by-partlabel/* \
379 /dev/disk/by-id/* \
380 /dev/disk/by-path/* \
381 ; do
382 [[ -e "$i" ]] || continue
383 [[ $i == /dev/mapper/control ]] && continue
384 [[ $i == /dev/mapper/mpath* ]] && continue
385 _tmp=$(get_maj_min "$i")
386 if [ "$_tmp" = "$_dev" ]; then
387 printf -- "%s" "$i"
388 return
390 done
393 expand_persistent_dev() {
394 local _dev=$1
396 case "$_dev" in
397 LABEL=*)
398 _dev="/dev/disk/by-label/${_dev#LABEL=}"
400 UUID=*)
401 _dev="${_dev#UUID=}"
402 _dev="${_dev,,}"
403 _dev="/dev/disk/by-uuid/${_dev}"
405 PARTUUID=*)
406 _dev="${_dev#PARTUUID=}"
407 _dev="${_dev,,}"
408 _dev="/dev/disk/by-partuuid/${_dev}"
410 PARTLABEL=*)
411 _dev="/dev/disk/by-partlabel/${_dev#PARTLABEL=}"
413 esac
414 printf "%s" "$_dev"
417 shorten_persistent_dev() {
418 local _dev="$1"
419 case "$_dev" in
420 /dev/disk/by-uuid/*)
421 printf "%s" "UUID=${_dev##*/}";;
422 /dev/disk/by-label/*)
423 printf "%s" "LABEL=${_dev##*/}";;
424 /dev/disk/by-partuuid/*)
425 printf "%s" "PARTUUID=${_dev##*/}";;
426 /dev/disk/by-partlabel/*)
427 printf "%s" "PARTLABEL=${_dev##*/}";;
429 printf "%s" "$_dev";;
430 esac
433 # find_block_device <mountpoint>
434 # Prints the major and minor number of the block device
435 # for a given mountpoint.
436 # Unless $use_fstab is set to "yes" the functions
437 # uses /proc/self/mountinfo as the primary source of the
438 # information and only falls back to /etc/fstab, if the mountpoint
439 # is not found there.
440 # Example:
441 # $ find_block_device /usr
442 # 8:4
443 find_block_device() {
444 local _majmin _dev _majmin _find_mpt
445 _find_mpt="$1"
446 if [[ $use_fstab != yes ]]; then
447 [[ -d $_find_mpt/. ]]
448 findmnt -e -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" | { \
449 while read _majmin _dev; do
450 if [[ -b $_dev ]]; then
451 if ! [[ $_majmin ]] || [[ $_majmin == 0:* ]]; then
452 _majmin=$(get_maj_min $_dev)
454 if [[ $_majmin ]]; then
455 printf "%s\n" "$_majmin"
456 else
457 printf "%s\n" "$_dev"
459 return 0
461 if [[ $_dev = *:* ]]; then
462 printf "%s\n" "$_dev"
463 return 0
465 done; return 1; } && return 0
467 # fall back to /etc/fstab
469 findmnt -e --fstab -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" | { \
470 while read _majmin _dev; do
471 if ! [[ $_dev ]]; then
472 _dev="$_majmin"
473 unset _majmin
475 if [[ -b $_dev ]]; then
476 [[ $_majmin ]] || _majmin=$(get_maj_min $_dev)
477 if [[ $_majmin ]]; then
478 printf "%s\n" "$_majmin"
479 else
480 printf "%s\n" "$_dev"
482 return 0
484 if [[ $_dev = *:* ]]; then
485 printf "%s\n" "$_dev"
486 return 0
488 done; return 1; } && return 0
490 return 1
493 # find_mp_fstype <mountpoint>
494 # Echo the filesystem type for a given mountpoint.
495 # /proc/self/mountinfo is taken as the primary source of information
496 # and /etc/fstab is used as a fallback.
497 # No newline is appended!
498 # Example:
499 # $ find_mp_fstype /;echo
500 # ext4
501 find_mp_fstype() {
502 local _fs
504 if [[ $use_fstab != yes ]]; then
505 findmnt -e -v -n -o 'FSTYPE' --target "$1" | { \
506 while read _fs; do
507 [[ $_fs ]] || continue
508 [[ $_fs = "autofs" ]] && continue
509 printf "%s" "$_fs"
510 return 0
511 done; return 1; } && return 0
514 findmnt --fstab -e -v -n -o 'FSTYPE' --target "$1" | { \
515 while read _fs; do
516 [[ $_fs ]] || continue
517 [[ $_fs = "autofs" ]] && continue
518 printf "%s" "$_fs"
519 return 0
520 done; return 1; } && return 0
522 return 1
525 # find_dev_fstype <device>
526 # Echo the filesystem type for a given device.
527 # /proc/self/mountinfo is taken as the primary source of information
528 # and /etc/fstab is used as a fallback.
529 # No newline is appended!
530 # Example:
531 # $ find_dev_fstype /dev/sda2;echo
532 # ext4
533 find_dev_fstype() {
534 local _find_dev _fs
535 _find_dev="$1"
536 if ! [[ "$_find_dev" = /dev* ]]; then
537 [[ -b "/dev/block/$_find_dev" ]] && _find_dev="/dev/block/$_find_dev"
540 if [[ $use_fstab != yes ]]; then
541 findmnt -e -v -n -o 'FSTYPE' --source "$_find_dev" | { \
542 while read _fs; do
543 [[ $_fs ]] || continue
544 [[ $_fs = "autofs" ]] && continue
545 printf "%s" "$_fs"
546 return 0
547 done; return 1; } && return 0
550 findmnt --fstab -e -v -n -o 'FSTYPE' --source "$_find_dev" | { \
551 while read _fs; do
552 [[ $_fs ]] || continue
553 [[ $_fs = "autofs" ]] && continue
554 printf "%s" "$_fs"
555 return 0
556 done; return 1; } && return 0
558 return 1
561 # find_mp_fsopts <mountpoint>
562 # Echo the filesystem options for a given mountpoint.
563 # /proc/self/mountinfo is taken as the primary source of information
564 # and /etc/fstab is used as a fallback.
565 # No newline is appended!
566 # Example:
567 # $ find_mp_fsopts /;echo
568 # rw,relatime,discard,data=ordered
569 find_mp_fsopts() {
570 if [[ $use_fstab != yes ]]; then
571 findmnt -e -v -n -o 'OPTIONS' --target "$1" 2>/dev/null && return 0
574 findmnt --fstab -e -v -n -o 'OPTIONS' --target "$1"
577 # find_dev_fsopts <device>
578 # Echo the filesystem options for a given device.
579 # /proc/self/mountinfo is taken as the primary source of information
580 # and /etc/fstab is used as a fallback.
581 # Example:
582 # $ find_dev_fsopts /dev/sda2
583 # rw,relatime,discard,data=ordered
584 find_dev_fsopts() {
585 local _find_dev _opts
586 _find_dev="$1"
587 if ! [[ "$_find_dev" = /dev* ]]; then
588 [[ -b "/dev/block/$_find_dev" ]] && _find_dev="/dev/block/$_find_dev"
591 if [[ $use_fstab != yes ]]; then
592 findmnt -e -v -n -o 'OPTIONS' --source "$_find_dev" 2>/dev/null && return 0
595 findmnt --fstab -e -v -n -o 'OPTIONS' --source "$_find_dev"
599 # finds the major:minor of the block device backing the root filesystem.
600 find_root_block_device() { find_block_device /; }
602 # for_each_host_dev_fs <func>
603 # Execute "<func> <dev> <filesystem>" for every "<dev> <fs>" pair found
604 # in ${host_fs_types[@]}
605 for_each_host_dev_fs()
607 local _func="$1"
608 local _dev
609 local _ret=1
611 [[ "${!host_fs_types[@]}" ]] || return 0
613 for _dev in "${!host_fs_types[@]}"; do
614 $_func "$_dev" "${host_fs_types[$_dev]}" && _ret=0
615 done
616 return $_ret
619 host_fs_all()
621 printf "%s\n" "${host_fs_types[@]}"
624 # Walk all the slave relationships for a given block device.
625 # Stop when our helper function returns success
626 # $1 = function to call on every found block device
627 # $2 = block device in major:minor format
628 check_block_and_slaves() {
629 local _x
630 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
631 if ! lvm_internal_dev $2; then "$1" $2 && return; fi
632 check_vol_slaves "$@" && return 0
633 if [[ -f /sys/dev/block/$2/../dev ]]; then
634 check_block_and_slaves $1 $(<"/sys/dev/block/$2/../dev") && return 0
636 [[ -d /sys/dev/block/$2/slaves ]] || return 1
637 for _x in /sys/dev/block/$2/slaves/*/dev; do
638 [[ -f $_x ]] || continue
639 check_block_and_slaves $1 $(<"$_x") && return 0
640 done
641 return 1
644 check_block_and_slaves_all() {
645 local _x _ret=1
646 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
647 if ! lvm_internal_dev $2 && "$1" $2; then
648 _ret=0
650 check_vol_slaves "$@" && return 0
651 if [[ -f /sys/dev/block/$2/../dev ]]; then
652 check_block_and_slaves_all $1 $(<"/sys/dev/block/$2/../dev") && _ret=0
654 [[ -d /sys/dev/block/$2/slaves ]] || return 1
655 for _x in /sys/dev/block/$2/slaves/*/dev; do
656 [[ -f $_x ]] || continue
657 check_block_and_slaves_all $1 $(<"$_x") && _ret=0
658 done
659 return $_ret
661 # for_each_host_dev_and_slaves <func>
662 # Execute "<func> <dev>" for every "<dev>" found
663 # in ${host_devs[@]} and their slaves
664 for_each_host_dev_and_slaves_all()
666 local _func="$1"
667 local _dev
668 local _ret=1
670 [[ "${host_devs[@]}" ]] || return 0
672 for _dev in ${host_devs[@]}; do
673 [[ -b "$_dev" ]] || continue
674 if check_block_and_slaves_all $_func $(get_maj_min $_dev); then
675 _ret=0
677 done
678 return $_ret
681 for_each_host_dev_and_slaves()
683 local _func="$1"
684 local _dev
686 [[ "${host_devs[@]}" ]] || return 0
688 for _dev in ${host_devs[@]}; do
689 [[ -b "$_dev" ]] || continue
690 check_block_and_slaves $_func $(get_maj_min $_dev) && return 0
691 done
692 return 1
695 # ugly workaround for the lvm design
696 # There is no volume group device,
697 # so, there are no slave devices for volume groups.
698 # Logical volumes only have the slave devices they really live on,
699 # but you cannot create the logical volume without the volume group.
700 # And the volume group might be bigger than the devices the LV needs.
701 check_vol_slaves() {
702 local _lv _vg _pv
703 for i in /dev/mapper/*; do
704 [[ $i == /dev/mapper/control ]] && continue
705 _lv=$(get_maj_min $i)
706 if [[ $_lv = $2 ]]; then
707 _vg=$(lvm lvs --noheadings -o vg_name $i 2>/dev/null)
708 # strip space
709 _vg=$(printf "%s\n" "$_vg")
710 if [[ $_vg ]]; then
711 for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2>/dev/null)
713 check_block_and_slaves $1 $(get_maj_min $_pv) && return 0
714 done
717 done
718 return 1
721 # fs_get_option <filesystem options> <search for option>
722 # search for a specific option in a bunch of filesystem options
723 # and return the value
724 fs_get_option() {
725 local _fsopts=$1
726 local _option=$2
727 local OLDIFS="$IFS"
728 IFS=,
729 set -- $_fsopts
730 IFS="$OLDIFS"
731 while [ $# -gt 0 ]; do
732 case $1 in
733 $_option=*)
734 echo ${1#${_option}=}
735 break
736 esac
737 shift
738 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
750 if ! [[ -x $DRACUT_INSTALL ]]; then
751 dfatal "dracut-install not found!"
752 exit 10
755 [[ $DRACUT_RESOLVE_LAZY ]] || export DRACUT_RESOLVE_DEPS=1
756 inst_dir() {
757 [[ -e ${initdir}/"$1" ]] && return 0 # already there
758 $DRACUT_INSTALL ${initdir:+-D "$initdir"} -d "$@"
759 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} -d "$@" || :
762 inst() {
763 local _hostonly_install
764 if [[ "$1" == "-H" ]]; then
765 _hostonly_install="-H"
766 shift
768 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
769 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
770 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
773 inst_simple() {
774 local _hostonly_install
775 if [[ "$1" == "-H" ]]; then
776 _hostonly_install="-H"
777 shift
779 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
780 [[ -e $1 ]] || return 1 # no source
781 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${_hostonly_install:+-H} "$@"
782 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${_hostonly_install:+-H} "$@" || :
785 inst_symlink() {
786 local _hostonly_install
787 if [[ "$1" == "-H" ]]; then
788 _hostonly_install="-H"
789 shift
791 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
792 [[ -L $1 ]] || return 1
793 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
794 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
797 inst_multiple() {
798 local _ret
799 $DRACUT_INSTALL ${initdir:+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
800 _ret=$?
801 (($_ret != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
802 return $_ret
805 dracut_install() {
806 inst_multiple "$@"
809 inst_library() {
810 local _hostonly_install
811 if [[ "$1" == "-H" ]]; then
812 _hostonly_install="-H"
813 shift
815 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
816 [[ -e $1 ]] || return 1 # no source
817 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
818 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
821 inst_binary() {
822 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
823 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@" || :
826 inst_script() {
827 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
828 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@" || :
831 mark_hostonly() {
832 for i in "$@"; do
833 echo "$i" >> "$initdir/lib/dracut/hostonly-files"
834 done
837 # find symlinks linked to given library file
838 # $1 = library file
839 # Function searches for symlinks by stripping version numbers appended to
840 # library filename, checks if it points to the same target and finally
841 # prints the list of symlinks to stdout.
843 # Example:
844 # rev_lib_symlinks libfoo.so.8.1
845 # output: libfoo.so.8 libfoo.so
846 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
847 rev_lib_symlinks() {
848 [[ ! $1 ]] && return 0
850 local fn="$1" orig="$(readlink -f "$1")" links=''
852 [[ ${fn} == *.so.* ]] || return 1
854 until [[ ${fn##*.} == so ]]; do
855 fn="${fn%.*}"
856 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
857 done
859 echo "${links}"
862 # attempt to install any programs specified in a udev rule
863 inst_rule_programs() {
864 local _prog _bin
866 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
867 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
868 _bin=""
869 if [ -x ${udevdir}/$_prog ]; then
870 _bin=${udevdir}/$_prog
871 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
872 _bin=$(find_binary "$_prog") || {
873 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
874 continue;
878 [[ $_bin ]] && inst_binary "$_bin"
879 done
881 if grep -qE 'RUN[+=]=?"[^ "]+' "$1"; then
882 for _prog in $(grep -E 'RUN[+=]=?"[^ "]+' "$1" | sed -r 's/.*RUN[+=]=?"([^ "]+).*/\1/'); do
883 _bin=""
884 if [ -x ${udevdir}/$_prog ]; then
885 _bin=${udevdir}/$_prog
886 elif [[ "${_prog/\$env\{/}" == "$_prog" ]] && [[ "${_prog}" != "/sbin/initqueue" ]]; then
887 _bin=$(find_binary "$_prog") || {
888 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
889 continue;
893 [[ $_bin ]] && inst_binary "$_bin"
894 done
896 if grep -qE 'IMPORT\{program\}==?"[^ "]+' "$1"; then
897 for _prog in $(grep -E 'IMPORT\{program\}==?"[^ "]+' "$1" | sed -r 's/.*IMPORT\{program\}==?"([^ "]+).*/\1/'); do
898 _bin=""
899 if [ -x ${udevdir}/$_prog ]; then
900 _bin=${udevdir}/$_prog
901 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
902 _bin=$(find_binary "$_prog") || {
903 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
904 continue;
908 [[ $_bin ]] && dracut_install "$_bin"
909 done
913 # attempt to install any programs specified in a udev rule
914 inst_rule_group_owner() {
915 local i
917 if grep -qE 'OWNER=?"[^ "]+' "$1"; then
918 for i in $(grep -E 'OWNER=?"[^ "]+' "$1" | sed -r 's/.*OWNER=?"([^ "]+).*/\1/'); do
919 if ! egrep -q "^$i:" "$initdir/etc/passwd" 2>/dev/null; then
920 egrep "^$i:" /etc/passwd 2>/dev/null >> "$initdir/etc/passwd"
922 done
924 if grep -qE 'GROUP=?"[^ "]+' "$1"; then
925 for i in $(grep -E 'GROUP=?"[^ "]+' "$1" | sed -r 's/.*GROUP=?"([^ "]+).*/\1/'); do
926 if ! egrep -q "^$i:" "$initdir/etc/group" 2>/dev/null; then
927 egrep "^$i:" /etc/group 2>/dev/null >> "$initdir/etc/group"
929 done
933 inst_rule_initqueue() {
934 if grep -q -F initqueue "$1"; then
935 dracut_need_initqueue
939 # udev rules always get installed in the same place, so
940 # create a function to install them to make life simpler.
941 inst_rules() {
942 local _target=/etc/udev/rules.d _rule _found
944 inst_dir "${udevdir}/rules.d"
945 inst_dir "$_target"
946 for _rule in "$@"; do
947 if [ "${_rule#/}" = "$_rule" ]; then
948 for r in ${udevdir}/rules.d ${hostonly:+/etc/udev/rules.d}; do
949 if [[ -e $r/$_rule ]]; then
950 _found="$r/$_rule"
951 inst_rule_programs "$_found"
952 inst_rule_group_owner "$_found"
953 inst_rule_initqueue "$_found"
954 inst_simple "$_found"
956 done
958 for r in '' $dracutbasedir/rules.d/; do
959 # skip rules without an absolute path
960 [[ "${r}$_rule" != /* ]] && continue
962 if [[ -f ${r}$_rule ]]; then
963 _found="${r}$_rule"
964 inst_rule_programs "$_found"
965 inst_rule_group_owner "$_found"
966 inst_rule_initqueue "$_found"
967 inst_simple "$_found" "$_target/${_found##*/}"
969 done
970 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
971 done
974 inst_rules_wildcard() {
975 local _target=/etc/udev/rules.d _rule _found
977 inst_dir "${udevdir}/rules.d"
978 inst_dir "$_target"
979 for _rule in ${udevdir}/rules.d/$1 ${dracutbasedir}/rules.d/$1 ; do
980 if [[ -e $_rule ]]; then
981 inst_rule_programs "$_rule"
982 inst_rule_group_owner "$_rule"
983 inst_rule_initqueue "$_rule"
984 inst_simple "$_rule"
985 _found=$_rule
987 done
988 if [ -n ${hostonly} ] ; then
989 for _rule in ${_target}/$1 ; do
990 if [[ -f $_rule ]]; then
991 inst_rule_programs "$_rule"
992 inst_rule_group_owner "$_rule"
993 inst_rule_initqueue "$_rule"
994 inst_simple "$_rule"
995 _found=$_rule
997 done
999 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
1002 prepare_udev_rules() {
1003 [ -z "$UDEVVERSION" ] && export UDEVVERSION=$(udevadm --version)
1005 for f in "$@"; do
1006 f="${initdir}/etc/udev/rules.d/$f"
1007 [ -e "$f" ] || continue
1008 while read line; do
1009 if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
1010 if [ $UDEVVERSION -ge 174 ]; then
1011 printf '%sIMPORT{builtin}="path_id"\n' "${line%%IMPORT PATH_ID}"
1012 else
1013 printf '%sIMPORT{program}="path_id %%p"\n' "${line%%IMPORT PATH_ID}"
1015 elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
1016 if [ $UDEVVERSION -ge 176 ]; then
1017 printf '%sIMPORT{builtin}="blkid"\n' "${line%%IMPORT BLKID}"
1018 else
1019 printf '%sIMPORT{program}="/sbin/blkid -o udev -p $tempnode"\n' "${line%%IMPORT BLKID}"
1021 else
1022 echo "$line"
1024 done < "${f}" > "${f}.new"
1025 mv "${f}.new" "$f"
1026 done
1029 # install function specialized for hooks
1030 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
1031 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
1032 inst_hook() {
1033 if ! [[ -f $3 ]]; then
1034 dfatal "Cannot install a hook ($3) that does not exist."
1035 dfatal "Aborting initrd creation."
1036 exit 1
1037 elif ! [[ "$hookdirs" == *$1* ]]; then
1038 dfatal "No such hook type $1. Aborting initrd creation."
1039 exit 1
1041 inst_simple "$3" "/lib/dracut/hooks/${1}/${2}-${3##*/}"
1044 # install any of listed files
1046 # If first argument is '-d' and second some destination path, first accessible
1047 # source is installed into this path, otherwise it will installed in the same
1048 # path as source. If none of listed files was installed, function return 1.
1049 # On first successful installation it returns with 0 status.
1051 # Example:
1053 # inst_any -d /bin/foo /bin/bar /bin/baz
1055 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
1056 # initramfs.
1057 inst_any() {
1058 local to f
1060 [[ $1 = '-d' ]] && to="$2" && shift 2
1062 for f in "$@"; do
1063 if [[ -e $f ]]; then
1064 [[ $to ]] && inst "$f" "$to" && return 0
1065 inst "$f" && return 0
1067 done
1069 return 1
1073 # inst_libdir_file [-n <pattern>] <file> [<file>...]
1074 # Install a <file> located on a lib directory to the initramfs image
1075 # -n <pattern> install matching files
1076 inst_libdir_file() {
1077 local _files
1078 if [[ "$1" == "-n" ]]; then
1079 local _pattern=$2
1080 shift 2
1081 for _dir in $libdirs; do
1082 for _i in "$@"; do
1083 for _f in "$_dir"/$_i; do
1084 [[ "$_f" =~ $_pattern ]] || continue
1085 [[ -e "$_f" ]] && _files+="$_f "
1086 done
1087 done
1088 done
1089 else
1090 for _dir in $libdirs; do
1091 for _i in "$@"; do
1092 for _f in "$_dir"/$_i; do
1093 [[ -e "$_f" ]] && _files+="$_f "
1094 done
1095 done
1096 done
1098 [[ $_files ]] && inst_multiple $_files
1102 # install function decompressing the target and handling symlinks
1103 # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
1105 # Function install targets in the same paths inside overlay but decompressed
1106 # and without extensions (.gz, .bz2).
1107 inst_decompress() {
1108 local _src _cmd
1110 for _src in $@
1112 case ${_src} in
1113 *.gz) _cmd='gzip -f -d' ;;
1114 *.bz2) _cmd='bzip2 -d' ;;
1115 *) return 1 ;;
1116 esac
1117 inst_simple ${_src}
1118 # Decompress with chosen tool. We assume that tool changes name e.g.
1119 # from 'name.gz' to 'name'.
1120 ${_cmd} "${initdir}${_src}"
1121 done
1124 # It's similar to above, but if file is not compressed, performs standard
1125 # install.
1126 # $@ = list of files
1127 inst_opt_decompress() {
1128 local _src
1130 for _src in $@
1132 inst_decompress "${_src}" || inst "${_src}"
1133 done
1136 # module_check <dracut module>
1137 # execute the check() function of module-setup.sh of <dracut module>
1138 # or the "check" script, if module-setup.sh is not found
1139 # "check $hostonly" is called
1140 module_check() {
1141 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1142 local _ret
1143 local _forced=0
1144 local _hostonly=$hostonly
1145 [ $# -eq 2 ] && _forced=$2
1146 [[ -d $_moddir ]] || return 1
1147 if [[ ! -f $_moddir/module-setup.sh ]]; then
1148 # if we do not have a check script, we are unconditionally included
1149 [[ -x $_moddir/check ]] || return 0
1150 [ $_forced -ne 0 ] && unset hostonly
1151 $_moddir/check $hostonly
1152 _ret=$?
1153 else
1154 unset check depends cmdline install installkernel
1155 check() { true; }
1156 . $_moddir/module-setup.sh
1157 is_func check || return 0
1158 [ $_forced -ne 0 ] && unset hostonly
1159 moddir=$_moddir check $hostonly
1160 _ret=$?
1161 unset check depends cmdline install installkernel
1163 hostonly=$_hostonly
1164 return $_ret
1167 # module_check_mount <dracut module>
1168 # execute the check() function of module-setup.sh of <dracut module>
1169 # or the "check" script, if module-setup.sh is not found
1170 # "mount_needs=1 check 0" is called
1171 module_check_mount() {
1172 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1173 local _ret
1174 mount_needs=1
1175 [[ -d $_moddir ]] || return 1
1176 if [[ ! -f $_moddir/module-setup.sh ]]; then
1177 # if we do not have a check script, we are unconditionally included
1178 [[ -x $_moddir/check ]] || return 0
1179 mount_needs=1 $_moddir/check 0
1180 _ret=$?
1181 else
1182 unset check depends cmdline install installkernel
1183 check() { false; }
1184 . $_moddir/module-setup.sh
1185 moddir=$_moddir check 0
1186 _ret=$?
1187 unset check depends cmdline install installkernel
1189 unset mount_needs
1190 return $_ret
1193 # module_depends <dracut module>
1194 # execute the depends() function of module-setup.sh of <dracut module>
1195 # or the "depends" script, if module-setup.sh is not found
1196 module_depends() {
1197 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1198 local _ret
1199 [[ -d $_moddir ]] || return 1
1200 if [[ ! -f $_moddir/module-setup.sh ]]; then
1201 # if we do not have a check script, we have no deps
1202 [[ -x $_moddir/check ]] || return 0
1203 $_moddir/check -d
1204 return $?
1205 else
1206 unset check depends cmdline install installkernel
1207 depends() { true; }
1208 . $_moddir/module-setup.sh
1209 moddir=$_moddir depends
1210 _ret=$?
1211 unset check depends cmdline install installkernel
1212 return $_ret
1216 # module_cmdline <dracut module>
1217 # execute the cmdline() function of module-setup.sh of <dracut module>
1218 # or the "cmdline" script, if module-setup.sh is not found
1219 module_cmdline() {
1220 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1221 local _ret
1222 [[ -d $_moddir ]] || return 1
1223 if [[ ! -f $_moddir/module-setup.sh ]]; then
1224 [[ -x $_moddir/cmdline ]] && . "$_moddir/cmdline"
1225 return $?
1226 else
1227 unset check depends cmdline install installkernel
1228 cmdline() { true; }
1229 . $_moddir/module-setup.sh
1230 moddir=$_moddir cmdline
1231 _ret=$?
1232 unset check depends cmdline install installkernel
1233 return $_ret
1237 # module_install <dracut module>
1238 # execute the install() function of module-setup.sh of <dracut module>
1239 # or the "install" script, if module-setup.sh is not found
1240 module_install() {
1241 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1242 local _ret
1243 [[ -d $_moddir ]] || return 1
1244 if [[ ! -f $_moddir/module-setup.sh ]]; then
1245 [[ -x $_moddir/install ]] && . "$_moddir/install"
1246 return $?
1247 else
1248 unset check depends cmdline install installkernel
1249 install() { true; }
1250 . $_moddir/module-setup.sh
1251 moddir=$_moddir install
1252 _ret=$?
1253 unset check depends cmdline install installkernel
1254 return $_ret
1258 # module_installkernel <dracut module>
1259 # execute the installkernel() function of module-setup.sh of <dracut module>
1260 # or the "installkernel" script, if module-setup.sh is not found
1261 module_installkernel() {
1262 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1263 local _ret
1264 [[ -d $_moddir ]] || return 1
1265 if [[ ! -f $_moddir/module-setup.sh ]]; then
1266 [[ -x $_moddir/installkernel ]] && . "$_moddir/installkernel"
1267 return $?
1268 else
1269 unset check depends cmdline install installkernel
1270 installkernel() { true; }
1271 . $_moddir/module-setup.sh
1272 moddir=$_moddir installkernel
1273 _ret=$?
1274 unset check depends cmdline install installkernel
1275 return $_ret
1279 # check_mount <dracut module>
1280 # check_mount checks, if a dracut module is needed for the given
1281 # device and filesystem types in "${host_fs_types[@]}"
1282 check_mount() {
1283 local _mod=$1
1284 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1285 local _ret
1286 local _moddep
1288 [ "${#host_fs_types[*]}" -le 0 ] && return 1
1290 # If we are already scheduled to be loaded, no need to check again.
1291 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
1292 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
1294 # This should never happen, but...
1295 [[ -d $_moddir ]] || return 1
1297 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
1299 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
1300 return 1
1303 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
1304 module_check_mount $_mod; ret=$?
1306 # explicit module, so also accept ret=255
1307 [[ $ret = 0 || $ret = 255 ]] || return 1
1308 else
1309 # module not in our list
1310 if [[ $dracutmodules = all ]]; then
1311 # check, if we can and should install this module
1312 module_check_mount $_mod || return 1
1313 else
1314 # skip this module
1315 return 1
1320 for _moddep in $(module_depends $_mod); do
1321 # handle deps as if they were manually added
1322 [[ " $add_dracutmodules " == *\ $_moddep\ * ]] || \
1323 add_dracutmodules+=" $_moddep "
1324 [[ " $force_add_dracutmodules " == *\ $_moddep\ * ]] || \
1325 force_add_dracutmodules+=" $_moddep "
1326 # if a module we depend on fail, fail also
1327 if ! check_module $_moddep; then
1328 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
1329 return 1
1331 done
1333 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
1334 mods_to_load+=" $_mod "
1336 return 0
1339 # check_module <dracut module> [<use_as_dep>]
1340 # check if a dracut module is to be used in the initramfs process
1341 # if <use_as_dep> is set, then the process also keeps track
1342 # that the modules were checked for the dependency tracking process
1343 check_module() {
1344 local _mod=$1
1345 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1346 local _ret
1347 local _moddep
1348 # If we are already scheduled to be loaded, no need to check again.
1349 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
1350 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
1352 # This should never happen, but...
1353 [[ -d $_moddir ]] || return 1
1355 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
1357 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
1358 dinfo "dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
1359 return 1
1362 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
1363 if [[ " $force_add_dracutmodules " == *\ $_mod\ * ]]; then
1364 module_check $_mod 1; ret=$?
1365 else
1366 module_check $_mod 0; ret=$?
1368 # explicit module, so also accept ret=255
1369 [[ $ret = 0 || $ret = 255 ]] || return 1
1370 else
1371 # module not in our list
1372 if [[ $dracutmodules = all ]]; then
1373 # check, if we can and should install this module
1374 module_check $_mod || return 1
1375 else
1376 # skip this module
1377 return 1
1381 for _moddep in $(module_depends $_mod); do
1382 # handle deps as if they were manually added
1383 [[ " $add_dracutmodules " == *\ $_moddep\ * ]] || \
1384 add_dracutmodules+=" $_moddep "
1385 [[ " $force_add_dracutmodules " == *\ $_moddep\ * ]] || \
1386 force_add_dracutmodules+=" $_moddep "
1387 # if a module we depend on fail, fail also
1388 if ! check_module $_moddep; then
1389 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
1390 return 1
1392 done
1394 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
1395 mods_to_load+=" $_mod "
1397 return 0
1400 # for_each_module_dir <func>
1401 # execute "<func> <dracut module> 1"
1402 for_each_module_dir() {
1403 local _modcheck
1404 local _mod
1405 local _moddir
1406 local _func
1407 _func=$1
1408 for _moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1409 [[ -d $_moddir ]] || continue;
1410 [[ -e $_moddir/install || -e $_moddir/installkernel || \
1411 -e $_moddir/module-setup.sh ]] || continue
1412 _mod=${_moddir##*/}; _mod=${_mod#[0-9][0-9]}
1413 $_func $_mod 1
1414 done
1416 # Report any missing dracut modules, the user has specified
1417 _modcheck="$add_dracutmodules $force_add_dracutmodules"
1418 [[ $dracutmodules != all ]] && _modcheck="$m $dracutmodules"
1419 for _mod in $_modcheck; do
1420 [[ " $mods_to_load " == *\ $_mod\ * ]] && continue
1421 [[ " $omit_dracutmodules " == *\ $_mod\ * ]] && continue
1422 derror "dracut module '$_mod' cannot be found or installed."
1423 done
1426 # Install a single kernel module along with any firmware it may require.
1427 # $1 = full path to kernel module to install
1428 install_kmod_with_fw() {
1429 # no need to go further if the module is already installed
1431 [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
1432 && return 0
1434 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -e "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}" ]]; then
1435 read ret < "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
1436 return $ret
1439 if [[ $omit_drivers ]]; then
1440 local _kmod=${1##*/}
1441 _kmod=${_kmod%.ko}
1442 _kmod=${_kmod/-/_}
1443 if [[ "$_kmod" =~ $omit_drivers ]]; then
1444 dinfo "Omitting driver $_kmod"
1445 return 0
1447 if [[ "${1##*/lib/modules/$kernel/}" =~ $omit_drivers ]]; then
1448 dinfo "Omitting driver $_kmod"
1449 return 0
1453 if [[ $silent_omit_drivers ]]; then
1454 local _kmod=${1##*/}
1455 _kmod=${_kmod%.ko}
1456 _kmod=${_kmod/-/_}
1457 [[ "$_kmod" =~ $silent_omit_drivers ]] && return 0
1458 [[ "${1##*/lib/modules/$kernel/}" =~ $silent_omit_drivers ]] && return 0
1461 inst_simple "$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
1462 ret=$?
1463 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1464 [[ -d "$DRACUT_KERNEL_LAZY_HASHDIR" ]] && \
1465 echo $ret > "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
1466 (($ret != 0)) && return $ret
1468 local _modname=${1##*/} _fwdir _found _fw
1469 _modname=${_modname%.ko*}
1470 for _fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do
1471 _found=''
1472 for _fwdir in $fw_dir; do
1473 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1474 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1475 _found=yes
1477 done
1478 if [[ $_found != yes ]]; then
1479 if ! [[ -d $(echo /sys/module/${_modname//-/_}|{ read a b; echo $a; }) ]]; then
1480 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
1481 "\"${_modname}.ko\""
1482 else
1483 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
1484 "\"${_modname}.ko\""
1487 done
1488 return 0
1491 # Do something with all the dependencies of a kernel module.
1492 # Note that kernel modules depend on themselves using the technique we use
1493 # $1 = function to call for each dependency we find
1494 # It will be passed the full path to the found kernel module
1495 # $2 = module to get dependencies for
1496 # rest of args = arguments to modprobe
1497 # _fderr specifies FD passed from surrounding scope
1498 for_each_kmod_dep() {
1499 local _func=$1 _kmod=$2 _cmd _modpath _options
1500 shift 2
1501 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
1502 while read _cmd _modpath _options; do
1503 [[ $_cmd = insmod ]] || continue
1504 $_func ${_modpath} || exit $?
1505 done
1509 dracut_kernel_post() {
1510 local _moddirname=${srcmods%%/lib/modules/*}
1511 local _pid
1513 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" ]]; then
1514 xargs -r modprobe -a ${_moddirname:+-d ${_moddirname}/} \
1515 --ignore-install --show-depends --set-version $kernel \
1516 < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" 2>/dev/null \
1517 | sort -u \
1518 | while read _cmd _modpath _options; do
1519 [[ $_cmd = insmod ]] || continue
1520 echo "$_modpath"
1521 done > "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1524 if [[ $DRACUT_INSTALL ]] && [[ -z $_moddirname ]]; then
1525 xargs -r $DRACUT_INSTALL ${initdir:+-D "$initdir"} -a < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1526 else
1527 while read _modpath; do
1528 local _destpath=$_modpath
1529 [[ $_moddirname ]] && _destpath=${_destpath##$_moddirname/}
1530 _destpath=${_destpath##*/lib/modules/$kernel/}
1531 inst_simple "$_modpath" "/lib/modules/$kernel/${_destpath}" || exit $?
1532 done < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1535 _pid=$(jobs -p | while read a ; do printf ":$a";done)
1536 _pid=${_pid##*:}
1538 if [[ $DRACUT_INSTALL ]]; then
1539 xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" \
1540 | while read line; do
1541 for _fwdir in $fw_dir; do
1542 echo $_fwdir/$line;
1543 done;
1544 done | xargs -r $DRACUT_INSTALL ${initdir:+-D "$initdir"} -a -o
1545 else
1546 for _fw in $(xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"); do
1547 for _fwdir in $fw_dir; do
1548 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1549 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1550 break
1552 done
1553 done
1556 wait $_pid
1559 for _f in modules.builtin.bin modules.builtin modules.order; do
1560 [[ $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f"
1561 done
1563 # generate module dependencies for the initrd
1564 if [[ -d $initdir/lib/modules/$kernel ]] && \
1565 ! depmod -a -b "$initdir" $kernel; then
1566 dfatal "\"depmod -a $kernel\" failed."
1567 exit 1
1570 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && rm -fr -- "$DRACUT_KERNEL_LAZY_HASHDIR"
1573 [[ "$kernel_current" ]] || export kernel_current=$(uname -r)
1575 module_is_host_only() {
1576 local _mod=$1
1577 local _modenc a i _k _s _v _aliases
1578 _mod=${_mod##*/}
1579 _mod=${_mod%.ko}
1580 _modenc=${_mod//-/_}
1582 [[ " $add_drivers " == *\ ${_mod}\ * ]] && return 0
1584 # check if module is loaded
1585 [[ ${host_modules["$_modenc"]} ]] && return 0
1587 [[ "$kernel_current" ]] || export kernel_current=$(uname -r)
1589 if [[ "$kernel_current" != "$kernel" ]]; then
1590 # check if module is loadable on the current kernel
1591 # this covers the case, where a new module is introduced
1592 # or a module was renamed
1593 # or a module changed from builtin to a module
1595 if [[ -d /lib/modules/$kernel_current ]]; then
1596 # if the modinfo can be parsed, but the module
1597 # is not loaded, then we can safely return 1
1598 modinfo -F filename "$_mod" &>/dev/null && return 1
1601 _aliases=$(modinfo -k $kernel -F alias $_mod 2>/dev/null)
1603 # if the module has no aliases, install it
1604 [[ $_aliases ]] || return 0
1606 # finally check all modalias
1607 for a in $_aliases; do
1608 for i in "${!host_modalias[@]}"; do
1609 [[ $i == $a ]] && return 0
1610 done
1611 done
1615 return 1
1618 find_kernel_modules_by_path () {
1619 local _OLDIFS
1621 [[ -f "$srcmods/modules.dep" ]] || return 0
1623 _OLDIFS=$IFS
1624 IFS=:
1625 while read a rest; do
1626 [[ $a = */$1/* ]] || [[ $a = updates/* ]] || continue
1627 printf "%s\n" "$srcmods/$a"
1628 done < "$srcmods/modules.dep"
1629 IFS=$_OLDIFS
1630 return 0
1633 find_kernel_modules () {
1634 find_kernel_modules_by_path drivers
1637 # instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1638 # instmods [-c [-s]] <kernel subsystem>
1639 # install kernel modules along with all their dependencies.
1640 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1641 instmods() {
1642 [[ $no_kernel = yes ]] && return
1643 # called [sub]functions inherit _fderr
1644 local _fderr=9
1645 local _check=no
1646 local _silent=no
1647 if [[ $1 = '-c' ]]; then
1648 _check=yes
1649 shift
1652 if [[ $1 = '-s' ]]; then
1653 _silent=yes
1654 shift
1657 function inst1mod() {
1658 local _ret=0 _mod="$1"
1659 case $_mod in
1661 ( [[ "$_mpargs" ]] && echo $_mpargs
1662 find_kernel_modules_by_path "${_mod#=}" ) \
1663 | instmods
1664 ((_ret+=$?))
1666 --*) _mpargs+=" $_mod" ;;
1668 _mod=${_mod##*/}
1669 # if we are already installed, skip this module and go on
1670 # to the next one.
1671 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1672 [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko" ]]; then
1673 read _ret <"$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko"
1674 return $_ret
1677 _mod=${_mod/-/_}
1678 if [[ $omit_drivers ]] && [[ "$_mod" =~ $omit_drivers ]]; then
1679 dinfo "Omitting driver ${_mod##$srcmods}"
1680 return 0
1683 # If we are building a host-specific initramfs and this
1684 # module is not already loaded, move on to the next one.
1685 [[ $hostonly ]] \
1686 && ! module_is_host_only "$_mod" \
1687 && return 0
1689 if [[ "$_check" = "yes" ]] || ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
1690 # We use '-d' option in modprobe only if modules prefix path
1691 # differs from default '/'. This allows us to use dracut with
1692 # old version of modprobe which doesn't have '-d' option.
1693 local _moddirname=${srcmods%%/lib/modules/*}
1694 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1696 # ok, load the module, all its dependencies, and any firmware
1697 # it may require
1698 for_each_kmod_dep install_kmod_with_fw $_mod \
1699 --set-version $kernel ${_moddirname} $_mpargs
1700 ((_ret+=$?))
1701 else
1702 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1703 echo $_mod >> "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist"
1706 esac
1707 return $_ret
1710 function instmods_1() {
1711 local _mod _mpargs
1712 if (($# == 0)); then # filenames from stdin
1713 while read _mod; do
1714 inst1mod "${_mod%.ko*}" || {
1715 if [[ "$_check" == "yes" ]]; then
1716 [[ "$_silent" == "no" ]] && dfatal "Failed to install module $_mod"
1717 return 1
1720 done
1722 while (($# > 0)); do # filenames as arguments
1723 inst1mod ${1%.ko*} || {
1724 if [[ "$_check" == "yes" ]]; then
1725 [[ "$_silent" == "no" ]] && dfatal "Failed to install module $1"
1726 return 1
1729 shift
1730 done
1731 return 0
1734 local _ret _filter_not_found='FATAL: Module .* not found.'
1735 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1736 # redirections, but that would make dracut require bash4 at least.
1737 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1738 | while read line; do [[ "$line" =~ $_filter_not_found ]] || echo $line;done | derror
1739 _ret=$?
1740 return $_ret
1742 # get_cpu_vendor
1743 # Only two values are returned: AMD or Intel
1744 get_cpu_vendor ()
1746 if grep -qE AMD /proc/cpuinfo; then
1747 printf "AMD"
1749 if grep -qE Intel /proc/cpuinfo; then
1750 printf "Intel"
1754 # get_host_ucode
1755 # Get the hosts' ucode file based on the /proc/cpuinfo
1756 get_ucode_file ()
1758 local family=`grep -E "cpu family" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1759 local model=`grep -E "model" /proc/cpuinfo |grep -v name | head -1 | sed s/.*:\ //`
1760 local stepping=`grep -E "stepping" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1762 if [[ "$(get_cpu_vendor)" == "AMD" ]]; then
1763 # If family greater or equal than 0x15
1764 if [[ $family -ge 21 ]]; then
1765 printf "microcode_amd_fam15h.bin"
1766 else
1767 printf "microcode_amd.bin"
1770 if [[ "$(get_cpu_vendor)" == "Intel" ]]; then
1771 # The /proc/cpuinfo are in decimal.
1772 printf "%02x-%02x-%02x" ${family} ${model} ${stepping}
1776 # Not every device in /dev/mapper should be examined.
1777 # If it is an LVM device, touch only devices which have /dev/VG/LV symlink.
1778 lvm_internal_dev() {
1779 local dev_dm_dir=/sys/dev/block/$1/dm
1780 [[ ! -f $dev_dm_dir/uuid || $(<$dev_dm_dir/uuid) != LVM-* ]] && return 1 # Not an LVM device
1781 local DM_VG_NAME DM_LV_NAME DM_LV_LAYER
1782 eval $(dmsetup splitname --nameprefixes --noheadings --rows "$(<$dev_dm_dir/name)" 2>/dev/null)
1783 [[ ${DM_VG_NAME} ]] && [[ ${DM_LV_NAME} ]] || return 0 # Better skip this!
1784 [[ ${DM_LV_LAYER} ]] || [[ ! -L /dev/${DM_VG_NAME}/${DM_LV_NAME} ]]