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