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