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