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