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