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