]> git.ipfire.org Git - thirdparty/dracut.git/blob - dracut-functions.sh
fix(base): suppress calls to getarg in build phase
[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 # is_func <command>
23 # Check whether $1 is a function.
24 is_func() {
25 [[ "$(type -t "$1")" == "function" ]]
26 }
27
28 # Generic substring function. If $2 is in $1, return 0.
29 strstr() { [[ $1 == *"$2"* ]]; }
30 # Generic glob matching function. If glob pattern $2 matches anywhere in $1, OK
31 strglobin() { [[ $1 == *$2* ]]; }
32 # Generic glob matching function. If glob pattern $2 matches all of $1, OK
33 # shellcheck disable=SC2053
34 strglob() { [[ $1 == $2 ]]; }
35 # returns OK if $1 contains literal string $2 at the beginning, and isn't empty
36 str_starts() { [ "${1#"$2"*}" != "$1" ]; }
37 # returns OK if $1 contains literal string $2 at the end, and isn't empty
38 str_ends() { [ "${1%*"$2"}" != "$1" ]; }
39
40 # find a binary. If we were not passed the full path directly,
41 # search in the usual places to find the binary.
42 find_binary() {
43 local _delim
44 local _path
45 local l
46 local p
47 [[ -z ${1##/*} ]] || _delim="/"
48
49 if [[ $1 == *.so* ]]; then
50 # shellcheck disable=SC2154
51 for l in $libdirs; do
52 _path="${l}${_delim}${1}"
53 if { $DRACUT_LDD "${dracutsysrootdir}${_path}" &> /dev/null; }; then
54 printf "%s\n" "${_path}"
55 return 0
56 fi
57 done
58 _path="${_delim}${1}"
59 if { $DRACUT_LDD "${dracutsysrootdir}${_path}" &> /dev/null; }; then
60 printf "%s\n" "${_path}"
61 return 0
62 fi
63 fi
64 if [[ $1 == */* ]]; then
65 _path="${_delim}${1}"
66 if [[ -L ${dracutsysrootdir}${_path} ]] || [[ -x ${dracutsysrootdir}${_path} ]]; then
67 printf "%s\n" "${_path}"
68 return 0
69 fi
70 fi
71 for p in $DRACUT_PATH; do
72 _path="${p}${_delim}${1}"
73 if [[ -L ${dracutsysrootdir}${_path} ]] || [[ -x ${dracutsysrootdir}${_path} ]]; then
74 printf "%s\n" "${_path}"
75 return 0
76 fi
77 done
78
79 [[ -n $dracutsysrootdir ]] && return 1
80 type -P "${1##*/}"
81 }
82
83 ldconfig_paths() {
84 $DRACUT_LDCONFIG ${dracutsysrootdir:+-r ${dracutsysrootdir} -f /etc/ld.so.conf} -pN 2> /dev/null | grep -E -v '/(lib|lib64|usr/lib|usr/lib64)/[^/]*$' | sed -n 's,.* => \(.*\)/.*,\1,p' | sort | uniq
85 }
86
87 # Version comparision function. Assumes Linux style version scheme.
88 # $1 = version a
89 # $2 = comparision op (gt, ge, eq, le, lt, ne)
90 # $3 = version b
91 vercmp() {
92 local _n1
93 read -r -a _n1 <<< "${1//./ }"
94 local _op=$2
95 local _n2
96 read -r -a _n2 <<< "${3//./ }"
97 local _i _res
98
99 for ((_i = 0; ; _i++)); do
100 if [[ ! ${_n1[_i]}${_n2[_i]} ]]; then
101 _res=0
102 elif ((${_n1[_i]:-0} > ${_n2[_i]:-0})); then
103 _res=1
104 elif ((${_n1[_i]:-0} < ${_n2[_i]:-0})); then
105 _res=2
106 else
107 continue
108 fi
109 break
110 done
111
112 case $_op in
113 gt) ((_res == 1)) ;;
114 ge) ((_res != 2)) ;;
115 eq) ((_res == 0)) ;;
116 le) ((_res != 1)) ;;
117 lt) ((_res == 2)) ;;
118 ne) ((_res != 0)) ;;
119 esac
120 }
121
122 # Create all subdirectories for given path without creating the last element.
123 # $1 = path
124 mksubdirs() {
125 # shellcheck disable=SC2174
126 [[ -e ${1%/*} ]] || mkdir -m 0755 -p -- "${1%/*}"
127 }
128
129 # Function prints global variables in format name=value line by line.
130 # $@ = list of global variables' name
131 print_vars() {
132 local _var _value
133
134 for _var in "$@"; do
135 eval printf -v _value "%s" \""\$$_var"\"
136 [[ ${_value} ]] && printf '%s="%s"\n' "$_var" "$_value"
137 done
138 }
139
140 # normalize_path <path>
141 # Prints the normalized path, where it removes any duplicated
142 # and trailing slashes.
143 # Example:
144 # $ normalize_path ///test/test//
145 # /test/test
146 normalize_path() {
147 # shellcheck disable=SC2064
148 trap "$(shopt -p extglob)" RETURN
149 shopt -q -s extglob
150 local p=${1//+(\/)//}
151 printf "%s\n" "${p%/}"
152 }
153
154 # convert_abs_rel <from> <to>
155 # Prints the relative path, when creating a symlink to <to> from <from>.
156 # Example:
157 # $ convert_abs_rel /usr/bin/test /bin/test-2
158 # ../../bin/test-2
159 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
160 convert_abs_rel() {
161 local __current __absolute __abssize __cursize __newpath
162 local -i __i __level
163
164 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
165
166 # corner case #1 - self looping link
167 [[ $1 == "$2" ]] && {
168 printf "%s\n" "${1##*/}"
169 return
170 }
171
172 # corner case #2 - own dir link
173 [[ ${1%/*} == "$2" ]] && {
174 printf ".\n"
175 return
176 }
177
178 IFS=/ read -r -a __current <<< "$1"
179 IFS=/ read -r -a __absolute <<< "$2"
180
181 __abssize=${#__absolute[@]}
182 __cursize=${#__current[@]}
183
184 while [[ ${__absolute[__level]} == "${__current[__level]}" ]]; do
185 ((__level++))
186 if ((__level > __abssize || __level > __cursize)); then
187 break
188 fi
189 done
190
191 for ((__i = __level; __i < __cursize - 1; __i++)); do
192 if ((__i > __level)); then
193 __newpath=$__newpath"/"
194 fi
195 __newpath=$__newpath".."
196 done
197
198 for ((__i = __level; __i < __abssize; __i++)); do
199 if [[ -n $__newpath ]]; then
200 __newpath=$__newpath"/"
201 fi
202 __newpath=$__newpath${__absolute[__i]}
203 done
204
205 printf -- "%s\n" "$__newpath"
206 }
207
208 # get_fs_env <device>
209 # Get and the ID_FS_TYPE variable from udev for a device.
210 # Example:
211 # $ get_fs_env /dev/sda2
212 # ext4
213 get_fs_env() {
214 [[ $1 ]] || return
215 unset ID_FS_TYPE
216 ID_FS_TYPE=$(blkid -u filesystem -o export -- "$1" \
217 | while read -r line || [ -n "$line" ]; do
218 if [[ $line == "TYPE="* ]]; then
219 printf "%s" "${line#TYPE=}"
220 exit 0
221 fi
222 done)
223 if [[ $ID_FS_TYPE ]]; then
224 printf "%s" "$ID_FS_TYPE"
225 return 0
226 fi
227 return 1
228 }
229
230 # get_maj_min <device>
231 # Prints the major and minor of a device node.
232 # Example:
233 # $ get_maj_min /dev/sda2
234 # 8:2
235 get_maj_min() {
236 local _majmin
237 out="$(grep -m1 -oP "^$1 \K\S+$" "${get_maj_min_cache_file:?}")"
238 if [ -z "$out" ]; then
239 _majmin="$(stat -L -c '%t:%T' "$1" 2> /dev/null)"
240 out="$(printf "%s" "$((0x${_majmin%:*})):$((0x${_majmin#*:}))")"
241 echo "$1 $out" >> "${get_maj_min_cache_file:?}"
242 fi
243 echo -n "$out"
244 }
245
246 # get_devpath_block <device>
247 # get the DEVPATH in /sys of a block device
248 get_devpath_block() {
249 local _majmin _i
250 _majmin=$(get_maj_min "$1")
251
252 for _i in /sys/block/*/dev /sys/block/*/*/dev; do
253 [[ -e $_i ]] || continue
254 if [[ $_majmin == "$(< "$_i")" ]]; then
255 printf "%s" "${_i%/dev}"
256 return 0
257 fi
258 done
259 return 1
260 }
261
262 # get a persistent path from a device
263 get_persistent_dev() {
264 local i _tmp _dev _pol
265
266 _dev=$(get_maj_min "$1")
267 [ -z "$_dev" ] && return
268
269 if [[ -n $persistent_policy ]]; then
270 _pol="/dev/disk/${persistent_policy}/*"
271 else
272 _pol=
273 fi
274
275 for i in \
276 $_pol \
277 /dev/mapper/* \
278 /dev/disk/by-uuid/* \
279 /dev/disk/by-label/* \
280 /dev/disk/by-partuuid/* \
281 /dev/disk/by-partlabel/* \
282 /dev/disk/by-id/* \
283 /dev/disk/by-path/*; do
284 [[ -e $i ]] || continue
285 [[ $i == /dev/mapper/control ]] && continue
286 [[ $i == /dev/mapper/mpath* ]] && continue
287 _tmp=$(get_maj_min "$i")
288 if [ "$_tmp" = "$_dev" ]; then
289 printf -- "%s" "$i"
290 return
291 fi
292 done
293 printf -- "%s" "$1"
294 }
295
296 expand_persistent_dev() {
297 local _dev=$1
298
299 case "$_dev" in
300 LABEL=*)
301 _dev="/dev/disk/by-label/${_dev#LABEL=}"
302 ;;
303 UUID=*)
304 _dev="${_dev#UUID=}"
305 _dev="${_dev,,}"
306 _dev="/dev/disk/by-uuid/${_dev}"
307 ;;
308 PARTUUID=*)
309 _dev="${_dev#PARTUUID=}"
310 _dev="${_dev,,}"
311 _dev="/dev/disk/by-partuuid/${_dev}"
312 ;;
313 PARTLABEL=*)
314 _dev="/dev/disk/by-partlabel/${_dev#PARTLABEL=}"
315 ;;
316 esac
317 printf "%s" "$_dev"
318 }
319
320 shorten_persistent_dev() {
321 local _dev="$1"
322 case "$_dev" in
323 /dev/disk/by-uuid/*)
324 printf "%s" "UUID=${_dev##*/}"
325 ;;
326 /dev/disk/by-label/*)
327 printf "%s" "LABEL=${_dev##*/}"
328 ;;
329 /dev/disk/by-partuuid/*)
330 printf "%s" "PARTUUID=${_dev##*/}"
331 ;;
332 /dev/disk/by-partlabel/*)
333 printf "%s" "PARTLABEL=${_dev##*/}"
334 ;;
335 *)
336 printf "%s" "$_dev"
337 ;;
338 esac
339 }
340
341 # find_block_device <mountpoint>
342 # Prints the major and minor number of the block device
343 # for a given mountpoint.
344 # Unless $use_fstab is set to "yes" the functions
345 # uses /proc/self/mountinfo as the primary source of the
346 # information and only falls back to /etc/fstab, if the mountpoint
347 # is not found there.
348 # Example:
349 # $ find_block_device /usr
350 # 8:4
351 find_block_device() {
352 local _dev _majmin _find_mpt
353 _find_mpt="$1"
354
355 if [[ $use_fstab != yes ]]; then
356 [[ -d $_find_mpt/. ]]
357 findmnt -e -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" | {
358 while read -r _majmin _dev || [ -n "$_dev" ]; do
359 if [[ -b $_dev ]]; then
360 if ! [[ $_majmin ]] || [[ $_majmin == 0:* ]]; then
361 _majmin=$(get_maj_min "$_dev")
362 fi
363 if [[ $_majmin ]]; then
364 printf "%s\n" "$_majmin"
365 else
366 printf "%s\n" "$_dev"
367 fi
368 return 0
369 fi
370 if [[ $_dev == *:* ]]; then
371 printf "%s\n" "$_dev"
372 return 0
373 fi
374 done
375 return 1
376 } && return 0
377 fi
378 # fall back to /etc/fstab
379
380 findmnt -e --fstab -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" | {
381 while read -r _majmin _dev || [ -n "$_dev" ]; do
382 if ! [[ $_dev ]]; then
383 _dev="$_majmin"
384 unset _majmin
385 fi
386 if [[ -b $_dev ]]; then
387 [[ $_majmin ]] || _majmin=$(get_maj_min "$_dev")
388 if [[ $_majmin ]]; then
389 printf "%s\n" "$_majmin"
390 else
391 printf "%s\n" "$_dev"
392 fi
393 return 0
394 fi
395 if [[ $_dev == *:* ]]; then
396 printf "%s\n" "$_dev"
397 return 0
398 fi
399 done
400 return 1
401 } && return 0
402
403 return 1
404 }
405
406 # find_mp_fstype <mountpoint>
407 # Echo the filesystem type for a given mountpoint.
408 # /proc/self/mountinfo is taken as the primary source of information
409 # and /etc/fstab is used as a fallback.
410 # No newline is appended!
411 # Example:
412 # $ find_mp_fstype /;echo
413 # ext4
414 find_mp_fstype() {
415 local _fs
416
417 if [[ $use_fstab != yes ]]; then
418 findmnt -e -v -n -o 'FSTYPE' --target "$1" | {
419 while read -r _fs || [ -n "$_fs" ]; do
420 [[ $_fs ]] || continue
421 [[ $_fs == "autofs" ]] && continue
422 printf "%s" "$_fs"
423 return 0
424 done
425 return 1
426 } && return 0
427 fi
428
429 findmnt --fstab -e -v -n -o 'FSTYPE' --target "$1" | {
430 while read -r _fs || [ -n "$_fs" ]; do
431 [[ $_fs ]] || continue
432 [[ $_fs == "autofs" ]] && continue
433 printf "%s" "$_fs"
434 return 0
435 done
436 return 1
437 } && return 0
438
439 return 1
440 }
441
442 # find_dev_fstype <device>
443 # Echo the filesystem type for a given device.
444 # /proc/self/mountinfo is taken as the primary source of information
445 # and /etc/fstab is used as a fallback.
446 # No newline is appended!
447 # Example:
448 # $ find_dev_fstype /dev/sda2;echo
449 # ext4
450 find_dev_fstype() {
451 local _find_dev _fs
452 _find_dev="$1"
453 if ! [[ $_find_dev == /dev* ]]; then
454 [[ -b "/dev/block/$_find_dev" ]] && _find_dev="/dev/block/$_find_dev"
455 fi
456
457 if [[ $use_fstab != yes ]]; then
458 findmnt -e -v -n -o 'FSTYPE' --source "$_find_dev" | {
459 while read -r _fs || [ -n "$_fs" ]; do
460 [[ $_fs ]] || continue
461 [[ $_fs == "autofs" ]] && continue
462 printf "%s" "$_fs"
463 return 0
464 done
465 return 1
466 } && return 0
467 fi
468
469 findmnt --fstab -e -v -n -o 'FSTYPE' --source "$_find_dev" | {
470 while read -r _fs || [ -n "$_fs" ]; do
471 [[ $_fs ]] || continue
472 [[ $_fs == "autofs" ]] && continue
473 printf "%s" "$_fs"
474 return 0
475 done
476 return 1
477 } && return 0
478
479 return 1
480 }
481
482 # find_mp_fsopts <mountpoint>
483 # Echo the filesystem options for a given mountpoint.
484 # /proc/self/mountinfo is taken as the primary source of information
485 # and /etc/fstab is used as a fallback.
486 # No newline is appended!
487 # Example:
488 # $ find_mp_fsopts /;echo
489 # rw,relatime,discard,data=ordered
490 find_mp_fsopts() {
491 if [[ $use_fstab != yes ]]; then
492 findmnt -e -v -n -o 'OPTIONS' --target "$1" 2> /dev/null && return 0
493 fi
494
495 findmnt --fstab -e -v -n -o 'OPTIONS' --target "$1"
496 }
497
498 # find_dev_fsopts <device>
499 # Echo the filesystem options for a given device.
500 # /proc/self/mountinfo is taken as the primary source of information
501 # and /etc/fstab is used as a fallback.
502 # if `use_fstab == yes`, then only `/etc/fstab` is used.
503 #
504 # Example:
505 # $ find_dev_fsopts /dev/sda2
506 # rw,relatime,discard,data=ordered
507 find_dev_fsopts() {
508 local _find_dev
509 _find_dev="$1"
510 if ! [[ $_find_dev == /dev* ]]; then
511 [[ -b "/dev/block/$_find_dev" ]] && _find_dev="/dev/block/$_find_dev"
512 fi
513
514 if [[ $use_fstab != yes ]]; then
515 findmnt -e -v -n -o 'OPTIONS' --source "$_find_dev" 2> /dev/null && return 0
516 fi
517
518 findmnt --fstab -e -v -n -o 'OPTIONS' --source "$_find_dev"
519 }
520
521 # finds the major:minor of the block device backing the root filesystem.
522 find_root_block_device() { find_block_device /; }
523
524 # for_each_host_dev_fs <func>
525 # Execute "<func> <dev> <filesystem>" for every "<dev> <fs>" pair found
526 # in ${host_fs_types[@]}
527 for_each_host_dev_fs() {
528 local _func="$1"
529 local _dev
530 local _ret=1
531
532 [[ "${#host_fs_types[@]}" ]] || return 2
533
534 for _dev in "${!host_fs_types[@]}"; do
535 $_func "$_dev" "${host_fs_types[$_dev]}" && _ret=0
536 done
537 return $_ret
538 }
539
540 host_fs_all() {
541 printf "%s\n" "${host_fs_types[@]}"
542 }
543
544 # Walk all the slave relationships for a given block device.
545 # Stop when our helper function returns success
546 # $1 = function to call on every found block device
547 # $2 = block device in major:minor format
548 check_block_and_slaves() {
549 local _x
550 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
551 if ! lvm_internal_dev "$2"; then "$1" "$2" && return; fi
552 check_vol_slaves "$@" && return 0
553 if [[ -f /sys/dev/block/$2/../dev ]] && [[ /sys/dev/block/$2/../subsystem -ef /sys/class/block ]]; then
554 check_block_and_slaves "$1" "$(< "/sys/dev/block/$2/../dev")" && return 0
555 fi
556 for _x in /sys/dev/block/"$2"/slaves/*; do
557 [[ -f $_x/dev ]] || continue
558 [[ $_x/subsystem -ef /sys/class/block ]] || continue
559 check_block_and_slaves "$1" "$(< "$_x/dev")" && return 0
560 done
561 return 1
562 }
563
564 check_block_and_slaves_all() {
565 local _x _ret=1
566 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
567 if ! lvm_internal_dev "$2" && "$1" "$2"; then
568 _ret=0
569 fi
570 check_vol_slaves_all "$@" && return 0
571 if [[ -f /sys/dev/block/$2/../dev ]] && [[ /sys/dev/block/$2/../subsystem -ef /sys/class/block ]]; then
572 check_block_and_slaves_all "$1" "$(< "/sys/dev/block/$2/../dev")" && _ret=0
573 fi
574 for _x in /sys/dev/block/"$2"/slaves/*; do
575 [[ -f $_x/dev ]] || continue
576 [[ $_x/subsystem -ef /sys/class/block ]] || continue
577 check_block_and_slaves_all "$1" "$(< "$_x/dev")" && _ret=0
578 done
579 return $_ret
580 }
581 # for_each_host_dev_and_slaves <func>
582 # Execute "<func> <dev>" for every "<dev>" found
583 # in ${host_devs[@]} and their slaves
584 for_each_host_dev_and_slaves_all() {
585 local _func="$1"
586 local _dev
587 local _ret=1
588
589 [[ "${host_devs[*]}" ]] || return 2
590
591 for _dev in "${host_devs[@]}"; do
592 [[ -b $_dev ]] || continue
593 if check_block_and_slaves_all "$_func" "$(get_maj_min "$_dev")"; then
594 _ret=0
595 fi
596 done
597 return $_ret
598 }
599
600 for_each_host_dev_and_slaves() {
601 local _func="$1"
602 local _dev
603
604 [[ "${host_devs[*]}" ]] || return 2
605
606 for _dev in "${host_devs[@]}"; do
607 [[ -b $_dev ]] || continue
608 check_block_and_slaves "$_func" "$(get_maj_min "$_dev")" && return 0
609 done
610 return 1
611 }
612
613 # ugly workaround for the lvm design
614 # There is no volume group device,
615 # so, there are no slave devices for volume groups.
616 # Logical volumes only have the slave devices they really live on,
617 # but you cannot create the logical volume without the volume group.
618 # And the volume group might be bigger than the devices the LV needs.
619 check_vol_slaves() {
620 local _vg _pv _dm _majmin
621 _majmin="$2"
622 _dm=/sys/dev/block/$_majmin/dm
623 [[ -f $_dm/uuid && $(< "$_dm"/uuid) =~ LVM-* ]] || return 1
624 _vg=$(dmsetup splitname --noheadings -o vg_name "$(< "$_dm/name")")
625 # strip space
626 _vg="${_vg//[[:space:]]/}"
627 if [[ $_vg ]]; then
628 for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2> /dev/null); do
629 check_block_and_slaves "$1" "$(get_maj_min "$_pv")" && return 0
630 done
631 fi
632 return 1
633 }
634
635 check_vol_slaves_all() {
636 local _vg _pv _majmin
637 _majmin="$2"
638 _dm="/sys/dev/block/$_majmin/dm"
639 [[ -f $_dm/uuid && $(< "$_dm"/uuid) =~ LVM-* ]] || return 1
640 _vg=$(dmsetup splitname --noheadings -o vg_name "$(< "$_dm/name")")
641 # strip space
642 _vg="${_vg//[[:space:]]/}"
643 if [[ $_vg ]]; then
644 # when filter/global_filter is set, lvm may be failed
645 if ! lvm lvs --noheadings -o vg_name "$_vg" 2> /dev/null 1> /dev/null; then
646 return 1
647 fi
648
649 for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2> /dev/null); do
650 check_block_and_slaves_all "$1" "$(get_maj_min "$_pv")"
651 done
652 return 0
653 fi
654 return 1
655 }
656
657 # fs_get_option <filesystem options> <search for option>
658 # search for a specific option in a bunch of filesystem options
659 # and return the value
660 fs_get_option() {
661 local _fsopts=$1
662 local _option=$2
663 local OLDIFS="$IFS"
664 IFS=,
665 # shellcheck disable=SC2086
666 set -- $_fsopts
667 IFS="$OLDIFS"
668 while [ $# -gt 0 ]; do
669 case $1 in
670 $_option=*)
671 echo "${1#${_option}=}"
672 break
673 ;;
674 esac
675 shift
676 done
677 }
678
679 check_kernel_config() {
680 local _config_opt="$1"
681 local _config_file
682 [[ -f $dracutsysrootdir/boot/config-$kernel ]] \
683 && _config_file="/boot/config-$kernel"
684 [[ -f $dracutsysrootdir/lib/modules/$kernel/config ]] \
685 && _config_file="/lib/modules/$kernel/config"
686
687 # no kernel config file, so return true
688 [[ $_config_file ]] || return 0
689
690 grep -q -F "${_config_opt}=" "$dracutsysrootdir$_config_file" && return 0
691 return 1
692 }
693
694 # 0 if the kernel module is either built-in or available
695 # 1 if the kernel module is not enabled
696 check_kernel_module() {
697 modprobe -S "$kernel" --dry-run "$1" &> /dev/null || return 1
698 }
699
700 # get_cpu_vendor
701 # Only two values are returned: AMD or Intel
702 get_cpu_vendor() {
703 if grep -qE AMD /proc/cpuinfo; then
704 printf "AMD"
705 fi
706 if grep -qE Intel /proc/cpuinfo; then
707 printf "Intel"
708 fi
709 }
710
711 # get_host_ucode
712 # Get the hosts' ucode file based on the /proc/cpuinfo
713 get_ucode_file() {
714 local family
715 local model
716 local stepping
717 family=$(grep -E "cpu family" /proc/cpuinfo | head -1 | sed "s/.*:\ //")
718 model=$(grep -E "model" /proc/cpuinfo | grep -v name | head -1 | sed "s/.*:\ //")
719 stepping=$(grep -E "stepping" /proc/cpuinfo | head -1 | sed "s/.*:\ //")
720
721 if [[ "$(get_cpu_vendor)" == "AMD" ]]; then
722 if [[ $family -ge 21 ]]; then
723 printf "microcode_amd_fam%xh.bin" "$family"
724 else
725 printf "microcode_amd.bin"
726 fi
727 fi
728 if [[ "$(get_cpu_vendor)" == "Intel" ]]; then
729 # The /proc/cpuinfo are in decimal.
730 printf "%02x-%02x-%02x" "${family}" "${model}" "${stepping}"
731 fi
732 }
733
734 # Not every device in /dev/mapper should be examined.
735 # If it is an LVM device, touch only devices which have /dev/VG/LV symlink.
736 lvm_internal_dev() {
737 local dev_dm_dir=/sys/dev/block/$1/dm
738 [[ ! -f $dev_dm_dir/uuid || $(< "$dev_dm_dir"/uuid) != LVM-* ]] && return 1 # Not an LVM device
739 local DM_VG_NAME DM_LV_NAME DM_LV_LAYER
740 eval "$(dmsetup splitname --nameprefixes --noheadings --rows "$(< "$dev_dm_dir"/name)" 2> /dev/null)"
741 [[ ${DM_VG_NAME} ]] && [[ ${DM_LV_NAME} ]] || return 0 # Better skip this!
742 [[ ${DM_LV_LAYER} ]] || [[ ! -L /dev/${DM_VG_NAME}/${DM_LV_NAME} ]]
743 }
744
745 btrfs_devs() {
746 local _mp="$1"
747 btrfs device usage "$_mp" \
748 | while read -r _dev _; do
749 str_starts "$_dev" "/" || continue
750 _dev=${_dev%,}
751 printf -- "%s\n" "$_dev"
752 done
753 }
754
755 iface_for_remote_addr() {
756 # shellcheck disable=SC2046
757 set -- $(ip -o route get to "$1")
758 echo "$3"
759 }
760
761 local_addr_for_remote_addr() {
762 # shellcheck disable=SC2046
763 set -- $(ip -o route get to "$1")
764 echo "$5"
765 }
766
767 peer_for_addr() {
768 local addr=$1
769 local qtd
770
771 # quote periods in IPv4 address
772 qtd=${addr//./\\.}
773 ip -o addr show \
774 | sed -n 's%^.* '"$qtd"' peer \([0-9a-f.:]\{1,\}\(/[0-9]*\)\?\).*$%\1%p'
775 }
776
777 netmask_for_addr() {
778 local addr=$1
779 local qtd
780
781 # quote periods in IPv4 address
782 qtd=${addr//./\\.}
783 ip -o addr show | sed -n 's,^.* '"$qtd"'/\([0-9]*\) .*$,\1,p'
784 }
785
786 gateway_for_iface() {
787 local ifname=$1 addr=$2
788
789 case $addr in
790 *.*) proto=4 ;;
791 *:*) proto=6 ;;
792 *) return ;;
793 esac
794 ip -o -$proto route show \
795 | sed -n "s/^default via \([0-9a-z.:]\{1,\}\) dev $ifname .*\$/\1/p"
796 }
797
798 # This works only for ifcfg-style network configuration!
799 bootproto_for_iface() {
800 local ifname=$1
801 local dir
802
803 # follow ifcfg settings for boot protocol
804 for dir in network-scripts network; do
805 [ -f "/etc/sysconfig/$dir/ifcfg-$ifname" ] && {
806 sed -n "s/BOOTPROTO=[\"']\?\([[:alnum:]]\{1,\}\)[\"']\?.*\$/\1/p" \
807 "/etc/sysconfig/$dir/ifcfg-$ifname"
808 return
809 }
810 done
811 }
812
813 is_unbracketed_ipv6_address() {
814 strglob "$1" '*:*' && ! strglob "$1" '\[*:*\]'
815 }
816
817 # Create an ip= string to set up networking such that the given
818 # remote address can be reached
819 ip_params_for_remote_addr() {
820 local remote_addr=$1
821 local ifname local_addr peer netmask gateway ifmac
822
823 [[ $remote_addr ]] || return 1
824 ifname=$(iface_for_remote_addr "$remote_addr")
825 [[ $ifname ]] || {
826 berror "failed to determine interface to connect to $remote_addr"
827 return 1
828 }
829
830 # ifname clause to bind the interface name to a MAC address
831 if [ -d "/sys/class/net/$ifname/bonding" ]; then
832 dinfo "Found bonded interface '${ifname}'. Make sure to provide an appropriate 'bond=' cmdline."
833 elif [ -e "/sys/class/net/$ifname/address" ]; then
834 ifmac=$(cat "/sys/class/net/$ifname/address")
835 [[ $ifmac ]] && printf 'ifname=%s:%s ' "${ifname}" "${ifmac}"
836 fi
837
838 bootproto=$(bootproto_for_iface "$ifname")
839 case $bootproto in
840 dhcp | dhcp6 | auto6) ;;
841 dhcp4)
842 bootproto=dhcp
843 ;;
844 static* | "")
845 bootproto=
846 ;;
847 *)
848 derror "bootproto \"$bootproto\" is unsupported by dracut, trying static configuration"
849 bootproto=
850 ;;
851 esac
852 if [[ $bootproto ]]; then
853 printf 'ip=%s:%s ' "${ifname}" "${bootproto}"
854 else
855 local_addr=$(local_addr_for_remote_addr "$remote_addr")
856 [[ $local_addr ]] || {
857 berror "failed to determine local address to connect to $remote_addr"
858 return 1
859 }
860 peer=$(peer_for_addr "$local_addr")
861 # Set peer or netmask, but not both
862 [[ $peer ]] || netmask=$(netmask_for_addr "$local_addr")
863 gateway=$(gateway_for_iface "$ifname" "$local_addr")
864 # Quote IPv6 addresses with brackets
865 is_unbracketed_ipv6_address "$local_addr" && local_addr="[$local_addr]"
866 is_unbracketed_ipv6_address "$peer" && peer="[$peer]"
867 is_unbracketed_ipv6_address "$gateway" && gateway="[$gateway]"
868 printf 'ip=%s:%s:%s:%s::%s:none ' \
869 "${local_addr}" "${peer}" "${gateway}" "${netmask}" "${ifname}"
870 fi
871
872 }
873
874 # block_is_nbd <maj:min>
875 # Check whether $1 is an nbd device
876 block_is_nbd() {
877 [[ -b /dev/block/$1 && $1 == 43:* ]]
878 }
879
880 # block_is_iscsi <maj:min>
881 # Check whether $1 is an nbd device
882 block_is_iscsi() {
883 local _dir
884 local _dev=$1
885 [[ -L "/sys/dev/block/$_dev" ]] || return
886 _dir="$(readlink -f "/sys/dev/block/$_dev")" || return
887 until [[ -d "$_dir/sys" || -d "$_dir/iscsi_session" ]]; do
888 _dir="$_dir/.."
889 done
890 [[ -d "$_dir/iscsi_session" ]]
891 }
892
893 # block_is_fcoe <maj:min>
894 # Check whether $1 is an FCoE device
895 # Will not work for HBAs that hide the ethernet aspect
896 # completely and present a pure FC device
897 block_is_fcoe() {
898 local _dir
899 local _dev=$1
900 [[ -L "/sys/dev/block/$_dev" ]] || return
901 _dir="$(readlink -f "/sys/dev/block/$_dev")"
902 until [[ -d "$_dir/sys" ]]; do
903 _dir="$_dir/.."
904 if [[ -d "$_dir/subsystem" ]]; then
905 subsystem=$(basename "$(readlink "$_dir"/subsystem)")
906 [[ $subsystem == "fcoe" ]] && return 0
907 fi
908 done
909 return 1
910 }
911
912 # block_is_netdevice <maj:min>
913 # Check whether $1 is a net device
914 block_is_netdevice() {
915 block_is_nbd "$1" || block_is_iscsi "$1" || block_is_fcoe "$1"
916 }
917
918 # get the corresponding kernel modules of a /sys/class/*/* or/dev/* device
919 get_dev_module() {
920 udevadm info -a "$1" | sed -n 's/\s*DRIVERS=="\(\S\+\)"/\1/p'
921 }