]> git.ipfire.org Git - thirdparty/dracut.git/blame - dracut-functions.sh
fix(dracut-functions.sh): suppress findmnt error msg if /etc/fstab not exist
[thirdparty/dracut.git] / dracut-functions.sh
CommitLineData
04b56f3a
JK
1#!/bin/bash
2#
33ee031c 3# functions used by dracut and other tools.
04b56f3a 4#
33ee031c 5# Copyright 2005-2009 Red Hat, Inc. All rights reserved.
04b56f3a
JK
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
0f9c78c1 18# along with this program. If not, see <http://www.gnu.org/licenses/>.
04b56f3a 19#
258de828 20export LC_MESSAGES=C
f4031e8a 21
561eb42f
HH
22# is_func <command>
23# Check whether $1 is a function.
24is_func() {
75d758e8 25 [[ "$(type -t "$1")" == "function" ]]
561eb42f
HH
26}
27
8d95b8b3 28# Generic substring function. If $2 is in $1, return 0.
75d758e8 29strstr() { [[ $1 == *"$2"* ]]; }
2c19a5fa 30# Generic glob matching function. If glob pattern $2 matches anywhere in $1, OK
75d758e8 31strglobin() { [[ $1 == *$2* ]]; }
2c19a5fa 32# Generic glob matching function. If glob pattern $2 matches all of $1, OK
3483509e 33# shellcheck disable=SC2053
75d758e8 34strglob() { [[ $1 == $2 ]]; }
1cadc26f
HH
35# returns OK if $1 contains literal string $2 at the beginning, and isn't empty
36str_starts() { [ "${1#"$2"*}" != "$1" ]; }
37# returns OK if $1 contains literal string $2 at the end, and isn't empty
38str_ends() { [ "${1%*"$2"}" != "$1" ]; }
8d95b8b3 39
f4031e8a
HH
40# find a binary. If we were not passed the full path directly,
41# search in the usual places to find the binary.
42find_binary() {
a0120420 43 local _delim
f769154b 44 local _path
a0120420
BZ
45 local l
46 local p
47 [[ -z ${1##/*} ]] || _delim="/"
48
75d758e8 49 if [[ $1 == *.so* ]]; then
3483509e 50 # shellcheck disable=SC2154
9a52c3fd 51 for l in $libdirs; do
f769154b 52 _path="${l}${_delim}${1}"
9a52c3fd 53 if { $DRACUT_LDD "${dracutsysrootdir}${_path}" &> /dev/null; }; then
f769154b 54 printf "%s\n" "${_path}"
a0120420
BZ
55 return 0
56 fi
57 done
f769154b 58 _path="${_delim}${1}"
9a52c3fd 59 if { $DRACUT_LDD "${dracutsysrootdir}${_path}" &> /dev/null; }; then
f769154b 60 printf "%s\n" "${_path}"
f4031e8a
HH
61 return 0
62 fi
63 fi
75d758e8 64 if [[ $1 == */* ]]; then
f769154b
AT
65 _path="${_delim}${1}"
66 if [[ -L ${dracutsysrootdir}${_path} ]] || [[ -x ${dracutsysrootdir}${_path} ]]; then
67 printf "%s\n" "${_path}"
a0120420
BZ
68 return 0
69 fi
70 fi
9a52c3fd 71 for p in $DRACUT_PATH; do
f769154b 72 _path="${p}${_delim}${1}"
9a52c3fd 73 if [[ -L ${dracutsysrootdir}${_path} ]] || [[ -x ${dracutsysrootdir}${_path} ]]; then
f769154b 74 printf "%s\n" "${_path}"
a0120420
BZ
75 return 0
76 fi
77 done
f4031e8a 78
75d758e8 79 [[ -n $dracutsysrootdir ]] && return 1
b093aa2d 80 type -P "${1##*/}"
f4031e8a
HH
81}
82
9a52c3fd
HH
83ldconfig_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
4fe1bdd4
HH
85}
86
f06c2b58
HH
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
91vercmp() {
3483509e 92 local _n1
2b5ddc77 93 read -r -a _n1 <<< "${1//./ }"
3483509e
HH
94 local _op=$2
95 local _n2
2b5ddc77 96 read -r -a _n2 <<< "${3//./ }"
3483509e 97 local _i _res
f06c2b58 98
9a52c3fd
HH
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
f06c2b58
HH
108 fi
109 break
110 done
111
112 case $_op in
9a52c3fd
HH
113 gt) ((_res == 1)) ;;
114 ge) ((_res != 2)) ;;
115 eq) ((_res == 0)) ;;
116 le) ((_res != 1)) ;;
117 lt) ((_res == 2)) ;;
118 ne) ((_res != 0)) ;;
f06c2b58
HH
119 esac
120}
121
87122afc
122# Create all subdirectories for given path without creating the last element.
123# $1 = path
b093aa2d 124mksubdirs() {
3483509e 125 # shellcheck disable=SC2174
b093aa2d
HH
126 [[ -e ${1%/*} ]] || mkdir -m 0755 -p -- "${1%/*}"
127}
87122afc 128
87122afc
129# Function prints global variables in format name=value line by line.
130# $@ = list of global variables' name
131print_vars() {
29b10e65 132 local _var _value
87122afc 133
9a52c3fd 134 for _var in "$@"; do
7a94a432 135 eval printf -v _value "%s" \""\$$_var"\"
b093aa2d 136 [[ ${_value} ]] && printf '%s="%s"\n' "$_var" "$_value"
87122afc
137 done
138}
139
7e2bca48
HH
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
626d9eba 146normalize_path() {
2b5ddc77
HH
147 # shellcheck disable=SC2064
148 trap "$(shopt -p extglob)" RETURN
c44e3cb4 149 shopt -q -s extglob
2b5ddc77
HH
150 local p=${1//+(\/)//}
151 printf "%s\n" "${p%/}"
626d9eba 152}
d4bb4316 153
7e2bca48
HH
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
d4bb4316 160convert_abs_rel() {
6d2a7942 161 local __current __absolute __abssize __cursize __newpath
c44e3cb4 162 local -i __i __level
d4bb4316 163
c44e3cb4
MS
164 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
165
c1609dd4 166 # corner case #1 - self looping link
75d758e8 167 [[ $1 == "$2" ]] && {
9a52c3fd
HH
168 printf "%s\n" "${1##*/}"
169 return
170 }
c1609dd4
MS
171
172 # corner case #2 - own dir link
75d758e8 173 [[ ${1%/*} == "$2" ]] && {
9a52c3fd
HH
174 printf ".\n"
175 return
176 }
c1609dd4 177
2b5ddc77
HH
178 IFS=/ read -r -a __current <<< "$1"
179 IFS=/ read -r -a __absolute <<< "$2"
d4bb4316
HH
180
181 __abssize=${#__absolute[@]}
182 __cursize=${#__current[@]}
183
75d758e8 184 while [[ ${__absolute[__level]} == "${__current[__level]}" ]]; do
9a52c3fd
HH
185 ((__level++))
186 if ((__level > __abssize || __level > __cursize)); then
d4bb4316
HH
187 break
188 fi
189 done
190
9a52c3fd
HH
191 for ((__i = __level; __i < __cursize - 1; __i++)); do
192 if ((__i > __level)); then
d4bb4316
HH
193 __newpath=$__newpath"/"
194 fi
195 __newpath=$__newpath".."
196 done
197
9a52c3fd
HH
198 for ((__i = __level; __i < __abssize; __i++)); do
199 if [[ -n $__newpath ]]; then
d4bb4316
HH
200 __newpath=$__newpath"/"
201 fi
202 __newpath=$__newpath${__absolute[__i]}
203 done
204
3483509e 205 printf -- "%s\n" "$__newpath"
d4bb4316
HH
206}
207
7e2bca48 208# get_fs_env <device>
97af51db 209# Get and the ID_FS_TYPE variable from udev for a device.
7e2bca48 210# Example:
97af51db 211# $ get_fs_env /dev/sda2
7e2bca48 212# ext4
9ede1929 213get_fs_env() {
7c179686 214 [[ $1 ]] || return
29b10e65 215 unset ID_FS_TYPE
69f7ed96 216 ID_FS_TYPE=$(blkid -u filesystem -o export -- "$1" \
2b5ddc77
HH
217 | while read -r line || [ -n "$line" ]; do
218 if [[ $line == "TYPE="* ]]; then
9a52c3fd
HH
219 printf "%s" "${line#TYPE=}"
220 exit 0
69f7ed96 221 fi
9a52c3fd 222 done)
69f7ed96
HH
223 if [[ $ID_FS_TYPE ]]; then
224 printf "%s" "$ID_FS_TYPE"
225 return 0
7c179686 226 fi
a5cde2dd
HH
227 return 1
228}
9ede1929 229
7e2bca48
HH
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
480d772f 235get_maj_min() {
224175d8 236 local _majmin
a277a5fc
HH
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
c3bb9d18 244 _majmin="$(stat -L -c '%t:%T' "$1" 2> /dev/null)"
a277a5fc
HH
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
c3bb9d18 249 fi
a277a5fc 250 echo -n "$_out"
480d772f
HH
251}
252
281327f7
HH
253# get_devpath_block <device>
254# get the DEVPATH in /sys of a block device
255get_devpath_block() {
8552a327 256 local _majmin _i
281327f7
HH
257 _majmin=$(get_maj_min "$1")
258
259 for _i in /sys/block/*/dev /sys/block/*/*/dev; do
75d758e8
HH
260 [[ -e $_i ]] || continue
261 if [[ $_majmin == "$(< "$_i")" ]]; then
281327f7
HH
262 printf "%s" "${_i%/dev}"
263 return 0
264 fi
265 done
266 return 1
267}
268
69f7ed96
HH
269# get a persistent path from a device
270get_persistent_dev() {
b6054b5d 271 local i _tmp _dev _pol
69f7ed96
HH
272
273 _dev=$(get_maj_min "$1")
274 [ -z "$_dev" ] && return
275
75d758e8 276 if [[ -n $persistent_policy ]]; then
9a52c3fd 277 _pol="/dev/disk/${persistent_policy}/*"
b6054b5d 278 else
9a52c3fd 279 _pol=
b6054b5d
MW
280 fi
281
324ea606 282 for i in \
b6054b5d 283 $_pol \
324ea606 284 /dev/mapper/* \
324ea606
HH
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/* \
9a52c3fd 290 /dev/disk/by-path/*; do
75d758e8 291 [[ -e $i ]] || continue
1743473b 292 [[ $i == /dev/mapper/control ]] && continue
69f7ed96
HH
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
9efb74a3 300 printf -- "%s" "$1"
69f7ed96
HH
301}
302
c82a1133
HH
303expand_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=}"
93b02f50 312 _dev="${_dev,,}"
c82a1133
HH
313 _dev="/dev/disk/by-uuid/${_dev}"
314 ;;
315 PARTUUID=*)
316 _dev="${_dev#PARTUUID=}"
93b02f50 317 _dev="${_dev,,}"
c82a1133
HH
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
324ea606 327shorten_persistent_dev() {
c82a1133
HH
328 local _dev="$1"
329 case "$_dev" in
324ea606 330 /dev/disk/by-uuid/*)
9a52c3fd
HH
331 printf "%s" "UUID=${_dev##*/}"
332 ;;
324ea606 333 /dev/disk/by-label/*)
9a52c3fd
HH
334 printf "%s" "LABEL=${_dev##*/}"
335 ;;
324ea606 336 /dev/disk/by-partuuid/*)
9a52c3fd
HH
337 printf "%s" "PARTUUID=${_dev##*/}"
338 ;;
324ea606 339 /dev/disk/by-partlabel/*)
9a52c3fd
HH
340 printf "%s" "PARTLABEL=${_dev##*/}"
341 ;;
324ea606 342 *)
9a52c3fd
HH
343 printf "%s" "$_dev"
344 ;;
324ea606
HH
345 esac
346}
347
7e2bca48
HH
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
f76ef3aa 358find_block_device() {
9918afd2 359 local _dev _majmin _find_mpt
c8d685c9 360 _find_mpt="$1"
3483509e 361
0b706743 362 if [[ $use_fstab != yes ]]; then
9d36d4fb 363 [[ -d $_find_mpt/. ]]
9a52c3fd 364 findmnt -e -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" | {
2b5ddc77 365 while read -r _majmin _dev || [ -n "$_dev" ]; do
dba20559
HH
366 if [[ -b $_dev ]]; then
367 if ! [[ $_majmin ]] || [[ $_majmin == 0:* ]]; then
2b5ddc77 368 _majmin=$(get_maj_min "$_dev")
dba20559
HH
369 fi
370 if [[ $_majmin ]]; then
466a5998 371 printf "%s\n" "$_majmin"
dba20559 372 else
466a5998 373 printf "%s\n" "$_dev"
dba20559
HH
374 fi
375 return 0
376 fi
75d758e8 377 if [[ $_dev == *:* ]]; then
466a5998 378 printf "%s\n" "$_dev"
dba20559
HH
379 return 0
380 fi
9a52c3fd
HH
381 done
382 return 1
383 } && return 0
dba20559
HH
384 fi
385 # fall back to /etc/fstab
e9ed44c8 386 [[ ! -f "$dracutsysrootdir"/etc/fstab ]] && return 1
dba20559 387
9a52c3fd 388 findmnt -e --fstab -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" | {
2b5ddc77 389 while read -r _majmin _dev || [ -n "$_dev" ]; do
dba20559
HH
390 if ! [[ $_dev ]]; then
391 _dev="$_majmin"
392 unset _majmin
393 fi
9d36d4fb 394 if [[ -b $_dev ]]; then
2b5ddc77 395 [[ $_majmin ]] || _majmin=$(get_maj_min "$_dev")
9d36d4fb 396 if [[ $_majmin ]]; then
466a5998 397 printf "%s\n" "$_majmin"
9d36d4fb 398 else
466a5998 399 printf "%s\n" "$_dev"
9d36d4fb
HH
400 fi
401 return 0
402 fi
75d758e8 403 if [[ $_dev == *:* ]]; then
466a5998 404 printf "%s\n" "$_dev"
9d36d4fb 405 return 0
cc02093d 406 fi
9a52c3fd
HH
407 done
408 return 1
409 } && return 0
0b706743
410
411 return 1
17829e94
VL
412}
413
9d36d4fb
HH
414# find_mp_fstype <mountpoint>
415# Echo the filesystem type for a given mountpoint.
7e2bca48
HH
416# /proc/self/mountinfo is taken as the primary source of information
417# and /etc/fstab is used as a fallback.
418# No newline is appended!
419# Example:
9d36d4fb 420# $ find_mp_fstype /;echo
7e2bca48 421# ext4
9d36d4fb
HH
422find_mp_fstype() {
423 local _fs
7ae5d9d1 424
9d36d4fb 425 if [[ $use_fstab != yes ]]; then
9a52c3fd 426 findmnt -e -v -n -o 'FSTYPE' --target "$1" | {
2b5ddc77 427 while read -r _fs || [ -n "$_fs" ]; do
dba20559 428 [[ $_fs ]] || continue
75d758e8 429 [[ $_fs == "autofs" ]] && continue
466a5998 430 printf "%s" "$_fs"
dba20559 431 return 0
9a52c3fd
HH
432 done
433 return 1
434 } && return 0
dba20559
HH
435 fi
436
e9ed44c8
TL
437 [[ ! -f "$dracutsysrootdir"/etc/fstab ]] && return 1
438
9a52c3fd 439 findmnt --fstab -e -v -n -o 'FSTYPE' --target "$1" | {
2b5ddc77 440 while read -r _fs || [ -n "$_fs" ]; do
9d36d4fb 441 [[ $_fs ]] || continue
75d758e8 442 [[ $_fs == "autofs" ]] && continue
466a5998 443 printf "%s" "$_fs"
9d36d4fb 444 return 0
9a52c3fd
HH
445 done
446 return 1
447 } && return 0
7ae5d9d1
HH
448
449 return 1
450}
451
9d36d4fb
HH
452# find_dev_fstype <device>
453# Echo the filesystem type for a given device.
81672479
HH
454# /proc/self/mountinfo is taken as the primary source of information
455# and /etc/fstab is used as a fallback.
456# No newline is appended!
457# Example:
9d36d4fb 458# $ find_dev_fstype /dev/sda2;echo
81672479 459# ext4
9d36d4fb 460find_dev_fstype() {
5e601454 461 local _find_dev _fs
9d36d4fb 462 _find_dev="$1"
75d758e8 463 if ! [[ $_find_dev == /dev* ]]; then
a4f7b504
HH
464 [[ -b "/dev/block/$_find_dev" ]] && _find_dev="/dev/block/$_find_dev"
465 fi
5e601454
HH
466
467 if [[ $use_fstab != yes ]]; then
9a52c3fd 468 findmnt -e -v -n -o 'FSTYPE' --source "$_find_dev" | {
2b5ddc77 469 while read -r _fs || [ -n "$_fs" ]; do
dba20559 470 [[ $_fs ]] || continue
75d758e8 471 [[ $_fs == "autofs" ]] && continue
466a5998 472 printf "%s" "$_fs"
dba20559 473 return 0
9a52c3fd
HH
474 done
475 return 1
476 } && return 0
dba20559
HH
477 fi
478
e9ed44c8
TL
479 [[ ! -f "$dracutsysrootdir"/etc/fstab ]] && return 1
480
9a52c3fd 481 findmnt --fstab -e -v -n -o 'FSTYPE' --source "$_find_dev" | {
2b5ddc77 482 while read -r _fs || [ -n "$_fs" ]; do
5e601454 483 [[ $_fs ]] || continue
75d758e8 484 [[ $_fs == "autofs" ]] && continue
466a5998 485 printf "%s" "$_fs"
5e601454 486 return 0
9a52c3fd
HH
487 done
488 return 1
489 } && return 0
5e601454
HH
490
491 return 1
97af51db 492}
5e601454 493
bbc9bfe1
HH
494# find_mp_fsopts <mountpoint>
495# Echo the filesystem options for a given mountpoint.
496# /proc/self/mountinfo is taken as the primary source of information
497# and /etc/fstab is used as a fallback.
498# No newline is appended!
499# Example:
500# $ find_mp_fsopts /;echo
501# rw,relatime,discard,data=ordered
502find_mp_fsopts() {
503 if [[ $use_fstab != yes ]]; then
9a52c3fd 504 findmnt -e -v -n -o 'OPTIONS' --target "$1" 2> /dev/null && return 0
bbc9bfe1
HH
505 fi
506
e9ed44c8
TL
507 [[ ! -f "$dracutsysrootdir"/etc/fstab ]] && return 1
508
bbc9bfe1
HH
509 findmnt --fstab -e -v -n -o 'OPTIONS' --target "$1"
510}
511
97af51db
HH
512# find_dev_fsopts <device>
513# Echo the filesystem options for a given device.
514# /proc/self/mountinfo is taken as the primary source of information
515# and /etc/fstab is used as a fallback.
3483509e
HH
516# if `use_fstab == yes`, then only `/etc/fstab` is used.
517#
97af51db
HH
518# Example:
519# $ find_dev_fsopts /dev/sda2
520# rw,relatime,discard,data=ordered
521find_dev_fsopts() {
3483509e 522 local _find_dev
97af51db 523 _find_dev="$1"
75d758e8 524 if ! [[ $_find_dev == /dev* ]]; then
97af51db
HH
525 [[ -b "/dev/block/$_find_dev" ]] && _find_dev="/dev/block/$_find_dev"
526 fi
527
528 if [[ $use_fstab != yes ]]; then
9a52c3fd 529 findmnt -e -v -n -o 'OPTIONS' --source "$_find_dev" 2> /dev/null && return 0
97af51db
HH
530 fi
531
e9ed44c8
TL
532 [[ ! -f "$dracutsysrootdir"/etc/fstab ]] && return 1
533
97af51db 534 findmnt --fstab -e -v -n -o 'OPTIONS' --source "$_find_dev"
81672479
HH
535}
536
7ae5d9d1 537# finds the major:minor of the block device backing the root filesystem.
f76ef3aa
VL
538find_root_block_device() { find_block_device /; }
539
7e2bca48 540# for_each_host_dev_fs <func>
d351541e 541# Execute "<func> <dev> <filesystem>" for every "<dev> <fs>" pair found
7e2bca48 542# in ${host_fs_types[@]}
9a52c3fd 543for_each_host_dev_fs() {
480d772f 544 local _func="$1"
d0096de7 545 local _dev
2efa546f 546 local _ret=1
fd191a7b 547
e6199960
PL
548 [[ "${#host_fs_types[@]}" ]] || return 2
549
d351541e
HH
550 for _dev in "${!host_fs_types[@]}"; do
551 $_func "$_dev" "${host_fs_types[$_dev]}" && _ret=0
480d772f 552 done
2efa546f 553 return $_ret
480d772f
HH
554}
555
9a52c3fd 556host_fs_all() {
466a5998 557 printf "%s\n" "${host_fs_types[@]}"
fd191a7b
HH
558}
559
17829e94
VL
560# Walk all the slave relationships for a given block device.
561# Stop when our helper function returns success
562# $1 = function to call on every found block device
563# $2 = block device in major:minor format
0b706743 564check_block_and_slaves() {
29b10e65 565 local _x
17829e94 566 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
2b5ddc77 567 if ! lvm_internal_dev "$2"; then "$1" "$2" && return; fi
533d7dc4 568 check_vol_slaves "$@" && return 0
e64dafd1 569 if [[ -f /sys/dev/block/$2/../dev ]] && [[ /sys/dev/block/$2/../subsystem -ef /sys/class/block ]]; then
2b5ddc77 570 check_block_and_slaves "$1" "$(< "/sys/dev/block/$2/../dev")" && return 0
a3afcf2a 571 fi
2b5ddc77 572 for _x in /sys/dev/block/"$2"/slaves/*; do
e64dafd1
HH
573 [[ -f $_x/dev ]] || continue
574 [[ $_x/subsystem -ef /sys/class/block ]] || continue
2b5ddc77 575 check_block_and_slaves "$1" "$(< "$_x/dev")" && return 0
17829e94
VL
576 done
577 return 1
578}
579
83e0dc7a
DY
580check_block_and_slaves_all() {
581 local _x _ret=1
582 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
2b5ddc77 583 if ! lvm_internal_dev "$2" && "$1" "$2"; then
dba20559 584 _ret=0
83e0dc7a 585 fi
c7c8c498 586 check_vol_slaves_all "$@" && return 0
e64dafd1 587 if [[ -f /sys/dev/block/$2/../dev ]] && [[ /sys/dev/block/$2/../subsystem -ef /sys/class/block ]]; then
2b5ddc77 588 check_block_and_slaves_all "$1" "$(< "/sys/dev/block/$2/../dev")" && _ret=0
83e0dc7a 589 fi
2b5ddc77 590 for _x in /sys/dev/block/"$2"/slaves/*; do
e64dafd1
HH
591 [[ -f $_x/dev ]] || continue
592 [[ $_x/subsystem -ef /sys/class/block ]] || continue
2b5ddc77 593 check_block_and_slaves_all "$1" "$(< "$_x/dev")" && _ret=0
83e0dc7a
DY
594 done
595 return $_ret
596}
597# for_each_host_dev_and_slaves <func>
598# Execute "<func> <dev>" for every "<dev>" found
599# in ${host_devs[@]} and their slaves
9a52c3fd 600for_each_host_dev_and_slaves_all() {
83e0dc7a
DY
601 local _func="$1"
602 local _dev
603 local _ret=1
fd191a7b 604
3483509e 605 [[ "${host_devs[*]}" ]] || return 2
fd191a7b 606
3721635b 607 for _dev in "${host_devs[@]}"; do
75d758e8 608 [[ -b $_dev ]] || continue
2b5ddc77 609 if check_block_and_slaves_all "$_func" "$(get_maj_min "$_dev")"; then
dba20559 610 _ret=0
83e0dc7a
DY
611 fi
612 done
613 return $_ret
614}
615
9a52c3fd 616for_each_host_dev_and_slaves() {
83e0dc7a
DY
617 local _func="$1"
618 local _dev
fd191a7b 619
3483509e 620 [[ "${host_devs[*]}" ]] || return 2
fd191a7b 621
3721635b 622 for _dev in "${host_devs[@]}"; do
75d758e8 623 [[ -b $_dev ]] || continue
2b5ddc77 624 check_block_and_slaves "$_func" "$(get_maj_min "$_dev")" && return 0
83e0dc7a
DY
625 done
626 return 1
627}
628
c86f4d28
PL
629# /sys/dev/block/major:minor is symbol link to real hardware device
630# go downstream $(realpath /sys/dev/block/major:minor) to detect driver
631get_blockdev_drv_through_sys() {
632 local _block_mods=""
633 local _path
634
635 _path=$(realpath "$1")
636 while true; do
637 if [[ -L "$_path"/driver/module ]]; then
638 _mod=$(realpath "$_path"/driver/module)
639 _mod=$(basename "$_mod")
640 _block_mods="$_block_mods $_mod"
641 fi
642 _path=$(dirname "$_path")
643 if [[ $_path == '/sys/devices' ]] || [[ $_path == '/' ]]; then
644 break
645 fi
646 done
647 echo "$_block_mods"
648}
649
533d7dc4
HH
650# ugly workaround for the lvm design
651# There is no volume group device,
652# so, there are no slave devices for volume groups.
653# Logical volumes only have the slave devices they really live on,
654# but you cannot create the logical volume without the volume group.
95bde758 655# And the volume group might be bigger than the devices the LV needs.
533d7dc4 656check_vol_slaves() {
2b5ddc77 657 local _vg _pv _dm _majmin
9ed6eb74 658 _majmin="$2"
9ed6eb74 659 _dm=/sys/dev/block/$_majmin/dm
2b5ddc77
HH
660 [[ -f $_dm/uuid && $(< "$_dm"/uuid) =~ LVM-* ]] || return 1
661 _vg=$(dmsetup splitname --noheadings -o vg_name "$(< "$_dm/name")")
9ed6eb74
HH
662 # strip space
663 _vg="${_vg//[[:space:]]/}"
664 if [[ $_vg ]]; then
9a52c3fd 665 for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2> /dev/null); do
2b5ddc77 666 check_block_and_slaves "$1" "$(get_maj_min "$_pv")" && return 0
9ed6eb74
HH
667 done
668 fi
533d7dc4
HH
669 return 1
670}
671
c7c8c498 672check_vol_slaves_all() {
2b5ddc77 673 local _vg _pv _majmin
9ed6eb74 674 _majmin="$2"
9ed6eb74 675 _dm="/sys/dev/block/$_majmin/dm"
2b5ddc77
HH
676 [[ -f $_dm/uuid && $(< "$_dm"/uuid) =~ LVM-* ]] || return 1
677 _vg=$(dmsetup splitname --noheadings -o vg_name "$(< "$_dm/name")")
9ed6eb74
HH
678 # strip space
679 _vg="${_vg//[[:space:]]/}"
680 if [[ $_vg ]]; then
da36b76a 681 # when filter/global_filter is set, lvm may be failed
2b5ddc77 682 if ! lvm lvs --noheadings -o vg_name "$_vg" 2> /dev/null 1> /dev/null; then
9a52c3fd 683 return 1
da36b76a 684 fi
685
9a52c3fd 686 for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2> /dev/null); do
2b5ddc77 687 check_block_and_slaves_all "$1" "$(get_maj_min "$_pv")"
9ed6eb74
HH
688 done
689 return 0
690 fi
c7c8c498
HH
691 return 1
692}
693
8aa99268
HH
694# fs_get_option <filesystem options> <search for option>
695# search for a specific option in a bunch of filesystem options
696# and return the value
697fs_get_option() {
698 local _fsopts=$1
699 local _option=$2
700 local OLDIFS="$IFS"
701 IFS=,
2b5ddc77 702 # shellcheck disable=SC2086
8aa99268
HH
703 set -- $_fsopts
704 IFS="$OLDIFS"
705 while [ $# -gt 0 ]; do
706 case $1 in
707 $_option=*)
2b5ddc77 708 echo "${1#${_option}=}"
8aa99268 709 break
9a52c3fd 710 ;;
8aa99268
HH
711 esac
712 shift
713 done
714}
715
9a52c3fd 716check_kernel_config() {
8e3f6537
HH
717 local _config_opt="$1"
718 local _config_file
a0120420 719 [[ -f $dracutsysrootdir/boot/config-$kernel ]] \
8e3f6537 720 && _config_file="/boot/config-$kernel"
a0120420 721 [[ -f $dracutsysrootdir/lib/modules/$kernel/config ]] \
8e3f6537
HH
722 && _config_file="/lib/modules/$kernel/config"
723
724 # no kernel config file, so return true
725 [[ $_config_file ]] || return 0
726
560f45b1 727 grep -q -F "${_config_opt}=" "$dracutsysrootdir$_config_file" && return 0
8e3f6537
HH
728 return 1
729}
730
c050190f
KS
731# 0 if the kernel module is either built-in or available
732# 1 if the kernel module is not enabled
733check_kernel_module() {
2b5ddc77 734 modprobe -S "$kernel" --dry-run "$1" &> /dev/null || return 1
c050190f 735}
8e3f6537 736
5f2c30d9
KRW
737# get_cpu_vendor
738# Only two values are returned: AMD or Intel
9a52c3fd 739get_cpu_vendor() {
5f2c30d9
KRW
740 if grep -qE AMD /proc/cpuinfo; then
741 printf "AMD"
742 fi
743 if grep -qE Intel /proc/cpuinfo; then
744 printf "Intel"
745 fi
746}
747
748# get_host_ucode
749# Get the hosts' ucode file based on the /proc/cpuinfo
9a52c3fd 750get_ucode_file() {
2b5ddc77
HH
751 local family
752 local model
753 local stepping
122657b2
KS
754 family=$(grep -E "cpu family" /proc/cpuinfo | head -1 | sed "s/.*:\ //")
755 model=$(grep -E "model" /proc/cpuinfo | grep -v name | head -1 | sed "s/.*:\ //")
756 stepping=$(grep -E "stepping" /proc/cpuinfo | head -1 | sed "s/.*:\ //")
5f2c30d9
KRW
757
758 if [[ "$(get_cpu_vendor)" == "AMD" ]]; then
19453dc8 759 if [[ $family -ge 21 ]]; then
2b5ddc77 760 printf "microcode_amd_fam%xh.bin" "$family"
5f2c30d9
KRW
761 else
762 printf "microcode_amd.bin"
763 fi
764 fi
765 if [[ "$(get_cpu_vendor)" == "Intel" ]]; then
766 # The /proc/cpuinfo are in decimal.
2b5ddc77 767 printf "%02x-%02x-%02x" "${family}" "${model}" "${stepping}"
5f2c30d9
KRW
768 fi
769}
96c6f6f3
MC
770
771# Not every device in /dev/mapper should be examined.
772# If it is an LVM device, touch only devices which have /dev/VG/LV symlink.
773lvm_internal_dev() {
774 local dev_dm_dir=/sys/dev/block/$1/dm
2b5ddc77 775 [[ ! -f $dev_dm_dir/uuid || $(< "$dev_dm_dir"/uuid) != LVM-* ]] && return 1 # Not an LVM device
96c6f6f3 776 local DM_VG_NAME DM_LV_NAME DM_LV_LAYER
2b5ddc77 777 eval "$(dmsetup splitname --nameprefixes --noheadings --rows "$(< "$dev_dm_dir"/name)" 2> /dev/null)"
96c6f6f3
MC
778 [[ ${DM_VG_NAME} ]] && [[ ${DM_LV_NAME} ]] || return 0 # Better skip this!
779 [[ ${DM_LV_LAYER} ]] || [[ ! -L /dev/${DM_VG_NAME}/${DM_LV_NAME} ]]
780}
781
1cadc26f
HH
782btrfs_devs() {
783 local _mp="$1"
784 btrfs device usage "$_mp" \
2b5ddc77 785 | while read -r _dev _; do
9a52c3fd
HH
786 str_starts "$_dev" "/" || continue
787 _dev=${_dev%,}
788 printf -- "%s\n" "$_dev"
1cadc26f
HH
789 done
790}
ceca74cc 791
9582f027
SJ
792zfs_devs() {
793 local _mp="$1"
794 zpool list -H -v -P "${_mp%%/*}" | awk -F$'\t' '$2 ~ /^\// {print $2}' \
795 | while read -r _dev; do
796 realpath "${_dev}"
797 done
798}
799
ceca74cc 800iface_for_remote_addr() {
2b5ddc77 801 # shellcheck disable=SC2046
ceca74cc 802 set -- $(ip -o route get to "$1")
d754e1c6
MW
803 while [ $# -gt 0 ]; do
804 case $1 in
805 dev)
806 echo "$2"
807 return
808 ;;
809 esac
810 shift
811 done
ceca74cc
MW
812}
813
814local_addr_for_remote_addr() {
2b5ddc77 815 # shellcheck disable=SC2046
ceca74cc 816 set -- $(ip -o route get to "$1")
d754e1c6
MW
817 while [ $# -gt 0 ]; do
818 case $1 in
819 src)
820 echo "$2"
821 return
822 ;;
823 esac
824 shift
825 done
ceca74cc
MW
826}
827
828peer_for_addr() {
829 local addr=$1
830 local qtd
831
832 # quote periods in IPv4 address
833 qtd=${addr//./\\.}
9a52c3fd
HH
834 ip -o addr show \
835 | sed -n 's%^.* '"$qtd"' peer \([0-9a-f.:]\{1,\}\(/[0-9]*\)\?\).*$%\1%p'
ceca74cc
MW
836}
837
838netmask_for_addr() {
839 local addr=$1
840 local qtd
841
842 # quote periods in IPv4 address
843 qtd=${addr//./\\.}
844 ip -o addr show | sed -n 's,^.* '"$qtd"'/\([0-9]*\) .*$,\1,p'
845}
846
847gateway_for_iface() {
848 local ifname=$1 addr=$2
849
850 case $addr in
9a52c3fd
HH
851 *.*) proto=4 ;;
852 *:*) proto=6 ;;
853 *) return ;;
ceca74cc 854 esac
9a52c3fd
HH
855 ip -o -$proto route show \
856 | sed -n "s/^default via \([0-9a-z.:]\{1,\}\) dev $ifname .*\$/\1/p"
ceca74cc
MW
857}
858
859# This works only for ifcfg-style network configuration!
860bootproto_for_iface() {
861 local ifname=$1
862 local dir
863
864 # follow ifcfg settings for boot protocol
865 for dir in network-scripts network; do
866 [ -f "/etc/sysconfig/$dir/ifcfg-$ifname" ] && {
867 sed -n "s/BOOTPROTO=[\"']\?\([[:alnum:]]\{1,\}\)[\"']\?.*\$/\1/p" \
868 "/etc/sysconfig/$dir/ifcfg-$ifname"
869 return
870 }
871 done
872}
873
874is_unbracketed_ipv6_address() {
875 strglob "$1" '*:*' && ! strglob "$1" '\[*:*\]'
876}
877
878# Create an ip= string to set up networking such that the given
879# remote address can be reached
880ip_params_for_remote_addr() {
881 local remote_addr=$1
2b5ddc77 882 local ifname local_addr peer netmask gateway ifmac
ceca74cc
MW
883
884 [[ $remote_addr ]] || return 1
885 ifname=$(iface_for_remote_addr "$remote_addr")
886 [[ $ifname ]] || {
887 berror "failed to determine interface to connect to $remote_addr"
888 return 1
889 }
890
891 # ifname clause to bind the interface name to a MAC address
892 if [ -d "/sys/class/net/$ifname/bonding" ]; then
893 dinfo "Found bonded interface '${ifname}'. Make sure to provide an appropriate 'bond=' cmdline."
9a52c3fd 894 elif [ -e "/sys/class/net/$ifname/address" ]; then
ceca74cc
MW
895 ifmac=$(cat "/sys/class/net/$ifname/address")
896 [[ $ifmac ]] && printf 'ifname=%s:%s ' "${ifname}" "${ifmac}"
897 fi
898
899 bootproto=$(bootproto_for_iface "$ifname")
900 case $bootproto in
9a52c3fd 901 dhcp | dhcp6 | auto6) ;;
ceca74cc 902 dhcp4)
9a52c3fd
HH
903 bootproto=dhcp
904 ;;
905 static* | "")
906 bootproto=
907 ;;
ceca74cc
MW
908 *)
909 derror "bootproto \"$bootproto\" is unsupported by dracut, trying static configuration"
9a52c3fd
HH
910 bootproto=
911 ;;
ceca74cc
MW
912 esac
913 if [[ $bootproto ]]; then
914 printf 'ip=%s:%s ' "${ifname}" "${bootproto}"
915 else
916 local_addr=$(local_addr_for_remote_addr "$remote_addr")
917 [[ $local_addr ]] || {
918 berror "failed to determine local address to connect to $remote_addr"
919 return 1
920 }
921 peer=$(peer_for_addr "$local_addr")
922 # Set peer or netmask, but not both
923 [[ $peer ]] || netmask=$(netmask_for_addr "$local_addr")
924 gateway=$(gateway_for_iface "$ifname" "$local_addr")
925 # Quote IPv6 addresses with brackets
926 is_unbracketed_ipv6_address "$local_addr" && local_addr="[$local_addr]"
927 is_unbracketed_ipv6_address "$peer" && peer="[$peer]"
928 is_unbracketed_ipv6_address "$gateway" && gateway="[$gateway]"
929 printf 'ip=%s:%s:%s:%s::%s:none ' \
9a52c3fd 930 "${local_addr}" "${peer}" "${gateway}" "${netmask}" "${ifname}"
ceca74cc
MW
931 fi
932
933}
480aa969
DM
934
935# block_is_nbd <maj:min>
936# Check whether $1 is an nbd device
937block_is_nbd() {
938 [[ -b /dev/block/$1 && $1 == 43:* ]]
939}
940
941# block_is_iscsi <maj:min>
0afa840e 942# Check whether $1 is an iSCSI device
480aa969
DM
943block_is_iscsi() {
944 local _dir
945 local _dev=$1
946 [[ -L "/sys/dev/block/$_dev" ]] || return
947 _dir="$(readlink -f "/sys/dev/block/$_dev")" || return
948 until [[ -d "$_dir/sys" || -d "$_dir/iscsi_session" ]]; do
949 _dir="$_dir/.."
950 done
951 [[ -d "$_dir/iscsi_session" ]]
952}
953
954# block_is_fcoe <maj:min>
955# Check whether $1 is an FCoE device
956# Will not work for HBAs that hide the ethernet aspect
957# completely and present a pure FC device
958block_is_fcoe() {
959 local _dir
960 local _dev=$1
961 [[ -L "/sys/dev/block/$_dev" ]] || return
962 _dir="$(readlink -f "/sys/dev/block/$_dev")"
963 until [[ -d "$_dir/sys" ]]; do
964 _dir="$_dir/.."
965 if [[ -d "$_dir/subsystem" ]]; then
2b5ddc77 966 subsystem=$(basename "$(readlink "$_dir"/subsystem)")
480aa969
DM
967 [[ $subsystem == "fcoe" ]] && return 0
968 fi
969 done
970 return 1
971}
972
973# block_is_netdevice <maj:min>
974# Check whether $1 is a net device
975block_is_netdevice() {
976 block_is_nbd "$1" || block_is_iscsi "$1" || block_is_fcoe "$1"
977}
cbafcd0f
KS
978
979# get the corresponding kernel modules of a /sys/class/*/* or/dev/* device
980get_dev_module() {
dc3b976f
AAF
981 local dev_attr_walk
982 local dev_drivers
983 dev_attr_walk=$(udevadm info -a "$1")
984 dev_drivers=$(echo "$dev_attr_walk" | sed -n 's/\s*DRIVERS=="\(\S\+\)"/\1/p')
985 # if no kernel modules found and device is in a virtual subsystem, follow symlinks
986 if [[ -z $dev_drivers && $(udevadm info -q path "$1") == "/devices/virtual"* ]]; then
987 local dev_vkernel
988 local dev_vsubsystem
989 local dev_vpath
990 dev_vkernel=$(echo "$dev_attr_walk" | sed -n 's/\s*KERNELS=="\(\S\+\)"/\1/p' | tail -1)
991 dev_vsubsystem=$(echo "$dev_attr_walk" | sed -n 's/\s*SUBSYSTEMS=="\(\S\+\)"/\1/p' | tail -1)
992 dev_vpath="/sys/devices/virtual/$dev_vsubsystem/$dev_vkernel"
993 if [[ -n $dev_vkernel && -n $dev_vsubsystem && -d $dev_vpath ]]; then
994 local dev_links
995 local dev_link
996 dev_links=$(find "$dev_vpath" -maxdepth 1 -type l ! -name "subsystem" -exec readlink {} \;)
997 for dev_link in $dev_links; do
998 [[ -n $dev_drivers && ${dev_drivers: -1} != $'\n' ]] && dev_drivers+=$'\n'
999 dev_drivers+=$(udevadm info -a "$dev_vpath/$dev_link" \
1000 | sed -n 's/\s*DRIVERS=="\(\S\+\)"/\1/p' \
1001 | grep -v -e pcieport)
1002 done
1003 fi
1004 fi
1005 echo "$dev_drivers"
cbafcd0f 1006}