]> git.ipfire.org Git - thirdparty/dracut.git/blame - dracut-functions.sh
dracut.sh: correct dropindirs_sort
[thirdparty/dracut.git] / dracut-functions.sh
CommitLineData
04b56f3a 1#!/bin/bash
cc02093d
HH
2# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3# ex: ts=8 sw=4 sts=4 et filetype=sh
04b56f3a 4#
33ee031c 5# functions used by dracut and other tools.
04b56f3a 6#
33ee031c 7# Copyright 2005-2009 Red Hat, Inc. All rights reserved.
04b56f3a
JK
8#
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.
13#
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.
18#
19# You should have received a copy of the GNU General Public License
0f9c78c1 20# along with this program. If not, see <http://www.gnu.org/licenses/>.
04b56f3a 21#
258de828 22export LC_MESSAGES=C
f4031e8a
HH
23
24if [[ $DRACUT_KERNEL_LAZY ]] && ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
25 if ! [[ -d "$initdir/.kernelmodseen" ]]; then
26 mkdir -p "$initdir/.kernelmodseen"
27 fi
28 DRACUT_KERNEL_LAZY_HASHDIR="$initdir/.kernelmodseen"
29fi
ec1aa3bc 30
eaa924b6
HH
31if [[ $initdir ]] && ! [[ -d $initdir ]]; then
32 mkdir -p "$initdir"
33fi
34
8d95b8b3 35# Generic substring function. If $2 is in $1, return 0.
520047c3 36strstr() { [[ $1 = *$2* ]]; }
8d95b8b3 37
f4031e8a
HH
38# find a binary. If we were not passed the full path directly,
39# search in the usual places to find the binary.
40find_binary() {
41 if [[ -z ${1##/*} ]]; then
b093aa2d
HH
42 if [[ -x $1 ]] || { [[ "$1" == *.so* ]] && ldd "$1" &>/dev/null; }; then
43 printf "%s\n" "$1"
f4031e8a
HH
44 return 0
45 fi
46 fi
47
b093aa2d 48 type -P "${1##*/}"
f4031e8a
HH
49}
50
0874654c
HH
51if ! [[ $dracutbasedir ]]; then
52 dracutbasedir=${BASH_SOURCE[0]%/*}
53 [[ $dracutbasedir = "dracut-functions" ]] && dracutbasedir="."
54 [[ $dracutbasedir ]] || dracutbasedir="."
55 dracutbasedir="$(readlink -f $dracutbasedir)"
56fi
57
f4031e8a
HH
58if ! [[ $DRACUT_INSTALL ]]; then
59 DRACUT_INSTALL=$(find_binary dracut-install)
60fi
61
62if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install ]]; then
63 DRACUT_INSTALL=$dracutbasedir/dracut-install
64fi
65
8d95b8b3
HH
66# Detect lib paths
67if ! [[ $libdirs ]] ; then
b093aa2d 68 if [[ "$(ldd /bin/sh)" == */lib64/* ]] &>/dev/null \
8d95b8b3
HH
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"
75 fi
76 export libdirs
77fi
78
79if ! [[ $kernel ]]; then
80 kernel=$(uname -r)
81 export kernel
82fi
83
84srcmods="/lib/modules/$kernel/"
85[[ $drivers_dir ]] && {
b093aa2d 86 if vercmp "$(modprobe --version | cut -d' ' -f3)" lt 3.7; then
8d95b8b3
HH
87 dfatal 'To use --kmoddir option module-init-tools >= 3.7 is required.'
88 exit 1
89 fi
90 srcmods="$drivers_dir"
91}
92export srcmods
93
d6d53f60 94if ! type dinfo >/dev/null 2>&1; then
552ecca6 95 . "$dracutbasedir/dracut-logger.sh"
e103615b
96 dlog_init
97fi
98
593b315c
HH
99if ! [[ $initdir ]]; then
100 dfatal "initdir not set"
101 exit 1
102fi
103
7e2bca48
HH
104# export standard hookdirs
105[[ $hookdirs ]] || {
4fed3ddf 106 hookdirs="cmdline pre-udev pre-trigger netroot "
6e3cc00f 107 hookdirs+="initqueue initqueue/settled initqueue/online initqueue/finished initqueue/timeout "
eef7649e 108 hookdirs+="pre-mount pre-pivot cleanup mount "
2b6dd6a1 109 hookdirs+="emergency shutdown-emergency pre-shutdown shutdown "
7e2bca48
HH
110 export hookdirs
111}
112
8d85429a
HH
113dracut_need_initqueue() {
114 >"$initdir/lib/dracut/need-initqueue"
115}
116
117dracut_module_included() {
b093aa2d 118 [[ "$mods_to_load $modules_loaded" == *$@* ]]
8d85429a
HH
119}
120
87122afc
121# Create all subdirectories for given path without creating the last element.
122# $1 = path
b093aa2d
HH
123mksubdirs() {
124 [[ -e ${1%/*} ]] || mkdir -m 0755 -p -- "${1%/*}"
125}
87122afc 126
22ecea45
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
ecf42850 131vercmp() {
b093aa2d 132 local _n1=${1//./ } _op=$2 _n2=${3//./ } _i _res
ecf42850 133
29b10e65 134 for ((_i=0; ; _i++))
22ecea45 135 do
29b10e65
HH
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
22ecea45
139 else continue
140 fi
141 break
ecf42850 142 done
ecf42850 143
29b10e65
HH
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));;
22ecea45 151 esac
ecf42850
152}
153
7e2bca48
HH
154# is_func <command>
155# Check whether $1 is a function.
95d2dabc 156is_func() {
b093aa2d 157 [[ "$(type -t "$1")" = "function" ]]
95d2dabc
HH
158}
159
87122afc
160# Function prints global variables in format name=value line by line.
161# $@ = list of global variables' name
162print_vars() {
29b10e65 163 local _var _value
87122afc 164
b093aa2d 165 for _var in "$@"
87122afc 166 do
b093aa2d
HH
167 eval printf -v _value "%s" "\$$_var"
168 [[ ${_value} ]] && printf '%s="%s"\n' "$_var" "$_value"
87122afc
169 done
170}
171
7e2bca48
HH
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
626d9eba 178normalize_path() {
c44e3cb4
MS
179 shopt -q -s extglob
180 set -- "${1//+(\/)//}"
181 shopt -q -u extglob
182 echo "${1%/}"
626d9eba 183}
d4bb4316 184
7e2bca48
HH
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
d4bb4316 191convert_abs_rel() {
6d2a7942 192 local __current __absolute __abssize __cursize __newpath
c44e3cb4 193 local -i __i __level
d4bb4316 194
c44e3cb4
MS
195 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
196
c1609dd4
MS
197 # corner case #1 - self looping link
198 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
199
200 # corner case #2 - own dir link
201 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
202
6d2a7942
HH
203 IFS="/" __current=($1)
204 IFS="/" __absolute=($2)
d4bb4316
HH
205
206 __abssize=${#__absolute[@]}
207 __cursize=${#__current[@]}
208
b093aa2d 209 while [[ "${__absolute[__level]}" == "${__current[__level]}" ]]
d4bb4316
HH
210 do
211 (( __level++ ))
212 if (( __level > __abssize || __level > __cursize ))
213 then
214 break
215 fi
216 done
217
218 for ((__i = __level; __i < __cursize-1; __i++))
219 do
220 if ((__i > __level))
221 then
222 __newpath=$__newpath"/"
223 fi
224 __newpath=$__newpath".."
225 done
226
227 for ((__i = __level; __i < __abssize; __i++))
228 do
229 if [[ -n $__newpath ]]
230 then
231 __newpath=$__newpath"/"
232 fi
233 __newpath=$__newpath${__absolute[__i]}
234 done
235
236 echo "$__newpath"
237}
238
b093aa2d 239if [[ "$(ln --help)" == *--relative* ]]; then
a20d24de
HH
240 ln_r() {
241 ln -sfnr "${initdir}/$1" "${initdir}/$2"
242 }
243else
244 ln_r() {
245 local _source=$1
246 local _dest=$2
247 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
b093aa2d 248 ln -sfn -- "$(convert_abs_rel "${_dest}" "${_source}")" "${initdir}/${_dest}"
a20d24de
HH
249 }
250fi
251
ff063663
DY
252get_persistent_dev() {
253 local i _tmp _dev
254
255 _dev=$(udevadm info --query=name --name="$1" 2>/dev/null)
256 [ -z "$_dev" ] && return
257
9037b63e 258 for i in /dev/mapper/* /dev/disk/by-uuid/* /dev/disk/by-id/*; do
15c5c072 259 [[ $i == /dev/mapper/mpath* ]] && continue
ff063663
DY
260 _tmp=$(udevadm info --query=name --name="$i" 2>/dev/null)
261 if [ "$_tmp" = "$_dev" ]; then
b093aa2d 262 printf -- "%s" "$i"
ff063663
DY
263 return
264 fi
265 done
266}
267
7e2bca48 268# get_fs_env <device>
b093aa2d 269# Get and set the ID_FS_TYPE variable from udev for a device.
7e2bca48 270# Example:
b093aa2d 271# $ get_fs_env /dev/sda2; echo $ID_FS_TYPE
7e2bca48 272# ext4
9ede1929 273get_fs_env() {
0e979d48
HH
274 local evalstr
275 local found
276
7c179686 277 [[ $1 ]] || return
29b10e65 278 unset ID_FS_TYPE
b093aa2d 279 if ID_FS_TYPE=$(udevadm info --query=env --name="$1" \
0e979d48 280 | { while read line; do
b093aa2d
HH
281 [[ "$line" == DEVPATH\=* ]] && found=1;
282 if [[ "$line" == ID_FS_TYPE\=* ]]; then
283 printf "%s" "${line#ID_FS_TYPE=}";
284 exit 0;
285 fi
0e979d48 286 done; [[ $found ]] && exit 0; exit 1; }) ; then
b093aa2d
HH
287 if [[ $ID_FS_TYPE ]]; then
288 printf "%s" "$ID_FS_TYPE"
289 return 0
290 fi
0e979d48
HH
291 return 1
292 fi
d8b9844c 293
0e979d48 294 # Fallback, if we don't have udev information
a5cde2dd 295 if find_binary blkid >/dev/null; then
b093aa2d 296 ID_FS_TYPE=$(blkid -u filesystem -o export -- "$1" \
a5cde2dd 297 | while read line; do
b093aa2d
HH
298 if [[ "$line" == TYPE\=* ]]; then
299 printf "%s" "${line#TYPE=}";
300 exit 0;
301 fi
a5cde2dd 302 done)
b093aa2d
HH
303 if [[ $ID_FS_TYPE ]]; then
304 printf "%s" "$ID_FS_TYPE"
305 return 0
306 fi
7c179686 307 fi
a5cde2dd
HH
308 return 1
309}
9ede1929 310
7e2bca48
HH
311# get_maj_min <device>
312# Prints the major and minor of a device node.
313# Example:
314# $ get_maj_min /dev/sda2
315# 8:2
480d772f 316get_maj_min() {
b093aa2d
HH
317 local _maj _min
318 read _maj _min < <(stat -L -c '%t %T' "$1" 2>/dev/null)
319 printf "%s" "$((0x$_maj)):$((0x$_min))"
480d772f
HH
320}
321
7e2bca48
HH
322# find_block_device <mountpoint>
323# Prints the major and minor number of the block device
324# for a given mountpoint.
325# Unless $use_fstab is set to "yes" the functions
326# uses /proc/self/mountinfo as the primary source of the
327# information and only falls back to /etc/fstab, if the mountpoint
328# is not found there.
329# Example:
330# $ find_block_device /usr
331# 8:4
f76ef3aa 332find_block_device() {
9d36d4fb 333 local _majmin _dev _majmin _find_mpt
c8d685c9 334 _find_mpt="$1"
0b706743 335 if [[ $use_fstab != yes ]]; then
9d36d4fb
HH
336 [[ -d $_find_mpt/. ]]
337 while read _majmin _dev; do
338 if [[ -b $_dev ]]; then
339 if ! [[ $_majmin ]] || [[ $_majmin == 0:* ]]; then
340 read _majmin < <(get_maj_min $_dev)
341 fi
342 if [[ $_majmin ]]; then
343 echo $_majmin
344 else
345 echo $_dev
346 fi
347 return 0
348 fi
349 if [[ $_dev = *:* ]]; then
350 echo $_dev
351 return 0
cc02093d 352 fi
5e601454 353 done < <(findmnt -e -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt")
7c179686 354 fi
0b706743 355 # fall back to /etc/fstab
9d36d4fb
HH
356
357 while read _majmin _dev; do
358 if ! [[ $_dev ]]; then
359 _dev="$_majmin"
360 unset _majmin
cc02093d 361 fi
9d36d4fb
HH
362 if [[ -b $_dev ]]; then
363 [[ $_majmin ]] || read _majmin < <(get_maj_min $_dev)
364 if [[ $_majmin ]]; then
365 echo $_majmin
366 else
367 echo $_dev
368 fi
369 return 0
370 fi
371 if [[ $_dev = *:* ]]; then
372 echo $_dev
373 return 0
374 fi
5e601454 375 done < <(findmnt -e --fstab -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt")
0b706743
376
377 return 1
17829e94
VL
378}
379
9d36d4fb
HH
380# find_mp_fstype <mountpoint>
381# Echo the filesystem type for a given mountpoint.
7e2bca48
HH
382# /proc/self/mountinfo is taken as the primary source of information
383# and /etc/fstab is used as a fallback.
384# No newline is appended!
385# Example:
9d36d4fb 386# $ find_mp_fstype /;echo
7e2bca48 387# ext4
9d36d4fb
HH
388find_mp_fstype() {
389 local _fs
7ae5d9d1 390
9d36d4fb
HH
391 if [[ $use_fstab != yes ]]; then
392 while read _fs; do
393 [[ $_fs ]] || continue
394 [[ $_fs = "autofs" ]] && continue
395 echo -n $_fs
396 return 0
5e601454 397 done < <(findmnt -e -v -n -o 'FSTYPE' --target "$1")
9d36d4fb
HH
398 fi
399
400 while read _fs; do
401 [[ $_fs ]] || continue
402 [[ $_fs = "autofs" ]] && continue
403 echo -n $_fs
404 return 0
5e601454 405 done < <(findmnt --fstab -e -v -n -o 'FSTYPE' --target "$1")
7ae5d9d1
HH
406
407 return 1
408}
409
9d36d4fb
HH
410# find_dev_fstype <device>
411# Echo the filesystem type for a given device.
81672479
HH
412# /proc/self/mountinfo is taken as the primary source of information
413# and /etc/fstab is used as a fallback.
414# No newline is appended!
415# Example:
9d36d4fb 416# $ find_dev_fstype /dev/sda2;echo
81672479 417# ext4
9d36d4fb 418find_dev_fstype() {
5e601454 419 local _find_dev _fs
9d36d4fb
HH
420 _find_dev="$1"
421 [[ "$_find_dev" = /dev* ]] || _find_dev="/dev/block/$_find_dev"
5e601454
HH
422
423 if [[ $use_fstab != yes ]]; then
424 while read _fs; do
425 [[ $_fs ]] || continue
426 [[ $_fs = "autofs" ]] && continue
427 echo -n $_fs
428 return 0
429 done < <(findmnt -e -v -n -o 'FSTYPE' --source "$_find_dev")
430 fi
431
432 while read _fs; do
433 [[ $_fs ]] || continue
434 [[ $_fs = "autofs" ]] && continue
435 echo -n $_fs
436 return 0
437 done < <(findmnt --fstab -e -v -n -o 'FSTYPE' --source "$_find_dev")
438
439 return 1
440
81672479
HH
441}
442
7ae5d9d1 443# finds the major:minor of the block device backing the root filesystem.
f76ef3aa
VL
444find_root_block_device() { find_block_device /; }
445
7e2bca48 446# for_each_host_dev_fs <func>
d351541e 447# Execute "<func> <dev> <filesystem>" for every "<dev> <fs>" pair found
7e2bca48 448# in ${host_fs_types[@]}
480d772f
HH
449for_each_host_dev_fs()
450{
451 local _func="$1"
d0096de7 452 local _dev
2efa546f 453 local _ret=1
fd191a7b
HH
454
455 [[ "${!host_fs_types[@]}" ]] || return 0
456
d351541e
HH
457 for _dev in "${!host_fs_types[@]}"; do
458 $_func "$_dev" "${host_fs_types[$_dev]}" && _ret=0
480d772f 459 done
2efa546f 460 return $_ret
480d772f
HH
461}
462
fd191a7b
HH
463host_fs_all()
464{
465 echo "${host_fs_types[@]}"
466}
467
17829e94
VL
468# Walk all the slave relationships for a given block device.
469# Stop when our helper function returns success
470# $1 = function to call on every found block device
471# $2 = block device in major:minor format
0b706743 472check_block_and_slaves() {
29b10e65 473 local _x
17829e94
VL
474 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
475 "$1" $2 && return
533d7dc4 476 check_vol_slaves "$@" && return 0
3478b314 477 if [[ -f /sys/dev/block/$2/../dev ]]; then
0b706743 478 check_block_and_slaves $1 $(cat "/sys/dev/block/$2/../dev") && return 0
a3afcf2a 479 fi
17829e94 480 [[ -d /sys/dev/block/$2/slaves ]] || return 1
29b10e65 481 for _x in /sys/dev/block/$2/slaves/*/dev; do
d82e0d7d 482 [[ -f $_x ]] || continue
29b10e65 483 check_block_and_slaves $1 $(cat "$_x") && return 0
17829e94
VL
484 done
485 return 1
486}
487
83e0dc7a
DY
488check_block_and_slaves_all() {
489 local _x _ret=1
490 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
491 if "$1" $2; then
492 _ret=0
493 fi
494 check_vol_slaves "$@" && return 0
495 if [[ -f /sys/dev/block/$2/../dev ]]; then
496 check_block_and_slaves_all $1 $(cat "/sys/dev/block/$2/../dev") && _ret=0
497 fi
498 [[ -d /sys/dev/block/$2/slaves ]] || return 1
499 for _x in /sys/dev/block/$2/slaves/*/dev; do
500 [[ -f $_x ]] || continue
501 check_block_and_slaves_all $1 $(cat "$_x") && _ret=0
502 done
503 return $_ret
504}
505# for_each_host_dev_and_slaves <func>
506# Execute "<func> <dev>" for every "<dev>" found
507# in ${host_devs[@]} and their slaves
508for_each_host_dev_and_slaves_all()
509{
510 local _func="$1"
511 local _dev
512 local _ret=1
fd191a7b
HH
513
514 [[ "${host_devs[@]}" ]] || return 0
515
83e0dc7a
DY
516 for _dev in ${host_devs[@]}; do
517 [[ -b "$_dev" ]] || continue
83e0dc7a
DY
518 if check_block_and_slaves_all $_func $(get_maj_min $_dev); then
519 _ret=0
520 fi
521 done
522 return $_ret
523}
524
525for_each_host_dev_and_slaves()
526{
527 local _func="$1"
528 local _dev
fd191a7b
HH
529
530 [[ "${host_devs[@]}" ]] || return 0
531
83e0dc7a
DY
532 for _dev in ${host_devs[@]}; do
533 [[ -b "$_dev" ]] || continue
a999414e 534 check_block_and_slaves $_func $(get_maj_min $_dev) && return 0
83e0dc7a
DY
535 done
536 return 1
537}
538
533d7dc4
HH
539# ugly workaround for the lvm design
540# There is no volume group device,
541# so, there are no slave devices for volume groups.
542# Logical volumes only have the slave devices they really live on,
543# but you cannot create the logical volume without the volume group.
95bde758 544# And the volume group might be bigger than the devices the LV needs.
533d7dc4 545check_vol_slaves() {
29b10e65 546 local _lv _vg _pv
0b706743 547 for i in /dev/mapper/*; do
c4e48eae 548 _lv=$(get_maj_min $i)
29b10e65
HH
549 if [[ $_lv = $2 ]]; then
550 _vg=$(lvm lvs --noheadings -o vg_name $i 2>/dev/null)
cc02093d 551 # strip space
29b10e65
HH
552 _vg=$(echo $_vg)
553 if [[ $_vg ]]; then
554 for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2>/dev/null)
0b706743 555 do
c4e48eae 556 check_block_and_slaves $1 $(get_maj_min $_pv) && return 0
cc02093d
HH
557 done
558 fi
559 fi
533d7dc4
HH
560 done
561 return 1
562}
563
8aa99268
HH
564# fs_get_option <filesystem options> <search for option>
565# search for a specific option in a bunch of filesystem options
566# and return the value
567fs_get_option() {
568 local _fsopts=$1
569 local _option=$2
570 local OLDIFS="$IFS"
571 IFS=,
572 set -- $_fsopts
573 IFS="$OLDIFS"
574 while [ $# -gt 0 ]; do
575 case $1 in
576 $_option=*)
577 echo ${1#${_option}=}
578 break
579 esac
580 shift
581 done
582}
583
f4031e8a 584if [[ $DRACUT_INSTALL ]]; then
89d44e72
HH
585 [[ $DRACUT_RESOLVE_LAZY ]] || export DRACUT_RESOLVE_DEPS=1
586 inst_dir() {
587 [[ -e ${initdir}/"$1" ]] && return 0 # already there
f4031e8a
HH
588 $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@"
589 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@" || :
89d44e72 590 }
19612483 591
89d44e72
HH
592 inst() {
593 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
f4031e8a
HH
594 #dinfo "$DRACUT_INSTALL -l $@"
595 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
596 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
89d44e72
HH
597 }
598
599 inst_simple() {
600 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
601 [[ -e $1 ]] || return 1 # no source
f4031e8a
HH
602 $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@"
603 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@" || :
89d44e72
HH
604 }
605
606 inst_symlink() {
607 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
608 [[ -L $1 ]] || return 1
f4031e8a
HH
609 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
610 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
89d44e72
HH
611 }
612
613 dracut_install() {
06e9f887 614 local ret
f4031e8a
HH
615 #dinfo "initdir=$initdir $DRACUT_INSTALL -l $@"
616 $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
06e9f887
DY
617 ret=$?
618 (($ret != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
619 return $ret
89d44e72
HH
620 }
621
622 inst_library() {
623 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
624 [[ -e $1 ]] || return 1 # no source
f4031e8a
HH
625 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
626 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
89d44e72
HH
627 }
628
629 inst_binary() {
f4031e8a
HH
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} "$@" || :
89d44e72
HH
632 }
633
634 inst_script() {
f4031e8a
HH
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} "$@" || :
89d44e72
HH
637 }
638
639else
640
641 # Install a directory, keeping symlinks as on the original system.
642 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
643 # will create ${initdir}/lib64, ${initdir}/lib64/file,
644 # and a symlink ${initdir}/lib -> lib64.
645 inst_dir() {
646 [[ -e ${initdir}/"$1" ]] && return 0 # already there
647
648 local _dir="$1" _part="${1%/*}" _file
649 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
650 _dir="$_part $_dir"
651 _part=${_part%/*}
652 done
653
654 # iterate over parent directories
655 for _file in $_dir; do
656 [[ -e "${initdir}/$_file" ]] && continue
657 if [[ -L $_file ]]; then
658 inst_symlink "$_file"
659 else
19612483 660 # create directory
89d44e72
HH
661 mkdir -m 0755 -p "${initdir}/$_file" || return 1
662 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
663 chmod u+w "${initdir}/$_file"
664 fi
665 done
666 }
667
668 # $1 = file to copy to ramdisk
669 # $2 (optional) Name for the file on the ramdisk
670 # Location of the image dir is assumed to be $initdir
671 # We never overwrite the target if it exists.
672 inst_simple() {
673 [[ -f "$1" ]] || return 1
b093aa2d 674 [[ "$1" == */* ]] || return 1
89d44e72
HH
675 local _src=$1 _target="${2:-$1}"
676
677 [[ -L $_src ]] && { inst_symlink $_src $_target; return $?; }
678
679 if ! [[ -d ${initdir}/$_target ]]; then
680 [[ -e ${initdir}/$_target ]] && return 0
681 [[ -L ${initdir}/$_target ]] && return 0
682 [[ -d "${initdir}/${_target%/*}" ]] || inst_dir "${_target%/*}"
19612483 683 fi
89d44e72
HH
684 if [[ $DRACUT_FIPS_MODE ]]; then
685 # install checksum files also
686 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
687 inst "${_src%/*}/.${_src##*/}.hmac" "${_target%/*}/.${_target##*/}.hmac"
688 fi
00083a09
MB
689 if [[ -e "/lib/fipscheck/${_src##*/}.hmac" ]]; then
690 inst "/lib/fipscheck/${_src##*/}.hmac" "/lib/fipscheck/${_target##*/}.hmac"
691 fi
692 if [[ -e "/lib64/fipscheck/${_src##*/}.hmac" ]]; then
693 inst "/lib64/fipscheck/${_src##*/}.hmac" "/lib64/fipscheck/${_target##*/}.hmac"
694 fi
89d44e72
HH
695 fi
696 ddebug "Installing $_src"
697 cp --reflink=auto --sparse=auto -pfL "$_src" "${initdir}/$_target"
698 }
19612483 699
89d44e72
HH
700 # same as above, but specialized for symlinks
701 inst_symlink() {
702 local _src=$1 _target=${2:-$1} _realsrc
b093aa2d 703 [[ "$1" == */* ]] || return 1
89d44e72
HH
704 [[ -L $1 ]] || return 1
705 [[ -L $initdir/$_target ]] && return 0
706 _realsrc=$(readlink -f "$_src")
707 if ! [[ -e $initdir/$_realsrc ]]; then
708 if [[ -d $_realsrc ]]; then
709 inst_dir "$_realsrc"
710 else
711 inst "$_realsrc"
712 fi
713 fi
714 [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}"
3f590c78 715
89d44e72
HH
716 ln_r "${_realsrc}" "${_target}"
717 }
22048b44 718
89d44e72
HH
719 # Same as above, but specialized to handle dynamic libraries.
720 # It handles making symlinks according to how the original library
721 # is referenced.
722 inst_library() {
723 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
b093aa2d 724 [[ "$1" == */* ]] || return 1
89d44e72
HH
725 [[ -e $initdir/$_dest ]] && return 0
726 if [[ -L $_src ]]; then
727 if [[ $DRACUT_FIPS_MODE ]]; then
728 # install checksum files also
729 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
730 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
731 fi
00083a09
MB
732 if [[ -e "/lib/fipscheck/${_src##*/}.hmac" ]]; then
733 inst "/lib/fipscheck/${_src##*/}.hmac" "/lib/fipscheck/${_dest##*/}.hmac"
734 fi
735 if [[ -e "/lib64/fipscheck/${_src##*/}.hmac" ]]; then
736 inst "/lib64/fipscheck/${_src##*/}.hmac" "/lib64/fipscheck/${_dest##*/}.hmac"
737 fi
89d44e72
HH
738 fi
739 _reallib=$(readlink -f "$_src")
740 inst_simple "$_reallib" "$_reallib"
741 inst_dir "${_dest%/*}"
742 ln_r "${_reallib}" "${_dest}"
743 else
744 inst_simple "$_src" "$_dest"
745 fi
22048b44 746
89d44e72
HH
747 # Create additional symlinks. See rev_symlinks description.
748 for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do
749 [[ ! -e $initdir/$_symlink ]] && {
750 ddebug "Creating extra symlink: $_symlink"
751 inst_symlink $_symlink
752 }
753 done
754 }
755
756 # Same as above, but specialized to install binary executables.
757 # Install binary executable, and all shared library dependencies, if any.
758 inst_binary() {
759 local _bin _target
760 _bin=$(find_binary "$1") || return 1
761 _target=${2:-$_bin}
762 [[ -e $initdir/$_target ]] && return 0
763 local _file _line
764 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
765 # I love bash!
766 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
767 [[ $_line = 'not a dynamic executable' ]] && break
768
769 if [[ $_line =~ $_so_regex ]]; then
770 _file=${BASH_REMATCH[1]}
771 [[ -e ${initdir}/$_file ]] && continue
772 inst_library "$_file"
773 continue
774 fi
775
b093aa2d 776 if [[ $_line == *not\ found* ]]; then
89d44e72
HH
777 dfatal "Missing a shared library required by $_bin."
778 dfatal "Run \"ldd $_bin\" to find out what it is."
779 dfatal "$_line"
780 dfatal "dracut cannot create an initrd."
781 exit 1
782 fi
783 done
784 inst_simple "$_bin" "$_target"
785 }
786
787 # same as above, except for shell scripts.
788 # If your shell script does not start with shebang, it is not a shell script.
789 inst_script() {
790 local _bin
791 _bin=$(find_binary "$1") || return 1
792 shift
793 local _line _shebang_regex
794 read -r -n 80 _line <"$_bin"
795 # If debug is set, clean unprintable chars to prevent messing up the term
796 [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]')
797 _shebang_regex='(#! *)(/[^ ]+).*'
798 [[ $_line =~ $_shebang_regex ]] || return 1
799 inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@"
800 }
801
802 # general purpose installation function
803 # Same args as above.
804 inst() {
805 local _x
806
807 case $# in
808 1) ;;
809 2) [[ ! $initdir && -d $2 ]] && export initdir=$2
810 [[ $initdir = $2 ]] && set $1;;
811 3) [[ -z $initdir ]] && export initdir=$2
812 set $1 $3;;
813 *) dfatal "inst only takes 1 or 2 or 3 arguments"
814 exit 1;;
815 esac
816 for _x in inst_symlink inst_script inst_binary inst_simple; do
817 $_x "$@" && return 0
818 done
819 return 1
820 }
821
822 # dracut_install [-o ] <file> [<file> ... ]
823 # Install <file> to the initramfs image
824 # -o optionally install the <file> and don't fail, if it is not there
825 dracut_install() {
826 local _optional=no
827 if [[ $1 = '-o' ]]; then
828 _optional=yes
829 shift
830 fi
831 while (($# > 0)); do
832 if ! inst "$1" ; then
833 if [[ $_optional = yes ]]; then
834 dinfo "Skipping program $1 as it cannot be found and is" \
835 "flagged to be optional"
836 else
837 dfatal "Failed to install $1"
838 exit 1
839 fi
840 fi
841 shift
842 done
843 }
844
845fi
36b24d7c 846
a10a1416
847# find symlinks linked to given library file
848# $1 = library file
849# Function searches for symlinks by stripping version numbers appended to
850# library filename, checks if it points to the same target and finally
851# prints the list of symlinks to stdout.
852#
853# Example:
854# rev_lib_symlinks libfoo.so.8.1
855# output: libfoo.so.8 libfoo.so
856# (Only if libfoo.so.8 and libfoo.so exists on host system.)
857rev_lib_symlinks() {
858 [[ ! $1 ]] && return 0
859
860 local fn="$1" orig="$(readlink -f "$1")" links=''
861
b093aa2d 862 [[ ${fn} == *.so.* ]] || return 1
a10a1416
863
864 until [[ ${fn##*.} == so ]]; do
865 fn="${fn%.*}"
866 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
867 done
868
0b706743 869 echo "${links}"
a10a1416
870}
871
62073c30
CG
872# attempt to install any programs specified in a udev rule
873inst_rule_programs() {
874 local _prog _bin
875
876 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
877 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
76d5c79e 878 _bin=""
604d1eb2
HH
879 if [ -x ${udevdir}/$_prog ]; then
880 _bin=${udevdir}/$_prog
34bd2be2 881 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
604d1eb2 882 _bin=$(find_binary "$_prog") || {
cc218087 883 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
604d1eb2
HH
884 continue;
885 }
886 fi
887
76d5c79e 888 [[ $_bin ]] && dracut_install "$_bin"
604d1eb2
HH
889 done
890 fi
da9c5181
HH
891 if grep -qE 'RUN[+=]=?"[^ "]+' "$1"; then
892 for _prog in $(grep -E 'RUN[+=]=?"[^ "]+' "$1" | sed -r 's/.*RUN[+=]=?"([^ "]+).*/\1/'); do
76d5c79e 893 _bin=""
604d1eb2
HH
894 if [ -x ${udevdir}/$_prog ]; then
895 _bin=${udevdir}/$_prog
34bd2be2 896 elif [[ "${_prog/\$env\{/}" == "$_prog" ]] && [[ "${_prog}" != "/sbin/initqueue" ]]; then
604d1eb2 897 _bin=$(find_binary "$_prog") || {
cc218087 898 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
604d1eb2
HH
899 continue;
900 }
901 fi
902
76d5c79e 903 [[ $_bin ]] && dracut_install "$_bin"
604d1eb2
HH
904 done
905 fi
65d1a8a4
HH
906 if grep -qE 'IMPORT\{program\}==?"[^ "]+' "$1"; then
907 for _prog in $(grep -E 'IMPORT\{program\}==?"[^ "]+' "$1" | sed -r 's/.*IMPORT\{program\}==?"([^ "]+).*/\1/'); do
76d5c79e 908 _bin=""
604d1eb2
HH
909 if [ -x ${udevdir}/$_prog ]; then
910 _bin=${udevdir}/$_prog
34bd2be2 911 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
62073c30 912 _bin=$(find_binary "$_prog") || {
cc218087 913 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
62073c30
CG
914 continue;
915 }
916 fi
917
76d5c79e 918 [[ $_bin ]] && dracut_install "$_bin"
62073c30
CG
919 done
920 fi
921}
922
e0a39336
HH
923# attempt to install any programs specified in a udev rule
924inst_rule_group_owner() {
925 local i
926
927 if grep -qE 'OWNER=?"[^ "]+' "$1"; then
928 for i in $(grep -E 'OWNER=?"[^ "]+' "$1" | sed -r 's/.*OWNER=?"([^ "]+).*/\1/'); do
929 if ! egrep -q "^$i:" "$initdir/etc/passwd" 2>/dev/null; then
930 egrep "^$i:" /etc/passwd 2>/dev/null >> "$initdir/etc/passwd"
931 fi
932 done
933 fi
934 if grep -qE 'GROUP=?"[^ "]+' "$1"; then
935 for i in $(grep -E 'GROUP=?"[^ "]+' "$1" | sed -r 's/.*GROUP=?"([^ "]+).*/\1/'); do
936 if ! egrep -q "^$i:" "$initdir/etc/group" 2>/dev/null; then
937 egrep "^$i:" /etc/group 2>/dev/null >> "$initdir/etc/group"
938 fi
939 done
940 fi
941}
942
8d85429a
HH
943inst_rule_initqueue() {
944 if grep -q -F initqueue "$1"; then
945 dracut_need_initqueue
946 fi
947}
948
f04dc5f3
VL
949# udev rules always get installed in the same place, so
950# create a function to install them to make life simpler.
0b706743 951inst_rules() {
29b10e65 952 local _target=/etc/udev/rules.d _rule _found
fd2312e0 953
604d1eb2 954 inst_dir "${udevdir}/rules.d"
29b10e65
HH
955 inst_dir "$_target"
956 for _rule in "$@"; do
256cba18 957 if [ "${_rule#/}" = "$_rule" ]; then
604d1eb2 958 for r in ${udevdir}/rules.d /etc/udev/rules.d; do
76f5fa54
HH
959 if [[ -f $r/$_rule ]]; then
960 _found="$r/$_rule"
62073c30 961 inst_rule_programs "$_found"
e0a39336 962 inst_rule_group_owner "$_found"
8d85429a 963 inst_rule_initqueue "$_found"
604d1eb2 964 inst_simple "$_found"
76f5fa54
HH
965 fi
966 done
967 fi
c97e1a76 968 for r in '' ./ $dracutbasedir/rules.d/; do
29b10e65
HH
969 if [[ -f ${r}$_rule ]]; then
970 _found="${r}$_rule"
62073c30 971 inst_rule_programs "$_found"
e0a39336 972 inst_rule_group_owner "$_found"
8d85429a 973 inst_rule_initqueue "$_found"
604d1eb2 974 inst_simple "$_found" "$_target/${_found##*/}"
c97e1a76
HH
975 fi
976 done
29b10e65 977 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
f04dc5f3
VL
978 done
979}
980
c46cda82
HH
981prepare_udev_rules() {
982 [ -z "$UDEVVERSION" ] && export UDEVVERSION=$(udevadm --version)
983
984 for f in "$@"; do
985 f="${initdir}/etc/udev/rules.d/$f"
986 [ -e "$f" ] || continue
987 while read line; do
988 if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
989 if [ $UDEVVERSION -ge 174 ]; then
990 printf '%sIMPORT{builtin}="path_id"\n' "${line%%IMPORT PATH_ID}"
991 else
992 printf '%sIMPORT{program}="path_id %%p"\n' "${line%%IMPORT PATH_ID}"
993 fi
994 elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
995 if [ $UDEVVERSION -ge 176 ]; then
996 printf '%sIMPORT{builtin}="blkid"\n' "${line%%IMPORT BLKID}"
997 else
998 printf '%sIMPORT{program}="/sbin/blkid -o udev -p $tempnode"\n' "${line%%IMPORT BLKID}"
999 fi
1000 else
1001 echo "$line"
1002 fi
1003 done < "${f}" > "${f}.new"
1004 mv "${f}.new" "$f"
1005 done
1006}
1007
53f95456
VL
1008# install function specialized for hooks
1009# $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
1010# All hooks should be POSIX/SuS compliant, they will be sourced by init.
1011inst_hook() {
cdad82fd 1012 if ! [[ -f $3 ]]; then
e27770e1
1013 dfatal "Cannot install a hook ($3) that does not exist."
1014 dfatal "Aborting initrd creation."
cc02093d 1015 exit 1
b093aa2d 1016 elif ! [[ "$hookdirs" == *$1* ]]; then
3b403b32 1017 dfatal "No such hook type $1. Aborting initrd creation."
cc02093d 1018 exit 1
cdad82fd 1019 fi
5a84ac3f 1020 inst_simple "$3" "/lib/dracut/hooks/${1}/${2}-${3##*/}"
53f95456
VL
1021}
1022
3378a54f
1023# install any of listed files
1024#
1025# If first argument is '-d' and second some destination path, first accessible
1026# source is installed into this path, otherwise it will installed in the same
1027# path as source. If none of listed files was installed, function return 1.
1028# On first successful installation it returns with 0 status.
1029#
1030# Example:
1031#
1032# inst_any -d /bin/foo /bin/bar /bin/baz
1033#
1034# Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
1035# initramfs.
1036inst_any() {
1037 local to f
1038
1039 [[ $1 = '-d' ]] && to="$2" && shift 2
1040
1041 for f in "$@"; do
1042 if [[ -e $f ]]; then
1043 [[ $to ]] && inst "$f" "$to" && return 0
1044 inst "$f" && return 0
1045 fi
1046 done
1047
1048 return 1
1049}
1050
c9143a63
JAH
1051
1052# inst_libdir_file [-n <pattern>] <file> [<file>...]
1053# Install a <file> located on a lib directory to the initramfs image
cacaa90c 1054# -n <pattern> install matching files
c9143a63 1055inst_libdir_file() {
0c6565c8 1056 local _files
c9143a63 1057 if [[ "$1" == "-n" ]]; then
cacaa90c 1058 local _pattern=$2
c9143a63
JAH
1059 shift 2
1060 for _dir in $libdirs; do
7828692f 1061 for _i in "$@"; do
c9143a63 1062 for _f in "$_dir"/$_i; do
7828692f 1063 [[ "$_f" =~ $_pattern ]] || continue
0c6565c8 1064 [[ -e "$_f" ]] && _files+="$_f "
c9143a63
JAH
1065 done
1066 done
1067 done
1068 else
1069 for _dir in $libdirs; do
7828692f 1070 for _i in "$@"; do
c9143a63 1071 for _f in "$_dir"/$_i; do
0c6565c8 1072 [[ -e "$_f" ]] && _files+="$_f "
c9143a63
JAH
1073 done
1074 done
1075 done
1076 fi
0c6565c8 1077 [[ $_files ]] && dracut_install $_files
c9143a63
JAH
1078}
1079
1080
87122afc
1081# install function decompressing the target and handling symlinks
1082# $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
1083#
1084# Function install targets in the same paths inside overlay but decompressed
1085# and without extensions (.gz, .bz2).
1086inst_decompress() {
b19c5d51 1087 local _src _cmd
87122afc 1088
29b10e65 1089 for _src in $@
87122afc 1090 do
29b10e65 1091 case ${_src} in
c62b9461 1092 *.gz) _cmd='gzip -f -d' ;;
29b10e65 1093 *.bz2) _cmd='bzip2 -d' ;;
87122afc
1094 *) return 1 ;;
1095 esac
b19c5d51 1096 inst_simple ${_src}
87122afc
1097 # Decompress with chosen tool. We assume that tool changes name e.g.
1098 # from 'name.gz' to 'name'.
29b10e65 1099 ${_cmd} "${initdir}${_src}"
87122afc
1100 done
1101}
1102
1103# It's similar to above, but if file is not compressed, performs standard
1104# install.
1105# $@ = list of files
1106inst_opt_decompress() {
29b10e65 1107 local _src
87122afc 1108
29b10e65 1109 for _src in $@
87122afc 1110 do
29b10e65 1111 inst_decompress "${_src}" || inst "${_src}"
87122afc
1112 done
1113}
1114
7e2bca48
HH
1115# module_check <dracut module>
1116# execute the check() function of module-setup.sh of <dracut module>
1117# or the "check" script, if module-setup.sh is not found
1118# "check $hostonly" is called
95d2dabc 1119module_check() {
29b10e65
HH
1120 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1121 local _ret
31f1c02d
AW
1122 local _forced=0
1123 local _hostonly=$hostonly
1124 [ $# -eq 2 ] && _forced=$2
29b10e65
HH
1125 [[ -d $_moddir ]] || return 1
1126 if [[ ! -f $_moddir/module-setup.sh ]]; then
95d2dabc 1127 # if we do not have a check script, we are unconditionally included
29b10e65 1128 [[ -x $_moddir/check ]] || return 0
31f1c02d 1129 [ $_forced -ne 0 ] && unset hostonly
29b10e65 1130 $_moddir/check $hostonly
31f1c02d 1131 _ret=$?
95d2dabc
HH
1132 else
1133 unset check depends install installkernel
b093aa2d 1134 check() { true; }
29b10e65 1135 . $_moddir/module-setup.sh
95d2dabc 1136 is_func check || return 0
31f1c02d
AW
1137 [ $_forced -ne 0 ] && unset hostonly
1138 check $hostonly
29b10e65 1139 _ret=$?
95d2dabc 1140 unset check depends install installkernel
0c2e3d12 1141 fi
31f1c02d
AW
1142 hostonly=$_hostonly
1143 return $_ret
0c2e3d12
VL
1144}
1145
7e2bca48
HH
1146# module_check_mount <dracut module>
1147# execute the check() function of module-setup.sh of <dracut module>
1148# or the "check" script, if module-setup.sh is not found
1149# "mount_needs=1 check 0" is called
1150module_check_mount() {
1151 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1152 local _ret
1153 mount_needs=1
1154 [[ -d $_moddir ]] || return 1
1155 if [[ ! -f $_moddir/module-setup.sh ]]; then
1156 # if we do not have a check script, we are unconditionally included
1157 [[ -x $_moddir/check ]] || return 0
1158 mount_needs=1 $_moddir/check 0
1159 _ret=$?
1160 else
1161 unset check depends install installkernel
b093aa2d 1162 check() { false; }
7e2bca48 1163 . $_moddir/module-setup.sh
7e2bca48
HH
1164 check 0
1165 _ret=$?
1166 unset check depends install installkernel
1167 fi
1168 unset mount_needs
1169 return $_ret
1170}
1171
1172# module_depends <dracut module>
1173# execute the depends() function of module-setup.sh of <dracut module>
1174# or the "depends" script, if module-setup.sh is not found
95d2dabc 1175module_depends() {
29b10e65
HH
1176 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1177 local _ret
1178 [[ -d $_moddir ]] || return 1
1179 if [[ ! -f $_moddir/module-setup.sh ]]; then
95d2dabc 1180 # if we do not have a check script, we have no deps
29b10e65
HH
1181 [[ -x $_moddir/check ]] || return 0
1182 $_moddir/check -d
95d2dabc
HH
1183 return $?
1184 else
1185 unset check depends install installkernel
b093aa2d 1186 depends() { true; }
29b10e65 1187 . $_moddir/module-setup.sh
95d2dabc 1188 depends
29b10e65 1189 _ret=$?
95d2dabc 1190 unset check depends install installkernel
29b10e65 1191 return $_ret
33ee031c 1192 fi
0c2e3d12
VL
1193}
1194
7e2bca48
HH
1195# module_install <dracut module>
1196# execute the install() function of module-setup.sh of <dracut module>
1197# or the "install" script, if module-setup.sh is not found
95d2dabc 1198module_install() {
29b10e65
HH
1199 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1200 local _ret
1201 [[ -d $_moddir ]] || return 1
1202 if [[ ! -f $_moddir/module-setup.sh ]]; then
1203 [[ -x $_moddir/install ]] && . "$_moddir/install"
95d2dabc
HH
1204 return $?
1205 else
1206 unset check depends install installkernel
b093aa2d 1207 install() { true; }
29b10e65 1208 . $_moddir/module-setup.sh
95d2dabc 1209 install
29b10e65 1210 _ret=$?
95d2dabc 1211 unset check depends install installkernel
29b10e65 1212 return $_ret
95d2dabc
HH
1213 fi
1214}
1215
7e2bca48
HH
1216# module_installkernel <dracut module>
1217# execute the installkernel() function of module-setup.sh of <dracut module>
1218# or the "installkernel" script, if module-setup.sh is not found
95d2dabc 1219module_installkernel() {
29b10e65
HH
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/installkernel ]] && . "$_moddir/installkernel"
95d2dabc
HH
1225 return $?
1226 else
1227 unset check depends install installkernel
b093aa2d
HH
1228 installkernel() { true; }
1229 . $_moddir/module-setup.sh
95d2dabc 1230 installkernel
29b10e65 1231 _ret=$?
95d2dabc 1232 unset check depends install installkernel
29b10e65 1233 return $_ret
95d2dabc
HH
1234 fi
1235}
1236
7e2bca48
HH
1237# check_mount <dracut module>
1238# check_mount checks, if a dracut module is needed for the given
1239# device and filesystem types in "${host_fs_types[@]}"
1b7fd0fa
AW
1240check_mount() {
1241 local _mod=$1
1242 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1243 local _ret
1244 local _moddep
ecfa02bd
HH
1245
1246 [ "${#host_fs_types[*]}" -le 0 ] && return 1
1247
1b7fd0fa 1248 # If we are already scheduled to be loaded, no need to check again.
b093aa2d
HH
1249 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
1250 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
1b7fd0fa
AW
1251
1252 # This should never happen, but...
1253 [[ -d $_moddir ]] || return 1
1254
1255 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
1256
b093aa2d 1257 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
600c8769 1258 dinfo "dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
0f283709
HH
1259 return 1
1260 fi
1b7fd0fa 1261
b093aa2d 1262 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
ecfa02bd
HH
1263 module_check_mount $_mod; ret=$?
1264
1265 # explicit module, so also accept ret=255
1266 [[ $ret = 0 || $ret = 255 ]] || return 1
1b7fd0fa 1267 else
ecfa02bd
HH
1268 # module not in our list
1269 if [[ $dracutmodules = all ]]; then
1270 # check, if we can and should install this module
1271 module_check_mount $_mod || return 1
1272 else
1273 # skip this module
1274 return 1
1275 fi
1b7fd0fa
AW
1276 fi
1277
ecfa02bd 1278
1b7fd0fa
AW
1279 for _moddep in $(module_depends $_mod); do
1280 # handle deps as if they were manually added
b093aa2d 1281 [[ " $add_dracutmodules " == *\ $_moddep\ * ]] || \
1b7fd0fa 1282 add_dracutmodules+=" $_moddep "
b093aa2d 1283 [[ " $force_add_dracutmodules " == *\ $_moddep\ * ]] || \
1b7fd0fa
AW
1284 force_add_dracutmodules+=" $_moddep "
1285 # if a module we depend on fail, fail also
0f283709 1286 if ! check_module $_moddep; then
600c8769 1287 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
0f283709
HH
1288 return 1
1289 fi
1b7fd0fa
AW
1290 done
1291
b093aa2d 1292 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
1b7fd0fa
AW
1293 mods_to_load+=" $_mod "
1294
1295 return 0
1296}
1297
7e2bca48
HH
1298# check_module <dracut module> [<use_as_dep>]
1299# check if a dracut module is to be used in the initramfs process
1300# if <use_as_dep> is set, then the process also keeps track
1301# that the modules were checked for the dependency tracking process
95d2dabc 1302check_module() {
29b10e65
HH
1303 local _mod=$1
1304 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1305 local _ret
1306 local _moddep
95d2dabc 1307 # If we are already scheduled to be loaded, no need to check again.
b093aa2d
HH
1308 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
1309 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
95d2dabc
HH
1310
1311 # This should never happen, but...
29b10e65 1312 [[ -d $_moddir ]] || return 1
95d2dabc 1313
29b10e65 1314 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
95d2dabc 1315
b093aa2d 1316 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
600c8769 1317 dinfo "dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
0f283709
HH
1318 return 1
1319 fi
95d2dabc 1320
b093aa2d
HH
1321 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
1322 if [[ " $force_add_dracutmodules " == *\ $_mod\ * ]]; then
31f1c02d
AW
1323 module_check $_mod 1; ret=$?
1324 else
1325 module_check $_mod 0; ret=$?
1326 fi
95d2dabc
HH
1327 # explicit module, so also accept ret=255
1328 [[ $ret = 0 || $ret = 255 ]] || return 1
1329 else
1330 # module not in our list
1331 if [[ $dracutmodules = all ]]; then
1332 # check, if we can and should install this module
29b10e65 1333 module_check $_mod || return 1
cc02093d 1334 else
95d2dabc
HH
1335 # skip this module
1336 return 1
970e646b 1337 fi
95d2dabc 1338 fi
bb764545 1339
29b10e65 1340 for _moddep in $(module_depends $_mod); do
95d2dabc 1341 # handle deps as if they were manually added
b093aa2d 1342 [[ " $add_dracutmodules " == *\ $_moddep\ * ]] || \
29b10e65 1343 add_dracutmodules+=" $_moddep "
b093aa2d 1344 [[ " $force_add_dracutmodules " == *\ $_moddep\ * ]] || \
31f1c02d 1345 force_add_dracutmodules+=" $_moddep "
95d2dabc 1346 # if a module we depend on fail, fail also
0f283709 1347 if ! check_module $_moddep; then
600c8769 1348 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
0f283709
HH
1349 return 1
1350 fi
0c2e3d12 1351 done
70503db4 1352
b093aa2d 1353 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
29b10e65 1354 mods_to_load+=" $_mod "
95d2dabc
HH
1355
1356 return 0
1357}
1358
7e2bca48
HH
1359# for_each_module_dir <func>
1360# execute "<func> <dracut module> 1"
1b7fd0fa 1361for_each_module_dir() {
29b10e65
HH
1362 local _modcheck
1363 local _mod
1364 local _moddir
1b7fd0fa
AW
1365 local _func
1366 _func=$1
29b10e65
HH
1367 for _moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1368 _mod=${_moddir##*/}; _mod=${_mod#[0-9][0-9]}
1b7fd0fa 1369 $_func $_mod 1
70503db4 1370 done
724b87a6
HH
1371
1372 # Report any missing dracut modules, the user has specified
31f1c02d 1373 _modcheck="$add_dracutmodules $force_add_dracutmodules"
29b10e65
HH
1374 [[ $dracutmodules != all ]] && _modcheck="$m $dracutmodules"
1375 for _mod in $_modcheck; do
b093aa2d
HH
1376 [[ " $mods_to_load " == *\ $_mod\ * ]] && continue
1377 [[ " $omit_dracutmodules " == *\ $_mod\ * ]] && continue
600c8769 1378 derror "dracut module '$_mod' cannot be found or installed."
724b87a6 1379 done
0c2e3d12
VL
1380}
1381
ddfd1d10
VL
1382# Install a single kernel module along with any firmware it may require.
1383# $1 = full path to kernel module to install
1384install_kmod_with_fw() {
0b440844 1385 # no need to go further if the module is already installed
379c34d2 1386
0b706743
1387 [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
1388 && return 0
fcbcb252 1389
f4031e8a
HH
1390 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -e "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}" ]]; then
1391 read ret < "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
d8aeb3a7
HH
1392 return $ret
1393 fi
379c34d2 1394
fcbcb252
HH
1395 if [[ $omit_drivers ]]; then
1396 local _kmod=${1##*/}
1397 _kmod=${_kmod%.ko}
1398 _kmod=${_kmod/-/_}
34248c92
HH
1399 if [[ "$_kmod" =~ $omit_drivers ]]; then
1400 dinfo "Omitting driver $_kmod"
7abd4264 1401 return 0
34248c92
HH
1402 fi
1403 if [[ "${1##*/lib/modules/$kernel/}" =~ $omit_drivers ]]; then
fcbcb252 1404 dinfo "Omitting driver $_kmod"
7abd4264 1405 return 0
fcbcb252
HH
1406 fi
1407 fi
1408
d8aeb3a7
HH
1409 inst_simple "$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
1410 ret=$?
f4031e8a
HH
1411 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1412 [[ -d "$DRACUT_KERNEL_LAZY_HASHDIR" ]] && \
1413 echo $ret > "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
d8aeb3a7 1414 (($ret != 0)) && return $ret
e6024e00
JR
1415
1416 local _modname=${1##*/} _fwdir _found _fw
1417 _modname=${_modname%.ko*}
29b10e65
HH
1418 for _fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do
1419 _found=''
1420 for _fwdir in $fw_dir; do
1421 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1422 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1423 _found=yes
cc02093d
HH
1424 fi
1425 done
29b10e65 1426 if [[ $_found != yes ]]; then
fe1484f3 1427 if ! [[ -d $(echo /sys/module/${_modname//-/_}|{ read a b; echo $a; }) ]]; then
29b10e65
HH
1428 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
1429 "\"${_modname}.ko\""
e2cdb570 1430 else
29b10e65
HH
1431 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
1432 "\"${_modname}.ko\""
e2cdb570 1433 fi
cc02093d 1434 fi
ddfd1d10 1435 done
0b440844 1436 return 0
ddfd1d10
VL
1437}
1438
1439# Do something with all the dependencies of a kernel module.
1440# Note that kernel modules depend on themselves using the technique we use
0b706743
1441# $1 = function to call for each dependency we find
1442# It will be passed the full path to the found kernel module
ddfd1d10
VL
1443# $2 = module to get dependencies for
1444# rest of args = arguments to modprobe
c32bda6b 1445# _fderr specifies FD passed from surrounding scope
ddfd1d10 1446for_each_kmod_dep() {
7abd4264 1447 local _func=$1 _kmod=$2 _cmd _modpath _options
ddfd1d10 1448 shift 2
c32bda6b 1449 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
29b10e65
HH
1450 while read _cmd _modpath _options; do
1451 [[ $_cmd = insmod ]] || continue
1452 $_func ${_modpath} || exit $?
0b706743 1453 done
0b706743 1454 )
ddfd1d10
VL
1455}
1456
70c6b773 1457dracut_kernel_post() {
d8aeb3a7
HH
1458 local _moddirname=${srcmods%%/lib/modules/*}
1459
f4031e8a 1460 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" ]]; then
ec61f0a3
HH
1461 xargs -r modprobe -a ${_moddirname+-d ${_moddirname}/} \
1462 --ignore-install --show-depends --set-version $kernel \
f4031e8a 1463 < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" 2>/dev/null \
89d44e72
HH
1464 | sort -u \
1465 | while read _cmd _modpath _options; do
1466 [[ $_cmd = insmod ]] || continue
1467 echo "$_modpath"
f4031e8a 1468 done > "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
d8aeb3a7 1469
89d44e72 1470 (
73575f11
HH
1471 if [[ $DRACUT_INSTALL ]] && [[ -z $_moddirname ]]; then
1472 xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
89d44e72
HH
1473 else
1474 while read _modpath; do
1475 local _destpath=$_modpath
1476 [[ $_moddirname ]] && _destpath=${_destpath##$_moddirname/}
1477 _destpath=${_destpath##*/lib/modules/$kernel/}
1478 inst_simple "$_modpath" "/lib/modules/$kernel/${_destpath}" || exit $?
f4031e8a 1479 done < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
89d44e72
HH
1480 fi
1481 ) &
d8aeb3a7 1482
73575f11
HH
1483 if [[ $DRACUT_INSTALL ]]; then
1484 xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" \
89d44e72
HH
1485 | while read line; do
1486 for _fwdir in $fw_dir; do
1487 echo $_fwdir/$line;
1488 done;
73575f11 1489 done | xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a -o
89d44e72 1490 else
73575f11 1491 for _fw in $(xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"); do
89d44e72
HH
1492 for _fwdir in $fw_dir; do
1493 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1494 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1495 break
1496 fi
1497 done
1498 done
1499 fi
d8aeb3a7 1500
89d44e72
HH
1501 wait
1502 fi
70c6b773
HH
1503
1504 for _f in modules.builtin.bin modules.builtin; do
1505 [[ $srcmods/$_f ]] && break
1506 done || {
1507 dfatal "No modules.builtin.bin and modules.builtin found!"
1508 return 1
1509 }
1510
1511 for _f in modules.builtin.bin modules.builtin modules.order; do
1512 [[ $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f"
1513 done
1514
1515 # generate module dependencies for the initrd
1516 if [[ -d $initdir/lib/modules/$kernel ]] && \
1517 ! depmod -a -b "$initdir" $kernel; then
1518 dfatal "\"depmod -a $kernel\" failed."
1519 exit 1
1520 fi
1521
32bd2fbb 1522 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && rm -fr -- "$DRACUT_KERNEL_LAZY_HASHDIR"
d8aeb3a7 1523}
fe1484f3 1524
b093aa2d 1525module_is_host_only() { (
080d1b71
HH
1526 local _mod=$1
1527 _mod=${_mod##*/}
1528 _mod=${_mod%.ko}
1529
b093aa2d 1530 [[ " $add_drivers " == *\ ${_mod}\ * ]] && return 0
080d1b71
HH
1531
1532 # check if module is loaded
b093aa2d
HH
1533 for i in /sys/module/${_mod//-/_}; do
1534 [[ -d $i ]] && return 0
1535 done
080d1b71
HH
1536
1537 # check if module is loadable on the current kernel
1538 # this covers the case, where a new module is introduced
1539 # or a module was renamed
1540 # or a module changed from builtin to a module
1541 modinfo -F filename "$_mod" &>/dev/null || return 0
1542
1543 return 1
b093aa2d
HH
1544 )
1545}
080d1b71 1546
b093aa2d 1547find_kernel_modules_by_path () {
74405477 1548 local _OLDIFS
5db6ca5a 1549
b093aa2d 1550 [[ -f "$srcmods/modules.dep" ]] || return 0
5db6ca5a
HH
1551
1552 _OLDIFS=$IFS
1553 IFS=:
1554 while read a rest; do
1555 [[ $a = */$1/* ]] || continue
b093aa2d
HH
1556 printf "%s\n" "$srcmods/$a"
1557 done < "$srcmods/modules.dep"
5db6ca5a 1558 IFS=$_OLDIFS
74405477 1559 return 0
b093aa2d 1560}
4073c815 1561
881eda69
JR
1562find_kernel_modules () {
1563 find_kernel_modules_by_path drivers
1564}
1565
338b43cd
HH
1566# instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1567# instmods [-c [-s]] <kernel subsystem>
ddfd1d10 1568# install kernel modules along with all their dependencies.
7e2bca48 1569# <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
6fa0c3d6 1570instmods() {
0c1a8ebc 1571 [[ $no_kernel = yes ]] && return
c32bda6b
MS
1572 # called [sub]functions inherit _fderr
1573 local _fderr=9
a6d3be9d 1574 local _check=no
338b43cd 1575 local _silent=no
a6d3be9d
AW
1576 if [[ $1 = '-c' ]]; then
1577 _check=yes
1578 shift
1579 fi
881eda69 1580
338b43cd
HH
1581 if [[ $1 = '-s' ]]; then
1582 _silent=yes
1583 shift
1584 fi
1585
881eda69 1586 function inst1mod() {
a6d3be9d 1587 local _ret=0 _mod="$1"
29b10e65 1588 case $_mod in
0b706743 1589 =*)
d8aeb3a7
HH
1590 ( [[ "$_mpargs" ]] && echo $_mpargs
1591 find_kernel_modules_by_path "${_mod#=}" ) \
1592 | instmods
1593 ((_ret+=$?))
cc02093d 1594 ;;
0024702f 1595 --*) _mpargs+=" $_mod" ;;
34248c92 1596 *)
379c34d2 1597 _mod=${_mod##*/}
6568d86a 1598 # if we are already installed, skip this module and go on
cc02093d 1599 # to the next one.
f4031e8a
HH
1600 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1601 [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko" ]]; then
1602 read _ret <"$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko"
d8aeb3a7
HH
1603 return $_ret
1604 fi
881eda69 1605
34248c92
HH
1606 if [[ $omit_drivers ]] && [[ "$1" =~ $omit_drivers ]]; then
1607 dinfo "Omitting driver ${_mod##$srcmods}"
7abd4264 1608 return 0
34248c92 1609 fi
080d1b71 1610
cc02093d
HH
1611 # If we are building a host-specific initramfs and this
1612 # module is not already loaded, move on to the next one.
fe1484f3 1613 [[ $hostonly ]] \
080d1b71 1614 && ! module_is_host_only "$_mod" \
7abd4264 1615 && return 0
04b56f3a 1616
f4031e8a 1617 if [[ "$_check" = "yes" ]] || ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
d8aeb3a7 1618 # We use '-d' option in modprobe only if modules prefix path
600c8769 1619 # differs from default '/'. This allows us to use dracut with
d8aeb3a7
HH
1620 # old version of modprobe which doesn't have '-d' option.
1621 local _moddirname=${srcmods%%/lib/modules/*}
1622 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1623
1624 # ok, load the module, all its dependencies, and any firmware
1625 # it may require
1626 for_each_kmod_dep install_kmod_with_fw $_mod \
1627 --set-version $kernel ${_moddirname} $_mpargs
1628 ((_ret+=$?))
1629 else
f4031e8a
HH
1630 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1631 echo $_mod >> "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist"
d8aeb3a7 1632 fi
cc02093d 1633 ;;
0b706743 1634 esac
a6d3be9d 1635 return $_ret
881eda69
JR
1636 }
1637
f9708da2 1638 function instmods_1() {
a6d3be9d 1639 local _mod _mpargs
f9708da2
JR
1640 if (($# == 0)); then # filenames from stdin
1641 while read _mod; do
a6d3be9d 1642 inst1mod "${_mod%.ko*}" || {
338b43cd 1643 if [[ "$_check" == "yes" ]]; then
19b5c109 1644 [[ "$_silent" == "no" ]] && dfatal "Failed to install module $_mod"
a6d3be9d
AW
1645 return 1
1646 fi
1647 }
f9708da2
JR
1648 done
1649 fi
1650 while (($# > 0)); do # filenames as arguments
a6d3be9d 1651 inst1mod ${1%.ko*} || {
338b43cd 1652 if [[ "$_check" == "yes" ]]; then
19b5c109 1653 [[ "$_silent" == "no" ]] && dfatal "Failed to install module $1"
a6d3be9d
AW
1654 return 1
1655 fi
1656 }
f9708da2 1657 shift
881eda69 1658 done
a6d3be9d 1659 return 0
f9708da2
JR
1660 }
1661
a6d3be9d 1662 local _ret _filter_not_found='FATAL: Module .* not found.'
c32bda6b
MS
1663 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1664 # redirections, but that would make dracut require bash4 at least.
1665 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
86191581 1666 | while read line; do [[ "$line" =~ $_filter_not_found ]] || echo $line;done | derror
a6d3be9d 1667 _ret=$?
a6d3be9d 1668 return $_ret
0b706743 1669}