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