]> git.ipfire.org Git - thirdparty/dracut.git/blob - dracut-functions.sh
dracut-functions.sh:get_persistent_dev(): use printf, instead of echo
[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 | grep -F '=>' | grep -E -v '/(lib|lib64|usr/lib|usr/lib64)/[^/]*$' | while read a b c d; do
113 d=${d%/*}
114 printf "%s\n" "$d";
115 done
116 ); do
117 a["$i"]=1;
118 done;
119 printf "%s\n" ${!a[@]}
120 }
121
122 # Detect lib paths
123 if ! [[ $libdirs ]] ; then
124 if [[ "$(ldd /bin/sh)" == */lib64/* ]] &>/dev/null \
125 && [[ -d /lib64 ]]; then
126 libdirs+=" /lib64"
127 [[ -d /usr/lib64 ]] && libdirs+=" /usr/lib64"
128 else
129 libdirs+=" /lib"
130 [[ -d /usr/lib ]] && libdirs+=" /usr/lib"
131 fi
132
133 libdirs+=" $(ldconfig_paths)"
134
135 export libdirs
136 fi
137
138 if ! [[ $kernel ]]; then
139 kernel=$(uname -r)
140 export kernel
141 fi
142
143 # Version comparision function. Assumes Linux style version scheme.
144 # $1 = version a
145 # $2 = comparision op (gt, ge, eq, le, lt, ne)
146 # $3 = version b
147 vercmp() {
148 local _n1=(${1//./ }) _op=$2 _n2=(${3//./ }) _i _res
149
150 for ((_i=0; ; _i++))
151 do
152 if [[ ! ${_n1[_i]}${_n2[_i]} ]]; then _res=0
153 elif ((${_n1[_i]:-0} > ${_n2[_i]:-0})); then _res=1
154 elif ((${_n1[_i]:-0} < ${_n2[_i]:-0})); then _res=2
155 else continue
156 fi
157 break
158 done
159
160 case $_op in
161 gt) ((_res == 1));;
162 ge) ((_res != 2));;
163 eq) ((_res == 0));;
164 le) ((_res != 1));;
165 lt) ((_res == 2));;
166 ne) ((_res != 0));;
167 esac
168 }
169
170 srcmods="/lib/modules/$kernel/"
171
172 [[ $drivers_dir ]] && {
173 if ! command -v kmod &>/dev/null && vercmp "$(modprobe --version | cut -d' ' -f3)" lt 3.7; then
174 dfatal 'To use --kmoddir option module-init-tools >= 3.7 is required.'
175 exit 1
176 fi
177 srcmods="$drivers_dir"
178 }
179 export srcmods
180
181 if ! type dinfo >/dev/null 2>&1; then
182 . "$dracutbasedir/dracut-logger.sh"
183 dlog_init
184 fi
185
186 if ! [[ $initdir ]]; then
187 dfatal "initdir not set"
188 exit 1
189 fi
190
191 # export standard hookdirs
192 [[ $hookdirs ]] || {
193 hookdirs="cmdline pre-udev pre-trigger netroot "
194 hookdirs+="initqueue initqueue/settled initqueue/online initqueue/finished initqueue/timeout "
195 hookdirs+="pre-mount pre-pivot cleanup mount "
196 hookdirs+="emergency shutdown-emergency pre-shutdown shutdown "
197 export hookdirs
198 }
199
200 dracut_need_initqueue() {
201 >"$initdir/lib/dracut/need-initqueue"
202 }
203
204 dracut_module_included() {
205 [[ " $mods_to_load $modules_loaded " == *\ $*\ * ]]
206 }
207
208 # Create all subdirectories for given path without creating the last element.
209 # $1 = path
210 mksubdirs() {
211 [[ -e ${1%/*} ]] || mkdir -m 0755 -p -- "${1%/*}"
212 }
213
214 # is_func <command>
215 # Check whether $1 is a function.
216 is_func() {
217 [[ "$(type -t "$1")" = "function" ]]
218 }
219
220 # Function prints global variables in format name=value line by line.
221 # $@ = list of global variables' name
222 print_vars() {
223 local _var _value
224
225 for _var in "$@"
226 do
227 eval printf -v _value "%s" \""\$$_var"\"
228 [[ ${_value} ]] && printf '%s="%s"\n' "$_var" "$_value"
229 done
230 }
231
232 # normalize_path <path>
233 # Prints the normalized path, where it removes any duplicated
234 # and trailing slashes.
235 # Example:
236 # $ normalize_path ///test/test//
237 # /test/test
238 normalize_path() {
239 shopt -q -s extglob
240 set -- "${1//+(\/)//}"
241 shopt -q -u extglob
242 printf "%s\n" "${1%/}"
243 }
244
245 # convert_abs_rel <from> <to>
246 # Prints the relative path, when creating a symlink to <to> from <from>.
247 # Example:
248 # $ convert_abs_rel /usr/bin/test /bin/test-2
249 # ../../bin/test-2
250 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
251 convert_abs_rel() {
252 local __current __absolute __abssize __cursize __newpath
253 local -i __i __level
254
255 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
256
257 # corner case #1 - self looping link
258 [[ "$1" == "$2" ]] && { printf "%s\n" "${1##*/}"; return; }
259
260 # corner case #2 - own dir link
261 [[ "${1%/*}" == "$2" ]] && { printf ".\n"; return; }
262
263 IFS="/" __current=($1)
264 IFS="/" __absolute=($2)
265
266 __abssize=${#__absolute[@]}
267 __cursize=${#__current[@]}
268
269 while [[ "${__absolute[__level]}" == "${__current[__level]}" ]]
270 do
271 (( __level++ ))
272 if (( __level > __abssize || __level > __cursize ))
273 then
274 break
275 fi
276 done
277
278 for ((__i = __level; __i < __cursize-1; __i++))
279 do
280 if ((__i > __level))
281 then
282 __newpath=$__newpath"/"
283 fi
284 __newpath=$__newpath".."
285 done
286
287 for ((__i = __level; __i < __abssize; __i++))
288 do
289 if [[ -n $__newpath ]]
290 then
291 __newpath=$__newpath"/"
292 fi
293 __newpath=$__newpath${__absolute[__i]}
294 done
295
296 printf "%s\n" "$__newpath"
297 }
298
299 if [[ "$(ln --help)" == *--relative* ]]; then
300 ln_r() {
301 ln -sfnr "${initdir}/$1" "${initdir}/$2"
302 }
303 else
304 ln_r() {
305 local _source=$1
306 local _dest=$2
307 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
308 ln -sfn -- "$(convert_abs_rel "${_dest}" "${_source}")" "${initdir}/${_dest}"
309 }
310 fi
311
312 # get_fs_env <device>
313 # Get and the ID_FS_TYPE variable from udev for a device.
314 # Example:
315 # $ get_fs_env /dev/sda2
316 # ext4
317 get_fs_env() {
318 local evalstr
319 local found
320
321 [[ $1 ]] || return
322 unset ID_FS_TYPE
323 ID_FS_TYPE=$(blkid -u filesystem -o export -- "$1" \
324 | while read line; do
325 if [[ "$line" == TYPE\=* ]]; then
326 printf "%s" "${line#TYPE=}";
327 exit 0;
328 fi
329 done)
330 if [[ $ID_FS_TYPE ]]; then
331 printf "%s" "$ID_FS_TYPE"
332 return 0
333 fi
334 return 1
335 }
336
337 # get_maj_min <device>
338 # Prints the major and minor of a device node.
339 # Example:
340 # $ get_maj_min /dev/sda2
341 # 8:2
342 get_maj_min() {
343 local _maj _min _majmin
344 _majmin="$(stat -L -c '%t:%T' "$1" 2>/dev/null)"
345 printf "%s" "$((0x${_majmin%:*})):$((0x${_majmin#*:}))"
346 }
347
348
349 # get_devpath_block <device>
350 # get the DEVPATH in /sys of a block device
351 get_devpath_block() {
352 local _majmin _i
353 _majmin=$(get_maj_min "$1")
354
355 for _i in /sys/block/*/dev /sys/block/*/*/dev; do
356 [[ -e "$_i" ]] || continue
357 if [[ "$_majmin" == "$(<"$_i")" ]]; then
358 printf "%s" "${_i%/dev}"
359 return 0
360 fi
361 done
362 return 1
363 }
364
365 # get a persistent path from a device
366 get_persistent_dev() {
367 local i _tmp _dev
368
369 _dev=$(get_maj_min "$1")
370 [ -z "$_dev" ] && return
371
372 for i in \
373 /dev/mapper/* \
374 /dev/disk/${persistent_policy:-by-uuid}/* \
375 /dev/disk/by-uuid/* \
376 /dev/disk/by-label/* \
377 /dev/disk/by-partuuid/* \
378 /dev/disk/by-partlabel/* \
379 /dev/disk/by-id/* \
380 /dev/disk/by-path/* \
381 ; do
382 [[ -e "$i" ]] || continue
383 [[ $i == /dev/mapper/control ]] && continue
384 [[ $i == /dev/mapper/mpath* ]] && continue
385 _tmp=$(get_maj_min "$i")
386 if [ "$_tmp" = "$_dev" ]; then
387 printf -- "%s" "$i"
388 return
389 fi
390 done
391 printf -- "%s" "$1"
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 local _hostonly_install
765 if [[ "$1" == "-H" ]]; then
766 _hostonly_install="-H"
767 shift
768 fi
769 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
770 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
771 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
772 }
773
774 inst_simple() {
775 local _hostonly_install
776 if [[ "$1" == "-H" ]]; then
777 _hostonly_install="-H"
778 shift
779 fi
780 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
781 [[ -e $1 ]] || return 1 # no source
782 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${_hostonly_install:+-H} "$@"
783 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${_hostonly_install:+-H} "$@" || :
784 }
785
786 inst_symlink() {
787 local _hostonly_install
788 if [[ "$1" == "-H" ]]; then
789 _hostonly_install="-H"
790 shift
791 fi
792 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
793 [[ -L $1 ]] || return 1
794 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
795 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
796 }
797
798 inst_multiple() {
799 local _ret
800 $DRACUT_INSTALL ${initdir:+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
801 _ret=$?
802 (($_ret != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
803 return $_ret
804 }
805
806 dracut_install() {
807 inst_multiple "$@"
808 }
809
810 inst_library() {
811 local _hostonly_install
812 if [[ "$1" == "-H" ]]; then
813 _hostonly_install="-H"
814 shift
815 fi
816 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
817 [[ -e $1 ]] || return 1 # no source
818 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
819 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
820 }
821
822 inst_binary() {
823 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
824 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@" || :
825 }
826
827 inst_script() {
828 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
829 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@" || :
830 }
831
832 mark_hostonly() {
833 for i in "$@"; do
834 echo "$i" >> "$initdir/lib/dracut/hostonly-files"
835 done
836 }
837
838 # find symlinks linked to given library file
839 # $1 = library file
840 # Function searches for symlinks by stripping version numbers appended to
841 # library filename, checks if it points to the same target and finally
842 # prints the list of symlinks to stdout.
843 #
844 # Example:
845 # rev_lib_symlinks libfoo.so.8.1
846 # output: libfoo.so.8 libfoo.so
847 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
848 rev_lib_symlinks() {
849 [[ ! $1 ]] && return 0
850
851 local fn="$1" orig="$(readlink -f "$1")" links=''
852
853 [[ ${fn} == *.so.* ]] || return 1
854
855 until [[ ${fn##*.} == so ]]; do
856 fn="${fn%.*}"
857 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
858 done
859
860 echo "${links}"
861 }
862
863 # attempt to install any programs specified in a udev rule
864 inst_rule_programs() {
865 local _prog _bin
866
867 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
868 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
869 _bin=""
870 if [ -x ${udevdir}/$_prog ]; then
871 _bin=${udevdir}/$_prog
872 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
873 _bin=$(find_binary "$_prog") || {
874 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
875 continue;
876 }
877 fi
878
879 [[ $_bin ]] && inst_binary "$_bin"
880 done
881 fi
882 if grep -qE 'RUN[+=]=?"[^ "]+' "$1"; then
883 for _prog in $(grep -E 'RUN[+=]=?"[^ "]+' "$1" | sed -r 's/.*RUN[+=]=?"([^ "]+).*/\1/'); do
884 _bin=""
885 if [ -x ${udevdir}/$_prog ]; then
886 _bin=${udevdir}/$_prog
887 elif [[ "${_prog/\$env\{/}" == "$_prog" ]] && [[ "${_prog}" != "/sbin/initqueue" ]]; then
888 _bin=$(find_binary "$_prog") || {
889 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
890 continue;
891 }
892 fi
893
894 [[ $_bin ]] && inst_binary "$_bin"
895 done
896 fi
897 if grep -qE 'IMPORT\{program\}==?"[^ "]+' "$1"; then
898 for _prog in $(grep -E 'IMPORT\{program\}==?"[^ "]+' "$1" | sed -r 's/.*IMPORT\{program\}==?"([^ "]+).*/\1/'); do
899 _bin=""
900 if [ -x ${udevdir}/$_prog ]; then
901 _bin=${udevdir}/$_prog
902 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
903 _bin=$(find_binary "$_prog") || {
904 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
905 continue;
906 }
907 fi
908
909 [[ $_bin ]] && dracut_install "$_bin"
910 done
911 fi
912 }
913
914 # attempt to install any programs specified in a udev rule
915 inst_rule_group_owner() {
916 local i
917
918 if grep -qE 'OWNER=?"[^ "]+' "$1"; then
919 for i in $(grep -E 'OWNER=?"[^ "]+' "$1" | sed -r 's/.*OWNER=?"([^ "]+).*/\1/'); do
920 if ! egrep -q "^$i:" "$initdir/etc/passwd" 2>/dev/null; then
921 egrep "^$i:" /etc/passwd 2>/dev/null >> "$initdir/etc/passwd"
922 fi
923 done
924 fi
925 if grep -qE 'GROUP=?"[^ "]+' "$1"; then
926 for i in $(grep -E 'GROUP=?"[^ "]+' "$1" | sed -r 's/.*GROUP=?"([^ "]+).*/\1/'); do
927 if ! egrep -q "^$i:" "$initdir/etc/group" 2>/dev/null; then
928 egrep "^$i:" /etc/group 2>/dev/null >> "$initdir/etc/group"
929 fi
930 done
931 fi
932 }
933
934 inst_rule_initqueue() {
935 if grep -q -F initqueue "$1"; then
936 dracut_need_initqueue
937 fi
938 }
939
940 # udev rules always get installed in the same place, so
941 # create a function to install them to make life simpler.
942 inst_rules() {
943 local _target=/etc/udev/rules.d _rule _found
944
945 inst_dir "${udevdir}/rules.d"
946 inst_dir "$_target"
947 for _rule in "$@"; do
948 if [ "${_rule#/}" = "$_rule" ]; then
949 for r in ${udevdir}/rules.d ${hostonly:+/etc/udev/rules.d}; do
950 if [[ -e $r/$_rule ]]; then
951 _found="$r/$_rule"
952 inst_rule_programs "$_found"
953 inst_rule_group_owner "$_found"
954 inst_rule_initqueue "$_found"
955 inst_simple "$_found"
956 fi
957 done
958 fi
959 for r in '' $dracutbasedir/rules.d/; do
960 # skip rules without an absolute path
961 [[ "${r}$_rule" != /* ]] && continue
962
963 if [[ -f ${r}$_rule ]]; then
964 _found="${r}$_rule"
965 inst_rule_programs "$_found"
966 inst_rule_group_owner "$_found"
967 inst_rule_initqueue "$_found"
968 inst_simple "$_found" "$_target/${_found##*/}"
969 fi
970 done
971 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
972 done
973 }
974
975 inst_rules_wildcard() {
976 local _target=/etc/udev/rules.d _rule _found
977
978 inst_dir "${udevdir}/rules.d"
979 inst_dir "$_target"
980 for _rule in ${udevdir}/rules.d/$1 ${dracutbasedir}/rules.d/$1 ; do
981 if [[ -e $_rule ]]; then
982 inst_rule_programs "$_rule"
983 inst_rule_group_owner "$_rule"
984 inst_rule_initqueue "$_rule"
985 inst_simple "$_rule"
986 _found=$_rule
987 fi
988 done
989 if [ -n ${hostonly} ] ; then
990 for _rule in ${_target}/$1 ; do
991 if [[ -f $_rule ]]; then
992 inst_rule_programs "$_rule"
993 inst_rule_group_owner "$_rule"
994 inst_rule_initqueue "$_rule"
995 inst_simple "$_rule"
996 _found=$_rule
997 fi
998 done
999 fi
1000 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
1001 }
1002
1003 prepare_udev_rules() {
1004 [ -z "$UDEVVERSION" ] && export UDEVVERSION=$(udevadm --version)
1005
1006 for f in "$@"; do
1007 f="${initdir}/etc/udev/rules.d/$f"
1008 [ -e "$f" ] || continue
1009 while read line; do
1010 if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
1011 if [ $UDEVVERSION -ge 174 ]; then
1012 printf '%sIMPORT{builtin}="path_id"\n' "${line%%IMPORT PATH_ID}"
1013 else
1014 printf '%sIMPORT{program}="path_id %%p"\n' "${line%%IMPORT PATH_ID}"
1015 fi
1016 elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
1017 if [ $UDEVVERSION -ge 176 ]; then
1018 printf '%sIMPORT{builtin}="blkid"\n' "${line%%IMPORT BLKID}"
1019 else
1020 printf '%sIMPORT{program}="/sbin/blkid -o udev -p $tempnode"\n' "${line%%IMPORT BLKID}"
1021 fi
1022 else
1023 echo "$line"
1024 fi
1025 done < "${f}" > "${f}.new"
1026 mv "${f}.new" "$f"
1027 done
1028 }
1029
1030 # install function specialized for hooks
1031 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
1032 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
1033 inst_hook() {
1034 if ! [[ -f $3 ]]; then
1035 dfatal "Cannot install a hook ($3) that does not exist."
1036 dfatal "Aborting initrd creation."
1037 exit 1
1038 elif ! [[ "$hookdirs" == *$1* ]]; then
1039 dfatal "No such hook type $1. Aborting initrd creation."
1040 exit 1
1041 fi
1042 inst_simple "$3" "/lib/dracut/hooks/${1}/${2}-${3##*/}"
1043 }
1044
1045 # install any of listed files
1046 #
1047 # If first argument is '-d' and second some destination path, first accessible
1048 # source is installed into this path, otherwise it will installed in the same
1049 # path as source. If none of listed files was installed, function return 1.
1050 # On first successful installation it returns with 0 status.
1051 #
1052 # Example:
1053 #
1054 # inst_any -d /bin/foo /bin/bar /bin/baz
1055 #
1056 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
1057 # initramfs.
1058 inst_any() {
1059 local to f
1060
1061 [[ $1 = '-d' ]] && to="$2" && shift 2
1062
1063 for f in "$@"; do
1064 if [[ -e $f ]]; then
1065 [[ $to ]] && inst "$f" "$to" && return 0
1066 inst "$f" && return 0
1067 fi
1068 done
1069
1070 return 1
1071 }
1072
1073
1074 # inst_libdir_file [-n <pattern>] <file> [<file>...]
1075 # Install a <file> located on a lib directory to the initramfs image
1076 # -n <pattern> install matching files
1077 inst_libdir_file() {
1078 local _files
1079 if [[ "$1" == "-n" ]]; then
1080 local _pattern=$2
1081 shift 2
1082 for _dir in $libdirs; do
1083 for _i in "$@"; do
1084 for _f in "$_dir"/$_i; do
1085 [[ "$_f" =~ $_pattern ]] || continue
1086 [[ -e "$_f" ]] && _files+="$_f "
1087 done
1088 done
1089 done
1090 else
1091 for _dir in $libdirs; do
1092 for _i in "$@"; do
1093 for _f in "$_dir"/$_i; do
1094 [[ -e "$_f" ]] && _files+="$_f "
1095 done
1096 done
1097 done
1098 fi
1099 [[ $_files ]] && inst_multiple $_files
1100 }
1101
1102
1103 # install function decompressing the target and handling symlinks
1104 # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
1105 #
1106 # Function install targets in the same paths inside overlay but decompressed
1107 # and without extensions (.gz, .bz2).
1108 inst_decompress() {
1109 local _src _cmd
1110
1111 for _src in $@
1112 do
1113 case ${_src} in
1114 *.gz) _cmd='gzip -f -d' ;;
1115 *.bz2) _cmd='bzip2 -d' ;;
1116 *) return 1 ;;
1117 esac
1118 inst_simple ${_src}
1119 # Decompress with chosen tool. We assume that tool changes name e.g.
1120 # from 'name.gz' to 'name'.
1121 ${_cmd} "${initdir}${_src}"
1122 done
1123 }
1124
1125 # It's similar to above, but if file is not compressed, performs standard
1126 # install.
1127 # $@ = list of files
1128 inst_opt_decompress() {
1129 local _src
1130
1131 for _src in $@
1132 do
1133 inst_decompress "${_src}" || inst "${_src}"
1134 done
1135 }
1136
1137 # module_check <dracut module>
1138 # execute the check() function of module-setup.sh of <dracut module>
1139 # or the "check" script, if module-setup.sh is not found
1140 # "check $hostonly" is called
1141 module_check() {
1142 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1143 local _ret
1144 local _forced=0
1145 local _hostonly=$hostonly
1146 [ $# -eq 2 ] && _forced=$2
1147 [[ -d $_moddir ]] || return 1
1148 if [[ ! -f $_moddir/module-setup.sh ]]; then
1149 # if we do not have a check script, we are unconditionally included
1150 [[ -x $_moddir/check ]] || return 0
1151 [ $_forced -ne 0 ] && unset hostonly
1152 $_moddir/check $hostonly
1153 _ret=$?
1154 else
1155 unset check depends cmdline install installkernel
1156 check() { true; }
1157 . $_moddir/module-setup.sh
1158 is_func check || return 0
1159 [ $_forced -ne 0 ] && unset hostonly
1160 moddir=$_moddir check $hostonly
1161 _ret=$?
1162 unset check depends cmdline install installkernel
1163 fi
1164 hostonly=$_hostonly
1165 return $_ret
1166 }
1167
1168 # module_check_mount <dracut module>
1169 # execute the check() function of module-setup.sh of <dracut module>
1170 # or the "check" script, if module-setup.sh is not found
1171 # "mount_needs=1 check 0" is called
1172 module_check_mount() {
1173 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1174 local _ret
1175 mount_needs=1
1176 [[ -d $_moddir ]] || return 1
1177 if [[ ! -f $_moddir/module-setup.sh ]]; then
1178 # if we do not have a check script, we are unconditionally included
1179 [[ -x $_moddir/check ]] || return 0
1180 mount_needs=1 $_moddir/check 0
1181 _ret=$?
1182 else
1183 unset check depends cmdline install installkernel
1184 check() { false; }
1185 . $_moddir/module-setup.sh
1186 moddir=$_moddir check 0
1187 _ret=$?
1188 unset check depends cmdline install installkernel
1189 fi
1190 unset mount_needs
1191 return $_ret
1192 }
1193
1194 # module_depends <dracut module>
1195 # execute the depends() function of module-setup.sh of <dracut module>
1196 # or the "depends" script, if module-setup.sh is not found
1197 module_depends() {
1198 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1199 local _ret
1200 [[ -d $_moddir ]] || return 1
1201 if [[ ! -f $_moddir/module-setup.sh ]]; then
1202 # if we do not have a check script, we have no deps
1203 [[ -x $_moddir/check ]] || return 0
1204 $_moddir/check -d
1205 return $?
1206 else
1207 unset check depends cmdline install installkernel
1208 depends() { true; }
1209 . $_moddir/module-setup.sh
1210 moddir=$_moddir depends
1211 _ret=$?
1212 unset check depends cmdline install installkernel
1213 return $_ret
1214 fi
1215 }
1216
1217 # module_cmdline <dracut module>
1218 # execute the cmdline() function of module-setup.sh of <dracut module>
1219 # or the "cmdline" script, if module-setup.sh is not found
1220 module_cmdline() {
1221 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1222 local _ret
1223 [[ -d $_moddir ]] || return 1
1224 if [[ ! -f $_moddir/module-setup.sh ]]; then
1225 [[ -x $_moddir/cmdline ]] && . "$_moddir/cmdline"
1226 return $?
1227 else
1228 unset check depends cmdline install installkernel
1229 cmdline() { true; }
1230 . $_moddir/module-setup.sh
1231 moddir=$_moddir cmdline
1232 _ret=$?
1233 unset check depends cmdline install installkernel
1234 return $_ret
1235 fi
1236 }
1237
1238 # module_install <dracut module>
1239 # execute the install() function of module-setup.sh of <dracut module>
1240 # or the "install" script, if module-setup.sh is not found
1241 module_install() {
1242 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1243 local _ret
1244 [[ -d $_moddir ]] || return 1
1245 if [[ ! -f $_moddir/module-setup.sh ]]; then
1246 [[ -x $_moddir/install ]] && . "$_moddir/install"
1247 return $?
1248 else
1249 unset check depends cmdline install installkernel
1250 install() { true; }
1251 . $_moddir/module-setup.sh
1252 moddir=$_moddir install
1253 _ret=$?
1254 unset check depends cmdline install installkernel
1255 return $_ret
1256 fi
1257 }
1258
1259 # module_installkernel <dracut module>
1260 # execute the installkernel() function of module-setup.sh of <dracut module>
1261 # or the "installkernel" script, if module-setup.sh is not found
1262 module_installkernel() {
1263 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1264 local _ret
1265 [[ -d $_moddir ]] || return 1
1266 if [[ ! -f $_moddir/module-setup.sh ]]; then
1267 [[ -x $_moddir/installkernel ]] && . "$_moddir/installkernel"
1268 return $?
1269 else
1270 unset check depends cmdline install installkernel
1271 installkernel() { true; }
1272 . $_moddir/module-setup.sh
1273 moddir=$_moddir installkernel
1274 _ret=$?
1275 unset check depends cmdline install installkernel
1276 return $_ret
1277 fi
1278 }
1279
1280 # check_mount <dracut module>
1281 # check_mount checks, if a dracut module is needed for the given
1282 # device and filesystem types in "${host_fs_types[@]}"
1283 check_mount() {
1284 local _mod=$1
1285 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1286 local _ret
1287 local _moddep
1288
1289 [ "${#host_fs_types[*]}" -le 0 ] && return 1
1290
1291 # If we are already scheduled to be loaded, no need to check again.
1292 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
1293 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
1294
1295 # This should never happen, but...
1296 [[ -d $_moddir ]] || return 1
1297
1298 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
1299
1300 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
1301 return 1
1302 fi
1303
1304 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
1305 module_check_mount $_mod; ret=$?
1306
1307 # explicit module, so also accept ret=255
1308 [[ $ret = 0 || $ret = 255 ]] || return 1
1309 else
1310 # module not in our list
1311 if [[ $dracutmodules = all ]]; then
1312 # check, if we can and should install this module
1313 module_check_mount $_mod || return 1
1314 else
1315 # skip this module
1316 return 1
1317 fi
1318 fi
1319
1320
1321 for _moddep in $(module_depends $_mod); do
1322 # handle deps as if they were manually added
1323 [[ " $add_dracutmodules " == *\ $_moddep\ * ]] || \
1324 add_dracutmodules+=" $_moddep "
1325 [[ " $force_add_dracutmodules " == *\ $_moddep\ * ]] || \
1326 force_add_dracutmodules+=" $_moddep "
1327 # if a module we depend on fail, fail also
1328 if ! check_module $_moddep; then
1329 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
1330 return 1
1331 fi
1332 done
1333
1334 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
1335 mods_to_load+=" $_mod "
1336
1337 return 0
1338 }
1339
1340 # check_module <dracut module> [<use_as_dep>]
1341 # check if a dracut module is to be used in the initramfs process
1342 # if <use_as_dep> is set, then the process also keeps track
1343 # that the modules were checked for the dependency tracking process
1344 check_module() {
1345 local _mod=$1
1346 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1347 local _ret
1348 local _moddep
1349 # If we are already scheduled to be loaded, no need to check again.
1350 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
1351 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
1352
1353 # This should never happen, but...
1354 [[ -d $_moddir ]] || return 1
1355
1356 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
1357
1358 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
1359 dinfo "dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
1360 return 1
1361 fi
1362
1363 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
1364 if [[ " $force_add_dracutmodules " == *\ $_mod\ * ]]; then
1365 module_check $_mod 1; ret=$?
1366 else
1367 module_check $_mod 0; ret=$?
1368 fi
1369 # explicit module, so also accept ret=255
1370 [[ $ret = 0 || $ret = 255 ]] || return 1
1371 else
1372 # module not in our list
1373 if [[ $dracutmodules = all ]]; then
1374 # check, if we can and should install this module
1375 module_check $_mod || return 1
1376 else
1377 # skip this module
1378 return 1
1379 fi
1380 fi
1381
1382 for _moddep in $(module_depends $_mod); do
1383 # handle deps as if they were manually added
1384 [[ " $add_dracutmodules " == *\ $_moddep\ * ]] || \
1385 add_dracutmodules+=" $_moddep "
1386 [[ " $force_add_dracutmodules " == *\ $_moddep\ * ]] || \
1387 force_add_dracutmodules+=" $_moddep "
1388 # if a module we depend on fail, fail also
1389 if ! check_module $_moddep; then
1390 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
1391 return 1
1392 fi
1393 done
1394
1395 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
1396 mods_to_load+=" $_mod "
1397
1398 return 0
1399 }
1400
1401 # for_each_module_dir <func>
1402 # execute "<func> <dracut module> 1"
1403 for_each_module_dir() {
1404 local _modcheck
1405 local _mod
1406 local _moddir
1407 local _func
1408 _func=$1
1409 for _moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1410 [[ -d $_moddir ]] || continue;
1411 [[ -e $_moddir/install || -e $_moddir/installkernel || \
1412 -e $_moddir/module-setup.sh ]] || continue
1413 _mod=${_moddir##*/}; _mod=${_mod#[0-9][0-9]}
1414 $_func $_mod 1
1415 done
1416
1417 # Report any missing dracut modules, the user has specified
1418 _modcheck="$add_dracutmodules $force_add_dracutmodules"
1419 [[ $dracutmodules != all ]] && _modcheck="$m $dracutmodules"
1420 for _mod in $_modcheck; do
1421 [[ " $mods_to_load " == *\ $_mod\ * ]] && continue
1422 [[ " $omit_dracutmodules " == *\ $_mod\ * ]] && continue
1423 derror "dracut module '$_mod' cannot be found or installed."
1424 done
1425 }
1426
1427 # Install a single kernel module along with any firmware it may require.
1428 # $1 = full path to kernel module to install
1429 install_kmod_with_fw() {
1430 # no need to go further if the module is already installed
1431
1432 [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
1433 && return 0
1434
1435 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -e "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}" ]]; then
1436 read ret < "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
1437 return $ret
1438 fi
1439
1440 if [[ $omit_drivers ]]; then
1441 local _kmod=${1##*/}
1442 _kmod=${_kmod%.ko}
1443 _kmod=${_kmod/-/_}
1444 if [[ "$_kmod" =~ $omit_drivers ]]; then
1445 dinfo "Omitting driver $_kmod"
1446 return 0
1447 fi
1448 if [[ "${1##*/lib/modules/$kernel/}" =~ $omit_drivers ]]; then
1449 dinfo "Omitting driver $_kmod"
1450 return 0
1451 fi
1452 fi
1453
1454 if [[ $silent_omit_drivers ]]; then
1455 local _kmod=${1##*/}
1456 _kmod=${_kmod%.ko}
1457 _kmod=${_kmod/-/_}
1458 [[ "$_kmod" =~ $silent_omit_drivers ]] && return 0
1459 [[ "${1##*/lib/modules/$kernel/}" =~ $silent_omit_drivers ]] && return 0
1460 fi
1461
1462 inst_simple "$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
1463 ret=$?
1464 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1465 [[ -d "$DRACUT_KERNEL_LAZY_HASHDIR" ]] && \
1466 echo $ret > "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
1467 (($ret != 0)) && return $ret
1468
1469 local _modname=${1##*/} _fwdir _found _fw
1470 _modname=${_modname%.ko*}
1471 for _fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do
1472 _found=''
1473 for _fwdir in $fw_dir; do
1474 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1475 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1476 _found=yes
1477 fi
1478 done
1479 if [[ $_found != yes ]]; then
1480 if ! [[ -d $(echo /sys/module/${_modname//-/_}|{ read a b; echo $a; }) ]]; then
1481 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
1482 "\"${_modname}.ko\""
1483 else
1484 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
1485 "\"${_modname}.ko\""
1486 fi
1487 fi
1488 done
1489 return 0
1490 }
1491
1492 # Do something with all the dependencies of a kernel module.
1493 # Note that kernel modules depend on themselves using the technique we use
1494 # $1 = function to call for each dependency we find
1495 # It will be passed the full path to the found kernel module
1496 # $2 = module to get dependencies for
1497 # rest of args = arguments to modprobe
1498 # _fderr specifies FD passed from surrounding scope
1499 for_each_kmod_dep() {
1500 local _func=$1 _kmod=$2 _cmd _modpath _options
1501 shift 2
1502 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
1503 while read _cmd _modpath _options; do
1504 [[ $_cmd = insmod ]] || continue
1505 $_func ${_modpath} || exit $?
1506 done
1507 )
1508 }
1509
1510 dracut_kernel_post() {
1511 local _moddirname=${srcmods%%/lib/modules/*}
1512 local _pid
1513
1514 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" ]]; then
1515 xargs -r modprobe -a ${_moddirname:+-d ${_moddirname}/} \
1516 --ignore-install --show-depends --set-version $kernel \
1517 < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" 2>/dev/null \
1518 | sort -u \
1519 | while read _cmd _modpath _options; do
1520 [[ $_cmd = insmod ]] || continue
1521 echo "$_modpath"
1522 done > "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1523
1524 (
1525 if [[ $DRACUT_INSTALL ]] && [[ -z $_moddirname ]]; then
1526 xargs -r $DRACUT_INSTALL ${initdir:+-D "$initdir"} -a < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1527 else
1528 while read _modpath; do
1529 local _destpath=$_modpath
1530 [[ $_moddirname ]] && _destpath=${_destpath##$_moddirname/}
1531 _destpath=${_destpath##*/lib/modules/$kernel/}
1532 inst_simple "$_modpath" "/lib/modules/$kernel/${_destpath}" || exit $?
1533 done < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1534 fi
1535 ) &
1536 _pid=$(jobs -p | while read a ; do printf ":$a";done)
1537 _pid=${_pid##*:}
1538
1539 if [[ $DRACUT_INSTALL ]]; then
1540 xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" \
1541 | while read line; do
1542 for _fwdir in $fw_dir; do
1543 echo $_fwdir/$line;
1544 done;
1545 done | xargs -r $DRACUT_INSTALL ${initdir:+-D "$initdir"} -a -o
1546 else
1547 for _fw in $(xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"); do
1548 for _fwdir in $fw_dir; do
1549 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1550 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1551 break
1552 fi
1553 done
1554 done
1555 fi
1556
1557 wait $_pid
1558 fi
1559
1560 for _f in modules.builtin.bin modules.builtin modules.order; do
1561 [[ $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f"
1562 done
1563
1564 # generate module dependencies for the initrd
1565 if [[ -d $initdir/lib/modules/$kernel ]] && \
1566 ! depmod -a -b "$initdir" $kernel; then
1567 dfatal "\"depmod -a $kernel\" failed."
1568 exit 1
1569 fi
1570
1571 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && rm -fr -- "$DRACUT_KERNEL_LAZY_HASHDIR"
1572 }
1573
1574 [[ "$kernel_current" ]] || export kernel_current=$(uname -r)
1575
1576 module_is_host_only() {
1577 local _mod=$1
1578 local _modenc a i _k _s _v _aliases
1579 _mod=${_mod##*/}
1580 _mod=${_mod%.ko}
1581 _modenc=${_mod//-/_}
1582
1583 [[ " $add_drivers " == *\ ${_mod}\ * ]] && return 0
1584
1585 # check if module is loaded
1586 [[ ${host_modules["$_modenc"]} ]] && return 0
1587
1588 [[ "$kernel_current" ]] || export kernel_current=$(uname -r)
1589
1590 if [[ "$kernel_current" != "$kernel" ]]; then
1591 # check if module is loadable on the current kernel
1592 # this covers the case, where a new module is introduced
1593 # or a module was renamed
1594 # or a module changed from builtin to a module
1595
1596 if [[ -d /lib/modules/$kernel_current ]]; then
1597 # if the modinfo can be parsed, but the module
1598 # is not loaded, then we can safely return 1
1599 modinfo -F filename "$_mod" &>/dev/null && return 1
1600 fi
1601
1602 _aliases=$(modinfo -k $kernel -F alias $_mod 2>/dev/null)
1603
1604 # if the module has no aliases, install it
1605 [[ $_aliases ]] || return 0
1606
1607 # finally check all modalias
1608 for a in $_aliases; do
1609 for i in "${!host_modalias[@]}"; do
1610 [[ $i == $a ]] && return 0
1611 done
1612 done
1613
1614 fi
1615
1616 return 1
1617 }
1618
1619 find_kernel_modules_by_path () {
1620 local _OLDIFS
1621
1622 [[ -f "$srcmods/modules.dep" ]] || return 0
1623
1624 _OLDIFS=$IFS
1625 IFS=:
1626 while read a rest; do
1627 [[ $a = */$1/* ]] || [[ $a = updates/* ]] || continue
1628 printf "%s\n" "$srcmods/$a"
1629 done < "$srcmods/modules.dep"
1630 IFS=$_OLDIFS
1631 return 0
1632 }
1633
1634 find_kernel_modules () {
1635 find_kernel_modules_by_path drivers
1636 }
1637
1638 # instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1639 # instmods [-c [-s]] <kernel subsystem>
1640 # install kernel modules along with all their dependencies.
1641 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1642 instmods() {
1643 [[ $no_kernel = yes ]] && return
1644 # called [sub]functions inherit _fderr
1645 local _fderr=9
1646 local _check=no
1647 local _silent=no
1648 if [[ $1 = '-c' ]]; then
1649 _check=yes
1650 shift
1651 fi
1652
1653 if [[ $1 = '-s' ]]; then
1654 _silent=yes
1655 shift
1656 fi
1657
1658 function inst1mod() {
1659 local _ret=0 _mod="$1"
1660 case $_mod in
1661 =*)
1662 ( [[ "$_mpargs" ]] && echo $_mpargs
1663 find_kernel_modules_by_path "${_mod#=}" ) \
1664 | instmods
1665 ((_ret+=$?))
1666 ;;
1667 --*) _mpargs+=" $_mod" ;;
1668 *)
1669 _mod=${_mod##*/}
1670 # if we are already installed, skip this module and go on
1671 # to the next one.
1672 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1673 [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko" ]]; then
1674 read _ret <"$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko"
1675 return $_ret
1676 fi
1677
1678 _mod=${_mod/-/_}
1679 if [[ $omit_drivers ]] && [[ "$_mod" =~ $omit_drivers ]]; then
1680 dinfo "Omitting driver ${_mod##$srcmods}"
1681 return 0
1682 fi
1683
1684 # If we are building a host-specific initramfs and this
1685 # module is not already loaded, move on to the next one.
1686 [[ $hostonly ]] \
1687 && ! module_is_host_only "$_mod" \
1688 && return 0
1689
1690 if [[ "$_check" = "yes" ]] || ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
1691 # We use '-d' option in modprobe only if modules prefix path
1692 # differs from default '/'. This allows us to use dracut with
1693 # old version of modprobe which doesn't have '-d' option.
1694 local _moddirname=${srcmods%%/lib/modules/*}
1695 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1696
1697 # ok, load the module, all its dependencies, and any firmware
1698 # it may require
1699 for_each_kmod_dep install_kmod_with_fw $_mod \
1700 --set-version $kernel ${_moddirname} $_mpargs
1701 ((_ret+=$?))
1702 else
1703 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1704 echo $_mod >> "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist"
1705 fi
1706 ;;
1707 esac
1708 return $_ret
1709 }
1710
1711 function instmods_1() {
1712 local _mod _mpargs
1713 if (($# == 0)); then # filenames from stdin
1714 while read _mod; do
1715 inst1mod "${_mod%.ko*}" || {
1716 if [[ "$_check" == "yes" ]]; then
1717 [[ "$_silent" == "no" ]] && dfatal "Failed to install module $_mod"
1718 return 1
1719 fi
1720 }
1721 done
1722 fi
1723 while (($# > 0)); do # filenames as arguments
1724 inst1mod ${1%.ko*} || {
1725 if [[ "$_check" == "yes" ]]; then
1726 [[ "$_silent" == "no" ]] && dfatal "Failed to install module $1"
1727 return 1
1728 fi
1729 }
1730 shift
1731 done
1732 return 0
1733 }
1734
1735 local _ret _filter_not_found='FATAL: Module .* not found.'
1736 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1737 # redirections, but that would make dracut require bash4 at least.
1738 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1739 | while read line; do [[ "$line" =~ $_filter_not_found ]] || echo $line;done | derror
1740 _ret=$?
1741 return $_ret
1742 }
1743 # get_cpu_vendor
1744 # Only two values are returned: AMD or Intel
1745 get_cpu_vendor ()
1746 {
1747 if grep -qE AMD /proc/cpuinfo; then
1748 printf "AMD"
1749 fi
1750 if grep -qE Intel /proc/cpuinfo; then
1751 printf "Intel"
1752 fi
1753 }
1754
1755 # get_host_ucode
1756 # Get the hosts' ucode file based on the /proc/cpuinfo
1757 get_ucode_file ()
1758 {
1759 local family=`grep -E "cpu family" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1760 local model=`grep -E "model" /proc/cpuinfo |grep -v name | head -1 | sed s/.*:\ //`
1761 local stepping=`grep -E "stepping" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1762
1763 if [[ "$(get_cpu_vendor)" == "AMD" ]]; then
1764 # If family greater or equal than 0x15
1765 if [[ $family -ge 21 ]]; then
1766 printf "microcode_amd_fam15h.bin"
1767 else
1768 printf "microcode_amd.bin"
1769 fi
1770 fi
1771 if [[ "$(get_cpu_vendor)" == "Intel" ]]; then
1772 # The /proc/cpuinfo are in decimal.
1773 printf "%02x-%02x-%02x" ${family} ${model} ${stepping}
1774 fi
1775 }
1776
1777 # Not every device in /dev/mapper should be examined.
1778 # If it is an LVM device, touch only devices which have /dev/VG/LV symlink.
1779 lvm_internal_dev() {
1780 local dev_dm_dir=/sys/dev/block/$1/dm
1781 [[ ! -f $dev_dm_dir/uuid || $(<$dev_dm_dir/uuid) != LVM-* ]] && return 1 # Not an LVM device
1782 local DM_VG_NAME DM_LV_NAME DM_LV_LAYER
1783 eval $(dmsetup splitname --nameprefixes --noheadings --rows "$(<$dev_dm_dir/name)" 2>/dev/null)
1784 [[ ${DM_VG_NAME} ]] && [[ ${DM_LV_NAME} ]] || return 0 # Better skip this!
1785 [[ ${DM_LV_LAYER} ]] || [[ ! -L /dev/${DM_VG_NAME}/${DM_LV_NAME} ]]
1786 }
1787