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