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