]> git.ipfire.org Git - thirdparty/dracut.git/blob - dracut-functions.sh
dracut-functions.sh: add find_dev_fsopts()
[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 the ID_FS_TYPE variable from udev for a device.
265 # Example:
266 # $ get_fs_env /dev/sda2
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 # find_dev_fsopts <device>
445 # Echo the filesystem options for a given device.
446 # /proc/self/mountinfo is taken as the primary source of information
447 # and /etc/fstab is used as a fallback.
448 # Example:
449 # $ find_dev_fsopts /dev/sda2
450 # rw,relatime,discard,data=ordered
451 find_dev_fsopts() {
452 local _find_dev _opts
453 _find_dev="$1"
454 if ! [[ "$_find_dev" = /dev* ]]; then
455 [[ -b "/dev/block/$_find_dev" ]] && _find_dev="/dev/block/$_find_dev"
456 fi
457
458 if [[ $use_fstab != yes ]]; then
459 findmnt -e -v -n -o 'OPTIONS' --source "$_find_dev" 2>/dev/null && return 0
460 fi
461
462 findmnt --fstab -e -v -n -o 'OPTIONS' --source "$_find_dev"
463 }
464
465
466 # finds the major:minor of the block device backing the root filesystem.
467 find_root_block_device() { find_block_device /; }
468
469 # for_each_host_dev_fs <func>
470 # Execute "<func> <dev> <filesystem>" for every "<dev> <fs>" pair found
471 # in ${host_fs_types[@]}
472 for_each_host_dev_fs()
473 {
474 local _func="$1"
475 local _dev
476 local _ret=1
477
478 [[ "${!host_fs_types[@]}" ]] || return 0
479
480 for _dev in "${!host_fs_types[@]}"; do
481 $_func "$_dev" "${host_fs_types[$_dev]}" && _ret=0
482 done
483 return $_ret
484 }
485
486 host_fs_all()
487 {
488 echo "${host_fs_types[@]}"
489 }
490
491 # Walk all the slave relationships for a given block device.
492 # Stop when our helper function returns success
493 # $1 = function to call on every found block device
494 # $2 = block device in major:minor format
495 check_block_and_slaves() {
496 local _x
497 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
498 "$1" $2 && return
499 check_vol_slaves "$@" && return 0
500 if [[ -f /sys/dev/block/$2/../dev ]]; then
501 check_block_and_slaves $1 $(<"/sys/dev/block/$2/../dev") && return 0
502 fi
503 [[ -d /sys/dev/block/$2/slaves ]] || return 1
504 for _x in /sys/dev/block/$2/slaves/*/dev; do
505 [[ -f $_x ]] || continue
506 check_block_and_slaves $1 $(<"$_x") && return 0
507 done
508 return 1
509 }
510
511 check_block_and_slaves_all() {
512 local _x _ret=1
513 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
514 if "$1" $2; then
515 _ret=0
516 fi
517 check_vol_slaves "$@" && return 0
518 if [[ -f /sys/dev/block/$2/../dev ]]; then
519 check_block_and_slaves_all $1 $(<"/sys/dev/block/$2/../dev") && _ret=0
520 fi
521 [[ -d /sys/dev/block/$2/slaves ]] || return 1
522 for _x in /sys/dev/block/$2/slaves/*/dev; do
523 [[ -f $_x ]] || continue
524 check_block_and_slaves_all $1 $(<"$_x") && _ret=0
525 done
526 return $_ret
527 }
528 # for_each_host_dev_and_slaves <func>
529 # Execute "<func> <dev>" for every "<dev>" found
530 # in ${host_devs[@]} and their slaves
531 for_each_host_dev_and_slaves_all()
532 {
533 local _func="$1"
534 local _dev
535 local _ret=1
536
537 [[ "${host_devs[@]}" ]] || return 0
538
539 for _dev in ${host_devs[@]}; do
540 [[ -b "$_dev" ]] || continue
541 if check_block_and_slaves_all $_func $(get_maj_min $_dev); then
542 _ret=0
543 fi
544 done
545 return $_ret
546 }
547
548 for_each_host_dev_and_slaves()
549 {
550 local _func="$1"
551 local _dev
552
553 [[ "${host_devs[@]}" ]] || return 0
554
555 for _dev in ${host_devs[@]}; do
556 [[ -b "$_dev" ]] || continue
557 check_block_and_slaves $_func $(get_maj_min $_dev) && return 0
558 done
559 return 1
560 }
561
562 # ugly workaround for the lvm design
563 # There is no volume group device,
564 # so, there are no slave devices for volume groups.
565 # Logical volumes only have the slave devices they really live on,
566 # but you cannot create the logical volume without the volume group.
567 # And the volume group might be bigger than the devices the LV needs.
568 check_vol_slaves() {
569 local _lv _vg _pv
570 for i in /dev/mapper/*; do
571 _lv=$(get_maj_min $i)
572 if [[ $_lv = $2 ]]; then
573 _vg=$(lvm lvs --noheadings -o vg_name $i 2>/dev/null)
574 # strip space
575 _vg=$(echo $_vg)
576 if [[ $_vg ]]; then
577 for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2>/dev/null)
578 do
579 check_block_and_slaves $1 $(get_maj_min $_pv) && return 0
580 done
581 fi
582 fi
583 done
584 return 1
585 }
586
587 # fs_get_option <filesystem options> <search for option>
588 # search for a specific option in a bunch of filesystem options
589 # and return the value
590 fs_get_option() {
591 local _fsopts=$1
592 local _option=$2
593 local OLDIFS="$IFS"
594 IFS=,
595 set -- $_fsopts
596 IFS="$OLDIFS"
597 while [ $# -gt 0 ]; do
598 case $1 in
599 $_option=*)
600 echo ${1#${_option}=}
601 break
602 esac
603 shift
604 done
605 }
606
607
608 if ! [[ $DRACUT_INSTALL ]]; then
609 DRACUT_INSTALL=$(find_binary dracut-install)
610 fi
611
612 if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install ]]; then
613 DRACUT_INSTALL=$dracutbasedir/dracut-install
614 fi
615
616 if ! [[ -x $DRACUT_INSTALL ]]; then
617 dfatal "dracut-install not found!"
618 exit 10
619 fi
620
621 [[ $DRACUT_RESOLVE_LAZY ]] || export DRACUT_RESOLVE_DEPS=1
622 inst_dir() {
623 [[ -e ${initdir}/"$1" ]] && return 0 # already there
624 $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@"
625 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@" || :
626 }
627
628 inst() {
629 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
630 #dinfo "$DRACUT_INSTALL -l $@"
631 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
632 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
633 }
634
635 inst_simple() {
636 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
637 [[ -e $1 ]] || return 1 # no source
638 $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@"
639 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@" || :
640 }
641
642 inst_symlink() {
643 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
644 [[ -L $1 ]] || return 1
645 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
646 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
647 }
648
649 inst_multiple() {
650 local ret
651 #dinfo "initdir=$initdir $DRACUT_INSTALL -l $@"
652 $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
653 ret=$?
654 (($ret != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
655 return $ret
656 }
657
658 dracut_install() {
659 inst_multiple "$@"
660 }
661
662 inst_library() {
663 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
664 [[ -e $1 ]] || return 1 # no source
665 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
666 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
667 }
668
669 inst_binary() {
670 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
671 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
672 }
673
674 inst_script() {
675 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
676 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
677 }
678
679 # find symlinks linked to given library file
680 # $1 = library file
681 # Function searches for symlinks by stripping version numbers appended to
682 # library filename, checks if it points to the same target and finally
683 # prints the list of symlinks to stdout.
684 #
685 # Example:
686 # rev_lib_symlinks libfoo.so.8.1
687 # output: libfoo.so.8 libfoo.so
688 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
689 rev_lib_symlinks() {
690 [[ ! $1 ]] && return 0
691
692 local fn="$1" orig="$(readlink -f "$1")" links=''
693
694 [[ ${fn} == *.so.* ]] || return 1
695
696 until [[ ${fn##*.} == so ]]; do
697 fn="${fn%.*}"
698 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
699 done
700
701 echo "${links}"
702 }
703
704 # attempt to install any programs specified in a udev rule
705 inst_rule_programs() {
706 local _prog _bin
707
708 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
709 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
710 _bin=""
711 if [ -x ${udevdir}/$_prog ]; then
712 _bin=${udevdir}/$_prog
713 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
714 _bin=$(find_binary "$_prog") || {
715 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
716 continue;
717 }
718 fi
719
720 [[ $_bin ]] && inst_binary "$_bin"
721 done
722 fi
723 if grep -qE 'RUN[+=]=?"[^ "]+' "$1"; then
724 for _prog in $(grep -E 'RUN[+=]=?"[^ "]+' "$1" | sed -r 's/.*RUN[+=]=?"([^ "]+).*/\1/'); do
725 _bin=""
726 if [ -x ${udevdir}/$_prog ]; then
727 _bin=${udevdir}/$_prog
728 elif [[ "${_prog/\$env\{/}" == "$_prog" ]] && [[ "${_prog}" != "/sbin/initqueue" ]]; then
729 _bin=$(find_binary "$_prog") || {
730 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
731 continue;
732 }
733 fi
734
735 [[ $_bin ]] && inst_binary "$_bin"
736 done
737 fi
738 if grep -qE 'IMPORT\{program\}==?"[^ "]+' "$1"; then
739 for _prog in $(grep -E 'IMPORT\{program\}==?"[^ "]+' "$1" | sed -r 's/.*IMPORT\{program\}==?"([^ "]+).*/\1/'); do
740 _bin=""
741 if [ -x ${udevdir}/$_prog ]; then
742 _bin=${udevdir}/$_prog
743 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
744 _bin=$(find_binary "$_prog") || {
745 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
746 continue;
747 }
748 fi
749
750 [[ $_bin ]] && dracut_install "$_bin"
751 done
752 fi
753 }
754
755 # attempt to install any programs specified in a udev rule
756 inst_rule_group_owner() {
757 local i
758
759 if grep -qE 'OWNER=?"[^ "]+' "$1"; then
760 for i in $(grep -E 'OWNER=?"[^ "]+' "$1" | sed -r 's/.*OWNER=?"([^ "]+).*/\1/'); do
761 if ! egrep -q "^$i:" "$initdir/etc/passwd" 2>/dev/null; then
762 egrep "^$i:" /etc/passwd 2>/dev/null >> "$initdir/etc/passwd"
763 fi
764 done
765 fi
766 if grep -qE 'GROUP=?"[^ "]+' "$1"; then
767 for i in $(grep -E 'GROUP=?"[^ "]+' "$1" | sed -r 's/.*GROUP=?"([^ "]+).*/\1/'); do
768 if ! egrep -q "^$i:" "$initdir/etc/group" 2>/dev/null; then
769 egrep "^$i:" /etc/group 2>/dev/null >> "$initdir/etc/group"
770 fi
771 done
772 fi
773 }
774
775 inst_rule_initqueue() {
776 if grep -q -F initqueue "$1"; then
777 dracut_need_initqueue
778 fi
779 }
780
781 # udev rules always get installed in the same place, so
782 # create a function to install them to make life simpler.
783 inst_rules() {
784 local _target=/etc/udev/rules.d _rule _found
785
786 inst_dir "${udevdir}/rules.d"
787 inst_dir "$_target"
788 for _rule in "$@"; do
789 if [ "${_rule#/}" = "$_rule" ]; then
790 for r in ${udevdir}/rules.d /etc/udev/rules.d; do
791 if [[ -f $r/$_rule ]]; then
792 _found="$r/$_rule"
793 inst_rule_programs "$_found"
794 inst_rule_group_owner "$_found"
795 inst_rule_initqueue "$_found"
796 inst_simple "$_found"
797 fi
798 done
799 fi
800 for r in '' ./ $dracutbasedir/rules.d/; do
801 if [[ -f ${r}$_rule ]]; then
802 _found="${r}$_rule"
803 inst_rule_programs "$_found"
804 inst_rule_group_owner "$_found"
805 inst_rule_initqueue "$_found"
806 inst_simple "$_found" "$_target/${_found##*/}"
807 fi
808 done
809 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
810 done
811 }
812
813 prepare_udev_rules() {
814 [ -z "$UDEVVERSION" ] && export UDEVVERSION=$(udevadm --version)
815
816 for f in "$@"; do
817 f="${initdir}/etc/udev/rules.d/$f"
818 [ -e "$f" ] || continue
819 while read line; do
820 if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
821 if [ $UDEVVERSION -ge 174 ]; then
822 printf '%sIMPORT{builtin}="path_id"\n' "${line%%IMPORT PATH_ID}"
823 else
824 printf '%sIMPORT{program}="path_id %%p"\n' "${line%%IMPORT PATH_ID}"
825 fi
826 elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
827 if [ $UDEVVERSION -ge 176 ]; then
828 printf '%sIMPORT{builtin}="blkid"\n' "${line%%IMPORT BLKID}"
829 else
830 printf '%sIMPORT{program}="/sbin/blkid -o udev -p $tempnode"\n' "${line%%IMPORT BLKID}"
831 fi
832 else
833 echo "$line"
834 fi
835 done < "${f}" > "${f}.new"
836 mv "${f}.new" "$f"
837 done
838 }
839
840 # install function specialized for hooks
841 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
842 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
843 inst_hook() {
844 if ! [[ -f $3 ]]; then
845 dfatal "Cannot install a hook ($3) that does not exist."
846 dfatal "Aborting initrd creation."
847 exit 1
848 elif ! [[ "$hookdirs" == *$1* ]]; then
849 dfatal "No such hook type $1. Aborting initrd creation."
850 exit 1
851 fi
852 inst_simple "$3" "/lib/dracut/hooks/${1}/${2}-${3##*/}"
853 }
854
855 # install any of listed files
856 #
857 # If first argument is '-d' and second some destination path, first accessible
858 # source is installed into this path, otherwise it will installed in the same
859 # path as source. If none of listed files was installed, function return 1.
860 # On first successful installation it returns with 0 status.
861 #
862 # Example:
863 #
864 # inst_any -d /bin/foo /bin/bar /bin/baz
865 #
866 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
867 # initramfs.
868 inst_any() {
869 local to f
870
871 [[ $1 = '-d' ]] && to="$2" && shift 2
872
873 for f in "$@"; do
874 if [[ -e $f ]]; then
875 [[ $to ]] && inst "$f" "$to" && return 0
876 inst "$f" && return 0
877 fi
878 done
879
880 return 1
881 }
882
883
884 # inst_libdir_file [-n <pattern>] <file> [<file>...]
885 # Install a <file> located on a lib directory to the initramfs image
886 # -n <pattern> install matching files
887 inst_libdir_file() {
888 local _files
889 if [[ "$1" == "-n" ]]; then
890 local _pattern=$2
891 shift 2
892 for _dir in $libdirs; do
893 for _i in "$@"; do
894 for _f in "$_dir"/$_i; do
895 [[ "$_f" =~ $_pattern ]] || continue
896 [[ -e "$_f" ]] && _files+="$_f "
897 done
898 done
899 done
900 else
901 for _dir in $libdirs; do
902 for _i in "$@"; do
903 for _f in "$_dir"/$_i; do
904 [[ -e "$_f" ]] && _files+="$_f "
905 done
906 done
907 done
908 fi
909 [[ $_files ]] && inst_multiple $_files
910 }
911
912
913 # install function decompressing the target and handling symlinks
914 # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
915 #
916 # Function install targets in the same paths inside overlay but decompressed
917 # and without extensions (.gz, .bz2).
918 inst_decompress() {
919 local _src _cmd
920
921 for _src in $@
922 do
923 case ${_src} in
924 *.gz) _cmd='gzip -f -d' ;;
925 *.bz2) _cmd='bzip2 -d' ;;
926 *) return 1 ;;
927 esac
928 inst_simple ${_src}
929 # Decompress with chosen tool. We assume that tool changes name e.g.
930 # from 'name.gz' to 'name'.
931 ${_cmd} "${initdir}${_src}"
932 done
933 }
934
935 # It's similar to above, but if file is not compressed, performs standard
936 # install.
937 # $@ = list of files
938 inst_opt_decompress() {
939 local _src
940
941 for _src in $@
942 do
943 inst_decompress "${_src}" || inst "${_src}"
944 done
945 }
946
947 # module_check <dracut module>
948 # execute the check() function of module-setup.sh of <dracut module>
949 # or the "check" script, if module-setup.sh is not found
950 # "check $hostonly" is called
951 module_check() {
952 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
953 local _ret
954 local _forced=0
955 local _hostonly=$hostonly
956 [ $# -eq 2 ] && _forced=$2
957 [[ -d $_moddir ]] || return 1
958 if [[ ! -f $_moddir/module-setup.sh ]]; then
959 # if we do not have a check script, we are unconditionally included
960 [[ -x $_moddir/check ]] || return 0
961 [ $_forced -ne 0 ] && unset hostonly
962 $_moddir/check $hostonly
963 _ret=$?
964 else
965 unset check depends install installkernel
966 check() { true; }
967 . $_moddir/module-setup.sh
968 is_func check || return 0
969 [ $_forced -ne 0 ] && unset hostonly
970 check $hostonly
971 _ret=$?
972 unset check depends install installkernel
973 fi
974 hostonly=$_hostonly
975 return $_ret
976 }
977
978 # module_check_mount <dracut module>
979 # execute the check() function of module-setup.sh of <dracut module>
980 # or the "check" script, if module-setup.sh is not found
981 # "mount_needs=1 check 0" is called
982 module_check_mount() {
983 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
984 local _ret
985 mount_needs=1
986 [[ -d $_moddir ]] || return 1
987 if [[ ! -f $_moddir/module-setup.sh ]]; then
988 # if we do not have a check script, we are unconditionally included
989 [[ -x $_moddir/check ]] || return 0
990 mount_needs=1 $_moddir/check 0
991 _ret=$?
992 else
993 unset check depends install installkernel
994 check() { false; }
995 . $_moddir/module-setup.sh
996 check 0
997 _ret=$?
998 unset check depends install installkernel
999 fi
1000 unset mount_needs
1001 return $_ret
1002 }
1003
1004 # module_depends <dracut module>
1005 # execute the depends() function of module-setup.sh of <dracut module>
1006 # or the "depends" script, if module-setup.sh is not found
1007 module_depends() {
1008 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1009 local _ret
1010 [[ -d $_moddir ]] || return 1
1011 if [[ ! -f $_moddir/module-setup.sh ]]; then
1012 # if we do not have a check script, we have no deps
1013 [[ -x $_moddir/check ]] || return 0
1014 $_moddir/check -d
1015 return $?
1016 else
1017 unset check depends install installkernel
1018 depends() { true; }
1019 . $_moddir/module-setup.sh
1020 depends
1021 _ret=$?
1022 unset check depends install installkernel
1023 return $_ret
1024 fi
1025 }
1026
1027 # module_install <dracut module>
1028 # execute the install() function of module-setup.sh of <dracut module>
1029 # or the "install" script, if module-setup.sh is not found
1030 module_install() {
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/install ]] && . "$_moddir/install"
1036 return $?
1037 else
1038 unset check depends install installkernel
1039 install() { true; }
1040 . $_moddir/module-setup.sh
1041 install
1042 _ret=$?
1043 unset check depends install installkernel
1044 return $_ret
1045 fi
1046 }
1047
1048 # module_installkernel <dracut module>
1049 # execute the installkernel() function of module-setup.sh of <dracut module>
1050 # or the "installkernel" script, if module-setup.sh is not found
1051 module_installkernel() {
1052 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1053 local _ret
1054 [[ -d $_moddir ]] || return 1
1055 if [[ ! -f $_moddir/module-setup.sh ]]; then
1056 [[ -x $_moddir/installkernel ]] && . "$_moddir/installkernel"
1057 return $?
1058 else
1059 unset check depends install installkernel
1060 installkernel() { true; }
1061 . $_moddir/module-setup.sh
1062 installkernel
1063 _ret=$?
1064 unset check depends install installkernel
1065 return $_ret
1066 fi
1067 }
1068
1069 # check_mount <dracut module>
1070 # check_mount checks, if a dracut module is needed for the given
1071 # device and filesystem types in "${host_fs_types[@]}"
1072 check_mount() {
1073 local _mod=$1
1074 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1075 local _ret
1076 local _moddep
1077
1078 [ "${#host_fs_types[*]}" -le 0 ] && return 1
1079
1080 # If we are already scheduled to be loaded, no need to check again.
1081 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
1082 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
1083
1084 # This should never happen, but...
1085 [[ -d $_moddir ]] || return 1
1086
1087 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
1088
1089 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
1090 return 1
1091 fi
1092
1093 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
1094 module_check_mount $_mod; ret=$?
1095
1096 # explicit module, so also accept ret=255
1097 [[ $ret = 0 || $ret = 255 ]] || return 1
1098 else
1099 # module not in our list
1100 if [[ $dracutmodules = all ]]; then
1101 # check, if we can and should install this module
1102 module_check_mount $_mod || return 1
1103 else
1104 # skip this module
1105 return 1
1106 fi
1107 fi
1108
1109
1110 for _moddep in $(module_depends $_mod); do
1111 # handle deps as if they were manually added
1112 [[ " $add_dracutmodules " == *\ $_moddep\ * ]] || \
1113 add_dracutmodules+=" $_moddep "
1114 [[ " $force_add_dracutmodules " == *\ $_moddep\ * ]] || \
1115 force_add_dracutmodules+=" $_moddep "
1116 # if a module we depend on fail, fail also
1117 if ! check_module $_moddep; then
1118 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
1119 return 1
1120 fi
1121 done
1122
1123 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
1124 mods_to_load+=" $_mod "
1125
1126 return 0
1127 }
1128
1129 # check_module <dracut module> [<use_as_dep>]
1130 # check if a dracut module is to be used in the initramfs process
1131 # if <use_as_dep> is set, then the process also keeps track
1132 # that the modules were checked for the dependency tracking process
1133 check_module() {
1134 local _mod=$1
1135 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1136 local _ret
1137 local _moddep
1138 # If we are already scheduled to be loaded, no need to check again.
1139 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
1140 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
1141
1142 # This should never happen, but...
1143 [[ -d $_moddir ]] || return 1
1144
1145 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
1146
1147 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
1148 dinfo "dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
1149 return 1
1150 fi
1151
1152 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
1153 if [[ " $force_add_dracutmodules " == *\ $_mod\ * ]]; then
1154 module_check $_mod 1; ret=$?
1155 else
1156 module_check $_mod 0; ret=$?
1157 fi
1158 # explicit module, so also accept ret=255
1159 [[ $ret = 0 || $ret = 255 ]] || return 1
1160 else
1161 # module not in our list
1162 if [[ $dracutmodules = all ]]; then
1163 # check, if we can and should install this module
1164 module_check $_mod || return 1
1165 else
1166 # skip this module
1167 return 1
1168 fi
1169 fi
1170
1171 for _moddep in $(module_depends $_mod); do
1172 # handle deps as if they were manually added
1173 [[ " $add_dracutmodules " == *\ $_moddep\ * ]] || \
1174 add_dracutmodules+=" $_moddep "
1175 [[ " $force_add_dracutmodules " == *\ $_moddep\ * ]] || \
1176 force_add_dracutmodules+=" $_moddep "
1177 # if a module we depend on fail, fail also
1178 if ! check_module $_moddep; then
1179 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
1180 return 1
1181 fi
1182 done
1183
1184 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
1185 mods_to_load+=" $_mod "
1186
1187 return 0
1188 }
1189
1190 # for_each_module_dir <func>
1191 # execute "<func> <dracut module> 1"
1192 for_each_module_dir() {
1193 local _modcheck
1194 local _mod
1195 local _moddir
1196 local _func
1197 _func=$1
1198 for _moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1199 _mod=${_moddir##*/}; _mod=${_mod#[0-9][0-9]}
1200 $_func $_mod 1
1201 done
1202
1203 # Report any missing dracut modules, the user has specified
1204 _modcheck="$add_dracutmodules $force_add_dracutmodules"
1205 [[ $dracutmodules != all ]] && _modcheck="$m $dracutmodules"
1206 for _mod in $_modcheck; do
1207 [[ " $mods_to_load " == *\ $_mod\ * ]] && continue
1208 [[ " $omit_dracutmodules " == *\ $_mod\ * ]] && continue
1209 derror "dracut module '$_mod' cannot be found or installed."
1210 done
1211 }
1212
1213 # Install a single kernel module along with any firmware it may require.
1214 # $1 = full path to kernel module to install
1215 install_kmod_with_fw() {
1216 # no need to go further if the module is already installed
1217
1218 [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
1219 && return 0
1220
1221 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -e "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}" ]]; then
1222 read ret < "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
1223 return $ret
1224 fi
1225
1226 if [[ $omit_drivers ]]; then
1227 local _kmod=${1##*/}
1228 _kmod=${_kmod%.ko}
1229 _kmod=${_kmod/-/_}
1230 if [[ "$_kmod" =~ $omit_drivers ]]; then
1231 dinfo "Omitting driver $_kmod"
1232 return 0
1233 fi
1234 if [[ "${1##*/lib/modules/$kernel/}" =~ $omit_drivers ]]; then
1235 dinfo "Omitting driver $_kmod"
1236 return 0
1237 fi
1238 fi
1239
1240 if [[ $silent_omit_drivers ]]; then
1241 local _kmod=${1##*/}
1242 _kmod=${_kmod%.ko}
1243 _kmod=${_kmod/-/_}
1244 [[ "$_kmod" =~ $silent_omit_drivers ]] && return 0
1245 [[ "${1##*/lib/modules/$kernel/}" =~ $silent_omit_drivers ]] && return 0
1246 fi
1247
1248 inst_simple "$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
1249 ret=$?
1250 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1251 [[ -d "$DRACUT_KERNEL_LAZY_HASHDIR" ]] && \
1252 echo $ret > "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
1253 (($ret != 0)) && return $ret
1254
1255 local _modname=${1##*/} _fwdir _found _fw
1256 _modname=${_modname%.ko*}
1257 for _fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do
1258 _found=''
1259 for _fwdir in $fw_dir; do
1260 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1261 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1262 _found=yes
1263 fi
1264 done
1265 if [[ $_found != yes ]]; then
1266 if ! [[ -d $(echo /sys/module/${_modname//-/_}|{ read a b; echo $a; }) ]]; then
1267 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
1268 "\"${_modname}.ko\""
1269 else
1270 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
1271 "\"${_modname}.ko\""
1272 fi
1273 fi
1274 done
1275 return 0
1276 }
1277
1278 # Do something with all the dependencies of a kernel module.
1279 # Note that kernel modules depend on themselves using the technique we use
1280 # $1 = function to call for each dependency we find
1281 # It will be passed the full path to the found kernel module
1282 # $2 = module to get dependencies for
1283 # rest of args = arguments to modprobe
1284 # _fderr specifies FD passed from surrounding scope
1285 for_each_kmod_dep() {
1286 local _func=$1 _kmod=$2 _cmd _modpath _options
1287 shift 2
1288 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
1289 while read _cmd _modpath _options; do
1290 [[ $_cmd = insmod ]] || continue
1291 $_func ${_modpath} || exit $?
1292 done
1293 )
1294 }
1295
1296 dracut_kernel_post() {
1297 local _moddirname=${srcmods%%/lib/modules/*}
1298 local _pid
1299
1300 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" ]]; then
1301 xargs -r modprobe -a ${_moddirname+-d ${_moddirname}/} \
1302 --ignore-install --show-depends --set-version $kernel \
1303 < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" 2>/dev/null \
1304 | sort -u \
1305 | while read _cmd _modpath _options; do
1306 [[ $_cmd = insmod ]] || continue
1307 echo "$_modpath"
1308 done > "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1309
1310 (
1311 if [[ $DRACUT_INSTALL ]] && [[ -z $_moddirname ]]; then
1312 xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1313 else
1314 while read _modpath; do
1315 local _destpath=$_modpath
1316 [[ $_moddirname ]] && _destpath=${_destpath##$_moddirname/}
1317 _destpath=${_destpath##*/lib/modules/$kernel/}
1318 inst_simple "$_modpath" "/lib/modules/$kernel/${_destpath}" || exit $?
1319 done < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1320 fi
1321 ) &
1322 _pid=$(jobs -p | while read a ; do printf ":$a";done)
1323 _pid=${_pid##*:}
1324
1325 if [[ $DRACUT_INSTALL ]]; then
1326 xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" \
1327 | while read line; do
1328 for _fwdir in $fw_dir; do
1329 echo $_fwdir/$line;
1330 done;
1331 done | xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a -o
1332 else
1333 for _fw in $(xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"); do
1334 for _fwdir in $fw_dir; do
1335 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1336 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1337 break
1338 fi
1339 done
1340 done
1341 fi
1342
1343 wait $_pid
1344 fi
1345
1346 for _f in modules.builtin.bin modules.builtin; do
1347 [[ $srcmods/$_f ]] && break
1348 done || {
1349 dfatal "No modules.builtin.bin and modules.builtin found!"
1350 return 1
1351 }
1352
1353 for _f in modules.builtin.bin modules.builtin modules.order; do
1354 [[ $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f"
1355 done
1356
1357 # generate module dependencies for the initrd
1358 if [[ -d $initdir/lib/modules/$kernel ]] && \
1359 ! depmod -a -b "$initdir" $kernel; then
1360 dfatal "\"depmod -a $kernel\" failed."
1361 exit 1
1362 fi
1363
1364 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && rm -fr -- "$DRACUT_KERNEL_LAZY_HASHDIR"
1365 }
1366
1367 module_is_host_only() {
1368 local _mod=$1
1369 _mod=${_mod##*/}
1370 _mod=${_mod%.ko}
1371
1372 [[ " $add_drivers " == *\ ${_mod}\ * ]] && return 0
1373
1374 # check if module is loaded
1375 for i in /sys/module/${_mod//-/_}; do
1376 [[ -d $i ]] && return 0
1377 done
1378
1379 # check if module is loadable on the current kernel
1380 # this covers the case, where a new module is introduced
1381 # or a module was renamed
1382 # or a module changed from builtin to a module
1383 modinfo -F filename "$_mod" &>/dev/null || return 0
1384
1385 return 1
1386 }
1387
1388 find_kernel_modules_by_path () {
1389 local _OLDIFS
1390
1391 [[ -f "$srcmods/modules.dep" ]] || return 0
1392
1393 _OLDIFS=$IFS
1394 IFS=:
1395 while read a rest; do
1396 [[ $a = */$1/* ]] || continue
1397 printf "%s\n" "$srcmods/$a"
1398 done < "$srcmods/modules.dep"
1399 IFS=$_OLDIFS
1400 return 0
1401 }
1402
1403 find_kernel_modules () {
1404 find_kernel_modules_by_path drivers
1405 }
1406
1407 # instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1408 # instmods [-c [-s]] <kernel subsystem>
1409 # install kernel modules along with all their dependencies.
1410 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1411 instmods() {
1412 [[ $no_kernel = yes ]] && return
1413 # called [sub]functions inherit _fderr
1414 local _fderr=9
1415 local _check=no
1416 local _silent=no
1417 if [[ $1 = '-c' ]]; then
1418 _check=yes
1419 shift
1420 fi
1421
1422 if [[ $1 = '-s' ]]; then
1423 _silent=yes
1424 shift
1425 fi
1426
1427 function inst1mod() {
1428 local _ret=0 _mod="$1"
1429 case $_mod in
1430 =*)
1431 ( [[ "$_mpargs" ]] && echo $_mpargs
1432 find_kernel_modules_by_path "${_mod#=}" ) \
1433 | instmods
1434 ((_ret+=$?))
1435 ;;
1436 --*) _mpargs+=" $_mod" ;;
1437 *)
1438 _mod=${_mod##*/}
1439 # if we are already installed, skip this module and go on
1440 # to the next one.
1441 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1442 [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko" ]]; then
1443 read _ret <"$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko"
1444 return $_ret
1445 fi
1446
1447 _mod=${_mod/-/_}
1448 if [[ $omit_drivers ]] && [[ "$_mod" =~ $omit_drivers ]]; then
1449 dinfo "Omitting driver ${_mod##$srcmods}"
1450 return 0
1451 fi
1452
1453 # If we are building a host-specific initramfs and this
1454 # module is not already loaded, move on to the next one.
1455 [[ $hostonly ]] \
1456 && ! module_is_host_only "$_mod" \
1457 && return 0
1458
1459 if [[ "$_check" = "yes" ]] || ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
1460 # We use '-d' option in modprobe only if modules prefix path
1461 # differs from default '/'. This allows us to use dracut with
1462 # old version of modprobe which doesn't have '-d' option.
1463 local _moddirname=${srcmods%%/lib/modules/*}
1464 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1465
1466 # ok, load the module, all its dependencies, and any firmware
1467 # it may require
1468 for_each_kmod_dep install_kmod_with_fw $_mod \
1469 --set-version $kernel ${_moddirname} $_mpargs
1470 ((_ret+=$?))
1471 else
1472 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1473 echo $_mod >> "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist"
1474 fi
1475 ;;
1476 esac
1477 return $_ret
1478 }
1479
1480 function instmods_1() {
1481 local _mod _mpargs
1482 if (($# == 0)); then # filenames from stdin
1483 while read _mod; do
1484 inst1mod "${_mod%.ko*}" || {
1485 if [[ "$_check" == "yes" ]]; then
1486 [[ "$_silent" == "no" ]] && dfatal "Failed to install module $_mod"
1487 return 1
1488 fi
1489 }
1490 done
1491 fi
1492 while (($# > 0)); do # filenames as arguments
1493 inst1mod ${1%.ko*} || {
1494 if [[ "$_check" == "yes" ]]; then
1495 [[ "$_silent" == "no" ]] && dfatal "Failed to install module $1"
1496 return 1
1497 fi
1498 }
1499 shift
1500 done
1501 return 0
1502 }
1503
1504 local _ret _filter_not_found='FATAL: Module .* not found.'
1505 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1506 # redirections, but that would make dracut require bash4 at least.
1507 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1508 | while read line; do [[ "$line" =~ $_filter_not_found ]] || echo $line;done | derror
1509 _ret=$?
1510 return $_ret
1511 }
1512 # get_cpu_vendor
1513 # Only two values are returned: AMD or Intel
1514 get_cpu_vendor ()
1515 {
1516 if grep -qE AMD /proc/cpuinfo; then
1517 printf "AMD"
1518 fi
1519 if grep -qE Intel /proc/cpuinfo; then
1520 printf "Intel"
1521 fi
1522 }
1523
1524 # get_host_ucode
1525 # Get the hosts' ucode file based on the /proc/cpuinfo
1526 get_ucode_file ()
1527 {
1528 local family=`grep -E "cpu family" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1529 local model=`grep -E "model" /proc/cpuinfo |grep -v name | head -1 | sed s/.*:\ //`
1530 local stepping=`grep -E "stepping" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1531
1532 if [[ "$(get_cpu_vendor)" == "AMD" ]]; then
1533 # If family greater or equal than 0x15
1534 if [[ $family -ge 21 ]]; then
1535 printf "microcode_amd_fam15h.bin"
1536 else
1537 printf "microcode_amd.bin"
1538 fi
1539 fi
1540 if [[ "$(get_cpu_vendor)" == "Intel" ]]; then
1541 # The /proc/cpuinfo are in decimal.
1542 printf "%02x-%02x-%02x" ${family} ${model} ${stepping}
1543 fi
1544 }