]> git.ipfire.org Git - thirdparty/dracut.git/blob - dracut-functions.sh
fix(load_fstype): use $1 if $2 is missing
[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 local _out
238
239 if [[ $get_maj_min_cache_file ]]; then
240 _out="$(grep -m1 -oP "^$1 \K\S+$" "$get_maj_min_cache_file")"
241 fi
242
243 if ! [[ "$_out" ]]; then
244 _majmin="$(stat -L -c '%t:%T' "$1" 2> /dev/null)"
245 _out="$(printf "%s" "$((0x${_majmin%:*})):$((0x${_majmin#*:}))")"
246 if [[ $get_maj_min_cache_file ]]; then
247 echo "$1 $_out" >> "$get_maj_min_cache_file"
248 fi
249 fi
250 echo -n "$_out"
251 }
252
253 # get_devpath_block <device>
254 # get the DEVPATH in /sys of a block device
255 get_devpath_block() {
256 local _majmin _i
257 _majmin=$(get_maj_min "$1")
258
259 for _i in /sys/block/*/dev /sys/block/*/*/dev; do
260 [[ -e $_i ]] || continue
261 if [[ $_majmin == "$(< "$_i")" ]]; then
262 printf "%s" "${_i%/dev}"
263 return 0
264 fi
265 done
266 return 1
267 }
268
269 # get a persistent path from a device
270 get_persistent_dev() {
271 local i _tmp _dev _pol
272
273 _dev=$(get_maj_min "$1")
274 [ -z "$_dev" ] && return
275
276 if [[ -n $persistent_policy ]]; then
277 _pol="/dev/disk/${persistent_policy}/*"
278 else
279 _pol=
280 fi
281
282 for i in \
283 $_pol \
284 /dev/mapper/* \
285 /dev/disk/by-uuid/* \
286 /dev/disk/by-label/* \
287 /dev/disk/by-partuuid/* \
288 /dev/disk/by-partlabel/* \
289 /dev/disk/by-id/* \
290 /dev/disk/by-path/*; do
291 [[ -e $i ]] || continue
292 [[ $i == /dev/mapper/control ]] && continue
293 [[ $i == /dev/mapper/mpath* ]] && continue
294 _tmp=$(get_maj_min "$i")
295 if [ "$_tmp" = "$_dev" ]; then
296 printf -- "%s" "$i"
297 return
298 fi
299 done
300 printf -- "%s" "$1"
301 }
302
303 expand_persistent_dev() {
304 local _dev=$1
305
306 case "$_dev" in
307 LABEL=*)
308 _dev="/dev/disk/by-label/${_dev#LABEL=}"
309 ;;
310 UUID=*)
311 _dev="${_dev#UUID=}"
312 _dev="${_dev,,}"
313 _dev="/dev/disk/by-uuid/${_dev}"
314 ;;
315 PARTUUID=*)
316 _dev="${_dev#PARTUUID=}"
317 _dev="${_dev,,}"
318 _dev="/dev/disk/by-partuuid/${_dev}"
319 ;;
320 PARTLABEL=*)
321 _dev="/dev/disk/by-partlabel/${_dev#PARTLABEL=}"
322 ;;
323 esac
324 printf "%s" "$_dev"
325 }
326
327 shorten_persistent_dev() {
328 local _dev="$1"
329 case "$_dev" in
330 /dev/disk/by-uuid/*)
331 printf "%s" "UUID=${_dev##*/}"
332 ;;
333 /dev/disk/by-label/*)
334 printf "%s" "LABEL=${_dev##*/}"
335 ;;
336 /dev/disk/by-partuuid/*)
337 printf "%s" "PARTUUID=${_dev##*/}"
338 ;;
339 /dev/disk/by-partlabel/*)
340 printf "%s" "PARTLABEL=${_dev##*/}"
341 ;;
342 *)
343 printf "%s" "$_dev"
344 ;;
345 esac
346 }
347
348 # find_block_device <mountpoint>
349 # Prints the major and minor number of the block device
350 # for a given mountpoint.
351 # Unless $use_fstab is set to "yes" the functions
352 # uses /proc/self/mountinfo as the primary source of the
353 # information and only falls back to /etc/fstab, if the mountpoint
354 # is not found there.
355 # Example:
356 # $ find_block_device /usr
357 # 8:4
358 find_block_device() {
359 local _dev _majmin _find_mpt
360 _find_mpt="$1"
361
362 if [[ $use_fstab != yes ]]; then
363 [[ -d $_find_mpt/. ]]
364 findmnt -e -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" | {
365 while read -r _majmin _dev || [ -n "$_dev" ]; do
366 if [[ -b $_dev ]]; then
367 if ! [[ $_majmin ]] || [[ $_majmin == 0:* ]]; then
368 _majmin=$(get_maj_min "$_dev")
369 fi
370 if [[ $_majmin ]]; then
371 printf "%s\n" "$_majmin"
372 else
373 printf "%s\n" "$_dev"
374 fi
375 return 0
376 fi
377 if [[ $_dev == *:* ]]; then
378 printf "%s\n" "$_dev"
379 return 0
380 fi
381 done
382 return 1
383 } && return 0
384 fi
385 # fall back to /etc/fstab
386
387 findmnt -e --fstab -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" | {
388 while read -r _majmin _dev || [ -n "$_dev" ]; do
389 if ! [[ $_dev ]]; then
390 _dev="$_majmin"
391 unset _majmin
392 fi
393 if [[ -b $_dev ]]; then
394 [[ $_majmin ]] || _majmin=$(get_maj_min "$_dev")
395 if [[ $_majmin ]]; then
396 printf "%s\n" "$_majmin"
397 else
398 printf "%s\n" "$_dev"
399 fi
400 return 0
401 fi
402 if [[ $_dev == *:* ]]; then
403 printf "%s\n" "$_dev"
404 return 0
405 fi
406 done
407 return 1
408 } && return 0
409
410 return 1
411 }
412
413 # find_mp_fstype <mountpoint>
414 # Echo the filesystem type for a given mountpoint.
415 # /proc/self/mountinfo is taken as the primary source of information
416 # and /etc/fstab is used as a fallback.
417 # No newline is appended!
418 # Example:
419 # $ find_mp_fstype /;echo
420 # ext4
421 find_mp_fstype() {
422 local _fs
423
424 if [[ $use_fstab != yes ]]; then
425 findmnt -e -v -n -o 'FSTYPE' --target "$1" | {
426 while read -r _fs || [ -n "$_fs" ]; do
427 [[ $_fs ]] || continue
428 [[ $_fs == "autofs" ]] && continue
429 printf "%s" "$_fs"
430 return 0
431 done
432 return 1
433 } && return 0
434 fi
435
436 findmnt --fstab -e -v -n -o 'FSTYPE' --target "$1" | {
437 while read -r _fs || [ -n "$_fs" ]; do
438 [[ $_fs ]] || continue
439 [[ $_fs == "autofs" ]] && continue
440 printf "%s" "$_fs"
441 return 0
442 done
443 return 1
444 } && return 0
445
446 return 1
447 }
448
449 # find_dev_fstype <device>
450 # Echo the filesystem type for a given device.
451 # /proc/self/mountinfo is taken as the primary source of information
452 # and /etc/fstab is used as a fallback.
453 # No newline is appended!
454 # Example:
455 # $ find_dev_fstype /dev/sda2;echo
456 # ext4
457 find_dev_fstype() {
458 local _find_dev _fs
459 _find_dev="$1"
460 if ! [[ $_find_dev == /dev* ]]; then
461 [[ -b "/dev/block/$_find_dev" ]] && _find_dev="/dev/block/$_find_dev"
462 fi
463
464 if [[ $use_fstab != yes ]]; then
465 findmnt -e -v -n -o 'FSTYPE' --source "$_find_dev" | {
466 while read -r _fs || [ -n "$_fs" ]; do
467 [[ $_fs ]] || continue
468 [[ $_fs == "autofs" ]] && continue
469 printf "%s" "$_fs"
470 return 0
471 done
472 return 1
473 } && return 0
474 fi
475
476 findmnt --fstab -e -v -n -o 'FSTYPE' --source "$_find_dev" | {
477 while read -r _fs || [ -n "$_fs" ]; do
478 [[ $_fs ]] || continue
479 [[ $_fs == "autofs" ]] && continue
480 printf "%s" "$_fs"
481 return 0
482 done
483 return 1
484 } && return 0
485
486 return 1
487 }
488
489 # find_mp_fsopts <mountpoint>
490 # Echo the filesystem options for a given mountpoint.
491 # /proc/self/mountinfo is taken as the primary source of information
492 # and /etc/fstab is used as a fallback.
493 # No newline is appended!
494 # Example:
495 # $ find_mp_fsopts /;echo
496 # rw,relatime,discard,data=ordered
497 find_mp_fsopts() {
498 if [[ $use_fstab != yes ]]; then
499 findmnt -e -v -n -o 'OPTIONS' --target "$1" 2> /dev/null && return 0
500 fi
501
502 findmnt --fstab -e -v -n -o 'OPTIONS' --target "$1"
503 }
504
505 # find_dev_fsopts <device>
506 # Echo the filesystem options for a given device.
507 # /proc/self/mountinfo is taken as the primary source of information
508 # and /etc/fstab is used as a fallback.
509 # if `use_fstab == yes`, then only `/etc/fstab` is used.
510 #
511 # Example:
512 # $ find_dev_fsopts /dev/sda2
513 # rw,relatime,discard,data=ordered
514 find_dev_fsopts() {
515 local _find_dev
516 _find_dev="$1"
517 if ! [[ $_find_dev == /dev* ]]; then
518 [[ -b "/dev/block/$_find_dev" ]] && _find_dev="/dev/block/$_find_dev"
519 fi
520
521 if [[ $use_fstab != yes ]]; then
522 findmnt -e -v -n -o 'OPTIONS' --source "$_find_dev" 2> /dev/null && return 0
523 fi
524
525 findmnt --fstab -e -v -n -o 'OPTIONS' --source "$_find_dev"
526 }
527
528 # finds the major:minor of the block device backing the root filesystem.
529 find_root_block_device() { find_block_device /; }
530
531 # for_each_host_dev_fs <func>
532 # Execute "<func> <dev> <filesystem>" for every "<dev> <fs>" pair found
533 # in ${host_fs_types[@]}
534 for_each_host_dev_fs() {
535 local _func="$1"
536 local _dev
537 local _ret=1
538
539 [[ "${#host_fs_types[@]}" ]] || return 2
540
541 for _dev in "${!host_fs_types[@]}"; do
542 $_func "$_dev" "${host_fs_types[$_dev]}" && _ret=0
543 done
544 return $_ret
545 }
546
547 host_fs_all() {
548 printf "%s\n" "${host_fs_types[@]}"
549 }
550
551 # Walk all the slave relationships for a given block device.
552 # Stop when our helper function returns success
553 # $1 = function to call on every found block device
554 # $2 = block device in major:minor format
555 check_block_and_slaves() {
556 local _x
557 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
558 if ! lvm_internal_dev "$2"; then "$1" "$2" && return; fi
559 check_vol_slaves "$@" && return 0
560 if [[ -f /sys/dev/block/$2/../dev ]] && [[ /sys/dev/block/$2/../subsystem -ef /sys/class/block ]]; then
561 check_block_and_slaves "$1" "$(< "/sys/dev/block/$2/../dev")" && return 0
562 fi
563 for _x in /sys/dev/block/"$2"/slaves/*; do
564 [[ -f $_x/dev ]] || continue
565 [[ $_x/subsystem -ef /sys/class/block ]] || continue
566 check_block_and_slaves "$1" "$(< "$_x/dev")" && return 0
567 done
568 return 1
569 }
570
571 check_block_and_slaves_all() {
572 local _x _ret=1
573 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
574 if ! lvm_internal_dev "$2" && "$1" "$2"; then
575 _ret=0
576 fi
577 check_vol_slaves_all "$@" && return 0
578 if [[ -f /sys/dev/block/$2/../dev ]] && [[ /sys/dev/block/$2/../subsystem -ef /sys/class/block ]]; then
579 check_block_and_slaves_all "$1" "$(< "/sys/dev/block/$2/../dev")" && _ret=0
580 fi
581 for _x in /sys/dev/block/"$2"/slaves/*; do
582 [[ -f $_x/dev ]] || continue
583 [[ $_x/subsystem -ef /sys/class/block ]] || continue
584 check_block_and_slaves_all "$1" "$(< "$_x/dev")" && _ret=0
585 done
586 return $_ret
587 }
588 # for_each_host_dev_and_slaves <func>
589 # Execute "<func> <dev>" for every "<dev>" found
590 # in ${host_devs[@]} and their slaves
591 for_each_host_dev_and_slaves_all() {
592 local _func="$1"
593 local _dev
594 local _ret=1
595
596 [[ "${host_devs[*]}" ]] || return 2
597
598 for _dev in "${host_devs[@]}"; do
599 [[ -b $_dev ]] || continue
600 if check_block_and_slaves_all "$_func" "$(get_maj_min "$_dev")"; then
601 _ret=0
602 fi
603 done
604 return $_ret
605 }
606
607 for_each_host_dev_and_slaves() {
608 local _func="$1"
609 local _dev
610
611 [[ "${host_devs[*]}" ]] || return 2
612
613 for _dev in "${host_devs[@]}"; do
614 [[ -b $_dev ]] || continue
615 check_block_and_slaves "$_func" "$(get_maj_min "$_dev")" && return 0
616 done
617 return 1
618 }
619
620 # /sys/dev/block/major:minor is symbol link to real hardware device
621 # go downstream $(realpath /sys/dev/block/major:minor) to detect driver
622 get_blockdev_drv_through_sys() {
623 local _block_mods=""
624 local _path
625
626 _path=$(realpath "$1")
627 while true; do
628 if [[ -L "$_path"/driver/module ]]; then
629 _mod=$(realpath "$_path"/driver/module)
630 _mod=$(basename "$_mod")
631 _block_mods="$_block_mods $_mod"
632 fi
633 _path=$(dirname "$_path")
634 if [[ $_path == '/sys/devices' ]] || [[ $_path == '/' ]]; then
635 break
636 fi
637 done
638 echo "$_block_mods"
639 }
640
641 # ugly workaround for the lvm design
642 # There is no volume group device,
643 # so, there are no slave devices for volume groups.
644 # Logical volumes only have the slave devices they really live on,
645 # but you cannot create the logical volume without the volume group.
646 # And the volume group might be bigger than the devices the LV needs.
647 check_vol_slaves() {
648 local _vg _pv _dm _majmin
649 _majmin="$2"
650 _dm=/sys/dev/block/$_majmin/dm
651 [[ -f $_dm/uuid && $(< "$_dm"/uuid) =~ LVM-* ]] || return 1
652 _vg=$(dmsetup splitname --noheadings -o vg_name "$(< "$_dm/name")")
653 # strip space
654 _vg="${_vg//[[:space:]]/}"
655 if [[ $_vg ]]; then
656 for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2> /dev/null); do
657 check_block_and_slaves "$1" "$(get_maj_min "$_pv")" && return 0
658 done
659 fi
660 return 1
661 }
662
663 check_vol_slaves_all() {
664 local _vg _pv _majmin
665 _majmin="$2"
666 _dm="/sys/dev/block/$_majmin/dm"
667 [[ -f $_dm/uuid && $(< "$_dm"/uuid) =~ LVM-* ]] || return 1
668 _vg=$(dmsetup splitname --noheadings -o vg_name "$(< "$_dm/name")")
669 # strip space
670 _vg="${_vg//[[:space:]]/}"
671 if [[ $_vg ]]; then
672 # when filter/global_filter is set, lvm may be failed
673 if ! lvm lvs --noheadings -o vg_name "$_vg" 2> /dev/null 1> /dev/null; then
674 return 1
675 fi
676
677 for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2> /dev/null); do
678 check_block_and_slaves_all "$1" "$(get_maj_min "$_pv")"
679 done
680 return 0
681 fi
682 return 1
683 }
684
685 # fs_get_option <filesystem options> <search for option>
686 # search for a specific option in a bunch of filesystem options
687 # and return the value
688 fs_get_option() {
689 local _fsopts=$1
690 local _option=$2
691 local OLDIFS="$IFS"
692 IFS=,
693 # shellcheck disable=SC2086
694 set -- $_fsopts
695 IFS="$OLDIFS"
696 while [ $# -gt 0 ]; do
697 case $1 in
698 $_option=*)
699 echo "${1#${_option}=}"
700 break
701 ;;
702 esac
703 shift
704 done
705 }
706
707 check_kernel_config() {
708 local _config_opt="$1"
709 local _config_file
710 [[ -f $dracutsysrootdir/boot/config-$kernel ]] \
711 && _config_file="/boot/config-$kernel"
712 [[ -f $dracutsysrootdir/lib/modules/$kernel/config ]] \
713 && _config_file="/lib/modules/$kernel/config"
714
715 # no kernel config file, so return true
716 [[ $_config_file ]] || return 0
717
718 grep -q -F "${_config_opt}=" "$dracutsysrootdir$_config_file" && return 0
719 return 1
720 }
721
722 # 0 if the kernel module is either built-in or available
723 # 1 if the kernel module is not enabled
724 check_kernel_module() {
725 modprobe -S "$kernel" --dry-run "$1" &> /dev/null || return 1
726 }
727
728 # get_cpu_vendor
729 # Only two values are returned: AMD or Intel
730 get_cpu_vendor() {
731 if grep -qE AMD /proc/cpuinfo; then
732 printf "AMD"
733 fi
734 if grep -qE Intel /proc/cpuinfo; then
735 printf "Intel"
736 fi
737 }
738
739 # get_host_ucode
740 # Get the hosts' ucode file based on the /proc/cpuinfo
741 get_ucode_file() {
742 local family
743 local model
744 local stepping
745 family=$(grep -E "cpu family" /proc/cpuinfo | head -1 | sed "s/.*:\ //")
746 model=$(grep -E "model" /proc/cpuinfo | grep -v name | head -1 | sed "s/.*:\ //")
747 stepping=$(grep -E "stepping" /proc/cpuinfo | head -1 | sed "s/.*:\ //")
748
749 if [[ "$(get_cpu_vendor)" == "AMD" ]]; then
750 if [[ $family -ge 21 ]]; then
751 printf "microcode_amd_fam%xh.bin" "$family"
752 else
753 printf "microcode_amd.bin"
754 fi
755 fi
756 if [[ "$(get_cpu_vendor)" == "Intel" ]]; then
757 # The /proc/cpuinfo are in decimal.
758 printf "%02x-%02x-%02x" "${family}" "${model}" "${stepping}"
759 fi
760 }
761
762 # Not every device in /dev/mapper should be examined.
763 # If it is an LVM device, touch only devices which have /dev/VG/LV symlink.
764 lvm_internal_dev() {
765 local dev_dm_dir=/sys/dev/block/$1/dm
766 [[ ! -f $dev_dm_dir/uuid || $(< "$dev_dm_dir"/uuid) != LVM-* ]] && return 1 # Not an LVM device
767 local DM_VG_NAME DM_LV_NAME DM_LV_LAYER
768 eval "$(dmsetup splitname --nameprefixes --noheadings --rows "$(< "$dev_dm_dir"/name)" 2> /dev/null)"
769 [[ ${DM_VG_NAME} ]] && [[ ${DM_LV_NAME} ]] || return 0 # Better skip this!
770 [[ ${DM_LV_LAYER} ]] || [[ ! -L /dev/${DM_VG_NAME}/${DM_LV_NAME} ]]
771 }
772
773 btrfs_devs() {
774 local _mp="$1"
775 btrfs device usage "$_mp" \
776 | while read -r _dev _; do
777 str_starts "$_dev" "/" || continue
778 _dev=${_dev%,}
779 printf -- "%s\n" "$_dev"
780 done
781 }
782
783 zfs_devs() {
784 local _mp="$1"
785 zpool list -H -v -P "${_mp%%/*}" | awk -F$'\t' '$2 ~ /^\// {print $2}' \
786 | while read -r _dev; do
787 realpath "${_dev}"
788 done
789 }
790
791 iface_for_remote_addr() {
792 # shellcheck disable=SC2046
793 set -- $(ip -o route get to "$1")
794 while [ $# -gt 0 ]; do
795 case $1 in
796 dev)
797 echo "$2"
798 return
799 ;;
800 esac
801 shift
802 done
803 }
804
805 local_addr_for_remote_addr() {
806 # shellcheck disable=SC2046
807 set -- $(ip -o route get to "$1")
808 while [ $# -gt 0 ]; do
809 case $1 in
810 src)
811 echo "$2"
812 return
813 ;;
814 esac
815 shift
816 done
817 }
818
819 peer_for_addr() {
820 local addr=$1
821 local qtd
822
823 # quote periods in IPv4 address
824 qtd=${addr//./\\.}
825 ip -o addr show \
826 | sed -n 's%^.* '"$qtd"' peer \([0-9a-f.:]\{1,\}\(/[0-9]*\)\?\).*$%\1%p'
827 }
828
829 netmask_for_addr() {
830 local addr=$1
831 local qtd
832
833 # quote periods in IPv4 address
834 qtd=${addr//./\\.}
835 ip -o addr show | sed -n 's,^.* '"$qtd"'/\([0-9]*\) .*$,\1,p'
836 }
837
838 gateway_for_iface() {
839 local ifname=$1 addr=$2
840
841 case $addr in
842 *.*) proto=4 ;;
843 *:*) proto=6 ;;
844 *) return ;;
845 esac
846 ip -o -$proto route show \
847 | sed -n "s/^default via \([0-9a-z.:]\{1,\}\) dev $ifname .*\$/\1/p"
848 }
849
850 # This works only for ifcfg-style network configuration!
851 bootproto_for_iface() {
852 local ifname=$1
853 local dir
854
855 # follow ifcfg settings for boot protocol
856 for dir in network-scripts network; do
857 [ -f "/etc/sysconfig/$dir/ifcfg-$ifname" ] && {
858 sed -n "s/BOOTPROTO=[\"']\?\([[:alnum:]]\{1,\}\)[\"']\?.*\$/\1/p" \
859 "/etc/sysconfig/$dir/ifcfg-$ifname"
860 return
861 }
862 done
863 }
864
865 is_unbracketed_ipv6_address() {
866 strglob "$1" '*:*' && ! strglob "$1" '\[*:*\]'
867 }
868
869 # Create an ip= string to set up networking such that the given
870 # remote address can be reached
871 ip_params_for_remote_addr() {
872 local remote_addr=$1
873 local ifname local_addr peer netmask gateway ifmac
874
875 [[ $remote_addr ]] || return 1
876 ifname=$(iface_for_remote_addr "$remote_addr")
877 [[ $ifname ]] || {
878 berror "failed to determine interface to connect to $remote_addr"
879 return 1
880 }
881
882 # ifname clause to bind the interface name to a MAC address
883 if [ -d "/sys/class/net/$ifname/bonding" ]; then
884 dinfo "Found bonded interface '${ifname}'. Make sure to provide an appropriate 'bond=' cmdline."
885 elif [ -e "/sys/class/net/$ifname/address" ]; then
886 ifmac=$(cat "/sys/class/net/$ifname/address")
887 [[ $ifmac ]] && printf 'ifname=%s:%s ' "${ifname}" "${ifmac}"
888 fi
889
890 bootproto=$(bootproto_for_iface "$ifname")
891 case $bootproto in
892 dhcp | dhcp6 | auto6) ;;
893 dhcp4)
894 bootproto=dhcp
895 ;;
896 static* | "")
897 bootproto=
898 ;;
899 *)
900 derror "bootproto \"$bootproto\" is unsupported by dracut, trying static configuration"
901 bootproto=
902 ;;
903 esac
904 if [[ $bootproto ]]; then
905 printf 'ip=%s:%s ' "${ifname}" "${bootproto}"
906 else
907 local_addr=$(local_addr_for_remote_addr "$remote_addr")
908 [[ $local_addr ]] || {
909 berror "failed to determine local address to connect to $remote_addr"
910 return 1
911 }
912 peer=$(peer_for_addr "$local_addr")
913 # Set peer or netmask, but not both
914 [[ $peer ]] || netmask=$(netmask_for_addr "$local_addr")
915 gateway=$(gateway_for_iface "$ifname" "$local_addr")
916 # Quote IPv6 addresses with brackets
917 is_unbracketed_ipv6_address "$local_addr" && local_addr="[$local_addr]"
918 is_unbracketed_ipv6_address "$peer" && peer="[$peer]"
919 is_unbracketed_ipv6_address "$gateway" && gateway="[$gateway]"
920 printf 'ip=%s:%s:%s:%s::%s:none ' \
921 "${local_addr}" "${peer}" "${gateway}" "${netmask}" "${ifname}"
922 fi
923
924 }
925
926 # block_is_nbd <maj:min>
927 # Check whether $1 is an nbd device
928 block_is_nbd() {
929 [[ -b /dev/block/$1 && $1 == 43:* ]]
930 }
931
932 # block_is_iscsi <maj:min>
933 # Check whether $1 is an iSCSI device
934 block_is_iscsi() {
935 local _dir
936 local _dev=$1
937 [[ -L "/sys/dev/block/$_dev" ]] || return
938 _dir="$(readlink -f "/sys/dev/block/$_dev")" || return
939 until [[ -d "$_dir/sys" || -d "$_dir/iscsi_session" ]]; do
940 _dir="$_dir/.."
941 done
942 [[ -d "$_dir/iscsi_session" ]]
943 }
944
945 # block_is_fcoe <maj:min>
946 # Check whether $1 is an FCoE device
947 # Will not work for HBAs that hide the ethernet aspect
948 # completely and present a pure FC device
949 block_is_fcoe() {
950 local _dir
951 local _dev=$1
952 [[ -L "/sys/dev/block/$_dev" ]] || return
953 _dir="$(readlink -f "/sys/dev/block/$_dev")"
954 until [[ -d "$_dir/sys" ]]; do
955 _dir="$_dir/.."
956 if [[ -d "$_dir/subsystem" ]]; then
957 subsystem=$(basename "$(readlink "$_dir"/subsystem)")
958 [[ $subsystem == "fcoe" ]] && return 0
959 fi
960 done
961 return 1
962 }
963
964 # block_is_netdevice <maj:min>
965 # Check whether $1 is a net device
966 block_is_netdevice() {
967 block_is_nbd "$1" || block_is_iscsi "$1" || block_is_fcoe "$1"
968 }
969
970 # get the corresponding kernel modules of a /sys/class/*/* or/dev/* device
971 get_dev_module() {
972 local dev_attr_walk
973 local dev_drivers
974 dev_attr_walk=$(udevadm info -a "$1")
975 dev_drivers=$(echo "$dev_attr_walk" | sed -n 's/\s*DRIVERS=="\(\S\+\)"/\1/p')
976 # if no kernel modules found and device is in a virtual subsystem, follow symlinks
977 if [[ -z $dev_drivers && $(udevadm info -q path "$1") == "/devices/virtual"* ]]; then
978 local dev_vkernel
979 local dev_vsubsystem
980 local dev_vpath
981 dev_vkernel=$(echo "$dev_attr_walk" | sed -n 's/\s*KERNELS=="\(\S\+\)"/\1/p' | tail -1)
982 dev_vsubsystem=$(echo "$dev_attr_walk" | sed -n 's/\s*SUBSYSTEMS=="\(\S\+\)"/\1/p' | tail -1)
983 dev_vpath="/sys/devices/virtual/$dev_vsubsystem/$dev_vkernel"
984 if [[ -n $dev_vkernel && -n $dev_vsubsystem && -d $dev_vpath ]]; then
985 local dev_links
986 local dev_link
987 dev_links=$(find "$dev_vpath" -maxdepth 1 -type l ! -name "subsystem" -exec readlink {} \;)
988 for dev_link in $dev_links; do
989 [[ -n $dev_drivers && ${dev_drivers: -1} != $'\n' ]] && dev_drivers+=$'\n'
990 dev_drivers+=$(udevadm info -a "$dev_vpath/$dev_link" \
991 | sed -n 's/\s*DRIVERS=="\(\S\+\)"/\1/p' \
992 | grep -v -e pcieport)
993 done
994 fi
995 fi
996 echo "$dev_drivers"
997 }