]> git.ipfire.org Git - thirdparty/dracut.git/blame - dracut-init.sh
feat(dracut-init.sh): specify if a module cannot be found or cannot be installed
[thirdparty/dracut.git] / dracut-init.sh
CommitLineData
777f2db0
HH
1#!/bin/bash
2#
561eb42f 3# functions used only by dracut and dracut modules
777f2db0
HH
4#
5# Copyright 2005-2009 Red Hat, Inc. All rights reserved.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19#
20export LC_MESSAGES=C
21
75d758e8 22if [[ $EUID == "0" ]] && ! [[ $DRACUT_NO_XATTR ]]; then
076fcd16
HH
23 export DRACUT_CP="cp --reflink=auto --sparse=auto --preserve=mode,timestamps,xattr,links -dfr"
24else
25 export DRACUT_CP="cp --reflink=auto --sparse=auto --preserve=mode,timestamps,links -dfr"
26fi
7e51a94f 27
561eb42f
HH
28# is_func <command>
29# Check whether $1 is a function.
30is_func() {
75d758e8 31 [[ "$(type -t "$1")" == "function" ]]
561eb42f
HH
32}
33
34if ! [[ $dracutbasedir ]]; then
35 dracutbasedir=${BASH_SOURCE[0]%/*}
75d758e8 36 [[ $dracutbasedir == dracut-functions* ]] && dracutbasedir="."
561eb42f
HH
37 [[ $dracutbasedir ]] || dracutbasedir="."
38 dracutbasedir="$(readlink -f $dracutbasedir)"
39fi
40
9a52c3fd 41if ! is_func dinfo > /dev/null 2>&1; then
2d4344bc 42 # shellcheck source=./dracut-logger.sh
561eb42f
HH
43 . "$dracutbasedir/dracut-logger.sh"
44 dlog_init
45fi
46
2d4344bc 47# shellcheck disable=SC2154
561eb42f
HH
48if ! [[ $initdir ]]; then
49 dfatal "initdir not set"
50 exit 1
51fi
52
53if ! [[ -d $initdir ]]; then
54 mkdir -p "$initdir"
55fi
56
561eb42f
HH
57if ! [[ $kernel ]]; then
58 kernel=$(uname -r)
59 export kernel
60fi
61
2d4344bc 62# shellcheck disable=SC2154
a0120420 63srcmods="$dracutsysrootdir/lib/modules/$kernel/"
561eb42f 64
2d4344bc 65# shellcheck disable=SC2154
561eb42f 66[[ $drivers_dir ]] && {
9a52c3fd 67 if ! command -v kmod &> /dev/null && vercmp "$(modprobe --version | cut -d' ' -f3)" lt 3.7; then
561eb42f
HH
68 dfatal 'To use --kmoddir option module-init-tools >= 3.7 is required.'
69 exit 1
70 fi
71 srcmods="$drivers_dir"
72}
73export srcmods
74
75# export standard hookdirs
76[[ $hookdirs ]] || {
77 hookdirs="cmdline pre-udev pre-trigger netroot "
78 hookdirs+="initqueue initqueue/settled initqueue/online initqueue/finished initqueue/timeout "
79 hookdirs+="pre-mount pre-pivot cleanup mount "
80 hookdirs+="emergency shutdown-emergency pre-shutdown shutdown "
81 export hookdirs
82}
83
a0120420
BZ
84DRACUT_LDD=${DRACUT_LDD:-ldd}
85DRACUT_TESTBIN=${DRACUT_TESTBIN:-/bin/sh}
86DRACUT_LDCONFIG=${DRACUT_LDCONFIG:-ldconfig}
87
2d4344bc
HH
88# shellcheck source=./dracut-functions.sh
89. "$dracutbasedir"/dracut-functions.sh
456f299a 90
561eb42f 91# Detect lib paths
9a52c3fd
HH
92if ! [[ $libdirs ]]; then
93 if [[ $("$DRACUT_LDD" "$dracutsysrootdir$DRACUT_TESTBIN") == */lib64/* ]] &> /dev/null \
a0120420 94 && [[ -d $dracutsysrootdir/lib64 ]]; then
561eb42f 95 libdirs+=" /lib64"
a0120420 96 [[ -d $dracutsysrootdir/usr/lib64 ]] && libdirs+=" /usr/lib64"
561eb42f
HH
97 else
98 libdirs+=" /lib"
a0120420 99 [[ -d $dracutsysrootdir/usr/lib ]] && libdirs+=" /usr/lib"
561eb42f
HH
100 fi
101
102 libdirs+=" $(ldconfig_paths)"
103
104 export libdirs
777f2db0
HH
105fi
106
561eb42f
HH
107# helper function for check() in module-setup.sh
108# to check for required installed binaries
109# issues a standardized warning message
110require_binaries() {
2d4344bc 111 # shellcheck disable=SC2154
561eb42f
HH
112 local _module_name="${moddir##*/}"
113 local _ret=0
114
75d758e8 115 if [[ $1 == "-m" ]]; then
561eb42f
HH
116 _module_name="$2"
117 shift 2
118 fi
119
120 for cmd in "$@"; do
9a52c3fd 121 if ! find_binary "$cmd" &> /dev/null; then
561eb42f
HH
122 dinfo "dracut module '${_module_name#[0-9][0-9]}' will not be installed, because command '$cmd' could not be found!"
123 ((_ret++))
124 fi
125 done
2d4344bc 126 return "$_ret"
561eb42f
HH
127}
128
129require_any_binary() {
130 local _module_name="${moddir##*/}"
131 local _ret=1
132
75d758e8 133 if [[ $1 == "-m" ]]; then
561eb42f
HH
134 _module_name="$2"
135 shift 2
136 fi
137
138 for cmd in "$@"; do
9a52c3fd 139 if find_binary "$cmd" &> /dev/null; then
561eb42f
HH
140 _ret=0
141 break
142 fi
143 done
144
9a52c3fd 145 if ((_ret != 0)); then
2d4344bc 146 dinfo "$_module_name: Could not find any command of '$*'!"
561eb42f
HH
147 return 1
148 fi
149
150 return 0
151}
152
d3a5e631
KS
153# helper function for check() in module-setup.sh
154# to check for required kernel modules
155# issues a standardized warning message
156require_kernel_modules() {
157 # shellcheck disable=SC2154
158 local _module_name="${moddir##*/}"
159 local _ret=0
160
d460941b
KS
161 # Ignore kernel module requirement for no-kernel build
162 [[ $no_kernel == yes ]] && return 0
163
d3a5e631
KS
164 if [[ $1 == "-m" ]]; then
165 _module_name="$2"
166 shift 2
167 fi
168
169 for mod in "$@"; do
170 if ! check_kernel_module "$mod" &> /dev/null; then
171 dinfo "dracut module '${_module_name#[0-9][0-9]}' will not be installed, because kernel module '$mod' is not available!"
172 ((_ret++))
173 fi
174 done
175 return "$_ret"
176}
177
561eb42f 178dracut_need_initqueue() {
9a52c3fd 179 : > "$initdir/lib/dracut/need-initqueue"
561eb42f
HH
180}
181
182dracut_module_included() {
2d4344bc 183 # shellcheck disable=SC2154
561eb42f
HH
184 [[ " $mods_to_load $modules_loaded " == *\ $*\ * ]]
185}
186
19e57585 187dracut_no_switch_root() {
9a52c3fd 188 : > "$initdir/lib/dracut/no-switch-root"
19e57585
KS
189}
190
5916d31b 191dracut_module_path() {
2d4344bc
HH
192 local _dir
193
194 # shellcheck disable=SC2231
195 for _dir in "${dracutbasedir}"/modules.d/??${1}; do
196 echo "$_dir"
197 return 0
198 done
199 return 1
5916d31b
KS
200}
201
561eb42f
HH
202if ! [[ $DRACUT_INSTALL ]]; then
203 DRACUT_INSTALL=$(find_binary dracut-install)
204fi
205
206if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install ]]; then
207 DRACUT_INSTALL=$dracutbasedir/dracut-install
72b700e3
AAF
208elif ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/src/install/dracut-install ]]; then
209 DRACUT_INSTALL=$dracutbasedir/src/install/dracut-install
561eb42f
HH
210fi
211
a0120420
BZ
212# Test if dracut-install is a standalone executable with no options.
213# E.g. DRACUT_INSTALL may be set externally as:
214# DRACUT_INSTALL="valgrind dracut-install"
215# or
216# DRACUT_INSTALL="dracut-install --debug"
217# in which case the string cannot be tested for being executable.
218DRINSTALLPARTS=0
9a52c3fd
HH
219for i in $DRACUT_INSTALL; do
220 DRINSTALLPARTS=$((DRINSTALLPARTS + 1))
a0120420
BZ
221done
222
75d758e8 223if [[ $DRINSTALLPARTS == 1 ]] && ! command -v "$DRACUT_INSTALL" > /dev/null 2>&1; then
561eb42f
HH
224 dfatal "dracut-install not found!"
225 exit 10
226fi
227
3ad12c7b 228if [[ $hostonly == "-h" ]]; then
75d758e8 229 if ! [[ $DRACUT_KERNEL_MODALIASES ]] || ! [[ -f $DRACUT_KERNEL_MODALIASES ]]; then
3ad12c7b 230 export DRACUT_KERNEL_MODALIASES="${DRACUT_TMPDIR}/modaliases"
097dd367 231 $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${srcmods:+--kerneldir "$srcmods"} --modalias > "$DRACUT_KERNEL_MODALIASES"
3ad12c7b
HH
232 fi
233fi
234
561eb42f
HH
235[[ $DRACUT_RESOLVE_LAZY ]] || export DRACUT_RESOLVE_DEPS=1
236inst_dir() {
d2f6f445 237 local _ret
9a52c3fd 238 [[ -e ${initdir}/"$1" ]] && return 0 # already there
097dd367 239 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} -d "$@"; then
d2f6f445
AAF
240 return 0
241 else
242 _ret=$?
2d4344bc 243 derror FAILED: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} -d "$@"
d2f6f445 244 return $_ret
2d4344bc 245 fi
561eb42f
HH
246}
247
248inst() {
d2f6f445 249 local _ret _hostonly_install
75d758e8 250 if [[ $1 == "-H" ]]; then
561eb42f
HH
251 _hostonly_install="-H"
252 shift
253 fi
9a52c3fd 254 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
097dd367 255 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"; then
d2f6f445
AAF
256 return 0
257 else
258 _ret=$?
2d4344bc 259 derror FAILED: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
d2f6f445 260 return $_ret
2d4344bc 261 fi
561eb42f
HH
262}
263
264inst_simple() {
d2f6f445 265 local _ret _hostonly_install
75d758e8 266 if [[ $1 == "-H" ]]; then
561eb42f
HH
267 _hostonly_install="-H"
268 shift
269 fi
9a52c3fd
HH
270 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
271 [[ -e $1 ]] || return 1 # no source
097dd367 272 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${_hostonly_install:+-H} "$@"; then
d2f6f445
AAF
273 return 0
274 else
275 _ret=$?
276 derror FAILED: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${_hostonly_install:+-H} "$@"
277 return $_ret
63356d54 278 fi
561eb42f
HH
279}
280
281inst_symlink() {
d2f6f445 282 local _ret _hostonly_install
75d758e8 283 if [[ $1 == "-H" ]]; then
561eb42f
HH
284 _hostonly_install="-H"
285 shift
286 fi
9a52c3fd 287 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
561eb42f 288 [[ -L $1 ]] || return 1
097dd367 289 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"; then
d2f6f445
AAF
290 return 0
291 else
292 _ret=$?
293 derror FAILED: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
294 return $_ret
63356d54 295 fi
561eb42f
HH
296}
297
298inst_multiple() {
e2fdb30b
AAF
299 local _ret _hostonly_install
300 if [[ $1 == "-H" ]]; then
301 _hostonly_install="-H"
302 shift
303 fi
097dd367 304 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} -a ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"; then
d437970c
AH
305 return 0
306 else
63356d54 307 _ret=$?
d2f6f445 308 derror FAILED: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} -a ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
63356d54
HH
309 return $_ret
310 fi
561eb42f
HH
311}
312
313dracut_install() {
314 inst_multiple "$@"
315}
316
794b2d2c 317dracut_instmods() {
d2f6f445 318 local _ret _silent=0
9a52c3fd 319 local i
63356d54 320 # shellcheck disable=SC2154
75d758e8 321 [[ $no_kernel == yes ]] && return
fa295f0b 322 for i in "$@"; do
d13071d7 323 [[ $i == "--silent" ]] && _silent=1
fa295f0b
HH
324 done
325
097dd367 326 if $DRACUT_INSTALL \
a0120420 327 ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
63356d54 328 ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${hostonly:+-H} ${omit_drivers:+-N "$omit_drivers"} ${srcmods:+--kerneldir "$srcmods"} -m "$@"; then
d2f6f445
AAF
329 return 0
330 else
331 _ret=$?
63356d54 332 if ((_silent == 0)); then
d2f6f445 333 derror FAILED: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${hostonly:+-H} ${omit_drivers:+-N "$omit_drivers"} ${srcmods:+--kerneldir "$srcmods"} -m "$@"
63356d54 334 fi
d2f6f445 335 return $_ret
63356d54 336 fi
794b2d2c
HH
337}
338
561eb42f 339inst_library() {
d2f6f445 340 local _ret _hostonly_install
75d758e8 341 if [[ $1 == "-H" ]]; then
561eb42f
HH
342 _hostonly_install="-H"
343 shift
344 fi
9a52c3fd
HH
345 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
346 [[ -e $1 ]] || return 1 # no source
097dd367 347 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"; then
d2f6f445
AAF
348 return 0
349 else
350 _ret=$?
351 derror FAILED: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
352 return $_ret
63356d54 353 fi
561eb42f
HH
354}
355
356inst_binary() {
d2f6f445 357 local _ret
097dd367 358 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"; then
d2f6f445
AAF
359 return 0
360 else
361 _ret=$?
362 derror FAILED: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
363 return $_ret
63356d54 364 fi
561eb42f
HH
365}
366
367inst_script() {
d2f6f445 368 local _ret
097dd367 369 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"; then
d2f6f445
AAF
370 return 0
371 else
372 _ret=$?
373 derror FAILED: "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
374 return $_ret
63356d54 375 fi
561eb42f
HH
376}
377
1f8a7ae7 378inst_fsck_help() {
d2f6f445 379 local _ret _helper="/run/dracut/fsck/fsck_help_$1.txt"
097dd367 380 if $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$2" "$_helper"; then
d2f6f445
AAF
381 return 0
382 else
383 _ret=$?
384 derror "$DRACUT_INSTALL" ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$2" "$_helper"
385 return $_ret
63356d54 386 fi
1f8a7ae7
MF
387}
388
a695250e 389# Use with form hostonly="$(optional_hostonly)" inst_xxxx <args>
1aafcab9 390# If hostonly mode is set to "strict", hostonly restrictions will still
a695250e
KS
391# be applied, else will ignore hostonly mode and try to install all
392# given modules.
393optional_hostonly() {
63356d54 394 # shellcheck disable=SC2154
75d758e8 395 if [[ $hostonly_mode == "strict" ]]; then
63356d54 396 printf -- "%s" "$hostonly"
a695250e
KS
397 else
398 printf ""
399 fi
400}
401
561eb42f
HH
402mark_hostonly() {
403 for i in "$@"; do
404 echo "$i" >> "$initdir/lib/dracut/hostonly-files"
405 done
406}
407
408# find symlinks linked to given library file
409# $1 = library file
410# Function searches for symlinks by stripping version numbers appended to
411# library filename, checks if it points to the same target and finally
412# prints the list of symlinks to stdout.
413#
414# Example:
415# rev_lib_symlinks libfoo.so.8.1
416# output: libfoo.so.8 libfoo.so
417# (Only if libfoo.so.8 and libfoo.so exists on host system.)
418rev_lib_symlinks() {
63356d54
HH
419 local _fn
420 local _orig
421 local _links
422
561eb42f
HH
423 [[ ! $1 ]] && return 0
424
63356d54
HH
425 _fn="$1"
426 _orig="$(readlink -f "$1")"
427 _links=()
561eb42f 428
63356d54 429 [[ ${_fn} == *.so.* ]] || return 1
561eb42f 430
63356d54
HH
431 until [[ ${_fn##*.} == so ]]; do
432 _fn="${_fn%.*}"
9a52c3fd 433 [[ -L ${_fn} ]] && [[ $(readlink -f "${_fn}") == "${_orig}" ]] && _links+=("${_fn}")
561eb42f
HH
434 done
435
63356d54 436 echo "${_links[*]}}"
561eb42f
HH
437}
438
439# attempt to install any programs specified in a udev rule
440inst_rule_programs() {
441 local _prog _bin
442
63356d54 443 # shellcheck disable=SC2013
374ef3ed 444 for _prog in $(sed -nr 's/.*PROGRAM==?"([^ "]+).*/\1/p' "$1"); do
06a1d076 445 _bin=""
63356d54
HH
446 # shellcheck disable=SC2154
447 if [[ -x ${udevdir}/$_prog ]]; then
448 _bin="${udevdir}"/$_prog
75d758e8 449 elif [[ ${_prog/\$env\{/} == "$_prog" ]]; then
06a1d076
VS
450 _bin=$(find_binary "$_prog") || {
451 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
9a52c3fd 452 continue
06a1d076
VS
453 }
454 fi
455
456 [[ $_bin ]] && inst_binary "$_bin"
457 done
63356d54
HH
458
459 # shellcheck disable=SC2013
374ef3ed 460 for _prog in $(sed -nr 's/.*RUN[+=]=?"([^ "]+).*/\1/p' "$1"); do
06a1d076 461 _bin=""
63356d54 462 if [[ -x ${udevdir}/$_prog ]]; then
06a1d076 463 _bin=${udevdir}/$_prog
75d758e8 464 elif [[ ${_prog/\$env\{/} == "$_prog" ]] && [[ ${_prog} != "/sbin/initqueue" ]]; then
06a1d076
VS
465 _bin=$(find_binary "$_prog") || {
466 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
9a52c3fd 467 continue
06a1d076
VS
468 }
469 fi
470
471 [[ $_bin ]] && inst_binary "$_bin"
472 done
63356d54
HH
473
474 # shellcheck disable=SC2013
374ef3ed 475 for _prog in $(sed -nr 's/.*IMPORT\{program\}==?"([^ "]+).*/\1/p' "$1"); do
06a1d076 476 _bin=""
63356d54 477 if [[ -x ${udevdir}/$_prog ]]; then
06a1d076 478 _bin=${udevdir}/$_prog
75d758e8 479 elif [[ ${_prog/\$env\{/} == "$_prog" ]]; then
06a1d076
VS
480 _bin=$(find_binary "$_prog") || {
481 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
9a52c3fd 482 continue
06a1d076
VS
483 }
484 fi
485
486 [[ $_bin ]] && dracut_install "$_bin"
487 done
561eb42f
HH
488}
489
bc4f196f 490# attempt to create any groups and users specified in a udev rule
561eb42f
HH
491inst_rule_group_owner() {
492 local i
493
63356d54 494 # shellcheck disable=SC2013
374ef3ed 495 for i in $(sed -nr 's/.*OWNER=?"([^ "]+).*/\1/p' "$1"); do
9a52c3fd
HH
496 if ! grep -Eq "^$i:" "$initdir/etc/passwd" 2> /dev/null; then
497 grep -E "^$i:" "$dracutsysrootdir"/etc/passwd 2> /dev/null >> "$initdir/etc/passwd"
06a1d076
VS
498 fi
499 done
63356d54
HH
500
501 # shellcheck disable=SC2013
06a1d076 502 for i in $(sed -nr 's/.*GROUP=?"([^ "]+).*/\1/p' "$1"); do
9a52c3fd
HH
503 if ! grep -Eq "^$i:" "$initdir/etc/group" 2> /dev/null; then
504 grep -E "^$i:" "$dracutsysrootdir"/etc/group 2> /dev/null >> "$initdir/etc/group"
06a1d076
VS
505 fi
506 done
561eb42f
HH
507}
508
509inst_rule_initqueue() {
510 if grep -q -F initqueue "$1"; then
511 dracut_need_initqueue
512 fi
513}
514
515# udev rules always get installed in the same place, so
516# create a function to install them to make life simpler.
517inst_rules() {
518 local _target=/etc/udev/rules.d _rule _found
519
520 inst_dir "${udevdir}/rules.d"
521 inst_dir "$_target"
522 for _rule in "$@"; do
523 if [ "${_rule#/}" = "$_rule" ]; then
63356d54 524 for r in "$dracutsysrootdir${udevdir}/rules.d" ${hostonly:+"$dracutsysrootdir"/etc/udev/rules.d}; do
561eb42f
HH
525 [[ -e $r/$_rule ]] || continue
526 _found="$r/$_rule"
527 inst_rule_programs "$_found"
528 inst_rule_group_owner "$_found"
529 inst_rule_initqueue "$_found"
530 inst_simple "$_found"
531 done
532 fi
994d0035 533 for r in '' "$dracutsysrootdir$dracutbasedir/rules.d/"; do
561eb42f
HH
534 # skip rules without an absolute path
535 [[ "${r}$_rule" != /* ]] && continue
536 [[ -f ${r}$_rule ]] || continue
537 _found="${r}$_rule"
538 inst_rule_programs "$_found"
539 inst_rule_group_owner "$_found"
540 inst_rule_initqueue "$_found"
541 inst_simple "$_found" "$_target/${_found##*/}"
542 done
543 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
544 done
545}
546
547inst_rules_wildcard() {
548 local _target=/etc/udev/rules.d _rule _found
549
550 inst_dir "${udevdir}/rules.d"
551 inst_dir "$_target"
9a52c3fd 552 for _rule in ${udevdir}/rules.d/$1 ${dracutbasedir}/rules.d/$1; do
561eb42f
HH
553 [[ -e $_rule ]] || continue
554 inst_rule_programs "$_rule"
555 inst_rule_group_owner "$_rule"
556 inst_rule_initqueue "$_rule"
557 inst_simple "$_rule"
558 _found=$_rule
559 done
9a52c3fd
HH
560 if [[ -n ${hostonly} ]]; then
561 for _rule in ${_target}/$1; do
561eb42f
HH
562 [[ -f $_rule ]] || continue
563 inst_rule_programs "$_rule"
564 inst_rule_group_owner "$_rule"
565 inst_rule_initqueue "$_rule"
566 inst_simple "$_rule"
567 _found=$_rule
568 done
569 fi
570 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
571}
572
bc1b23c2
HB
573# make sure that library links are correct and up to date
574build_ld_cache() {
575 for f in "$dracutsysrootdir"/etc/ld.so.conf "$dracutsysrootdir"/etc/ld.so.conf.d/*; do
08b63a25 576 [[ -f $f ]] && inst_simple "${f#"$dracutsysrootdir"}"
bc1b23c2
HB
577 done
578 if ! $DRACUT_LDCONFIG -r "$initdir" -f /etc/ld.so.conf; then
579 if [[ $EUID == 0 ]]; then
580 derror "ldconfig exited ungracefully"
581 else
582 derror "ldconfig might need uid=0 (root) for chroot()"
583 fi
584 fi
585}
586
561eb42f 587prepare_udev_rules() {
994d0035
HH
588 if [ -z "$UDEVVERSION" ]; then
589 UDEVVERSION=$(udevadm --version)
590 export UDEVVERSION
591 fi
592
6e761674
MN
593 if [ -z "$UDEVVERSION" ]; then
594 derror "Failed to detect udev version!"
595 return 1
596 fi
718aefda
MN
597 if [ -z "${UDEVVERSION##*[!0-9]*}" ]; then
598 derror "udevadm --version did not report an integer, udev version cannot be determined!"
599 return 1
600 fi
561eb42f
HH
601
602 for f in "$@"; do
603 f="${initdir}/etc/udev/rules.d/$f"
604 [ -e "$f" ] || continue
994d0035 605 while read -r line || [ -n "$line" ]; do
561eb42f 606 if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
9a52c3fd 607 if ((UDEVVERSION >= 174)); then
561eb42f
HH
608 printf '%sIMPORT{builtin}="path_id"\n' "${line%%IMPORT PATH_ID}"
609 else
610 printf '%sIMPORT{program}="path_id %%p"\n' "${line%%IMPORT PATH_ID}"
611 fi
612 elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
9a52c3fd 613 if ((UDEVVERSION >= 176)); then
561eb42f
HH
614 printf '%sIMPORT{builtin}="blkid"\n' "${line%%IMPORT BLKID}"
615 else
994d0035 616 # shellcheck disable=SC2016
561eb42f
HH
617 printf '%sIMPORT{program}="/sbin/blkid -o udev -p $tempnode"\n' "${line%%IMPORT BLKID}"
618 fi
619 else
620 echo "$line"
621 fi
622 done < "${f}" > "${f}.new"
623 mv "${f}.new" "$f"
624 done
625}
626
627# install function specialized for hooks
628# $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
629# All hooks should be POSIX/SuS compliant, they will be sourced by init.
630inst_hook() {
2b5ddc77 631 local hook
561eb42f
HH
632 if ! [[ -f $3 ]]; then
633 dfatal "Cannot install a hook ($3) that does not exist."
634 dfatal "Aborting initrd creation."
635 exit 1
75d758e8 636 elif ! [[ $hookdirs == *$1* ]]; then
561eb42f
HH
637 dfatal "No such hook type $1. Aborting initrd creation."
638 exit 1
639 fi
2b5ddc77
HH
640 hook="/lib/dracut/hooks/${1}/${2}-${3##*/}"
641 inst_simple "$3" "$hook"
642 chmod u+x "$initdir/$hook"
561eb42f
HH
643}
644
645# install any of listed files
646#
647# If first argument is '-d' and second some destination path, first accessible
648# source is installed into this path, otherwise it will installed in the same
649# path as source. If none of listed files was installed, function return 1.
650# On first successful installation it returns with 0 status.
651#
652# Example:
653#
654# inst_any -d /bin/foo /bin/bar /bin/baz
655#
656# Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
657# initramfs.
658inst_any() {
659 local to f
660
75d758e8 661 [[ $1 == '-d' ]] && to="$2" && shift 2
561eb42f
HH
662
663 for f in "$@"; do
664 [[ -e $f ]] || continue
665 [[ $to ]] && inst "$f" "$to" && return 0
666 inst "$f" && return 0
667 done
668
669 return 1
670}
671
cc669250
AAF
672# inst_libdir_dir <dir> [<dir>...]
673# Install a <dir> located on a lib directory to the initramfs image
674inst_libdir_dir() {
675 local -a _dirs
676 for _dir in $libdirs; do
677 for _i in "$@"; do
678 for _d in "$dracutsysrootdir$_dir"/$_i; do
679 [[ -d $_d ]] && _dirs+=("${_d#"$dracutsysrootdir"}")
680 done
681 done
682 done
683 for _dir in "${_dirs[@]}"; do
684 inst_dir "$_dir"
685 done
686}
687
561eb42f
HH
688# inst_libdir_file [-n <pattern>] <file> [<file>...]
689# Install a <file> located on a lib directory to the initramfs image
690# -n <pattern> install matching files
691inst_libdir_file() {
994d0035 692 local -a _files
75d758e8 693 if [[ $1 == "-n" ]]; then
561eb42f
HH
694 local _pattern=$2
695 shift 2
696 for _dir in $libdirs; do
697 for _i in "$@"; do
b955dbf4 698 for _f in "$dracutsysrootdir$_dir"/$_i; do
08b63a25
LG
699 [[ ${_f#"$dracutsysrootdir"} =~ $_pattern ]] || continue
700 [[ -e $_f ]] && _files+=("${_f#"$dracutsysrootdir"}")
561eb42f
HH
701 done
702 done
703 done
704 else
705 for _dir in $libdirs; do
706 for _i in "$@"; do
b955dbf4 707 for _f in "$dracutsysrootdir$_dir"/$_i; do
08b63a25 708 [[ -e $_f ]] && _files+=("${_f#"$dracutsysrootdir"}")
561eb42f
HH
709 done
710 done
711 done
712 fi
994d0035 713 [[ ${#_files[@]} -gt 0 ]] && inst_multiple "${_files[@]}"
561eb42f
HH
714}
715
3e53195b
TI
716# get a command to decompress the given file
717get_decompress_cmd() {
718 case "$1" in
719 *.gz) echo 'gzip -f -d' ;;
720 *.bz2) echo 'bzip2 -d' ;;
721 *.xz) echo 'xz -f -d' ;;
ce9af251 722 *.zst) echo 'zstd -f -d ' ;;
3e53195b
TI
723 esac
724}
561eb42f
HH
725
726# install function decompressing the target and handling symlinks
727# $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
728#
729# Function install targets in the same paths inside overlay but decompressed
730# and without extensions (.gz, .bz2).
731inst_decompress() {
732 local _src _cmd
733
994d0035
HH
734 for _src in "$@"; do
735 _cmd=$(get_decompress_cmd "${_src}")
75d758e8 736 [[ -z ${_cmd} ]] && return 1
994d0035 737 inst_simple "${_src}"
561eb42f
HH
738 # Decompress with chosen tool. We assume that tool changes name e.g.
739 # from 'name.gz' to 'name'.
740 ${_cmd} "${initdir}${_src}"
741 done
742}
743
744# It's similar to above, but if file is not compressed, performs standard
745# install.
746# $@ = list of files
747inst_opt_decompress() {
748 local _src
749
994d0035 750 for _src in "$@"; do
561eb42f
HH
751 inst_decompress "${_src}" || inst "${_src}"
752 done
753}
754
5916d31b 755# module_check <dracut module> [<forced>] [<module path>]
561eb42f
HH
756# execute the check() function of module-setup.sh of <dracut module>
757# or the "check" script, if module-setup.sh is not found
758# "check $hostonly" is called
759module_check() {
5916d31b 760 local _moddir=$3
561eb42f
HH
761 local _ret
762 local _forced=0
763 local _hostonly=$hostonly
5916d31b 764 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
561eb42f
HH
765 [ $# -eq 2 ] && _forced=$2
766 [[ -d $_moddir ]] || return 1
767 if [[ ! -f $_moddir/module-setup.sh ]]; then
768 # if we do not have a check script, we are unconditionally included
769 [[ -x $_moddir/check ]] || return 0
994d0035
HH
770 [[ $_forced != 0 ]] && unset hostonly
771 # don't quote $hostonly to leave argument empty
772 # shellcheck disable=SC2086
773 "$_moddir"/check $hostonly
561eb42f
HH
774 _ret=$?
775 else
776 unset check depends cmdline install installkernel
777 check() { true; }
2b5ddc77 778 # shellcheck disable=SC1090
994d0035 779 . "$_moddir"/module-setup.sh
561eb42f 780 is_func check || return 0
994d0035
HH
781 [[ $_forced != 0 ]] && unset hostonly
782 # don't quote $hostonly to leave argument empty
783 # shellcheck disable=SC2086
784 moddir="$_moddir" check $hostonly
561eb42f
HH
785 _ret=$?
786 unset check depends cmdline install installkernel
787 fi
788 hostonly=$_hostonly
789 return $_ret
790}
791
5916d31b 792# module_check_mount <dracut module> [<module path>]
561eb42f
HH
793# execute the check() function of module-setup.sh of <dracut module>
794# or the "check" script, if module-setup.sh is not found
795# "mount_needs=1 check 0" is called
796module_check_mount() {
5916d31b 797 local _moddir=$2
561eb42f 798 local _ret
994d0035 799 export mount_needs=1
5916d31b 800 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
561eb42f
HH
801 [[ -d $_moddir ]] || return 1
802 if [[ ! -f $_moddir/module-setup.sh ]]; then
803 # if we do not have a check script, we are unconditionally included
804 [[ -x $_moddir/check ]] || return 0
994d0035 805 mount_needs=1 "$_moddir"/check 0
561eb42f
HH
806 _ret=$?
807 else
808 unset check depends cmdline install installkernel
809 check() { false; }
2b5ddc77 810 # shellcheck disable=SC1090
994d0035 811 . "$_moddir"/module-setup.sh
561eb42f
HH
812 moddir=$_moddir check 0
813 _ret=$?
814 unset check depends cmdline install installkernel
815 fi
816 unset mount_needs
994d0035 817 return "$_ret"
561eb42f
HH
818}
819
5916d31b 820# module_depends <dracut module> [<module path>]
561eb42f
HH
821# execute the depends() function of module-setup.sh of <dracut module>
822# or the "depends" script, if module-setup.sh is not found
823module_depends() {
5916d31b 824 local _moddir=$2
561eb42f 825 local _ret
5916d31b 826 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
561eb42f
HH
827 [[ -d $_moddir ]] || return 1
828 if [[ ! -f $_moddir/module-setup.sh ]]; then
829 # if we do not have a check script, we have no deps
830 [[ -x $_moddir/check ]] || return 0
cdb714c5 831 "$_moddir"/check -d
561eb42f
HH
832 return $?
833 else
834 unset check depends cmdline install installkernel
835 depends() { true; }
2b5ddc77 836 # shellcheck disable=SC1090
cdb714c5 837 . "$_moddir"/module-setup.sh
561eb42f
HH
838 moddir=$_moddir depends
839 _ret=$?
840 unset check depends cmdline install installkernel
841 return $_ret
842 fi
843}
844
5916d31b 845# module_cmdline <dracut module> [<module path>]
561eb42f
HH
846# execute the cmdline() function of module-setup.sh of <dracut module>
847# or the "cmdline" script, if module-setup.sh is not found
848module_cmdline() {
5916d31b 849 local _moddir=$2
561eb42f 850 local _ret
5916d31b 851 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
561eb42f
HH
852 [[ -d $_moddir ]] || return 1
853 if [[ ! -f $_moddir/module-setup.sh ]]; then
2b5ddc77 854 # shellcheck disable=SC1090
561eb42f
HH
855 [[ -x $_moddir/cmdline ]] && . "$_moddir/cmdline"
856 return $?
857 else
858 unset check depends cmdline install installkernel
859 cmdline() { true; }
2b5ddc77 860 # shellcheck disable=SC1090
cdb714c5
HH
861 . "$_moddir"/module-setup.sh
862 moddir="$_moddir" cmdline
561eb42f
HH
863 _ret=$?
864 unset check depends cmdline install installkernel
865 return $_ret
866 fi
867}
868
5916d31b 869# module_install <dracut module> [<module path>]
561eb42f
HH
870# execute the install() function of module-setup.sh of <dracut module>
871# or the "install" script, if module-setup.sh is not found
872module_install() {
5916d31b 873 local _moddir=$2
561eb42f 874 local _ret
5916d31b 875 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
561eb42f
HH
876 [[ -d $_moddir ]] || return 1
877 if [[ ! -f $_moddir/module-setup.sh ]]; then
2b5ddc77 878 # shellcheck disable=SC1090
561eb42f
HH
879 [[ -x $_moddir/install ]] && . "$_moddir/install"
880 return $?
881 else
882 unset check depends cmdline install installkernel
883 install() { true; }
2b5ddc77 884 # shellcheck disable=SC1090
cdb714c5
HH
885 . "$_moddir"/module-setup.sh
886 moddir="$_moddir" install
561eb42f
HH
887 _ret=$?
888 unset check depends cmdline install installkernel
889 return $_ret
890 fi
891}
892
5916d31b 893# module_installkernel <dracut module> [<module path>]
561eb42f
HH
894# execute the installkernel() function of module-setup.sh of <dracut module>
895# or the "installkernel" script, if module-setup.sh is not found
896module_installkernel() {
5916d31b 897 local _moddir=$2
561eb42f 898 local _ret
5916d31b 899 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
561eb42f
HH
900 [[ -d $_moddir ]] || return 1
901 if [[ ! -f $_moddir/module-setup.sh ]]; then
2b5ddc77 902 # shellcheck disable=SC1090
561eb42f
HH
903 [[ -x $_moddir/installkernel ]] && . "$_moddir/installkernel"
904 return $?
905 else
906 unset check depends cmdline install installkernel
907 installkernel() { true; }
2b5ddc77 908 # shellcheck disable=SC1090
cdb714c5
HH
909 . "$_moddir"/module-setup.sh
910 moddir="$_moddir" installkernel
561eb42f
HH
911 _ret=$?
912 unset check depends cmdline install installkernel
913 return $_ret
914 fi
915}
916
5916d31b 917# check_mount <dracut module> [<use_as_dep>] [<module path>]
561eb42f
HH
918# check_mount checks, if a dracut module is needed for the given
919# device and filesystem types in "${host_fs_types[@]}"
920check_mount() {
921 local _mod=$1
5916d31b 922 local _moddir=$3
561eb42f
HH
923 local _ret
924 local _moddep
925
5916d31b 926 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
cdb714c5 927 # shellcheck disable=SC2154
561eb42f
HH
928 [ "${#host_fs_types[@]}" -le 0 ] && return 1
929
930 # If we are already scheduled to be loaded, no need to check again.
931 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
932 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
933
934 # This should never happen, but...
935 [[ -d $_moddir ]] || return 1
936
937 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
938
cdb714c5 939 # shellcheck disable=SC2154
561eb42f
HH
940 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
941 return 1
942 fi
943
944 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
9a52c3fd
HH
945 module_check_mount "$_mod" "$_moddir"
946 ret=$?
561eb42f
HH
947
948 # explicit module, so also accept ret=255
75d758e8 949 [[ $ret == 0 || $ret == 255 ]] || return 1
561eb42f
HH
950 else
951 # module not in our list
75d758e8 952 if [[ $dracutmodules == all ]]; then
561eb42f 953 # check, if we can and should install this module
cdb714c5 954 module_check_mount "$_mod" "$_moddir" || return 1
561eb42f
HH
955 else
956 # skip this module
957 return 1
958 fi
959 fi
960
cdb714c5 961 for _moddep in $(module_depends "$_mod" "$_moddir"); do
561eb42f
HH
962 # handle deps as if they were manually added
963 [[ " $dracutmodules " == *\ $_mod\ * ]] \
964 && [[ " $dracutmodules " != *\ $_moddep\ * ]] \
965 && dracutmodules+=" $_moddep "
966 [[ " $add_dracutmodules " == *\ $_mod\ * ]] \
967 && [[ " $add_dracutmodules " != *\ $_moddep\ * ]] \
968 && add_dracutmodules+=" $_moddep "
969 [[ " $force_add_dracutmodules " == *\ $_mod\ * ]] \
970 && [[ " $force_add_dracutmodules " != *\ $_moddep\ * ]] \
971 && force_add_dracutmodules+=" $_moddep "
972 # if a module we depend on fail, fail also
cdb714c5 973 if ! check_module "$_moddep"; then
561eb42f
HH
974 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
975 return 1
976 fi
977 done
978
9a52c3fd
HH
979 [[ " $mods_to_load " == *\ $_mod\ * ]] \
980 || mods_to_load+=" $_mod "
561eb42f
HH
981
982 return 0
983}
984
5916d31b 985# check_module <dracut module> [<use_as_dep>] [<module path>]
561eb42f
HH
986# check if a dracut module is to be used in the initramfs process
987# if <use_as_dep> is set, then the process also keeps track
988# that the modules were checked for the dependency tracking process
989check_module() {
990 local _mod=$1
5916d31b 991 local _moddir=$3
561eb42f
HH
992 local _ret
993 local _moddep
5916d31b
KS
994
995 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
561eb42f
HH
996 # If we are already scheduled to be loaded, no need to check again.
997 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
998 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
999
1000 # This should never happen, but...
1001 [[ -d $_moddir ]] || return 1
1002
1003 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
1004
1005 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
6b8ee4fc 1006 ddebug "dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
561eb42f
HH
1007 return 1
1008 fi
1009
1010 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
1011 if [[ " $dracutmodules $force_add_dracutmodules " == *\ $_mod\ * ]]; then
9a52c3fd
HH
1012 module_check "$_mod" 1 "$_moddir"
1013 ret=$?
561eb42f 1014 else
9a52c3fd
HH
1015 module_check "$_mod" 0 "$_moddir"
1016 ret=$?
561eb42f
HH
1017 fi
1018 # explicit module, so also accept ret=255
75d758e8 1019 [[ $ret == 0 || $ret == 255 ]] || return 1
561eb42f
HH
1020 else
1021 # module not in our list
75d758e8 1022 if [[ $dracutmodules == all ]]; then
561eb42f 1023 # check, if we can and should install this module
9a52c3fd
HH
1024 module_check "$_mod" 0 "$_moddir"
1025 ret=$?
561eb42f
HH
1026 if [[ $ret != 0 ]]; then
1027 [[ $2 ]] && return 1
1028 [[ $ret != 255 ]] && return 1
1029 fi
1030 else
1031 # skip this module
1032 return 1
1033 fi
1034 fi
1035
ad64c686 1036 for _moddep in $(module_depends "$_mod" "$_moddir"); do
561eb42f
HH
1037 # handle deps as if they were manually added
1038 [[ " $dracutmodules " == *\ $_mod\ * ]] \
1039 && [[ " $dracutmodules " != *\ $_moddep\ * ]] \
1040 && dracutmodules+=" $_moddep "
1041 [[ " $add_dracutmodules " == *\ $_mod\ * ]] \
1042 && [[ " $add_dracutmodules " != *\ $_moddep\ * ]] \
1043 && add_dracutmodules+=" $_moddep "
1044 [[ " $force_add_dracutmodules " == *\ $_mod\ * ]] \
1045 && [[ " $force_add_dracutmodules " != *\ $_moddep\ * ]] \
1046 && force_add_dracutmodules+=" $_moddep "
1047 # if a module we depend on fail, fail also
ad64c686 1048 if ! check_module "$_moddep"; then
561eb42f
HH
1049 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
1050 return 1
1051 fi
1052 done
1053
9a52c3fd
HH
1054 [[ " $mods_to_load " == *\ $_mod\ * ]] \
1055 || mods_to_load+=" $_mod "
561eb42f
HH
1056
1057 return 0
1058}
1059
1060# for_each_module_dir <func>
5916d31b 1061# execute "<func> <dracut module> 1 <module path>"
561eb42f
HH
1062for_each_module_dir() {
1063 local _modcheck
1064 local _mod
1065 local _moddir
1066 local _func
a10078a5 1067 local _reason
561eb42f
HH
1068 _func=$1
1069 for _moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
9a52c3fd
HH
1070 [[ -d $_moddir ]] || continue
1071 [[ -e $_moddir/install || -e $_moddir/installkernel || -e \
1072 $_moddir/module-setup.sh ]] || continue
1073 _mod=${_moddir##*/}
1074 _mod=${_mod#[0-9][0-9]}
ad64c686 1075 $_func "$_mod" 1 "$_moddir"
561eb42f
HH
1076 done
1077
1078 # Report any missing dracut modules, the user has specified
1079 _modcheck="$add_dracutmodules $force_add_dracutmodules"
1080 [[ $dracutmodules != all ]] && _modcheck="$_modcheck $dracutmodules"
1081 for _mod in $_modcheck; do
1082 [[ " $mods_to_load " == *\ $_mod\ * ]] && continue
1083
1084 [[ " $force_add_dracutmodules " != *\ $_mod\ * ]] \
1085 && [[ " $dracutmodules " != *\ $_mod\ * ]] \
1086 && [[ " $omit_dracutmodules " == *\ $_mod\ * ]] \
1087 && continue
1088
a10078a5
AAF
1089 [[ -d $(echo "$dracutbasedir/modules.d"/[0-9][0-9]"$_mod") ]] \
1090 && _reason="installed" \
1091 || _reason="found"
1092 derror "dracut module '$_mod' cannot be $_reason."
561eb42f
HH
1093 [[ " $force_add_dracutmodules " == *\ $_mod\ * ]] && exit 1
1094 [[ " $dracutmodules " == *\ $_mod\ * ]] && exit 1
1095 [[ " $add_dracutmodules " == *\ $_mod\ * ]] && exit 1
1096 done
1097}
1098
561eb42f 1099dracut_kernel_post() {
7f633747 1100 for _f in modules.builtin modules.builtin.alias modules.builtin.modinfo modules.order; do
3aa37caf 1101 [[ -e $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f"
561eb42f
HH
1102 done
1103
1104 # generate module dependencies for the initrd
9a52c3fd
HH
1105 if [[ -d $initdir/lib/modules/$kernel ]] \
1106 && ! depmod -a -b "$initdir" "$kernel"; then
561eb42f
HH
1107 dfatal "\"depmod -a $kernel\" failed."
1108 exit 1
1109 fi
1110
561eb42f
HH
1111}
1112
561eb42f 1113instmods() {
794b2d2c
HH
1114 # instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1115 # instmods [-c [-s]] <kernel subsystem>
1116 # install kernel modules along with all their dependencies.
1117 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1118 # -c check
1119 # -s silent
1120 local _optional="-o"
1121 local _silent
1122 local _ret
9bb030c5 1123
75d758e8 1124 [[ $no_kernel == yes ]] && return
9bb030c5 1125
75d758e8 1126 if [[ $1 == '-c' ]]; then
9bb030c5 1127 unset _optional
561eb42f
HH
1128 shift
1129 fi
75d758e8 1130 if [[ $1 == '-s' ]]; then
794b2d2c 1131 _silent=1
561eb42f
HH
1132 shift
1133 fi
9bb030c5 1134
794b2d2c
HH
1135 if (($# == 0)); then
1136 read -r -d '' -a args
1137 set -- "${args[@]}"
1138 fi
9bb030c5 1139
8f773264
HH
1140 if (($# == 0)); then
1141 return 0
1142 fi
1143
097dd367 1144 $DRACUT_INSTALL \
9bb030c5 1145 ${initdir:+-D "$initdir"} \
a0120420 1146 ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
9bb030c5
HH
1147 ${loginstall:+-L "$loginstall"} \
1148 ${hostonly:+-H} \
1149 ${omit_drivers:+-N "$omit_drivers"} \
1150 ${srcmods:+--kerneldir "$srcmods"} \
1151 ${_optional:+-o} \
1152 ${_silent:+--silent} \
1153 -m "$@"
561eb42f 1154 _ret=$?
9bb030c5 1155
75d758e8 1156 if ((_ret != 0)) && [[ -z $_silent ]]; then
9bb030c5 1157 derror "FAILED: " \
ad64c686 1158 "$DRACUT_INSTALL" \
9a52c3fd
HH
1159 ${initdir:+-D "$initdir"} \
1160 ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
1161 ${loginstall:+-L "$loginstall"} \
1162 ${hostonly:+-H} \
1163 ${omit_drivers:+-N "$omit_drivers"} \
1164 ${srcmods:+--kerneldir "$srcmods"} \
1165 ${_optional:+-o} \
1166 ${_silent:+--silent} \
1167 -m "$@"
9bb030c5
HH
1168 fi
1169
b12ee558 1170 [[ "$_optional" ]] && return 0
561eb42f
HH
1171 return $_ret
1172}
19015001
HH
1173
1174if [[ "$(ln --help)" == *--relative* ]]; then
1175 ln_r() {
1176 ln -sfnr "${initdir}/$1" "${initdir}/$2"
1177 }
1178else
1179 ln_r() {
1180 local _source=$1
1181 local _dest=$2
75d758e8 1182 [[ -d ${_dest%/*} ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
19015001
HH
1183 ln -sfn -- "$(convert_abs_rel "${_dest}" "${_source}")" "${initdir}/${_dest}"
1184 }
1185fi
b6b1bf92
DM
1186
1187is_qemu_virtualized() {
1188 # 0 if a virt environment was detected
1189 # 1 if a virt environment could not be detected
1190 # 255 if any error was encountered
9a52c3fd
HH
1191 if type -P systemd-detect-virt > /dev/null 2>&1; then
1192 if ! vm=$(systemd-detect-virt --vm > /dev/null 2>&1); then
ad64c686
HH
1193 return 255
1194 fi
75d758e8
HH
1195 [[ $vm == "qemu" ]] && return 0
1196 [[ $vm == "kvm" ]] && return 0
1197 [[ $vm == "bochs" ]] && return 0
b6b1bf92
DM
1198 fi
1199
1200 for i in /sys/class/dmi/id/*_vendor; do
1201 [[ -f $i ]] || continue
ad64c686 1202 read -r vendor < "$i"
75d758e8
HH
1203 [[ $vendor == "QEMU" ]] && return 0
1204 [[ $vendor == "Red Hat" ]] && return 0
1205 [[ $vendor == "Bochs" ]] && return 0
b6b1bf92
DM
1206 done
1207 return 1
6e761674 1208}