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" ]; }
42 var
="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
43 var
="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
47 # find a binary. If we were not passed the full path directly,
48 # search in the usual places to find the binary.
54 [[ -z ${1##/*} ]] || _delim
="/"
56 if [[ $1 == *.so
* ]]; then
58 _path
="${l}${_delim}${1}"
59 if { $DRACUT_LDD "${dracutsysrootdir}${_path}" &> /dev/null; }; then
60 printf "%s
\n" "${_path}"
65 if { $DRACUT_LDD "${dracutsysrootdir}${_path}" &> /dev/null; }; then
66 printf "%s\n" "${_path}"
70 if [[ $1 == */* ]]; then
72 if [[ -L ${dracutsysrootdir}${_path} ]] || [[ -x ${dracutsysrootdir}${_path} ]]; then
73 printf "%s\n" "${_path}"
77 for p
in $DRACUT_PATH; do
78 _path
="${p}${_delim}${1}"
79 if [[ -L ${dracutsysrootdir}${_path} ]] || [[ -x ${dracutsysrootdir}${_path} ]]; then
80 printf "%s\n" "${_path}"
85 [[ -n $dracutsysrootdir ]] && return 1
90 $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
93 # Version comparison function. Assumes Linux style version scheme.
95 # $2 = comparison op (gt, ge, eq, le, lt, ne)
99 read -r -a _n1
<<< "${1//./ }"
102 read -r -a _n2
<<< "${3//./ }"
105 for ((_i
= 0; ; _i
++)); do
106 if [[ ! ${_n1[_i]}${_n2[_i]} ]]; then
108 elif ((${_n1[_i]:-0} > ${_n2[_i]:-0})); then
110 elif ((${_n1[_i]:-0} < ${_n2[_i]:-0})); then
128 # Create all subdirectories for given path without creating the last element.
131 # shellcheck disable=SC2174
132 [[ -e ${1%/*} ]] || mkdir
-m 0755 -p -- "${1%/*}"
135 # Function prints global variables in format name=value line by line.
136 # $@ = list of global variables' name
141 eval printf -v _value
"%s" \""\$$_var"\"
142 [[ ${_value} ]] && printf '%s="%s"\n' "$_var" "$_value"
146 # normalize_path <path>
147 # Prints the normalized path, where it removes any duplicated
148 # and trailing slashes.
150 # $ normalize_path ///test/test//
153 # shellcheck disable=SC2064
154 trap "$(shopt -p extglob)" RETURN
156 local p
=${1//+(\/)//}
157 printf "%s\n" "${p%/}"
160 # convert_abs_rel <from> <to>
161 # Prints the relative path, when creating a symlink to <to> from <from>.
163 # $ convert_abs_rel /usr/bin/test /bin/test-2
165 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
167 local __current __absolute __abssize __cursize __newpath
170 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
172 # corner case #1 - self looping link
173 [[ $1 == "$2" ]] && {
174 printf "%s\n" "${1##*/}"
178 # corner case #2 - own dir link
179 [[ ${1%/*} == "$2" ]] && {
184 IFS
=/ read -r -a __current
<<< "$1"
185 IFS
=/ read -r -a __absolute
<<< "$2"
187 __abssize
=${#__absolute[@]}
188 __cursize
=${#__current[@]}
190 while [[ ${__absolute[__level]} == "${__current[__level]}" ]]; do
192 if ((__level
> __abssize || __level
> __cursize
)); then
197 for ((__i
= __level
; __i
< __cursize
- 1; __i
++)); do
198 if ((__i
> __level
)); then
199 __newpath
=$__newpath"/"
201 __newpath
=$__newpath".."
204 for ((__i
= __level
; __i
< __abssize
; __i
++)); do
205 if [[ -n $__newpath ]]; then
206 __newpath
=$__newpath"/"
208 __newpath
=$__newpath${__absolute[__i]}
211 printf -- "%s\n" "$__newpath"
214 # get_fs_env <device>
215 # Get and the ID_FS_TYPE variable from udev for a device.
217 # $ get_fs_env /dev/sda2
222 ID_FS_TYPE
=$
(blkid
-u filesystem
-o export -- "$1" \
223 |
while read -r line ||
[ -n "$line" ]; do
224 if [[ $line == "TYPE="* ]]; then
225 printf "%s" "${line#TYPE=}"
229 if [[ $ID_FS_TYPE ]]; then
230 printf "%s" "$ID_FS_TYPE"
236 # get_maj_min <device>
237 # Prints the major and minor of a device node.
239 # $ get_maj_min /dev/sda2
245 if [[ $get_maj_min_cache_file ]]; then
246 _out
="$(grep -m1 -oE "^
$1 \S
+$
" "$get_maj_min_cache_file" | awk '{print $NF}')"
249 if ! [[ "$_out" ]]; then
250 _majmin
="$(stat -L -c '%t:%T' "$1" 2> /dev/null)"
251 _out
="$(printf "%s
" "$
((0x
${_majmin%:*})):$
((0x
${_majmin#*:}))")"
252 if [[ $get_maj_min_cache_file ]]; then
253 echo "$1 $_out" >> "$get_maj_min_cache_file"
259 # get_devpath_block <device>
260 # get the DEVPATH in /sys of a block device
261 get_devpath_block
() {
263 _majmin
=$
(get_maj_min
"$1")
265 for _i
in /sys
/block
/*/dev
/sys
/block
/*/*/dev
; do
266 [[ -e $_i ]] ||
continue
267 if [[ $_majmin == "$(< "$_i")" ]]; then
268 printf "%s" "${_i%/dev}"
275 # get a persistent path from a device
276 get_persistent_dev
() {
277 local i _tmp _dev _pol
279 _dev
=$
(get_maj_min
"$1")
280 [ -z "$_dev" ] && return
282 if [[ -n $persistent_policy ]]; then
283 _pol
="/dev/disk/${persistent_policy}/*"
291 /dev
/disk
/by-uuid
/* \
292 /dev
/disk
/by-label
/* \
293 /dev
/disk
/by-partuuid
/* \
294 /dev
/disk
/by-partlabel
/* \
296 /dev
/disk
/by-path
/*; do
297 [[ -e $i ]] ||
continue
298 [[ $i == /dev
/mapper
/control
]] && continue
299 [[ $i == /dev
/mapper
/mpath
* ]] && continue
300 _tmp
=$
(get_maj_min
"$i")
301 if [ "$_tmp" = "$_dev" ]; then
309 expand_persistent_dev
() {
314 _dev
="/dev/disk/by-label/${_dev#LABEL=}"
319 _dev
="/dev/disk/by-uuid/${_dev}"
322 _dev
="${_dev#PARTUUID=}"
324 _dev
="/dev/disk/by-partuuid/${_dev}"
327 _dev
="/dev/disk/by-partlabel/${_dev#PARTLABEL=}"
333 shorten_persistent_dev
() {
337 printf "%s" "UUID=${_dev##*/}"
339 /dev
/disk
/by-label
/*)
340 printf "%s" "LABEL=${_dev##*/}"
342 /dev
/disk
/by-partuuid
/*)
343 printf "%s" "PARTUUID=${_dev##*/}"
345 /dev
/disk
/by-partlabel
/*)
346 printf "%s" "PARTLABEL=${_dev##*/}"
354 # find_block_device <mountpoint>
355 # Prints the major and minor number of the block device
356 # for a given mountpoint.
357 # Unless $use_fstab is set to "yes" the functions
358 # uses /proc/self/mountinfo as the primary source of the
359 # information and only falls back to /etc/fstab, if the mountpoint
360 # is not found there.
362 # $ find_block_device /usr
364 find_block_device
() {
365 local _dev _majmin _find_mpt
368 if [[ $use_fstab != yes ]]; then
369 [[ -d $_find_mpt/.
]]
370 findmnt
-e -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" |
{
371 while read -r _majmin _dev ||
[ -n "$_dev" ]; do
372 if [[ -b $_dev ]]; then
373 if ! [[ $_majmin ]] ||
[[ $_majmin == 0:* ]]; then
374 _majmin
=$
(get_maj_min
"$_dev")
376 if [[ $_majmin ]]; then
377 printf "%s\n" "$_majmin"
379 printf "%s\n" "$_dev"
383 if [[ $_dev == *:* ]]; then
384 printf "%s\n" "$_dev"
391 # fall back to /etc/fstab
392 [[ ! -f "$dracutsysrootdir"/etc
/fstab
]] && return 1
394 findmnt
-e --fstab -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" |
{
395 while read -r _majmin _dev ||
[ -n "$_dev" ]; do
396 if ! [[ $_dev ]]; then
400 if [[ -b $_dev ]]; then
401 [[ $_majmin ]] || _majmin
=$
(get_maj_min
"$_dev")
402 if [[ $_majmin ]]; then
403 printf "%s\n" "$_majmin"
405 printf "%s\n" "$_dev"
409 if [[ $_dev == *:* ]]; then
410 printf "%s\n" "$_dev"
420 # find_mp_fstype <mountpoint>
421 # Echo the filesystem type for a given mountpoint.
422 # /proc/self/mountinfo is taken as the primary source of information
423 # and /etc/fstab is used as a fallback.
424 # No newline is appended!
426 # $ find_mp_fstype /;echo
431 if [[ $use_fstab != yes ]]; then
432 findmnt
-e -v -n -o 'FSTYPE' --target "$1" |
{
433 while read -r _fs ||
[ -n "$_fs" ]; do
434 [[ $_fs ]] ||
continue
435 [[ $_fs == "autofs" ]] && continue
443 [[ ! -f "$dracutsysrootdir"/etc
/fstab
]] && return 1
445 findmnt
--fstab -e -v -n -o 'FSTYPE' --target "$1" |
{
446 while read -r _fs ||
[ -n "$_fs" ]; do
447 [[ $_fs ]] ||
continue
448 [[ $_fs == "autofs" ]] && continue
458 # find_dev_fstype <device>
459 # Echo the filesystem type for a given device.
460 # /proc/self/mountinfo is taken as the primary source of information
461 # and /etc/fstab is used as a fallback.
462 # No newline is appended!
464 # $ find_dev_fstype /dev/sda2;echo
469 if ! [[ $_find_dev == /dev
* ]]; then
470 [[ -b "/dev/block/$_find_dev" ]] && _find_dev
="/dev/block/$_find_dev"
473 if [[ $use_fstab != yes ]]; then
474 findmnt
-e -v -n -o 'FSTYPE' --source "$_find_dev" |
{
475 while read -r _fs ||
[ -n "$_fs" ]; do
476 [[ $_fs ]] ||
continue
477 [[ $_fs == "autofs" ]] && continue
485 [[ ! -f "$dracutsysrootdir"/etc
/fstab
]] && return 1
487 findmnt
--fstab -e -v -n -o 'FSTYPE' --source "$_find_dev" |
{
488 while read -r _fs ||
[ -n "$_fs" ]; do
489 [[ $_fs ]] ||
continue
490 [[ $_fs == "autofs" ]] && continue
500 # find_mp_fsopts <mountpoint>
501 # Echo the filesystem options for a given mountpoint.
502 # /proc/self/mountinfo is taken as the primary source of information
503 # and /etc/fstab is used as a fallback.
504 # No newline is appended!
506 # $ find_mp_fsopts /;echo
507 # rw,relatime,discard,data=ordered
509 if [[ $use_fstab != yes ]]; then
510 findmnt
-e -v -n -o 'OPTIONS' --target "$1" 2> /dev
/null
&& return 0
513 [[ ! -f "$dracutsysrootdir"/etc
/fstab
]] && return 1
515 findmnt
--fstab -e -v -n -o 'OPTIONS' --target "$1"
518 # find_dev_fsopts <device>
519 # Echo the filesystem options for a given device.
520 # /proc/self/mountinfo is taken as the primary source of information
521 # and /etc/fstab is used as a fallback.
522 # if `use_fstab == yes`, then only `/etc/fstab` is used.
525 # $ find_dev_fsopts /dev/sda2
526 # rw,relatime,discard,data=ordered
530 if ! [[ $_find_dev == /dev
* ]]; then
531 [[ -b "/dev/block/$_find_dev" ]] && _find_dev
="/dev/block/$_find_dev"
534 if [[ $use_fstab != yes ]]; then
535 findmnt
-e -v -n -o 'OPTIONS' --source "$_find_dev" 2> /dev
/null
&& return 0
538 [[ ! -f "$dracutsysrootdir"/etc
/fstab
]] && return 1
540 findmnt
--fstab -e -v -n -o 'OPTIONS' --source "$_find_dev"
543 # finds the major:minor of the block device backing the root filesystem.
544 find_root_block_device
() { find_block_device
/; }
546 # for_each_host_dev_fs <func>
547 # Execute "<func> <dev> <filesystem>" for every "<dev> <fs>" pair found
548 # in ${host_fs_types[@]}
549 for_each_host_dev_fs
() {
554 [[ "${#host_fs_types[@]}" ]] ||
return 2
556 for _dev
in "${!host_fs_types[@]}"; do
557 $_func "$_dev" "${host_fs_types[$_dev]}" && _ret
=0
563 printf "%s\n" "${host_fs_types[@]}"
566 # Walk all the slave relationships for a given block device.
567 # Stop when our helper function returns success
568 # $1 = function to call on every found block device
569 # $2 = block device in major:minor format
570 check_block_and_slaves
() {
572 [[ -b /dev
/block
/$2 ]] ||
return 1 # Not a block device? So sorry.
573 if ! lvm_internal_dev
"$2"; then "$1" "$2" && return; fi
574 check_vol_slaves
"$@" && return 0
575 if [[ -f /sys
/dev
/block
/$2/..
/dev
]] && [[ /sys
/dev
/block
/$2/..
/subsystem
-ef /sys
/class
/block
]]; then
576 check_block_and_slaves
"$1" "$(< "/sys
/dev
/block
/$2/..
/dev
")" && return 0
578 for _x
in /sys
/dev
/block
/"$2"/slaves
/*; do
579 [[ -f $_x/dev
]] ||
continue
580 [[ $_x/subsystem
-ef /sys
/class
/block
]] ||
continue
581 check_block_and_slaves
"$1" "$(< "$_x/dev
")" && return 0
586 check_block_and_slaves_all
() {
588 [[ -b /dev
/block
/$2 ]] ||
return 1 # Not a block device? So sorry.
589 if ! lvm_internal_dev
"$2" && "$1" "$2"; then
592 check_vol_slaves_all
"$@" && return 0
593 if [[ -f /sys
/dev
/block
/$2/..
/dev
]] && [[ /sys
/dev
/block
/$2/..
/subsystem
-ef /sys
/class
/block
]]; then
594 check_block_and_slaves_all
"$1" "$(< "/sys
/dev
/block
/$2/..
/dev
")" && _ret
=0
596 for _x
in /sys
/dev
/block
/"$2"/slaves
/*; do
597 [[ -f $_x/dev
]] ||
continue
598 [[ $_x/subsystem
-ef /sys
/class
/block
]] ||
continue
599 check_block_and_slaves_all
"$1" "$(< "$_x/dev
")" && _ret
=0
603 # for_each_host_dev_and_slaves <func>
604 # Execute "<func> <dev>" for every "<dev>" found
605 # in ${host_devs[@]} and their slaves
606 for_each_host_dev_and_slaves_all
() {
611 [[ "${host_devs[*]}" ]] ||
return 2
613 for _dev
in "${host_devs[@]}"; do
614 [[ -b $_dev ]] ||
continue
615 if check_block_and_slaves_all
"$_func" "$(get_maj_min "$_dev")"; then
622 for_each_host_dev_and_slaves
() {
626 [[ "${host_devs[*]}" ]] ||
return 2
628 for _dev
in "${host_devs[@]}"; do
629 [[ -b $_dev ]] ||
continue
630 check_block_and_slaves
"$_func" "$(get_maj_min "$_dev")" && return 0
635 # /sys/dev/block/major:minor is symbol link to real hardware device
636 # go downstream $(realpath /sys/dev/block/major:minor) to detect driver
637 get_blockdev_drv_through_sys
() {
641 _path
=$
(realpath
"$1")
643 if [[ -L "$_path"/driver
/module
]]; then
644 _mod
=$
(realpath
"$_path"/driver
/module
)
645 _mod
=$
(basename "$_mod")
646 _block_mods
="$_block_mods $_mod"
648 _path
=$
(dirname "$_path")
649 if [[ $_path == '/sys/devices' ]] ||
[[ $_path == '/' ]]; then
656 # ugly workaround for the lvm design
657 # There is no volume group device,
658 # so, there are no slave devices for volume groups.
659 # Logical volumes only have the slave devices they really live on,
660 # but you cannot create the logical volume without the volume group.
661 # And the volume group might be bigger than the devices the LV needs.
663 local _vg _pv _dm _majmin
665 _dm
=/sys
/dev
/block
/$_majmin/dm
666 [[ -f $_dm/uuid
&& $
(< "$_dm"/uuid
) =~ LVM-
* ]] ||
return 1
667 _vg
=$
(dmsetup splitname
--noheadings -o vg_name
"$(< "$_dm/name
")")
669 _vg
="${_vg//[[:space:]]/}"
671 for _pv
in $
(lvm vgs
--noheadings -o pv_name
"$_vg" 2> /dev
/null
); do
672 check_block_and_slaves
"$1" "$(get_maj_min "$_pv")" && return 0
678 check_vol_slaves_all
() {
679 local _vg _pv _majmin
681 _dm
="/sys/dev/block/$_majmin/dm"
682 [[ -f $_dm/uuid
&& $
(< "$_dm"/uuid
) =~ LVM-
* ]] ||
return 1
683 _vg
=$
(dmsetup splitname
--noheadings -o vg_name
"$(< "$_dm/name
")")
685 _vg
="${_vg//[[:space:]]/}"
687 # when filter/global_filter is set, lvm may be failed
688 if ! lvm lvs
--noheadings -o vg_name
"$_vg" 2> /dev
/null
1> /dev
/null
; then
692 for _pv
in $
(lvm vgs
--noheadings -o pv_name
"$_vg" 2> /dev
/null
); do
693 check_block_and_slaves_all
"$1" "$(get_maj_min "$_pv")"
700 # fs_get_option <filesystem options> <search for option>
701 # search for a specific option in a bunch of filesystem options
702 # and return the value
708 # shellcheck disable=SC2086
711 while [ $# -gt 0 ]; do
714 echo "${1#"${_option}"=}"
722 check_kernel_config
() {
723 local _config_opt
="$1"
725 [[ -f $dracutsysrootdir/boot
/config-
$kernel ]] \
726 && _config_file
="/boot/config-$kernel"
727 [[ -f $dracutsysrootdir/lib
/modules
/$kernel/config
]] \
728 && _config_file
="/lib/modules/$kernel/config"
730 # no kernel config file, so return true
731 [[ $_config_file ]] ||
return 0
733 grep -q -F "${_config_opt}=" "$dracutsysrootdir$_config_file" && return 0
737 # 0 if the kernel module is either built-in or available
738 # 1 if the kernel module is not enabled
739 check_kernel_module
() {
740 modprobe
-d "$dracutsysrootdir" -S "$kernel" --dry-run "$1" &> /dev
/null ||
return 1
744 # Only two values are returned: AMD or Intel
746 if grep -qE AMD
/proc
/cpuinfo
; then
749 if grep -qE Intel
/proc
/cpuinfo
; then
755 # Get the hosts' ucode file based on the /proc/cpuinfo
760 family
=$
(grep -E "cpu family" /proc
/cpuinfo |
head -1 |
sed "s/.*:\ //")
761 model
=$
(grep -E "model" /proc
/cpuinfo |
grep -v name |
head -1 |
sed "s/.*:\ //")
762 stepping
=$
(grep -E "stepping" /proc
/cpuinfo |
head -1 |
sed "s/.*:\ //")
764 if [[ "$(get_cpu_vendor)" == "AMD" ]]; then
765 if [[ $family -ge 21 ]]; then
766 printf "microcode_amd_fam%xh.bin" "$family"
768 printf "microcode_amd.bin"
771 if [[ "$(get_cpu_vendor)" == "Intel" ]]; then
772 # The /proc/cpuinfo are in decimal.
773 printf "%02x-%02x-%02x" "${family}" "${model}" "${stepping}"
777 # Not every device in /dev/mapper should be examined.
778 # If it is an LVM device, touch only devices which have /dev/VG/LV symlink.
780 local dev_dm_dir
=/sys
/dev
/block
/$1/dm
781 [[ ! -f $dev_dm_dir/uuid || $
(< "$dev_dm_dir"/uuid
) != LVM-
* ]] && return 1 # Not an LVM device
782 local DM_VG_NAME DM_LV_NAME DM_LV_LAYER
783 eval "$(dmsetup splitname --nameprefixes --noheadings --rows "$
(< "$dev_dm_dir"/name
)" 2> /dev/null)"
784 [[ ${DM_VG_NAME} ]] && [[ ${DM_LV_NAME} ]] ||
return 0 # Better skip this!
785 [[ ${DM_LV_LAYER} ]] || [[ ! -L /dev/${DM_VG_NAME}/${DM_LV_NAME} ]]
790 btrfs device usage
"$_mp" \
791 |
while read -r _dev _
; do
792 str_starts
"$_dev" "/" ||
continue
794 printf -- "%s\n" "$_dev"
800 zpool list
-H -v -P "${_mp%%/*}" |
awk -F$
'\t' '$2 ~ /^\// {print $2}' \
801 |
while read -r _dev
; do
806 iface_for_remote_addr
() {
807 # shellcheck disable=SC2046
808 set -- $
(ip
-o route get to
"$1")
809 while [ $# -gt 0 ]; do
820 local_addr_for_remote_addr
() {
821 # shellcheck disable=SC2046
822 set -- $
(ip
-o route get to
"$1")
823 while [ $# -gt 0 ]; do
838 # quote periods in IPv4 address
841 |
sed -n 's%^.* '"$qtd"' peer \([0-9a-f.:]\{1,\}\(/[0-9]*\)\?\).*$%\1%p'
848 # quote periods in IPv4 address
850 ip
-o addr show |
sed -n 's,^.* '"$qtd"'/\([0-9]*\) .*$,\1,p'
853 gateway_for_iface
() {
854 local ifname
=$1 addr
=$2
861 ip
-o -$proto route show \
862 |
sed -n "s/^default via \([0-9a-z.:]\{1,\}\) dev $ifname .*\$/\1/p"
865 # This works only for ifcfg-style network configuration!
866 bootproto_for_iface
() {
870 # follow ifcfg settings for boot protocol
871 for dir
in network-scripts network
; do
872 [ -f "/etc/sysconfig/$dir/ifcfg-$ifname" ] && {
873 sed -n "s/BOOTPROTO=[\"']\?\([[:alnum:]]\{1,\}\)[\"']\?.*\$/\1/p" \
874 "/etc/sysconfig/$dir/ifcfg-$ifname"
880 is_unbracketed_ipv6_address
() {
881 strglob
"$1" '*:*' && ! strglob
"$1" '\[*:*\]'
884 # Create an ip= string to set up networking such that the given
885 # remote address can be reached
886 ip_params_for_remote_addr
() {
888 local ifname local_addr peer netmask gateway ifmac
890 [[ $remote_addr ]] ||
return 1
891 ifname
=$
(iface_for_remote_addr
"$remote_addr")
893 berror
"failed to determine interface to connect to $remote_addr"
897 # ifname clause to bind the interface name to a MAC address
898 if [ -d "/sys/class/net/$ifname/bonding" ]; then
899 dinfo
"Found bonded interface '${ifname}'. Make sure to provide an appropriate 'bond=' cmdline."
900 elif [ -e "/sys/class/net/$ifname/address" ]; then
901 ifmac
=$
(cat "/sys/class/net/$ifname/address")
902 [[ $ifmac ]] && printf 'ifname=%s:%s ' "${ifname}" "${ifmac}"
905 bootproto
=$
(bootproto_for_iface
"$ifname")
907 dhcp | dhcp6 | auto6
) ;;
915 derror
"bootproto \"$bootproto\" is unsupported by dracut, trying static configuration"
919 if [[ $bootproto ]]; then
920 printf 'ip=%s:%s ' "${ifname}" "${bootproto}"
922 local_addr
=$
(local_addr_for_remote_addr
"$remote_addr")
923 [[ $local_addr ]] ||
{
924 berror
"failed to determine local address to connect to $remote_addr"
927 peer
=$
(peer_for_addr
"$local_addr")
928 # Set peer or netmask, but not both
929 [[ $peer ]] || netmask
=$
(netmask_for_addr
"$local_addr")
930 gateway
=$
(gateway_for_iface
"$ifname" "$local_addr")
931 # Quote IPv6 addresses with brackets
932 is_unbracketed_ipv6_address
"$local_addr" && local_addr
="[$local_addr]"
933 is_unbracketed_ipv6_address
"$peer" && peer
="[$peer]"
934 is_unbracketed_ipv6_address
"$gateway" && gateway
="[$gateway]"
935 printf 'ip=%s:%s:%s:%s::%s:none ' \
936 "${local_addr}" "${peer}" "${gateway}" "${netmask}" "${ifname}"
941 # block_is_nbd <maj:min>
942 # Check whether $1 is an nbd device
944 [[ -b /dev
/block
/$1 && $1 == 43:* ]]
947 # block_is_iscsi <maj:min>
948 # Check whether $1 is an iSCSI device
952 [[ -L "/sys/dev/block/$_dev" ]] ||
return
953 _dir
="$(readlink -f "/sys
/dev
/block
/$_dev")" ||
return
954 until [[ -d "$_dir/sys" ||
-d "$_dir/iscsi_session" ]]; do
957 [[ -d "$_dir/iscsi_session" ]]
960 # block_is_fcoe <maj:min>
961 # Check whether $1 is an FCoE device
962 # Will not work for HBAs that hide the ethernet aspect
963 # completely and present a pure FC device
967 [[ -L "/sys/dev/block/$_dev" ]] ||
return
968 _dir
="$(readlink -f "/sys
/dev
/block
/$_dev")"
969 until [[ -d "$_dir/sys" ]]; do
971 if [[ -d "$_dir/subsystem" ]]; then
972 subsystem
=$
(basename "$(readlink "$_dir"/subsystem)")
973 [[ $subsystem == "fcoe" ]] && return 0
979 # block_is_netdevice <maj:min>
980 # Check whether $1 is a net device
981 block_is_netdevice
() {
982 block_is_nbd
"$1" || block_is_iscsi
"$1" || block_is_fcoe
"$1"
985 # convert the driver name given by udevadm to the corresponding kernel module name
988 while read -r dev_driver
; do
989 case "$dev_driver" in
1000 # get the corresponding kernel modules of a /sys/class/*/* or/dev/* device
1005 dev_attr_walk
=$
(udevadm info
-a "$1")
1006 dev_drivers
=$
(echo "$dev_attr_walk" \
1007 |
sed -n 's/\s*DRIVERS=="\(\S\+\)"/\1/p' \
1010 # also return modalias info from sysfs paths parsed by udevadm
1011 dev_paths
=$
(echo "$dev_attr_walk" |
sed -n 's/.*\(\/devices\/.*\)'\'':/\1/p')
1013 for dev_path
in $dev_paths; do
1014 local modalias_file
="/sys$dev_path/modalias"
1015 if [ -e "$modalias_file" ]; then
1016 dev_drivers
="$(printf "%s
\n%s
" "$dev_drivers" "$
(cat "$modalias_file")")"
1020 # if no kernel modules found and device is in a virtual subsystem, follow symlinks
1021 if [[ -z $dev_drivers && $
(udevadm info
-q path
"$1") == "/devices/virtual"* ]]; then
1023 local dev_vsubsystem
1025 dev_vkernel
=$
(echo "$dev_attr_walk" |
sed -n 's/\s*KERNELS=="\(\S\+\)"/\1/p' |
tail -1)
1026 dev_vsubsystem
=$
(echo "$dev_attr_walk" |
sed -n 's/\s*SUBSYSTEMS=="\(\S\+\)"/\1/p' |
tail -1)
1027 dev_vpath
="/sys/devices/virtual/$dev_vsubsystem/$dev_vkernel"
1028 if [[ -n $dev_vkernel && -n $dev_vsubsystem && -d $dev_vpath ]]; then
1031 dev_links
=$
(find "$dev_vpath" -maxdepth 1 -type l
! -name "subsystem" -exec readlink
{} \
;)
1032 for dev_link
in $dev_links; do
1033 [[ -n $dev_drivers && ${dev_drivers: -1} != $
'\n' ]] && dev_drivers
+=$
'\n'
1034 dev_drivers
+=$
(udevadm info
-a "$dev_vpath/$dev_link" \
1035 |
sed -n 's/\s*DRIVERS=="\(\S\+\)"/\1/p' \
1037 |
grep -v -e pcieport
)
1044 # Check if file is in PE format
1046 if [[ $# -eq 1 ]]; then
1048 magic
=$
(objdump
-p "$1" \
1049 | gawk
'{if ($1 == "Magic"){print strtonum("0x"$2)}}')
1050 magic
=$
(printf "0x%x" "$magic")
1051 # 0x10b (PE32), 0x20b (PE32+)
1052 [[ $magic == 0x20b ||
$magic == 0x10b ]] && return 0
1057 # Get specific data from the PE header
1058 pe_get_header_data
() {
1060 [[ $# -ne "2" ]] && return 1
1061 [[ $
(pe_file_format
"$1") -eq 1 ]] && return 1
1062 data_header
=$
(objdump
-p "$1" \
1063 |
awk -v data
="$2" '{if ($1 == data){print $2}}')
1067 # Get the SectionAlignment data from the PE header
1068 pe_get_section_align
() {
1070 [[ $# -ne "1" ]] && return 1
1071 align_hex
=$
(pe_get_header_data
"$1" "SectionAlignment")
1072 [[ $?
-eq 1 ]] && return 1
1073 echo "$((16#$align_hex))"
1076 # Get the ImageBase data from the PE header
1077 pe_get_image_base
() {
1079 [[ $# -ne "1" ]] && return 1
1080 base_image
=$
(pe_get_header_data
"$1" "ImageBase")
1081 [[ $?
-eq 1 ]] && return 1
1082 echo "$((16#$base_image))"