Makefile: add debug module to the testimage target
[dracut/plouj.git] / dracut-functions
blob43373a089f8408bda1d2e00e376295f1bef46675
1 #!/bin/bash
3 # functions used by mkinitrd and other tools.
5 # Copyright 2005-2008 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/>.
20 # Authors:
21 # Peter Jones <pjones@redhat.com>
22 # Jeremy Katz <katzj@redhat.com>
23 # Jakub Jelinek <jakub@redhat.com>
25 IF_RTLD=""
26 IF_dynamic=""
28 # Generic substring function. If $2 is in $1, return 0.
29 strstr() { [[ ! ${1#*$2*} = $1 ]]; }
31 # Log initrd creation.
32 if ! [[ $dracutlogfile ]]; then
33 [[ $dsrc = /usr/share/dracut ]] && \
34 dracutlogfile=/var/log/dracut.log || \
35 dracutlogfile=/tmp/dracut.log
36 [[ -w "$dracutlogfile" ]] || dracutlogfile=/tmp/dracut.log
37 >"$dracutlogfile"
40 dwarning() {
41 echo "W: $@" >&2
42 [[ -w "$dracutlogfile" ]] && echo "W: $@" >>"$dracutlogfile"
45 dinfo() {
46 [[ $beverbose ]] && echo "I: $@" >&2
47 [[ -w "$dracutlogfile" ]] && echo "I: $@" >>"$dracutlogfile"
50 derror() {
51 echo "E: $@" >&2
52 [[ -w "$dracutlogfile" ]] && echo "E: $@" >>"$dracutlogfile"
55 # $1 = file to copy to ramdisk
56 # $2 (optional) Name for the file on the ramdisk
57 # Location of the image dir is assumed to be $initdir
58 # We never overwrite the target if it exists.
59 inst_simple() {
60 local src target
61 [[ -f $1 ]] || return 1
62 src=$1 target="${initdir}${2:-$1}"
63 [[ -f $target ]] && return 0
64 mkdir -p "${target%/*}"
65 dinfo "Installing $src"
66 cp -fL "$src" "$target"
69 # Same as above, but specialzed to handle dynamic libraries.
70 # It handles making symlinks according to how the original library
71 # is referenced.
72 inst_library() {
73 local src=$1 dest=${2:-$1}
74 [[ -f $initdir$dest ]] && return 0
75 if [[ -L $src ]]; then
76 reallib="$(readlink -f "$src")"
77 lib=${src##*/}
78 inst_simple "$reallib" "$reallib"
79 mkdir -p ${initdir}${dest%/*}
80 (cd "${initdir}${dest%/*}" && ln -s "$reallib" "$lib")
81 else
82 inst_simple "$src" "$dest"
86 # find a binary. If we were not passed the full path directly,
87 # search in the usual places to find the binary.
88 find_binary() {
89 local binpath="/bin /sbin /usr/bin /usr/sbin" p
90 [[ -x $1 ]] && { echo $1; return 0; }
91 for p in $binpath; do
92 [[ -x $p/$1 ]] && { echo "$p/$1"; return 0; }
93 done
94 return 1
97 # Same as above, but specialized to install binary executables.
98 # Install binary executable, and all shared library dependencies, if any.
99 inst_binary() {
100 local bin target
101 bin=$(find_binary "$1") || return 1
102 target=${2:-$bin}
103 local LDSO NAME IO FILE ADDR I1 n f TLIBDIR
104 [[ -f $initdir$target ]] && return 0
105 # I love bash!
106 ldd $bin 2>/dev/null | while read line; do
107 [[ $line = 'not a dynamic executable' ]] && return 1
108 if [[ $line =~ not\ found ]]; then
109 derror "Missing a shared library required by $bin."
110 derror "Run \"ldd $bin\" to find out what it is."
111 derror "dracut cannot create an initrd."
112 exit 1
114 [[ $line =~ ([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*) ]] || continue
115 FILE=${BASH_REMATCH[1]}
116 [[ -f ${initdir}$FILE ]] && continue
117 # see if we are loading an optimized version of a shared lib.
118 if [[ $FILE =~ ^(/lib[^/]*).* ]]; then
119 TLIBDIR=${BASH_REMATCH[1]}
120 BASE="${FILE##*/}"
121 # prefer nosegneg libs, then unoptimized ones.
122 for f in "$TLIBDIR/i686/nosegneg" "$TLIBDIR"; do
123 [[ -f $f/$BASE ]] || continue
124 FILE="$f/$BASE"
125 break
126 done
127 inst_library "$FILE" "$TLIBDIR/$BASE"
128 IF_dynamic="yes"
129 continue
131 inst_library "$FILE"
132 done
133 inst_simple "$bin" "$target"
136 # same as above, except for shell scripts.
137 # If your shell script does not start with shebang, it is not a shell script.
138 inst_script() {
139 [[ -f $1 ]] || return 1
140 local line
141 read -r -n 80 line <"$1"
142 # If debug is set, clean unprintable chars to prevent messing up the term
143 [[ $debug ]] && line=$(echo -n "$line" | tr -c -d '[:print:][:space:]')
144 [[ $line =~ (#! *)(/[^ ]+).* ]] || return 1
145 inst "${BASH_REMATCH[2]}" && inst_simple "$@"
148 # same as above, but specialized for symlinks
149 inst_symlink() {
150 local src=$1 target=$initdir${2:-$1} realsrc
151 [[ -L $1 ]] || return 1
152 [[ -L $target ]] && return 0
153 realsrc=$(readlink -f "$src")
154 [[ $realsrc = ${realsrc##*/} ]] && realsrc="${src%/*}/$realsrc"
155 inst "$realsrc" && ln -s "$realsrc" "$target"
158 # find a rule in the usual places.
159 find_rule() {
160 [[ -f $1 ]] && { echo "$1"; return 0; }
161 for r in . /lib/udev/rules.d /etc/udev/rules.d $dsrc/rules.d; do
162 [[ -f $r/$1 ]] && { echo "$r/$1"; return 0; }
163 done
164 return 1
167 # udev rules always get installed in the same place, so
168 # create a function to install them to make life simpler.
169 inst_rules() {
170 local target="/etc/udev/rules.d"
171 mkdir -p "$initdir/lib/udev/rules.d" "$initdir$target"
172 for rule in "$@"; do
173 rule=$(find_rule $rule) && \
174 inst_simple "$rule" "$target/${rule##*/}"
175 done
178 # general purpose installation function
179 # Same args as above.
180 inst() {
181 if (($# != 1 && $# != 2 )); then
182 derror "inst only takes 1 or 2 arguments"
183 exit 1
185 for x in inst_symlink inst_script inst_binary inst_simple; do
186 $x "$@" && return 0
187 done
188 return 1
191 # install function specialized for hooks
192 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
193 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
194 inst_hook() {
195 if ! [[ -f $3 ]]; then
196 derror "Cannot install a hook ($3) that does not exist."
197 derror "Aborting initrd creation."
198 exit 1
199 elif ! strstr "$hookdirs" "$1"; then
200 derror "No such hook type $1. Aborting initrd creation."
201 exit 1
203 inst_simple "$3" "/${1}/${2}${3##*/}"
206 dracut_install() {
207 while (($# > 0)); do
208 if inst "$1" ; then
209 shift
210 continue
212 derror "Failed to install $1"; exit 1
213 done
216 check_module_deps() {
217 local moddir dep ret
218 # if we are already set to be loaded, we do not have to be checked again.
219 strstr "$mods_to_load" " $1 "
220 # turn a module name into a directory, if we can.
221 moddir=$(echo ${dsrc}/modules.d/??${1})
222 [[ -d $moddir && -x $moddir/install ]] || return 1
223 # if we do not have a check script, we are unconditionally included
224 if [[ -x $moddir/check ]]; then
225 "$moddir/check"
226 ret=$?
227 # a return value of 255 = load module only as a dependency.
228 ((ret==0||ret==255)) || return 1
229 for dep in $("$moddir/check" -d); do
230 check_module_deps "$dep" && continue
231 dwarning "Dependency $mod failed."
232 return 1
233 done
235 mods_to_load+=" $1 "
238 should_source_module() {
239 local dep
240 [[ -x $1/install ]] || return 1
241 [[ -x $1/check ]] || return 0
242 "$1/check" $hostonly || return 1
243 for dep in $("$1/check" -d); do
244 check_module_deps "$dep" && continue
245 dwarning "Cannot load $mod, dependencies failed."
246 return 1
247 done
250 check_modules() {
251 for moddir in "$dsrc/modules.d"/[0-9][0-9]*; do
252 local mod=${moddir##*/}; mod=${mod#[0-9][0-9]}
253 # If we are already scheduled to be loaded, no need to check again.
254 strstr "$mods_to_load" " $mod " && continue
255 # This should never happen, but...
256 [[ -d $moddir ]] || continue
257 [[ $dracutmodules != all ]] && ! strstr "$dracutmodules" "$mod" && \
258 continue
259 strstr "$omit_dracutmodules" "$mod" && continue
260 if ! strstr "$add_dracutmodules" "$mod"; then
261 should_source_module "$moddir" || continue
263 mods_to_load+=" $mod "
264 done
267 # install kernel modules, and handle installing all their dependencies as well.
268 instmods() {
269 local mod mpargs modpath modname cmd
270 local srcmods="/lib/modules/$kernel/"
271 while (($# > 0)); do
272 mod=${1%.ko}
273 case $mod in
274 =*) # This introduces 2 incompatible meanings for =* arguments
275 # to instmods. We need to decide which one to keep.
276 if [ "$mod" = "=ata" -a -f $srcmods/modules.block ] ; then
277 instmods $mpargs $(egrep 'ata|ahci' "${srcmods}/modules.block")
278 elif [ -f $srcmods/modules.${mod#=} ]; then
279 instmods $mpargs $(cat ${srcmods}/modules.${mod#=} )
280 else
281 instmods $mpargs $(find "$srcmods" -path "*/${mod#=}/*")
284 --*) mpargs+=" $mod";;
285 i2o_scsi)
286 # Must never run this diagnostic-only module
287 shift; continue;
289 *) mod=${mod##*/}
290 # if we are already installed, skip this module and go on
291 # to the next one.
292 [[ -f $initdir/$1 ]] && { shift; continue; }
293 # If we are building a host-specific initramfs and this
294 # module is not already loaded, move on to the next one.
295 [[ $hostonly ]] && ! grep -q "$mod" /proc/modules && {
296 shift; continue;
298 modprobe $mpargs --ignore-install --set-version $kernel \
299 --show-depends $mod 2>/dev/null | \
300 while read cmd modpath options; do
301 [[ $cmd = insmod ]] || continue
302 modname=${modpath##*/}
303 modname=${modname%.ko}
304 if [[ ${mod/-/_} != ${modname/-/_} ]]; then
305 dinfo "Installing dependencies for $mod ($modpath)"
306 instmods $mpargs $modname
308 inst_simple "$modpath"
309 done
310 for fw in $(modinfo -F firmware $mod 2>/dev/null); do
311 if [[ -f /lib/firmware/$fw ]]; then
312 inst_simple "/lib/firmware/$fw"
313 else
314 dwarning "Possible missing firmware /lib/firmware/${fw} for module ${mod}.ko"
316 done
318 esac
319 shift
320 done
323 # vim:ts=8:sw=4:sts=4:et