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" ]]
29 # Generic substring function. If $2 is in $1, return 0.
30 strstr
() { [[ $1 = *"$2"* ]]; }
31 # Generic glob matching function. If glob pattern $2 matches anywhere in $1, OK
32 strglobin
() { [[ $1 = *$2* ]]; }
33 # Generic glob matching function. If glob pattern $2 matches all of $1, OK
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.
43 if [[ -z ${1##/*} ]]; then
44 if [[ -x $1 ]] ||
{ [[ "$1" == *.so
* ]] && ldd
"$1" &>/dev
/null
; }; then
55 ldconfig
-pN 2>/dev
/null |
grep -E -v '/(lib|lib64|usr/lib|usr/lib64)/[^/]*$' |
sed -n 's,.* => \(.*\)/.*,\1,p' |
sort |
uniq
58 # Version comparision function. Assumes Linux style version scheme.
60 # $2 = comparision op (gt, ge, eq, le, lt, ne)
63 local _n1
=(${1//./ }) _op
=$2 _n2
=(${3//./ }) _i _res
67 if [[ ! ${_n1[_i]}${_n2[_i]} ]]; then _res
=0
68 elif ((${_n1[_i]:-0} > ${_n2[_i]:-0})); then _res
=1
69 elif ((${_n1[_i]:-0} < ${_n2[_i]:-0})); then _res
=2
85 # Create all subdirectories for given path without creating the last element.
88 [[ -e ${1%/*} ]] || mkdir
-m 0755 -p -- "${1%/*}"
91 # Function prints global variables in format name=value line by line.
92 # $@ = list of global variables' name
98 eval printf -v _value
"%s" \""\$$_var"\"
99 [[ ${_value} ]] && printf '%s="%s"\n' "$_var" "$_value"
103 # normalize_path <path>
104 # Prints the normalized path, where it removes any duplicated
105 # and trailing slashes.
107 # $ normalize_path ///test/test//
111 set -- "${1//+(\/)//}"
113 printf "%s\n" "${1%/}"
116 # convert_abs_rel <from> <to>
117 # Prints the relative path, when creating a symlink to <to> from <from>.
119 # $ convert_abs_rel /usr/bin/test /bin/test-2
121 # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
123 local __current __absolute __abssize __cursize __newpath
126 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
128 # corner case #1 - self looping link
129 [[ "$1" == "$2" ]] && { printf "%s\n" "${1##*/}"; return; }
131 # corner case #2 - own dir link
132 [[ "${1%/*}" == "$2" ]] && { printf ".\n"; return; }
134 IFS
="/" __current
=($1)
135 IFS
="/" __absolute
=($2)
137 __abssize
=${#__absolute[@]}
138 __cursize
=${#__current[@]}
140 while [[ "${__absolute[__level]}" == "${__current[__level]}" ]]
143 if (( __level
> __abssize || __level
> __cursize
))
149 for ((__i
= __level
; __i
< __cursize-1
; __i
++))
153 __newpath
=$__newpath"/"
155 __newpath
=$__newpath".."
158 for ((__i
= __level
; __i
< __abssize
; __i
++))
160 if [[ -n $__newpath ]]
162 __newpath
=$__newpath"/"
164 __newpath
=$__newpath${__absolute[__i]}
167 printf "%s\n" "$__newpath"
171 # get_fs_env <device>
172 # Get and the ID_FS_TYPE variable from udev for a device.
174 # $ get_fs_env /dev/sda2
182 ID_FS_TYPE
=$
(blkid
-u filesystem
-o export -- "$1" \
183 |
while read line ||
[ -n "$line" ]; do
184 if [[ "$line" == TYPE\
=* ]]; then
185 printf "%s" "${line#TYPE=}";
189 if [[ $ID_FS_TYPE ]]; then
190 printf "%s" "$ID_FS_TYPE"
196 # get_maj_min <device>
197 # Prints the major and minor of a device node.
199 # $ get_maj_min /dev/sda2
202 local _maj _min _majmin
203 _majmin
="$(stat -L -c '%t:%T' "$1" 2>/dev/null)"
204 printf "%s" "$((0x${_majmin%:*})):$((0x${_majmin#*:}))"
208 # get_devpath_block <device>
209 # get the DEVPATH in /sys of a block device
210 get_devpath_block
() {
212 _majmin
=$
(get_maj_min
"$1")
214 for _i
in /sys
/block
/*/dev
/sys
/block
/*/*/dev
; do
215 [[ -e "$_i" ]] ||
continue
216 if [[ "$_majmin" == "$(<"$_i")" ]]; then
217 printf "%s" "${_i%/dev}"
224 # get a persistent path from a device
225 get_persistent_dev
() {
226 local i _tmp _dev _pol
228 _dev
=$
(get_maj_min
"$1")
229 [ -z "$_dev" ] && return
231 if [[ -n "$persistent_policy" ]]; then
232 _pol
="/dev/disk/${persistent_policy}/*"
240 /dev
/disk
/by-uuid
/* \
241 /dev
/disk
/by-label
/* \
242 /dev
/disk
/by-partuuid
/* \
243 /dev
/disk
/by-partlabel
/* \
245 /dev
/disk
/by-path
/* \
247 [[ -e "$i" ]] ||
continue
248 [[ $i == /dev
/mapper
/control
]] && continue
249 [[ $i == /dev
/mapper
/mpath
* ]] && continue
250 _tmp
=$
(get_maj_min
"$i")
251 if [ "$_tmp" = "$_dev" ]; then
259 expand_persistent_dev
() {
264 _dev
="/dev/disk/by-label/${_dev#LABEL=}"
269 _dev
="/dev/disk/by-uuid/${_dev}"
272 _dev
="${_dev#PARTUUID=}"
274 _dev
="/dev/disk/by-partuuid/${_dev}"
277 _dev
="/dev/disk/by-partlabel/${_dev#PARTLABEL=}"
283 shorten_persistent_dev
() {
287 printf "%s" "UUID=${_dev##*/}";;
288 /dev
/disk
/by-label
/*)
289 printf "%s" "LABEL=${_dev##*/}";;
290 /dev
/disk
/by-partuuid
/*)
291 printf "%s" "PARTUUID=${_dev##*/}";;
292 /dev
/disk
/by-partlabel
/*)
293 printf "%s" "PARTLABEL=${_dev##*/}";;
295 printf "%s" "$_dev";;
299 # find_block_device <mountpoint>
300 # Prints the major and minor number of the block device
301 # for a given mountpoint.
302 # Unless $use_fstab is set to "yes" the functions
303 # uses /proc/self/mountinfo as the primary source of the
304 # information and only falls back to /etc/fstab, if the mountpoint
305 # is not found there.
307 # $ find_block_device /usr
309 find_block_device
() {
310 local _dev _majmin _find_mpt
312 if [[ $use_fstab != yes ]]; then
313 [[ -d $_find_mpt/.
]]
314 findmnt
-e -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" |
{ \
315 while read _majmin _dev ||
[ -n "$_dev" ]; do
316 if [[ -b $_dev ]]; then
317 if ! [[ $_majmin ]] ||
[[ $_majmin == 0:* ]]; then
318 _majmin
=$
(get_maj_min
$_dev)
320 if [[ $_majmin ]]; then
321 printf "%s\n" "$_majmin"
323 printf "%s\n" "$_dev"
327 if [[ $_dev = *:* ]]; then
328 printf "%s\n" "$_dev"
331 done; return 1; } && return 0
333 # fall back to /etc/fstab
335 findmnt
-e --fstab -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" |
{ \
336 while read _majmin _dev ||
[ -n "$_dev" ]; do
337 if ! [[ $_dev ]]; then
341 if [[ -b $_dev ]]; then
342 [[ $_majmin ]] || _majmin
=$
(get_maj_min
$_dev)
343 if [[ $_majmin ]]; then
344 printf "%s\n" "$_majmin"
346 printf "%s\n" "$_dev"
350 if [[ $_dev = *:* ]]; then
351 printf "%s\n" "$_dev"
354 done; return 1; } && return 0
359 # find_mp_fstype <mountpoint>
360 # Echo the filesystem type for a given mountpoint.
361 # /proc/self/mountinfo is taken as the primary source of information
362 # and /etc/fstab is used as a fallback.
363 # No newline is appended!
365 # $ find_mp_fstype /;echo
370 if [[ $use_fstab != yes ]]; then
371 findmnt
-e -v -n -o 'FSTYPE' --target "$1" |
{ \
372 while read _fs ||
[ -n "$_fs" ]; do
373 [[ $_fs ]] ||
continue
374 [[ $_fs = "autofs" ]] && continue
377 done; return 1; } && return 0
380 findmnt
--fstab -e -v -n -o 'FSTYPE' --target "$1" |
{ \
381 while read _fs ||
[ -n "$_fs" ]; do
382 [[ $_fs ]] ||
continue
383 [[ $_fs = "autofs" ]] && continue
386 done; return 1; } && return 0
391 # find_dev_fstype <device>
392 # Echo the filesystem type for a given device.
393 # /proc/self/mountinfo is taken as the primary source of information
394 # and /etc/fstab is used as a fallback.
395 # No newline is appended!
397 # $ find_dev_fstype /dev/sda2;echo
402 if ! [[ "$_find_dev" = /dev
* ]]; then
403 [[ -b "/dev/block/$_find_dev" ]] && _find_dev
="/dev/block/$_find_dev"
406 if [[ $use_fstab != yes ]]; then
407 findmnt
-e -v -n -o 'FSTYPE' --source "$_find_dev" |
{ \
408 while read _fs ||
[ -n "$_fs" ]; do
409 [[ $_fs ]] ||
continue
410 [[ $_fs = "autofs" ]] && continue
413 done; return 1; } && return 0
416 findmnt
--fstab -e -v -n -o 'FSTYPE' --source "$_find_dev" |
{ \
417 while read _fs ||
[ -n "$_fs" ]; do
418 [[ $_fs ]] ||
continue
419 [[ $_fs = "autofs" ]] && continue
422 done; return 1; } && return 0
427 # find_mp_fsopts <mountpoint>
428 # Echo the filesystem options for a given mountpoint.
429 # /proc/self/mountinfo is taken as the primary source of information
430 # and /etc/fstab is used as a fallback.
431 # No newline is appended!
433 # $ find_mp_fsopts /;echo
434 # rw,relatime,discard,data=ordered
436 if [[ $use_fstab != yes ]]; then
437 findmnt
-e -v -n -o 'OPTIONS' --target "$1" 2>/dev
/null
&& return 0
440 findmnt
--fstab -e -v -n -o 'OPTIONS' --target "$1"
443 # find_dev_fsopts <device>
444 # Echo the filesystem options for a given device.
445 # /proc/self/mountinfo is taken as the primary source of information
446 # and /etc/fstab is used as a fallback.
448 # $ find_dev_fsopts /dev/sda2
449 # rw,relatime,discard,data=ordered
451 local _find_dev _opts
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 'OPTIONS' --source "$_find_dev" 2>/dev
/null
&& return 0
461 findmnt
--fstab -e -v -n -o 'OPTIONS' --source "$_find_dev"
465 # finds the major:minor of the block device backing the root filesystem.
466 find_root_block_device
() { find_block_device
/; }
468 # for_each_host_dev_fs <func>
469 # Execute "<func> <dev> <filesystem>" for every "<dev> <fs>" pair found
470 # in ${host_fs_types[@]}
471 for_each_host_dev_fs
()
477 [[ "${#host_fs_types[@]}" ]] ||
return 2
480 for _dev
in "${!host_fs_types[@]}"; do
481 $_func "$_dev" "${host_fs_types[$_dev]}" && _ret
=0
488 printf "%s\n" "${host_fs_types[@]}"
491 # Walk all the slave relationships for a given block device.
492 # Stop when our helper function returns success
493 # $1 = function to call on every found block device
494 # $2 = block device in major:minor format
495 check_block_and_slaves
() {
497 [[ -b /dev
/block
/$2 ]] ||
return 1 # Not a block device? So sorry.
498 if ! lvm_internal_dev
$2; then "$1" $2 && return; fi
499 check_vol_slaves
"$@" && return 0
500 if [[ -f /sys
/dev
/block
/$2/..
/dev
]] && [[ /sys
/dev
/block
/$2/..
/subsystem
-ef /sys
/class
/block
]]; then
501 check_block_and_slaves
$1 $
(<"/sys/dev/block/$2/../dev") && return 0
503 [[ -d /sys
/dev
/block
/$2/slaves
]] ||
return 1
504 for _x
in /sys
/dev
/block
/$2/slaves
/*; do
505 [[ -f $_x/dev
]] ||
continue
506 [[ $_x/subsystem
-ef /sys
/class
/block
]] ||
continue
507 check_block_and_slaves
$1 $
(<"$_x/dev") && return 0
512 check_block_and_slaves_all
() {
514 [[ -b /dev
/block
/$2 ]] ||
return 1 # Not a block device? So sorry.
515 if ! lvm_internal_dev
$2 && "$1" $2; then
518 check_vol_slaves_all
"$@" && return 0
519 if [[ -f /sys
/dev
/block
/$2/..
/dev
]] && [[ /sys
/dev
/block
/$2/..
/subsystem
-ef /sys
/class
/block
]]; then
520 check_block_and_slaves_all
$1 $
(<"/sys/dev/block/$2/../dev") && _ret
=0
522 [[ -d /sys
/dev
/block
/$2/slaves
]] ||
return 1
523 for _x
in /sys
/dev
/block
/$2/slaves
/*; do
524 [[ -f $_x/dev
]] ||
continue
525 [[ $_x/subsystem
-ef /sys
/class
/block
]] ||
continue
526 check_block_and_slaves_all
$1 $
(<"$_x/dev") && _ret
=0
530 # for_each_host_dev_and_slaves <func>
531 # Execute "<func> <dev>" for every "<dev>" found
532 # in ${host_devs[@]} and their slaves
533 for_each_host_dev_and_slaves_all
()
539 [[ "${host_devs[@]}" ]] ||
return 2
541 for _dev
in "${host_devs[@]}"; do
542 [[ -b "$_dev" ]] ||
continue
543 if check_block_and_slaves_all
$_func $
(get_maj_min
$_dev); then
550 for_each_host_dev_and_slaves
()
555 [[ "${host_devs[@]}" ]] ||
return 2
557 for _dev
in "${host_devs[@]}"; do
558 [[ -b "$_dev" ]] ||
continue
559 check_block_and_slaves
$_func $
(get_maj_min
$_dev) && return 0
564 # ugly workaround for the lvm design
565 # There is no volume group device,
566 # so, there are no slave devices for volume groups.
567 # Logical volumes only have the slave devices they really live on,
568 # but you cannot create the logical volume without the volume group.
569 # And the volume group might be bigger than the devices the LV needs.
571 local _lv _vg _pv _dm _majmin
573 _lv
="/dev/block/$_majmin"
574 _dm
=/sys
/dev
/block
/$_majmin/dm
575 [[ -f $_dm/uuid
&& $
(<$_dm/uuid
) =~ LVM-
* ]] ||
return 1
576 _vg
=$
(dmsetup splitname
--noheadings -o vg_name $
(<"$_dm/name") )
578 _vg
="${_vg//[[:space:]]/}"
580 for _pv
in $
(lvm vgs
--noheadings -o pv_name
"$_vg" 2>/dev
/null
)
582 check_block_and_slaves
$1 $
(get_maj_min
$_pv) && return 0
588 check_vol_slaves_all
() {
589 local _lv _vg _pv _majmin
591 _lv
="/dev/block/$_majmin"
592 _dm
="/sys/dev/block/$_majmin/dm"
593 [[ -f $_dm/uuid
&& $
(<$_dm/uuid
) =~ LVM-
* ]] ||
return 1
594 _vg
=$
(dmsetup splitname
--noheadings -o vg_name $
(<"$_dm/name") )
596 _vg
="${_vg//[[:space:]]/}"
598 for _pv
in $
(lvm vgs
--noheadings -o pv_name
"$_vg" 2>/dev
/null
)
600 check_block_and_slaves_all
$1 $
(get_maj_min
$_pv)
609 # fs_get_option <filesystem options> <search for option>
610 # search for a specific option in a bunch of filesystem options
611 # and return the value
619 while [ $# -gt 0 ]; do
622 echo ${1#${_option}=}
629 check_kernel_config
()
631 local _config_opt
="$1"
633 [[ -f /boot
/config-
$kernel ]] \
634 && _config_file
="/boot/config-$kernel"
635 [[ -f /lib
/modules
/$kernel/config
]] \
636 && _config_file
="/lib/modules/$kernel/config"
638 # no kernel config file, so return true
639 [[ $_config_file ]] ||
return 0
641 grep -q -F "${_config_opt}=" "$_config_file" && return 0
647 # Only two values are returned: AMD or Intel
650 if grep -qE AMD
/proc
/cpuinfo
; then
653 if grep -qE Intel
/proc
/cpuinfo
; then
659 # Get the hosts' ucode file based on the /proc/cpuinfo
662 local family
=`grep -E "cpu family" /proc/cpuinfo | head -1 | sed s/.*:\ //`
663 local model
=`grep -E "model" /proc/cpuinfo |grep -v name | head -1 | sed s/.*:\ //`
664 local stepping
=`grep -E "stepping" /proc/cpuinfo | head -1 | sed s/.*:\ //`
666 if [[ "$(get_cpu_vendor)" == "AMD" ]]; then
667 if [[ $family -ge 21 ]]; then
668 printf "microcode_amd_fam%xh.bin" $family
670 printf "microcode_amd.bin"
673 if [[ "$(get_cpu_vendor)" == "Intel" ]]; then
674 # The /proc/cpuinfo are in decimal.
675 printf "%02x-%02x-%02x" ${family} ${model} ${stepping}
679 # Not every device in /dev/mapper should be examined.
680 # If it is an LVM device, touch only devices which have /dev/VG/LV symlink.
682 local dev_dm_dir
=/sys
/dev
/block
/$1/dm
683 [[ ! -f $dev_dm_dir/uuid || $
(<$dev_dm_dir/uuid
) != LVM-
* ]] && return 1 # Not an LVM device
684 local DM_VG_NAME DM_LV_NAME DM_LV_LAYER
685 eval $
(dmsetup splitname
--nameprefixes --noheadings --rows "$(<$dev_dm_dir/name)" 2>/dev
/null
)
686 [[ ${DM_VG_NAME} ]] && [[ ${DM_LV_NAME} ]] ||
return 0 # Better skip this!
687 [[ ${DM_LV_LAYER} ]] || [[ ! -L /dev/${DM_VG_NAME}/${DM_LV_NAME} ]]
692 btrfs device usage
"$_mp" \
693 |
while read _dev _rest
; do
694 str_starts
"$_dev" "/" ||
continue
696 printf -- "%s\n" "$_dev"