]> git.ipfire.org Git - thirdparty/dracut.git/blame - dracut-functions.sh
add parameter --print-cmdline
[thirdparty/dracut.git] / dracut-functions.sh
CommitLineData
04b56f3a 1#!/bin/bash
cc02093d
HH
2# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3# ex: ts=8 sw=4 sts=4 et filetype=sh
04b56f3a 4#
33ee031c 5# functions used by dracut and other tools.
04b56f3a 6#
33ee031c 7# Copyright 2005-2009 Red Hat, Inc. All rights reserved.
04b56f3a
JK
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
0f9c78c1 20# along with this program. If not, see <http://www.gnu.org/licenses/>.
04b56f3a 21#
258de828 22export LC_MESSAGES=C
f4031e8a
HH
23
24if [[ $DRACUT_KERNEL_LAZY ]] && ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
25 if ! [[ -d "$initdir/.kernelmodseen" ]]; then
26 mkdir -p "$initdir/.kernelmodseen"
27 fi
28 DRACUT_KERNEL_LAZY_HASHDIR="$initdir/.kernelmodseen"
29fi
ec1aa3bc 30
eaa924b6
HH
31if [[ $initdir ]] && ! [[ -d $initdir ]]; then
32 mkdir -p "$initdir"
33fi
34
8d95b8b3 35# Generic substring function. If $2 is in $1, return 0.
520047c3 36strstr() { [[ $1 = *$2* ]]; }
8d95b8b3 37
f4031e8a
HH
38# find a binary. If we were not passed the full path directly,
39# search in the usual places to find the binary.
40find_binary() {
41 if [[ -z ${1##/*} ]]; then
b093aa2d
HH
42 if [[ -x $1 ]] || { [[ "$1" == *.so* ]] && ldd "$1" &>/dev/null; }; then
43 printf "%s\n" "$1"
f4031e8a
HH
44 return 0
45 fi
46 fi
47
b093aa2d 48 type -P "${1##*/}"
f4031e8a
HH
49}
50
0874654c
HH
51if ! [[ $dracutbasedir ]]; then
52 dracutbasedir=${BASH_SOURCE[0]%/*}
53 [[ $dracutbasedir = "dracut-functions" ]] && dracutbasedir="."
54 [[ $dracutbasedir ]] || dracutbasedir="."
55 dracutbasedir="$(readlink -f $dracutbasedir)"
56fi
57
4fe1bdd4
HH
58ldconfig_paths()
59{
60 local a i
61 declare -A a
62 for i in $(
63 ldconfig -pN 2>/dev/null | while read a b c d; do
64 [[ "$c" != "=>" ]] && continue
466a5998 65 printf "%s\n" ${d%/*};
4fe1bdd4
HH
66 done
67 ); do
68 a["$i"]=1;
69 done;
70 printf "%s\n" ${!a[@]}
71}
72
8d95b8b3
HH
73# Detect lib paths
74if ! [[ $libdirs ]] ; then
b093aa2d 75 if [[ "$(ldd /bin/sh)" == */lib64/* ]] &>/dev/null \
8d95b8b3
HH
76 && [[ -d /lib64 ]]; then
77 libdirs+=" /lib64"
78 [[ -d /usr/lib64 ]] && libdirs+=" /usr/lib64"
79 else
80 libdirs+=" /lib"
81 [[ -d /usr/lib ]] && libdirs+=" /usr/lib"
82 fi
4fe1bdd4
HH
83
84 libdirs+="$(ldconfig_paths)"
85
8d95b8b3
HH
86 export libdirs
87fi
88
89if ! [[ $kernel ]]; then
90 kernel=$(uname -r)
91 export kernel
92fi
93
f06c2b58
HH
94# Version comparision function. Assumes Linux style version scheme.
95# $1 = version a
96# $2 = comparision op (gt, ge, eq, le, lt, ne)
97# $3 = version b
98vercmp() {
99 local _n1=(${1//./ }) _op=$2 _n2=(${3//./ }) _i _res
100
101 for ((_i=0; ; _i++))
102 do
103 if [[ ! ${_n1[_i]}${_n2[_i]} ]]; then _res=0
104 elif ((${_n1[_i]:-0} > ${_n2[_i]:-0})); then _res=1
105 elif ((${_n1[_i]:-0} < ${_n2[_i]:-0})); then _res=2
106 else continue
107 fi
108 break
109 done
110
111 case $_op in
112 gt) ((_res == 1));;
113 ge) ((_res != 2));;
114 eq) ((_res == 0));;
115 le) ((_res != 1));;
116 lt) ((_res == 2));;
117 ne) ((_res != 0));;
118 esac
119}
120
8d95b8b3 121srcmods="/lib/modules/$kernel/"
f06c2b58 122
8d95b8b3 123[[ $drivers_dir ]] && {
f06c2b58 124 if ! command -v kmod &>/dev/null && vercmp "$(modprobe --version | cut -d' ' -f3)" lt 3.7; then
8d95b8b3
HH
125 dfatal 'To use --kmoddir option module-init-tools >= 3.7 is required.'
126 exit 1
127 fi
128 srcmods="$drivers_dir"
129}
130export srcmods
131
d6d53f60 132if ! type dinfo >/dev/null 2>&1; then
552ecca6 133 . "$dracutbasedir/dracut-logger.sh"
e103615b
134 dlog_init
135fi
136
593b315c
HH
137if ! [[ $initdir ]]; then
138 dfatal "initdir not set"
139 exit 1
140fi
141
7e2bca48
HH
142# export standard hookdirs
143[[ $hookdirs ]] || {
4fed3ddf 144 hookdirs="cmdline pre-udev pre-trigger netroot "
6e3cc00f 145 hookdirs+="initqueue initqueue/settled initqueue/online initqueue/finished initqueue/timeout "
eef7649e 146 hookdirs+="pre-mount pre-pivot cleanup mount "
2b6dd6a1 147 hookdirs+="emergency shutdown-emergency pre-shutdown shutdown "
7e2bca48
HH
148 export hookdirs
149}
150
8d85429a
HH
151dracut_need_initqueue() {
152 >"$initdir/lib/dracut/need-initqueue"
153}
154
155dracut_module_included() {
b093aa2d 156 [[ "$mods_to_load $modules_loaded" == *$@* ]]
8d85429a
HH
157}
158
87122afc
159# Create all subdirectories for given path without creating the last element.
160# $1 = path
b093aa2d
HH
161mksubdirs() {
162 [[ -e ${1%/*} ]] || mkdir -m 0755 -p -- "${1%/*}"
163}
87122afc 164
7e2bca48
HH
165# is_func <command>
166# Check whether $1 is a function.
95d2dabc 167is_func() {
b093aa2d 168 [[ "$(type -t "$1")" = "function" ]]
95d2dabc
HH
169}
170
87122afc
171# Function prints global variables in format name=value line by line.
172# $@ = list of global variables' name
173print_vars() {
29b10e65 174 local _var _value
87122afc 175
b093aa2d 176 for _var in "$@"
87122afc 177 do
b093aa2d
HH
178 eval printf -v _value "%s" "\$$_var"
179 [[ ${_value} ]] && printf '%s="%s"\n' "$_var" "$_value"
87122afc
180 done
181}
182
7e2bca48
HH
183# normalize_path <path>
184# Prints the normalized path, where it removes any duplicated
185# and trailing slashes.
186# Example:
187# $ normalize_path ///test/test//
188# /test/test
626d9eba 189normalize_path() {
c44e3cb4
MS
190 shopt -q -s extglob
191 set -- "${1//+(\/)//}"
192 shopt -q -u extglob
466a5998 193 printf "%s\n" "${1%/}"
626d9eba 194}
d4bb4316 195
7e2bca48
HH
196# convert_abs_rel <from> <to>
197# Prints the relative path, when creating a symlink to <to> from <from>.
198# Example:
199# $ convert_abs_rel /usr/bin/test /bin/test-2
200# ../../bin/test-2
201# $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
d4bb4316 202convert_abs_rel() {
6d2a7942 203 local __current __absolute __abssize __cursize __newpath
c44e3cb4 204 local -i __i __level
d4bb4316 205
c44e3cb4
MS
206 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
207
c1609dd4 208 # corner case #1 - self looping link
466a5998 209 [[ "$1" == "$2" ]] && { printf "%s\n" "${1##*/}"; return; }
c1609dd4
MS
210
211 # corner case #2 - own dir link
466a5998 212 [[ "${1%/*}" == "$2" ]] && { printf ".\n"; return; }
c1609dd4 213
6d2a7942
HH
214 IFS="/" __current=($1)
215 IFS="/" __absolute=($2)
d4bb4316
HH
216
217 __abssize=${#__absolute[@]}
218 __cursize=${#__current[@]}
219
b093aa2d 220 while [[ "${__absolute[__level]}" == "${__current[__level]}" ]]
d4bb4316
HH
221 do
222 (( __level++ ))
223 if (( __level > __abssize || __level > __cursize ))
224 then
225 break
226 fi
227 done
228
229 for ((__i = __level; __i < __cursize-1; __i++))
230 do
231 if ((__i > __level))
232 then
233 __newpath=$__newpath"/"
234 fi
235 __newpath=$__newpath".."
236 done
237
238 for ((__i = __level; __i < __abssize; __i++))
239 do
240 if [[ -n $__newpath ]]
241 then
242 __newpath=$__newpath"/"
243 fi
244 __newpath=$__newpath${__absolute[__i]}
245 done
246
466a5998 247 printf "%s\n" "$__newpath"
d4bb4316
HH
248}
249
b093aa2d 250if [[ "$(ln --help)" == *--relative* ]]; then
a20d24de
HH
251 ln_r() {
252 ln -sfnr "${initdir}/$1" "${initdir}/$2"
253 }
254else
255 ln_r() {
256 local _source=$1
257 local _dest=$2
258 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
b093aa2d 259 ln -sfn -- "$(convert_abs_rel "${_dest}" "${_source}")" "${initdir}/${_dest}"
a20d24de
HH
260 }
261fi
262
7e2bca48 263# get_fs_env <device>
97af51db 264# Get and the ID_FS_TYPE variable from udev for a device.
7e2bca48 265# Example:
97af51db 266# $ get_fs_env /dev/sda2
7e2bca48 267# ext4
9ede1929 268get_fs_env() {
0e979d48
HH
269 local evalstr
270 local found
271
7c179686 272 [[ $1 ]] || return
29b10e65 273 unset ID_FS_TYPE
69f7ed96
HH
274 ID_FS_TYPE=$(blkid -u filesystem -o export -- "$1" \
275 | while read line; do
276 if [[ "$line" == TYPE\=* ]]; then
277 printf "%s" "${line#TYPE=}";
278 exit 0;
279 fi
280 done)
281 if [[ $ID_FS_TYPE ]]; then
282 printf "%s" "$ID_FS_TYPE"
283 return 0
7c179686 284 fi
a5cde2dd
HH
285 return 1
286}
9ede1929 287
7e2bca48
HH
288# get_maj_min <device>
289# Prints the major and minor of a device node.
290# Example:
291# $ get_maj_min /dev/sda2
292# 8:2
480d772f 293get_maj_min() {
dba20559
HH
294 local _maj _min _majmin
295 _majmin="$(stat -L -c '%t:%T' "$1" 2>/dev/null)"
296 printf "%s" "$((0x${_majmin%:*})):$((0x${_majmin#*:}))"
480d772f
HH
297}
298
69f7ed96
HH
299# get a persistent path from a device
300get_persistent_dev() {
301 local i _tmp _dev
302
303 _dev=$(get_maj_min "$1")
304 [ -z "$_dev" ] && return
305
306 for i in /dev/mapper/* /dev/disk/by-uuid/* /dev/disk/by-id/*; do
1743473b 307 [[ $i == /dev/mapper/control ]] && continue
69f7ed96
HH
308 [[ $i == /dev/mapper/mpath* ]] && continue
309 _tmp=$(get_maj_min "$i")
310 if [ "$_tmp" = "$_dev" ]; then
311 printf -- "%s" "$i"
312 return
313 fi
314 done
315}
316
7e2bca48
HH
317# find_block_device <mountpoint>
318# Prints the major and minor number of the block device
319# for a given mountpoint.
320# Unless $use_fstab is set to "yes" the functions
321# uses /proc/self/mountinfo as the primary source of the
322# information and only falls back to /etc/fstab, if the mountpoint
323# is not found there.
324# Example:
325# $ find_block_device /usr
326# 8:4
f76ef3aa 327find_block_device() {
9d36d4fb 328 local _majmin _dev _majmin _find_mpt
c8d685c9 329 _find_mpt="$1"
0b706743 330 if [[ $use_fstab != yes ]]; then
9d36d4fb 331 [[ -d $_find_mpt/. ]]
dba20559
HH
332 findmnt -e -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" | { \
333 while read _majmin _dev; do
334 if [[ -b $_dev ]]; then
335 if ! [[ $_majmin ]] || [[ $_majmin == 0:* ]]; then
336 _majmin=$(get_maj_min $_dev)
337 fi
338 if [[ $_majmin ]]; then
466a5998 339 printf "%s\n" "$_majmin"
dba20559 340 else
466a5998 341 printf "%s\n" "$_dev"
dba20559
HH
342 fi
343 return 0
344 fi
345 if [[ $_dev = *:* ]]; then
466a5998 346 printf "%s\n" "$_dev"
dba20559
HH
347 return 0
348 fi
349 done; return 1; } && return 0
350 fi
351 # fall back to /etc/fstab
352
353 findmnt -e --fstab -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" | { \
9d36d4fb 354 while read _majmin _dev; do
dba20559
HH
355 if ! [[ $_dev ]]; then
356 _dev="$_majmin"
357 unset _majmin
358 fi
9d36d4fb 359 if [[ -b $_dev ]]; then
dba20559 360 [[ $_majmin ]] || _majmin=$(get_maj_min $_dev)
9d36d4fb 361 if [[ $_majmin ]]; then
466a5998 362 printf "%s\n" "$_majmin"
9d36d4fb 363 else
466a5998 364 printf "%s\n" "$_dev"
9d36d4fb
HH
365 fi
366 return 0
367 fi
368 if [[ $_dev = *:* ]]; then
466a5998 369 printf "%s\n" "$_dev"
9d36d4fb 370 return 0
cc02093d 371 fi
dba20559 372 done; return 1; } && return 0
0b706743
373
374 return 1
17829e94
VL
375}
376
9d36d4fb
HH
377# find_mp_fstype <mountpoint>
378# Echo the filesystem type for a given mountpoint.
7e2bca48
HH
379# /proc/self/mountinfo is taken as the primary source of information
380# and /etc/fstab is used as a fallback.
381# No newline is appended!
382# Example:
9d36d4fb 383# $ find_mp_fstype /;echo
7e2bca48 384# ext4
9d36d4fb
HH
385find_mp_fstype() {
386 local _fs
7ae5d9d1 387
9d36d4fb 388 if [[ $use_fstab != yes ]]; then
dba20559
HH
389 findmnt -e -v -n -o 'FSTYPE' --target "$1" | { \
390 while read _fs; do
391 [[ $_fs ]] || continue
392 [[ $_fs = "autofs" ]] && continue
466a5998 393 printf "%s" "$_fs"
dba20559
HH
394 return 0
395 done; return 1; } && return 0
396 fi
397
398 findmnt --fstab -e -v -n -o 'FSTYPE' --target "$1" | { \
9d36d4fb
HH
399 while read _fs; do
400 [[ $_fs ]] || continue
401 [[ $_fs = "autofs" ]] && continue
466a5998 402 printf "%s" "$_fs"
9d36d4fb 403 return 0
dba20559 404 done; return 1; } && return 0
7ae5d9d1
HH
405
406 return 1
407}
408
9d36d4fb
HH
409# find_dev_fstype <device>
410# Echo the filesystem type for a given device.
81672479
HH
411# /proc/self/mountinfo is taken as the primary source of information
412# and /etc/fstab is used as a fallback.
413# No newline is appended!
414# Example:
9d36d4fb 415# $ find_dev_fstype /dev/sda2;echo
81672479 416# ext4
9d36d4fb 417find_dev_fstype() {
5e601454 418 local _find_dev _fs
9d36d4fb 419 _find_dev="$1"
a4f7b504
HH
420 if ! [[ "$_find_dev" = /dev* ]]; then
421 [[ -b "/dev/block/$_find_dev" ]] && _find_dev="/dev/block/$_find_dev"
422 fi
5e601454
HH
423
424 if [[ $use_fstab != yes ]]; then
dba20559
HH
425 findmnt -e -v -n -o 'FSTYPE' --source "$_find_dev" | { \
426 while read _fs; do
427 [[ $_fs ]] || continue
428 [[ $_fs = "autofs" ]] && continue
466a5998 429 printf "%s" "$_fs"
dba20559
HH
430 return 0
431 done; return 1; } && return 0
432 fi
433
434 findmnt --fstab -e -v -n -o 'FSTYPE' --source "$_find_dev" | { \
5e601454
HH
435 while read _fs; do
436 [[ $_fs ]] || continue
437 [[ $_fs = "autofs" ]] && continue
466a5998 438 printf "%s" "$_fs"
5e601454 439 return 0
dba20559 440 done; return 1; } && return 0
5e601454
HH
441
442 return 1
97af51db 443}
5e601454 444
bbc9bfe1
HH
445# find_mp_fsopts <mountpoint>
446# Echo the filesystem options for a given mountpoint.
447# /proc/self/mountinfo is taken as the primary source of information
448# and /etc/fstab is used as a fallback.
449# No newline is appended!
450# Example:
451# $ find_mp_fsopts /;echo
452# rw,relatime,discard,data=ordered
453find_mp_fsopts() {
454 if [[ $use_fstab != yes ]]; then
455 findmnt -e -v -n -o 'OPTIONS' --target "$1" 2>/dev/null && return 0
456 fi
457
458 findmnt --fstab -e -v -n -o 'OPTIONS' --target "$1"
459}
460
97af51db
HH
461# find_dev_fsopts <device>
462# Echo the filesystem options for a given device.
463# /proc/self/mountinfo is taken as the primary source of information
464# and /etc/fstab is used as a fallback.
465# Example:
466# $ find_dev_fsopts /dev/sda2
467# rw,relatime,discard,data=ordered
468find_dev_fsopts() {
469 local _find_dev _opts
470 _find_dev="$1"
471 if ! [[ "$_find_dev" = /dev* ]]; then
472 [[ -b "/dev/block/$_find_dev" ]] && _find_dev="/dev/block/$_find_dev"
473 fi
474
475 if [[ $use_fstab != yes ]]; then
476 findmnt -e -v -n -o 'OPTIONS' --source "$_find_dev" 2>/dev/null && return 0
477 fi
478
479 findmnt --fstab -e -v -n -o 'OPTIONS' --source "$_find_dev"
81672479
HH
480}
481
97af51db 482
7ae5d9d1 483# finds the major:minor of the block device backing the root filesystem.
f76ef3aa
VL
484find_root_block_device() { find_block_device /; }
485
7e2bca48 486# for_each_host_dev_fs <func>
d351541e 487# Execute "<func> <dev> <filesystem>" for every "<dev> <fs>" pair found
7e2bca48 488# in ${host_fs_types[@]}
480d772f
HH
489for_each_host_dev_fs()
490{
491 local _func="$1"
d0096de7 492 local _dev
2efa546f 493 local _ret=1
fd191a7b
HH
494
495 [[ "${!host_fs_types[@]}" ]] || return 0
496
d351541e
HH
497 for _dev in "${!host_fs_types[@]}"; do
498 $_func "$_dev" "${host_fs_types[$_dev]}" && _ret=0
480d772f 499 done
2efa546f 500 return $_ret
480d772f
HH
501}
502
fd191a7b
HH
503host_fs_all()
504{
466a5998 505 printf "%s\n" "${host_fs_types[@]}"
fd191a7b
HH
506}
507
17829e94
VL
508# Walk all the slave relationships for a given block device.
509# Stop when our helper function returns success
510# $1 = function to call on every found block device
511# $2 = block device in major:minor format
0b706743 512check_block_and_slaves() {
29b10e65 513 local _x
17829e94
VL
514 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
515 "$1" $2 && return
533d7dc4 516 check_vol_slaves "$@" && return 0
3478b314 517 if [[ -f /sys/dev/block/$2/../dev ]]; then
dba20559 518 check_block_and_slaves $1 $(<"/sys/dev/block/$2/../dev") && return 0
a3afcf2a 519 fi
17829e94 520 [[ -d /sys/dev/block/$2/slaves ]] || return 1
29b10e65 521 for _x in /sys/dev/block/$2/slaves/*/dev; do
d82e0d7d 522 [[ -f $_x ]] || continue
dba20559 523 check_block_and_slaves $1 $(<"$_x") && return 0
17829e94
VL
524 done
525 return 1
526}
527
83e0dc7a
DY
528check_block_and_slaves_all() {
529 local _x _ret=1
530 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
531 if "$1" $2; then
dba20559 532 _ret=0
83e0dc7a
DY
533 fi
534 check_vol_slaves "$@" && return 0
535 if [[ -f /sys/dev/block/$2/../dev ]]; then
dba20559 536 check_block_and_slaves_all $1 $(<"/sys/dev/block/$2/../dev") && _ret=0
83e0dc7a
DY
537 fi
538 [[ -d /sys/dev/block/$2/slaves ]] || return 1
539 for _x in /sys/dev/block/$2/slaves/*/dev; do
540 [[ -f $_x ]] || continue
dba20559 541 check_block_and_slaves_all $1 $(<"$_x") && _ret=0
83e0dc7a
DY
542 done
543 return $_ret
544}
545# for_each_host_dev_and_slaves <func>
546# Execute "<func> <dev>" for every "<dev>" found
547# in ${host_devs[@]} and their slaves
548for_each_host_dev_and_slaves_all()
549{
550 local _func="$1"
551 local _dev
552 local _ret=1
fd191a7b
HH
553
554 [[ "${host_devs[@]}" ]] || return 0
555
83e0dc7a
DY
556 for _dev in ${host_devs[@]}; do
557 [[ -b "$_dev" ]] || continue
83e0dc7a 558 if check_block_and_slaves_all $_func $(get_maj_min $_dev); then
dba20559 559 _ret=0
83e0dc7a
DY
560 fi
561 done
562 return $_ret
563}
564
565for_each_host_dev_and_slaves()
566{
567 local _func="$1"
568 local _dev
fd191a7b
HH
569
570 [[ "${host_devs[@]}" ]] || return 0
571
83e0dc7a
DY
572 for _dev in ${host_devs[@]}; do
573 [[ -b "$_dev" ]] || continue
a999414e 574 check_block_and_slaves $_func $(get_maj_min $_dev) && return 0
83e0dc7a
DY
575 done
576 return 1
577}
578
533d7dc4
HH
579# ugly workaround for the lvm design
580# There is no volume group device,
581# so, there are no slave devices for volume groups.
582# Logical volumes only have the slave devices they really live on,
583# but you cannot create the logical volume without the volume group.
95bde758 584# And the volume group might be bigger than the devices the LV needs.
533d7dc4 585check_vol_slaves() {
29b10e65 586 local _lv _vg _pv
0b706743 587 for i in /dev/mapper/*; do
1743473b 588 [[ $i == /dev/mapper/control ]] && continue
c4e48eae 589 _lv=$(get_maj_min $i)
29b10e65
HH
590 if [[ $_lv = $2 ]]; then
591 _vg=$(lvm lvs --noheadings -o vg_name $i 2>/dev/null)
cc02093d 592 # strip space
466a5998 593 _vg=$(printf "%s\n" "$_vg")
29b10e65
HH
594 if [[ $_vg ]]; then
595 for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2>/dev/null)
0b706743 596 do
c4e48eae 597 check_block_and_slaves $1 $(get_maj_min $_pv) && return 0
cc02093d
HH
598 done
599 fi
600 fi
533d7dc4
HH
601 done
602 return 1
603}
604
8aa99268
HH
605# fs_get_option <filesystem options> <search for option>
606# search for a specific option in a bunch of filesystem options
607# and return the value
608fs_get_option() {
609 local _fsopts=$1
610 local _option=$2
611 local OLDIFS="$IFS"
612 IFS=,
613 set -- $_fsopts
614 IFS="$OLDIFS"
615 while [ $# -gt 0 ]; do
616 case $1 in
617 $_option=*)
618 echo ${1#${_option}=}
619 break
620 esac
621 shift
622 done
623}
624
89d44e72 625
55a0b3ab
HH
626if ! [[ $DRACUT_INSTALL ]]; then
627 DRACUT_INSTALL=$(find_binary dracut-install)
628fi
19612483 629
55a0b3ab
HH
630if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install ]]; then
631 DRACUT_INSTALL=$dracutbasedir/dracut-install
632fi
3f590c78 633
55a0b3ab
HH
634if ! [[ -x $DRACUT_INSTALL ]]; then
635 dfatal "dracut-install not found!"
636 exit 10
637fi
22048b44 638
55a0b3ab
HH
639[[ $DRACUT_RESOLVE_LAZY ]] || export DRACUT_RESOLVE_DEPS=1
640inst_dir() {
641 [[ -e ${initdir}/"$1" ]] && return 0 # already there
642 $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@"
643 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@" || :
644}
22048b44 645
55a0b3ab
HH
646inst() {
647 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
648 #dinfo "$DRACUT_INSTALL -l $@"
649 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
650 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
651}
89d44e72 652
55a0b3ab
HH
653inst_simple() {
654 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
655 [[ -e $1 ]] || return 1 # no source
656 $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@"
657 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@" || :
658}
89d44e72 659
55a0b3ab
HH
660inst_symlink() {
661 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
662 [[ -L $1 ]] || return 1
663 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
664 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
665}
89d44e72 666
af119460 667inst_multiple() {
55a0b3ab
HH
668 local ret
669 #dinfo "initdir=$initdir $DRACUT_INSTALL -l $@"
670 $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
671 ret=$?
672 (($ret != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
673 return $ret
674}
89d44e72 675
af119460
HH
676dracut_install() {
677 inst_multiple "$@"
678}
679
55a0b3ab
HH
680inst_library() {
681 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
682 [[ -e $1 ]] || return 1 # no source
683 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
684 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
685}
89d44e72 686
55a0b3ab
HH
687inst_binary() {
688 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
689 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
690}
89d44e72 691
55a0b3ab
HH
692inst_script() {
693 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
694 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
695}
36b24d7c 696
a10a1416
697# find symlinks linked to given library file
698# $1 = library file
699# Function searches for symlinks by stripping version numbers appended to
700# library filename, checks if it points to the same target and finally
701# prints the list of symlinks to stdout.
702#
703# Example:
704# rev_lib_symlinks libfoo.so.8.1
705# output: libfoo.so.8 libfoo.so
706# (Only if libfoo.so.8 and libfoo.so exists on host system.)
707rev_lib_symlinks() {
708 [[ ! $1 ]] && return 0
709
710 local fn="$1" orig="$(readlink -f "$1")" links=''
711
b093aa2d 712 [[ ${fn} == *.so.* ]] || return 1
a10a1416
713
714 until [[ ${fn##*.} == so ]]; do
715 fn="${fn%.*}"
716 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
717 done
718
0b706743 719 echo "${links}"
a10a1416
720}
721
62073c30
CG
722# attempt to install any programs specified in a udev rule
723inst_rule_programs() {
724 local _prog _bin
725
726 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
727 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
76d5c79e 728 _bin=""
604d1eb2
HH
729 if [ -x ${udevdir}/$_prog ]; then
730 _bin=${udevdir}/$_prog
34bd2be2 731 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
604d1eb2 732 _bin=$(find_binary "$_prog") || {
cc218087 733 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
604d1eb2
HH
734 continue;
735 }
736 fi
737
af119460 738 [[ $_bin ]] && inst_binary "$_bin"
604d1eb2
HH
739 done
740 fi
da9c5181
HH
741 if grep -qE 'RUN[+=]=?"[^ "]+' "$1"; then
742 for _prog in $(grep -E 'RUN[+=]=?"[^ "]+' "$1" | sed -r 's/.*RUN[+=]=?"([^ "]+).*/\1/'); do
76d5c79e 743 _bin=""
604d1eb2
HH
744 if [ -x ${udevdir}/$_prog ]; then
745 _bin=${udevdir}/$_prog
34bd2be2 746 elif [[ "${_prog/\$env\{/}" == "$_prog" ]] && [[ "${_prog}" != "/sbin/initqueue" ]]; then
604d1eb2 747 _bin=$(find_binary "$_prog") || {
cc218087 748 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
604d1eb2
HH
749 continue;
750 }
751 fi
752
af119460 753 [[ $_bin ]] && inst_binary "$_bin"
604d1eb2
HH
754 done
755 fi
65d1a8a4
HH
756 if grep -qE 'IMPORT\{program\}==?"[^ "]+' "$1"; then
757 for _prog in $(grep -E 'IMPORT\{program\}==?"[^ "]+' "$1" | sed -r 's/.*IMPORT\{program\}==?"([^ "]+).*/\1/'); do
76d5c79e 758 _bin=""
604d1eb2
HH
759 if [ -x ${udevdir}/$_prog ]; then
760 _bin=${udevdir}/$_prog
34bd2be2 761 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
62073c30 762 _bin=$(find_binary "$_prog") || {
cc218087 763 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
62073c30
CG
764 continue;
765 }
766 fi
767
76d5c79e 768 [[ $_bin ]] && dracut_install "$_bin"
62073c30
CG
769 done
770 fi
771}
772
e0a39336
HH
773# attempt to install any programs specified in a udev rule
774inst_rule_group_owner() {
775 local i
776
777 if grep -qE 'OWNER=?"[^ "]+' "$1"; then
778 for i in $(grep -E 'OWNER=?"[^ "]+' "$1" | sed -r 's/.*OWNER=?"([^ "]+).*/\1/'); do
779 if ! egrep -q "^$i:" "$initdir/etc/passwd" 2>/dev/null; then
780 egrep "^$i:" /etc/passwd 2>/dev/null >> "$initdir/etc/passwd"
781 fi
782 done
783 fi
784 if grep -qE 'GROUP=?"[^ "]+' "$1"; then
785 for i in $(grep -E 'GROUP=?"[^ "]+' "$1" | sed -r 's/.*GROUP=?"([^ "]+).*/\1/'); do
786 if ! egrep -q "^$i:" "$initdir/etc/group" 2>/dev/null; then
787 egrep "^$i:" /etc/group 2>/dev/null >> "$initdir/etc/group"
788 fi
789 done
790 fi
791}
792
8d85429a
HH
793inst_rule_initqueue() {
794 if grep -q -F initqueue "$1"; then
795 dracut_need_initqueue
796 fi
797}
798
f04dc5f3
VL
799# udev rules always get installed in the same place, so
800# create a function to install them to make life simpler.
0b706743 801inst_rules() {
29b10e65 802 local _target=/etc/udev/rules.d _rule _found
fd2312e0 803
604d1eb2 804 inst_dir "${udevdir}/rules.d"
29b10e65
HH
805 inst_dir "$_target"
806 for _rule in "$@"; do
256cba18 807 if [ "${_rule#/}" = "$_rule" ]; then
604d1eb2 808 for r in ${udevdir}/rules.d /etc/udev/rules.d; do
76f5fa54
HH
809 if [[ -f $r/$_rule ]]; then
810 _found="$r/$_rule"
62073c30 811 inst_rule_programs "$_found"
e0a39336 812 inst_rule_group_owner "$_found"
8d85429a 813 inst_rule_initqueue "$_found"
604d1eb2 814 inst_simple "$_found"
76f5fa54
HH
815 fi
816 done
817 fi
c97e1a76 818 for r in '' ./ $dracutbasedir/rules.d/; do
29b10e65
HH
819 if [[ -f ${r}$_rule ]]; then
820 _found="${r}$_rule"
62073c30 821 inst_rule_programs "$_found"
e0a39336 822 inst_rule_group_owner "$_found"
8d85429a 823 inst_rule_initqueue "$_found"
604d1eb2 824 inst_simple "$_found" "$_target/${_found##*/}"
c97e1a76
HH
825 fi
826 done
29b10e65 827 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
f04dc5f3
VL
828 done
829}
830
c46cda82
HH
831prepare_udev_rules() {
832 [ -z "$UDEVVERSION" ] && export UDEVVERSION=$(udevadm --version)
833
834 for f in "$@"; do
835 f="${initdir}/etc/udev/rules.d/$f"
836 [ -e "$f" ] || continue
837 while read line; do
838 if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
839 if [ $UDEVVERSION -ge 174 ]; then
840 printf '%sIMPORT{builtin}="path_id"\n' "${line%%IMPORT PATH_ID}"
841 else
842 printf '%sIMPORT{program}="path_id %%p"\n' "${line%%IMPORT PATH_ID}"
843 fi
844 elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
845 if [ $UDEVVERSION -ge 176 ]; then
846 printf '%sIMPORT{builtin}="blkid"\n' "${line%%IMPORT BLKID}"
847 else
848 printf '%sIMPORT{program}="/sbin/blkid -o udev -p $tempnode"\n' "${line%%IMPORT BLKID}"
849 fi
850 else
851 echo "$line"
852 fi
853 done < "${f}" > "${f}.new"
854 mv "${f}.new" "$f"
855 done
856}
857
53f95456
VL
858# install function specialized for hooks
859# $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
860# All hooks should be POSIX/SuS compliant, they will be sourced by init.
861inst_hook() {
cdad82fd 862 if ! [[ -f $3 ]]; then
e27770e1
863 dfatal "Cannot install a hook ($3) that does not exist."
864 dfatal "Aborting initrd creation."
cc02093d 865 exit 1
b093aa2d 866 elif ! [[ "$hookdirs" == *$1* ]]; then
3b403b32 867 dfatal "No such hook type $1. Aborting initrd creation."
cc02093d 868 exit 1
cdad82fd 869 fi
5a84ac3f 870 inst_simple "$3" "/lib/dracut/hooks/${1}/${2}-${3##*/}"
53f95456
VL
871}
872
3378a54f
873# install any of listed files
874#
875# If first argument is '-d' and second some destination path, first accessible
876# source is installed into this path, otherwise it will installed in the same
877# path as source. If none of listed files was installed, function return 1.
878# On first successful installation it returns with 0 status.
879#
880# Example:
881#
882# inst_any -d /bin/foo /bin/bar /bin/baz
883#
884# Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
885# initramfs.
886inst_any() {
887 local to f
888
889 [[ $1 = '-d' ]] && to="$2" && shift 2
890
891 for f in "$@"; do
892 if [[ -e $f ]]; then
893 [[ $to ]] && inst "$f" "$to" && return 0
894 inst "$f" && return 0
895 fi
896 done
897
898 return 1
899}
900
c9143a63
JAH
901
902# inst_libdir_file [-n <pattern>] <file> [<file>...]
903# Install a <file> located on a lib directory to the initramfs image
cacaa90c 904# -n <pattern> install matching files
c9143a63 905inst_libdir_file() {
0c6565c8 906 local _files
c9143a63 907 if [[ "$1" == "-n" ]]; then
cacaa90c 908 local _pattern=$2
c9143a63
JAH
909 shift 2
910 for _dir in $libdirs; do
7828692f 911 for _i in "$@"; do
c9143a63 912 for _f in "$_dir"/$_i; do
7828692f 913 [[ "$_f" =~ $_pattern ]] || continue
0c6565c8 914 [[ -e "$_f" ]] && _files+="$_f "
c9143a63
JAH
915 done
916 done
917 done
918 else
919 for _dir in $libdirs; do
7828692f 920 for _i in "$@"; do
c9143a63 921 for _f in "$_dir"/$_i; do
0c6565c8 922 [[ -e "$_f" ]] && _files+="$_f "
c9143a63
JAH
923 done
924 done
925 done
926 fi
af119460 927 [[ $_files ]] && inst_multiple $_files
c9143a63
JAH
928}
929
930
87122afc
931# install function decompressing the target and handling symlinks
932# $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
933#
934# Function install targets in the same paths inside overlay but decompressed
935# and without extensions (.gz, .bz2).
936inst_decompress() {
b19c5d51 937 local _src _cmd
87122afc 938
29b10e65 939 for _src in $@
87122afc 940 do
29b10e65 941 case ${_src} in
c62b9461 942 *.gz) _cmd='gzip -f -d' ;;
29b10e65 943 *.bz2) _cmd='bzip2 -d' ;;
87122afc
944 *) return 1 ;;
945 esac
b19c5d51 946 inst_simple ${_src}
87122afc
947 # Decompress with chosen tool. We assume that tool changes name e.g.
948 # from 'name.gz' to 'name'.
29b10e65 949 ${_cmd} "${initdir}${_src}"
87122afc
950 done
951}
952
953# It's similar to above, but if file is not compressed, performs standard
954# install.
955# $@ = list of files
956inst_opt_decompress() {
29b10e65 957 local _src
87122afc 958
29b10e65 959 for _src in $@
87122afc 960 do
29b10e65 961 inst_decompress "${_src}" || inst "${_src}"
87122afc
962 done
963}
964
7e2bca48
HH
965# module_check <dracut module>
966# execute the check() function of module-setup.sh of <dracut module>
967# or the "check" script, if module-setup.sh is not found
968# "check $hostonly" is called
95d2dabc 969module_check() {
29b10e65
HH
970 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
971 local _ret
31f1c02d
AW
972 local _forced=0
973 local _hostonly=$hostonly
974 [ $# -eq 2 ] && _forced=$2
29b10e65
HH
975 [[ -d $_moddir ]] || return 1
976 if [[ ! -f $_moddir/module-setup.sh ]]; then
95d2dabc 977 # if we do not have a check script, we are unconditionally included
29b10e65 978 [[ -x $_moddir/check ]] || return 0
31f1c02d 979 [ $_forced -ne 0 ] && unset hostonly
29b10e65 980 $_moddir/check $hostonly
31f1c02d 981 _ret=$?
95d2dabc 982 else
1743473b 983 unset check depends cmdline install installkernel
b093aa2d 984 check() { true; }
29b10e65 985 . $_moddir/module-setup.sh
95d2dabc 986 is_func check || return 0
31f1c02d
AW
987 [ $_forced -ne 0 ] && unset hostonly
988 check $hostonly
29b10e65 989 _ret=$?
1743473b 990 unset check depends cmdline install installkernel
0c2e3d12 991 fi
31f1c02d
AW
992 hostonly=$_hostonly
993 return $_ret
0c2e3d12
VL
994}
995
7e2bca48
HH
996# module_check_mount <dracut module>
997# execute the check() function of module-setup.sh of <dracut module>
998# or the "check" script, if module-setup.sh is not found
999# "mount_needs=1 check 0" is called
1000module_check_mount() {
1001 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1002 local _ret
1003 mount_needs=1
1004 [[ -d $_moddir ]] || return 1
1005 if [[ ! -f $_moddir/module-setup.sh ]]; then
1006 # if we do not have a check script, we are unconditionally included
1007 [[ -x $_moddir/check ]] || return 0
1008 mount_needs=1 $_moddir/check 0
1009 _ret=$?
1010 else
1743473b 1011 unset check depends cmdline install installkernel
b093aa2d 1012 check() { false; }
7e2bca48 1013 . $_moddir/module-setup.sh
7e2bca48
HH
1014 check 0
1015 _ret=$?
1743473b 1016 unset check depends cmdline install installkernel
7e2bca48
HH
1017 fi
1018 unset mount_needs
1019 return $_ret
1020}
1021
1022# module_depends <dracut module>
1023# execute the depends() function of module-setup.sh of <dracut module>
1024# or the "depends" script, if module-setup.sh is not found
95d2dabc 1025module_depends() {
29b10e65
HH
1026 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1027 local _ret
1028 [[ -d $_moddir ]] || return 1
1029 if [[ ! -f $_moddir/module-setup.sh ]]; then
95d2dabc 1030 # if we do not have a check script, we have no deps
29b10e65
HH
1031 [[ -x $_moddir/check ]] || return 0
1032 $_moddir/check -d
95d2dabc
HH
1033 return $?
1034 else
1743473b 1035 unset check depends cmdline install installkernel
b093aa2d 1036 depends() { true; }
29b10e65 1037 . $_moddir/module-setup.sh
95d2dabc 1038 depends
29b10e65 1039 _ret=$?
1743473b
HH
1040 unset check depends cmdline install installkernel
1041 return $_ret
1042 fi
1043}
1044
1045# module_cmdline <dracut module>
1046# execute the cmdline() function of module-setup.sh of <dracut module>
1047# or the "cmdline" script, if module-setup.sh is not found
1048module_cmdline() {
1049 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1050 local _ret
1051 [[ -d $_moddir ]] || return 1
1052 if [[ ! -f $_moddir/module-setup.sh ]]; then
1053 [[ -x $_moddir/cmdline ]] && . "$_moddir/cmdline"
1054 return $?
1055 else
1056 unset check depends cmdline install installkernel
1057 cmdline() { true; }
1058 . $_moddir/module-setup.sh
1059 cmdline
1060 _ret=$?
1061 unset check depends cmdline install installkernel
29b10e65 1062 return $_ret
33ee031c 1063 fi
0c2e3d12
VL
1064}
1065
7e2bca48
HH
1066# module_install <dracut module>
1067# execute the install() function of module-setup.sh of <dracut module>
1068# or the "install" script, if module-setup.sh is not found
95d2dabc 1069module_install() {
29b10e65
HH
1070 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1071 local _ret
1072 [[ -d $_moddir ]] || return 1
1073 if [[ ! -f $_moddir/module-setup.sh ]]; then
1074 [[ -x $_moddir/install ]] && . "$_moddir/install"
95d2dabc
HH
1075 return $?
1076 else
1743473b 1077 unset check depends cmdline install installkernel
b093aa2d 1078 install() { true; }
29b10e65 1079 . $_moddir/module-setup.sh
95d2dabc 1080 install
29b10e65 1081 _ret=$?
1743473b 1082 unset check depends cmdline install installkernel
29b10e65 1083 return $_ret
95d2dabc
HH
1084 fi
1085}
1086
7e2bca48
HH
1087# module_installkernel <dracut module>
1088# execute the installkernel() function of module-setup.sh of <dracut module>
1089# or the "installkernel" script, if module-setup.sh is not found
95d2dabc 1090module_installkernel() {
29b10e65
HH
1091 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1092 local _ret
1093 [[ -d $_moddir ]] || return 1
1094 if [[ ! -f $_moddir/module-setup.sh ]]; then
1095 [[ -x $_moddir/installkernel ]] && . "$_moddir/installkernel"
95d2dabc
HH
1096 return $?
1097 else
1743473b 1098 unset check depends cmdline install installkernel
b093aa2d 1099 installkernel() { true; }
dba20559 1100 . $_moddir/module-setup.sh
95d2dabc 1101 installkernel
29b10e65 1102 _ret=$?
1743473b 1103 unset check depends cmdline install installkernel
29b10e65 1104 return $_ret
95d2dabc
HH
1105 fi
1106}
1107
7e2bca48
HH
1108# check_mount <dracut module>
1109# check_mount checks, if a dracut module is needed for the given
1110# device and filesystem types in "${host_fs_types[@]}"
1b7fd0fa
AW
1111check_mount() {
1112 local _mod=$1
1113 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1114 local _ret
1115 local _moddep
ecfa02bd
HH
1116
1117 [ "${#host_fs_types[*]}" -le 0 ] && return 1
1118
1b7fd0fa 1119 # If we are already scheduled to be loaded, no need to check again.
b093aa2d
HH
1120 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
1121 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
1b7fd0fa
AW
1122
1123 # This should never happen, but...
1124 [[ -d $_moddir ]] || return 1
1125
1126 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
1127
b093aa2d 1128 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
0f283709
HH
1129 return 1
1130 fi
1b7fd0fa 1131
b093aa2d 1132 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
ecfa02bd
HH
1133 module_check_mount $_mod; ret=$?
1134
1135 # explicit module, so also accept ret=255
1136 [[ $ret = 0 || $ret = 255 ]] || return 1
1b7fd0fa 1137 else
ecfa02bd
HH
1138 # module not in our list
1139 if [[ $dracutmodules = all ]]; then
1140 # check, if we can and should install this module
1141 module_check_mount $_mod || return 1
1142 else
1143 # skip this module
1144 return 1
1145 fi
1b7fd0fa
AW
1146 fi
1147
ecfa02bd 1148
1b7fd0fa
AW
1149 for _moddep in $(module_depends $_mod); do
1150 # handle deps as if they were manually added
b093aa2d 1151 [[ " $add_dracutmodules " == *\ $_moddep\ * ]] || \
1b7fd0fa 1152 add_dracutmodules+=" $_moddep "
b093aa2d 1153 [[ " $force_add_dracutmodules " == *\ $_moddep\ * ]] || \
1b7fd0fa
AW
1154 force_add_dracutmodules+=" $_moddep "
1155 # if a module we depend on fail, fail also
0f283709 1156 if ! check_module $_moddep; then
600c8769 1157 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
0f283709
HH
1158 return 1
1159 fi
1b7fd0fa
AW
1160 done
1161
b093aa2d 1162 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
1b7fd0fa
AW
1163 mods_to_load+=" $_mod "
1164
1165 return 0
1166}
1167
7e2bca48
HH
1168# check_module <dracut module> [<use_as_dep>]
1169# check if a dracut module is to be used in the initramfs process
1170# if <use_as_dep> is set, then the process also keeps track
1171# that the modules were checked for the dependency tracking process
95d2dabc 1172check_module() {
29b10e65
HH
1173 local _mod=$1
1174 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1175 local _ret
1176 local _moddep
95d2dabc 1177 # If we are already scheduled to be loaded, no need to check again.
b093aa2d
HH
1178 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
1179 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
95d2dabc
HH
1180
1181 # This should never happen, but...
29b10e65 1182 [[ -d $_moddir ]] || return 1
95d2dabc 1183
29b10e65 1184 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
95d2dabc 1185
b093aa2d 1186 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
600c8769 1187 dinfo "dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
0f283709
HH
1188 return 1
1189 fi
95d2dabc 1190
b093aa2d
HH
1191 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
1192 if [[ " $force_add_dracutmodules " == *\ $_mod\ * ]]; then
31f1c02d
AW
1193 module_check $_mod 1; ret=$?
1194 else
1195 module_check $_mod 0; ret=$?
1196 fi
95d2dabc
HH
1197 # explicit module, so also accept ret=255
1198 [[ $ret = 0 || $ret = 255 ]] || return 1
1199 else
1200 # module not in our list
1201 if [[ $dracutmodules = all ]]; then
1202 # check, if we can and should install this module
29b10e65 1203 module_check $_mod || return 1
cc02093d 1204 else
95d2dabc
HH
1205 # skip this module
1206 return 1
970e646b 1207 fi
95d2dabc 1208 fi
bb764545 1209
29b10e65 1210 for _moddep in $(module_depends $_mod); do
95d2dabc 1211 # handle deps as if they were manually added
b093aa2d 1212 [[ " $add_dracutmodules " == *\ $_moddep\ * ]] || \
29b10e65 1213 add_dracutmodules+=" $_moddep "
b093aa2d 1214 [[ " $force_add_dracutmodules " == *\ $_moddep\ * ]] || \
31f1c02d 1215 force_add_dracutmodules+=" $_moddep "
95d2dabc 1216 # if a module we depend on fail, fail also
0f283709 1217 if ! check_module $_moddep; then
600c8769 1218 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
0f283709
HH
1219 return 1
1220 fi
0c2e3d12 1221 done
70503db4 1222
b093aa2d 1223 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
29b10e65 1224 mods_to_load+=" $_mod "
95d2dabc
HH
1225
1226 return 0
1227}
1228
7e2bca48
HH
1229# for_each_module_dir <func>
1230# execute "<func> <dracut module> 1"
1b7fd0fa 1231for_each_module_dir() {
29b10e65
HH
1232 local _modcheck
1233 local _mod
1234 local _moddir
1b7fd0fa
AW
1235 local _func
1236 _func=$1
29b10e65
HH
1237 for _moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1238 _mod=${_moddir##*/}; _mod=${_mod#[0-9][0-9]}
1b7fd0fa 1239 $_func $_mod 1
70503db4 1240 done
724b87a6
HH
1241
1242 # Report any missing dracut modules, the user has specified
31f1c02d 1243 _modcheck="$add_dracutmodules $force_add_dracutmodules"
29b10e65
HH
1244 [[ $dracutmodules != all ]] && _modcheck="$m $dracutmodules"
1245 for _mod in $_modcheck; do
b093aa2d
HH
1246 [[ " $mods_to_load " == *\ $_mod\ * ]] && continue
1247 [[ " $omit_dracutmodules " == *\ $_mod\ * ]] && continue
600c8769 1248 derror "dracut module '$_mod' cannot be found or installed."
724b87a6 1249 done
0c2e3d12
VL
1250}
1251
ddfd1d10
VL
1252# Install a single kernel module along with any firmware it may require.
1253# $1 = full path to kernel module to install
1254install_kmod_with_fw() {
0b440844 1255 # no need to go further if the module is already installed
379c34d2 1256
0b706743
1257 [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
1258 && return 0
fcbcb252 1259
f4031e8a
HH
1260 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -e "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}" ]]; then
1261 read ret < "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
d8aeb3a7
HH
1262 return $ret
1263 fi
379c34d2 1264
fcbcb252
HH
1265 if [[ $omit_drivers ]]; then
1266 local _kmod=${1##*/}
1267 _kmod=${_kmod%.ko}
1268 _kmod=${_kmod/-/_}
34248c92
HH
1269 if [[ "$_kmod" =~ $omit_drivers ]]; then
1270 dinfo "Omitting driver $_kmod"
7abd4264 1271 return 0
34248c92
HH
1272 fi
1273 if [[ "${1##*/lib/modules/$kernel/}" =~ $omit_drivers ]]; then
fcbcb252 1274 dinfo "Omitting driver $_kmod"
7abd4264 1275 return 0
fcbcb252
HH
1276 fi
1277 fi
1278
868f935e
HH
1279 if [[ $silent_omit_drivers ]]; then
1280 local _kmod=${1##*/}
1281 _kmod=${_kmod%.ko}
1282 _kmod=${_kmod/-/_}
1283 [[ "$_kmod" =~ $silent_omit_drivers ]] && return 0
1284 [[ "${1##*/lib/modules/$kernel/}" =~ $silent_omit_drivers ]] && return 0
1285 fi
1286
d8aeb3a7
HH
1287 inst_simple "$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
1288 ret=$?
f4031e8a
HH
1289 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1290 [[ -d "$DRACUT_KERNEL_LAZY_HASHDIR" ]] && \
1291 echo $ret > "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
d8aeb3a7 1292 (($ret != 0)) && return $ret
e6024e00
JR
1293
1294 local _modname=${1##*/} _fwdir _found _fw
1295 _modname=${_modname%.ko*}
29b10e65
HH
1296 for _fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do
1297 _found=''
1298 for _fwdir in $fw_dir; do
1299 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1300 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1301 _found=yes
cc02093d
HH
1302 fi
1303 done
29b10e65 1304 if [[ $_found != yes ]]; then
fe1484f3 1305 if ! [[ -d $(echo /sys/module/${_modname//-/_}|{ read a b; echo $a; }) ]]; then
29b10e65
HH
1306 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
1307 "\"${_modname}.ko\""
e2cdb570 1308 else
29b10e65
HH
1309 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
1310 "\"${_modname}.ko\""
e2cdb570 1311 fi
cc02093d 1312 fi
ddfd1d10 1313 done
0b440844 1314 return 0
ddfd1d10
VL
1315}
1316
1317# Do something with all the dependencies of a kernel module.
1318# Note that kernel modules depend on themselves using the technique we use
0b706743
1319# $1 = function to call for each dependency we find
1320# It will be passed the full path to the found kernel module
ddfd1d10
VL
1321# $2 = module to get dependencies for
1322# rest of args = arguments to modprobe
c32bda6b 1323# _fderr specifies FD passed from surrounding scope
ddfd1d10 1324for_each_kmod_dep() {
7abd4264 1325 local _func=$1 _kmod=$2 _cmd _modpath _options
ddfd1d10 1326 shift 2
c32bda6b 1327 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
29b10e65
HH
1328 while read _cmd _modpath _options; do
1329 [[ $_cmd = insmod ]] || continue
1330 $_func ${_modpath} || exit $?
0b706743 1331 done
0b706743 1332 )
ddfd1d10
VL
1333}
1334
70c6b773 1335dracut_kernel_post() {
d8aeb3a7 1336 local _moddirname=${srcmods%%/lib/modules/*}
3c4bfd68 1337 local _pid
d8aeb3a7 1338
f4031e8a 1339 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" ]]; then
ec61f0a3
HH
1340 xargs -r modprobe -a ${_moddirname+-d ${_moddirname}/} \
1341 --ignore-install --show-depends --set-version $kernel \
f4031e8a 1342 < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" 2>/dev/null \
89d44e72
HH
1343 | sort -u \
1344 | while read _cmd _modpath _options; do
1345 [[ $_cmd = insmod ]] || continue
1346 echo "$_modpath"
f4031e8a 1347 done > "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
d8aeb3a7 1348
89d44e72 1349 (
73575f11
HH
1350 if [[ $DRACUT_INSTALL ]] && [[ -z $_moddirname ]]; then
1351 xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
89d44e72
HH
1352 else
1353 while read _modpath; do
1354 local _destpath=$_modpath
1355 [[ $_moddirname ]] && _destpath=${_destpath##$_moddirname/}
1356 _destpath=${_destpath##*/lib/modules/$kernel/}
1357 inst_simple "$_modpath" "/lib/modules/$kernel/${_destpath}" || exit $?
f4031e8a 1358 done < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
89d44e72
HH
1359 fi
1360 ) &
dba20559
HH
1361 _pid=$(jobs -p | while read a ; do printf ":$a";done)
1362 _pid=${_pid##*:}
d8aeb3a7 1363
73575f11
HH
1364 if [[ $DRACUT_INSTALL ]]; then
1365 xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" \
89d44e72
HH
1366 | while read line; do
1367 for _fwdir in $fw_dir; do
1368 echo $_fwdir/$line;
1369 done;
73575f11 1370 done | xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a -o
89d44e72 1371 else
73575f11 1372 for _fw in $(xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"); do
89d44e72
HH
1373 for _fwdir in $fw_dir; do
1374 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1375 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1376 break
1377 fi
1378 done
1379 done
1380 fi
d8aeb3a7 1381
3c4bfd68 1382 wait $_pid
89d44e72 1383 fi
70c6b773
HH
1384
1385 for _f in modules.builtin.bin modules.builtin; do
1386 [[ $srcmods/$_f ]] && break
1387 done || {
1388 dfatal "No modules.builtin.bin and modules.builtin found!"
1389 return 1
1390 }
1391
1392 for _f in modules.builtin.bin modules.builtin modules.order; do
1393 [[ $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f"
1394 done
1395
1396 # generate module dependencies for the initrd
1397 if [[ -d $initdir/lib/modules/$kernel ]] && \
1398 ! depmod -a -b "$initdir" $kernel; then
1399 dfatal "\"depmod -a $kernel\" failed."
1400 exit 1
1401 fi
1402
32bd2fbb 1403 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && rm -fr -- "$DRACUT_KERNEL_LAZY_HASHDIR"
d8aeb3a7 1404}
fe1484f3 1405
dba20559 1406module_is_host_only() {
080d1b71
HH
1407 local _mod=$1
1408 _mod=${_mod##*/}
1409 _mod=${_mod%.ko}
1410
b093aa2d 1411 [[ " $add_drivers " == *\ ${_mod}\ * ]] && return 0
080d1b71
HH
1412
1413 # check if module is loaded
b093aa2d
HH
1414 for i in /sys/module/${_mod//-/_}; do
1415 [[ -d $i ]] && return 0
1416 done
080d1b71
HH
1417
1418 # check if module is loadable on the current kernel
1419 # this covers the case, where a new module is introduced
1420 # or a module was renamed
1421 # or a module changed from builtin to a module
1422 modinfo -F filename "$_mod" &>/dev/null || return 0
1423
1424 return 1
b093aa2d 1425}
080d1b71 1426
b093aa2d 1427find_kernel_modules_by_path () {
74405477 1428 local _OLDIFS
5db6ca5a 1429
b093aa2d 1430 [[ -f "$srcmods/modules.dep" ]] || return 0
5db6ca5a
HH
1431
1432 _OLDIFS=$IFS
1433 IFS=:
1434 while read a rest; do
1435 [[ $a = */$1/* ]] || continue
b093aa2d
HH
1436 printf "%s\n" "$srcmods/$a"
1437 done < "$srcmods/modules.dep"
5db6ca5a 1438 IFS=$_OLDIFS
74405477 1439 return 0
b093aa2d 1440}
4073c815 1441
881eda69
JR
1442find_kernel_modules () {
1443 find_kernel_modules_by_path drivers
1444}
1445
338b43cd
HH
1446# instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1447# instmods [-c [-s]] <kernel subsystem>
ddfd1d10 1448# install kernel modules along with all their dependencies.
7e2bca48 1449# <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
6fa0c3d6 1450instmods() {
0c1a8ebc 1451 [[ $no_kernel = yes ]] && return
c32bda6b
MS
1452 # called [sub]functions inherit _fderr
1453 local _fderr=9
a6d3be9d 1454 local _check=no
338b43cd 1455 local _silent=no
a6d3be9d
AW
1456 if [[ $1 = '-c' ]]; then
1457 _check=yes
1458 shift
1459 fi
881eda69 1460
338b43cd
HH
1461 if [[ $1 = '-s' ]]; then
1462 _silent=yes
1463 shift
1464 fi
1465
881eda69 1466 function inst1mod() {
a6d3be9d 1467 local _ret=0 _mod="$1"
29b10e65 1468 case $_mod in
0b706743 1469 =*)
d8aeb3a7
HH
1470 ( [[ "$_mpargs" ]] && echo $_mpargs
1471 find_kernel_modules_by_path "${_mod#=}" ) \
1472 | instmods
1473 ((_ret+=$?))
cc02093d 1474 ;;
0024702f 1475 --*) _mpargs+=" $_mod" ;;
34248c92 1476 *)
379c34d2 1477 _mod=${_mod##*/}
6568d86a 1478 # if we are already installed, skip this module and go on
cc02093d 1479 # to the next one.
f4031e8a
HH
1480 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1481 [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko" ]]; then
1482 read _ret <"$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko"
d8aeb3a7
HH
1483 return $_ret
1484 fi
881eda69 1485
beb26a08
DY
1486 _mod=${_mod/-/_}
1487 if [[ $omit_drivers ]] && [[ "$_mod" =~ $omit_drivers ]]; then
34248c92 1488 dinfo "Omitting driver ${_mod##$srcmods}"
7abd4264 1489 return 0
34248c92 1490 fi
080d1b71 1491
cc02093d
HH
1492 # If we are building a host-specific initramfs and this
1493 # module is not already loaded, move on to the next one.
fe1484f3 1494 [[ $hostonly ]] \
080d1b71 1495 && ! module_is_host_only "$_mod" \
7abd4264 1496 && return 0
04b56f3a 1497
f4031e8a 1498 if [[ "$_check" = "yes" ]] || ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
d8aeb3a7 1499 # We use '-d' option in modprobe only if modules prefix path
600c8769 1500 # differs from default '/'. This allows us to use dracut with
d8aeb3a7
HH
1501 # old version of modprobe which doesn't have '-d' option.
1502 local _moddirname=${srcmods%%/lib/modules/*}
1503 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1504
1505 # ok, load the module, all its dependencies, and any firmware
1506 # it may require
1507 for_each_kmod_dep install_kmod_with_fw $_mod \
1508 --set-version $kernel ${_moddirname} $_mpargs
1509 ((_ret+=$?))
1510 else
f4031e8a
HH
1511 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1512 echo $_mod >> "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist"
d8aeb3a7 1513 fi
cc02093d 1514 ;;
0b706743 1515 esac
a6d3be9d 1516 return $_ret
881eda69
JR
1517 }
1518
f9708da2 1519 function instmods_1() {
a6d3be9d 1520 local _mod _mpargs
f9708da2
JR
1521 if (($# == 0)); then # filenames from stdin
1522 while read _mod; do
a6d3be9d 1523 inst1mod "${_mod%.ko*}" || {
338b43cd 1524 if [[ "$_check" == "yes" ]]; then
19b5c109 1525 [[ "$_silent" == "no" ]] && dfatal "Failed to install module $_mod"
a6d3be9d
AW
1526 return 1
1527 fi
1528 }
f9708da2
JR
1529 done
1530 fi
1531 while (($# > 0)); do # filenames as arguments
a6d3be9d 1532 inst1mod ${1%.ko*} || {
338b43cd 1533 if [[ "$_check" == "yes" ]]; then
19b5c109 1534 [[ "$_silent" == "no" ]] && dfatal "Failed to install module $1"
a6d3be9d
AW
1535 return 1
1536 fi
1537 }
f9708da2 1538 shift
881eda69 1539 done
a6d3be9d 1540 return 0
f9708da2
JR
1541 }
1542
a6d3be9d 1543 local _ret _filter_not_found='FATAL: Module .* not found.'
c32bda6b
MS
1544 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1545 # redirections, but that would make dracut require bash4 at least.
1546 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
dba20559 1547 | while read line; do [[ "$line" =~ $_filter_not_found ]] || echo $line;done | derror
a6d3be9d 1548 _ret=$?
a6d3be9d 1549 return $_ret
0b706743 1550}
5f2c30d9
KRW
1551# get_cpu_vendor
1552# Only two values are returned: AMD or Intel
1553get_cpu_vendor ()
1554{
1555 if grep -qE AMD /proc/cpuinfo; then
1556 printf "AMD"
1557 fi
1558 if grep -qE Intel /proc/cpuinfo; then
1559 printf "Intel"
1560 fi
1561}
1562
1563# get_host_ucode
1564# Get the hosts' ucode file based on the /proc/cpuinfo
1565get_ucode_file ()
1566{
1567 local family=`grep -E "cpu family" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1568 local model=`grep -E "model" /proc/cpuinfo |grep -v name | head -1 | sed s/.*:\ //`
1569 local stepping=`grep -E "stepping" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1570
1571 if [[ "$(get_cpu_vendor)" == "AMD" ]]; then
1572 # If family greater or equal than 0x15
1573 if [[ $family -ge 21 ]]; then
1574 printf "microcode_amd_fam15h.bin"
1575 else
1576 printf "microcode_amd.bin"
1577 fi
1578 fi
1579 if [[ "$(get_cpu_vendor)" == "Intel" ]]; then
1580 # The /proc/cpuinfo are in decimal.
1581 printf "%02x-%02x-%02x" ${family} ${model} ${stepping}
1582 fi
1583}