dracut.8: corrections of parameters and fixed typos
[dracut/plouj.git] / dracut-functions
blobd4caacfe8a89cce2384a8318869035374ed979dd
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 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]}
118 BASE="${FILE##*/}"
119 # prefer nosegneg libs, then unoptimized ones.
120 for f in "$TLIBDIR/i686/nosegneg" "$TLIBDIR"; do
121 [[ -f $f/$BASE ]] || continue
122 FILE="$f/$BASE"
123 break
124 done
125 inst_library "$FILE" "$TLIBDIR/$BASE"
126 IF_dynamic="yes"
127 continue
129 inst_library "$FILE"
130 done
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.
136 inst_script() {
137 [[ -f $1 ]] || return 1
138 local line
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
148 inst_symlink() {
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.
158 find_rule() {
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; }
162 done
163 return 1
166 # udev rules always get installed in the same place, so
167 # create a function to install them to make life simpler.
168 inst_rules() {
169 local target="/etc/udev/rules.d"
170 mkdir -p "$initdir/lib/udev/rules.d" "$initdir$target"
171 for rule in "$@"; do
172 rule=$(find_rule $rule) && \
173 inst_simple "$rule" "$target/${rule##*/}"
174 done
177 # general purpose installation function
178 # Same args as above.
179 inst() {
180 if (($# != 1 && $# != 2 )); then
181 derror "inst only takes 1 or 2 arguments"
182 exit 1
184 for x in inst_symlink inst_script inst_binary inst_simple; do
185 $x "$@" && return 0
186 done
187 return 1
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.
193 inst_hook() {
194 if ! [[ -f $3 ]]; then
195 derror "Cannot install a hook ($3) that does not exist."
196 derror "Aborting initrd creation."
197 exit 1
198 elif ! strstr "$hookdirs" "$1"; then
199 derror "No such hook type $1. Aborting initrd creation."
200 exit 1
202 inst_simple "$3" "/${1}/${2}${3##*/}"
205 dracut_install() {
206 local optional=
207 while (($# > 0)); do
208 # Might be nice to optionally install a binary
209 if [ "$1" == "-o" ]; then
210 optional="yes"
211 shift
212 continue
214 if inst "$1" ; then
215 shift
216 continue
218 if [ "$optional" == "yes" ]; then
219 dwarning "Skipping program $1 as it cannot be found and is flagged to be optional"
220 shift
221 continue
222 else
223 derror "Failed to install $1"; exit 1
225 done
228 check_module_deps() {
229 local moddir dep ret
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
237 "$moddir/check"
238 ret=$?
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."
244 return 1
245 done
247 mods_to_load+=" $1 "
250 should_source_module() {
251 local dep
252 [[ -x $1/install ]] || return 1
253 if [[ "$kernel_only" = "yes" ]]; then
254 [[ -x $1/installkernel ]] && return 0
255 return 1
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."
262 return 1
263 done
266 check_modules() {
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" && \
274 continue
275 strstr "$omit_dracutmodules" "$mod" && continue
276 if ! strstr "$add_dracutmodules" "$mod"; then
277 should_source_module "$moddir" || continue
279 mods_to_load+=" $mod "
280 done
283 # install kernel modules, and handle installing all their dependencies as well.
284 instmods() {
285 [[ "$no_kernel" = "yes" ]] && return
286 local mod mpargs modpath modname cmd
287 while (($# > 0)); do
288 mod=${1%.ko}
289 case $mod in
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#=} )
296 else
297 instmods $mpargs $(find "$srcmods" -path "*/${mod#=}/*")
300 --*) mpargs+=" $mod";;
301 i2o_scsi)
302 # Must never run this diagnostic-only module
303 shift; continue;
305 *) mod=${mod##*/}
306 # if we are already installed, skip this module and go on
307 # to the next one.
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 && {
312 shift; continue;
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
326 unset found
327 IFS=:
328 for fwdir in $fw_dir; do
329 if [ -d "$fwdir" -a -f $fwdir/$fw ]; then
330 inst_simple "$fwdir/$fw" "/lib/firmware/$fw"
331 found=yes
333 done
334 if [ "$found" != "yes" ]; then
335 dwarning "Possible missing firmware ${fw} for module ${mod}.ko"
337 done
338 done
340 esac
341 shift
342 done
345 # vim:ts=8:sw=4:sts=4:et