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