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