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