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