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