]>
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 |
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 | |
255 | get_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 |
270 | get_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 |
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=}" | |
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 | 327 | shorten_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 | 358 | find_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 | |
386 | ||
9a52c3fd | 387 | findmnt -e --fstab -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" | { |
2b5ddc77 | 388 | while read -r _majmin _dev || [ -n "$_dev" ]; do |
dba20559 HH |
389 | if ! [[ $_dev ]]; then |
390 | _dev="$_majmin" | |
391 | unset _majmin | |
392 | fi | |
9d36d4fb | 393 | if [[ -b $_dev ]]; then |
2b5ddc77 | 394 | [[ $_majmin ]] || _majmin=$(get_maj_min "$_dev") |
9d36d4fb | 395 | if [[ $_majmin ]]; then |
466a5998 | 396 | printf "%s\n" "$_majmin" |
9d36d4fb | 397 | else |
466a5998 | 398 | printf "%s\n" "$_dev" |
9d36d4fb HH |
399 | fi |
400 | return 0 | |
401 | fi | |
75d758e8 | 402 | if [[ $_dev == *:* ]]; then |
466a5998 | 403 | printf "%s\n" "$_dev" |
9d36d4fb | 404 | return 0 |
cc02093d | 405 | fi |
9a52c3fd HH |
406 | done |
407 | return 1 | |
408 | } && return 0 | |
0b706743 AŻ |
409 | |
410 | return 1 | |
17829e94 VL |
411 | } |
412 | ||
9d36d4fb HH |
413 | # find_mp_fstype <mountpoint> |
414 | # Echo the filesystem type for a given mountpoint. | |
7e2bca48 HH |
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: | |
9d36d4fb | 419 | # $ find_mp_fstype /;echo |
7e2bca48 | 420 | # ext4 |
9d36d4fb HH |
421 | find_mp_fstype() { |
422 | local _fs | |
7ae5d9d1 | 423 | |
9d36d4fb | 424 | if [[ $use_fstab != yes ]]; then |
9a52c3fd | 425 | findmnt -e -v -n -o 'FSTYPE' --target "$1" | { |
2b5ddc77 | 426 | while read -r _fs || [ -n "$_fs" ]; do |
dba20559 | 427 | [[ $_fs ]] || continue |
75d758e8 | 428 | [[ $_fs == "autofs" ]] && continue |
466a5998 | 429 | printf "%s" "$_fs" |
dba20559 | 430 | return 0 |
9a52c3fd HH |
431 | done |
432 | return 1 | |
433 | } && return 0 | |
dba20559 HH |
434 | fi |
435 | ||
9a52c3fd | 436 | findmnt --fstab -e -v -n -o 'FSTYPE' --target "$1" | { |
2b5ddc77 | 437 | while read -r _fs || [ -n "$_fs" ]; do |
9d36d4fb | 438 | [[ $_fs ]] || continue |
75d758e8 | 439 | [[ $_fs == "autofs" ]] && continue |
466a5998 | 440 | printf "%s" "$_fs" |
9d36d4fb | 441 | return 0 |
9a52c3fd HH |
442 | done |
443 | return 1 | |
444 | } && return 0 | |
7ae5d9d1 HH |
445 | |
446 | return 1 | |
447 | } | |
448 | ||
9d36d4fb HH |
449 | # find_dev_fstype <device> |
450 | # Echo the filesystem type for a given device. | |
81672479 HH |
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: | |
9d36d4fb | 455 | # $ find_dev_fstype /dev/sda2;echo |
81672479 | 456 | # ext4 |
9d36d4fb | 457 | find_dev_fstype() { |
5e601454 | 458 | local _find_dev _fs |
9d36d4fb | 459 | _find_dev="$1" |
75d758e8 | 460 | if ! [[ $_find_dev == /dev* ]]; then |
a4f7b504 HH |
461 | [[ -b "/dev/block/$_find_dev" ]] && _find_dev="/dev/block/$_find_dev" |
462 | fi | |
5e601454 HH |
463 | |
464 | if [[ $use_fstab != yes ]]; then | |
9a52c3fd | 465 | findmnt -e -v -n -o 'FSTYPE' --source "$_find_dev" | { |
2b5ddc77 | 466 | while read -r _fs || [ -n "$_fs" ]; do |
dba20559 | 467 | [[ $_fs ]] || continue |
75d758e8 | 468 | [[ $_fs == "autofs" ]] && continue |
466a5998 | 469 | printf "%s" "$_fs" |
dba20559 | 470 | return 0 |
9a52c3fd HH |
471 | done |
472 | return 1 | |
473 | } && return 0 | |
dba20559 HH |
474 | fi |
475 | ||
9a52c3fd | 476 | findmnt --fstab -e -v -n -o 'FSTYPE' --source "$_find_dev" | { |
2b5ddc77 | 477 | while read -r _fs || [ -n "$_fs" ]; do |
5e601454 | 478 | [[ $_fs ]] || continue |
75d758e8 | 479 | [[ $_fs == "autofs" ]] && continue |
466a5998 | 480 | printf "%s" "$_fs" |
5e601454 | 481 | return 0 |
9a52c3fd HH |
482 | done |
483 | return 1 | |
484 | } && return 0 | |
5e601454 HH |
485 | |
486 | return 1 | |
97af51db | 487 | } |
5e601454 | 488 | |
bbc9bfe1 HH |
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 | |
9a52c3fd | 499 | findmnt -e -v -n -o 'OPTIONS' --target "$1" 2> /dev/null && return 0 |
bbc9bfe1 HH |
500 | fi |
501 | ||
502 | findmnt --fstab -e -v -n -o 'OPTIONS' --target "$1" | |
503 | } | |
504 | ||
97af51db HH |
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. | |
3483509e HH |
509 | # if `use_fstab == yes`, then only `/etc/fstab` is used. |
510 | # | |
97af51db HH |
511 | # Example: |
512 | # $ find_dev_fsopts /dev/sda2 | |
513 | # rw,relatime,discard,data=ordered | |
514 | find_dev_fsopts() { | |
3483509e | 515 | local _find_dev |
97af51db | 516 | _find_dev="$1" |
75d758e8 | 517 | if ! [[ $_find_dev == /dev* ]]; then |
97af51db HH |
518 | [[ -b "/dev/block/$_find_dev" ]] && _find_dev="/dev/block/$_find_dev" |
519 | fi | |
520 | ||
521 | if [[ $use_fstab != yes ]]; then | |
9a52c3fd | 522 | findmnt -e -v -n -o 'OPTIONS' --source "$_find_dev" 2> /dev/null && return 0 |
97af51db HH |
523 | fi |
524 | ||
525 | findmnt --fstab -e -v -n -o 'OPTIONS' --source "$_find_dev" | |
81672479 HH |
526 | } |
527 | ||
7ae5d9d1 | 528 | # finds the major:minor of the block device backing the root filesystem. |
f76ef3aa VL |
529 | find_root_block_device() { find_block_device /; } |
530 | ||
7e2bca48 | 531 | # for_each_host_dev_fs <func> |
d351541e | 532 | # Execute "<func> <dev> <filesystem>" for every "<dev> <fs>" pair found |
7e2bca48 | 533 | # in ${host_fs_types[@]} |
9a52c3fd | 534 | for_each_host_dev_fs() { |
480d772f | 535 | local _func="$1" |
d0096de7 | 536 | local _dev |
2efa546f | 537 | local _ret=1 |
fd191a7b | 538 | |
e6199960 PL |
539 | [[ "${#host_fs_types[@]}" ]] || return 2 |
540 | ||
d351541e HH |
541 | for _dev in "${!host_fs_types[@]}"; do |
542 | $_func "$_dev" "${host_fs_types[$_dev]}" && _ret=0 | |
480d772f | 543 | done |
2efa546f | 544 | return $_ret |
480d772f HH |
545 | } |
546 | ||
9a52c3fd | 547 | host_fs_all() { |
466a5998 | 548 | printf "%s\n" "${host_fs_types[@]}" |
fd191a7b HH |
549 | } |
550 | ||
17829e94 VL |
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 | |
0b706743 | 555 | check_block_and_slaves() { |
29b10e65 | 556 | local _x |
17829e94 | 557 | [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry. |
2b5ddc77 | 558 | if ! lvm_internal_dev "$2"; then "$1" "$2" && return; fi |
533d7dc4 | 559 | check_vol_slaves "$@" && return 0 |
e64dafd1 | 560 | if [[ -f /sys/dev/block/$2/../dev ]] && [[ /sys/dev/block/$2/../subsystem -ef /sys/class/block ]]; then |
2b5ddc77 | 561 | check_block_and_slaves "$1" "$(< "/sys/dev/block/$2/../dev")" && return 0 |
a3afcf2a | 562 | fi |
2b5ddc77 | 563 | for _x in /sys/dev/block/"$2"/slaves/*; do |
e64dafd1 HH |
564 | [[ -f $_x/dev ]] || continue |
565 | [[ $_x/subsystem -ef /sys/class/block ]] || continue | |
2b5ddc77 | 566 | check_block_and_slaves "$1" "$(< "$_x/dev")" && return 0 |
17829e94 VL |
567 | done |
568 | return 1 | |
569 | } | |
570 | ||
83e0dc7a DY |
571 | check_block_and_slaves_all() { |
572 | local _x _ret=1 | |
573 | [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry. | |
2b5ddc77 | 574 | if ! lvm_internal_dev "$2" && "$1" "$2"; then |
dba20559 | 575 | _ret=0 |
83e0dc7a | 576 | fi |
c7c8c498 | 577 | check_vol_slaves_all "$@" && return 0 |
e64dafd1 | 578 | if [[ -f /sys/dev/block/$2/../dev ]] && [[ /sys/dev/block/$2/../subsystem -ef /sys/class/block ]]; then |
2b5ddc77 | 579 | check_block_and_slaves_all "$1" "$(< "/sys/dev/block/$2/../dev")" && _ret=0 |
83e0dc7a | 580 | fi |
2b5ddc77 | 581 | for _x in /sys/dev/block/"$2"/slaves/*; do |
e64dafd1 HH |
582 | [[ -f $_x/dev ]] || continue |
583 | [[ $_x/subsystem -ef /sys/class/block ]] || continue | |
2b5ddc77 | 584 | check_block_and_slaves_all "$1" "$(< "$_x/dev")" && _ret=0 |
83e0dc7a DY |
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 | |
9a52c3fd | 591 | for_each_host_dev_and_slaves_all() { |
83e0dc7a DY |
592 | local _func="$1" |
593 | local _dev | |
594 | local _ret=1 | |
fd191a7b | 595 | |
3483509e | 596 | [[ "${host_devs[*]}" ]] || return 2 |
fd191a7b | 597 | |
3721635b | 598 | for _dev in "${host_devs[@]}"; do |
75d758e8 | 599 | [[ -b $_dev ]] || continue |
2b5ddc77 | 600 | if check_block_and_slaves_all "$_func" "$(get_maj_min "$_dev")"; then |
dba20559 | 601 | _ret=0 |
83e0dc7a DY |
602 | fi |
603 | done | |
604 | return $_ret | |
605 | } | |
606 | ||
9a52c3fd | 607 | for_each_host_dev_and_slaves() { |
83e0dc7a DY |
608 | local _func="$1" |
609 | local _dev | |
fd191a7b | 610 | |
3483509e | 611 | [[ "${host_devs[*]}" ]] || return 2 |
fd191a7b | 612 | |
3721635b | 613 | for _dev in "${host_devs[@]}"; do |
75d758e8 | 614 | [[ -b $_dev ]] || continue |
2b5ddc77 | 615 | check_block_and_slaves "$_func" "$(get_maj_min "$_dev")" && return 0 |
83e0dc7a DY |
616 | done |
617 | return 1 | |
618 | } | |
619 | ||
c86f4d28 PL |
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 | ||
533d7dc4 HH |
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. | |
95bde758 | 646 | # And the volume group might be bigger than the devices the LV needs. |
533d7dc4 | 647 | check_vol_slaves() { |
2b5ddc77 | 648 | local _vg _pv _dm _majmin |
9ed6eb74 | 649 | _majmin="$2" |
9ed6eb74 | 650 | _dm=/sys/dev/block/$_majmin/dm |
2b5ddc77 HH |
651 | [[ -f $_dm/uuid && $(< "$_dm"/uuid) =~ LVM-* ]] || return 1 |
652 | _vg=$(dmsetup splitname --noheadings -o vg_name "$(< "$_dm/name")") | |
9ed6eb74 HH |
653 | # strip space |
654 | _vg="${_vg//[[:space:]]/}" | |
655 | if [[ $_vg ]]; then | |
9a52c3fd | 656 | for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2> /dev/null); do |
2b5ddc77 | 657 | check_block_and_slaves "$1" "$(get_maj_min "$_pv")" && return 0 |
9ed6eb74 HH |
658 | done |
659 | fi | |
533d7dc4 HH |
660 | return 1 |
661 | } | |
662 | ||
c7c8c498 | 663 | check_vol_slaves_all() { |
2b5ddc77 | 664 | local _vg _pv _majmin |
9ed6eb74 | 665 | _majmin="$2" |
9ed6eb74 | 666 | _dm="/sys/dev/block/$_majmin/dm" |
2b5ddc77 HH |
667 | [[ -f $_dm/uuid && $(< "$_dm"/uuid) =~ LVM-* ]] || return 1 |
668 | _vg=$(dmsetup splitname --noheadings -o vg_name "$(< "$_dm/name")") | |
9ed6eb74 HH |
669 | # strip space |
670 | _vg="${_vg//[[:space:]]/}" | |
671 | if [[ $_vg ]]; then | |
da36b76a | 672 | # when filter/global_filter is set, lvm may be failed |
2b5ddc77 | 673 | if ! lvm lvs --noheadings -o vg_name "$_vg" 2> /dev/null 1> /dev/null; then |
9a52c3fd | 674 | return 1 |
da36b76a | 675 | fi |
676 | ||
9a52c3fd | 677 | for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2> /dev/null); do |
2b5ddc77 | 678 | check_block_and_slaves_all "$1" "$(get_maj_min "$_pv")" |
9ed6eb74 HH |
679 | done |
680 | return 0 | |
681 | fi | |
c7c8c498 HH |
682 | return 1 |
683 | } | |
684 | ||
8aa99268 HH |
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=, | |
2b5ddc77 | 693 | # shellcheck disable=SC2086 |
8aa99268 HH |
694 | set -- $_fsopts |
695 | IFS="$OLDIFS" | |
696 | while [ $# -gt 0 ]; do | |
697 | case $1 in | |
698 | $_option=*) | |
2b5ddc77 | 699 | echo "${1#${_option}=}" |
8aa99268 | 700 | break |
9a52c3fd | 701 | ;; |
8aa99268 HH |
702 | esac |
703 | shift | |
704 | done | |
705 | } | |
706 | ||
9a52c3fd | 707 | check_kernel_config() { |
8e3f6537 HH |
708 | local _config_opt="$1" |
709 | local _config_file | |
a0120420 | 710 | [[ -f $dracutsysrootdir/boot/config-$kernel ]] \ |
8e3f6537 | 711 | && _config_file="/boot/config-$kernel" |
a0120420 | 712 | [[ -f $dracutsysrootdir/lib/modules/$kernel/config ]] \ |
8e3f6537 HH |
713 | && _config_file="/lib/modules/$kernel/config" |
714 | ||
715 | # no kernel config file, so return true | |
716 | [[ $_config_file ]] || return 0 | |
717 | ||
560f45b1 | 718 | grep -q -F "${_config_opt}=" "$dracutsysrootdir$_config_file" && return 0 |
8e3f6537 HH |
719 | return 1 |
720 | } | |
721 | ||
c050190f KS |
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() { | |
2b5ddc77 | 725 | modprobe -S "$kernel" --dry-run "$1" &> /dev/null || return 1 |
c050190f | 726 | } |
8e3f6537 | 727 | |
5f2c30d9 KRW |
728 | # get_cpu_vendor |
729 | # Only two values are returned: AMD or Intel | |
9a52c3fd | 730 | get_cpu_vendor() { |
5f2c30d9 KRW |
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 | |
9a52c3fd | 741 | get_ucode_file() { |
2b5ddc77 HH |
742 | local family |
743 | local model | |
744 | local stepping | |
122657b2 KS |
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/.*:\ //") | |
5f2c30d9 KRW |
748 | |
749 | if [[ "$(get_cpu_vendor)" == "AMD" ]]; then | |
19453dc8 | 750 | if [[ $family -ge 21 ]]; then |
2b5ddc77 | 751 | printf "microcode_amd_fam%xh.bin" "$family" |
5f2c30d9 KRW |
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. | |
2b5ddc77 | 758 | printf "%02x-%02x-%02x" "${family}" "${model}" "${stepping}" |
5f2c30d9 KRW |
759 | fi |
760 | } | |
96c6f6f3 MC |
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 | |
2b5ddc77 | 766 | [[ ! -f $dev_dm_dir/uuid || $(< "$dev_dm_dir"/uuid) != LVM-* ]] && return 1 # Not an LVM device |
96c6f6f3 | 767 | local DM_VG_NAME DM_LV_NAME DM_LV_LAYER |
2b5ddc77 | 768 | eval "$(dmsetup splitname --nameprefixes --noheadings --rows "$(< "$dev_dm_dir"/name)" 2> /dev/null)" |
96c6f6f3 MC |
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 | ||
1cadc26f HH |
773 | btrfs_devs() { |
774 | local _mp="$1" | |
775 | btrfs device usage "$_mp" \ | |
2b5ddc77 | 776 | | while read -r _dev _; do |
9a52c3fd HH |
777 | str_starts "$_dev" "/" || continue |
778 | _dev=${_dev%,} | |
779 | printf -- "%s\n" "$_dev" | |
1cadc26f HH |
780 | done |
781 | } | |
ceca74cc | 782 | |
9582f027 SJ |
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 | ||
ceca74cc | 791 | iface_for_remote_addr() { |
2b5ddc77 | 792 | # shellcheck disable=SC2046 |
ceca74cc | 793 | set -- $(ip -o route get to "$1") |
d754e1c6 MW |
794 | while [ $# -gt 0 ]; do |
795 | case $1 in | |
796 | dev) | |
797 | echo "$2" | |
798 | return | |
799 | ;; | |
800 | esac | |
801 | shift | |
802 | done | |
ceca74cc MW |
803 | } |
804 | ||
805 | local_addr_for_remote_addr() { | |
2b5ddc77 | 806 | # shellcheck disable=SC2046 |
ceca74cc | 807 | set -- $(ip -o route get to "$1") |
d754e1c6 MW |
808 | while [ $# -gt 0 ]; do |
809 | case $1 in | |
810 | src) | |
811 | echo "$2" | |
812 | return | |
813 | ;; | |
814 | esac | |
815 | shift | |
816 | done | |
ceca74cc MW |
817 | } |
818 | ||
819 | peer_for_addr() { | |
820 | local addr=$1 | |
821 | local qtd | |
822 | ||
823 | # quote periods in IPv4 address | |
824 | qtd=${addr//./\\.} | |
9a52c3fd HH |
825 | ip -o addr show \ |
826 | | sed -n 's%^.* '"$qtd"' peer \([0-9a-f.:]\{1,\}\(/[0-9]*\)\?\).*$%\1%p' | |
ceca74cc MW |
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 | |
9a52c3fd HH |
842 | *.*) proto=4 ;; |
843 | *:*) proto=6 ;; | |
844 | *) return ;; | |
ceca74cc | 845 | esac |
9a52c3fd HH |
846 | ip -o -$proto route show \ |
847 | | sed -n "s/^default via \([0-9a-z.:]\{1,\}\) dev $ifname .*\$/\1/p" | |
ceca74cc MW |
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 | |
2b5ddc77 | 873 | local ifname local_addr peer netmask gateway ifmac |
ceca74cc MW |
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." | |
9a52c3fd | 885 | elif [ -e "/sys/class/net/$ifname/address" ]; then |
ceca74cc MW |
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 | |
9a52c3fd | 892 | dhcp | dhcp6 | auto6) ;; |
ceca74cc | 893 | dhcp4) |
9a52c3fd HH |
894 | bootproto=dhcp |
895 | ;; | |
896 | static* | "") | |
897 | bootproto= | |
898 | ;; | |
ceca74cc MW |
899 | *) |
900 | derror "bootproto \"$bootproto\" is unsupported by dracut, trying static configuration" | |
9a52c3fd HH |
901 | bootproto= |
902 | ;; | |
ceca74cc MW |
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 ' \ | |
9a52c3fd | 921 | "${local_addr}" "${peer}" "${gateway}" "${netmask}" "${ifname}" |
ceca74cc MW |
922 | fi |
923 | ||
924 | } | |
480aa969 DM |
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> | |
0afa840e | 933 | # Check whether $1 is an iSCSI device |
480aa969 DM |
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 | |
2b5ddc77 | 957 | subsystem=$(basename "$(readlink "$_dir"/subsystem)") |
480aa969 DM |
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 | } | |
cbafcd0f KS |
969 | |
970 | # get the corresponding kernel modules of a /sys/class/*/* or/dev/* device | |
971 | get_dev_module() { | |
dc3b976f AAF |
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" | |
cbafcd0f | 997 | } |