3 # functions used by dracut and other tools.
5 # Copyright 2005-2009 Red Hat, Inc. All rights reserved.
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.
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.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 # Check whether $1 is a function.
25 [[ "$(type -t "$1")" == "function" ]]
28 # Generic substring function. If $2 is in $1, return 0.
29 strstr
() { [[ $1 == *"$2"* ]]; }
30 # Generic glob matching function. If glob pattern $2 matches anywhere in $1, OK
31 strglobin
() { [[ $1 == *$2* ]]; }
32 # Generic glob matching function. If glob pattern $2 matches all of $1, OK
33 # shellcheck disable=SC2053
34 strglob
() { [[ $1 == $2 ]]; }
35 # returns OK if $1 contains literal string $2 at the beginning, and isn't empty
36 str_starts
() { [ "${1#"$2"*}" != "$1" ]; }
37 # returns OK if $1 contains literal string $2 at the end, and isn't empty
38 str_ends
() { [ "${1%*"$2"}" != "$1" ]; }
40 # find a binary. If we were not passed the full path directly,
41 # search in the usual places to find the binary.
47 [[ -z ${1##/*} ]] || _delim
="/"
49 if [[ $1 == *.so
* ]]; then
50 # shellcheck disable=SC2154
52 _path
="${l}${_delim}${1}"
53 if { $DRACUT_LDD "${dracutsysrootdir}${_path}" &> /dev/null; }; then
54 printf "%s
\n" "${_path}"
59 if { $DRACUT_LDD "${dracutsysrootdir}${_path}" &> /dev/null; }; then
60 printf "%s\n" "${_path}"
64 if [[ $1 == */* ]]; then
66 if [[ -L ${dracutsysrootdir}${_path} ]] || [[ -x ${dracutsysrootdir}${_path} ]]; then
67 printf "%s\n" "${_path}"
71 for p
in $DRACUT_PATH; do
72 _path
="${p}${_delim}${1}"
73 if [[ -L ${dracutsysrootdir}${_path} ]] || [[ -x ${dracutsysrootdir}${_path} ]]; then
74 printf "%s\n" "${_path}"
79 [[ -n $dracutsysrootdir ]] && return 1
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
87 # Version comparision function. Assumes Linux style version scheme.
89 # $2 = comparision op (gt, ge, eq, le, lt, ne)
93 read -r -a _n1
<<< "${1//./ }"
96 read -r -a _n2
<<< "${3//./ }"
99 for ((_i
= 0; ; _i
++)); do
100 if [[ ! ${_n1[_i]}${_n2[_i]} ]]; then
102 elif ((${_n1[_i]:-0} > ${_n2[_i]:-0})); then
104 elif ((${_n1[_i]:-0} < ${_n2[_i]:-0})); then
122 # Create all subdirectories for given path without creating the last element.
125 # shellcheck disable=SC2174
126 [[ -e ${1%/*} ]] || mkdir
-m 0755 -p -- "${1%/*}"
129 # Function prints global variables in format name=value line by line.
130 # $@ = list of global variables' name
135 eval printf -v _value
"%s" \""\$$_var"\"
136 [[ ${_value} ]] && printf '%s="%s"\n' "$_var" "$_value"
140 # normalize_path <path>
141 # Prints the normalized path, where it removes any duplicated
142 # and trailing slashes.
144 # $ normalize_path ///test/test//
147 # shellcheck disable=SC2064
148 trap "$(shopt -p extglob)" RETURN
150 local p
=${1//+(\/)//}
151 printf "%s\n" "${p%/}"
154 # convert_abs_rel <from> <to>
155 # Prints the relative path, when creating a symlink to <to> from <from>.
157 # $ convert_abs_rel /usr/bin/test /bin/test-2
159 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
161 local __current __absolute __abssize __cursize __newpath
164 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
166 # corner case #1 - self looping link
167 [[ $1 == "$2" ]] && {
168 printf "%s\n" "${1##*/}"
172 # corner case #2 - own dir link
173 [[ ${1%/*} == "$2" ]] && {
178 IFS
=/ read -r -a __current
<<< "$1"
179 IFS
=/ read -r -a __absolute
<<< "$2"
181 __abssize
=${#__absolute[@]}
182 __cursize
=${#__current[@]}
184 while [[ ${__absolute[__level]} == "${__current[__level]}" ]]; do
186 if ((__level
> __abssize || __level
> __cursize
)); then
191 for ((__i
= __level
; __i
< __cursize
- 1; __i
++)); do
192 if ((__i
> __level
)); then
193 __newpath
=$__newpath"/"
195 __newpath
=$__newpath".."
198 for ((__i
= __level
; __i
< __abssize
; __i
++)); do
199 if [[ -n $__newpath ]]; then
200 __newpath
=$__newpath"/"
202 __newpath
=$__newpath${__absolute[__i]}
205 printf -- "%s\n" "$__newpath"
208 # get_fs_env <device>
209 # Get and the ID_FS_TYPE variable from udev for a device.
211 # $ get_fs_env /dev/sda2
216 ID_FS_TYPE
=$
(blkid
-u filesystem
-o export -- "$1" \
217 |
while read -r line ||
[ -n "$line" ]; do
218 if [[ $line == "TYPE="* ]]; then
219 printf "%s" "${line#TYPE=}"
223 if [[ $ID_FS_TYPE ]]; then
224 printf "%s" "$ID_FS_TYPE"
230 # get_maj_min <device>
231 # Prints the major and minor of a device node.
233 # $ get_maj_min /dev/sda2
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:?}"
246 # get_devpath_block <device>
247 # get the DEVPATH in /sys of a block device
248 get_devpath_block
() {
250 _majmin
=$
(get_maj_min
"$1")
252 for _i
in /sys
/block
/*/dev
/sys
/block
/*/*/dev
; do
253 [[ -e $_i ]] ||
continue
254 if [[ $_majmin == "$(< "$_i")" ]]; then
255 printf "%s" "${_i%/dev}"
262 # get a persistent path from a device
263 get_persistent_dev
() {
264 local i _tmp _dev _pol
266 _dev
=$
(get_maj_min
"$1")
267 [ -z "$_dev" ] && return
269 if [[ -n $persistent_policy ]]; then
270 _pol
="/dev/disk/${persistent_policy}/*"
278 /dev
/disk
/by-uuid
/* \
279 /dev
/disk
/by-label
/* \
280 /dev
/disk
/by-partuuid
/* \
281 /dev
/disk
/by-partlabel
/* \
283 /dev
/disk
/by-path
/*; do
284 [[ -e $i ]] ||
continue
285 [[ $i == /dev
/mapper
/control
]] && continue
286 [[ $i == /dev
/mapper
/mpath
* ]] && continue
287 _tmp
=$
(get_maj_min
"$i")
288 if [ "$_tmp" = "$_dev" ]; then
296 expand_persistent_dev
() {
301 _dev
="/dev/disk/by-label/${_dev#LABEL=}"
306 _dev
="/dev/disk/by-uuid/${_dev}"
309 _dev
="${_dev#PARTUUID=}"
311 _dev
="/dev/disk/by-partuuid/${_dev}"
314 _dev
="/dev/disk/by-partlabel/${_dev#PARTLABEL=}"
320 shorten_persistent_dev
() {
324 printf "%s" "UUID=${_dev##*/}"
326 /dev
/disk
/by-label
/*)
327 printf "%s" "LABEL=${_dev##*/}"
329 /dev
/disk
/by-partuuid
/*)
330 printf "%s" "PARTUUID=${_dev##*/}"
332 /dev
/disk
/by-partlabel
/*)
333 printf "%s" "PARTLABEL=${_dev##*/}"
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.
349 # $ find_block_device /usr
351 find_block_device
() {
352 local _dev _majmin _find_mpt
355 if [[ $use_fstab != yes ]]; then
356 [[ -d $_find_mpt/.
]]
357 findmnt
-e -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" |
{
358 while read -r _majmin _dev ||
[ -n "$_dev" ]; do
359 if [[ -b $_dev ]]; then
360 if ! [[ $_majmin ]] ||
[[ $_majmin == 0:* ]]; then
361 _majmin
=$
(get_maj_min
"$_dev")
363 if [[ $_majmin ]]; then
364 printf "%s\n" "$_majmin"
366 printf "%s\n" "$_dev"
370 if [[ $_dev == *:* ]]; then
371 printf "%s\n" "$_dev"
378 # fall back to /etc/fstab
380 findmnt
-e --fstab -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" |
{
381 while read -r _majmin _dev ||
[ -n "$_dev" ]; do
382 if ! [[ $_dev ]]; then
386 if [[ -b $_dev ]]; then
387 [[ $_majmin ]] || _majmin
=$
(get_maj_min
"$_dev")
388 if [[ $_majmin ]]; then
389 printf "%s\n" "$_majmin"
391 printf "%s\n" "$_dev"
395 if [[ $_dev == *:* ]]; then
396 printf "%s\n" "$_dev"
406 # find_mp_fstype <mountpoint>
407 # Echo the filesystem type for a given mountpoint.
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!
412 # $ find_mp_fstype /;echo
417 if [[ $use_fstab != yes ]]; then
418 findmnt
-e -v -n -o 'FSTYPE' --target "$1" |
{
419 while read -r _fs ||
[ -n "$_fs" ]; do
420 [[ $_fs ]] ||
continue
421 [[ $_fs == "autofs" ]] && continue
429 findmnt
--fstab -e -v -n -o 'FSTYPE' --target "$1" |
{
430 while read -r _fs ||
[ -n "$_fs" ]; do
431 [[ $_fs ]] ||
continue
432 [[ $_fs == "autofs" ]] && continue
442 # find_dev_fstype <device>
443 # Echo the filesystem type for a given device.
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!
448 # $ find_dev_fstype /dev/sda2;echo
453 if ! [[ $_find_dev == /dev
* ]]; then
454 [[ -b "/dev/block/$_find_dev" ]] && _find_dev
="/dev/block/$_find_dev"
457 if [[ $use_fstab != yes ]]; then
458 findmnt
-e -v -n -o 'FSTYPE' --source "$_find_dev" |
{
459 while read -r _fs ||
[ -n "$_fs" ]; do
460 [[ $_fs ]] ||
continue
461 [[ $_fs == "autofs" ]] && continue
469 findmnt
--fstab -e -v -n -o 'FSTYPE' --source "$_find_dev" |
{
470 while read -r _fs ||
[ -n "$_fs" ]; do
471 [[ $_fs ]] ||
continue
472 [[ $_fs == "autofs" ]] && continue
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!
488 # $ find_mp_fsopts /;echo
489 # rw,relatime,discard,data=ordered
491 if [[ $use_fstab != yes ]]; then
492 findmnt
-e -v -n -o 'OPTIONS' --target "$1" 2> /dev
/null
&& return 0
495 findmnt
--fstab -e -v -n -o 'OPTIONS' --target "$1"
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.
502 # if `use_fstab == yes`, then only `/etc/fstab` is used.
505 # $ find_dev_fsopts /dev/sda2
506 # rw,relatime,discard,data=ordered
510 if ! [[ $_find_dev == /dev
* ]]; then
511 [[ -b "/dev/block/$_find_dev" ]] && _find_dev
="/dev/block/$_find_dev"
514 if [[ $use_fstab != yes ]]; then
515 findmnt
-e -v -n -o 'OPTIONS' --source "$_find_dev" 2> /dev
/null
&& return 0
518 findmnt
--fstab -e -v -n -o 'OPTIONS' --source "$_find_dev"
521 # finds the major:minor of the block device backing the root filesystem.
522 find_root_block_device
() { find_block_device
/; }
524 # for_each_host_dev_fs <func>
525 # Execute "<func> <dev> <filesystem>" for every "<dev> <fs>" pair found
526 # in ${host_fs_types[@]}
527 for_each_host_dev_fs
() {
532 [[ "${#host_fs_types[@]}" ]] ||
return 2
534 for _dev
in "${!host_fs_types[@]}"; do
535 $_func "$_dev" "${host_fs_types[$_dev]}" && _ret
=0
541 printf "%s\n" "${host_fs_types[@]}"
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
548 check_block_and_slaves
() {
550 [[ -b /dev
/block
/$2 ]] ||
return 1 # Not a block device? So sorry.
551 if ! lvm_internal_dev
"$2"; then "$1" "$2" && return; fi
552 check_vol_slaves
"$@" && return 0
553 if [[ -f /sys
/dev
/block
/$2/..
/dev
]] && [[ /sys
/dev
/block
/$2/..
/subsystem
-ef /sys
/class
/block
]]; then
554 check_block_and_slaves
"$1" "$(< "/sys
/dev
/block
/$2/..
/dev
")" && return 0
556 for _x
in /sys
/dev
/block
/"$2"/slaves
/*; do
557 [[ -f $_x/dev
]] ||
continue
558 [[ $_x/subsystem
-ef /sys
/class
/block
]] ||
continue
559 check_block_and_slaves
"$1" "$(< "$_x/dev
")" && return 0
564 check_block_and_slaves_all
() {
566 [[ -b /dev
/block
/$2 ]] ||
return 1 # Not a block device? So sorry.
567 if ! lvm_internal_dev
"$2" && "$1" "$2"; then
570 check_vol_slaves_all
"$@" && return 0
571 if [[ -f /sys
/dev
/block
/$2/..
/dev
]] && [[ /sys
/dev
/block
/$2/..
/subsystem
-ef /sys
/class
/block
]]; then
572 check_block_and_slaves_all
"$1" "$(< "/sys
/dev
/block
/$2/..
/dev
")" && _ret
=0
574 for _x
in /sys
/dev
/block
/"$2"/slaves
/*; do
575 [[ -f $_x/dev
]] ||
continue
576 [[ $_x/subsystem
-ef /sys
/class
/block
]] ||
continue
577 check_block_and_slaves_all
"$1" "$(< "$_x/dev
")" && _ret
=0
581 # for_each_host_dev_and_slaves <func>
582 # Execute "<func> <dev>" for every "<dev>" found
583 # in ${host_devs[@]} and their slaves
584 for_each_host_dev_and_slaves_all
() {
589 [[ "${host_devs[*]}" ]] ||
return 2
591 for _dev
in "${host_devs[@]}"; do
592 [[ -b $_dev ]] ||
continue
593 if check_block_and_slaves_all
"$_func" "$(get_maj_min "$_dev")"; then
600 for_each_host_dev_and_slaves
() {
604 [[ "${host_devs[*]}" ]] ||
return 2
606 for _dev
in "${host_devs[@]}"; do
607 [[ -b $_dev ]] ||
continue
608 check_block_and_slaves
"$_func" "$(get_maj_min "$_dev")" && return 0
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.
618 # And the volume group might be bigger than the devices the LV needs.
620 local _vg _pv _dm _majmin
622 _dm
=/sys
/dev
/block
/$_majmin/dm
623 [[ -f $_dm/uuid
&& $
(< "$_dm"/uuid
) =~ LVM-
* ]] ||
return 1
624 _vg
=$
(dmsetup splitname
--noheadings -o vg_name
"$(< "$_dm/name
")")
626 _vg
="${_vg//[[:space:]]/}"
628 for _pv
in $
(lvm vgs
--noheadings -o pv_name
"$_vg" 2> /dev
/null
); do
629 check_block_and_slaves
"$1" "$(get_maj_min "$_pv")" && return 0
635 check_vol_slaves_all
() {
636 local _vg _pv _majmin
638 _dm
="/sys/dev/block/$_majmin/dm"
639 [[ -f $_dm/uuid
&& $
(< "$_dm"/uuid
) =~ LVM-
* ]] ||
return 1
640 _vg
=$
(dmsetup splitname
--noheadings -o vg_name
"$(< "$_dm/name
")")
642 _vg
="${_vg//[[:space:]]/}"
644 # when filter/global_filter is set, lvm may be failed
645 if ! lvm lvs
--noheadings -o vg_name
"$_vg" 2> /dev
/null
1> /dev
/null
; then
649 for _pv
in $
(lvm vgs
--noheadings -o pv_name
"$_vg" 2> /dev
/null
); do
650 check_block_and_slaves_all
"$1" "$(get_maj_min "$_pv")"
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
665 # shellcheck disable=SC2086
668 while [ $# -gt 0 ]; do
671 echo "${1#${_option}=}"
679 check_kernel_config
() {
680 local _config_opt
="$1"
682 [[ -f $dracutsysrootdir/boot
/config-
$kernel ]] \
683 && _config_file
="/boot/config-$kernel"
684 [[ -f $dracutsysrootdir/lib
/modules
/$kernel/config
]] \
685 && _config_file
="/lib/modules/$kernel/config"
687 # no kernel config file, so return true
688 [[ $_config_file ]] ||
return 0
690 grep -q -F "${_config_opt}=" "$dracutsysrootdir$_config_file" && return 0
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
() {
697 modprobe
-S "$kernel" --dry-run "$1" &> /dev
/null ||
return 1
701 # Only two values are returned: AMD or Intel
703 if grep -qE AMD
/proc
/cpuinfo
; then
706 if grep -qE Intel
/proc
/cpuinfo
; then
712 # Get the hosts' ucode file based on the /proc/cpuinfo
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/.*:\ //")
721 if [[ "$(get_cpu_vendor)" == "AMD" ]]; then
722 if [[ $family -ge 21 ]]; then
723 printf "microcode_amd_fam%xh.bin" "$family"
725 printf "microcode_amd.bin"
728 if [[ "$(get_cpu_vendor)" == "Intel" ]]; then
729 # The /proc/cpuinfo are in decimal.
730 printf "%02x-%02x-%02x" "${family}" "${model}" "${stepping}"
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.
737 local dev_dm_dir
=/sys
/dev
/block
/$1/dm
738 [[ ! -f $dev_dm_dir/uuid || $
(< "$dev_dm_dir"/uuid
) != LVM-
* ]] && return 1 # Not an LVM device
739 local DM_VG_NAME DM_LV_NAME DM_LV_LAYER
740 eval "$(dmsetup splitname --nameprefixes --noheadings --rows "$
(< "$dev_dm_dir"/name
)" 2> /dev/null)"
741 [[ ${DM_VG_NAME} ]] && [[ ${DM_LV_NAME} ]] ||
return 0 # Better skip this!
742 [[ ${DM_LV_LAYER} ]] || [[ ! -L /dev/${DM_VG_NAME}/${DM_LV_NAME} ]]
747 btrfs device usage
"$_mp" \
748 |
while read -r _dev _
; do
749 str_starts
"$_dev" "/" ||
continue
751 printf -- "%s\n" "$_dev"
755 iface_for_remote_addr
() {
756 # shellcheck disable=SC2046
757 set -- $
(ip
-o route get to
"$1")
761 local_addr_for_remote_addr
() {
762 # shellcheck disable=SC2046
763 set -- $
(ip
-o route get to
"$1")
771 # quote periods in IPv4 address
774 |
sed -n 's%^.* '"$qtd"' peer \([0-9a-f.:]\{1,\}\(/[0-9]*\)\?\).*$%\1%p'
781 # quote periods in IPv4 address
783 ip
-o addr show |
sed -n 's,^.* '"$qtd"'/\([0-9]*\) .*$,\1,p'
786 gateway_for_iface
() {
787 local ifname
=$1 addr
=$2
794 ip
-o -$proto route show \
795 |
sed -n "s/^default via \([0-9a-z.:]\{1,\}\) dev $ifname .*\$/\1/p"
798 # This works only for ifcfg-style network configuration!
799 bootproto_for_iface
() {
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"
813 is_unbracketed_ipv6_address
() {
814 strglob
"$1" '*:*' && ! strglob
"$1" '\[*:*\]'
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
() {
821 local ifname local_addr peer netmask gateway ifmac
823 [[ $remote_addr ]] ||
return 1
824 ifname
=$
(iface_for_remote_addr
"$remote_addr")
826 berror
"failed to determine interface to connect to $remote_addr"
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."
833 elif [ -e "/sys/class/net/$ifname/address" ]; then
834 ifmac
=$
(cat "/sys/class/net/$ifname/address")
835 [[ $ifmac ]] && printf 'ifname=%s:%s ' "${ifname}" "${ifmac}"
838 bootproto
=$
(bootproto_for_iface
"$ifname")
840 dhcp | dhcp6 | auto6
) ;;
848 derror
"bootproto \"$bootproto\" is unsupported by dracut, trying static configuration"
852 if [[ $bootproto ]]; then
853 printf 'ip=%s:%s ' "${ifname}" "${bootproto}"
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"
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 ' \
869 "${local_addr}" "${peer}" "${gateway}" "${netmask}" "${ifname}"
874 # block_is_nbd <maj:min>
875 # Check whether $1 is an nbd device
877 [[ -b /dev
/block
/$1 && $1 == 43:* ]]
880 # block_is_iscsi <maj:min>
881 # Check whether $1 is an nbd device
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
890 [[ -d "$_dir/iscsi_session" ]]
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
900 [[ -L "/sys/dev/block/$_dev" ]] ||
return
901 _dir
="$(readlink -f "/sys
/dev
/block
/$_dev")"
902 until [[ -d "$_dir/sys" ]]; do
904 if [[ -d "$_dir/subsystem" ]]; then
905 subsystem
=$
(basename "$(readlink "$_dir"/subsystem)")
906 [[ $subsystem == "fcoe" ]] && return 0
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"
918 # get the corresponding kernel modules of a /sys/class/*/* or/dev/* device
920 udevadm info
-a "$1" |
sed -n 's/\s*DRIVERS=="\(\S\+\)"/\1/p'