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