]> git.ipfire.org Git - thirdparty/dracut.git/blob - dracut-functions.sh
install: add -H flag for install
[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:+-f} "$@"
767 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@" || :
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:+-f} "$@"
781 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@" || :
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:+-f} "$@"
788 ret=$?
789 (($ret != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@" || :
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:+-f} "$@"
801 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@" || :
802 }
803
804 inst_binary() {
805 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
806 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@" || :
807 }
808
809 inst_script() {
810 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
811 (($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@" || :
812 }
813
814 mark_hostonly() {
815 for i in "$@"; do
816 echo "$i" >> "$initdir/lib/dracut/hostonly-files"
817 done
818 }
819
820 # find symlinks linked to given library file
821 # $1 = library file
822 # Function searches for symlinks by stripping version numbers appended to
823 # library filename, checks if it points to the same target and finally
824 # prints the list of symlinks to stdout.
825 #
826 # Example:
827 # rev_lib_symlinks libfoo.so.8.1
828 # output: libfoo.so.8 libfoo.so
829 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
830 rev_lib_symlinks() {
831 [[ ! $1 ]] && return 0
832
833 local fn="$1" orig="$(readlink -f "$1")" links=''
834
835 [[ ${fn} == *.so.* ]] || return 1
836
837 until [[ ${fn##*.} == so ]]; do
838 fn="${fn%.*}"
839 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
840 done
841
842 echo "${links}"
843 }
844
845 # attempt to install any programs specified in a udev rule
846 inst_rule_programs() {
847 local _prog _bin
848
849 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
850 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
851 _bin=""
852 if [ -x ${udevdir}/$_prog ]; then
853 _bin=${udevdir}/$_prog
854 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
855 _bin=$(find_binary "$_prog") || {
856 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
857 continue;
858 }
859 fi
860
861 [[ $_bin ]] && inst_binary "$_bin"
862 done
863 fi
864 if grep -qE 'RUN[+=]=?"[^ "]+' "$1"; then
865 for _prog in $(grep -E 'RUN[+=]=?"[^ "]+' "$1" | sed -r 's/.*RUN[+=]=?"([^ "]+).*/\1/'); do
866 _bin=""
867 if [ -x ${udevdir}/$_prog ]; then
868 _bin=${udevdir}/$_prog
869 elif [[ "${_prog/\$env\{/}" == "$_prog" ]] && [[ "${_prog}" != "/sbin/initqueue" ]]; then
870 _bin=$(find_binary "$_prog") || {
871 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
872 continue;
873 }
874 fi
875
876 [[ $_bin ]] && inst_binary "$_bin"
877 done
878 fi
879 if grep -qE 'IMPORT\{program\}==?"[^ "]+' "$1"; then
880 for _prog in $(grep -E 'IMPORT\{program\}==?"[^ "]+' "$1" | sed -r 's/.*IMPORT\{program\}==?"([^ "]+).*/\1/'); do
881 _bin=""
882 if [ -x ${udevdir}/$_prog ]; then
883 _bin=${udevdir}/$_prog
884 elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
885 _bin=$(find_binary "$_prog") || {
886 dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
887 continue;
888 }
889 fi
890
891 [[ $_bin ]] && dracut_install "$_bin"
892 done
893 fi
894 }
895
896 # attempt to install any programs specified in a udev rule
897 inst_rule_group_owner() {
898 local i
899
900 if grep -qE 'OWNER=?"[^ "]+' "$1"; then
901 for i in $(grep -E 'OWNER=?"[^ "]+' "$1" | sed -r 's/.*OWNER=?"([^ "]+).*/\1/'); do
902 if ! egrep -q "^$i:" "$initdir/etc/passwd" 2>/dev/null; then
903 egrep "^$i:" /etc/passwd 2>/dev/null >> "$initdir/etc/passwd"
904 fi
905 done
906 fi
907 if grep -qE 'GROUP=?"[^ "]+' "$1"; then
908 for i in $(grep -E 'GROUP=?"[^ "]+' "$1" | sed -r 's/.*GROUP=?"([^ "]+).*/\1/'); do
909 if ! egrep -q "^$i:" "$initdir/etc/group" 2>/dev/null; then
910 egrep "^$i:" /etc/group 2>/dev/null >> "$initdir/etc/group"
911 fi
912 done
913 fi
914 }
915
916 inst_rule_initqueue() {
917 if grep -q -F initqueue "$1"; then
918 dracut_need_initqueue
919 fi
920 }
921
922 # udev rules always get installed in the same place, so
923 # create a function to install them to make life simpler.
924 inst_rules() {
925 local _target=/etc/udev/rules.d _rule _found
926
927 inst_dir "${udevdir}/rules.d"
928 inst_dir "$_target"
929 for _rule in "$@"; do
930 if [ "${_rule#/}" = "$_rule" ]; then
931 for r in ${udevdir}/rules.d ${hostonly:+/etc/udev/rules.d}; do
932 if [[ -e $r/$_rule ]]; then
933 _found="$r/$_rule"
934 inst_rule_programs "$_found"
935 inst_rule_group_owner "$_found"
936 inst_rule_initqueue "$_found"
937 inst_simple "$_found"
938 fi
939 done
940 fi
941 for r in '' $dracutbasedir/rules.d/; do
942 # skip rules without an absolute path
943 [[ "${r}$_rule" != /* ]] && continue
944
945 if [[ -f ${r}$_rule ]]; then
946 _found="${r}$_rule"
947 inst_rule_programs "$_found"
948 inst_rule_group_owner "$_found"
949 inst_rule_initqueue "$_found"
950 inst_simple "$_found" "$_target/${_found##*/}"
951 fi
952 done
953 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
954 done
955 }
956
957 inst_rules_wildcard() {
958 local _target=/etc/udev/rules.d _rule _found
959
960 inst_dir "${udevdir}/rules.d"
961 inst_dir "$_target"
962 for _rule in ${udevdir}/rules.d/$1 ${dracutbasedir}/rules.d/$1 ; do
963 if [[ -e $_rule ]]; then
964 inst_rule_programs "$_rule"
965 inst_rule_group_owner "$_rule"
966 inst_rule_initqueue "$_rule"
967 inst_simple "$_rule"
968 _found=$_rule
969 fi
970 done
971 if [ -n ${hostonly} ] ; then
972 for _rule in ${_target}/$1 ; do
973 if [[ -f $_rule ]]; then
974 inst_rule_programs "$_rule"
975 inst_rule_group_owner "$_rule"
976 inst_rule_initqueue "$_rule"
977 inst_simple "$_rule"
978 _found=$_rule
979 fi
980 done
981 fi
982 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
983 }
984
985 prepare_udev_rules() {
986 [ -z "$UDEVVERSION" ] && export UDEVVERSION=$(udevadm --version)
987
988 for f in "$@"; do
989 f="${initdir}/etc/udev/rules.d/$f"
990 [ -e "$f" ] || continue
991 while read line; do
992 if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
993 if [ $UDEVVERSION -ge 174 ]; then
994 printf '%sIMPORT{builtin}="path_id"\n' "${line%%IMPORT PATH_ID}"
995 else
996 printf '%sIMPORT{program}="path_id %%p"\n' "${line%%IMPORT PATH_ID}"
997 fi
998 elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
999 if [ $UDEVVERSION -ge 176 ]; then
1000 printf '%sIMPORT{builtin}="blkid"\n' "${line%%IMPORT BLKID}"
1001 else
1002 printf '%sIMPORT{program}="/sbin/blkid -o udev -p $tempnode"\n' "${line%%IMPORT BLKID}"
1003 fi
1004 else
1005 echo "$line"
1006 fi
1007 done < "${f}" > "${f}.new"
1008 mv "${f}.new" "$f"
1009 done
1010 }
1011
1012 # install function specialized for hooks
1013 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
1014 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
1015 inst_hook() {
1016 if ! [[ -f $3 ]]; then
1017 dfatal "Cannot install a hook ($3) that does not exist."
1018 dfatal "Aborting initrd creation."
1019 exit 1
1020 elif ! [[ "$hookdirs" == *$1* ]]; then
1021 dfatal "No such hook type $1. Aborting initrd creation."
1022 exit 1
1023 fi
1024 inst_simple "$3" "/lib/dracut/hooks/${1}/${2}-${3##*/}"
1025 }
1026
1027 # install any of listed files
1028 #
1029 # If first argument is '-d' and second some destination path, first accessible
1030 # source is installed into this path, otherwise it will installed in the same
1031 # path as source. If none of listed files was installed, function return 1.
1032 # On first successful installation it returns with 0 status.
1033 #
1034 # Example:
1035 #
1036 # inst_any -d /bin/foo /bin/bar /bin/baz
1037 #
1038 # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
1039 # initramfs.
1040 inst_any() {
1041 local to f
1042
1043 [[ $1 = '-d' ]] && to="$2" && shift 2
1044
1045 for f in "$@"; do
1046 if [[ -e $f ]]; then
1047 [[ $to ]] && inst "$f" "$to" && return 0
1048 inst "$f" && return 0
1049 fi
1050 done
1051
1052 return 1
1053 }
1054
1055
1056 # inst_libdir_file [-n <pattern>] <file> [<file>...]
1057 # Install a <file> located on a lib directory to the initramfs image
1058 # -n <pattern> install matching files
1059 inst_libdir_file() {
1060 local _files
1061 if [[ "$1" == "-n" ]]; then
1062 local _pattern=$2
1063 shift 2
1064 for _dir in $libdirs; do
1065 for _i in "$@"; do
1066 for _f in "$_dir"/$_i; do
1067 [[ "$_f" =~ $_pattern ]] || continue
1068 [[ -e "$_f" ]] && _files+="$_f "
1069 done
1070 done
1071 done
1072 else
1073 for _dir in $libdirs; do
1074 for _i in "$@"; do
1075 for _f in "$_dir"/$_i; do
1076 [[ -e "$_f" ]] && _files+="$_f "
1077 done
1078 done
1079 done
1080 fi
1081 [[ $_files ]] && inst_multiple $_files
1082 }
1083
1084
1085 # install function decompressing the target and handling symlinks
1086 # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
1087 #
1088 # Function install targets in the same paths inside overlay but decompressed
1089 # and without extensions (.gz, .bz2).
1090 inst_decompress() {
1091 local _src _cmd
1092
1093 for _src in $@
1094 do
1095 case ${_src} in
1096 *.gz) _cmd='gzip -f -d' ;;
1097 *.bz2) _cmd='bzip2 -d' ;;
1098 *) return 1 ;;
1099 esac
1100 inst_simple ${_src}
1101 # Decompress with chosen tool. We assume that tool changes name e.g.
1102 # from 'name.gz' to 'name'.
1103 ${_cmd} "${initdir}${_src}"
1104 done
1105 }
1106
1107 # It's similar to above, but if file is not compressed, performs standard
1108 # install.
1109 # $@ = list of files
1110 inst_opt_decompress() {
1111 local _src
1112
1113 for _src in $@
1114 do
1115 inst_decompress "${_src}" || inst "${_src}"
1116 done
1117 }
1118
1119 # module_check <dracut module>
1120 # execute the check() function of module-setup.sh of <dracut module>
1121 # or the "check" script, if module-setup.sh is not found
1122 # "check $hostonly" is called
1123 module_check() {
1124 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1125 local _ret
1126 local _forced=0
1127 local _hostonly=$hostonly
1128 [ $# -eq 2 ] && _forced=$2
1129 [[ -d $_moddir ]] || return 1
1130 if [[ ! -f $_moddir/module-setup.sh ]]; then
1131 # if we do not have a check script, we are unconditionally included
1132 [[ -x $_moddir/check ]] || return 0
1133 [ $_forced -ne 0 ] && unset hostonly
1134 $_moddir/check $hostonly
1135 _ret=$?
1136 else
1137 unset check depends cmdline install installkernel
1138 check() { true; }
1139 . $_moddir/module-setup.sh
1140 is_func check || return 0
1141 [ $_forced -ne 0 ] && unset hostonly
1142 moddir=$_moddir check $hostonly
1143 _ret=$?
1144 unset check depends cmdline install installkernel
1145 fi
1146 hostonly=$_hostonly
1147 return $_ret
1148 }
1149
1150 # module_check_mount <dracut module>
1151 # execute the check() function of module-setup.sh of <dracut module>
1152 # or the "check" script, if module-setup.sh is not found
1153 # "mount_needs=1 check 0" is called
1154 module_check_mount() {
1155 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1156 local _ret
1157 mount_needs=1
1158 [[ -d $_moddir ]] || return 1
1159 if [[ ! -f $_moddir/module-setup.sh ]]; then
1160 # if we do not have a check script, we are unconditionally included
1161 [[ -x $_moddir/check ]] || return 0
1162 mount_needs=1 $_moddir/check 0
1163 _ret=$?
1164 else
1165 unset check depends cmdline install installkernel
1166 check() { false; }
1167 . $_moddir/module-setup.sh
1168 moddir=$_moddir check 0
1169 _ret=$?
1170 unset check depends cmdline install installkernel
1171 fi
1172 unset mount_needs
1173 return $_ret
1174 }
1175
1176 # module_depends <dracut module>
1177 # execute the depends() function of module-setup.sh of <dracut module>
1178 # or the "depends" script, if module-setup.sh is not found
1179 module_depends() {
1180 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1181 local _ret
1182 [[ -d $_moddir ]] || return 1
1183 if [[ ! -f $_moddir/module-setup.sh ]]; then
1184 # if we do not have a check script, we have no deps
1185 [[ -x $_moddir/check ]] || return 0
1186 $_moddir/check -d
1187 return $?
1188 else
1189 unset check depends cmdline install installkernel
1190 depends() { true; }
1191 . $_moddir/module-setup.sh
1192 moddir=$_moddir depends
1193 _ret=$?
1194 unset check depends cmdline install installkernel
1195 return $_ret
1196 fi
1197 }
1198
1199 # module_cmdline <dracut module>
1200 # execute the cmdline() function of module-setup.sh of <dracut module>
1201 # or the "cmdline" script, if module-setup.sh is not found
1202 module_cmdline() {
1203 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1204 local _ret
1205 [[ -d $_moddir ]] || return 1
1206 if [[ ! -f $_moddir/module-setup.sh ]]; then
1207 [[ -x $_moddir/cmdline ]] && . "$_moddir/cmdline"
1208 return $?
1209 else
1210 unset check depends cmdline install installkernel
1211 cmdline() { true; }
1212 . $_moddir/module-setup.sh
1213 moddir=$_moddir cmdline
1214 _ret=$?
1215 unset check depends cmdline install installkernel
1216 return $_ret
1217 fi
1218 }
1219
1220 # module_install <dracut module>
1221 # execute the install() function of module-setup.sh of <dracut module>
1222 # or the "install" script, if module-setup.sh is not found
1223 module_install() {
1224 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1225 local _ret
1226 [[ -d $_moddir ]] || return 1
1227 if [[ ! -f $_moddir/module-setup.sh ]]; then
1228 [[ -x $_moddir/install ]] && . "$_moddir/install"
1229 return $?
1230 else
1231 unset check depends cmdline install installkernel
1232 install() { true; }
1233 . $_moddir/module-setup.sh
1234 moddir=$_moddir install
1235 _ret=$?
1236 unset check depends cmdline install installkernel
1237 return $_ret
1238 fi
1239 }
1240
1241 # module_installkernel <dracut module>
1242 # execute the installkernel() function of module-setup.sh of <dracut module>
1243 # or the "installkernel" script, if module-setup.sh is not found
1244 module_installkernel() {
1245 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1246 local _ret
1247 [[ -d $_moddir ]] || return 1
1248 if [[ ! -f $_moddir/module-setup.sh ]]; then
1249 [[ -x $_moddir/installkernel ]] && . "$_moddir/installkernel"
1250 return $?
1251 else
1252 unset check depends cmdline install installkernel
1253 installkernel() { true; }
1254 . $_moddir/module-setup.sh
1255 moddir=$_moddir installkernel
1256 _ret=$?
1257 unset check depends cmdline install installkernel
1258 return $_ret
1259 fi
1260 }
1261
1262 # check_mount <dracut module>
1263 # check_mount checks, if a dracut module is needed for the given
1264 # device and filesystem types in "${host_fs_types[@]}"
1265 check_mount() {
1266 local _mod=$1
1267 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1268 local _ret
1269 local _moddep
1270
1271 [ "${#host_fs_types[*]}" -le 0 ] && return 1
1272
1273 # If we are already scheduled to be loaded, no need to check again.
1274 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
1275 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
1276
1277 # This should never happen, but...
1278 [[ -d $_moddir ]] || return 1
1279
1280 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
1281
1282 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
1283 return 1
1284 fi
1285
1286 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
1287 module_check_mount $_mod; ret=$?
1288
1289 # explicit module, so also accept ret=255
1290 [[ $ret = 0 || $ret = 255 ]] || return 1
1291 else
1292 # module not in our list
1293 if [[ $dracutmodules = all ]]; then
1294 # check, if we can and should install this module
1295 module_check_mount $_mod || return 1
1296 else
1297 # skip this module
1298 return 1
1299 fi
1300 fi
1301
1302
1303 for _moddep in $(module_depends $_mod); do
1304 # handle deps as if they were manually added
1305 [[ " $add_dracutmodules " == *\ $_moddep\ * ]] || \
1306 add_dracutmodules+=" $_moddep "
1307 [[ " $force_add_dracutmodules " == *\ $_moddep\ * ]] || \
1308 force_add_dracutmodules+=" $_moddep "
1309 # if a module we depend on fail, fail also
1310 if ! check_module $_moddep; then
1311 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
1312 return 1
1313 fi
1314 done
1315
1316 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
1317 mods_to_load+=" $_mod "
1318
1319 return 0
1320 }
1321
1322 # check_module <dracut module> [<use_as_dep>]
1323 # check if a dracut module is to be used in the initramfs process
1324 # if <use_as_dep> is set, then the process also keeps track
1325 # that the modules were checked for the dependency tracking process
1326 check_module() {
1327 local _mod=$1
1328 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1329 local _ret
1330 local _moddep
1331 # If we are already scheduled to be loaded, no need to check again.
1332 [[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
1333 [[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1
1334
1335 # This should never happen, but...
1336 [[ -d $_moddir ]] || return 1
1337
1338 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
1339
1340 if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
1341 dinfo "dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
1342 return 1
1343 fi
1344
1345 if [[ " $dracutmodules $add_dracutmodules $force_add_dracutmodules" == *\ $_mod\ * ]]; then
1346 if [[ " $force_add_dracutmodules " == *\ $_mod\ * ]]; then
1347 module_check $_mod 1; ret=$?
1348 else
1349 module_check $_mod 0; ret=$?
1350 fi
1351 # explicit module, so also accept ret=255
1352 [[ $ret = 0 || $ret = 255 ]] || return 1
1353 else
1354 # module not in our list
1355 if [[ $dracutmodules = all ]]; then
1356 # check, if we can and should install this module
1357 module_check $_mod || return 1
1358 else
1359 # skip this module
1360 return 1
1361 fi
1362 fi
1363
1364 for _moddep in $(module_depends $_mod); do
1365 # handle deps as if they were manually added
1366 [[ " $add_dracutmodules " == *\ $_moddep\ * ]] || \
1367 add_dracutmodules+=" $_moddep "
1368 [[ " $force_add_dracutmodules " == *\ $_moddep\ * ]] || \
1369 force_add_dracutmodules+=" $_moddep "
1370 # if a module we depend on fail, fail also
1371 if ! check_module $_moddep; then
1372 derror "dracut module '$_mod' depends on '$_moddep', which can't be installed"
1373 return 1
1374 fi
1375 done
1376
1377 [[ " $mods_to_load " == *\ $_mod\ * ]] || \
1378 mods_to_load+=" $_mod "
1379
1380 return 0
1381 }
1382
1383 # for_each_module_dir <func>
1384 # execute "<func> <dracut module> 1"
1385 for_each_module_dir() {
1386 local _modcheck
1387 local _mod
1388 local _moddir
1389 local _func
1390 _func=$1
1391 for _moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1392 [[ -d $_moddir ]] || continue;
1393 [[ -e $_moddir/install || -e $_moddir/installkernel || \
1394 -e $_moddir/module-setup.sh ]] || continue
1395 _mod=${_moddir##*/}; _mod=${_mod#[0-9][0-9]}
1396 $_func $_mod 1
1397 done
1398
1399 # Report any missing dracut modules, the user has specified
1400 _modcheck="$add_dracutmodules $force_add_dracutmodules"
1401 [[ $dracutmodules != all ]] && _modcheck="$m $dracutmodules"
1402 for _mod in $_modcheck; do
1403 [[ " $mods_to_load " == *\ $_mod\ * ]] && continue
1404 [[ " $omit_dracutmodules " == *\ $_mod\ * ]] && continue
1405 derror "dracut module '$_mod' cannot be found or installed."
1406 done
1407 }
1408
1409 # Install a single kernel module along with any firmware it may require.
1410 # $1 = full path to kernel module to install
1411 install_kmod_with_fw() {
1412 # no need to go further if the module is already installed
1413
1414 [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
1415 && return 0
1416
1417 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -e "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}" ]]; then
1418 read ret < "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
1419 return $ret
1420 fi
1421
1422 if [[ $omit_drivers ]]; then
1423 local _kmod=${1##*/}
1424 _kmod=${_kmod%.ko}
1425 _kmod=${_kmod/-/_}
1426 if [[ "$_kmod" =~ $omit_drivers ]]; then
1427 dinfo "Omitting driver $_kmod"
1428 return 0
1429 fi
1430 if [[ "${1##*/lib/modules/$kernel/}" =~ $omit_drivers ]]; then
1431 dinfo "Omitting driver $_kmod"
1432 return 0
1433 fi
1434 fi
1435
1436 if [[ $silent_omit_drivers ]]; then
1437 local _kmod=${1##*/}
1438 _kmod=${_kmod%.ko}
1439 _kmod=${_kmod/-/_}
1440 [[ "$_kmod" =~ $silent_omit_drivers ]] && return 0
1441 [[ "${1##*/lib/modules/$kernel/}" =~ $silent_omit_drivers ]] && return 0
1442 fi
1443
1444 inst_simple "$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
1445 ret=$?
1446 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1447 [[ -d "$DRACUT_KERNEL_LAZY_HASHDIR" ]] && \
1448 echo $ret > "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
1449 (($ret != 0)) && return $ret
1450
1451 local _modname=${1##*/} _fwdir _found _fw
1452 _modname=${_modname%.ko*}
1453 for _fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do
1454 _found=''
1455 for _fwdir in $fw_dir; do
1456 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1457 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1458 _found=yes
1459 fi
1460 done
1461 if [[ $_found != yes ]]; then
1462 if ! [[ -d $(echo /sys/module/${_modname//-/_}|{ read a b; echo $a; }) ]]; then
1463 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
1464 "\"${_modname}.ko\""
1465 else
1466 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
1467 "\"${_modname}.ko\""
1468 fi
1469 fi
1470 done
1471 return 0
1472 }
1473
1474 # Do something with all the dependencies of a kernel module.
1475 # Note that kernel modules depend on themselves using the technique we use
1476 # $1 = function to call for each dependency we find
1477 # It will be passed the full path to the found kernel module
1478 # $2 = module to get dependencies for
1479 # rest of args = arguments to modprobe
1480 # _fderr specifies FD passed from surrounding scope
1481 for_each_kmod_dep() {
1482 local _func=$1 _kmod=$2 _cmd _modpath _options
1483 shift 2
1484 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
1485 while read _cmd _modpath _options; do
1486 [[ $_cmd = insmod ]] || continue
1487 $_func ${_modpath} || exit $?
1488 done
1489 )
1490 }
1491
1492 dracut_kernel_post() {
1493 local _moddirname=${srcmods%%/lib/modules/*}
1494 local _pid
1495
1496 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" ]]; then
1497 xargs -r modprobe -a ${_moddirname:+-d ${_moddirname}/} \
1498 --ignore-install --show-depends --set-version $kernel \
1499 < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" 2>/dev/null \
1500 | sort -u \
1501 | while read _cmd _modpath _options; do
1502 [[ $_cmd = insmod ]] || continue
1503 echo "$_modpath"
1504 done > "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1505
1506 (
1507 if [[ $DRACUT_INSTALL ]] && [[ -z $_moddirname ]]; then
1508 xargs -r $DRACUT_INSTALL ${initdir:+-D "$initdir"} -a < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1509 else
1510 while read _modpath; do
1511 local _destpath=$_modpath
1512 [[ $_moddirname ]] && _destpath=${_destpath##$_moddirname/}
1513 _destpath=${_destpath##*/lib/modules/$kernel/}
1514 inst_simple "$_modpath" "/lib/modules/$kernel/${_destpath}" || exit $?
1515 done < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
1516 fi
1517 ) &
1518 _pid=$(jobs -p | while read a ; do printf ":$a";done)
1519 _pid=${_pid##*:}
1520
1521 if [[ $DRACUT_INSTALL ]]; then
1522 xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" \
1523 | while read line; do
1524 for _fwdir in $fw_dir; do
1525 echo $_fwdir/$line;
1526 done;
1527 done | xargs -r $DRACUT_INSTALL ${initdir:+-D "$initdir"} -a -o
1528 else
1529 for _fw in $(xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"); do
1530 for _fwdir in $fw_dir; do
1531 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1532 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1533 break
1534 fi
1535 done
1536 done
1537 fi
1538
1539 wait $_pid
1540 fi
1541
1542 for _f in modules.builtin.bin modules.builtin modules.order; do
1543 [[ $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f"
1544 done
1545
1546 # generate module dependencies for the initrd
1547 if [[ -d $initdir/lib/modules/$kernel ]] && \
1548 ! depmod -a -b "$initdir" $kernel; then
1549 dfatal "\"depmod -a $kernel\" failed."
1550 exit 1
1551 fi
1552
1553 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && rm -fr -- "$DRACUT_KERNEL_LAZY_HASHDIR"
1554 }
1555
1556 [[ "$kernel_current" ]] || export kernel_current=$(uname -r)
1557
1558 module_is_host_only() {
1559 local _mod=$1
1560 local _modenc a i _k _s _v _aliases
1561 _mod=${_mod##*/}
1562 _mod=${_mod%.ko}
1563 _modenc=${_mod//-/_}
1564
1565 [[ " $add_drivers " == *\ ${_mod}\ * ]] && return 0
1566
1567 # check if module is loaded
1568 [[ ${host_modules["$_modenc"]} ]] && return 0
1569
1570 [[ "$kernel_current" ]] || export kernel_current=$(uname -r)
1571
1572 if [[ "$kernel_current" != "$kernel" ]]; then
1573 # check if module is loadable on the current kernel
1574 # this covers the case, where a new module is introduced
1575 # or a module was renamed
1576 # or a module changed from builtin to a module
1577
1578 if [[ -d /lib/modules/$kernel_current ]]; then
1579 # if the modinfo can be parsed, but the module
1580 # is not loaded, then we can safely return 1
1581 modinfo -F filename "$_mod" &>/dev/null && return 1
1582 fi
1583
1584 _aliases=$(modinfo -k $kernel -F alias $_mod 2>/dev/null)
1585
1586 # if the module has no aliases, install it
1587 [[ $_aliases ]] || return 0
1588
1589 # finally check all modalias
1590 for a in $_aliases; do
1591 for i in "${!host_modalias[@]}"; do
1592 [[ $i == $a ]] && return 0
1593 done
1594 done
1595
1596 fi
1597
1598 return 1
1599 }
1600
1601 find_kernel_modules_by_path () {
1602 local _OLDIFS
1603
1604 [[ -f "$srcmods/modules.dep" ]] || return 0
1605
1606 _OLDIFS=$IFS
1607 IFS=:
1608 while read a rest; do
1609 [[ $a = */$1/* ]] || [[ $a = updates/* ]] || continue
1610 printf "%s\n" "$srcmods/$a"
1611 done < "$srcmods/modules.dep"
1612 IFS=$_OLDIFS
1613 return 0
1614 }
1615
1616 find_kernel_modules () {
1617 find_kernel_modules_by_path drivers
1618 }
1619
1620 # instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1621 # instmods [-c [-s]] <kernel subsystem>
1622 # install kernel modules along with all their dependencies.
1623 # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1624 instmods() {
1625 [[ $no_kernel = yes ]] && return
1626 # called [sub]functions inherit _fderr
1627 local _fderr=9
1628 local _check=no
1629 local _silent=no
1630 if [[ $1 = '-c' ]]; then
1631 _check=yes
1632 shift
1633 fi
1634
1635 if [[ $1 = '-s' ]]; then
1636 _silent=yes
1637 shift
1638 fi
1639
1640 function inst1mod() {
1641 local _ret=0 _mod="$1"
1642 case $_mod in
1643 =*)
1644 ( [[ "$_mpargs" ]] && echo $_mpargs
1645 find_kernel_modules_by_path "${_mod#=}" ) \
1646 | instmods
1647 ((_ret+=$?))
1648 ;;
1649 --*) _mpargs+=" $_mod" ;;
1650 *)
1651 _mod=${_mod##*/}
1652 # if we are already installed, skip this module and go on
1653 # to the next one.
1654 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1655 [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko" ]]; then
1656 read _ret <"$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko"
1657 return $_ret
1658 fi
1659
1660 _mod=${_mod/-/_}
1661 if [[ $omit_drivers ]] && [[ "$_mod" =~ $omit_drivers ]]; then
1662 dinfo "Omitting driver ${_mod##$srcmods}"
1663 return 0
1664 fi
1665
1666 # If we are building a host-specific initramfs and this
1667 # module is not already loaded, move on to the next one.
1668 [[ $hostonly ]] \
1669 && ! module_is_host_only "$_mod" \
1670 && return 0
1671
1672 if [[ "$_check" = "yes" ]] || ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
1673 # We use '-d' option in modprobe only if modules prefix path
1674 # differs from default '/'. This allows us to use dracut with
1675 # old version of modprobe which doesn't have '-d' option.
1676 local _moddirname=${srcmods%%/lib/modules/*}
1677 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1678
1679 # ok, load the module, all its dependencies, and any firmware
1680 # it may require
1681 for_each_kmod_dep install_kmod_with_fw $_mod \
1682 --set-version $kernel ${_moddirname} $_mpargs
1683 ((_ret+=$?))
1684 else
1685 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1686 echo $_mod >> "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist"
1687 fi
1688 ;;
1689 esac
1690 return $_ret
1691 }
1692
1693 function instmods_1() {
1694 local _mod _mpargs
1695 if (($# == 0)); then # filenames from stdin
1696 while read _mod; do
1697 inst1mod "${_mod%.ko*}" || {
1698 if [[ "$_check" == "yes" ]]; then
1699 [[ "$_silent" == "no" ]] && dfatal "Failed to install module $_mod"
1700 return 1
1701 fi
1702 }
1703 done
1704 fi
1705 while (($# > 0)); do # filenames as arguments
1706 inst1mod ${1%.ko*} || {
1707 if [[ "$_check" == "yes" ]]; then
1708 [[ "$_silent" == "no" ]] && dfatal "Failed to install module $1"
1709 return 1
1710 fi
1711 }
1712 shift
1713 done
1714 return 0
1715 }
1716
1717 local _ret _filter_not_found='FATAL: Module .* not found.'
1718 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1719 # redirections, but that would make dracut require bash4 at least.
1720 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1721 | while read line; do [[ "$line" =~ $_filter_not_found ]] || echo $line;done | derror
1722 _ret=$?
1723 return $_ret
1724 }
1725 # get_cpu_vendor
1726 # Only two values are returned: AMD or Intel
1727 get_cpu_vendor ()
1728 {
1729 if grep -qE AMD /proc/cpuinfo; then
1730 printf "AMD"
1731 fi
1732 if grep -qE Intel /proc/cpuinfo; then
1733 printf "Intel"
1734 fi
1735 }
1736
1737 # get_host_ucode
1738 # Get the hosts' ucode file based on the /proc/cpuinfo
1739 get_ucode_file ()
1740 {
1741 local family=`grep -E "cpu family" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1742 local model=`grep -E "model" /proc/cpuinfo |grep -v name | head -1 | sed s/.*:\ //`
1743 local stepping=`grep -E "stepping" /proc/cpuinfo | head -1 | sed s/.*:\ //`
1744
1745 if [[ "$(get_cpu_vendor)" == "AMD" ]]; then
1746 # If family greater or equal than 0x15
1747 if [[ $family -ge 21 ]]; then
1748 printf "microcode_amd_fam15h.bin"
1749 else
1750 printf "microcode_amd.bin"
1751 fi
1752 fi
1753 if [[ "$(get_cpu_vendor)" == "Intel" ]]; then
1754 # The /proc/cpuinfo are in decimal.
1755 printf "%02x-%02x-%02x" ${family} ${model} ${stepping}
1756 fi
1757 }
1758
1759 # Not every device in /dev/mapper should be examined.
1760 # If it is an LVM device, touch only devices which have /dev/VG/LV symlink.
1761 lvm_internal_dev() {
1762 local dev_dm_dir=/sys/dev/block/$1/dm
1763 [[ ! -f $dev_dm_dir/uuid || $(<$dev_dm_dir/uuid) != LVM-* ]] && return 1 # Not an LVM device
1764 local DM_VG_NAME DM_LV_NAME DM_LV_LAYER
1765 eval $(dmsetup splitname --nameprefixes --noheadings --rows "$(<$dev_dm_dir/name)" 2>/dev/null)
1766 [[ ${DM_VG_NAME} ]] && [[ ${DM_LV_NAME} ]] || return 0 # Better skip this!
1767 [[ ${DM_LV_LAYER} ]] || [[ ! -L /dev/${DM_VG_NAME}/${DM_LV_NAME} ]]
1768 }
1769