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/>.
24 # Generic substring function. If $2 is in $1, return 0.
25 strstr
() { [[ $1 =~
$2 ]]; }
27 # Create all subdirectories for given path without creating the last element.
29 mksubdirs
() { mkdir
-p ${1%/*}; }
31 # Version comparision function. Assumes Linux style version scheme.
33 # $2 = comparision op (gt, ge, eq, le, lt, ne)
36 local n1
=(${1//./ }) op
=$2 n2
=(${3//./ }) i res
40 if [[ ! ${n1[i]}${n2[i]} ]]; then res
=0
41 elif ((${n1[i]:-0} > ${n2[i]:-0})); then res
=1
42 elif ((${n1[i]:-0} < ${n2[i]:-0})); then res
=2
58 # Log initrd creation.
59 if ! [[ $dracutlogfile ]]; then
60 [[ $dracutbasedir = /usr
/share
/dracut
]] && \
61 dracutlogfile
=/var
/log
/dracut.log || \
62 dracutlogfile
=/tmp
/dracut.log
63 # [[ -w $dracutlogfile ]] || dracutlogfile=/tmp/dracut.log
64 if [[ -w $dracutlogfile ]]; then
71 [[ -w $dracutlogfile ]] && echo "W: $@" >>"$dracutlogfile"
75 [[ $beverbose ]] && echo "I: $@" >&2
76 [[ -w $dracutlogfile ]] && echo "I: $@" >>"$dracutlogfile"
81 [[ -w $dracutlogfile ]] && echo "E: $@" >>"$dracutlogfile"
84 # Function prints global variables in format name=value line by line.
85 # $@ = list of global variables' name
91 value
=$
(eval echo \$
$var)
92 [[ ${value} ]] && echo "${var}=\"${value}\""
98 eval $(udevadm info --query=env --name=$1|egrep 'ID_FS_(TYPE|UUID)=')
99 [[ $ID_FS_TYPE ]] && return
101 if [[ -x /lib/udev/vol_id ]]; then
102 eval $(/lib/udev/vol_id --export $1)
103 elif find_binary blkid >/dev/null; then
104 eval $(blkid -o udev $1)
112 if [[ $1 != ${1#/dev/block/nfs:} ]] \
113 || [[ $1 != ${1#/dev/block/nfs3:} ]] \
114 || [[ $1 != ${1#/dev/block/nfs4:} ]]; then
118 get_fs_env $1 || return
123 get_fs_env $1 || return
127 # finds the major:minor of the block device backing the root filesystem.
128 find_block_device() {
129 local x mpt majmin dev fs misc maj min
130 if [[ $use_fstab != yes ]]; then
131 while read x x majmin x mpt x x fs misc; do
132 [[ $fs = nfs ]] && { echo $dev; return 0;}
133 [[ $fs = nfs3 ]] && { echo $dev; return 0;}
134 [[ $fs = nfs4 ]] && { echo $dev; return 0;}
135 if [[ $mpt = $1 ]] && [[ ${majmin#0:} = $majmin ]]; then
137 return 0 # we have a winner!
139 done < /proc/self/mountinfo
141 # fall back to /etc/fstab
142 while read dev mpt fs misc; do
143 if [[ $mpt = $1 ]]; then
144 [[ $fs = nfs ]] && { echo $dev; return 0;}
145 [[ $fs = nfs3 ]] && { echo $dev; return 0;}
146 [[ $fs = nfs4 ]] && { echo $dev; return 0;}
147 [[ $dev != ${dev#UUID=} ]] && dev=/dev/disk/by-uuid/${dev#UUID=}
148 [[ $dev != ${dev#LABEL=} ]] && dev=/dev/disk/by-label/${dev#LABEL=}
149 [[ -b $dev ]] || return 1 # oops, not a block device.
151 read x x x x maj min x;
160 find_root_block_device() { find_block_device /; }
162 # Walk all the slave relationships for a given block device.
163 # Stop when our helper function returns success
164 # $1 = function to call on every found block device
165 # $2 = block device in major:minor format
166 check_block_and_slaves() {
168 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
170 check_vol_slaves "$@
" && return 0
171 if [[ -f "/sys
/dev
/block
/$2/..
/dev
" ]]; then
172 check_block_and_slaves $1 $(cat "/sys
/dev
/block
/$2/..
/dev
") && return 0
174 [[ -d /sys/dev/block/$2/slaves ]] || return 1
175 for x in /sys/dev/block/$2/slaves/*/dev; do
176 [[ -f $x ]] || continue
177 check_block_and_slaves $1 $(cat "$x") && return 0
183 ls -lH "$1" | { read a b c d maj min rest; printf "%d
:%d
" ${maj%%,} $min;}
186 # ugly workaround for the lvm design
187 # There is no volume group device,
188 # so, there are no slave devices for volume groups.
189 # Logical volumes only have the slave devices they really live on,
190 # but you cannot create the logical volume without the volume group.
191 # And the volume group might be bigger than the devices the LV needs.
193 for i in /dev/mapper/*; do
194 lv=$(get_numeric_dev $i)
195 if [[ $lv = $2 ]]; then
196 vg=$(lvm lvs --noheadings -o vg_name $i 2>/dev/null)
200 for pv in $(lvm vgs --noheadings -o pv_name "$vg" 2>/dev/null); \
202 check_block_and_slaves $1 $(get_numeric_dev $pv) \
211 # Install a directory, keeping symlinks as on the original system.
212 # Example: if /lib64 points to /lib on the host, "inst_dir
/lib
/file"
213 # will create ${initdir}/lib64, ${initdir}/lib64/file,
214 # and a symlink ${initdir}/lib -> lib64.
217 [[ -e "${initdir}$dir" ]] && return 0
219 # iterate over parent directories
223 [ -z "$part" ] && continue
225 [[ -e "${initdir}$file" ]] && continue
227 if [ -L "$file" ]; then
228 # create link as the original
229 local target=$(readlink "$file")
230 ln -sfn "$target" "${initdir}$file" || return 1
231 # resolve relative path and recursively install destionation
232 [[ "$target" = "${target##*/}" ]] && target="${file%/*}/$target"
236 mkdir -p "${initdir}$file" || return 1
241 # $1 = file to copy to ramdisk
242 # $2 (optional) Name for the file on the ramdisk
243 # Location of the image dir is assumed to be $initdir
244 # We never overwrite the target if it exists.
247 [[ -f $1 ]] || return 1
248 src=$1 target="${2:-$1}"
249 if ! [[ -d ${initdir}$target ]]; then
250 [[ -e ${initdir}$target ]] && return 0
251 inst_dir "${target%/*}"
253 dinfo "Installing
$src"
254 cp -pfL "$src" "${initdir}$target"
257 # find symlinks linked to given library file
259 # Function searches for symlinks by stripping version numbers appended to
260 # library filename, checks if it points to the same target and finally
261 # prints the list of symlinks to stdout.
264 # rev_lib_symlinks libfoo.so.8.1
265 # output: libfoo.so.8 libfoo.so
266 # (Only if libfoo.so.8 and libfoo.so exists on host system.)
268 [[ ! $1 ]] && return 0
270 local fn="$1" orig="$
(readlink
-f "$1")" links=''
272 [[ ${fn} =~ .*\.so\..* ]] || return 1
274 until [[ ${fn##*.} == so ]]; do
276 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
282 # Same as above, but specialized to handle dynamic libraries.
283 # It handles making symlinks according to how the original library
286 local src
=$1 dest
=${2:-$1} lib reallib symlink
287 [[ -e $initdir$dest ]] && return 0
288 if [[ -L $src ]]; then
289 reallib
=$
(readlink
-f "$src")
291 inst_simple
"$reallib" "$reallib"
292 inst_dir
"${dest%/*}"
293 (cd "${initdir}${dest%/*}" && ln -s "$reallib" "$lib")
295 inst_simple
"$src" "$dest"
298 # Create additional symlinks. See rev_symlinks description.
299 for symlink
in $
(rev_lib_symlinks
$src) $
(rev_lib_symlinks
$reallib); do
300 [[ ! -e $initdir$symlink ]] && {
301 dinfo
"Creating extra symlink: $symlink"
302 inst_symlink
$symlink
307 # find a binary. If we were not passed the full path directly,
308 # search in the usual places to find the binary.
310 local binpath
="/bin /sbin /usr/bin /usr/sbin" p
311 [[ -z ${1##/*} && -x $1 ]] && { echo $1; return 0; }
312 for p
in $binpath; do
313 [[ -x $p/$1 ]] && { echo "$p/$1"; return 0; }
318 # Same as above, but specialized to install binary executables.
319 # Install binary executable, and all shared library dependencies, if any.
322 bin
=$
(find_binary
"$1") ||
return 1
324 inst_symlink
$bin $target && return 0
325 local LDSO NAME IO FILE ADDR I1 n f TLIBDIR
326 [[ -e $initdir$target ]] && return 0
328 LC_ALL
=C ldd
$bin 2>/dev
/null |
while read line
; do
329 [[ $line = 'not a dynamic executable' ]] && return 1
330 if [[ $line =~ not\ found
]]; then
331 derror
"Missing a shared library required by $bin."
332 derror
"Run \"ldd $bin\" to find out what it is."
333 derror
"dracut cannot create an initrd."
336 so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
337 [[ $line =~
$so_regex ]] ||
continue
338 FILE
=${BASH_REMATCH[1]}
339 [[ -e ${initdir}$FILE ]] && continue
340 # see if we are loading an optimized version of a shared lib.
341 lib_regex
='^(/lib[^/]*).*'
342 if [[ $FILE =~
$lib_regex ]]; then
343 TLIBDIR
=${BASH_REMATCH[1]}
345 # prefer nosegneg libs, then unoptimized ones.
346 for f
in "$TLIBDIR/i686/nosegneg" "$TLIBDIR"; do
347 [[ -e $f/$BASE ]] ||
continue
351 inst_library
"$FILE" "$TLIBDIR/$BASE"
357 inst_simple
"$bin" "$target"
360 # same as above, except for shell scripts.
361 # If your shell script does not start with shebang, it is not a shell script.
363 [[ -f $1 ]] ||
return 1
365 read -r -n 80 line
<"$1"
366 # If debug is set, clean unprintable chars to prevent messing up the term
367 [[ $debug ]] && line
=$
(echo -n "$line" |
tr -c -d '[:print:][:space:]')
368 shebang_regex
='(#! *)(/[^ ]+).*'
369 [[ $line =~
$shebang_regex ]] ||
return 1
370 inst
"${BASH_REMATCH[2]}" && inst_simple
"$@"
373 # same as above, but specialized for symlinks
375 local src
=$1 target
=$initdir${2:-$1} realsrc
376 [[ -L $1 ]] ||
return 1
377 [[ -L $target ]] && return 0
378 realsrc
=$
(readlink
-f "$src")
379 [[ $realsrc = ${realsrc##*/} ]] && realsrc
=${src%/*}/$realsrc
380 inst
"$realsrc" && ln -s "$realsrc" "$target"
383 # find a udev rule in the usual places.
385 [[ -f $1 ]] && { echo "$1"; return 0; }
386 for r
in .
/lib
/udev
/rules.d
/etc
/udev
/rules.d
$dracutbasedir/rules.d
; do
387 [[ -f $r/$1 ]] && { echo "$r/$1"; return 0; }
392 # udev rules always get installed in the same place, so
393 # create a function to install them to make life simpler.
395 local target
=/etc
/udev
/rules.d rule found
397 inst_dir
"/lib/udev/rules.d"
400 found
=$
(find_rule
"$rule") && \
401 inst_simple
"$found" "$target/${found##*/}" \
402 || dinfo
"Skipping udev rule: $rule"
406 # general purpose installation function
407 # Same args as above.
412 [[ -z $initdir ]] && [[ -d $2 ]] && export initdir
=$2
413 [[ $initdir = $2 ]] && set $1
416 [[ -z $initdir ]] && export initdir
=$2
420 derror
"inst only takes 1 or 2 or 3 arguments"
424 for x
in inst_symlink inst_script inst_binary inst_simple
; do
430 # install function specialized for hooks
431 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
432 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
434 if ! [[ -f $3 ]]; then
435 derror
"Cannot install a hook ($3) that does not exist."
436 derror
"Aborting initrd creation."
438 elif ! strstr
"$hookdirs" "$1"; then
439 derror
"No such hook type $1. Aborting initrd creation."
442 inst_simple
"$3" "/${1}/${2}${3##*/}"
446 if [[ $1 = '-o' ]]; then
451 if ! inst
"$1" ; then
452 if [[ $optional = yes ]]; then
453 dwarning
"Skipping program $1 as it cannot be found and is flagged to be optional"
455 derror
"Failed to install $1"
463 # install function decompressing the target and handling symlinks
464 # $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
466 # Function install targets in the same paths inside overlay but decompressed
467 # and without extensions (.gz, .bz2).
469 local src dst realsrc realdst cmd
474 *.gz
) cmd
='gzip -d' ;;
475 *.bz2
) cmd
='bzip2 -d' ;;
481 realsrc
="$(readlink -f ${src})" # symlink target with extension
482 dst
="${src%.*}" # symlink without extension
483 realdst
="${realsrc%.*}" # symlink target without extension
484 mksubdirs
"${initdir}/${src}"
485 # Create symlink without extension to target without extension.
486 ln -s "${realdst}" "${initdir}/${dst}"
489 # If the source is symlink we operate on its target.
490 [[ ${realsrc} ]] && src
=${realsrc}
492 # Decompress with chosen tool. We assume that tool changes name e.g.
493 # from 'name.gz' to 'name'.
494 ${cmd} "${initdir}${src}"
498 # It's similar to above, but if file is not compressed, performs standard
501 inst_opt_decompress() {
506 inst_decompress "${src}" || inst "${src}"
510 check_module_deps() {
512 # if we are already set to be loaded, we do not have to be checked again.
513 strstr " $mods_to_load " " $1 " && return
514 strstr " $omit_dracutmodules " " $1 " && return 1
515 # turn a module name into a directory, if we can.
516 moddir=$(echo ${dracutbasedir}/modules.d/??${1})
517 [[ -d $moddir && -x $moddir/install ]] || return 1
518 # if we do not have a check script, we are unconditionally included
519 if [[ -x $moddir/check ]]; then
522 # a return value of 255 = load module only as a dependency.
523 ((ret==0||ret==255)) || return 1
524 for dep in $("$moddir/check
" -d); do
525 check_module_deps "$dep" && continue
526 dwarning "Dependency
$mod failed.
"
533 should_source_module() {
536 if [[ $kernel_only = yes ]]; then
537 [[ -x $1/installkernel ]] && return 0
540 [[ -x $1/install ]] || [[ -x $1/installkernel ]] || return 1
541 [[ -x $1/check ]] || return 0
542 "$1/check
" $hostonly || continue
543 for dep in $("$1/check
" -d); do
544 check_module_deps "$dep" && continue
545 dwarning "Cannot load dracut module
\"$mod\", dependencies failed.
"
553 for moddir in "$dracutbasedir/modules.d
"/[0-9][0-9]*; do
554 local mod=${moddir##*/}; mod=${mod#[0-9][0-9]}
555 # If we are already scheduled to be loaded, no need to check again.
556 strstr " $mods_to_load " " $mod " && continue
557 # This should never happen, but...
558 [[ -d $moddir ]] || continue
559 strstr " $omit_dracutmodules " " $mod " && continue
561 if ! strstr " $dracutmodules $add_dracutmodules " " $mod "; then
562 # module not in our list
563 if [[ $dracutmodules = all ]]; then
564 # check, if we can install this module
565 should_source_module "$moddir" || continue
571 if [ -x "$moddir/check
" ] \
572 && "$moddir/check
" -d > /dev/null 2>&1; then
573 check_module_deps "$mod" || {
574 dwarning "Cannot load dracut module
\"$mod\", dependencies failed.
"
580 mods_to_load+=" $mod "
583 modcheck=$add_dracutmodules
584 [[ $dracutmodules != all ]] && modcheck="$m $dracutmodules"
585 for mod in $modcheck; do
586 strstr " $mods_to_load " " $mod " && continue
587 strstr " $omit_dracutmodules " " $mod " && continue
588 dwarning "Dracut module
\"$mod\" cannot be found.
"
592 # Install a single kernel module along with any firmware it may require.
593 # $1 = full path to kernel module to install
594 install_kmod_with_fw() {
595 local modname=${1##*/} fwdir found
596 modname=${modname%.ko*}
597 inst_simple "$1" "/lib
/modules
/$kernel/${1##*/lib/modules/$kernel/}" || \
598 return 0 # no need to go further if the module is already installed
599 for fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do
601 for fwdir in $fw_dir; do
602 if [[ -d $fwdir && -f $fwdir/$fw ]]; then
603 inst_simple "$fwdir/$fw" "/lib
/firmware
/$fw"
607 if [[ $found != yes ]]; then
608 dinfo "Possible missing firmware
\"${fw}\" for kernel module
\"${mod}.ko
\""
613 # Do something with all the dependencies of a kernel module.
614 # Note that kernel modules depend on themselves using the technique we use
615 # $1 = function to call for each dependency we find
616 # It will be passed the full path to the found kernel module
617 # $2 = module to get dependencies for
618 # rest of args = arguments to modprobe
619 for_each_kmod_dep() {
620 local func=$1 kmod=$2 cmd modpapth options
622 modprobe "$@
" --ignore-install --show-depends $kmod 2>/dev/null | \
623 while read cmd modpath options; do
624 [[ $cmd = insmod ]] || continue
629 # filter kernel modules to install certain modules that meet specific
631 # $1 = function to call with module name to filter.
632 # This function will be passed the full path to the module to test.
633 # The behaviour of this function can vary depending on whether $hostonly is set.
634 # If it is, we will only look at modules that are already in memory.
635 # If it is not, we will look at all kernel modules
636 # This function returns the full filenames of modules that match $1
637 filter_kernel_modules () (
638 if ! [[ $hostonly ]]; then
639 filtercmd='find "$srcmods/kernel
/drivers
" -name "*.ko
" -o -name "*.ko.gz
"'
641 filtercmd='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename -k $kernel'
643 for modname in $(eval $filtercmd); do
646 "$1" "$modname" && echo "$modname"
649 gzip -dc "$modname" > $initdir/$$.ko
650 $1 $initdir/$$.ko && echo "$modname"
657 # install kernel modules along with all their dependencies.
659 [[ $no_kernel = yes ]] && return
660 local mod mpargs modpath modname cmd moddirname
664 =*) # This introduces 2 incompatible meanings for =* arguments
665 # to instmods. We need to decide which one to keep.
666 if [[ $mod = =ata && -f $srcmods/modules.block ]] ; then
667 instmods $mpargs $(egrep 'ata|ahci' "${srcmods}/modules.block
")
668 elif [ -f $srcmods/modules.${mod#=} ]; then
669 instmods $mpargs $(cat ${srcmods}/modules.${mod#=} )
671 instmods $mpargs $(find "$srcmods" -path "*/${mod#=}/*")
678 # Must never run this diagnostic-only module
683 # if we are already installed, skip this module and go on
685 [[ -f $initdir/$1 ]] && { shift; continue; }
686 # If we are building a host-specific initramfs and this
687 # module is not already loaded, move on to the next one.
688 [[ $hostonly ]] && ! grep -qe "\
<${mod//-/_}\
>" /proc/modules && \
689 ! echo $add_drivers | grep -qe "\
<${mod}\
>" && {
693 # We use '-d' option in modprobe only if modules prefix path
694 # differs from default '/'. This allows us to use Dracut with
695 # old version of modprobe which doesn't have '-d' option.
696 moddirname=${srcmods%%/lib/modules/*}
697 [[ -n ${moddirname} ]] && moddirname="-d ${moddirname}/"
699 # ok, load the module, all its dependencies, and any firmware
701 for_each_kmod_dep install_kmod_with_fw $mod \
702 --set-version $kernel ${moddirname}
709 # vim:ts=8:sw=4:sts=4:et