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