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