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