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 -fL "$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 [[ -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 [[ $line =~
([^
]*/lib
[^
/]*/[^
]*\.so
[^
]*) ]] ||
continue
111 FILE
=${BASH_REMATCH[1]}
112 [[ -f ${initdir}$FILE ]] && continue
113 # see if we are loading an optimized version of a shared lib.
114 if [[ $FILE =~ ^
(/lib
[^
/]*).
* ]]; then
115 TLIBDIR
=${BASH_REMATCH[1]}
117 # prefer nosegneg libs, then unoptimized ones.
118 for f
in "$TLIBDIR/i686/nosegneg" "$TLIBDIR"; do
119 [[ -f $f/$BASE ]] ||
continue
123 inst_library
"$FILE" "$TLIBDIR/$BASE"
129 inst_simple
"$bin" "$target"
132 # same as above, except for shell scripts.
133 # If your shell script does not start with shebang, it is not a shell script.
135 [[ -f $1 ]] ||
return 1
137 read -r -n 80 line
<"$1"
138 # If debug is set, clean unprintable chars to prevent messing up the term
139 [[ $debug ]] && line
=$
(echo -n "$line" |
tr -c -d '[:print:][:space:]')
140 [[ $line =~
(#! *)(/[^ ]+).* ]] || return 1
141 inst
"${BASH_REMATCH[2]}" && inst_simple
"$@"
144 # same as above, but specialized for symlinks
146 local src
=$1 target
=$initdir${2:-$1} realsrc
147 [[ -L $1 ]] ||
return 1
148 [[ -L $target ]] && return 0
149 realsrc
=$
(readlink
-f "$src")
150 [[ $realsrc = ${realsrc##*/} ]] && realsrc
="${src%/*}/$realsrc"
151 inst
"$realsrc" && ln -s "$realsrc" "$target"
154 # find a rule in the usual places.
156 [[ -f $1 ]] && { echo "$1"; return 0; }
157 for r
in .
/lib
/udev
/rules.d
/etc
/udev
/rules.d
$dsrc/rules.d
; do
158 [[ -f $r/$1 ]] && { echo "$r/$1"; return 0; }
163 # udev rules always get installed in the same place, so
164 # create a function to install them to make life simpler.
166 local target
="/etc/udev/rules.d"
167 mkdir
-p "$initdir/lib/udev/rules.d" "$initdir$target"
169 rule
=$
(find_rule
$rule) && \
170 inst_simple
"$rule" "$target/${rule##*/}"
174 # general purpose installation function
175 # Same args as above.
177 if (($# != 1 && $# != 2 )); then
178 derror
"inst only takes 1 or 2 arguments"
181 for x
in inst_symlink inst_script inst_binary inst_simple
; do
187 # install function specialized for hooks
188 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
189 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
191 if ! [[ -f $3 ]]; then
192 derror
"Cannot install a hook ($3) that does not exist."
193 derror
"Aborting initrd creation."
195 elif ! strstr
"$hookdirs" "$1"; then
196 derror
"No such hook type $1. Aborting initrd creation."
199 inst_simple
"$3" "/${1}/${2}${3##*/}"
205 # Might be nice to optionally install a binary
206 if [ "$1" == "-o" ]; then
215 if [ "$optional" == "yes" ]; then
216 dwarning
"Skipping program $1 as it cannot be found and is flagged to be optional"
220 derror
"Failed to install $1"; exit 1
225 check_module_deps
() {
227 # if we are already set to be loaded, we do not have to be checked again.
228 strstr
"$mods_to_load" " $1 "
229 # turn a module name into a directory, if we can.
230 moddir
=$
(echo ${dsrc}/modules.d
/??
${1})
231 [[ -d $moddir && -x $moddir/install ]] ||
return 1
232 # if we do not have a check script, we are unconditionally included
233 if [[ -x $moddir/check
]]; then
236 # a return value of 255 = load module only as a dependency.
237 ((ret
==0||ret
==255)) ||
return 1
238 for dep
in $
("$moddir/check" -d); do
239 check_module_deps
"$dep" && continue
240 dwarning
"Dependency $mod failed."
247 should_source_module
() {
249 [[ -x $1/install ]] ||
return 1
250 if [[ "$kernel_only" = "yes" ]]; then
251 [[ -x $1/installkernel
]] && return 0
254 [[ -x $1/check
]] ||
return 0
255 "$1/check" $hostonly ||
return 1
256 for dep
in $
("$1/check" -d); do
257 check_module_deps
"$dep" && continue
258 dwarning
"Cannot load $mod, dependencies failed."
264 for moddir
in "$dsrc/modules.d"/[0-9][0-9]*; do
265 local mod
=${moddir##*/}; mod
=${mod#[0-9][0-9]}
266 # If we are already scheduled to be loaded, no need to check again.
267 strstr
"$mods_to_load" " $mod " && continue
268 # This should never happen, but...
269 [[ -d $moddir ]] ||
continue
270 [[ $dracutmodules != all
]] && ! strstr
"$dracutmodules" "$mod" && \
272 strstr
"$omit_dracutmodules" "$mod" && continue
273 if ! strstr
"$add_dracutmodules" "$mod"; then
274 should_source_module
"$moddir" ||
continue
276 mods_to_load
+=" $mod "
280 # install kernel modules, and handle installing all their dependencies as well.
282 [[ "$no_kernel" = "yes" ]] && return
283 local mod mpargs modpath modname cmd
287 =*) # This introduces 2 incompatible meanings for =* arguments
288 # to instmods. We need to decide which one to keep.
289 if [ "$mod" = "=ata" -a -f $srcmods/modules.block
] ; then
290 instmods
$mpargs $
(egrep 'ata|ahci' "${srcmods}/modules.block")
291 elif [ -f $srcmods/modules.
${mod#=} ]; then
292 instmods
$mpargs $
(cat ${srcmods}/modules.
${mod#=} )
294 instmods
$mpargs $
(find "$srcmods" -path "*/${mod#=}/*")
297 --*) mpargs
+=" $mod";;
299 # Must never run this diagnostic-only module
303 # if we are already installed, skip this module and go on
305 [[ -f $initdir/$1 ]] && { shift; continue; }
306 # If we are building a host-specific initramfs and this
307 # module is not already loaded, move on to the next one.
308 [[ $hostonly ]] && ! grep -q "$mod" /proc
/modules
&& {
311 modprobe
$mpargs --ignore-install --set-version $kernel -d ${srcmods%%/lib/modules/*}/ \
312 --show-depends $mod 2>/dev
/null | \
313 while read cmd modpath options
; do
314 [[ $cmd = insmod
]] ||
continue
315 modname
=${modpath##*/}
316 modname
=${modname%.ko}
317 if [[ ${mod/-/_} != ${modname/-/_} ]]; then
318 dinfo
"Installing dependencies for $mod ($modpath)"
319 instmods
$mpargs $modname
321 inst_simple
"$modpath" "/lib/modules/$kernel/${modpath##*/lib/modules/$kernel/}"
322 for fw
in $
(modinfo
-k $kernel -F firmware
$modpath 2>/dev
/null
); do
325 for fwdir
in $fw_dir; do
326 if [ -d "$fwdir" -a -f $fwdir/$fw ]; then
327 inst_simple
"$fwdir/$fw" "/lib/firmware/$fw"
331 if [ "$found" != "yes" ]; then
332 dwarning
"Possible missing firmware ${fw} for module ${mod}.ko"
342 # vim:ts=8:sw=4:sts=4:et