]> git.ipfire.org Git - thirdparty/dracut.git/blame - dracut-init.sh
dracut_mkdir(): create parent directories as needed.
[thirdparty/dracut.git] / dracut-init.sh
CommitLineData
777f2db0
HH
1#!/bin/bash
2#
561eb42f 3# functions used only by dracut and dracut modules
777f2db0
HH
4#
5# Copyright 2005-2009 Red Hat, Inc. All rights reserved.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19#
20export LC_MESSAGES=C
21
93be2bc6 22if [[ "$EUID" = "0" ]] && ! [[ $DRACUT_NO_XATTR ]]; then
076fcd16
HH
23 export DRACUT_CP="cp --reflink=auto --sparse=auto --preserve=mode,timestamps,xattr,links -dfr"
24else
25 export DRACUT_CP="cp --reflink=auto --sparse=auto --preserve=mode,timestamps,links -dfr"
26fi
7e51a94f 27
561eb42f
HH
28# is_func <command>
29# Check whether $1 is a function.
30is_func() {
31 [[ "$(type -t "$1")" = "function" ]]
32}
33
34if ! [[ $dracutbasedir ]]; then
35 dracutbasedir=${BASH_SOURCE[0]%/*}
36 [[ $dracutbasedir = dracut-functions* ]] && dracutbasedir="."
37 [[ $dracutbasedir ]] || dracutbasedir="."
38 dracutbasedir="$(readlink -f $dracutbasedir)"
39fi
40
41if ! is_func dinfo >/dev/null 2>&1; then
42 . "$dracutbasedir/dracut-logger.sh"
43 dlog_init
44fi
45
46if ! [[ $initdir ]]; then
47 dfatal "initdir not set"
48 exit 1
49fi
50
51if ! [[ -d $initdir ]]; then
52 mkdir -p "$initdir"
53fi
54
561eb42f
HH
55if ! [[ $kernel ]]; then
56 kernel=$(uname -r)
57 export kernel
58fi
59
a0120420 60srcmods="$dracutsysrootdir/lib/modules/$kernel/"
561eb42f
HH
61
62[[ $drivers_dir ]] && {
63 if ! command -v kmod &>/dev/null && vercmp "$(modprobe --version | cut -d' ' -f3)" lt 3.7; then
64 dfatal 'To use --kmoddir option module-init-tools >= 3.7 is required.'
65 exit 1
66 fi
67 srcmods="$drivers_dir"
68}
69export srcmods
70
8e010957
HH
71[[ $DRACUT_FIRMWARE_PATH ]] || export DRACUT_FIRMWARE_PATH="/lib/firmware/updates:/lib/firmware:/lib/firmware/$kernel"
72
561eb42f
HH
73# export standard hookdirs
74[[ $hookdirs ]] || {
75 hookdirs="cmdline pre-udev pre-trigger netroot "
76 hookdirs+="initqueue initqueue/settled initqueue/online initqueue/finished initqueue/timeout "
77 hookdirs+="pre-mount pre-pivot cleanup mount "
78 hookdirs+="emergency shutdown-emergency pre-shutdown shutdown "
79 export hookdirs
80}
81
a0120420
BZ
82DRACUT_LDD=${DRACUT_LDD:-ldd}
83DRACUT_TESTBIN=${DRACUT_TESTBIN:-/bin/sh}
84DRACUT_LDCONFIG=${DRACUT_LDCONFIG:-ldconfig}
85
456f299a
HH
86. $dracutbasedir/dracut-functions.sh
87
561eb42f
HH
88# Detect lib paths
89if ! [[ $libdirs ]] ; then
a0120420
BZ
90 if [[ "$($DRACUT_LDD $dracutsysrootdir$DRACUT_TESTBIN)" == */lib64/* ]] &>/dev/null \
91 && [[ -d $dracutsysrootdir/lib64 ]]; then
561eb42f 92 libdirs+=" /lib64"
a0120420 93 [[ -d $dracutsysrootdir/usr/lib64 ]] && libdirs+=" /usr/lib64"
561eb42f
HH
94 else
95 libdirs+=" /lib"
a0120420 96 [[ -d $dracutsysrootdir/usr/lib ]] && libdirs+=" /usr/lib"
561eb42f
HH
97 fi
98
99 libdirs+=" $(ldconfig_paths)"
100
101 export libdirs
777f2db0
HH
102fi
103
561eb42f
HH
104# helper function for check() in module-setup.sh
105# to check for required installed binaries
106# issues a standardized warning message
107require_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
125require_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
149dracut_need_initqueue() {
150 >"$initdir/lib/dracut/need-initqueue"
151}
152
153dracut_module_included() {
154 [[ " $mods_to_load $modules_loaded " == *\ $*\ * ]]
155}
156
19e57585
KS
157dracut_no_switch_root() {
158 >"$initdir/lib/dracut/no-switch-root"
159}
160
5916d31b
KS
161dracut_module_path() {
162 echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; }
163}
164
561eb42f
HH
165if ! [[ $DRACUT_INSTALL ]]; then
166 DRACUT_INSTALL=$(find_binary dracut-install)
167fi
168
169if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install ]]; then
170 DRACUT_INSTALL=$dracutbasedir/dracut-install
171elif ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/install/dracut-install ]]; then
172 DRACUT_INSTALL=$dracutbasedir/install/dracut-install
173fi
174
a0120420
BZ
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.
181DRINSTALLPARTS=0
182for i in $DRACUT_INSTALL ; do
183 DRINSTALLPARTS=$(($DRINSTALLPARTS+1))
184done
185
186if [[ $DRINSTALLPARTS = 1 ]] && ! [[ -x $DRACUT_INSTALL ]]; then
561eb42f
HH
187 dfatal "dracut-install not found!"
188 exit 10
189fi
190
3ad12c7b 191if [[ $hostonly == "-h" ]]; then
d84f4687 192 if ! [[ $DRACUT_KERNEL_MODALIASES ]] || ! [[ -f "$DRACUT_KERNEL_MODALIASES" ]]; then
3ad12c7b 193 export DRACUT_KERNEL_MODALIASES="${DRACUT_TMPDIR}/modaliases"
a0120420 194 $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${srcmods:+--kerneldir "$srcmods"} --modalias > "$DRACUT_KERNEL_MODALIASES"
3ad12c7b
HH
195 fi
196fi
197
561eb42f
HH
198[[ $DRACUT_RESOLVE_LAZY ]] || export DRACUT_RESOLVE_DEPS=1
199inst_dir() {
200 [[ -e ${initdir}/"$1" ]] && return 0 # already there
a0120420
BZ
201 $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} -d "$@"
202 (($? != 0)) && derror FAILED: $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} -d "$@" || :
561eb42f
HH
203}
204
205inst() {
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
a0120420
BZ
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} "$@" || :
561eb42f
HH
214}
215
216inst_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
a0120420
BZ
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} "$@" || :
561eb42f
HH
226}
227
228inst_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
a0120420
BZ
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} "$@" || :
561eb42f
HH
238}
239
240inst_multiple() {
241 local _ret
a0120420 242 $DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} ${initdir:+-D "$initdir"} -a ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
561eb42f 243 _ret=$?
a0120420 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} "$@" || :
561eb42f
HH
245 return $_ret
246}
247
248dracut_install() {
249 inst_multiple "$@"
250}
251
794b2d2c 252dracut_instmods() {
fa295f0b
HH
253 local _silent=0;
254 local i;
794b2d2c 255 [[ $no_kernel = yes ]] && return
fa295f0b 256 for i in "$@"; do
d13071d7 257 [[ $i == "--silent" ]] && _silent=1
fa295f0b
HH
258 done
259
794b2d2c 260 $DRACUT_INSTALL \
a0120420 261 ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
794b2d2c 262 ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${hostonly:+-H} ${omit_drivers:+-N "$omit_drivers"} ${srcmods:+--kerneldir "$srcmods"} -m "$@"
a0120420 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 "$@" || :
794b2d2c
HH
264}
265
561eb42f
HH
266inst_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
a0120420
BZ
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} "$@" || :
561eb42f
HH
276}
277
278inst_binary() {
a0120420
BZ
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} "$@" || :
561eb42f
HH
281}
282
283inst_script() {
a0120420
BZ
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} "$@" || :
561eb42f
HH
286}
287
1f8a7ae7
MF
288inst_fsck_help() {
289 local _helper="/run/dracut/fsck/fsck_help_$1.txt"
a0120420
BZ
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 || :
1f8a7ae7
MF
292}
293
a695250e
KS
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.
298optional_hostonly() {
299 if [[ $hostonly_mode = "strict" ]]; then
300 printf -- "$hostonly"
301 else
302 printf ""
303 fi
304}
305
561eb42f
HH
306mark_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.)
322rev_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
338inst_rule_programs() {
339 local _prog _bin
340
374ef3ed 341 for _prog in $(sed -nr 's/.*PROGRAM==?"([^ "]+).*/\1/p' "$1"); do
06a1d076
VS
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
374ef3ed 354 for _prog in $(sed -nr 's/.*RUN[+=]=?"([^ "]+).*/\1/p' "$1"); do
06a1d076
VS
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
374ef3ed 367 for _prog in $(sed -nr 's/.*IMPORT\{program\}==?"([^ "]+).*/\1/p' "$1"); do
06a1d076
VS
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
561eb42f
HH
380}
381
382# attempt to install any programs specified in a udev rule
383inst_rule_group_owner() {
384 local i
385
374ef3ed 386 for i in $(sed -nr 's/.*OWNER=?"([^ "]+).*/\1/p' "$1"); do
06a1d076 387 if ! grep -Eq "^$i:" "$initdir/etc/passwd" 2>/dev/null; then
a0120420 388 grep -E "^$i:" $dracutsysrootdir/etc/passwd 2>/dev/null >> "$initdir/etc/passwd"
06a1d076
VS
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
a0120420 393 grep -E "^$i:" $dracutsysrootdir/etc/group 2>/dev/null >> "$initdir/etc/group"
06a1d076
VS
394 fi
395 done
561eb42f
HH
396}
397
398inst_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.
406inst_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
a0120420 413 for r in $dracutsysrootdir${udevdir}/rules.d ${hostonly:+$dracutsysrootdir/etc/udev/rules.d}; do
561eb42f
HH
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
a0120420 422 for r in '' $dracutsysrootdir$dracutbasedir/rules.d/; do
561eb42f
HH
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
436inst_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
462prepare_udev_rules() {
463 [ -z "$UDEVVERSION" ] && export UDEVVERSION=$(udevadm --version)
6e761674
MN
464 if [ -z "$UDEVVERSION" ]; then
465 derror "Failed to detect udev version!"
466 return 1
467 fi
718aefda
MN
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
561eb42f
HH
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.
500inst_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.
525inst_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
543inst_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
a0120420 552 [[ -e "$dracutsysrootdir$_f" ]] && _files+="$_f "
561eb42f
HH
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
a0120420 560 [[ -e "$dracutsysrootdir$_f" ]] && _files+="$_f "
561eb42f
HH
561 done
562 done
563 done
564 fi
565 [[ $_files ]] && inst_multiple $_files
566}
567
3e53195b
TI
568# get a command to decompress the given file
569get_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}
561eb42f
HH
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).
582inst_decompress() {
583 local _src _cmd
584
585 for _src in $@
586 do
3e53195b
TI
587 _cmd=$(get_decompress_cmd ${_src})
588 [[ -z "${_cmd}" ]] && return 1
561eb42f
HH
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
599inst_opt_decompress() {
600 local _src
601
602 for _src in $@; do
603 inst_decompress "${_src}" || inst "${_src}"
604 done
605}
606
5916d31b 607# module_check <dracut module> [<forced>] [<module path>]
561eb42f
HH
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
611module_check() {
5916d31b 612 local _moddir=$3
561eb42f
HH
613 local _ret
614 local _forced=0
615 local _hostonly=$hostonly
5916d31b 616 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
561eb42f
HH
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
5916d31b 639# module_check_mount <dracut module> [<module path>]
561eb42f
HH
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
643module_check_mount() {
5916d31b 644 local _moddir=$2
561eb42f
HH
645 local _ret
646 mount_needs=1
5916d31b 647 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
561eb42f
HH
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
5916d31b 666# module_depends <dracut module> [<module path>]
561eb42f
HH
667# execute the depends() function of module-setup.sh of <dracut module>
668# or the "depends" script, if module-setup.sh is not found
669module_depends() {
5916d31b 670 local _moddir=$2
561eb42f 671 local _ret
5916d31b 672 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
561eb42f
HH
673 [[ -d $_moddir ]] || return 1
674 if [[ ! -f $_moddir/module-setup.sh ]]; then
675 # 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
5916d31b 690# module_cmdline <dracut module> [<module path>]
561eb42f
HH
691# execute the cmdline() function of module-setup.sh of <dracut module>
692# or the "cmdline" script, if module-setup.sh is not found
693module_cmdline() {
5916d31b 694 local _moddir=$2
561eb42f 695 local _ret
5916d31b 696 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
561eb42f
HH
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
5916d31b 712# module_install <dracut module> [<module path>]
561eb42f
HH
713# execute the install() function of module-setup.sh of <dracut module>
714# or the "install" script, if module-setup.sh is not found
715module_install() {
5916d31b 716 local _moddir=$2
561eb42f 717 local _ret
5916d31b 718 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
561eb42f
HH
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
5916d31b 734# module_installkernel <dracut module> [<module path>]
561eb42f
HH
735# execute the installkernel() function of module-setup.sh of <dracut module>
736# or the "installkernel" script, if module-setup.sh is not found
737module_installkernel() {
5916d31b 738 local _moddir=$2
561eb42f 739 local _ret
5916d31b 740 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
561eb42f
HH
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
5916d31b 756# check_mount <dracut module> [<use_as_dep>] [<module path>]
561eb42f
HH
757# check_mount checks, if a dracut module is needed for the given
758# device and filesystem types in "${host_fs_types[@]}"
759check_mount() {
760 local _mod=$1
5916d31b 761 local _moddir=$3
561eb42f
HH
762 local _ret
763 local _moddep
764
5916d31b 765 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
561eb42f
HH
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
5916d31b 782 module_check_mount $_mod $_moddir; ret=$?
561eb42f
HH
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
5916d31b 790 module_check_mount $_mod $_moddir || return 1
561eb42f
HH
791 else
792 # skip this module
793 return 1
794 fi
795 fi
796
5916d31b 797 for _moddep in $(module_depends $_mod $_moddir); do
561eb42f
HH
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
5916d31b 821# check_module <dracut module> [<use_as_dep>] [<module path>]
561eb42f
HH
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
825check_module() {
826 local _mod=$1
5916d31b 827 local _moddir=$3
561eb42f
HH
828 local _ret
829 local _moddep
5916d31b
KS
830
831 [[ -z $_moddir ]] && _moddir=$(dracut_module_path "$1")
561eb42f
HH
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
5916d31b 848 module_check $_mod 1 $_moddir; ret=$?
561eb42f 849 else
5916d31b 850 module_check $_mod 0 $_moddir; ret=$?
561eb42f
HH
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
5916d31b 858 module_check $_mod 0 $_moddir; ret=$?
561eb42f
HH
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
5916d31b 869 for _moddep in $(module_depends $_mod $_moddir); do
561eb42f
HH
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>
5916d31b 894# execute "<func> <dracut module> 1 <module path>"
561eb42f
HH
895for_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]}
5916d31b 906 $_func $_mod 1 $_moddir
561eb42f
HH
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
561eb42f 927dracut_kernel_post() {
561eb42f 928 for _f in modules.builtin.bin modules.builtin modules.order; do
3aa37caf 929 [[ -e $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f"
561eb42f
HH
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
561eb42f
HH
939}
940
561eb42f 941instmods() {
794b2d2c
HH
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
9bb030c5 951
561eb42f 952 [[ $no_kernel = yes ]] && return
9bb030c5 953
561eb42f 954 if [[ $1 = '-c' ]]; then
9bb030c5 955 unset _optional
561eb42f
HH
956 shift
957 fi
561eb42f 958 if [[ $1 = '-s' ]]; then
794b2d2c 959 _silent=1
561eb42f
HH
960 shift
961 fi
9bb030c5 962
794b2d2c
HH
963 if (($# == 0)); then
964 read -r -d '' -a args
965 set -- "${args[@]}"
966 fi
9bb030c5 967
8f773264
HH
968 if (($# == 0)); then
969 return 0
970 fi
971
9bb030c5
HH
972 $DRACUT_INSTALL \
973 ${initdir:+-D "$initdir"} \
a0120420 974 ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
9bb030c5
HH
975 ${loginstall:+-L "$loginstall"} \
976 ${hostonly:+-H} \
977 ${omit_drivers:+-N "$omit_drivers"} \
978 ${srcmods:+--kerneldir "$srcmods"} \
979 ${_optional:+-o} \
980 ${_silent:+--silent} \
981 -m "$@"
561eb42f 982 _ret=$?
9bb030c5
HH
983
984 if (($_ret != 0)) && [[ -z "$_silent" ]]; then
985 derror "FAILED: " \
986 $DRACUT_INSTALL \
987 ${initdir:+-D "$initdir"} \
a0120420 988 ${dracutsysrootdir:+-r "$dracutsysrootdir"} \
9bb030c5
HH
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
794b2d2c 998 [[ "$optional" ]] && return 0
561eb42f
HH
999 return $_ret
1000}
19015001
HH
1001
1002if [[ "$(ln --help)" == *--relative* ]]; then
1003 ln_r() {
1004 ln -sfnr "${initdir}/$1" "${initdir}/$2"
1005 }
1006else
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 }
1013fi
b6b1bf92
DM
1014
1015is_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
6e761674 1035}