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