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"
170 if [[ "$(ln --help)" == *--relative* ]]; then
172 ln -sfnr "${initdir}/$1" "${initdir}/$2"
178 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
179 ln -sfn -- "$
(convert_abs_rel
"${_dest}" "${_source}")" "${initdir}/${_dest}"
183 # get_fs_env <device>
184 # Get and the ID_FS_TYPE variable from udev for a device.
186 # $ get_fs_env /dev/sda2
194 ID_FS_TYPE
=$
(blkid
-u filesystem
-o export -- "$1" \
195 |
while read line ||
[ -n "$line" ]; do
196 if [[ "$line" == TYPE\
=* ]]; then
197 printf "%s" "${line#TYPE=}";
201 if [[ $ID_FS_TYPE ]]; then
202 printf "%s" "$ID_FS_TYPE"
208 # get_maj_min <device>
209 # Prints the major and minor of a device node.
211 # $ get_maj_min /dev/sda2
214 local _maj _min _majmin
215 _majmin
="$(stat -L -c '%t:%T' "$1" 2>/dev/null)"
216 printf "%s" "$((0x${_majmin%:*})):$((0x${_majmin#*:}))"
220 # get_devpath_block <device>
221 # get the DEVPATH in /sys of a block device
222 get_devpath_block
() {
224 _majmin
=$
(get_maj_min
"$1")
226 for _i
in /sys
/block
/*/dev
/sys
/block
/*/*/dev
; do
227 [[ -e "$_i" ]] ||
continue
228 if [[ "$_majmin" == "$(<"$_i")" ]]; then
229 printf "%s" "${_i%/dev}"
236 # get a persistent path from a device
237 get_persistent_dev
() {
240 _dev
=$
(get_maj_min
"$1")
241 [ -z "$_dev" ] && return
245 /dev
/disk
/${persistent_policy:-by-uuid}/* \
246 /dev
/disk
/by-uuid
/* \
247 /dev
/disk
/by-label
/* \
248 /dev
/disk
/by-partuuid
/* \
249 /dev
/disk
/by-partlabel
/* \
251 /dev
/disk
/by-path
/* \
253 [[ -e "$i" ]] ||
continue
254 [[ $i == /dev
/mapper
/control
]] && continue
255 [[ $i == /dev
/mapper
/mpath
* ]] && continue
256 _tmp
=$
(get_maj_min
"$i")
257 if [ "$_tmp" = "$_dev" ]; then
265 expand_persistent_dev
() {
270 _dev
="/dev/disk/by-label/${_dev#LABEL=}"
275 _dev
="/dev/disk/by-uuid/${_dev}"
278 _dev
="${_dev#PARTUUID=}"
280 _dev
="/dev/disk/by-partuuid/${_dev}"
283 _dev
="/dev/disk/by-partlabel/${_dev#PARTLABEL=}"
289 shorten_persistent_dev
() {
293 printf "%s" "UUID=${_dev##*/}";;
294 /dev
/disk
/by-label
/*)
295 printf "%s" "LABEL=${_dev##*/}";;
296 /dev
/disk
/by-partuuid
/*)
297 printf "%s" "PARTUUID=${_dev##*/}";;
298 /dev
/disk
/by-partlabel
/*)
299 printf "%s" "PARTLABEL=${_dev##*/}";;
301 printf "%s" "$_dev";;
305 # find_block_device <mountpoint>
306 # Prints the major and minor number of the block device
307 # for a given mountpoint.
308 # Unless $use_fstab is set to "yes" the functions
309 # uses /proc/self/mountinfo as the primary source of the
310 # information and only falls back to /etc/fstab, if the mountpoint
311 # is not found there.
313 # $ find_block_device /usr
315 find_block_device
() {
316 local _dev _majmin _find_mpt
318 if [[ $use_fstab != yes ]]; then
319 [[ -d $_find_mpt/.
]]
320 findmnt
-e -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" |
{ \
321 while read _majmin _dev ||
[ -n "$_dev" ]; do
322 if [[ -b $_dev ]]; then
323 if ! [[ $_majmin ]] ||
[[ $_majmin == 0:* ]]; then
324 _majmin
=$
(get_maj_min
$_dev)
326 if [[ $_majmin ]]; then
327 printf "%s\n" "$_majmin"
329 printf "%s\n" "$_dev"
333 if [[ $_dev = *:* ]]; then
334 printf "%s\n" "$_dev"
337 done; return 1; } && return 0
339 # fall back to /etc/fstab
341 findmnt
-e --fstab -v -n -o 'MAJ:MIN,SOURCE' --target "$_find_mpt" |
{ \
342 while read _majmin _dev ||
[ -n "$_dev" ]; do
343 if ! [[ $_dev ]]; then
347 if [[ -b $_dev ]]; then
348 [[ $_majmin ]] || _majmin
=$
(get_maj_min
$_dev)
349 if [[ $_majmin ]]; then
350 printf "%s\n" "$_majmin"
352 printf "%s\n" "$_dev"
356 if [[ $_dev = *:* ]]; then
357 printf "%s\n" "$_dev"
360 done; return 1; } && return 0
365 # find_mp_fstype <mountpoint>
366 # Echo the filesystem type for a given mountpoint.
367 # /proc/self/mountinfo is taken as the primary source of information
368 # and /etc/fstab is used as a fallback.
369 # No newline is appended!
371 # $ find_mp_fstype /;echo
376 if [[ $use_fstab != yes ]]; then
377 findmnt
-e -v -n -o 'FSTYPE' --target "$1" |
{ \
378 while read _fs ||
[ -n "$_fs" ]; do
379 [[ $_fs ]] ||
continue
380 [[ $_fs = "autofs" ]] && continue
383 done; return 1; } && return 0
386 findmnt
--fstab -e -v -n -o 'FSTYPE' --target "$1" |
{ \
387 while read _fs ||
[ -n "$_fs" ]; do
388 [[ $_fs ]] ||
continue
389 [[ $_fs = "autofs" ]] && continue
392 done; return 1; } && return 0
397 # find_dev_fstype <device>
398 # Echo the filesystem type for a given device.
399 # /proc/self/mountinfo is taken as the primary source of information
400 # and /etc/fstab is used as a fallback.
401 # No newline is appended!
403 # $ find_dev_fstype /dev/sda2;echo
408 if ! [[ "$_find_dev" = /dev
* ]]; then
409 [[ -b "/dev/block/$_find_dev" ]] && _find_dev
="/dev/block/$_find_dev"
412 if [[ $use_fstab != yes ]]; then
413 findmnt
-e -v -n -o 'FSTYPE' --source "$_find_dev" |
{ \
414 while read _fs ||
[ -n "$_fs" ]; do
415 [[ $_fs ]] ||
continue
416 [[ $_fs = "autofs" ]] && continue
419 done; return 1; } && return 0
422 findmnt
--fstab -e -v -n -o 'FSTYPE' --source "$_find_dev" |
{ \
423 while read _fs ||
[ -n "$_fs" ]; do
424 [[ $_fs ]] ||
continue
425 [[ $_fs = "autofs" ]] && continue
428 done; return 1; } && return 0
433 # find_mp_fsopts <mountpoint>
434 # Echo the filesystem options for a given mountpoint.
435 # /proc/self/mountinfo is taken as the primary source of information
436 # and /etc/fstab is used as a fallback.
437 # No newline is appended!
439 # $ find_mp_fsopts /;echo
440 # rw,relatime,discard,data=ordered
442 if [[ $use_fstab != yes ]]; then
443 findmnt
-e -v -n -o 'OPTIONS' --target "$1" 2>/dev
/null
&& return 0
446 findmnt
--fstab -e -v -n -o 'OPTIONS' --target "$1"
449 # find_dev_fsopts <device>
450 # Echo the filesystem options for a given device.
451 # /proc/self/mountinfo is taken as the primary source of information
452 # and /etc/fstab is used as a fallback.
454 # $ find_dev_fsopts /dev/sda2
455 # rw,relatime,discard,data=ordered
457 local _find_dev _opts
459 if ! [[ "$_find_dev" = /dev
* ]]; then
460 [[ -b "/dev/block/$_find_dev" ]] && _find_dev
="/dev/block/$_find_dev"
463 if [[ $use_fstab != yes ]]; then
464 findmnt
-e -v -n -o 'OPTIONS' --source "$_find_dev" 2>/dev
/null
&& return 0
467 findmnt
--fstab -e -v -n -o 'OPTIONS' --source "$_find_dev"
471 # finds the major:minor of the block device backing the root filesystem.
472 find_root_block_device
() { find_block_device
/; }
474 # for_each_host_dev_fs <func>
475 # Execute "<func> <dev> <filesystem>" for every "<dev> <fs>" pair found
476 # in ${host_fs_types[@]}
477 for_each_host_dev_fs
()
483 [[ "${#host_fs_types[@]}" ]] ||
return 0
485 for _dev
in "${!host_fs_types[@]}"; do
486 $_func "$_dev" "${host_fs_types[$_dev]}" && _ret
=0
493 printf "%s\n" "${host_fs_types[@]}"
496 # Walk all the slave relationships for a given block device.
497 # Stop when our helper function returns success
498 # $1 = function to call on every found block device
499 # $2 = block device in major:minor format
500 check_block_and_slaves
() {
502 [[ -b /dev
/block
/$2 ]] ||
return 1 # Not a block device? So sorry.
503 if ! lvm_internal_dev
$2; then "$1" $2 && return; fi
504 check_vol_slaves
"$@" && return 0
505 if [[ -f /sys
/dev
/block
/$2/..
/dev
]]; then
506 check_block_and_slaves
$1 $
(<"/sys/dev/block/$2/../dev") && return 0
508 [[ -d /sys
/dev
/block
/$2/slaves
]] ||
return 1
509 for _x
in /sys
/dev
/block
/$2/slaves
/*/dev
; do
510 [[ -f $_x ]] ||
continue
511 check_block_and_slaves
$1 $
(<"$_x") && return 0
516 check_block_and_slaves_all
() {
518 [[ -b /dev
/block
/$2 ]] ||
return 1 # Not a block device? So sorry.
519 if ! lvm_internal_dev
$2 && "$1" $2; then
522 check_vol_slaves
"$@" && return 0
523 if [[ -f /sys
/dev
/block
/$2/..
/dev
]]; then
524 check_block_and_slaves_all
$1 $
(<"/sys/dev/block/$2/../dev") && _ret
=0
526 [[ -d /sys
/dev
/block
/$2/slaves
]] ||
return 1
527 for _x
in /sys
/dev
/block
/$2/slaves
/*/dev
; do
528 [[ -f $_x ]] ||
continue
529 check_block_and_slaves_all
$1 $
(<"$_x") && _ret
=0
533 # for_each_host_dev_and_slaves <func>
534 # Execute "<func> <dev>" for every "<dev>" found
535 # in ${host_devs[@]} and their slaves
536 for_each_host_dev_and_slaves_all
()
542 [[ "${host_devs[@]}" ]] ||
return 0
544 for _dev
in "${host_devs[@]}"; do
545 [[ -b "$_dev" ]] ||
continue
546 if check_block_and_slaves_all
$_func $
(get_maj_min
$_dev); then
553 for_each_host_dev_and_slaves
()
558 [[ "${host_devs[@]}" ]] ||
return 0
560 for _dev
in "${host_devs[@]}"; do
561 [[ -b "$_dev" ]] ||
continue
562 check_block_and_slaves
$_func $
(get_maj_min
$_dev) && return 0
567 # ugly workaround for the lvm design
568 # There is no volume group device,
569 # so, there are no slave devices for volume groups.
570 # Logical volumes only have the slave devices they really live on,
571 # but you cannot create the logical volume without the volume group.
572 # And the volume group might be bigger than the devices the LV needs.
574 local _lv _vg _pv _dm
575 for i
in /dev
/mapper
/*; do
576 [[ $i == /dev
/mapper
/control
]] && continue
577 _lv
=$
(get_maj_min
$i)
578 _dm
=/sys
/dev
/block
/$_lv/dm
579 [[ -f $_dm/uuid
&& $
(<$_dm/uuid
) =~ LVM-
* ]] ||
continue
580 if [[ $_lv = $2 ]]; then
581 _vg
=$
(lvm lvs
--noheadings -o vg_name
$i 2>/dev
/null
)
583 _vg
=$
(printf "%s\n" "$_vg")
585 for _pv
in $
(lvm vgs
--noheadings -o pv_name
"$_vg" 2>/dev
/null
)
587 check_block_and_slaves
$1 $
(get_maj_min
$_pv) && return 0
595 # fs_get_option <filesystem options> <search for option>
596 # search for a specific option in a bunch of filesystem options
597 # and return the value
605 while [ $# -gt 0 ]; do
608 echo ${1#${_option}=}
615 check_kernel_config
()
617 local _config_opt
="$1"
619 [[ -f /boot
/config-
$kernel ]] \
620 && _config_file
="/boot/config-$kernel"
621 [[ -f /lib
/modules
/$kernel/config
]] \
622 && _config_file
="/lib/modules/$kernel/config"
624 # no kernel config file, so return true
625 [[ $_config_file ]] ||
return 0
627 grep -q -F "${_config_opt}=" "$_config_file" && return 0
633 # Only two values are returned: AMD or Intel
636 if grep -qE AMD
/proc
/cpuinfo
; then
639 if grep -qE Intel
/proc
/cpuinfo
; then
645 # Get the hosts' ucode file based on the /proc/cpuinfo
648 local family
=`grep -E "cpu family" /proc/cpuinfo | head -1 | sed s/.*:\ //`
649 local model
=`grep -E "model" /proc/cpuinfo |grep -v name | head -1 | sed s/.*:\ //`
650 local stepping
=`grep -E "stepping" /proc/cpuinfo | head -1 | sed s/.*:\ //`
652 if [[ "$(get_cpu_vendor)" == "AMD" ]]; then
653 # If family greater or equal than 0x15
654 if [[ $family -ge 21 ]]; then
655 printf "microcode_amd_fam15h.bin"
657 printf "microcode_amd.bin"
660 if [[ "$(get_cpu_vendor)" == "Intel" ]]; then
661 # The /proc/cpuinfo are in decimal.
662 printf "%02x-%02x-%02x" ${family} ${model} ${stepping}
666 # Not every device in /dev/mapper should be examined.
667 # If it is an LVM device, touch only devices which have /dev/VG/LV symlink.
669 local dev_dm_dir
=/sys
/dev
/block
/$1/dm
670 [[ ! -f $dev_dm_dir/uuid || $
(<$dev_dm_dir/uuid
) != LVM-
* ]] && return 1 # Not an LVM device
671 local DM_VG_NAME DM_LV_NAME DM_LV_LAYER
672 eval $
(dmsetup splitname
--nameprefixes --noheadings --rows "$(<$dev_dm_dir/name)" 2>/dev
/null
)
673 [[ ${DM_VG_NAME} ]] && [[ ${DM_LV_NAME} ]] ||
return 0 # Better skip this!
674 [[ ${DM_LV_LAYER} ]] || [[ ! -L /dev/${DM_VG_NAME}/${DM_LV_NAME} ]]
679 btrfs device usage
"$_mp" \
680 |
while read _dev _rest
; do
681 str_starts
"$_dev" "/" ||
continue
683 printf -- "%s\n" "$_dev"