]> git.ipfire.org Git - thirdparty/dracut.git/blame - dracut-functions.sh
dracut-functions.sh: get_fs_env() replaced egrep with shell
[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#
04b56f3a 22
0874654c
HH
23if ! [[ $dracutbasedir ]]; then
24 dracutbasedir=${BASH_SOURCE[0]%/*}
25 [[ $dracutbasedir = "dracut-functions" ]] && dracutbasedir="."
26 [[ $dracutbasedir ]] || dracutbasedir="."
27 dracutbasedir="$(readlink -f $dracutbasedir)"
28fi
29
d6d53f60 30if ! type dinfo >/dev/null 2>&1; then
552ecca6 31 . "$dracutbasedir/dracut-logger.sh"
e103615b
32 dlog_init
33fi
34
7e2bca48
HH
35# export standard hookdirs
36[[ $hookdirs ]] || {
37 hookdirs="cmdline pre-udev pre-trigger netroot initqueue pre-mount"
38 hookdirs+=" pre-pivot mount emergency shutdown-emergency shutdown cleanup"
39 export hookdirs
40}
41
3198f171 42# Generic substring function. If $2 is in $1, return 0.
43dfbeec 43strstr() { [ "${1#*$2*}" != "$1" ]; }
f04dc5f3 44
87122afc
45# Create all subdirectories for given path without creating the last element.
46# $1 = path
94f49230 47mksubdirs() { mkdir -m 0755 -p ${1%/*}; }
87122afc 48
22ecea45
49# Version comparision function. Assumes Linux style version scheme.
50# $1 = version a
51# $2 = comparision op (gt, ge, eq, le, lt, ne)
52# $3 = version b
ecf42850 53vercmp() {
29b10e65 54 local _n1=(${1//./ }) _op=$2 _n2=(${3//./ }) _i _res
ecf42850 55
29b10e65 56 for ((_i=0; ; _i++))
22ecea45 57 do
29b10e65
HH
58 if [[ ! ${_n1[_i]}${_n2[_i]} ]]; then _res=0
59 elif ((${_n1[_i]:-0} > ${_n2[_i]:-0})); then _res=1
60 elif ((${_n1[_i]:-0} < ${_n2[_i]:-0})); then _res=2
22ecea45
61 else continue
62 fi
63 break
ecf42850 64 done
ecf42850 65
29b10e65
HH
66 case $_op in
67 gt) ((_res == 1));;
68 ge) ((_res != 2));;
69 eq) ((_res == 0));;
70 le) ((_res != 1));;
71 lt) ((_res == 2));;
72 ne) ((_res != 0));;
22ecea45 73 esac
ecf42850
74}
75
7e2bca48
HH
76# is_func <command>
77# Check whether $1 is a function.
95d2dabc 78is_func() {
0b706743 79 [[ $(type -t $1) = "function" ]]
95d2dabc
HH
80}
81
87122afc
82# Function prints global variables in format name=value line by line.
83# $@ = list of global variables' name
84print_vars() {
29b10e65 85 local _var _value
87122afc 86
29b10e65 87 for _var in $@
87122afc 88 do
29b10e65
HH
89 _value=$(eval echo \$$_var)
90 [[ ${_value} ]] && echo "${_var}=\"${_value}\""
87122afc
91 done
92}
93
7e2bca48
HH
94# normalize_path <path>
95# Prints the normalized path, where it removes any duplicated
96# and trailing slashes.
97# Example:
98# $ normalize_path ///test/test//
99# /test/test
626d9eba 100normalize_path() {
c44e3cb4
MS
101 shopt -q -s extglob
102 set -- "${1//+(\/)//}"
103 shopt -q -u extglob
104 echo "${1%/}"
626d9eba 105}
d4bb4316 106
7e2bca48
HH
107# convert_abs_rel <from> <to>
108# Prints the relative path, when creating a symlink to <to> from <from>.
109# Example:
110# $ convert_abs_rel /usr/bin/test /bin/test-2
111# ../../bin/test-2
112# $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
d4bb4316 113convert_abs_rel() {
6d2a7942 114 local __current __absolute __abssize __cursize __newpath
c44e3cb4 115 local -i __i __level
d4bb4316 116
c44e3cb4
MS
117 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
118
c1609dd4
MS
119 # corner case #1 - self looping link
120 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
121
122 # corner case #2 - own dir link
123 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
124
6d2a7942
HH
125 IFS="/" __current=($1)
126 IFS="/" __absolute=($2)
d4bb4316
HH
127
128 __abssize=${#__absolute[@]}
129 __cursize=${#__current[@]}
130
131 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
132 do
133 (( __level++ ))
134 if (( __level > __abssize || __level > __cursize ))
135 then
136 break
137 fi
138 done
139
140 for ((__i = __level; __i < __cursize-1; __i++))
141 do
142 if ((__i > __level))
143 then
144 __newpath=$__newpath"/"
145 fi
146 __newpath=$__newpath".."
147 done
148
149 for ((__i = __level; __i < __abssize; __i++))
150 do
151 if [[ -n $__newpath ]]
152 then
153 __newpath=$__newpath"/"
154 fi
155 __newpath=$__newpath${__absolute[__i]}
156 done
157
158 echo "$__newpath"
159}
160
7e2bca48
HH
161# get_fs_env <device>
162# Get and set the ID_FS_TYPE and ID_FS_UUID variable from udev for a device.
163# Example:
164# $ get_fs_env /dev/sda2; echo $ID_FS_TYPE; echo $ID_FS_UUID
165# ext4
166# 551a39aa-4ae9-4e70-a262-ef665cadb574
9ede1929 167get_fs_env() {
7c179686 168 [[ $1 ]] || return
29b10e65
HH
169 unset ID_FS_TYPE
170 unset ID_FS_UUID
07f3e4f1
HH
171 eval $(udevadm info --query=env --name=$1 \
172 | while read line; do
173 [[ "$line" =~ 'ID_FS_(TYPE|UUID)=' ]] && echo $line;
174 done)
d8b9844c
HH
175 [[ $ID_FS_TYPE ]] && return
176
59c88f49 177 if [[ -x /lib/udev/vol_id ]]; then
cc02093d 178 eval $(/lib/udev/vol_id --export $1)
9ede1929 179 elif find_binary blkid >/dev/null; then
cc02093d 180 eval $(blkid -o udev $1)
59c88f49 181 else
cc02093d 182 return 1
59c88f49 183 fi
9ede1929
VL
184}
185
7e2bca48
HH
186# get_fs_uuid <device>
187# Prints the filesystem UUID for a device.
188# Example:
189# $ get_fs_uuid /dev/sda2
190# 551a39aa-4ae9-4e70-a262-ef665cadb574
191get_fs_uuid() (
192 get_fs_env $1 || return
193 echo $ID_FS_UUID
194)
195
196# get_fs_type <device>
197# Prints the filesystem type for a device.
198# Example:
199# $ get_fs_type /dev/sda1
200# ext4
9ede1929 201get_fs_type() (
7c179686
HH
202 [[ $1 ]] || return
203 if [[ $1 != ${1#/dev/block/nfs:} ]] \
cc02093d
HH
204 || [[ $1 != ${1#/dev/block/nfs3:} ]] \
205 || [[ $1 != ${1#/dev/block/nfs4:} ]]; then
206 echo "nfs"
207 return
7c179686 208 fi
7ae5d9d1
HH
209 if get_fs_env $1; then
210 echo $ID_FS_TYPE
211 return
212 fi
213 find_dev_fstype $1
9ede1929
VL
214)
215
7e2bca48
HH
216# get_maj_min <device>
217# Prints the major and minor of a device node.
218# Example:
219# $ get_maj_min /dev/sda2
220# 8:2
480d772f
HH
221get_maj_min() {
222 local _dev
74132a10 223 _dev=$(stat -L -c '$((0x%t)):$((0x%T))' "$1" 2>/dev/null)
480d772f
HH
224 _dev=$(eval "echo $_dev")
225 echo $_dev
226}
227
7e2bca48
HH
228# find_block_device <mountpoint>
229# Prints the major and minor number of the block device
230# for a given mountpoint.
231# Unless $use_fstab is set to "yes" the functions
232# uses /proc/self/mountinfo as the primary source of the
233# information and only falls back to /etc/fstab, if the mountpoint
234# is not found there.
235# Example:
236# $ find_block_device /usr
237# 8:4
f76ef3aa 238find_block_device() {
29b10e65 239 local _x _mpt _majmin _dev _fs _maj _min
0b706743 240 if [[ $use_fstab != yes ]]; then
29b10e65 241 while read _x _x _majmin _x _mpt _x _x _fs _dev _x; do
7ae5d9d1 242 [[ $_mpt = $1 ]] || continue
29b10e65
HH
243 [[ $_fs = nfs ]] && { echo $_dev; return 0;}
244 [[ $_fs = nfs3 ]] && { echo $_dev; return 0;}
245 [[ $_fs = nfs4 ]] && { echo $_dev; return 0;}
246 [[ $_fs = btrfs ]] && {
480d772f
HH
247 get_maj_min $_dev
248 return 0;
51c977d1 249 }
7ae5d9d1 250 if [[ ${_majmin#0:} = $_majmin ]]; then
29b10e65 251 echo $_majmin
cc02093d
HH
252 return 0 # we have a winner!
253 fi
0b706743 254 done < /proc/self/mountinfo
7c179686 255 fi
0b706743 256 # fall back to /etc/fstab
29b10e65
HH
257 while read _dev _mpt _fs _x; do
258 if [[ $_mpt = $1 ]]; then
259 [[ $_fs = nfs ]] && { echo $_dev; return 0;}
260 [[ $_fs = nfs3 ]] && { echo $_dev; return 0;}
261 [[ $_fs = nfs4 ]] && { echo $_dev; return 0;}
262 [[ $_dev != ${_dev#UUID=} ]] && _dev=/dev/disk/by-uuid/${_dev#UUID=}
263 [[ $_dev != ${_dev#LABEL=} ]] && _dev=/dev/disk/by-label/${_dev#LABEL=}
264 [[ -b $_dev ]] || return 1 # oops, not a block device.
c4e48eae 265 get_maj_min "$_dev" && return 0
cc02093d 266 fi
7c179686 267 done < /etc/fstab
0b706743
268
269 return 1
17829e94
VL
270}
271
7e2bca48
HH
272# find_dev_fstype <device>
273# Echo the filesystem type for a given device.
274# /proc/self/mountinfo is taken as the primary source of information
275# and /etc/fstab is used as a fallback.
276# No newline is appended!
277# Example:
278# $ find_dev_fstype /dev/sda2;echo
279# ext4
7ae5d9d1
HH
280find_dev_fstype() {
281 local _x _mpt _majmin _dev _fs _maj _min
282 while read _x _x _majmin _x _mpt _x _x _fs _dev _x; do
283 [[ $_dev = $1 ]] || continue
284 echo -n $_fs;
285 return 0;
286 done < /proc/self/mountinfo
287
288 # fall back to /etc/fstab
289 while read _dev _mpt _fs _x; do
290 [[ $_dev = $1 ]] || continue
291 echo -n $_fs;
292 return 0;
293 done < /etc/fstab
294
295 return 1
296}
297
298# finds the major:minor of the block device backing the root filesystem.
f76ef3aa
VL
299find_root_block_device() { find_block_device /; }
300
7e2bca48
HH
301# for_each_host_dev_fs <func>
302# Execute "<func> <dev> <filesystem>" for every "<dev>|<fs>" pair found
303# in ${host_fs_types[@]}
480d772f
HH
304for_each_host_dev_fs()
305{
306 local _func="$1"
d0096de7
AW
307 local _dev
308 local _fs
2efa546f 309 local _ret=1
480d772f
HH
310 for f in ${host_fs_types[@]}; do
311 OLDIFS="$IFS"
312 IFS="|"
313 set -- $f
314 IFS="$OLDIFS"
d0096de7
AW
315 _dev="$1"
316 [[ -b "$_dev" ]] || continue
317 _fs="$2"
2efa546f 318 $_func $_dev $_fs && _ret=0
480d772f 319 done
2efa546f 320 return $_ret
480d772f
HH
321}
322
17829e94
VL
323# Walk all the slave relationships for a given block device.
324# Stop when our helper function returns success
325# $1 = function to call on every found block device
326# $2 = block device in major:minor format
0b706743 327check_block_and_slaves() {
29b10e65 328 local _x
17829e94
VL
329 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
330 "$1" $2 && return
533d7dc4 331 check_vol_slaves "$@" && return 0
3478b314 332 if [[ -f /sys/dev/block/$2/../dev ]]; then
0b706743 333 check_block_and_slaves $1 $(cat "/sys/dev/block/$2/../dev") && return 0
a3afcf2a 334 fi
17829e94 335 [[ -d /sys/dev/block/$2/slaves ]] || return 1
29b10e65
HH
336 for _x in /sys/dev/block/$2/slaves/*/dev; do
337 [[ -f $_x ]] || continue
338 check_block_and_slaves $1 $(cat "$_x") && return 0
17829e94
VL
339 done
340 return 1
341}
342
533d7dc4
HH
343# ugly workaround for the lvm design
344# There is no volume group device,
345# so, there are no slave devices for volume groups.
346# Logical volumes only have the slave devices they really live on,
347# but you cannot create the logical volume without the volume group.
95bde758 348# And the volume group might be bigger than the devices the LV needs.
533d7dc4 349check_vol_slaves() {
29b10e65 350 local _lv _vg _pv
0b706743 351 for i in /dev/mapper/*; do
c4e48eae 352 _lv=$(get_maj_min $i)
29b10e65
HH
353 if [[ $_lv = $2 ]]; then
354 _vg=$(lvm lvs --noheadings -o vg_name $i 2>/dev/null)
cc02093d 355 # strip space
29b10e65
HH
356 _vg=$(echo $_vg)
357 if [[ $_vg ]]; then
358 for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2>/dev/null)
0b706743 359 do
c4e48eae 360 check_block_and_slaves $1 $(get_maj_min $_pv) && return 0
cc02093d
HH
361 done
362 fi
363 fi
533d7dc4
HH
364 done
365 return 1
366}
367
19612483 368# Install a directory, keeping symlinks as on the original system.
3f590c78 369# Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
19612483
LA
370# will create ${initdir}/lib64, ${initdir}/lib64/file,
371# and a symlink ${initdir}/lib -> lib64.
372inst_dir() {
4637c5c2 373 [[ -e ${initdir}/"$1" ]] && return 0 # already there
a76dc278 374
9e103df4 375 local _dir="$1" _part="${1%/*}" _file
4637c5c2 376 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
a76dc278
HH
377 _dir="$_part $_dir"
378 _part=${_part%/*}
379 done
19612483 380
a76dc278
HH
381 # iterate over parent directories
382 for _file in $_dir; do
3249c257 383 [[ -e "${initdir}/$_file" ]] && continue
29b10e65 384 if [[ -L $_file ]]; then
d4bb4316 385 inst_symlink "$_file"
19612483
LA
386 else
387 # create directory
3249c257
HH
388 mkdir -m 0755 -p "${initdir}/$_file" || return 1
389 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
390 chmod u+w "${initdir}/$_file"
19612483
LA
391 fi
392 done
393}
394
36b24d7c
VL
395# $1 = file to copy to ramdisk
396# $2 (optional) Name for the file on the ramdisk
397# Location of the image dir is assumed to be $initdir
3198f171 398# We never overwrite the target if it exists.
36b24d7c 399inst_simple() {
4637c5c2
HH
400 [[ -f "$1" ]] || return 1
401 strstr "$1" "/" || return 1
3f590c78
JR
402
403 local _src=$1 target="${2:-$1}"
4637c5c2
HH
404 if ! [[ -d ${initdir}/$target ]]; then
405 [[ -e ${initdir}/$target ]] && return 0
cc227886
HH
406 [[ -L ${initdir}/$target ]] && return 0
407 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
3c56f372 408 fi
5242d8fb 409 # install checksum files also
29b10e65 410 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
5f06f0c3 411 inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
5242d8fb 412 fi
29b10e65 413 ddebug "Installing $_src"
7fffc9f1 414 cp --sparse=always -pfL "$_src" "${initdir}/$target"
36b24d7c
VL
415}
416
a10a1416
417# find symlinks linked to given library file
418# $1 = library file
419# Function searches for symlinks by stripping version numbers appended to
420# library filename, checks if it points to the same target and finally
421# prints the list of symlinks to stdout.
422#
423# Example:
424# rev_lib_symlinks libfoo.so.8.1
425# output: libfoo.so.8 libfoo.so
426# (Only if libfoo.so.8 and libfoo.so exists on host system.)
427rev_lib_symlinks() {
428 [[ ! $1 ]] && return 0
429
430 local fn="$1" orig="$(readlink -f "$1")" links=''
431
432 [[ ${fn} =~ .*\.so\..* ]] || return 1
433
434 until [[ ${fn##*.} == so ]]; do
435 fn="${fn%.*}"
436 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
437 done
438
0b706743 439 echo "${links}"
a10a1416
440}
441
95bde758 442# Same as above, but specialized to handle dynamic libraries.
3198f171
VL
443# It handles making symlinks according to how the original library
444# is referenced.
36b24d7c 445inst_library() {
4637c5c2
HH
446 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
447 strstr "$1" "/" || return 1
448 [[ -e $initdir/$_dest ]] && return 0
29b10e65 449 if [[ -L $_src ]]; then
0b706743 450 # install checksum files also
29b10e65 451 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
5f06f0c3 452 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
f90fd5b3 453 fi
29b10e65 454 _reallib=$(readlink -f "$_src")
29b10e65
HH
455 inst_simple "$_reallib" "$_reallib"
456 inst_dir "${_dest%/*}"
9cb8447c 457 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
4637c5c2 458 ln -sfn $(convert_abs_rel "${_dest}" "${_reallib}") "${initdir}/${_dest}"
36b24d7c 459 else
29b10e65 460 inst_simple "$_src" "$_dest"
36b24d7c 461 fi
a10a1416
462
463 # Create additional symlinks. See rev_symlinks description.
29b10e65 464 for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do
4637c5c2 465 [[ ! -e $initdir/$_symlink ]] && {
29b10e65
HH
466 ddebug "Creating extra symlink: $_symlink"
467 inst_symlink $_symlink
a10a1416
468 }
469 done
36b24d7c 470}
3198f171
VL
471
472# find a binary. If we were not passed the full path directly,
473# search in the usual places to find the binary.
6b25d71a 474find_binary() {
81c6e7fb 475 if [[ -z ${1##/*} ]]; then
86bf239e 476 if [[ -x $1 ]] || { strstr "$1" ".so" && ldd $1 &>/dev/null; }; then
81c6e7fb
HH
477 echo $1
478 return 0
479 fi
480 fi
1610a566 481
e29d0b8b 482 type -P $1
3359c8da 483}
36b24d7c 484
3198f171
VL
485# Same as above, but specialized to install binary executables.
486# Install binary executable, and all shared library dependencies, if any.
36b24d7c 487inst_binary() {
d18bc907 488 local _bin _target
29b10e65
HH
489 _bin=$(find_binary "$1") || return 1
490 _target=${2:-$_bin}
4637c5c2 491 [[ -e $initdir/$_target ]] && return 0
d18bc907
HH
492 [[ -L $_bin ]] && inst_symlink $_bin $_target && return 0
493 local _file _line
494 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
8667f2b7 495 # I love bash!
d18bc907
HH
496 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
497 [[ $_line = 'not a dynamic executable' ]] && break
498
499 if [[ $_line =~ $_so_regex ]]; then
500 _file=${BASH_REMATCH[1]}
501 [[ -e ${initdir}/$_file ]] && continue
502 inst_library "$_file"
503 continue
504 fi
505
29b10e65
HH
506 if [[ $_line =~ not\ found ]]; then
507 dfatal "Missing a shared library required by $_bin."
508 dfatal "Run \"ldd $_bin\" to find out what it is."
d18bc907 509 dfatal "$_line"
e27770e1 510 dfatal "dracut cannot create an initrd."
cc02093d
HH
511 exit 1
512 fi
5c862533 513 done
d18bc907 514 inst_simple "$_bin" "$_target"
36b24d7c 515}
04b56f3a 516
36b24d7c
VL
517# same as above, except for shell scripts.
518# If your shell script does not start with shebang, it is not a shell script.
519inst_script() {
4637c5c2
HH
520 local _bin
521 _bin=$(find_binary "$1") || return 1
f60995ad 522 shift
29b10e65 523 local _line _shebang_regex
4637c5c2 524 read -r -n 80 _line <"$_bin"
00531568 525 # If debug is set, clean unprintable chars to prevent messing up the term
29b10e65
HH
526 [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]')
527 _shebang_regex='(#! *)(/[^ ]+).*'
528 [[ $_line =~ $_shebang_regex ]] || return 1
03cec388 529 inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@"
04b56f3a
JK
530}
531
36b24d7c
VL
532# same as above, but specialized for symlinks
533inst_symlink() {
76a80dff 534 local _src=$1 _target=${2:-$1} _realsrc
4637c5c2 535 strstr "$1" "/" || return 1
36b24d7c 536 [[ -L $1 ]] || return 1
76a80dff 537 [[ -L $initdir/$_target ]] && return 0
29b10e65 538 _realsrc=$(readlink -f "$_src")
3249c257
HH
539 if ! [[ -e $initdir/$_realsrc ]]; then
540 if [[ -d $_realsrc ]]; then
541 inst_dir "$_realsrc"
542 else
76a80dff 543 inst "$_realsrc"
3249c257 544 fi
c3b0d83d 545 fi
3249c257
HH
546 [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
547 ln -sfn $(convert_abs_rel "${_target}" "${_realsrc}") "$initdir/$_target"
36b24d7c
VL
548}
549
62073c30
CG
550# attempt to install any programs specified in a udev rule
551inst_rule_programs() {
552 local _prog _bin
553
554 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
555 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
556 if [ -x /lib/udev/$_prog ]; then
557 _bin=/lib/udev/$_prog
558 else
559 _bin=$(find_binary "$_prog") || {
560 dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
561 continue;
562 }
563 fi
564
565 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
566 dracut_install "$_bin"
567 done
568 fi
569}
570
f04dc5f3
VL
571# udev rules always get installed in the same place, so
572# create a function to install them to make life simpler.
0b706743 573inst_rules() {
29b10e65 574 local _target=/etc/udev/rules.d _rule _found
fd2312e0 575
19612483 576 inst_dir "/lib/udev/rules.d"
29b10e65
HH
577 inst_dir "$_target"
578 for _rule in "$@"; do
08769b7f 579 if [ "${rule#/}" = "$rule" ]; then
76f5fa54
HH
580 for r in /lib/udev/rules.d /etc/udev/rules.d; do
581 if [[ -f $r/$_rule ]]; then
582 _found="$r/$_rule"
583 inst_simple "$_found"
62073c30 584 inst_rule_programs "$_found"
76f5fa54
HH
585 fi
586 done
587 fi
c97e1a76 588 for r in '' ./ $dracutbasedir/rules.d/; do
29b10e65
HH
589 if [[ -f ${r}$_rule ]]; then
590 _found="${r}$_rule"
591 inst_simple "$_found" "$_target/${_found##*/}"
62073c30 592 inst_rule_programs "$_found"
c97e1a76
HH
593 fi
594 done
29b10e65 595 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
f04dc5f3
VL
596 done
597}
598
36b24d7c
VL
599# general purpose installation function
600# Same args as above.
6b24de99 601inst() {
29b10e65
HH
602 local _x
603
94dcc5b8
HH
604 case $# in
605 1) ;;
3478b314 606 2) [[ ! $initdir && -d $2 ]] && export initdir=$2
cc02093d 607 [[ $initdir = $2 ]] && set $1;;
0b706743 608 3) [[ -z $initdir ]] && export initdir=$2
cc02093d 609 set $1 $3;;
e27770e1 610 *) dfatal "inst only takes 1 or 2 or 3 arguments"
cc02093d 611 exit 1;;
94dcc5b8 612 esac
29b10e65
HH
613 for _x in inst_symlink inst_script inst_binary inst_simple; do
614 $_x "$@" && return 0
36b24d7c
VL
615 done
616 return 1
04b56f3a
JK
617}
618
53f95456
VL
619# install function specialized for hooks
620# $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
621# All hooks should be POSIX/SuS compliant, they will be sourced by init.
622inst_hook() {
cdad82fd 623 if ! [[ -f $3 ]]; then
e27770e1
624 dfatal "Cannot install a hook ($3) that does not exist."
625 dfatal "Aborting initrd creation."
cc02093d 626 exit 1
cdad82fd 627 elif ! strstr "$hookdirs" "$1"; then
3b403b32 628 dfatal "No such hook type $1. Aborting initrd creation."
cc02093d 629 exit 1
cdad82fd 630 fi
0b53ca70 631 inst_simple "$3" "/lib/dracut/hooks/${1}/${2}${3##*/}"
53f95456
VL
632}
633
3378a54f
634# install any of listed files
635#
636# If first argument is '-d' and second some destination path, first accessible
637# source is installed into this path, otherwise it will installed in the same
638# path as source. If none of listed files was installed, function return 1.
639# On first successful installation it returns with 0 status.
640#
641# Example:
642#
643# inst_any -d /bin/foo /bin/bar /bin/baz
644#
645# Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
646# initramfs.
647inst_any() {
648 local to f
649
650 [[ $1 = '-d' ]] && to="$2" && shift 2
651
652 for f in "$@"; do
653 if [[ -e $f ]]; then
654 [[ $to ]] && inst "$f" "$to" && return 0
655 inst "$f" && return 0
656 fi
657 done
658
659 return 1
660}
661
7e2bca48
HH
662# dracut_install [-o ] <file> [<file> ... ]
663# Install <file> to the initramfs image
664# -o optionally install the <file> and don't fail, if it is not there
f4fff04e 665dracut_install() {
29b10e65 666 local _optional=no
0b706743 667 if [[ $1 = '-o' ]]; then
29b10e65 668 _optional=yes
cc02093d 669 shift
bd4c4bcb 670 fi
f4fff04e 671 while (($# > 0)); do
cc02093d 672 if ! inst "$1" ; then
29b10e65 673 if [[ $_optional = yes ]]; then
a4c235ed 674 dinfo "Skipping program $1 as it cannot be found and is" \
e27770e1 675 "flagged to be optional"
cc02093d 676 else
e27770e1 677 dfatal "Failed to install $1"
cc02093d 678 exit 1
bd4c4bcb 679 fi
cc02093d
HH
680 fi
681 shift
f4fff04e
VL
682 done
683}
684
87122afc
685# install function decompressing the target and handling symlinks
686# $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
687#
688# Function install targets in the same paths inside overlay but decompressed
689# and without extensions (.gz, .bz2).
690inst_decompress() {
29b10e65 691 local _src _dst _realsrc _realdst _cmd
87122afc 692
29b10e65 693 for _src in $@
87122afc 694 do
29b10e65
HH
695 case ${_src} in
696 *.gz) _cmd='gzip -d' ;;
697 *.bz2) _cmd='bzip2 -d' ;;
87122afc
698 *) return 1 ;;
699 esac
700
29b10e65 701 if [[ -L ${_src} ]]
87122afc 702 then
29b10e65
HH
703 _realsrc="$(readlink -f ${_src})" # symlink target with extension
704 _dst="${_src%.*}" # symlink without extension
705 _realdst="${_realsrc%.*}" # symlink target without extension
706 mksubdirs "${initdir}/${_src}"
87122afc 707 # Create symlink without extension to target without extension.
29b10e65 708 ln -sfn "${_realdst}" "${initdir}/${_dst}"
87122afc
709 fi
710
711 # If the source is symlink we operate on its target.
29b10e65
HH
712 [[ ${_realsrc} ]] && _src=${_realsrc}
713 inst ${_src}
87122afc
714 # Decompress with chosen tool. We assume that tool changes name e.g.
715 # from 'name.gz' to 'name'.
29b10e65 716 ${_cmd} "${initdir}${_src}"
87122afc
717 done
718}
719
720# It's similar to above, but if file is not compressed, performs standard
721# install.
722# $@ = list of files
723inst_opt_decompress() {
29b10e65 724 local _src
87122afc 725
29b10e65 726 for _src in $@
87122afc 727 do
29b10e65 728 inst_decompress "${_src}" || inst "${_src}"
87122afc
729 done
730}
731
7e2bca48
HH
732# module_check <dracut module>
733# execute the check() function of module-setup.sh of <dracut module>
734# or the "check" script, if module-setup.sh is not found
735# "check $hostonly" is called
95d2dabc 736module_check() {
29b10e65
HH
737 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
738 local _ret
31f1c02d
AW
739 local _forced=0
740 local _hostonly=$hostonly
741 [ $# -eq 2 ] && _forced=$2
29b10e65
HH
742 [[ -d $_moddir ]] || return 1
743 if [[ ! -f $_moddir/module-setup.sh ]]; then
95d2dabc 744 # if we do not have a check script, we are unconditionally included
29b10e65 745 [[ -x $_moddir/check ]] || return 0
31f1c02d 746 [ $_forced -ne 0 ] && unset hostonly
29b10e65 747 $_moddir/check $hostonly
31f1c02d 748 _ret=$?
95d2dabc
HH
749 else
750 unset check depends install installkernel
29b10e65 751 . $_moddir/module-setup.sh
95d2dabc 752 is_func check || return 0
31f1c02d
AW
753 [ $_forced -ne 0 ] && unset hostonly
754 check $hostonly
29b10e65 755 _ret=$?
95d2dabc 756 unset check depends install installkernel
0c2e3d12 757 fi
31f1c02d
AW
758 hostonly=$_hostonly
759 return $_ret
0c2e3d12
VL
760}
761
7e2bca48
HH
762# module_check_mount <dracut module>
763# execute the check() function of module-setup.sh of <dracut module>
764# or the "check" script, if module-setup.sh is not found
765# "mount_needs=1 check 0" is called
766module_check_mount() {
767 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
768 local _ret
769 mount_needs=1
770 [[ -d $_moddir ]] || return 1
771 if [[ ! -f $_moddir/module-setup.sh ]]; then
772 # if we do not have a check script, we are unconditionally included
773 [[ -x $_moddir/check ]] || return 0
774 mount_needs=1 $_moddir/check 0
775 _ret=$?
776 else
777 unset check depends install installkernel
778 . $_moddir/module-setup.sh
779 is_func check || return 1
780 check 0
781 _ret=$?
782 unset check depends install installkernel
783 fi
784 unset mount_needs
785 return $_ret
786}
787
788# module_depends <dracut module>
789# execute the depends() function of module-setup.sh of <dracut module>
790# or the "depends" script, if module-setup.sh is not found
95d2dabc 791module_depends() {
29b10e65
HH
792 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
793 local _ret
794 [[ -d $_moddir ]] || return 1
795 if [[ ! -f $_moddir/module-setup.sh ]]; then
95d2dabc 796 # if we do not have a check script, we have no deps
29b10e65
HH
797 [[ -x $_moddir/check ]] || return 0
798 $_moddir/check -d
95d2dabc
HH
799 return $?
800 else
801 unset check depends install installkernel
29b10e65 802 . $_moddir/module-setup.sh
95d2dabc
HH
803 is_func depends || return 0
804 depends
29b10e65 805 _ret=$?
95d2dabc 806 unset check depends install installkernel
29b10e65 807 return $_ret
33ee031c 808 fi
0c2e3d12
VL
809}
810
7e2bca48
HH
811# module_install <dracut module>
812# execute the install() function of module-setup.sh of <dracut module>
813# or the "install" script, if module-setup.sh is not found
95d2dabc 814module_install() {
29b10e65
HH
815 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
816 local _ret
817 [[ -d $_moddir ]] || return 1
818 if [[ ! -f $_moddir/module-setup.sh ]]; then
819 [[ -x $_moddir/install ]] && . "$_moddir/install"
95d2dabc
HH
820 return $?
821 else
822 unset check depends install installkernel
29b10e65 823 . $_moddir/module-setup.sh
95d2dabc
HH
824 is_func install || return 0
825 install
29b10e65 826 _ret=$?
95d2dabc 827 unset check depends install installkernel
29b10e65 828 return $_ret
95d2dabc
HH
829 fi
830}
831
7e2bca48
HH
832# module_installkernel <dracut module>
833# execute the installkernel() function of module-setup.sh of <dracut module>
834# or the "installkernel" script, if module-setup.sh is not found
95d2dabc 835module_installkernel() {
29b10e65
HH
836 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
837 local _ret
838 [[ -d $_moddir ]] || return 1
839 if [[ ! -f $_moddir/module-setup.sh ]]; then
840 [[ -x $_moddir/installkernel ]] && . "$_moddir/installkernel"
95d2dabc
HH
841 return $?
842 else
843 unset check depends install installkernel
29b10e65 844 . $_moddir/module-setup.sh
95d2dabc
HH
845 is_func installkernel || return 0
846 installkernel
29b10e65 847 _ret=$?
95d2dabc 848 unset check depends install installkernel
29b10e65 849 return $_ret
95d2dabc
HH
850 fi
851}
852
7e2bca48
HH
853# check_mount <dracut module>
854# check_mount checks, if a dracut module is needed for the given
855# device and filesystem types in "${host_fs_types[@]}"
1b7fd0fa
AW
856check_mount() {
857 local _mod=$1
858 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
859 local _ret
860 local _moddep
861 # If we are already scheduled to be loaded, no need to check again.
862 strstr " $mods_to_load " " $_mod " && return 0
863 strstr " $mods_checked_as_dep " " $_mod " && return 1
864
865 # This should never happen, but...
866 [[ -d $_moddir ]] || return 1
867
868 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
869
870 strstr " $omit_dracutmodules " " $_mod " && return 1
871
872 if [ "${#host_fs_types[*]}" -gt 0 ]; then
873 module_check_mount $_mod || return 1
874 else
875 # skip this module
876 return 1
877 fi
878
879 for _moddep in $(module_depends $_mod); do
880 # handle deps as if they were manually added
881 strstr " $add_dracutmodules " " $_moddep " || \
882 add_dracutmodules+=" $_moddep "
883 strstr " $force_add_dracutmodules " " $_moddep " || \
884 force_add_dracutmodules+=" $_moddep "
885 # if a module we depend on fail, fail also
886 check_module $_moddep || return 1
887 done
888
889 strstr " $mods_to_load " " $_mod " || \
890 mods_to_load+=" $_mod "
891
892 return 0
893}
894
7e2bca48
HH
895# check_module <dracut module> [<use_as_dep>]
896# check if a dracut module is to be used in the initramfs process
897# if <use_as_dep> is set, then the process also keeps track
898# that the modules were checked for the dependency tracking process
95d2dabc 899check_module() {
29b10e65
HH
900 local _mod=$1
901 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
902 local _ret
903 local _moddep
95d2dabc 904 # If we are already scheduled to be loaded, no need to check again.
29b10e65
HH
905 strstr " $mods_to_load " " $_mod " && return 0
906 strstr " $mods_checked_as_dep " " $_mod " && return 1
95d2dabc
HH
907
908 # This should never happen, but...
29b10e65 909 [[ -d $_moddir ]] || return 1
95d2dabc 910
29b10e65 911 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
95d2dabc 912
29b10e65 913 strstr " $omit_dracutmodules " " $_mod " && return 1
95d2dabc 914
31f1c02d
AW
915 if strstr " $dracutmodules $add_dracutmodules $force_add_dracutmodules" " $_mod "; then
916 if strstr " $force_add_dracutmodules" " $_mod"; then
917 module_check $_mod 1; ret=$?
918 else
919 module_check $_mod 0; ret=$?
920 fi
95d2dabc
HH
921 # explicit module, so also accept ret=255
922 [[ $ret = 0 || $ret = 255 ]] || return 1
923 else
924 # module not in our list
925 if [[ $dracutmodules = all ]]; then
926 # check, if we can and should install this module
29b10e65 927 module_check $_mod || return 1
cc02093d 928 else
95d2dabc
HH
929 # skip this module
930 return 1
970e646b 931 fi
95d2dabc 932 fi
bb764545 933
29b10e65 934 for _moddep in $(module_depends $_mod); do
95d2dabc 935 # handle deps as if they were manually added
29b10e65
HH
936 strstr " $add_dracutmodules " " $_moddep " || \
937 add_dracutmodules+=" $_moddep "
31f1c02d
AW
938 strstr " $force_add_dracutmodules " " $_moddep " || \
939 force_add_dracutmodules+=" $_moddep "
95d2dabc 940 # if a module we depend on fail, fail also
29b10e65 941 check_module $_moddep || return 1
0c2e3d12 942 done
70503db4 943
29b10e65
HH
944 strstr " $mods_to_load " " $_mod " || \
945 mods_to_load+=" $_mod "
95d2dabc
HH
946
947 return 0
948}
949
7e2bca48
HH
950# for_each_module_dir <func>
951# execute "<func> <dracut module> 1"
1b7fd0fa 952for_each_module_dir() {
29b10e65
HH
953 local _modcheck
954 local _mod
955 local _moddir
1b7fd0fa
AW
956 local _func
957 _func=$1
29b10e65
HH
958 for _moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
959 _mod=${_moddir##*/}; _mod=${_mod#[0-9][0-9]}
1b7fd0fa 960 $_func $_mod 1
70503db4 961 done
724b87a6
HH
962
963 # Report any missing dracut modules, the user has specified
31f1c02d 964 _modcheck="$add_dracutmodules $force_add_dracutmodules"
29b10e65
HH
965 [[ $dracutmodules != all ]] && _modcheck="$m $dracutmodules"
966 for _mod in $_modcheck; do
967 strstr "$mods_to_load" "$_mod" && continue
968 strstr "$omit_dracutmodules" "$_mod" && continue
969 derror "Dracut module \"$_mod\" cannot be found."
724b87a6 970 done
0c2e3d12
VL
971}
972
ddfd1d10
VL
973# Install a single kernel module along with any firmware it may require.
974# $1 = full path to kernel module to install
975install_kmod_with_fw() {
0b440844 976 # no need to go further if the module is already installed
0b706743
977 [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
978 && return 0
fcbcb252
HH
979
980 if [[ $omit_drivers ]]; then
981 local _kmod=${1##*/}
982 _kmod=${_kmod%.ko}
983 _kmod=${_kmod/-/_}
34248c92
HH
984 if [[ "$_kmod" =~ $omit_drivers ]]; then
985 dinfo "Omitting driver $_kmod"
986 return 1
987 fi
988 if [[ "${1##*/lib/modules/$kernel/}" =~ $omit_drivers ]]; then
fcbcb252
HH
989 dinfo "Omitting driver $_kmod"
990 return 1
991 fi
992 fi
993
0b706743
994 inst_simple "$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" \
995 || return $?
e6024e00
JR
996
997 local _modname=${1##*/} _fwdir _found _fw
998 _modname=${_modname%.ko*}
29b10e65
HH
999 for _fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do
1000 _found=''
1001 for _fwdir in $fw_dir; do
1002 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1003 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1004 _found=yes
cc02093d
HH
1005 fi
1006 done
29b10e65
HH
1007 if [[ $_found != yes ]]; then
1008 if ! grep -qe "\<${_modname//-/_}\>" /proc/modules; then
1009 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
1010 "\"${_modname}.ko\""
e2cdb570 1011 else
29b10e65
HH
1012 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
1013 "\"${_modname}.ko\""
e2cdb570 1014 fi
cc02093d 1015 fi
ddfd1d10 1016 done
0b440844 1017 return 0
ddfd1d10
VL
1018}
1019
1020# Do something with all the dependencies of a kernel module.
1021# Note that kernel modules depend on themselves using the technique we use
0b706743
1022# $1 = function to call for each dependency we find
1023# It will be passed the full path to the found kernel module
ddfd1d10
VL
1024# $2 = module to get dependencies for
1025# rest of args = arguments to modprobe
c32bda6b 1026# _fderr specifies FD passed from surrounding scope
ddfd1d10 1027for_each_kmod_dep() {
29b10e65 1028 local _func=$1 _kmod=$2 _cmd _modpath _options _found=0
ddfd1d10 1029 shift 2
c32bda6b 1030 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
29b10e65
HH
1031 while read _cmd _modpath _options; do
1032 [[ $_cmd = insmod ]] || continue
1033 $_func ${_modpath} || exit $?
1034 _found=1
0b706743 1035 done
29b10e65 1036 [[ $_found -eq 0 ]] && exit 1
0b706743
1037 exit 0
1038 )
ddfd1d10
VL
1039}
1040
4073c815
HH
1041# filter kernel modules to install certain modules that meet specific
1042# requirements.
1043# $1 = search only in subdirectory of /kernel/$1
1044# $2 = function to call with module name to filter.
1045# This function will be passed the full path to the module to test.
1046# The behaviour of this function can vary depending on whether $hostonly is set.
1047# If it is, we will only look at modules that are already in memory.
1048# If it is not, we will look at all kernel modules
1049# This function returns the full filenames of modules that match $1
1050filter_kernel_modules_by_path () (
1051 local _modname _filtercmd
1052 if ! [[ $hostonly ]]; then
1053 _filtercmd='find "$srcmods/kernel/$1" "$srcmods/extra"'
1054 _filtercmd+=' "$srcmods/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
7fffc9f1 1055 _filtercmd+=' -o -name "*.ko.xz"'
4073c815
HH
1056 _filtercmd+=' 2>/dev/null'
1057 else
1058 _filtercmd='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
1059 _filtercmd+='-k $kernel 2>/dev/null'
1060 fi
1061 for _modname in $(eval $_filtercmd); do
1062 case $_modname in
1063 *.ko) "$2" "$_modname" && echo "$_modname";;
1064 *.ko.gz) gzip -dc "$_modname" > $initdir/$$.ko
1065 $2 $initdir/$$.ko && echo "$_modname"
1066 rm -f $initdir/$$.ko
1067 ;;
7fffc9f1
JB
1068 *.ko.xz) xz -dc "$_modname" > $initdir/$$.ko
1069 $2 $initdir/$$.ko && echo "$_modname"
1070 rm -f $initdir/$$.ko
1071 ;;
4073c815
HH
1072 esac
1073 done
1074)
881eda69
JR
1075find_kernel_modules_by_path () (
1076 if ! [[ $hostonly ]]; then
1077 find "$srcmods/kernel/$1" "$srcmods/extra" "$srcmods/weak-updates" \
7fffc9f1 1078 -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev/null
881eda69
JR
1079 else
1080 cut -d " " -f 1 </proc/modules \
1081 | xargs modinfo -F filename -k $kernel 2>/dev/null
1082 fi
1083)
4073c815 1084
ceebd9ac
JR
1085filter_kernel_modules () {
1086 filter_kernel_modules_by_path drivers "$1"
1087}
240cc7c4 1088
881eda69
JR
1089find_kernel_modules () {
1090 find_kernel_modules_by_path drivers
1091}
1092
7e2bca48
HH
1093# instmods <kernel module> [<kernel module> ... ]
1094# instmods <kernel subsystem>
ddfd1d10 1095# install kernel modules along with all their dependencies.
7e2bca48 1096# <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
6fa0c3d6 1097instmods() {
0c1a8ebc 1098 [[ $no_kernel = yes ]] && return
c32bda6b
MS
1099 # called [sub]functions inherit _fderr
1100 local _fderr=9
881eda69
JR
1101
1102 function inst1mod() {
1103 local _mod="$1"
29b10e65 1104 case $_mod in
0b706743 1105 =*)
cc02093d 1106 # This introduces 2 incompatible meanings for =* arguments
74534e19 1107 # to instmods. We need to decide which one to keep.
29b10e65 1108 if [[ $_mod = =ata && -f $srcmods/modules.block ]]; then
0024702f
JR
1109 ( [[ "$_mpargs" ]] && echo $_mpargs
1110 egrep 'ata|ahci' "${srcmods}/modules.block" ) \
881eda69 1111 | instmods
29b10e65 1112 elif [ -f $srcmods/modules.${_mod#=} ]; then
0024702f
JR
1113 ( [[ "$_mpargs" ]] && echo $_mpargs
1114 cat "${srcmods}/modules.${_mod#=}" ) \
881eda69 1115 | instmods
cc02093d 1116 else
0024702f
JR
1117 ( [[ "$_mpargs" ]] && echo $_mpargs
1118 find "$srcmods" -path "*/${_mod#=}/*" ) \
881eda69 1119 | instmods
cc02093d
HH
1120 fi
1121 ;;
0024702f 1122 --*) _mpargs+=" $_mod" ;;
881eda69 1123 i2o_scsi) return ;; # Do not load this diagnostic-only module
34248c92 1124 *)
6568d86a 1125 # if we are already installed, skip this module and go on
cc02093d 1126 # to the next one.
881eda69
JR
1127 [[ -f $initdir/$1 ]] && return
1128
34248c92
HH
1129 if [[ $omit_drivers ]] && [[ "$1" =~ $omit_drivers ]]; then
1130 dinfo "Omitting driver ${_mod##$srcmods}"
1131 return
1132 fi
1133
1134 _mod=${_mod##*/}
cc02093d
HH
1135 # If we are building a host-specific initramfs and this
1136 # module is not already loaded, move on to the next one.
29b10e65 1137 [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc/modules \
881eda69
JR
1138 && ! echo $add_drivers | grep -qe "\<${_mod}\>" \
1139 && return
04b56f3a 1140
cc02093d
HH
1141 # We use '-d' option in modprobe only if modules prefix path
1142 # differs from default '/'. This allows us to use Dracut with
1143 # old version of modprobe which doesn't have '-d' option.
881eda69 1144 local _moddirname=${srcmods%%/lib/modules/*}
29b10e65 1145 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
0b706743 1146
cc02093d
HH
1147 # ok, load the module, all its dependencies, and any firmware
1148 # it may require
29b10e65 1149 for_each_kmod_dep install_kmod_with_fw $_mod \
f4ca564b 1150 --set-version $kernel ${_moddirname} $_mpargs
29b10e65 1151 ((_ret+=$?))
cc02093d 1152 ;;
0b706743 1153 esac
881eda69
JR
1154 }
1155
f9708da2
JR
1156 function instmods_1() {
1157 local _ret=0 _mod _mpargs
1158 if (($# == 0)); then # filenames from stdin
1159 while read _mod; do
1160 inst1mod "${_mod%.ko*}"
1161 done
1162 fi
1163 while (($# > 0)); do # filenames as arguments
1164 inst1mod ${1%.ko*}
1165 shift
881eda69 1166 done
f9708da2
JR
1167 return $_ret
1168 }
1169
c32bda6b
MS
1170 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1171 # redirections, but that would make dracut require bash4 at least.
1172 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
f9708da2
JR
1173 | egrep -v 'FATAL: Module .* not found.' | derror
1174 return $?
0b706743 1175}