]> git.ipfire.org Git - thirdparty/dracut.git/blame - dracut-functions.sh
FIPS workaround for fipscheck dir
[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
7ae5d9d1 386# finds the major:minor of the block device backing the root filesystem.
f76ef3aa
VL
387find_root_block_device() { find_block_device /; }
388
7e2bca48
HH
389# for_each_host_dev_fs <func>
390# Execute "<func> <dev> <filesystem>" for every "<dev>|<fs>" pair found
391# in ${host_fs_types[@]}
480d772f
HH
392for_each_host_dev_fs()
393{
394 local _func="$1"
d0096de7
AW
395 local _dev
396 local _fs
2efa546f 397 local _ret=1
480d772f
HH
398 for f in ${host_fs_types[@]}; do
399 OLDIFS="$IFS"
400 IFS="|"
401 set -- $f
402 IFS="$OLDIFS"
d0096de7
AW
403 _dev="$1"
404 [[ -b "$_dev" ]] || continue
405 _fs="$2"
2efa546f 406 $_func $_dev $_fs && _ret=0
480d772f 407 done
2efa546f 408 return $_ret
480d772f
HH
409}
410
17829e94
VL
411# Walk all the slave relationships for a given block device.
412# Stop when our helper function returns success
413# $1 = function to call on every found block device
414# $2 = block device in major:minor format
0b706743 415check_block_and_slaves() {
29b10e65 416 local _x
17829e94
VL
417 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
418 "$1" $2 && return
533d7dc4 419 check_vol_slaves "$@" && return 0
3478b314 420 if [[ -f /sys/dev/block/$2/../dev ]]; then
0b706743 421 check_block_and_slaves $1 $(cat "/sys/dev/block/$2/../dev") && return 0
a3afcf2a 422 fi
17829e94 423 [[ -d /sys/dev/block/$2/slaves ]] || return 1
29b10e65 424 for _x in /sys/dev/block/$2/slaves/*/dev; do
d82e0d7d 425 [[ -f $_x ]] || continue
29b10e65 426 check_block_and_slaves $1 $(cat "$_x") && return 0
17829e94
VL
427 done
428 return 1
429}
430
83e0dc7a
DY
431check_block_and_slaves_all() {
432 local _x _ret=1
433 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
434 if "$1" $2; then
435 _ret=0
436 fi
437 check_vol_slaves "$@" && return 0
438 if [[ -f /sys/dev/block/$2/../dev ]]; then
439 check_block_and_slaves_all $1 $(cat "/sys/dev/block/$2/../dev") && _ret=0
440 fi
441 [[ -d /sys/dev/block/$2/slaves ]] || return 1
442 for _x in /sys/dev/block/$2/slaves/*/dev; do
443 [[ -f $_x ]] || continue
444 check_block_and_slaves_all $1 $(cat "$_x") && _ret=0
445 done
446 return $_ret
447}
448# for_each_host_dev_and_slaves <func>
449# Execute "<func> <dev>" for every "<dev>" found
450# in ${host_devs[@]} and their slaves
451for_each_host_dev_and_slaves_all()
452{
453 local _func="$1"
454 local _dev
455 local _ret=1
456 for _dev in ${host_devs[@]}; do
457 [[ -b "$_dev" ]] || continue
83e0dc7a
DY
458 if check_block_and_slaves_all $_func $(get_maj_min $_dev); then
459 _ret=0
460 fi
461 done
462 return $_ret
463}
464
465for_each_host_dev_and_slaves()
466{
467 local _func="$1"
468 local _dev
469 for _dev in ${host_devs[@]}; do
470 [[ -b "$_dev" ]] || continue
83e0dc7a
DY
471 check_block_and_slaves_all $_func $(get_maj_min $_dev) && return 0
472 done
473 return 1
474}
475
533d7dc4
HH
476# ugly workaround for the lvm design
477# There is no volume group device,
478# so, there are no slave devices for volume groups.
479# Logical volumes only have the slave devices they really live on,
480# but you cannot create the logical volume without the volume group.
95bde758 481# And the volume group might be bigger than the devices the LV needs.
533d7dc4 482check_vol_slaves() {
29b10e65 483 local _lv _vg _pv
0b706743 484 for i in /dev/mapper/*; do
c4e48eae 485 _lv=$(get_maj_min $i)
29b10e65
HH
486 if [[ $_lv = $2 ]]; then
487 _vg=$(lvm lvs --noheadings -o vg_name $i 2>/dev/null)
cc02093d 488 # strip space
29b10e65
HH
489 _vg=$(echo $_vg)
490 if [[ $_vg ]]; then
491 for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2>/dev/null)
0b706743 492 do
c4e48eae 493 check_block_and_slaves $1 $(get_maj_min $_pv) && return 0
cc02093d
HH
494 done
495 fi
496 fi
533d7dc4
HH
497 done
498 return 1
499}
500
8aa99268
HH
501# fs_get_option <filesystem options> <search for option>
502# search for a specific option in a bunch of filesystem options
503# and return the value
504fs_get_option() {
505 local _fsopts=$1
506 local _option=$2
507 local OLDIFS="$IFS"
508 IFS=,
509 set -- $_fsopts
510 IFS="$OLDIFS"
511 while [ $# -gt 0 ]; do
512 case $1 in
513 $_option=*)
514 echo ${1#${_option}=}
515 break
516 esac
517 shift
518 done
519}
520
f4031e8a 521if [[ $DRACUT_INSTALL ]]; then
89d44e72
HH
522 [[ $DRACUT_RESOLVE_LAZY ]] || export DRACUT_RESOLVE_DEPS=1
523 inst_dir() {
524 [[ -e ${initdir}/"$1" ]] && return 0 # already there
f4031e8a
HH
525 $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@"
526 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@" || :
89d44e72 527 }
19612483 528
89d44e72
HH
529 inst() {
530 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
f4031e8a
HH
531 #dinfo "$DRACUT_INSTALL -l $@"
532 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
533 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
89d44e72
HH
534 }
535
536 inst_simple() {
537 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
538 [[ -e $1 ]] || return 1 # no source
f4031e8a
HH
539 $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@"
540 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@" || :
89d44e72
HH
541 }
542
543 inst_symlink() {
544 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
545 [[ -L $1 ]] || return 1
f4031e8a
HH
546 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
547 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
89d44e72
HH
548 }
549
550 dracut_install() {
f4031e8a
HH
551 #dinfo "initdir=$initdir $DRACUT_INSTALL -l $@"
552 $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
553 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
89d44e72
HH
554 }
555
556 inst_library() {
557 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
558 [[ -e $1 ]] || return 1 # no source
f4031e8a
HH
559 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
560 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
89d44e72
HH
561 }
562
563 inst_binary() {
f4031e8a
HH
564 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
565 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
89d44e72
HH
566 }
567
568 inst_script() {
f4031e8a
HH
569 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
570 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
89d44e72
HH
571 }
572
573else
574
575 # Install a directory, keeping symlinks as on the original system.
576 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
577 # will create ${initdir}/lib64, ${initdir}/lib64/file,
578 # and a symlink ${initdir}/lib -> lib64.
579 inst_dir() {
580 [[ -e ${initdir}/"$1" ]] && return 0 # already there
581
582 local _dir="$1" _part="${1%/*}" _file
583 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
584 _dir="$_part $_dir"
585 _part=${_part%/*}
586 done
587
588 # iterate over parent directories
589 for _file in $_dir; do
590 [[ -e "${initdir}/$_file" ]] && continue
591 if [[ -L $_file ]]; then
592 inst_symlink "$_file"
593 else
19612483 594 # create directory
89d44e72
HH
595 mkdir -m 0755 -p "${initdir}/$_file" || return 1
596 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
597 chmod u+w "${initdir}/$_file"
598 fi
599 done
600 }
601
602 # $1 = file to copy to ramdisk
603 # $2 (optional) Name for the file on the ramdisk
604 # Location of the image dir is assumed to be $initdir
605 # We never overwrite the target if it exists.
606 inst_simple() {
607 [[ -f "$1" ]] || return 1
608 strstr "$1" "/" || return 1
609 local _src=$1 _target="${2:-$1}"
610
611 [[ -L $_src ]] && { inst_symlink $_src $_target; return $?; }
612
613 if ! [[ -d ${initdir}/$_target ]]; then
614 [[ -e ${initdir}/$_target ]] && return 0
615 [[ -L ${initdir}/$_target ]] && return 0
616 [[ -d "${initdir}/${_target%/*}" ]] || inst_dir "${_target%/*}"
19612483 617 fi
89d44e72
HH
618 if [[ $DRACUT_FIPS_MODE ]]; then
619 # install checksum files also
620 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
621 inst "${_src%/*}/.${_src##*/}.hmac" "${_target%/*}/.${_target##*/}.hmac"
622 fi
00083a09
MB
623 if [[ -e "/lib/fipscheck/${_src##*/}.hmac" ]]; then
624 inst "/lib/fipscheck/${_src##*/}.hmac" "/lib/fipscheck/${_target##*/}.hmac"
625 fi
626 if [[ -e "/lib64/fipscheck/${_src##*/}.hmac" ]]; then
627 inst "/lib64/fipscheck/${_src##*/}.hmac" "/lib64/fipscheck/${_target##*/}.hmac"
628 fi
89d44e72
HH
629 fi
630 ddebug "Installing $_src"
631 cp --reflink=auto --sparse=auto -pfL "$_src" "${initdir}/$_target"
632 }
19612483 633
89d44e72
HH
634 # same as above, but specialized for symlinks
635 inst_symlink() {
636 local _src=$1 _target=${2:-$1} _realsrc
637 strstr "$1" "/" || return 1
638 [[ -L $1 ]] || return 1
639 [[ -L $initdir/$_target ]] && return 0
640 _realsrc=$(readlink -f "$_src")
641 if ! [[ -e $initdir/$_realsrc ]]; then
642 if [[ -d $_realsrc ]]; then
643 inst_dir "$_realsrc"
644 else
645 inst "$_realsrc"
646 fi
647 fi
648 [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}"
3f590c78 649
89d44e72
HH
650 ln_r "${_realsrc}" "${_target}"
651 }
22048b44 652
89d44e72
HH
653 # Same as above, but specialized to handle dynamic libraries.
654 # It handles making symlinks according to how the original library
655 # is referenced.
656 inst_library() {
657 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
658 strstr "$1" "/" || return 1
659 [[ -e $initdir/$_dest ]] && return 0
660 if [[ -L $_src ]]; then
661 if [[ $DRACUT_FIPS_MODE ]]; then
662 # install checksum files also
663 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
664 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
665 fi
00083a09
MB
666 if [[ -e "/lib/fipscheck/${_src##*/}.hmac" ]]; then
667 inst "/lib/fipscheck/${_src##*/}.hmac" "/lib/fipscheck/${_dest##*/}.hmac"
668 fi
669 if [[ -e "/lib64/fipscheck/${_src##*/}.hmac" ]]; then
670 inst "/lib64/fipscheck/${_src##*/}.hmac" "/lib64/fipscheck/${_dest##*/}.hmac"
671 fi
89d44e72
HH
672 fi
673 _reallib=$(readlink -f "$_src")
674 inst_simple "$_reallib" "$_reallib"
675 inst_dir "${_dest%/*}"
676 ln_r "${_reallib}" "${_dest}"
677 else
678 inst_simple "$_src" "$_dest"
679 fi
22048b44 680
89d44e72
HH
681 # Create additional symlinks. See rev_symlinks description.
682 for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do
683 [[ ! -e $initdir/$_symlink ]] && {
684 ddebug "Creating extra symlink: $_symlink"
685 inst_symlink $_symlink
686 }
687 done
688 }
689
690 # Same as above, but specialized to install binary executables.
691 # Install binary executable, and all shared library dependencies, if any.
692 inst_binary() {
693 local _bin _target
694 _bin=$(find_binary "$1") || return 1
695 _target=${2:-$_bin}
696 [[ -e $initdir/$_target ]] && return 0
697 local _file _line
698 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
699 # I love bash!
700 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
701 [[ $_line = 'not a dynamic executable' ]] && break
702
703 if [[ $_line =~ $_so_regex ]]; then
704 _file=${BASH_REMATCH[1]}
705 [[ -e ${initdir}/$_file ]] && continue
706 inst_library "$_file"
707 continue
708 fi
709
710 if [[ $_line =~ not\ found ]]; then
711 dfatal "Missing a shared library required by $_bin."
712 dfatal "Run \"ldd $_bin\" to find out what it is."
713 dfatal "$_line"
714 dfatal "dracut cannot create an initrd."
715 exit 1
716 fi
717 done
718 inst_simple "$_bin" "$_target"
719 }
720
721 # same as above, except for shell scripts.
722 # If your shell script does not start with shebang, it is not a shell script.
723 inst_script() {
724 local _bin
725 _bin=$(find_binary "$1") || return 1
726 shift
727 local _line _shebang_regex
728 read -r -n 80 _line <"$_bin"
729 # If debug is set, clean unprintable chars to prevent messing up the term
730 [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]')
731 _shebang_regex='(#! *)(/[^ ]+).*'
732 [[ $_line =~ $_shebang_regex ]] || return 1
733 inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@"
734 }
735
736 # general purpose installation function
737 # Same args as above.
738 inst() {
739 local _x
740
741 case $# in
742 1) ;;
743 2) [[ ! $initdir && -d $2 ]] && export initdir=$2
744 [[ $initdir = $2 ]] && set $1;;
745 3) [[ -z $initdir ]] && export initdir=$2
746 set $1 $3;;
747 *) dfatal "inst only takes 1 or 2 or 3 arguments"
748 exit 1;;
749 esac
750 for _x in inst_symlink inst_script inst_binary inst_simple; do
751 $_x "$@" && return 0
752 done
753 return 1
754 }
755
756 # dracut_install [-o ] <file> [<file> ... ]
757 # Install <file> to the initramfs image
758 # -o optionally install the <file> and don't fail, if it is not there
759 dracut_install() {
760 local _optional=no
761 if [[ $1 = '-o' ]]; then
762 _optional=yes
763 shift
764 fi
765 while (($# > 0)); do
766 if ! inst "$1" ; then
767 if [[ $_optional = yes ]]; then
768 dinfo "Skipping program $1 as it cannot be found and is" \
769 "flagged to be optional"
770 else
771 dfatal "Failed to install $1"
772 exit 1
773 fi
774 fi
775 shift
776 done
777 }
778
779fi
36b24d7c 780
a10a1416
781# find symlinks linked to given library file
782# $1 = library file
783# Function searches for symlinks by stripping version numbers appended to
784# library filename, checks if it points to the same target and finally
785# prints the list of symlinks to stdout.
786#
787# Example:
788# rev_lib_symlinks libfoo.so.8.1
789# output: libfoo.so.8 libfoo.so
790# (Only if libfoo.so.8 and libfoo.so exists on host system.)
791rev_lib_symlinks() {
792 [[ ! $1 ]] && return 0
793
794 local fn="$1" orig="$(readlink -f "$1")" links=''
795
796 [[ ${fn} =~ .*\.so\..* ]] || return 1
797
798 until [[ ${fn##*.} == so ]]; do
799 fn="${fn%.*}"
800 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
801 done
802
0b706743 803 echo "${links}"
a10a1416
804}
805
62073c30
CG
806# attempt to install any programs specified in a udev rule
807inst_rule_programs() {
808 local _prog _bin
809
810 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
811 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
604d1eb2
HH
812 if [ -x ${udevdir}/$_prog ]; then
813 _bin=${udevdir}/$_prog
814 else
815 _bin=$(find_binary "$_prog") || {
cc218087 816 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
604d1eb2
HH
817 continue;
818 }
819 fi
820
c072e131 821 #dinfo "Installing $_bin due to it's use in the udev rule $(${1##*/})"
604d1eb2
HH
822 dracut_install "$_bin"
823 done
824 fi
825 if grep -qE 'RUN==?"[^ "]+' "$1"; then
826 for _prog in $(grep -E 'RUN==?"[^ "]+' "$1" | sed -r 's/.*RUN==?"([^ "]+).*/\1/'); do
827 if [ -x ${udevdir}/$_prog ]; then
828 _bin=${udevdir}/$_prog
829 else
830 _bin=$(find_binary "$_prog") || {
cc218087 831 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
604d1eb2
HH
832 continue;
833 }
834 fi
835
c072e131 836 #dinfo "Installing $_bin due to it's use in the udev rule $(${1##*/})"
604d1eb2
HH
837 dracut_install "$_bin"
838 done
839 fi
65d1a8a4
HH
840 if grep -qE 'IMPORT\{program\}==?"[^ "]+' "$1"; then
841 for _prog in $(grep -E 'IMPORT\{program\}==?"[^ "]+' "$1" | sed -r 's/.*IMPORT\{program\}==?"([^ "]+).*/\1/'); do
604d1eb2
HH
842 if [ -x ${udevdir}/$_prog ]; then
843 _bin=${udevdir}/$_prog
62073c30
CG
844 else
845 _bin=$(find_binary "$_prog") || {
cc218087 846 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
62073c30
CG
847 continue;
848 }
849 fi
850
c072e131 851 #dinfo "Installing $_bin due to it's use in the udev rule $(${1##*/})"
62073c30
CG
852 dracut_install "$_bin"
853 done
854 fi
855}
856
f04dc5f3
VL
857# udev rules always get installed in the same place, so
858# create a function to install them to make life simpler.
0b706743 859inst_rules() {
29b10e65 860 local _target=/etc/udev/rules.d _rule _found
fd2312e0 861
604d1eb2 862 inst_dir "${udevdir}/rules.d"
29b10e65
HH
863 inst_dir "$_target"
864 for _rule in "$@"; do
08769b7f 865 if [ "${rule#/}" = "$rule" ]; then
604d1eb2 866 for r in ${udevdir}/rules.d /etc/udev/rules.d; do
76f5fa54
HH
867 if [[ -f $r/$_rule ]]; then
868 _found="$r/$_rule"
62073c30 869 inst_rule_programs "$_found"
604d1eb2 870 inst_simple "$_found"
76f5fa54
HH
871 fi
872 done
873 fi
c97e1a76 874 for r in '' ./ $dracutbasedir/rules.d/; do
29b10e65
HH
875 if [[ -f ${r}$_rule ]]; then
876 _found="${r}$_rule"
62073c30 877 inst_rule_programs "$_found"
604d1eb2 878 inst_simple "$_found" "$_target/${_found##*/}"
c97e1a76
HH
879 fi
880 done
29b10e65 881 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
f04dc5f3
VL
882 done
883}
884
53f95456
VL
885# install function specialized for hooks
886# $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
887# All hooks should be POSIX/SuS compliant, they will be sourced by init.
888inst_hook() {
cdad82fd 889 if ! [[ -f $3 ]]; then
e27770e1
890 dfatal "Cannot install a hook ($3) that does not exist."
891 dfatal "Aborting initrd creation."
cc02093d 892 exit 1
cdad82fd 893 elif ! strstr "$hookdirs" "$1"; then
3b403b32 894 dfatal "No such hook type $1. Aborting initrd creation."
cc02093d 895 exit 1
cdad82fd 896 fi
5a84ac3f 897 inst_simple "$3" "/lib/dracut/hooks/${1}/${2}-${3##*/}"
53f95456
VL
898}
899
3378a54f
900# install any of listed files
901#
902# If first argument is '-d' and second some destination path, first accessible
903# source is installed into this path, otherwise it will installed in the same
904# path as source. If none of listed files was installed, function return 1.
905# On first successful installation it returns with 0 status.
906#
907# Example:
908#
909# inst_any -d /bin/foo /bin/bar /bin/baz
910#
911# Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
912# initramfs.
913inst_any() {
914 local to f
915
916 [[ $1 = '-d' ]] && to="$2" && shift 2
917
918 for f in "$@"; do
919 if [[ -e $f ]]; then
920 [[ $to ]] && inst "$f" "$to" && return 0
921 inst "$f" && return 0
922 fi
923 done
924
925 return 1
926}
927
c9143a63
JAH
928
929# inst_libdir_file [-n <pattern>] <file> [<file>...]
930# Install a <file> located on a lib directory to the initramfs image
cacaa90c 931# -n <pattern> install matching files
c9143a63 932inst_libdir_file() {
0c6565c8 933 local _files
c9143a63 934 if [[ "$1" == "-n" ]]; then
cacaa90c 935 local _pattern=$2
c9143a63
JAH
936 shift 2
937 for _dir in $libdirs; do
7828692f 938 for _i in "$@"; do
c9143a63 939 for _f in "$_dir"/$_i; do
7828692f 940 [[ "$_f" =~ $_pattern ]] || continue
0c6565c8 941 [[ -e "$_f" ]] && _files+="$_f "
c9143a63
JAH
942 done
943 done
944 done
945 else
946 for _dir in $libdirs; do
7828692f 947 for _i in "$@"; do
c9143a63 948 for _f in "$_dir"/$_i; do
0c6565c8 949 [[ -e "$_f" ]] && _files+="$_f "
c9143a63
JAH
950 done
951 done
952 done
953 fi
0c6565c8 954 [[ $_files ]] && dracut_install $_files
c9143a63
JAH
955}
956
957
87122afc
958# install function decompressing the target and handling symlinks
959# $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
960#
961# Function install targets in the same paths inside overlay but decompressed
962# and without extensions (.gz, .bz2).
963inst_decompress() {
b19c5d51 964 local _src _cmd
87122afc 965
29b10e65 966 for _src in $@
87122afc 967 do
29b10e65
HH
968 case ${_src} in
969 *.gz) _cmd='gzip -d' ;;
970 *.bz2) _cmd='bzip2 -d' ;;
87122afc
971 *) return 1 ;;
972 esac
b19c5d51 973 inst_simple ${_src}
87122afc
974 # Decompress with chosen tool. We assume that tool changes name e.g.
975 # from 'name.gz' to 'name'.
29b10e65 976 ${_cmd} "${initdir}${_src}"
87122afc
977 done
978}
979
980# It's similar to above, but if file is not compressed, performs standard
981# install.
982# $@ = list of files
983inst_opt_decompress() {
29b10e65 984 local _src
87122afc 985
29b10e65 986 for _src in $@
87122afc 987 do
29b10e65 988 inst_decompress "${_src}" || inst "${_src}"
87122afc
989 done
990}
991
7e2bca48
HH
992# module_check <dracut module>
993# execute the check() function of module-setup.sh of <dracut module>
994# or the "check" script, if module-setup.sh is not found
995# "check $hostonly" is called
95d2dabc 996module_check() {
29b10e65
HH
997 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
998 local _ret
31f1c02d
AW
999 local _forced=0
1000 local _hostonly=$hostonly
1001 [ $# -eq 2 ] && _forced=$2
29b10e65
HH
1002 [[ -d $_moddir ]] || return 1
1003 if [[ ! -f $_moddir/module-setup.sh ]]; then
95d2dabc 1004 # if we do not have a check script, we are unconditionally included
29b10e65 1005 [[ -x $_moddir/check ]] || return 0
31f1c02d 1006 [ $_forced -ne 0 ] && unset hostonly
29b10e65 1007 $_moddir/check $hostonly
31f1c02d 1008 _ret=$?
95d2dabc
HH
1009 else
1010 unset check depends install installkernel
29b10e65 1011 . $_moddir/module-setup.sh
95d2dabc 1012 is_func check || return 0
31f1c02d
AW
1013 [ $_forced -ne 0 ] && unset hostonly
1014 check $hostonly
29b10e65 1015 _ret=$?
95d2dabc 1016 unset check depends install installkernel
0c2e3d12 1017 fi
31f1c02d
AW
1018 hostonly=$_hostonly
1019 return $_ret
0c2e3d12
VL
1020}
1021
7e2bca48
HH
1022# module_check_mount <dracut module>
1023# execute the check() function of module-setup.sh of <dracut module>
1024# or the "check" script, if module-setup.sh is not found
1025# "mount_needs=1 check 0" is called
1026module_check_mount() {
1027 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1028 local _ret
1029 mount_needs=1
1030 [[ -d $_moddir ]] || return 1
1031 if [[ ! -f $_moddir/module-setup.sh ]]; then
1032 # if we do not have a check script, we are unconditionally included
1033 [[ -x $_moddir/check ]] || return 0
1034 mount_needs=1 $_moddir/check 0
1035 _ret=$?
1036 else
1037 unset check depends install installkernel
1038 . $_moddir/module-setup.sh
1039 is_func check || return 1
1040 check 0
1041 _ret=$?
1042 unset check depends install installkernel
1043 fi
1044 unset mount_needs
1045 return $_ret
1046}
1047
1048# module_depends <dracut module>
1049# execute the depends() function of module-setup.sh of <dracut module>
1050# or the "depends" script, if module-setup.sh is not found
95d2dabc 1051module_depends() {
29b10e65
HH
1052 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1053 local _ret
1054 [[ -d $_moddir ]] || return 1
1055 if [[ ! -f $_moddir/module-setup.sh ]]; then
95d2dabc 1056 # if we do not have a check script, we have no deps
29b10e65
HH
1057 [[ -x $_moddir/check ]] || return 0
1058 $_moddir/check -d
95d2dabc
HH
1059 return $?
1060 else
1061 unset check depends install installkernel
29b10e65 1062 . $_moddir/module-setup.sh
95d2dabc
HH
1063 is_func depends || return 0
1064 depends
29b10e65 1065 _ret=$?
95d2dabc 1066 unset check depends install installkernel
29b10e65 1067 return $_ret
33ee031c 1068 fi
0c2e3d12
VL
1069}
1070
7e2bca48
HH
1071# module_install <dracut module>
1072# execute the install() function of module-setup.sh of <dracut module>
1073# or the "install" script, if module-setup.sh is not found
95d2dabc 1074module_install() {
29b10e65
HH
1075 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1076 local _ret
1077 [[ -d $_moddir ]] || return 1
1078 if [[ ! -f $_moddir/module-setup.sh ]]; then
1079 [[ -x $_moddir/install ]] && . "$_moddir/install"
95d2dabc
HH
1080 return $?
1081 else
1082 unset check depends install installkernel
29b10e65 1083 . $_moddir/module-setup.sh
95d2dabc
HH
1084 is_func install || return 0
1085 install
29b10e65 1086 _ret=$?
95d2dabc 1087 unset check depends install installkernel
29b10e65 1088 return $_ret
95d2dabc
HH
1089 fi
1090}
1091
7e2bca48
HH
1092# module_installkernel <dracut module>
1093# execute the installkernel() function of module-setup.sh of <dracut module>
1094# or the "installkernel" script, if module-setup.sh is not found
95d2dabc 1095module_installkernel() {
29b10e65
HH
1096 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1097 local _ret
1098 [[ -d $_moddir ]] || return 1
1099 if [[ ! -f $_moddir/module-setup.sh ]]; then
1100 [[ -x $_moddir/installkernel ]] && . "$_moddir/installkernel"
95d2dabc
HH
1101 return $?
1102 else
1103 unset check depends install installkernel
29b10e65 1104 . $_moddir/module-setup.sh
95d2dabc
HH
1105 is_func installkernel || return 0
1106 installkernel
29b10e65 1107 _ret=$?
95d2dabc 1108 unset check depends install installkernel
29b10e65 1109 return $_ret
95d2dabc
HH
1110 fi
1111}
1112
7e2bca48
HH
1113# check_mount <dracut module>
1114# check_mount checks, if a dracut module is needed for the given
1115# device and filesystem types in "${host_fs_types[@]}"
1b7fd0fa
AW
1116check_mount() {
1117 local _mod=$1
1118 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1119 local _ret
1120 local _moddep
1121 # If we are already scheduled to be loaded, no need to check again.
1122 strstr " $mods_to_load " " $_mod " && return 0
1123 strstr " $mods_checked_as_dep " " $_mod " && return 1
1124
1125 # This should never happen, but...
1126 [[ -d $_moddir ]] || return 1
1127
1128 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
1129
0f283709
HH
1130 if strstr " $omit_dracutmodules " " $_mod "; then
1131 dinfo "Dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
1132 return 1
1133 fi
1b7fd0fa
AW
1134
1135 if [ "${#host_fs_types[*]}" -gt 0 ]; then
1136 module_check_mount $_mod || return 1
1137 else
1138 # skip this module
1139 return 1
1140 fi
1141
1142 for _moddep in $(module_depends $_mod); do
1143 # handle deps as if they were manually added
1144 strstr " $add_dracutmodules " " $_moddep " || \
1145 add_dracutmodules+=" $_moddep "
1146 strstr " $force_add_dracutmodules " " $_moddep " || \
1147 force_add_dracutmodules+=" $_moddep "
1148 # if a module we depend on fail, fail also
0f283709
HH
1149 if ! check_module $_moddep; then
1150 derror "Dracut module '$_mod' depends on '$_moddep', which can't be installed"
1151 return 1
1152 fi
1b7fd0fa
AW
1153 done
1154
1155 strstr " $mods_to_load " " $_mod " || \
1156 mods_to_load+=" $_mod "
1157
1158 return 0
1159}
1160
7e2bca48
HH
1161# check_module <dracut module> [<use_as_dep>]
1162# check if a dracut module is to be used in the initramfs process
1163# if <use_as_dep> is set, then the process also keeps track
1164# that the modules were checked for the dependency tracking process
95d2dabc 1165check_module() {
29b10e65
HH
1166 local _mod=$1
1167 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1168 local _ret
1169 local _moddep
95d2dabc 1170 # If we are already scheduled to be loaded, no need to check again.
29b10e65
HH
1171 strstr " $mods_to_load " " $_mod " && return 0
1172 strstr " $mods_checked_as_dep " " $_mod " && return 1
95d2dabc
HH
1173
1174 # This should never happen, but...
29b10e65 1175 [[ -d $_moddir ]] || return 1
95d2dabc 1176
29b10e65 1177 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
95d2dabc 1178
0f283709
HH
1179 if strstr " $omit_dracutmodules " " $_mod "; then
1180 dinfo "Dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
1181 return 1
1182 fi
95d2dabc 1183
31f1c02d
AW
1184 if strstr " $dracutmodules $add_dracutmodules $force_add_dracutmodules" " $_mod "; then
1185 if strstr " $force_add_dracutmodules" " $_mod"; then
1186 module_check $_mod 1; ret=$?
1187 else
1188 module_check $_mod 0; ret=$?
1189 fi
95d2dabc
HH
1190 # explicit module, so also accept ret=255
1191 [[ $ret = 0 || $ret = 255 ]] || return 1
1192 else
1193 # module not in our list
1194 if [[ $dracutmodules = all ]]; then
1195 # check, if we can and should install this module
29b10e65 1196 module_check $_mod || return 1
cc02093d 1197 else
95d2dabc
HH
1198 # skip this module
1199 return 1
970e646b 1200 fi
95d2dabc 1201 fi
bb764545 1202
29b10e65 1203 for _moddep in $(module_depends $_mod); do
95d2dabc 1204 # handle deps as if they were manually added
29b10e65
HH
1205 strstr " $add_dracutmodules " " $_moddep " || \
1206 add_dracutmodules+=" $_moddep "
31f1c02d
AW
1207 strstr " $force_add_dracutmodules " " $_moddep " || \
1208 force_add_dracutmodules+=" $_moddep "
95d2dabc 1209 # if a module we depend on fail, fail also
0f283709
HH
1210 if ! check_module $_moddep; then
1211 derror "Dracut module '$_mod' depends on '$_moddep', which can't be installed"
1212 return 1
1213 fi
0c2e3d12 1214 done
70503db4 1215
29b10e65
HH
1216 strstr " $mods_to_load " " $_mod " || \
1217 mods_to_load+=" $_mod "
95d2dabc
HH
1218
1219 return 0
1220}
1221
7e2bca48
HH
1222# for_each_module_dir <func>
1223# execute "<func> <dracut module> 1"
1b7fd0fa 1224for_each_module_dir() {
29b10e65
HH
1225 local _modcheck
1226 local _mod
1227 local _moddir
1b7fd0fa
AW
1228 local _func
1229 _func=$1
29b10e65
HH
1230 for _moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1231 _mod=${_moddir##*/}; _mod=${_mod#[0-9][0-9]}
1b7fd0fa 1232 $_func $_mod 1
70503db4 1233 done
724b87a6
HH
1234
1235 # Report any missing dracut modules, the user has specified
31f1c02d 1236 _modcheck="$add_dracutmodules $force_add_dracutmodules"
29b10e65
HH
1237 [[ $dracutmodules != all ]] && _modcheck="$m $dracutmodules"
1238 for _mod in $_modcheck; do
1239 strstr "$mods_to_load" "$_mod" && continue
1240 strstr "$omit_dracutmodules" "$_mod" && continue
f2271428 1241 derror "Dracut module '$_mod' cannot be found or installed."
724b87a6 1242 done
0c2e3d12
VL
1243}
1244
ddfd1d10
VL
1245# Install a single kernel module along with any firmware it may require.
1246# $1 = full path to kernel module to install
1247install_kmod_with_fw() {
0b440844 1248 # no need to go further if the module is already installed
379c34d2 1249
0b706743
1250 [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
1251 && return 0
fcbcb252 1252
f4031e8a
HH
1253 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -e "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}" ]]; then
1254 read ret < "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
d8aeb3a7
HH
1255 return $ret
1256 fi
379c34d2 1257
fcbcb252
HH
1258 if [[ $omit_drivers ]]; then
1259 local _kmod=${1##*/}
1260 _kmod=${_kmod%.ko}
1261 _kmod=${_kmod/-/_}
34248c92
HH
1262 if [[ "$_kmod" =~ $omit_drivers ]]; then
1263 dinfo "Omitting driver $_kmod"
7abd4264 1264 return 0
34248c92
HH
1265 fi
1266 if [[ "${1##*/lib/modules/$kernel/}" =~ $omit_drivers ]]; then
fcbcb252 1267 dinfo "Omitting driver $_kmod"
7abd4264 1268 return 0
fcbcb252
HH
1269 fi
1270 fi
1271
d8aeb3a7
HH
1272 inst_simple "$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
1273 ret=$?
f4031e8a
HH
1274 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1275 [[ -d "$DRACUT_KERNEL_LAZY_HASHDIR" ]] && \
1276 echo $ret > "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
d8aeb3a7 1277 (($ret != 0)) && return $ret
e6024e00
JR
1278
1279 local _modname=${1##*/} _fwdir _found _fw
1280 _modname=${_modname%.ko*}
29b10e65
HH
1281 for _fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do
1282 _found=''
1283 for _fwdir in $fw_dir; do
1284 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1285 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1286 _found=yes
cc02093d
HH
1287 fi
1288 done
29b10e65 1289 if [[ $_found != yes ]]; then
fe1484f3 1290 if ! [[ -d $(echo /sys/module/${_modname//-/_}|{ read a b; echo $a; }) ]]; then
29b10e65
HH
1291 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
1292 "\"${_modname}.ko\""
e2cdb570 1293 else
29b10e65
HH
1294 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
1295 "\"${_modname}.ko\""
e2cdb570 1296 fi
cc02093d 1297 fi
ddfd1d10 1298 done
0b440844 1299 return 0
ddfd1d10
VL
1300}
1301
1302# Do something with all the dependencies of a kernel module.
1303# Note that kernel modules depend on themselves using the technique we use
0b706743
1304# $1 = function to call for each dependency we find
1305# It will be passed the full path to the found kernel module
ddfd1d10
VL
1306# $2 = module to get dependencies for
1307# rest of args = arguments to modprobe
c32bda6b 1308# _fderr specifies FD passed from surrounding scope
ddfd1d10 1309for_each_kmod_dep() {
7abd4264 1310 local _func=$1 _kmod=$2 _cmd _modpath _options
ddfd1d10 1311 shift 2
c32bda6b 1312 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
29b10e65
HH
1313 while read _cmd _modpath _options; do
1314 [[ $_cmd = insmod ]] || continue
1315 $_func ${_modpath} || exit $?
0b706743 1316 done
0b706743 1317 )
ddfd1d10
VL
1318}
1319
70c6b773 1320dracut_kernel_post() {
d8aeb3a7
HH
1321 local _moddirname=${srcmods%%/lib/modules/*}
1322
f4031e8a 1323 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" ]]; then
ec61f0a3
HH
1324 xargs -r modprobe -a ${_moddirname+-d ${_moddirname}/} \
1325 --ignore-install --show-depends --set-version $kernel \
f4031e8a 1326 < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" 2>/dev/null \
89d44e72
HH
1327 | sort -u \
1328 | while read _cmd _modpath _options; do
1329 [[ $_cmd = insmod ]] || continue
1330 echo "$_modpath"
f4031e8a 1331 done > "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
d8aeb3a7 1332
89d44e72 1333 (
73575f11
HH
1334 if [[ $DRACUT_INSTALL ]] && [[ -z $_moddirname ]]; then
1335 xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
89d44e72
HH
1336 else
1337 while read _modpath; do
1338 local _destpath=$_modpath
1339 [[ $_moddirname ]] && _destpath=${_destpath##$_moddirname/}
1340 _destpath=${_destpath##*/lib/modules/$kernel/}
1341 inst_simple "$_modpath" "/lib/modules/$kernel/${_destpath}" || exit $?
f4031e8a 1342 done < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
89d44e72
HH
1343 fi
1344 ) &
d8aeb3a7 1345
73575f11
HH
1346 if [[ $DRACUT_INSTALL ]]; then
1347 xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" \
89d44e72
HH
1348 | while read line; do
1349 for _fwdir in $fw_dir; do
1350 echo $_fwdir/$line;
1351 done;
73575f11 1352 done | xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a -o
89d44e72 1353 else
73575f11 1354 for _fw in $(xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"); do
89d44e72
HH
1355 for _fwdir in $fw_dir; do
1356 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1357 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1358 break
1359 fi
1360 done
1361 done
1362 fi
d8aeb3a7 1363
89d44e72
HH
1364 wait
1365 fi
70c6b773
HH
1366
1367 for _f in modules.builtin.bin modules.builtin; do
1368 [[ $srcmods/$_f ]] && break
1369 done || {
1370 dfatal "No modules.builtin.bin and modules.builtin found!"
1371 return 1
1372 }
1373
1374 for _f in modules.builtin.bin modules.builtin modules.order; do
1375 [[ $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f"
1376 done
1377
1378 # generate module dependencies for the initrd
1379 if [[ -d $initdir/lib/modules/$kernel ]] && \
1380 ! depmod -a -b "$initdir" $kernel; then
1381 dfatal "\"depmod -a $kernel\" failed."
1382 exit 1
1383 fi
1384
f4031e8a 1385 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && rm -fr "$DRACUT_KERNEL_LAZY_HASHDIR"
d8aeb3a7 1386}
fe1484f3 1387
881eda69 1388find_kernel_modules_by_path () (
74405477 1389 local _OLDIFS
881eda69 1390 if ! [[ $hostonly ]]; then
74405477
HH
1391 _OLDIFS=$IFS
1392 IFS=:
fe1484f3 1393 while read a rest; do
d8aeb3a7 1394 [[ $a = kernel*/$1/* ]] || continue
74405477 1395 echo $srcmods/$a
fe1484f3 1396 done < $srcmods/modules.dep
74405477 1397 IFS=$_OLDIFS
881eda69 1398 else
fe1484f3 1399 ( cd /sys/module; echo *; ) \
194b80f9
HH
1400 | xargs -r modinfo -F filename -k $kernel 2>/dev/null \
1401 | while read a; do
52f74c80
HH
1402 [[ $a = */kernel*/$1/* ]] || continue
1403 echo $a
194b80f9 1404 done
881eda69 1405 fi
74405477 1406 return 0
881eda69 1407)
4073c815 1408
881eda69
JR
1409find_kernel_modules () {
1410 find_kernel_modules_by_path drivers
1411}
1412
338b43cd
HH
1413# instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1414# instmods [-c [-s]] <kernel subsystem>
ddfd1d10 1415# install kernel modules along with all their dependencies.
7e2bca48 1416# <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
6fa0c3d6 1417instmods() {
0c1a8ebc 1418 [[ $no_kernel = yes ]] && return
c32bda6b
MS
1419 # called [sub]functions inherit _fderr
1420 local _fderr=9
a6d3be9d 1421 local _check=no
338b43cd 1422 local _silent=no
a6d3be9d
AW
1423 if [[ $1 = '-c' ]]; then
1424 _check=yes
1425 shift
1426 fi
881eda69 1427
338b43cd
HH
1428 if [[ $1 = '-s' ]]; then
1429 _silent=yes
1430 shift
1431 fi
1432
881eda69 1433 function inst1mod() {
a6d3be9d 1434 local _ret=0 _mod="$1"
29b10e65 1435 case $_mod in
0b706743 1436 =*)
d8aeb3a7
HH
1437 ( [[ "$_mpargs" ]] && echo $_mpargs
1438 find_kernel_modules_by_path "${_mod#=}" ) \
1439 | instmods
1440 ((_ret+=$?))
cc02093d 1441 ;;
0024702f 1442 --*) _mpargs+=" $_mod" ;;
7abd4264 1443 i2o_scsi) return 0;; # Do not load this diagnostic-only module
34248c92 1444 *)
379c34d2 1445 _mod=${_mod##*/}
6568d86a 1446 # if we are already installed, skip this module and go on
cc02093d 1447 # to the next one.
f4031e8a
HH
1448 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1449 [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko" ]]; then
1450 read _ret <"$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko"
d8aeb3a7
HH
1451 return $_ret
1452 fi
881eda69 1453
34248c92
HH
1454 if [[ $omit_drivers ]] && [[ "$1" =~ $omit_drivers ]]; then
1455 dinfo "Omitting driver ${_mod##$srcmods}"
7abd4264 1456 return 0
34248c92 1457 fi
cc02093d
HH
1458 # If we are building a host-specific initramfs and this
1459 # module is not already loaded, move on to the next one.
fe1484f3
HH
1460 [[ $hostonly ]] \
1461 && ! [[ -d $(echo /sys/module/${_mod//-/_}|{ read a b; echo $a; }) ]] \
1462 && ! [[ "$add_drivers" =~ " ${_mod} " ]] \
7abd4264 1463 && return 0
04b56f3a 1464
f4031e8a 1465 if [[ "$_check" = "yes" ]] || ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
d8aeb3a7
HH
1466 # We use '-d' option in modprobe only if modules prefix path
1467 # differs from default '/'. This allows us to use Dracut with
1468 # old version of modprobe which doesn't have '-d' option.
1469 local _moddirname=${srcmods%%/lib/modules/*}
1470 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1471
1472 # ok, load the module, all its dependencies, and any firmware
1473 # it may require
1474 for_each_kmod_dep install_kmod_with_fw $_mod \
1475 --set-version $kernel ${_moddirname} $_mpargs
1476 ((_ret+=$?))
1477 else
f4031e8a
HH
1478 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1479 echo $_mod >> "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist"
d8aeb3a7 1480 fi
cc02093d 1481 ;;
0b706743 1482 esac
a6d3be9d 1483 return $_ret
881eda69
JR
1484 }
1485
f9708da2 1486 function instmods_1() {
a6d3be9d 1487 local _mod _mpargs
f9708da2
JR
1488 if (($# == 0)); then # filenames from stdin
1489 while read _mod; do
a6d3be9d 1490 inst1mod "${_mod%.ko*}" || {
338b43cd
HH
1491 if [[ "$_check" == "yes" ]]; then
1492 [[ "$_silent" == "no" ]] && dfatal "Failed to install $_mod"
a6d3be9d
AW
1493 return 1
1494 fi
1495 }
f9708da2
JR
1496 done
1497 fi
1498 while (($# > 0)); do # filenames as arguments
a6d3be9d 1499 inst1mod ${1%.ko*} || {
338b43cd
HH
1500 if [[ "$_check" == "yes" ]]; then
1501 [[ "$_silent" == "no" ]] && dfatal "Failed to install $1"
a6d3be9d
AW
1502 return 1
1503 fi
1504 }
f9708da2 1505 shift
881eda69 1506 done
a6d3be9d 1507 return 0
f9708da2
JR
1508 }
1509
a6d3be9d 1510 local _ret _filter_not_found='FATAL: Module .* not found.'
c32bda6b
MS
1511 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1512 # redirections, but that would make dracut require bash4 at least.
1513 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
86191581 1514 | while read line; do [[ "$line" =~ $_filter_not_found ]] || echo $line;done | derror
a6d3be9d 1515 _ret=$?
a6d3be9d 1516 return $_ret
0b706743 1517}