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