]> git.ipfire.org Git - thirdparty/dracut.git/blob - dracut-init.sh
dracut_mkdir(): create parent directories as needed.
[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 . "$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 if [ -z "${UDEVVERSION##*[!0-9]*}" ]; then
469 derror "udevadm --version did not report an integer, udev version cannot be determined!"
470 return 1
471 fi
472
473 for f in "$@"; do
474 f="${initdir}/etc/udev/rules.d/$f"
475 [ -e "$f" ] || continue
476 while read line || [ -n "$line" ]; do
477 if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
478 if [ $UDEVVERSION -ge 174 ]; then
479 printf '%sIMPORT{builtin}="path_id"\n' "${line%%IMPORT PATH_ID}"
480 else
481 printf '%sIMPORT{program}="path_id %%p"\n' "${line%%IMPORT PATH_ID}"
482 fi
483 elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
484 if [ $UDEVVERSION -ge 176 ]; then
485 printf '%sIMPORT{builtin}="blkid"\n' "${line%%IMPORT BLKID}"
486 else
487 printf '%sIMPORT{program}="/sbin/blkid -o udev -p $tempnode"\n' "${line%%IMPORT BLKID}"
488 fi
489 else
490 echo "$line"
491 fi
492 done < "${f}" > "${f}.new"
493 mv "${f}.new" "$f"
494 done
495 }
496
497 # install function specialized for hooks
498 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
499 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
500 inst_hook() {
501 if ! [[ -f $3 ]]; then
502 dfatal "Cannot install a hook ($3) that does not exist."
503 dfatal "Aborting initrd creation."
504 exit 1
505 elif ! [[ "$hookdirs" == *$1* ]]; then
506 dfatal "No such hook type $1. Aborting initrd creation."
507 exit 1
508 fi
509 inst_simple "$3" "/lib/dracut/hooks/${1}/${2}-${3##*/}"
510 }
511
512 # install any of listed files
513 #
514 # If first argument is '-d' and second some destination path, first accessible
515 # source is installed into this path, otherwise it will installed in the same
516 # path as source. If none of listed files was installed, function return 1.
517 # On first successful installation it returns with 0 status.
518 #
519 # Example:
520 #
521 # inst_any -d /bin/foo /bin/bar /bin/baz
522 #
523 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
524 # initramfs.
525 inst_any() {
526 local to f
527
528 [[ $1 = '-d' ]] && to="$2" && shift 2
529
530 for f in "$@"; do
531 [[ -e $f ]] || continue
532 [[ $to ]] && inst "$f" "$to" && return 0
533 inst "$f" && return 0
534 done
535
536 return 1
537 }
538
539
540 # inst_libdir_file [-n <pattern>] <file> [<file>...]
541 # Install a <file> located on a lib directory to the initramfs image
542 # -n <pattern> install matching files
543 inst_libdir_file() {
544 local _files
545 if [[ "$1" == "-n" ]]; then
546 local _pattern=$2
547 shift 2
548 for _dir in $libdirs; do
549 for _i in "$@"; do
550 for _f in "$_dir"/$_i; do
551 [[ "$_f" =~ $_pattern ]] || continue
552 [[ -e "$dracutsysrootdir$_f" ]] && _files+="$_f "
553 done
554 done
555 done
556 else
557 for _dir in $libdirs; do
558 for _i in "$@"; do
559 for _f in "$_dir"/$_i; do
560 [[ -e "$dracutsysrootdir$_f" ]] && _files+="$_f "
561 done
562 done
563 done
564 fi
565 [[ $_files ]] && inst_multiple $_files
566 }
567
568 # get a command to decompress the given file
569 get_decompress_cmd() {
570 case "$1" in
571 *.gz) echo 'gzip -f -d' ;;
572 *.bz2) echo 'bzip2 -d' ;;
573 *.xz) echo 'xz -f -d' ;;
574 esac
575 }
576
577 # install function decompressing the target and handling symlinks
578 # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
579 #
580 # Function install targets in the same paths inside overlay but decompressed
581 # and without extensions (.gz, .bz2).
582 inst_decompress() {
583 local _src _cmd
584
585 for _src in $@
586 do
587 _cmd=$(get_decompress_cmd ${_src})
588 [[ -z "${_cmd}" ]] && return 1
589 inst_simple ${_src}
590 # Decompress with chosen tool. We assume that tool changes name e.g.
591 # from 'name.gz' to 'name'.
592 ${_cmd} "${initdir}${_src}"
593 done
594 }
595
596 # It's similar to above, but if file is not compressed, performs standard
597 # install.
598 # $@ = list of files
599 inst_opt_decompress() {
600 local _src
601
602 for _src in $@; do
603 inst_decompress "${_src}" || inst "${_src}"
604 done
605 }
606
607 # module_check <dracut module> [<forced>] [<module path>]
608 # execute the check() function of module-setup.sh of <dracut module>
609 # or the "check" script, if module-setup.sh is not found
610 # "check $hostonly" is called
611 module_check() {
612 local _moddir=$3
613 local _ret
614 local _forced=0
615 local _hostonly=$hostonly
616 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
617 [ $# -eq 2 ] && _forced=$2
618 [[ -d $_moddir ]] || return 1
619 if [[ ! -f $_moddir/module-setup.sh ]]; then
620 # if we do not have a check script, we are unconditionally included
621 [[ -x $_moddir/check ]] || return 0
622 [ $_forced -ne 0 ] && unset hostonly
623 $_moddir/check $hostonly
624 _ret=$?
625 else
626 unset check depends cmdline install installkernel
627 check() { true; }
628 . $_moddir/module-setup.sh
629 is_func check || return 0
630 [ $_forced -ne 0 ] && unset hostonly
631 moddir=$_moddir check $hostonly
632 _ret=$?
633 unset check depends cmdline install installkernel
634 fi
635 hostonly=$_hostonly
636 return $_ret
637 }
638
639 # module_check_mount <dracut module> [<module path>]
640 # execute the check() function of module-setup.sh of <dracut module>
641 # or the "check" script, if module-setup.sh is not found
642 # "mount_needs=1 check 0" is called
643 module_check_mount() {
644 local _moddir=$2
645 local _ret
646 mount_needs=1
647 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
648 [[ -d $_moddir ]] || return 1
649 if [[ ! -f $_moddir/module-setup.sh ]]; then
650 # if we do not have a check script, we are unconditionally included
651 [[ -x $_moddir/check ]] || return 0
652 mount_needs=1 $_moddir/check 0
653 _ret=$?
654 else
655 unset check depends cmdline install installkernel
656 check() { false; }
657 . $_moddir/module-setup.sh
658 moddir=$_moddir check 0
659 _ret=$?
660 unset check depends cmdline install installkernel
661 fi
662 unset mount_needs
663 return $_ret
664 }
665
666 # module_depends <dracut module> [<module path>]
667 # execute the depends() function of module-setup.sh of <dracut module>
668 # or the "depends" script, if module-setup.sh is not found
669 module_depends() {
670 local _moddir=$2
671 local _ret
672 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
673 [[ -d $_moddir ]] || return 1
674 if [[ ! -f $_moddir/module-setup.sh ]]; then
675 # if we do not have a check script, we have no deps
676 [[ -x $_moddir/check ]] || return 0
677 $_moddir/check -d
678 return $?
679 else
680 unset check depends cmdline install installkernel
681 depends() { true; }
682 . $_moddir/module-setup.sh
683 moddir=$_moddir depends
684 _ret=$?
685 unset check depends cmdline install installkernel
686 return $_ret
687 fi
688 }
689
690 # module_cmdline <dracut module> [<module path>]
691 # execute the cmdline() function of module-setup.sh of <dracut module>
692 # or the "cmdline" script, if module-setup.sh is not found
693 module_cmdline() {
694 local _moddir=$2
695 local _ret
696 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
697 [[ -d $_moddir ]] || return 1
698 if [[ ! -f $_moddir/module-setup.sh ]]; then
699 [[ -x $_moddir/cmdline ]] && . "$_moddir/cmdline"
700 return $?
701 else
702 unset check depends cmdline install installkernel
703 cmdline() { true; }
704 . $_moddir/module-setup.sh
705 moddir=$_moddir cmdline
706 _ret=$?
707 unset check depends cmdline install installkernel
708 return $_ret
709 fi
710 }
711
712 # module_install <dracut module> [<module path>]
713 # execute the install() function of module-setup.sh of <dracut module>
714 # or the "install" script, if module-setup.sh is not found
715 module_install() {
716 local _moddir=$2
717 local _ret
718 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
719 [[ -d $_moddir ]] || return 1
720 if [[ ! -f $_moddir/module-setup.sh ]]; then
721 [[ -x $_moddir/install ]] && . "$_moddir/install"
722 return $?
723 else
724 unset check depends cmdline install installkernel
725 install() { true; }
726 . $_moddir/module-setup.sh
727 moddir=$_moddir install
728 _ret=$?
729 unset check depends cmdline install installkernel
730 return $_ret
731 fi
732 }
733
734 # module_installkernel <dracut module> [<module path>]
735 # execute the installkernel() function of module-setup.sh of <dracut module>
736 # or the "installkernel" script, if module-setup.sh is not found
737 module_installkernel() {
738 local _moddir=$2
739 local _ret
740 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
741 [[ -d $_moddir ]] || return 1
742 if [[ ! -f $_moddir/module-setup.sh ]]; then
743 [[ -x $_moddir/installkernel ]] && . "$_moddir/installkernel"
744 return $?
745 else
746 unset check depends cmdline install installkernel
747 installkernel() { true; }
748 . $_moddir/module-setup.sh
749 moddir=$_moddir installkernel
750 _ret=$?
751 unset check depends cmdline install installkernel
752 return $_ret
753 fi
754 }
755
756 # check_mount <dracut module> [<use_as_dep>] [<module path>]
757 # check_mount checks, if a dracut module is needed for the given
758 # device and filesystem types in "${host_fs_types[@]}"
759 check_mount() {
760 local _mod=$1
761 local _moddir=$3
762 local _ret
763 local _moddep
764
765 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
766 [ "${#host_fs_types[@]}" -le 0 ] && return 1
767
768 # If we are already scheduled to be loaded, no need to check again.
769 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
770 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
771
772 # This should never happen, but...
773 [[ -d $_moddir ]] || return 1
774
775 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
776
777 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
778 return 1
779 fi
780
781 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
782 module_check_mount $_mod $_moddir; ret=$?
783
784 # explicit module, so also accept ret=255
785 [[ $ret = 0 || $ret = 255 ]] || return 1
786 else
787 # module not in our list
788 if [[ $dracutmodules = all ]]; then
789 # check, if we can and should install this module
790 module_check_mount $_mod $_moddir || return 1
791 else
792 # skip this module
793 return 1
794 fi
795 fi
796
797 for _moddep in $(module_depends $_mod $_moddir); do
798 # handle deps as if they were manually added
799 [[ " $dracutmodules " == *\ $_mod\ * ]] \
800 && [[ " $dracutmodules " != *\ $_moddep\ * ]] \
801 && dracutmodules+=" $_moddep "
802 [[ " $add_dracutmodules " == *\ $_mod\ * ]] \
803 && [[ " $add_dracutmodules " != *\ $_moddep\ * ]] \
804 && add_dracutmodules+=" $_moddep "
805 [[ " $force_add_dracutmodules " == *\ $_mod\ * ]] \
806 && [[ " $force_add_dracutmodules " != *\ $_moddep\ * ]] \
807 && force_add_dracutmodules+=" $_moddep "
808 # if a module we depend on fail, fail also
809 if ! check_module $_moddep; then
810 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
811 return 1
812 fi
813 done
814
815 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
816 mods_to_load+=" $_mod "
817
818 return 0
819 }
820
821 # check_module <dracut module> [<use_as_dep>] [<module path>]
822 # check if a dracut module is to be used in the initramfs process
823 # if <use_as_dep> is set, then the process also keeps track
824 # that the modules were checked for the dependency tracking process
825 check_module() {
826 local _mod=$1
827 local _moddir=$3
828 local _ret
829 local _moddep
830
831 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
832 # If we are already scheduled to be loaded, no need to check again.
833 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
834 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
835
836 # This should never happen, but...
837 [[ -d $_moddir ]] || return 1
838
839 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
840
841 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
842 dinfo "dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
843 return 1
844 fi
845
846 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
847 if [[ " $dracutmodules $force_add_dracutmodules " == *\ $_mod\ * ]]; then
848 module_check $_mod 1 $_moddir; ret=$?
849 else
850 module_check $_mod 0 $_moddir; ret=$?
851 fi
852 # explicit module, so also accept ret=255
853 [[ $ret = 0 || $ret = 255 ]] || return 1
854 else
855 # module not in our list
856 if [[ $dracutmodules = all ]]; then
857 # check, if we can and should install this module
858 module_check $_mod 0 $_moddir; ret=$?
859 if [[ $ret != 0 ]]; then
860 [[ $2 ]] && return 1
861 [[ $ret != 255 ]] && return 1
862 fi
863 else
864 # skip this module
865 return 1
866 fi
867 fi
868
869 for _moddep in $(module_depends $_mod $_moddir); do
870 # handle deps as if they were manually added
871 [[ " $dracutmodules " == *\ $_mod\ * ]] \
872 && [[ " $dracutmodules " != *\ $_moddep\ * ]] \
873 && dracutmodules+=" $_moddep "
874 [[ " $add_dracutmodules " == *\ $_mod\ * ]] \
875 && [[ " $add_dracutmodules " != *\ $_moddep\ * ]] \
876 && add_dracutmodules+=" $_moddep "
877 [[ " $force_add_dracutmodules " == *\ $_mod\ * ]] \
878 && [[ " $force_add_dracutmodules " != *\ $_moddep\ * ]] \
879 && force_add_dracutmodules+=" $_moddep "
880 # if a module we depend on fail, fail also
881 if ! check_module $_moddep; then
882 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
883 return 1
884 fi
885 done
886
887 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
888 mods_to_load+=" $_mod "
889
890 return 0
891 }
892
893 # for_each_module_dir <func>
894 # execute "<func> <dracut module> 1 <module path>"
895 for_each_module_dir() {
896 local _modcheck
897 local _mod
898 local _moddir
899 local _func
900 _func=$1
901 for _moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
902 [[ -d $_moddir ]] || continue;
903 [[ -e $_moddir/install || -e $_moddir/installkernel || \
904 -e $_moddir/module-setup.sh ]] || continue
905 _mod=${_moddir##*/}; _mod=${_mod#[0-9][0-9]}
906 $_func $_mod 1 $_moddir
907 done
908
909 # Report any missing dracut modules, the user has specified
910 _modcheck="$add_dracutmodules $force_add_dracutmodules"
911 [[ $dracutmodules != all ]] && _modcheck="$_modcheck $dracutmodules"
912 for _mod in $_modcheck; do
913 [[ " $mods_to_load " == *\ $_mod\ * ]] && continue
914
915 [[ " $force_add_dracutmodules " != *\ $_mod\ * ]] \
916 && [[ " $dracutmodules " != *\ $_mod\ * ]] \
917 && [[ " $omit_dracutmodules " == *\ $_mod\ * ]] \
918 && continue
919
920 derror "dracut module '$_mod' cannot be found or installed."
921 [[ " $force_add_dracutmodules " == *\ $_mod\ * ]] && exit 1
922 [[ " $dracutmodules " == *\ $_mod\ * ]] && exit 1
923 [[ " $add_dracutmodules " == *\ $_mod\ * ]] && exit 1
924 done
925 }
926
927 dracut_kernel_post() {
928 for _f in modules.builtin.bin modules.builtin modules.order; do
929 [[ -e $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f"
930 done
931
932 # generate module dependencies for the initrd
933 if [[ -d $initdir/lib/modules/$kernel ]] && \
934 ! depmod -a -b "$initdir" $kernel; then
935 dfatal "\"depmod -a $kernel\" failed."
936 exit 1
937 fi
938
939 }
940
941 instmods() {
942 # instmods [-c [-s]] <kernel module> [<kernel module> ... ]
943 # instmods [-c [-s]] <kernel subsystem>
944 # install kernel modules along with all their dependencies.
945 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
946 # -c check
947 # -s silent
948 local _optional="-o"
949 local _silent
950 local _ret
951
952 [[ $no_kernel = yes ]] && return
953
954 if [[ $1 = '-c' ]]; then
955 unset _optional
956 shift
957 fi
958 if [[ $1 = '-s' ]]; then
959 _silent=1
960 shift
961 fi
962
963 if (($# == 0)); then
964 read -r -d '' -a args
965 set -- "${args[@]}"
966 fi
967
968 if (($# == 0)); then
969 return 0
970 fi
971
972 $DRACUT_INSTALL \
973 ${initdir:+-D "$initdir"} \
974 ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
975 ${loginstall:+-L "$loginstall"} \
976 ${hostonly:+-H} \
977 ${omit_drivers:+-N "$omit_drivers"} \
978 ${srcmods:+--kerneldir "$srcmods"} \
979 ${_optional:+-o} \
980 ${_silent:+--silent} \
981 -m "$@"
982 _ret=$?
983
984 if (($_ret != 0)) && [[ -z "$_silent" ]]; then
985 derror "FAILED: " \
986 $DRACUT_INSTALL \
987 ${initdir:+-D "$initdir"} \
988 ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
989 ${loginstall:+-L "$loginstall"} \
990 ${hostonly:+-H} \
991 ${omit_drivers:+-N "$omit_drivers"} \
992 ${srcmods:+--kerneldir "$srcmods"} \
993 ${_optional:+-o} \
994 ${_silent:+--silent} \
995 -m "$@"
996 fi
997
998 [[ "$optional" ]] && return 0
999 return $_ret
1000 }
1001
1002 if [[ "$(ln --help)" == *--relative* ]]; then
1003 ln_r() {
1004 ln -sfnr "${initdir}/$1" "${initdir}/$2"
1005 }
1006 else
1007 ln_r() {
1008 local _source=$1
1009 local _dest=$2
1010 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
1011 ln -sfn -- "$(convert_abs_rel "${_dest}" "${_source}")" "${initdir}/${_dest}"
1012 }
1013 fi
1014
1015 is_qemu_virtualized() {
1016 # 0 if a virt environment was detected
1017 # 1 if a virt environment could not be detected
1018 # 255 if any error was encountered
1019 if type -P systemd-detect-virt >/dev/null 2>&1; then
1020 vm=$(systemd-detect-virt --vm >/dev/null 2>&1)
1021 (($? != 0)) && return 255
1022 [[ $vm = "qemu" ]] && return 0
1023 [[ $vm = "kvm" ]] && return 0
1024 [[ $vm = "bochs" ]] && return 0
1025 fi
1026
1027 for i in /sys/class/dmi/id/*_vendor; do
1028 [[ -f $i ]] || continue
1029 read vendor < $i
1030 [[ "$vendor" == "QEMU" ]] && return 0
1031 [[ "$vendor" == "Red Hat" ]] && return 0
1032 [[ "$vendor" == "Bochs" ]] && return 0
1033 done
1034 return 1
1035 }