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