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