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*} = $1 ]]; }
27 # Log initrd creation.
28 if ! [[ $dracutlogfile ]]; then
29 [[ $dsrc = /usr
/share
/dracut
]] && \
30 dracutlogfile
=/var
/log
/dracut.log || \
31 dracutlogfile
=/tmp
/dracut.log
32 [[ -w "$dracutlogfile" ]] || dracutlogfile
=/tmp
/dracut.log
38 [[ -w "$dracutlogfile" ]] && echo "W: $@" >>"$dracutlogfile"
42 [[ $beverbose ]] && echo "I: $@" >&2
43 [[ -w "$dracutlogfile" ]] && echo "I: $@" >>"$dracutlogfile"
48 [[ -w "$dracutlogfile" ]] && echo "E: $@" >>"$dracutlogfile"
51 # $1 = file to copy to ramdisk
52 # $2 (optional) Name for the file on the ramdisk
53 # Location of the image dir is assumed to be $initdir
54 # We never overwrite the target if it exists.
57 [[ -f $1 ]] ||
return 1
58 src
=$1 target
="${initdir}${2:-$1}"
59 [[ -f $target ]] && return 0
60 mkdir
-p "${target%/*}"
61 dinfo
"Installing $src"
62 cp -pfL "$src" "$target"
65 # Same as above, but specialzed to handle dynamic libraries.
66 # It handles making symlinks according to how the original library
69 local src
=$1 dest
=${2:-$1}
70 [[ -f $initdir$dest ]] && return 0
71 if [[ -L $src ]]; then
72 reallib
="$(readlink -f "$src")"
74 inst_simple
"$reallib" "$reallib"
75 mkdir
-p ${initdir}${dest%/*}
76 (cd "${initdir}${dest%/*}" && ln -s "$reallib" "$lib")
78 inst_simple
"$src" "$dest"
82 # find a binary. If we were not passed the full path directly,
83 # search in the usual places to find the binary.
85 local binpath
="/bin /sbin /usr/bin /usr/sbin" p
86 [[ -z ${1##/*} && -x $1 ]] && { echo $1; return 0; }
88 [[ -x $p/$1 ]] && { echo "$p/$1"; return 0; }
93 # Same as above, but specialized to install binary executables.
94 # Install binary executable, and all shared library dependencies, if any.
97 bin
=$
(find_binary
"$1") ||
return 1
99 local LDSO NAME IO FILE ADDR I1 n f TLIBDIR
100 [[ -f $initdir$target ]] && return 0
102 ldd
$bin 2>/dev
/null |
while read line
; do
103 [[ $line = 'not a dynamic executable' ]] && return 1
104 if [[ $line =~ not\ found
]]; then
105 derror
"Missing a shared library required by $bin."
106 derror
"Run \"ldd $bin\" to find out what it is."
107 derror
"dracut cannot create an initrd."
110 so_regex
='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
111 [[ $line =~
$so_regex ]] ||
continue
112 FILE
=${BASH_REMATCH[1]}
113 [[ -f ${initdir}$FILE ]] && continue
114 # see if we are loading an optimized version of a shared lib.
115 lib_regex
='^(/lib[^/]*).*'
116 if [[ $FILE =~
$lib_regex ]]; then
117 TLIBDIR
=${BASH_REMATCH[1]}
119 # prefer nosegneg libs, then unoptimized ones.
120 for f
in "$TLIBDIR/i686/nosegneg" "$TLIBDIR"; do
121 [[ -f $f/$BASE ]] ||
continue
125 inst_library
"$FILE" "$TLIBDIR/$BASE"
131 inst_simple
"$bin" "$target"
134 # same as above, except for shell scripts.
135 # If your shell script does not start with shebang, it is not a shell script.
137 [[ -f $1 ]] ||
return 1
139 read -r -n 80 line
<"$1"
140 # If debug is set, clean unprintable chars to prevent messing up the term
141 [[ $debug ]] && line
=$
(echo -n "$line" |
tr -c -d '[:print:][:space:]')
142 shebang_regex
='(#! *)(/[^ ]+).*'
143 [[ $line =~
$shebang_regex ]] ||
return 1
144 inst
"${BASH_REMATCH[2]}" && inst_simple
"$@"
147 # same as above, but specialized for symlinks
149 local src
=$1 target
=$initdir${2:-$1} realsrc
150 [[ -L $1 ]] ||
return 1
151 [[ -L $target ]] && return 0
152 realsrc
=$
(readlink
-f "$src")
153 [[ $realsrc = ${realsrc##*/} ]] && realsrc
="${src%/*}/$realsrc"
154 inst
"$realsrc" && ln -s "$realsrc" "$target"
157 # find a rule in the usual places.
159 [[ -f $1 ]] && { echo "$1"; return 0; }
160 for r
in .
/lib
/udev
/rules.d
/etc
/udev
/rules.d
$dsrc/rules.d
; do
161 [[ -f $r/$1 ]] && { echo "$r/$1"; return 0; }
166 # udev rules always get installed in the same place, so
167 # create a function to install them to make life simpler.
169 local target
="/etc/udev/rules.d"
170 mkdir
-p "$initdir/lib/udev/rules.d" "$initdir$target"
172 rule
=$
(find_rule
$rule) && \
173 inst_simple
"$rule" "$target/${rule##*/}"
177 # general purpose installation function
178 # Same args as above.
180 if (($# != 1 && $# != 2 )); then
181 derror
"inst only takes 1 or 2 arguments"
184 for x
in inst_symlink inst_script inst_binary inst_simple
; do
190 # install function specialized for hooks
191 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
192 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
194 if ! [[ -f $3 ]]; then
195 derror
"Cannot install a hook ($3) that does not exist."
196 derror
"Aborting initrd creation."
198 elif ! strstr
"$hookdirs" "$1"; then
199 derror
"No such hook type $1. Aborting initrd creation."
202 inst_simple
"$3" "/${1}/${2}${3##*/}"
208 # Might be nice to optionally install a binary
209 if [ "$1" == "-o" ]; then
218 if [ "$optional" == "yes" ]; then
219 dwarning
"Skipping program $1 as it cannot be found and is flagged to be optional"
223 derror
"Failed to install $1"; exit 1
228 check_module_deps
() {
230 # if we are already set to be loaded, we do not have to be checked again.
231 strstr
"$mods_to_load" " $1 "
232 # turn a module name into a directory, if we can.
233 moddir
=$
(echo ${dsrc}/modules.d
/??
${1})
234 [[ -d $moddir && -x $moddir/install ]] ||
return 1
235 # if we do not have a check script, we are unconditionally included
236 if [[ -x $moddir/check
]]; then
239 # a return value of 255 = load module only as a dependency.
240 ((ret
==0||ret
==255)) ||
return 1
241 for dep
in $
("$moddir/check" -d); do
242 check_module_deps
"$dep" && continue
243 dwarning
"Dependency $mod failed."
250 should_source_module
() {
252 [[ -x $1/install ]] ||
return 1
253 if [[ "$kernel_only" = "yes" ]]; then
254 [[ -x $1/installkernel
]] && return 0
257 [[ -x $1/check
]] ||
return 0
258 "$1/check" $hostonly ||
return 1
259 for dep
in $
("$1/check" -d); do
260 check_module_deps
"$dep" && continue
261 dwarning
"Cannot load $mod, dependencies failed."
267 for moddir
in "$dsrc/modules.d"/[0-9][0-9]*; do
268 local mod
=${moddir##*/}; mod
=${mod#[0-9][0-9]}
269 # If we are already scheduled to be loaded, no need to check again.
270 strstr
"$mods_to_load" " $mod " && continue
271 # This should never happen, but...
272 [[ -d $moddir ]] ||
continue
273 [[ $dracutmodules != all
]] && ! strstr
"$dracutmodules" "$mod" && \
275 strstr
"$omit_dracutmodules" "$mod" && continue
276 if ! strstr
"$add_dracutmodules" "$mod"; then
277 should_source_module
"$moddir" ||
continue
279 mods_to_load
+=" $mod "
283 # install kernel modules, and handle installing all their dependencies as well.
285 [[ "$no_kernel" = "yes" ]] && return
286 local mod mpargs modpath modname cmd
290 =*) # This introduces 2 incompatible meanings for =* arguments
291 # to instmods. We need to decide which one to keep.
292 if [ "$mod" = "=ata" -a -f $srcmods/modules.block
] ; then
293 instmods
$mpargs $
(egrep 'ata|ahci' "${srcmods}/modules.block")
294 elif [ -f $srcmods/modules.
${mod#=} ]; then
295 instmods
$mpargs $
(cat ${srcmods}/modules.
${mod#=} )
297 instmods
$mpargs $
(find "$srcmods" -path "*/${mod#=}/*")
300 --*) mpargs
+=" $mod";;
302 # Must never run this diagnostic-only module
306 # if we are already installed, skip this module and go on
308 [[ -f $initdir/$1 ]] && { shift; continue; }
309 # If we are building a host-specific initramfs and this
310 # module is not already loaded, move on to the next one.
311 [[ $hostonly ]] && ! grep -q "$mod" /proc
/modules
&& {
314 modprobe
$mpargs --ignore-install --set-version $kernel -d ${srcmods%%/lib/modules/*}/ \
315 --show-depends $mod 2>/dev
/null | \
316 while read cmd modpath options
; do
317 [[ $cmd = insmod
]] ||
continue
318 modname
=${modpath##*/}
319 modname
=${modname%.ko}
320 if [[ ${mod/-/_} != ${modname/-/_} ]]; then
321 dinfo
"Installing dependencies for $mod ($modpath)"
322 instmods
$mpargs $modname
324 inst_simple
"$modpath" "/lib/modules/$kernel/${modpath##*/lib/modules/$kernel/}"
325 for fw
in $
(modinfo
-k $kernel -F firmware
$modpath 2>/dev
/null
); do
328 for fwdir
in $fw_dir; do
329 if [ -d "$fwdir" -a -f $fwdir/$fw ]; then
330 inst_simple
"$fwdir/$fw" "/lib/firmware/$fw"
334 if [ "$found" != "yes" ]; then
335 dwarning
"Possible missing firmware ${fw} for module ${mod}.ko"
345 # vim:ts=8:sw=4:sts=4:et