kernel-modules/module-setup.sh: silently omit some filesystem drivers
[dracut.git] / dracut-functions.sh
blobeb1278e0104c2db23830fcb3132b468c18b497af
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* ]]; }
38 # find a binary. If we were not passed the full path directly,
39 # search in the usual places to find the binary.
40 find_binary() {
41 if [[ -z ${1##/*} ]]; then
42 if [[ -x $1 ]] || { [[ "$1" == *.so* ]] && ldd "$1" &>/dev/null; }; then
43 printf "%s\n" "$1"
44 return 0
48 type -P "${1##*/}"
51 if ! [[ $dracutbasedir ]]; then
52 dracutbasedir=${BASH_SOURCE[0]%/*}
53 [[ $dracutbasedir = "dracut-functions" ]] && dracutbasedir="."
54 [[ $dracutbasedir ]] || dracutbasedir="."
55 dracutbasedir="$(readlink -f $dracutbasedir)"
58 if ! [[ $DRACUT_INSTALL ]]; then
59 DRACUT_INSTALL=$(find_binary dracut-install)
62 if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install ]]; then
63 DRACUT_INSTALL=$dracutbasedir/dracut-install
66 # Detect lib paths
67 if ! [[ $libdirs ]] ; then
68 if [[ "$(ldd /bin/sh)" == */lib64/* ]] &>/dev/null \
69 && [[ -d /lib64 ]]; then
70 libdirs+=" /lib64"
71 [[ -d /usr/lib64 ]] && libdirs+=" /usr/lib64"
72 else
73 libdirs+=" /lib"
74 [[ -d /usr/lib ]] && libdirs+=" /usr/lib"
76 export libdirs
79 if ! [[ $kernel ]]; then
80 kernel=$(uname -r)
81 export kernel
84 srcmods="/lib/modules/$kernel/"
85 [[ $drivers_dir ]] && {
86 if vercmp "$(modprobe --version | cut -d' ' -f3)" lt 3.7; then
87 dfatal 'To use --kmoddir option module-init-tools >= 3.7 is required.'
88 exit 1
90 srcmods="$drivers_dir"
92 export srcmods
94 if ! type dinfo >/dev/null 2>&1; then
95 . "$dracutbasedir/dracut-logger.sh"
96 dlog_init
99 if ! [[ $initdir ]]; then
100 dfatal "initdir not set"
101 exit 1
104 # export standard hookdirs
105 [[ $hookdirs ]] || {
106 hookdirs="cmdline pre-udev pre-trigger netroot "
107 hookdirs+="initqueue initqueue/settled initqueue/online initqueue/finished initqueue/timeout "
108 hookdirs+="pre-mount pre-pivot cleanup mount "
109 hookdirs+="emergency shutdown-emergency pre-shutdown shutdown "
110 export hookdirs
113 dracut_need_initqueue() {
114 >"$initdir/lib/dracut/need-initqueue"
117 dracut_module_included() {
118 [[ "$mods_to_load $modules_loaded" == *$@* ]]
121 # Create all subdirectories for given path without creating the last element.
122 # $1 = path
123 mksubdirs() {
124 [[ -e ${1%/*} ]] || mkdir -m 0755 -p -- "${1%/*}"
127 # Version comparision function. Assumes Linux style version scheme.
128 # $1 = version a
129 # $2 = comparision op (gt, ge, eq, le, lt, ne)
130 # $3 = version b
131 vercmp() {
132 local _n1=${1//./ } _op=$2 _n2=${3//./ } _i _res
134 for ((_i=0; ; _i++))
136 if [[ ! ${_n1[_i]}${_n2[_i]} ]]; then _res=0
137 elif ((${_n1[_i]:-0} > ${_n2[_i]:-0})); then _res=1
138 elif ((${_n1[_i]:-0} < ${_n2[_i]:-0})); then _res=2
139 else continue
141 break
142 done
144 case $_op in
145 gt) ((_res == 1));;
146 ge) ((_res != 2));;
147 eq) ((_res == 0));;
148 le) ((_res != 1));;
149 lt) ((_res == 2));;
150 ne) ((_res != 0));;
151 esac
154 # is_func <command>
155 # Check whether $1 is a function.
156 is_func() {
157 [[ "$(type -t "$1")" = "function" ]]
160 # Function prints global variables in format name=value line by line.
161 # $@ = list of global variables' name
162 print_vars() {
163 local _var _value
165 for _var in "$@"
167 eval printf -v _value "%s" "\$$_var"
168 [[ ${_value} ]] && printf '%s="%s"\n' "$_var" "$_value"
169 done
172 # normalize_path <path>
173 # Prints the normalized path, where it removes any duplicated
174 # and trailing slashes.
175 # Example:
176 # $ normalize_path ///test/test//
177 # /test/test
178 normalize_path() {
179 shopt -q -s extglob
180 set -- "${1//+(\/)//}"
181 shopt -q -u extglob
182 echo "${1%/}"
185 # convert_abs_rel <from> <to>
186 # Prints the relative path, when creating a symlink to <to> from <from>.
187 # Example:
188 # $ convert_abs_rel /usr/bin/test /bin/test-2
189 # ../../bin/test-2
190 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
191 convert_abs_rel() {
192 local __current __absolute __abssize __cursize __newpath
193 local -i __i __level
195 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
197 # corner case #1 - self looping link
198 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
200 # corner case #2 - own dir link
201 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
203 IFS="/" __current=($1)
204 IFS="/" __absolute=($2)
206 __abssize=${#__absolute[@]}
207 __cursize=${#__current[@]}
209 while [[ "${__absolute[__level]}" == "${__current[__level]}" ]]
211 (( __level++ ))
212 if (( __level > __abssize || __level > __cursize ))
213 then
214 break
216 done
218 for ((__i = __level; __i < __cursize-1; __i++))
220 if ((__i > __level))
221 then
222 __newpath=$__newpath"/"
224 __newpath=$__newpath".."
225 done
227 for ((__i = __level; __i < __abssize; __i++))
229 if [[ -n $__newpath ]]
230 then
231 __newpath=$__newpath"/"
233 __newpath=$__newpath${__absolute[__i]}
234 done
236 echo "$__newpath"
239 if [[ "$(ln --help)" == *--relative* ]]; then
240 ln_r() {
241 ln -sfnr "${initdir}/$1" "${initdir}/$2"
243 else
244 ln_r() {
245 local _source=$1
246 local _dest=$2
247 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
248 ln -sfn -- "$(convert_abs_rel "${_dest}" "${_source}")" "${initdir}/${_dest}"
252 get_persistent_dev() {
253 local i _tmp _dev
255 _dev=$(udevadm info --query=name --name="$1" 2>/dev/null)
256 [ -z "$_dev" ] && return
258 for i in /dev/mapper/* /dev/disk/by-uuid/* /dev/disk/by-id/*; do
259 [[ $i == /dev/mapper/mpath* ]] && continue
260 _tmp=$(udevadm info --query=name --name="$i" 2>/dev/null)
261 if [ "$_tmp" = "$_dev" ]; then
262 printf -- "%s" "$i"
263 return
265 done
268 # get_fs_env <device>
269 # Get and set the ID_FS_TYPE variable from udev for a device.
270 # Example:
271 # $ get_fs_env /dev/sda2; echo $ID_FS_TYPE
272 # ext4
273 get_fs_env() {
274 local evalstr
275 local found
277 [[ $1 ]] || return
278 unset ID_FS_TYPE
279 if ID_FS_TYPE=$(udevadm info --query=env --name="$1" \
280 | { while read line; do
281 [[ "$line" == DEVPATH\=* ]] && found=1;
282 if [[ "$line" == ID_FS_TYPE\=* ]]; then
283 printf "%s" "${line#ID_FS_TYPE=}";
284 exit 0;
286 done; [[ $found ]] && exit 0; exit 1; }) ; then
287 if [[ $ID_FS_TYPE ]]; then
288 printf "%s" "$ID_FS_TYPE"
289 return 0
293 # Fallback, if we don't have udev information
294 if find_binary blkid >/dev/null; then
295 ID_FS_TYPE=$(blkid -u filesystem -o export -- "$1" \
296 | while read line; do
297 if [[ "$line" == TYPE\=* ]]; then
298 printf "%s" "${line#TYPE=}";
299 exit 0;
301 done)
302 if [[ $ID_FS_TYPE ]]; then
303 printf "%s" "$ID_FS_TYPE"
304 return 0
307 return 1
310 # get_maj_min <device>
311 # Prints the major and minor of a device node.
312 # Example:
313 # $ get_maj_min /dev/sda2
314 # 8:2
315 get_maj_min() {
316 local _maj _min _majmin
317 _majmin="$(stat -L -c '%t:%T' "$1" 2>/dev/null)"
318 printf "%s" "$((0x${_majmin%:*})):$((0x${_majmin#*:}))"
321 # find_block_device <mountpoint>
322 # Prints the major and minor number of the block device
323 # for a given mountpoint.
324 # Unless $use_fstab is set to "yes" the functions
325 # uses /proc/self/mountinfo as the primary source of the
326 # information and only falls back to /etc/fstab, if the mountpoint
327 # is not found there.
328 # Example:
329 # $ find_block_device /usr
330 # 8:4
331 find_block_device() {
332 local _majmin _dev _majmin _find_mpt
333 _find_mpt="$1"
334 if [[ $use_fstab != yes ]]; then
335 [[ -d $_find_mpt/. ]]
336 findmnt -e -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" | { \
337 while read _majmin _dev; do
338 if [[ -b $_dev ]]; then
339 if ! [[ $_majmin ]] || [[ $_majmin == 0:* ]]; then
340 _majmin=$(get_maj_min $_dev)
342 if [[ $_majmin ]]; then
343 echo $_majmin
344 else
345 echo $_dev
347 return 0
349 if [[ $_dev = *:* ]]; then
350 echo $_dev
351 return 0
353 done; return 1; } && return 0
355 # fall back to /etc/fstab
357 findmnt -e --fstab -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" | { \
358 while read _majmin _dev; do
359 if ! [[ $_dev ]]; then
360 _dev="$_majmin"
361 unset _majmin
363 if [[ -b $_dev ]]; then
364 [[ $_majmin ]] || _majmin=$(get_maj_min $_dev)
365 if [[ $_majmin ]]; then
366 echo $_majmin
367 else
368 echo $_dev
370 return 0
372 if [[ $_dev = *:* ]]; then
373 echo $_dev
374 return 0
376 done; return 1; } && return 0
378 return 1
381 # find_mp_fstype <mountpoint>
382 # Echo the filesystem type for a given mountpoint.
383 # /proc/self/mountinfo is taken as the primary source of information
384 # and /etc/fstab is used as a fallback.
385 # No newline is appended!
386 # Example:
387 # $ find_mp_fstype /;echo
388 # ext4
389 find_mp_fstype() {
390 local _fs
392 if [[ $use_fstab != yes ]]; then
393 findmnt -e -v -n -o 'FSTYPE' --target "$1" | { \
394 while read _fs; do
395 [[ $_fs ]] || continue
396 [[ $_fs = "autofs" ]] && continue
397 echo -n $_fs
398 return 0
399 done; return 1; } && return 0
402 findmnt --fstab -e -v -n -o 'FSTYPE' --target "$1" | { \
403 while read _fs; do
404 [[ $_fs ]] || continue
405 [[ $_fs = "autofs" ]] && continue
406 echo -n $_fs
407 return 0
408 done; return 1; } && return 0
410 return 1
413 # find_dev_fstype <device>
414 # Echo the filesystem type for a given device.
415 # /proc/self/mountinfo is taken as the primary source of information
416 # and /etc/fstab is used as a fallback.
417 # No newline is appended!
418 # Example:
419 # $ find_dev_fstype /dev/sda2;echo
420 # ext4
421 find_dev_fstype() {
422 local _find_dev _fs
423 _find_dev="$1"
424 [[ "$_find_dev" = /dev* ]] || _find_dev="/dev/block/$_find_dev"
426 if [[ $use_fstab != yes ]]; then
427 findmnt -e -v -n -o 'FSTYPE' --source "$_find_dev" | { \
428 while read _fs; do
429 [[ $_fs ]] || continue
430 [[ $_fs = "autofs" ]] && continue
431 echo -n $_fs
432 return 0
433 done; return 1; } && return 0
436 findmnt --fstab -e -v -n -o 'FSTYPE' --source "$_find_dev" | { \
437 while read _fs; do
438 [[ $_fs ]] || continue
439 [[ $_fs = "autofs" ]] && continue
440 echo -n $_fs
441 return 0
442 done; return 1; } && return 0
444 return 1
448 # finds the major:minor of the block device backing the root filesystem.
449 find_root_block_device() { find_block_device /; }
451 # for_each_host_dev_fs <func>
452 # Execute "<func> <dev> <filesystem>" for every "<dev> <fs>" pair found
453 # in ${host_fs_types[@]}
454 for_each_host_dev_fs()
456 local _func="$1"
457 local _dev
458 local _ret=1
460 [[ "${!host_fs_types[@]}" ]] || return 0
462 for _dev in "${!host_fs_types[@]}"; do
463 $_func "$_dev" "${host_fs_types[$_dev]}" && _ret=0
464 done
465 return $_ret
468 host_fs_all()
470 echo "${host_fs_types[@]}"
473 # Walk all the slave relationships for a given block device.
474 # Stop when our helper function returns success
475 # $1 = function to call on every found block device
476 # $2 = block device in major:minor format
477 check_block_and_slaves() {
478 local _x
479 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
480 "$1" $2 && return
481 check_vol_slaves "$@" && return 0
482 if [[ -f /sys/dev/block/$2/../dev ]]; then
483 check_block_and_slaves $1 $(<"/sys/dev/block/$2/../dev") && return 0
485 [[ -d /sys/dev/block/$2/slaves ]] || return 1
486 for _x in /sys/dev/block/$2/slaves/*/dev; do
487 [[ -f $_x ]] || continue
488 check_block_and_slaves $1 $(<"$_x") && return 0
489 done
490 return 1
493 check_block_and_slaves_all() {
494 local _x _ret=1
495 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
496 if "$1" $2; then
497 _ret=0
499 check_vol_slaves "$@" && return 0
500 if [[ -f /sys/dev/block/$2/../dev ]]; then
501 check_block_and_slaves_all $1 $(<"/sys/dev/block/$2/../dev") && _ret=0
503 [[ -d /sys/dev/block/$2/slaves ]] || return 1
504 for _x in /sys/dev/block/$2/slaves/*/dev; do
505 [[ -f $_x ]] || continue
506 check_block_and_slaves_all $1 $(<"$_x") && _ret=0
507 done
508 return $_ret
510 # for_each_host_dev_and_slaves <func>
511 # Execute "<func> <dev>" for every "<dev>" found
512 # in ${host_devs[@]} and their slaves
513 for_each_host_dev_and_slaves_all()
515 local _func="$1"
516 local _dev
517 local _ret=1
519 [[ "${host_devs[@]}" ]] || return 0
521 for _dev in ${host_devs[@]}; do
522 [[ -b "$_dev" ]] || continue
523 if check_block_and_slaves_all $_func $(get_maj_min $_dev); then
524 _ret=0
526 done
527 return $_ret
530 for_each_host_dev_and_slaves()
532 local _func="$1"
533 local _dev
535 [[ "${host_devs[@]}" ]] || return 0
537 for _dev in ${host_devs[@]}; do
538 [[ -b "$_dev" ]] || continue
539 check_block_and_slaves $_func $(get_maj_min $_dev) && return 0
540 done
541 return 1
544 # ugly workaround for the lvm design
545 # There is no volume group device,
546 # so, there are no slave devices for volume groups.
547 # Logical volumes only have the slave devices they really live on,
548 # but you cannot create the logical volume without the volume group.
549 # And the volume group might be bigger than the devices the LV needs.
550 check_vol_slaves() {
551 local _lv _vg _pv
552 for i in /dev/mapper/*; do
553 _lv=$(get_maj_min $i)
554 if [[ $_lv = $2 ]]; then
555 _vg=$(lvm lvs --noheadings -o vg_name $i 2>/dev/null)
556 # strip space
557 _vg=$(echo $_vg)
558 if [[ $_vg ]]; then
559 for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2>/dev/null)
561 check_block_and_slaves $1 $(get_maj_min $_pv) && return 0
562 done
565 done
566 return 1
569 # fs_get_option <filesystem options> <search for option>
570 # search for a specific option in a bunch of filesystem options
571 # and return the value
572 fs_get_option() {
573 local _fsopts=$1
574 local _option=$2
575 local OLDIFS="$IFS"
576 IFS=,
577 set -- $_fsopts
578 IFS="$OLDIFS"
579 while [ $# -gt 0 ]; do
580 case $1 in
581 $_option=*)
582 echo ${1#${_option}=}
583 break
584 esac
585 shift
586 done
589 if [[ $DRACUT_INSTALL ]]; then
590 [[ $DRACUT_RESOLVE_LAZY ]] || export DRACUT_RESOLVE_DEPS=1
591 inst_dir() {
592 [[ -e ${initdir}/"$1" ]] && return 0 # already there
593 $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@"
594 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@" || :
597 inst() {
598 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
599 #dinfo "$DRACUT_INSTALL -l $@"
600 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
601 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
604 inst_simple() {
605 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
606 [[ -e $1 ]] || return 1 # no source
607 $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@"
608 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@" || :
611 inst_symlink() {
612 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
613 [[ -L $1 ]] || return 1
614 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
615 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
618 dracut_install() {
619 local ret
620 #dinfo "initdir=$initdir $DRACUT_INSTALL -l $@"
621 $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
622 ret=$?
623 (($ret != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
624 return $ret
627 inst_library() {
628 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
629 [[ -e $1 ]] || return 1 # no source
630 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
631 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
634 inst_binary() {
635 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
636 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
639 inst_script() {
640 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
641 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
644 else
646 # Install a directory, keeping symlinks as on the original system.
647 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
648 # will create ${initdir}/lib64, ${initdir}/lib64/file,
649 # and a symlink ${initdir}/lib -> lib64.
650 inst_dir() {
651 [[ -e ${initdir}/"$1" ]] && return 0 # already there
653 local _dir="$1" _part="${1%/*}" _file
654 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
655 _dir="$_part $_dir"
656 _part=${_part%/*}
657 done
659 # iterate over parent directories
660 for _file in $_dir; do
661 [[ -e "${initdir}/$_file" ]] && continue
662 if [[ -L $_file ]]; then
663 inst_symlink "$_file"
664 else
665 # create directory
666 mkdir -m 0755 -p "${initdir}/$_file" || return 1
667 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
668 chmod u+w "${initdir}/$_file"
670 done
673 # $1 = file to copy to ramdisk
674 # $2 (optional) Name for the file on the ramdisk
675 # Location of the image dir is assumed to be $initdir
676 # We never overwrite the target if it exists.
677 inst_simple() {
678 [[ -f "$1" ]] || return 1
679 [[ "$1" == */* ]] || return 1
680 local _src=$1 _target="${2:-$1}"
682 [[ -L $_src ]] && { inst_symlink $_src $_target; return $?; }
684 if ! [[ -d ${initdir}/$_target ]]; then
685 [[ -e ${initdir}/$_target ]] && return 0
686 [[ -L ${initdir}/$_target ]] && return 0
687 [[ -d "${initdir}/${_target%/*}" ]] || inst_dir "${_target%/*}"
689 if [[ $DRACUT_FIPS_MODE ]]; then
690 # install checksum files also
691 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
692 inst "${_src%/*}/.${_src##*/}.hmac" "${_target%/*}/.${_target##*/}.hmac"
694 if [[ -e "/lib/fipscheck/${_src##*/}.hmac" ]]; then
695 inst "/lib/fipscheck/${_src##*/}.hmac" "/lib/fipscheck/${_target##*/}.hmac"
697 if [[ -e "/lib64/fipscheck/${_src##*/}.hmac" ]]; then
698 inst "/lib64/fipscheck/${_src##*/}.hmac" "/lib64/fipscheck/${_target##*/}.hmac"
701 ddebug "Installing $_src"
702 cp --reflink=auto --sparse=auto -pfL "$_src" "${initdir}/$_target"
705 # same as above, but specialized for symlinks
706 inst_symlink() {
707 local _src=$1 _target=${2:-$1} _realsrc
708 [[ "$1" == */* ]] || return 1
709 [[ -L $1 ]] || return 1
710 [[ -L $initdir/$_target ]] && return 0
711 _realsrc=$(readlink -f "$_src")
712 if ! [[ -e $initdir/$_realsrc ]]; then
713 if [[ -d $_realsrc ]]; then
714 inst_dir "$_realsrc"
715 else
716 inst "$_realsrc"
719 [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}"
721 ln_r "${_realsrc}" "${_target}"
724 # Same as above, but specialized to handle dynamic libraries.
725 # It handles making symlinks according to how the original library
726 # is referenced.
727 inst_library() {
728 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
729 [[ "$1" == */* ]] || return 1
730 [[ -e $initdir/$_dest ]] && return 0
731 if [[ -L $_src ]]; then
732 if [[ $DRACUT_FIPS_MODE ]]; then
733 # install checksum files also
734 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
735 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
737 if [[ -e "/lib/fipscheck/${_src##*/}.hmac" ]]; then
738 inst "/lib/fipscheck/${_src##*/}.hmac" "/lib/fipscheck/${_dest##*/}.hmac"
740 if [[ -e "/lib64/fipscheck/${_src##*/}.hmac" ]]; then
741 inst "/lib64/fipscheck/${_src##*/}.hmac" "/lib64/fipscheck/${_dest##*/}.hmac"
744 _reallib=$(readlink -f "$_src")
745 inst_simple "$_reallib" "$_reallib"
746 inst_dir "${_dest%/*}"
747 ln_r "${_reallib}" "${_dest}"
748 else
749 inst_simple "$_src" "$_dest"
752 # Create additional symlinks. See rev_symlinks description.
753 for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do
754 [[ ! -e $initdir/$_symlink ]] && {
755 ddebug "Creating extra symlink: $_symlink"
756 inst_symlink $_symlink
758 done
761 # Same as above, but specialized to install binary executables.
762 # Install binary executable, and all shared library dependencies, if any.
763 inst_binary() {
764 local _bin _target
765 _bin=$(find_binary "$1") || return 1
766 _target=${2:-$_bin}
767 [[ -e $initdir/$_target ]] && return 0
768 local _file _line
769 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
770 # I love bash!
771 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
772 [[ $_line = 'not a dynamic executable' ]] && break
774 if [[ $_line =~ $_so_regex ]]; then
775 _file=${BASH_REMATCH[1]}
776 [[ -e ${initdir}/$_file ]] && continue
777 inst_library "$_file"
778 continue
781 if [[ $_line == *not\ found* ]]; then
782 dfatal "Missing a shared library required by $_bin."
783 dfatal "Run \"ldd $_bin\" to find out what it is."
784 dfatal "$_line"
785 dfatal "dracut cannot create an initrd."
786 exit 1
788 done
789 inst_simple "$_bin" "$_target"
792 # same as above, except for shell scripts.
793 # If your shell script does not start with shebang, it is not a shell script.
794 inst_script() {
795 local _bin
796 _bin=$(find_binary "$1") || return 1
797 shift
798 local _line _shebang_regex
799 read -r -n 80 _line <"$_bin"
800 # If debug is set, clean unprintable chars to prevent messing up the term
801 [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]')
802 _shebang_regex='(#! *)(/[^ ]+).*'
803 [[ $_line =~ $_shebang_regex ]] || return 1
804 inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@"
807 # general purpose installation function
808 # Same args as above.
809 inst() {
810 local _x
812 case $# in
813 1) ;;
814 2) [[ ! $initdir && -d $2 ]] && export initdir=$2
815 [[ $initdir = $2 ]] && set $1;;
816 3) [[ -z $initdir ]] && export initdir=$2
817 set $1 $3;;
818 *) dfatal "inst only takes 1 or 2 or 3 arguments"
819 exit 1;;
820 esac
821 for _x in inst_symlink inst_script inst_binary inst_simple; do
822 $_x "$@" && return 0
823 done
824 return 1
827 # dracut_install [-o ] <file> [<file> ... ]
828 # Install <file> to the initramfs image
829 # -o optionally install the <file> and don't fail, if it is not there
830 dracut_install() {
831 local _optional=no
832 if [[ $1 = '-o' ]]; then
833 _optional=yes
834 shift
836 while (($# > 0)); do
837 if ! inst "$1" ; then
838 if [[ $_optional = yes ]]; then
839 dinfo "Skipping program $1 as it cannot be found and is" \
840 "flagged to be optional"
841 else
842 dfatal "Failed to install $1"
843 exit 1
846 shift
847 done
852 # find symlinks linked to given library file
853 # $1 = library file
854 # Function searches for symlinks by stripping version numbers appended to
855 # library filename, checks if it points to the same target and finally
856 # prints the list of symlinks to stdout.
858 # Example:
859 # rev_lib_symlinks libfoo.so.8.1
860 # output: libfoo.so.8 libfoo.so
861 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
862 rev_lib_symlinks() {
863 [[ ! $1 ]] && return 0
865 local fn="$1" orig="$(readlink -f "$1")" links=''
867 [[ ${fn} == *.so.* ]] || return 1
869 until [[ ${fn##*.} == so ]]; do
870 fn="${fn%.*}"
871 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
872 done
874 echo "${links}"
877 # attempt to install any programs specified in a udev rule
878 inst_rule_programs() {
879 local _prog _bin
881 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
882 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
883 _bin=""
884 if [ -x ${udevdir}/$_prog ]; then
885 _bin=${udevdir}/$_prog
886 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; 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 ]] && dracut_install "$_bin"
894 done
896 if grep -qE 'RUN[+=]=?"[^ "]+' "$1"; then
897 for _prog in $(grep -E 'RUN[+=]=?"[^ "]+' "$1" | sed -r 's/.*RUN[+=]=?"([^ "]+).*/\1/'); do
898 _bin=""
899 if [ -x ${udevdir}/$_prog ]; then
900 _bin=${udevdir}/$_prog
901 elif [[ "${_prog/\$env\{/}" == "$_prog" ]] && [[ "${_prog}" != "/sbin/initqueue" ]]; 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
911 if grep -qE 'IMPORT\{program\}==?"[^ "]+' "$1"; then
912 for _prog in $(grep -E 'IMPORT\{program\}==?"[^ "]+' "$1" | sed -r 's/.*IMPORT\{program\}==?"([^ "]+).*/\1/'); do
913 _bin=""
914 if [ -x ${udevdir}/$_prog ]; then
915 _bin=${udevdir}/$_prog
916 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
917 _bin=$(find_binary "$_prog") || {
918 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
919 continue;
923 [[ $_bin ]] && dracut_install "$_bin"
924 done
928 # attempt to install any programs specified in a udev rule
929 inst_rule_group_owner() {
930 local i
932 if grep -qE 'OWNER=?"[^ "]+' "$1"; then
933 for i in $(grep -E 'OWNER=?"[^ "]+' "$1" | sed -r 's/.*OWNER=?"([^ "]+).*/\1/'); do
934 if ! egrep -q "^$i:" "$initdir/etc/passwd" 2>/dev/null; then
935 egrep "^$i:" /etc/passwd 2>/dev/null >> "$initdir/etc/passwd"
937 done
939 if grep -qE 'GROUP=?"[^ "]+' "$1"; then
940 for i in $(grep -E 'GROUP=?"[^ "]+' "$1" | sed -r 's/.*GROUP=?"([^ "]+).*/\1/'); do
941 if ! egrep -q "^$i:" "$initdir/etc/group" 2>/dev/null; then
942 egrep "^$i:" /etc/group 2>/dev/null >> "$initdir/etc/group"
944 done
948 inst_rule_initqueue() {
949 if grep -q -F initqueue "$1"; then
950 dracut_need_initqueue
954 # udev rules always get installed in the same place, so
955 # create a function to install them to make life simpler.
956 inst_rules() {
957 local _target=/etc/udev/rules.d _rule _found
959 inst_dir "${udevdir}/rules.d"
960 inst_dir "$_target"
961 for _rule in "$@"; do
962 if [ "${_rule#/}" = "$_rule" ]; then
963 for r in ${udevdir}/rules.d /etc/udev/rules.d; do
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"
971 done
973 for r in '' ./ $dracutbasedir/rules.d/; do
974 if [[ -f ${r}$_rule ]]; then
975 _found="${r}$_rule"
976 inst_rule_programs "$_found"
977 inst_rule_group_owner "$_found"
978 inst_rule_initqueue "$_found"
979 inst_simple "$_found" "$_target/${_found##*/}"
981 done
982 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
983 done
986 prepare_udev_rules() {
987 [ -z "$UDEVVERSION" ] && export UDEVVERSION=$(udevadm --version)
989 for f in "$@"; do
990 f="${initdir}/etc/udev/rules.d/$f"
991 [ -e "$f" ] || continue
992 while read line; do
993 if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
994 if [ $UDEVVERSION -ge 174 ]; then
995 printf '%sIMPORT{builtin}="path_id"\n' "${line%%IMPORT PATH_ID}"
996 else
997 printf '%sIMPORT{program}="path_id %%p"\n' "${line%%IMPORT PATH_ID}"
999 elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
1000 if [ $UDEVVERSION -ge 176 ]; then
1001 printf '%sIMPORT{builtin}="blkid"\n' "${line%%IMPORT BLKID}"
1002 else
1003 printf '%sIMPORT{program}="/sbin/blkid -o udev -p $tempnode"\n' "${line%%IMPORT BLKID}"
1005 else
1006 echo "$line"
1008 done < "${f}" > "${f}.new"
1009 mv "${f}.new" "$f"
1010 done
1013 # install function specialized for hooks
1014 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
1015 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
1016 inst_hook() {
1017 if ! [[ -f $3 ]]; then
1018 dfatal "Cannot install a hook ($3) that does not exist."
1019 dfatal "Aborting initrd creation."
1020 exit 1
1021 elif ! [[ "$hookdirs" == *$1* ]]; then
1022 dfatal "No such hook type $1. Aborting initrd creation."
1023 exit 1
1025 inst_simple "$3" "/lib/dracut/hooks/${1}/${2}-${3##*/}"
1028 # install any of listed files
1030 # If first argument is '-d' and second some destination path, first accessible
1031 # source is installed into this path, otherwise it will installed in the same
1032 # path as source. If none of listed files was installed, function return 1.
1033 # On first successful installation it returns with 0 status.
1035 # Example:
1037 # inst_any -d /bin/foo /bin/bar /bin/baz
1039 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
1040 # initramfs.
1041 inst_any() {
1042 local to f
1044 [[ $1 = '-d' ]] && to="$2" && shift 2
1046 for f in "$@"; do
1047 if [[ -e $f ]]; then
1048 [[ $to ]] && inst "$f" "$to" && return 0
1049 inst "$f" && return 0
1051 done
1053 return 1
1057 # inst_libdir_file [-n <pattern>] <file> [<file>...]
1058 # Install a <file> located on a lib directory to the initramfs image
1059 # -n <pattern> install matching files
1060 inst_libdir_file() {
1061 local _files
1062 if [[ "$1" == "-n" ]]; then
1063 local _pattern=$2
1064 shift 2
1065 for _dir in $libdirs; do
1066 for _i in "$@"; do
1067 for _f in "$_dir"/$_i; do
1068 [[ "$_f" =~ $_pattern ]] || continue
1069 [[ -e "$_f" ]] && _files+="$_f "
1070 done
1071 done
1072 done
1073 else
1074 for _dir in $libdirs; do
1075 for _i in "$@"; do
1076 for _f in "$_dir"/$_i; do
1077 [[ -e "$_f" ]] && _files+="$_f "
1078 done
1079 done
1080 done
1082 [[ $_files ]] && dracut_install $_files
1086 # install function decompressing the target and handling symlinks
1087 # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
1089 # Function install targets in the same paths inside overlay but decompressed
1090 # and without extensions (.gz, .bz2).
1091 inst_decompress() {
1092 local _src _cmd
1094 for _src in $@
1096 case ${_src} in
1097 *.gz) _cmd='gzip -f -d' ;;
1098 *.bz2) _cmd='bzip2 -d' ;;
1099 *) return 1 ;;
1100 esac
1101 inst_simple ${_src}
1102 # Decompress with chosen tool. We assume that tool changes name e.g.
1103 # from 'name.gz' to 'name'.
1104 ${_cmd} "${initdir}${_src}"
1105 done
1108 # It's similar to above, but if file is not compressed, performs standard
1109 # install.
1110 # $@ = list of files
1111 inst_opt_decompress() {
1112 local _src
1114 for _src in $@
1116 inst_decompress "${_src}" || inst "${_src}"
1117 done
1120 # module_check <dracut module>
1121 # execute the check() function of module-setup.sh of <dracut module>
1122 # or the "check" script, if module-setup.sh is not found
1123 # "check $hostonly" is called
1124 module_check() {
1125 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1126 local _ret
1127 local _forced=0
1128 local _hostonly=$hostonly
1129 [ $# -eq 2 ] && _forced=$2
1130 [[ -d $_moddir ]] || return 1
1131 if [[ ! -f $_moddir/module-setup.sh ]]; then
1132 # if we do not have a check script, we are unconditionally included
1133 [[ -x $_moddir/check ]] || return 0
1134 [ $_forced -ne 0 ] && unset hostonly
1135 $_moddir/check $hostonly
1136 _ret=$?
1137 else
1138 unset check depends install installkernel
1139 check() { true; }
1140 . $_moddir/module-setup.sh
1141 is_func check || return 0
1142 [ $_forced -ne 0 ] && unset hostonly
1143 check $hostonly
1144 _ret=$?
1145 unset check depends install installkernel
1147 hostonly=$_hostonly
1148 return $_ret
1151 # module_check_mount <dracut module>
1152 # execute the check() function of module-setup.sh of <dracut module>
1153 # or the "check" script, if module-setup.sh is not found
1154 # "mount_needs=1 check 0" is called
1155 module_check_mount() {
1156 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1157 local _ret
1158 mount_needs=1
1159 [[ -d $_moddir ]] || return 1
1160 if [[ ! -f $_moddir/module-setup.sh ]]; then
1161 # if we do not have a check script, we are unconditionally included
1162 [[ -x $_moddir/check ]] || return 0
1163 mount_needs=1 $_moddir/check 0
1164 _ret=$?
1165 else
1166 unset check depends install installkernel
1167 check() { false; }
1168 . $_moddir/module-setup.sh
1169 check 0
1170 _ret=$?
1171 unset check depends install installkernel
1173 unset mount_needs
1174 return $_ret
1177 # module_depends <dracut module>
1178 # execute the depends() function of module-setup.sh of <dracut module>
1179 # or the "depends" script, if module-setup.sh is not found
1180 module_depends() {
1181 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1182 local _ret
1183 [[ -d $_moddir ]] || return 1
1184 if [[ ! -f $_moddir/module-setup.sh ]]; then
1185 # if we do not have a check script, we have no deps
1186 [[ -x $_moddir/check ]] || return 0
1187 $_moddir/check -d
1188 return $?
1189 else
1190 unset check depends install installkernel
1191 depends() { true; }
1192 . $_moddir/module-setup.sh
1193 depends
1194 _ret=$?
1195 unset check depends install installkernel
1196 return $_ret
1200 # module_install <dracut module>
1201 # execute the install() function of module-setup.sh of <dracut module>
1202 # or the "install" script, if module-setup.sh is not found
1203 module_install() {
1204 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1205 local _ret
1206 [[ -d $_moddir ]] || return 1
1207 if [[ ! -f $_moddir/module-setup.sh ]]; then
1208 [[ -x $_moddir/install ]] && . "$_moddir/install"
1209 return $?
1210 else
1211 unset check depends install installkernel
1212 install() { true; }
1213 . $_moddir/module-setup.sh
1214 install
1215 _ret=$?
1216 unset check depends install installkernel
1217 return $_ret
1221 # module_installkernel <dracut module>
1222 # execute the installkernel() function of module-setup.sh of <dracut module>
1223 # or the "installkernel" script, if module-setup.sh is not found
1224 module_installkernel() {
1225 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1226 local _ret
1227 [[ -d $_moddir ]] || return 1
1228 if [[ ! -f $_moddir/module-setup.sh ]]; then
1229 [[ -x $_moddir/installkernel ]] && . "$_moddir/installkernel"
1230 return $?
1231 else
1232 unset check depends install installkernel
1233 installkernel() { true; }
1234 . $_moddir/module-setup.sh
1235 installkernel
1236 _ret=$?
1237 unset check depends install installkernel
1238 return $_ret
1242 # check_mount <dracut module>
1243 # check_mount checks, if a dracut module is needed for the given
1244 # device and filesystem types in "${host_fs_types[@]}"
1245 check_mount() {
1246 local _mod=$1
1247 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1248 local _ret
1249 local _moddep
1251 [ "${#host_fs_types[*]}" -le 0 ] && return 1
1253 # If we are already scheduled to be loaded, no need to check again.
1254 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
1255 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
1257 # This should never happen, but...
1258 [[ -d $_moddir ]] || return 1
1260 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
1262 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
1263 return 1
1266 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
1267 module_check_mount $_mod; ret=$?
1269 # explicit module, so also accept ret=255
1270 [[ $ret = 0 || $ret = 255 ]] || return 1
1271 else
1272 # module not in our list
1273 if [[ $dracutmodules = all ]]; then
1274 # check, if we can and should install this module
1275 module_check_mount $_mod || return 1
1276 else
1277 # skip this module
1278 return 1
1283 for _moddep in $(module_depends $_mod); do
1284 # handle deps as if they were manually added
1285 [[ " $add_dracutmodules " == *\ $_moddep\ * ]] || \
1286 add_dracutmodules+=" $_moddep "
1287 [[ " $force_add_dracutmodules " == *\ $_moddep\ * ]] || \
1288 force_add_dracutmodules+=" $_moddep "
1289 # if a module we depend on fail, fail also
1290 if ! check_module $_moddep; then
1291 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
1292 return 1
1294 done
1296 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
1297 mods_to_load+=" $_mod "
1299 return 0
1302 # check_module <dracut module> [<use_as_dep>]
1303 # check if a dracut module is to be used in the initramfs process
1304 # if <use_as_dep> is set, then the process also keeps track
1305 # that the modules were checked for the dependency tracking process
1306 check_module() {
1307 local _mod=$1
1308 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1309 local _ret
1310 local _moddep
1311 # If we are already scheduled to be loaded, no need to check again.
1312 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
1313 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
1315 # This should never happen, but...
1316 [[ -d $_moddir ]] || return 1
1318 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
1320 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
1321 dinfo "dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
1322 return 1
1325 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
1326 if [[ " $force_add_dracutmodules " == *\ $_mod\ * ]]; then
1327 module_check $_mod 1; ret=$?
1328 else
1329 module_check $_mod 0; ret=$?
1331 # explicit module, so also accept ret=255
1332 [[ $ret = 0 || $ret = 255 ]] || return 1
1333 else
1334 # module not in our list
1335 if [[ $dracutmodules = all ]]; then
1336 # check, if we can and should install this module
1337 module_check $_mod || return 1
1338 else
1339 # skip this module
1340 return 1
1344 for _moddep in $(module_depends $_mod); do
1345 # handle deps as if they were manually added
1346 [[ " $add_dracutmodules " == *\ $_moddep\ * ]] || \
1347 add_dracutmodules+=" $_moddep "
1348 [[ " $force_add_dracutmodules " == *\ $_moddep\ * ]] || \
1349 force_add_dracutmodules+=" $_moddep "
1350 # if a module we depend on fail, fail also
1351 if ! check_module $_moddep; then
1352 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
1353 return 1
1355 done
1357 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
1358 mods_to_load+=" $_mod "
1360 return 0
1363 # for_each_module_dir <func>
1364 # execute "<func> <dracut module> 1"
1365 for_each_module_dir() {
1366 local _modcheck
1367 local _mod
1368 local _moddir
1369 local _func
1370 _func=$1
1371 for _moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1372 _mod=${_moddir##*/}; _mod=${_mod#[0-9][0-9]}
1373 $_func $_mod 1
1374 done
1376 # Report any missing dracut modules, the user has specified
1377 _modcheck="$add_dracutmodules $force_add_dracutmodules"
1378 [[ $dracutmodules != all ]] && _modcheck="$m $dracutmodules"
1379 for _mod in $_modcheck; do
1380 [[ " $mods_to_load " == *\ $_mod\ * ]] && continue
1381 [[ " $omit_dracutmodules " == *\ $_mod\ * ]] && continue
1382 derror "dracut module '$_mod' cannot be found or installed."
1383 done
1386 # Install a single kernel module along with any firmware it may require.
1387 # $1 = full path to kernel module to install
1388 install_kmod_with_fw() {
1389 # no need to go further if the module is already installed
1391 [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
1392 && return 0
1394 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -e "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}" ]]; then
1395 read ret < "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
1396 return $ret
1399 if [[ $omit_drivers ]]; then
1400 local _kmod=${1##*/}
1401 _kmod=${_kmod%.ko}
1402 _kmod=${_kmod/-/_}
1403 if [[ "$_kmod" =~ $omit_drivers ]]; then
1404 dinfo "Omitting driver $_kmod"
1405 return 0
1407 if [[ "${1##*/lib/modules/$kernel/}" =~ $omit_drivers ]]; then
1408 dinfo "Omitting driver $_kmod"
1409 return 0
1413 if [[ $silent_omit_drivers ]]; then
1414 local _kmod=${1##*/}
1415 _kmod=${_kmod%.ko}
1416 _kmod=${_kmod/-/_}
1417 [[ "$_kmod" =~ $silent_omit_drivers ]] && return 0
1418 [[ "${1##*/lib/modules/$kernel/}" =~ $silent_omit_drivers ]] && return 0
1421 inst_simple "$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
1422 ret=$?
1423 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1424 [[ -d "$DRACUT_KERNEL_LAZY_HASHDIR" ]] && \
1425 echo $ret > "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
1426 (($ret != 0)) && return $ret
1428 local _modname=${1##*/} _fwdir _found _fw
1429 _modname=${_modname%.ko*}
1430 for _fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do
1431 _found=''
1432 for _fwdir in $fw_dir; do
1433 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1434 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1435 _found=yes
1437 done
1438 if [[ $_found != yes ]]; then
1439 if ! [[ -d $(echo /sys/module/${_modname//-/_}|{ read a b; echo $a; }) ]]; then
1440 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
1441 "\"${_modname}.ko\""
1442 else
1443 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
1444 "\"${_modname}.ko\""
1447 done
1448 return 0
1451 # Do something with all the dependencies of a kernel module.
1452 # Note that kernel modules depend on themselves using the technique we use
1453 # $1 = function to call for each dependency we find
1454 # It will be passed the full path to the found kernel module
1455 # $2 = module to get dependencies for
1456 # rest of args = arguments to modprobe
1457 # _fderr specifies FD passed from surrounding scope
1458 for_each_kmod_dep() {
1459 local _func=$1 _kmod=$2 _cmd _modpath _options
1460 shift 2
1461 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
1462 while read _cmd _modpath _options; do
1463 [[ $_cmd = insmod ]] || continue
1464 $_func ${_modpath} || exit $?
1465 done
1469 dracut_kernel_post() {
1470 local _moddirname=${srcmods%%/lib/modules/*}
1471 local _pid
1473 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" ]]; then
1474 xargs -r modprobe -a ${_moddirname+-d ${_moddirname}/} \
1475 --ignore-install --show-depends --set-version $kernel \
1476 < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" 2>/dev/null \
1477 | sort -u \
1478 | while read _cmd _modpath _options; do
1479 [[ $_cmd = insmod ]] || continue
1480 echo "$_modpath"
1481 done > "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1484 if [[ $DRACUT_INSTALL ]] && [[ -z $_moddirname ]]; then
1485 xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1486 else
1487 while read _modpath; do
1488 local _destpath=$_modpath
1489 [[ $_moddirname ]] && _destpath=${_destpath##$_moddirname/}
1490 _destpath=${_destpath##*/lib/modules/$kernel/}
1491 inst_simple "$_modpath" "/lib/modules/$kernel/${_destpath}" || exit $?
1492 done < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1495 _pid=$(jobs -p | while read a ; do printf ":$a";done)
1496 _pid=${_pid##*:}
1498 if [[ $DRACUT_INSTALL ]]; then
1499 xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" \
1500 | while read line; do
1501 for _fwdir in $fw_dir; do
1502 echo $_fwdir/$line;
1503 done;
1504 done | xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a -o
1505 else
1506 for _fw in $(xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"); do
1507 for _fwdir in $fw_dir; do
1508 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1509 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1510 break
1512 done
1513 done
1516 wait $_pid
1519 for _f in modules.builtin.bin modules.builtin; do
1520 [[ $srcmods/$_f ]] && break
1521 done || {
1522 dfatal "No modules.builtin.bin and modules.builtin found!"
1523 return 1
1526 for _f in modules.builtin.bin modules.builtin modules.order; do
1527 [[ $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f"
1528 done
1530 # generate module dependencies for the initrd
1531 if [[ -d $initdir/lib/modules/$kernel ]] && \
1532 ! depmod -a -b "$initdir" $kernel; then
1533 dfatal "\"depmod -a $kernel\" failed."
1534 exit 1
1537 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && rm -fr -- "$DRACUT_KERNEL_LAZY_HASHDIR"
1540 module_is_host_only() {
1541 local _mod=$1
1542 _mod=${_mod##*/}
1543 _mod=${_mod%.ko}
1545 [[ " $add_drivers " == *\ ${_mod}\ * ]] && return 0
1547 # check if module is loaded
1548 for i in /sys/module/${_mod//-/_}; do
1549 [[ -d $i ]] && return 0
1550 done
1552 # check if module is loadable on the current kernel
1553 # this covers the case, where a new module is introduced
1554 # or a module was renamed
1555 # or a module changed from builtin to a module
1556 modinfo -F filename "$_mod" &>/dev/null || return 0
1558 return 1
1561 find_kernel_modules_by_path () {
1562 local _OLDIFS
1564 [[ -f "$srcmods/modules.dep" ]] || return 0
1566 _OLDIFS=$IFS
1567 IFS=:
1568 while read a rest; do
1569 [[ $a = */$1/* ]] || continue
1570 printf "%s\n" "$srcmods/$a"
1571 done < "$srcmods/modules.dep"
1572 IFS=$_OLDIFS
1573 return 0
1576 find_kernel_modules () {
1577 find_kernel_modules_by_path drivers
1580 # instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1581 # instmods [-c [-s]] <kernel subsystem>
1582 # install kernel modules along with all their dependencies.
1583 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1584 instmods() {
1585 [[ $no_kernel = yes ]] && return
1586 # called [sub]functions inherit _fderr
1587 local _fderr=9
1588 local _check=no
1589 local _silent=no
1590 if [[ $1 = '-c' ]]; then
1591 _check=yes
1592 shift
1595 if [[ $1 = '-s' ]]; then
1596 _silent=yes
1597 shift
1600 function inst1mod() {
1601 local _ret=0 _mod="$1"
1602 case $_mod in
1604 ( [[ "$_mpargs" ]] && echo $_mpargs
1605 find_kernel_modules_by_path "${_mod#=}" ) \
1606 | instmods
1607 ((_ret+=$?))
1609 --*) _mpargs+=" $_mod" ;;
1611 _mod=${_mod##*/}
1612 # if we are already installed, skip this module and go on
1613 # to the next one.
1614 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1615 [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko" ]]; then
1616 read _ret <"$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko"
1617 return $_ret
1620 if [[ $omit_drivers ]] && [[ "$1" =~ $omit_drivers ]]; then
1621 dinfo "Omitting driver ${_mod##$srcmods}"
1622 return 0
1625 # If we are building a host-specific initramfs and this
1626 # module is not already loaded, move on to the next one.
1627 [[ $hostonly ]] \
1628 && ! module_is_host_only "$_mod" \
1629 && return 0
1631 if [[ "$_check" = "yes" ]] || ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
1632 # We use '-d' option in modprobe only if modules prefix path
1633 # differs from default '/'. This allows us to use dracut with
1634 # old version of modprobe which doesn't have '-d' option.
1635 local _moddirname=${srcmods%%/lib/modules/*}
1636 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1638 # ok, load the module, all its dependencies, and any firmware
1639 # it may require
1640 for_each_kmod_dep install_kmod_with_fw $_mod \
1641 --set-version $kernel ${_moddirname} $_mpargs
1642 ((_ret+=$?))
1643 else
1644 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1645 echo $_mod >> "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist"
1648 esac
1649 return $_ret
1652 function instmods_1() {
1653 local _mod _mpargs
1654 if (($# == 0)); then # filenames from stdin
1655 while read _mod; do
1656 inst1mod "${_mod%.ko*}" || {
1657 if [[ "$_check" == "yes" ]]; then
1658 [[ "$_silent" == "no" ]] && dfatal "Failed to install module $_mod"
1659 return 1
1662 done
1664 while (($# > 0)); do # filenames as arguments
1665 inst1mod ${1%.ko*} || {
1666 if [[ "$_check" == "yes" ]]; then
1667 [[ "$_silent" == "no" ]] && dfatal "Failed to install module $1"
1668 return 1
1671 shift
1672 done
1673 return 0
1676 local _ret _filter_not_found='FATAL: Module .* not found.'
1677 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1678 # redirections, but that would make dracut require bash4 at least.
1679 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1680 | while read line; do [[ "$line" =~ $_filter_not_found ]] || echo $line;done | derror
1681 _ret=$?
1682 return $_ret
1684 # get_cpu_vendor
1685 # Only two values are returned: AMD or Intel
1686 get_cpu_vendor ()
1688 if grep -qE AMD /proc/cpuinfo; then
1689 printf "AMD"
1691 if grep -qE Intel /proc/cpuinfo; then
1692 printf "Intel"
1696 # get_host_ucode
1697 # Get the hosts' ucode file based on the /proc/cpuinfo
1698 get_ucode_file ()
1700 local family=`grep -E "cpu family" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1701 local model=`grep -E "model" /proc/cpuinfo |grep -v name | head -1 | sed s/.*:\ //`
1702 local stepping=`grep -E "stepping" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1704 if [[ "$(get_cpu_vendor)" == "AMD" ]]; then
1705 # If family greater or equal than 0x15
1706 if [[ $family -ge 21 ]]; then
1707 printf "microcode_amd_fam15h.bin"
1708 else
1709 printf "microcode_amd.bin"
1712 if [[ "$(get_cpu_vendor)" == "Intel" ]]; then
1713 # The /proc/cpuinfo are in decimal.
1714 printf "%02x-%02x-%02x" ${family} ${model} ${stepping}