40network/check: output which binary is missing for the network module
[dracut/plouj.git] / dracut-functions
blob97de9b4e557ecf773e5ddb41cd5d658fb02dc423
1 #!/bin/bash
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/>.
21 IF_RTLD=""
22 IF_dynamic=""
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
33 >"$dracutlogfile"
36 dwarning() {
37 echo "W: $@" >&2
38 [[ -w "$dracutlogfile" ]] && echo "W: $@" >>"$dracutlogfile"
41 dinfo() {
42 [[ $beverbose ]] && echo "I: $@" >&2
43 [[ -w "$dracutlogfile" ]] && echo "I: $@" >>"$dracutlogfile"
46 derror() {
47 echo "E: $@" >&2
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.
55 inst_simple() {
56 local src target
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
67 # is referenced.
68 inst_library() {
69 local src=$1 dest=${2:-$1}
70 [[ -f $initdir$dest ]] && return 0
71 if [[ -L $src ]]; then
72 reallib="$(readlink -f "$src")"
73 lib=${src##*/}
74 inst_simple "$reallib" "$reallib"
75 mkdir -p ${initdir}${dest%/*}
76 (cd "${initdir}${dest%/*}" && ln -s "$reallib" "$lib")
77 else
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.
84 find_binary() {
85 local binpath="/bin /sbin /usr/bin /usr/sbin" p
86 [[ -x $1 ]] && { echo $1; return 0; }
87 for p in $binpath; do
88 [[ -x $p/$1 ]] && { echo "$p/$1"; return 0; }
89 done
90 return 1
93 # Same as above, but specialized to install binary executables.
94 # Install binary executable, and all shared library dependencies, if any.
95 inst_binary() {
96 local bin target
97 bin=$(find_binary "$1") || return 1
98 target=${2:-$bin}
99 local LDSO NAME IO FILE ADDR I1 n f TLIBDIR
100 [[ -f $initdir$target ]] && return 0
101 # I love bash!
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."
108 exit 1
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]}
116 BASE="${FILE##*/}"
117 # prefer nosegneg libs, then unoptimized ones.
118 for f in "$TLIBDIR/i686/nosegneg" "$TLIBDIR"; do
119 [[ -f $f/$BASE ]] || continue
120 FILE="$f/$BASE"
121 break
122 done
123 inst_library "$FILE" "$TLIBDIR/$BASE"
124 IF_dynamic="yes"
125 continue
127 inst_library "$FILE"
128 done
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.
134 inst_script() {
135 [[ -f $1 ]] || return 1
136 local line
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
145 inst_symlink() {
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.
155 find_rule() {
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; }
159 done
160 return 1
163 # udev rules always get installed in the same place, so
164 # create a function to install them to make life simpler.
165 inst_rules() {
166 local target="/etc/udev/rules.d"
167 mkdir -p "$initdir/lib/udev/rules.d" "$initdir$target"
168 for rule in "$@"; do
169 rule=$(find_rule $rule) && \
170 inst_simple "$rule" "$target/${rule##*/}"
171 done
174 # general purpose installation function
175 # Same args as above.
176 inst() {
177 if (($# != 1 && $# != 2 )); then
178 derror "inst only takes 1 or 2 arguments"
179 exit 1
181 for x in inst_symlink inst_script inst_binary inst_simple; do
182 $x "$@" && return 0
183 done
184 return 1
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.
190 inst_hook() {
191 if ! [[ -f $3 ]]; then
192 derror "Cannot install a hook ($3) that does not exist."
193 derror "Aborting initrd creation."
194 exit 1
195 elif ! strstr "$hookdirs" "$1"; then
196 derror "No such hook type $1. Aborting initrd creation."
197 exit 1
199 inst_simple "$3" "/${1}/${2}${3##*/}"
202 dracut_install() {
203 local optional=
204 while (($# > 0)); do
205 # Might be nice to optionally install a binary
206 if [ "$1" == "-o" ]; then
207 optional="yes"
208 shift
209 continue
211 if inst "$1" ; then
212 shift
213 continue
215 if [ "$optional" == "yes" ]; then
216 dwarning "Skipping program $1 as it cannot be found and is flagged to be optional"
217 shift
218 continue
219 else
220 derror "Failed to install $1"; exit 1
222 done
225 check_module_deps() {
226 local moddir dep ret
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
234 "$moddir/check"
235 ret=$?
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."
241 return 1
242 done
244 mods_to_load+=" $1 "
247 should_source_module() {
248 local dep
249 [[ -x $1/install ]] || return 1
250 if [[ "$kernel_only" = "yes" ]]; then
251 [[ -x $1/installkernel ]] && return 0
252 return 1
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."
259 return 1
260 done
263 check_modules() {
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" && \
271 continue
272 strstr "$omit_dracutmodules" "$mod" && continue
273 if ! strstr "$add_dracutmodules" "$mod"; then
274 should_source_module "$moddir" || continue
276 mods_to_load+=" $mod "
277 done
280 # install kernel modules, and handle installing all their dependencies as well.
281 instmods() {
282 [[ "$no_kernel" = "yes" ]] && return
283 local mod mpargs modpath modname cmd
284 while (($# > 0)); do
285 mod=${1%.ko}
286 case $mod in
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#=} )
293 else
294 instmods $mpargs $(find "$srcmods" -path "*/${mod#=}/*")
297 --*) mpargs+=" $mod";;
298 i2o_scsi)
299 # Must never run this diagnostic-only module
300 shift; continue;
302 *) mod=${mod##*/}
303 # if we are already installed, skip this module and go on
304 # to the next one.
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 && {
309 shift; continue;
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
323 unset found
324 IFS=:
325 for fwdir in $fw_dir; do
326 if [ -d "$fwdir" -a -f $fwdir/$fw ]; then
327 inst_simple "$fwdir/$fw" "/lib/firmware/$fw"
328 found=yes
330 done
331 if [ "$found" != "yes" ]; then
332 dwarning "Possible missing firmware ${fw} for module ${mod}.ko"
334 done
335 done
337 esac
338 shift
339 done
342 # vim:ts=8:sw=4:sts=4:et