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