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