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