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 # Log initrd creation.
28 if ! [[ $dracutlogfile ]]; then
29 [[ $dracutbasedir = /usr
/share
/dracut
]] && \
30 dracutlogfile
=/var
/log
/dracut.log || \
31 dracutlogfile
=/tmp
/dracut.log
32 # [[ -w $dracutlogfile ]] || dracutlogfile=/tmp/dracut.log
33 if [[ -w $dracutlogfile ]]; then
40 [[ -w $dracutlogfile ]] && echo "W: $@" >>"$dracutlogfile"
44 [[ $beverbose ]] && echo "I: $@" >&2
45 [[ -w $dracutlogfile ]] && echo "I: $@" >>"$dracutlogfile"
50 [[ -w $dracutlogfile ]] && echo "E: $@" >>"$dracutlogfile"
54 if [[ -x /lib
/udev
/vol_id
]]; then
55 eval $
(/lib
/udev
/vol_id
--export $1)
56 elif find_binary blkid
>/dev
/null
; then
57 eval $
(blkid
-o udev
$1)
64 get_fs_env
$1 ||
return
69 get_fs_env
$1 ||
return
73 # finds the major:minor of the block device backing the root filesystem.
75 local rootdev blkdev fs
type opts misc
76 while read blkdev fs
type opts misc
; do
77 [[ $blkdev = rootfs
]] && continue # skip rootfs entry
78 [[ $fs = $1 ]] && { rootdev
=$blkdev; break; } # we have a winner!
80 [[ -b $rootdev ]] ||
return 1 # oops, not a block device.
81 # get major/minor for the device
82 ls -nLl "$rootdev" | \
83 (read x x x x maj min x
; maj
=${maj//,/}; echo $maj:$min)
86 find_root_block_device
() { find_block_device
/; }
88 # Walk all the slave relationships for a given block device.
89 # Stop when our helper function returns success
90 # $1 = function to call on every found block device
91 # $2 = block device in major:minor format
92 check_block_and_slaves
() {
94 [[ -b /dev
/block
/$2 ]] ||
return 1 # Not a block device? So sorry.
96 check_vol_slaves
"$@" && return 0
97 [[ -d /sys
/dev
/block
/$2/slaves
]] ||
return 1
98 for x
in /sys
/dev
/block
/$2/slaves
/*/dev
; do
99 [[ -f $x ]] ||
continue
100 check_block_and_slaves
$1 $
(cat "$x") && return 0
106 ls -lH "$1" |
{ read a b c d maj min rest
; printf "%d:%d" ${maj%%,} $min;}
109 # ugly workaround for the lvm design
110 # There is no volume group device,
111 # so, there are no slave devices for volume groups.
112 # Logical volumes only have the slave devices they really live on,
113 # but you cannot create the logical volume without the volume group.
114 # And the volume group might be bigger than the devices the LV needs.
116 for i
in /dev
/mapper
/*; do
117 lv
=$
(get_numeric_dev
$i)
118 if [[ $lv = $2 ]]; then
119 vg
=$
(lvm lvs
--noheadings -o vg_name
$i 2>/dev
/null
)
123 for pv
in $
(lvm vgs
--noheadings -o pv_name
"$vg" 2>/dev
/null
); \
125 check_block_and_slaves
$1 $
(get_numeric_dev
$pv) \
134 # Install a directory, keeping symlinks as on the original system.
135 # Example: if /lib64 points to /lib on the host, "inst_dir /lib/file"
136 # will create ${initdir}/lib64, ${initdir}/lib64/file,
137 # and a symlink ${initdir}/lib -> lib64.
140 [[ -e "${initdir}$dir" ]] && return 0
142 # iterate over parent directories
146 [ -z "$part" ] && continue
148 [[ -e "${initdir}$file" ]] && continue
150 if [ -L "$file" ]; then
151 # create link as the original
152 local target
=$
(readlink
"$file")
153 ln -sfn "$target" "${initdir}$file" ||
return 1
154 # resolve relative path and recursively install destionation
155 [[ "$target" = "${target##*/}" ]] && target
="${file%/*}/$target"
159 mkdir
-p "${initdir}$file" ||
return 1
164 # $1 = file to copy to ramdisk
165 # $2 (optional) Name for the file on the ramdisk
166 # Location of the image dir is assumed to be $initdir
167 # We never overwrite the target if it exists.
170 [[ -f $1 ]] ||
return 1
171 src
=$1 target
="${2:-$1}"
172 if ! [[ -d ${initdir}$target ]]; then
173 [[ -e ${initdir}$target ]] && return 0
174 inst_dir
"${target%/*}"
176 dinfo
"Installing $src"
177 cp -pfL "$src" "${initdir}$target"
180 # Same as above, but specialized to handle dynamic libraries.
181 # It handles making symlinks according to how the original library
184 local src
=$1 dest
=${2:-$1}
185 [[ -e $initdir$dest ]] && return 0
186 if [[ -L $src ]]; then
187 reallib
=$
(readlink
-f "$src")
189 inst_simple
"$reallib" "$reallib"
190 inst_dir
"${dest%/*}"
191 (cd "${initdir}${dest%/*}" && ln -s "$reallib" "$lib")
193 inst_simple
"$src" "$dest"
197 # find a binary. If we were not passed the full path directly,
198 # search in the usual places to find the binary.
200 local binpath
="/bin /sbin /usr/bin /usr/sbin" p
201 [[ -z ${1##/*} && -x $1 ]] && { echo $1; return 0; }
202 for p
in $binpath; do
203 [[ -x $p/$1 ]] && { echo "$p/$1"; return 0; }
208 # Same as above, but specialized to install binary executables.
209 # Install binary executable, and all shared library dependencies, if any.
212 bin
=$
(find_binary
"$1") ||
return 1
214 inst_symlink
$bin $target && return 0
215 local LDSO NAME IO FILE ADDR I1 n f TLIBDIR
216 [[ -e $initdir$target ]] && return 0
218 ldd
$bin 2>/dev
/null |
while read line
; do
219 [[ $line = 'not a dynamic executable' ]] && return 1
220 if [[ $line =~ not\ found
]]; then
221 derror
"Missing a shared library required by $bin."
222 derror
"Run \"ldd $bin\" to find out what it is."
223 derror
"dracut cannot create an initrd."
226 so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
227 [[ $line =~
$so_regex ]] ||
continue
228 FILE
=${BASH_REMATCH[1]}
229 [[ -e ${initdir}$FILE ]] && continue
230 # see if we are loading an optimized version of a shared lib.
231 lib_regex
='^(/lib[^/]*).*'
232 if [[ $FILE =~
$lib_regex ]]; then
233 TLIBDIR
=${BASH_REMATCH[1]}
235 # prefer nosegneg libs, then unoptimized ones.
236 for f
in "$TLIBDIR/i686/nosegneg" "$TLIBDIR"; do
237 [[ -e $f/$BASE ]] ||
continue
241 inst_library
"$FILE" "$TLIBDIR/$BASE"
247 inst_simple
"$bin" "$target"
250 # same as above, except for shell scripts.
251 # If your shell script does not start with shebang, it is not a shell script.
253 [[ -f $1 ]] ||
return 1
255 read -r -n 80 line
<"$1"
256 # If debug is set, clean unprintable chars to prevent messing up the term
257 [[ $debug ]] && line
=$
(echo -n "$line" |
tr -c -d '[:print:][:space:]')
258 shebang_regex
='(#! *)(/[^ ]+).*'
259 [[ $line =~
$shebang_regex ]] ||
return 1
260 inst
"${BASH_REMATCH[2]}" && inst_simple
"$@"
263 # same as above, but specialized for symlinks
265 local src
=$1 target
=$initdir${2:-$1} realsrc
266 [[ -L $1 ]] ||
return 1
267 [[ -L $target ]] && return 0
268 realsrc
=$
(readlink
-f "$src")
269 [[ $realsrc = ${realsrc##*/} ]] && realsrc
=${src%/*}/$realsrc
270 inst
"$realsrc" && ln -s "$realsrc" "$target"
273 # find a udev rule in the usual places.
275 [[ -f $1 ]] && { echo "$1"; return 0; }
276 for r
in .
/lib
/udev
/rules.d
/etc
/udev
/rules.d
$dracutbasedir/rules.d
; do
277 [[ -f $r/$1 ]] && { echo "$r/$1"; return 0; }
282 # udev rules always get installed in the same place, so
283 # create a function to install them to make life simpler.
285 local target
=/etc
/udev
/rules.d
286 inst_dir
"/lib/udev/rules.d"
289 rule
=$
(find_rule
"$rule") && \
290 inst_simple
"$rule" "$target/${rule##*/}"
294 # general purpose installation function
295 # Same args as above.
300 [[ -z $initdir ]] && [[ -d $2 ]] && export initdir
=$2
301 [[ $initdir = $2 ]] && set $1
304 [[ -z $initdir ]] && export initdir
=$2
308 derror
"inst only takes 1 or 2 or 3 arguments"
312 for x
in inst_symlink inst_script inst_binary inst_simple
; do
318 # install function specialized for hooks
319 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
320 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
322 if ! [[ -f $3 ]]; then
323 derror
"Cannot install a hook ($3) that does not exist."
324 derror
"Aborting initrd creation."
326 elif ! strstr
"$hookdirs" "$1"; then
327 derror
"No such hook type $1. Aborting initrd creation."
330 inst_simple
"$3" "/${1}/${2}${3##*/}"
334 if [[ $1 = '-o' ]]; then
339 if ! inst
"$1" ; then
340 if [[ $optional = yes ]]; then
341 dwarning
"Skipping program $1 as it cannot be found and is flagged to be optional"
343 derror
"Failed to install $1"
351 check_module_deps
() {
353 # if we are already set to be loaded, we do not have to be checked again.
354 strstr
"$mods_to_load" " $1 " && return
355 # turn a module name into a directory, if we can.
356 moddir
=$
(echo ${dracutbasedir}/modules.d
/??
${1})
357 [[ -d $moddir && -x $moddir/install ]] ||
return 1
358 # if we do not have a check script, we are unconditionally included
359 if [[ -x $moddir/check
]]; then
362 # a return value of 255 = load module only as a dependency.
363 ((ret
==0||ret
==255)) ||
return 1
364 for dep
in $
("$moddir/check" -d); do
365 check_module_deps
"$dep" && continue
366 dwarning
"Dependency $mod failed."
373 should_source_module
() {
375 if [[ $kernel_only = yes ]]; then
376 [[ -x $1/installkernel
]] && return 0
379 [[ -x $1/install ]] ||
[[ -x $1/installkernel
]] ||
return 1
380 [[ -x $1/check
]] ||
return 0
381 "$1/check" $hostonly ||
return 1
382 for dep
in $
("$1/check" -d); do
383 check_module_deps
"$dep" && continue
384 dwarning
"Cannot load $mod, dependencies failed."
390 for moddir
in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
391 local mod
=${moddir##*/}; mod
=${mod#[0-9][0-9]}
392 # If we are already scheduled to be loaded, no need to check again.
393 strstr
"$mods_to_load" " $mod " && continue
394 # This should never happen, but...
395 [[ -d $moddir ]] ||
continue
396 [[ $dracutmodules != all
]] && ! strstr
"$dracutmodules" "$mod" && \
398 strstr
"$omit_dracutmodules" "$mod" && continue
399 if ! strstr
"$add_dracutmodules" "$mod"; then
400 should_source_module
"$moddir" ||
continue
402 mods_to_load
+=" $mod "
406 # Install a single kernel module along with any firmware it may require.
407 # $1 = full path to kernel module to install
408 install_kmod_with_fw
() {
409 local modname
=${1##*/} fwdir found
410 modname
=${modname%.ko*}
411 inst_simple
"$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" || \
412 return 0 # no need to go further if the module is already installed
413 for fw
in $
(modinfo
-k $kernel -F firmware
$1 2>/dev
/null
); do
415 for fwdir
in $fw_dir; do
416 if [[ -d $fwdir && -f $fwdir/$fw ]]; then
417 inst_simple
"$fwdir/$fw" "/lib/firmware/$fw"
421 if [[ $found != yes ]]; then
422 dinfo
"Possible missing firmware ${fw} for module ${mod}.ko"
427 # Do something with all the dependencies of a kernel module.
428 # Note that kernel modules depend on themselves using the technique we use
429 # $1 = function to call for each dependency we find
430 # It will be passed the full path to the found kernel module
431 # $2 = module to get dependencies for
432 # rest of args = arguments to modprobe
433 for_each_kmod_dep
() {
434 local func
=$1 kmod
=$2 cmd modpapth options
436 modprobe
"$@" --ignore-install --show-depends $kmod 2>/dev
/null | \
437 while read cmd modpath options
; do
438 [[ $cmd = insmod
]] ||
continue
443 # filter kernel modules to install certain modules that meet specific
445 # $1 = function to call with module name to filter.
446 # This function will be passed the full path to the module to test.
447 # The behaviour of this function can vary depending on whether $hostonly is set.
448 # If it is, we will only look at modules that are already in memory.
449 # If it is not, we will look at all kernel modules
450 # This function returns the full filenames of modules that match $1
451 filter_kernel_modules
() (
452 if ! [[ $hostonly ]]; then
453 filtercmd
='find "$srcmods/kernel/drivers" -name "*.ko" -o -name "*.ko.gz"'
455 filtercmd
='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename -k $kernel'
457 for modname
in $
(eval $filtercmd); do
460 "$1" "$modname" && echo "$modname"
463 gzip -dc "$modname" > $initdir/$$.ko
464 $1 $initdir/$$.ko
&& echo "$modname"
471 # install kernel modules along with all their dependencies.
473 [[ $no_kernel = yes ]] && return
474 local mod mpargs modpath modname cmd
478 =*) # This introduces 2 incompatible meanings for =* arguments
479 # to instmods. We need to decide which one to keep.
480 if [[ $mod = =ata
&& -f $srcmods/modules.block
]] ; then
481 instmods
$mpargs $
(egrep 'ata|ahci' "${srcmods}/modules.block")
482 elif [ -f $srcmods/modules.
${mod#=} ]; then
483 instmods
$mpargs $
(cat ${srcmods}/modules.
${mod#=} )
485 instmods
$mpargs $
(find "$srcmods" -path "*/${mod#=}/*")
492 # Must never run this diagnostic-only module
497 # if we are already installed, skip this module and go on
499 [[ -f $initdir/$1 ]] && { shift; continue; }
500 # If we are building a host-specific initramfs and this
501 # module is not already loaded, move on to the next one.
502 [[ $hostonly ]] && ! grep -q "${mod//-/_}" /proc
/modules
&& \
503 ! echo $add_drivers |
grep -qe "\<${mod}\>" && {
506 # ok, load the module, all its dependencies, and any firmware
508 for_each_kmod_dep install_kmod_with_fw
$mod \
509 --set-version $kernel -d ${srcmods%%/lib/modules/*}/
516 # vim:ts=8:sw=4:sts=4:et