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/>.
21 # Peter Jones <pjones@redhat.com>
22 # Jeremy Katz <katzj@redhat.com>
23 # Jakub Jelinek <jakub@redhat.com>
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
42 [[ -w "$dracutlogfile" ]] && echo "W: $@" >>"$dracutlogfile"
46 [[ $beverbose ]] && echo "I: $@" >&2
47 [[ -w "$dracutlogfile" ]] && echo "I: $@" >>"$dracutlogfile"
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.
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
73 local src
=$1 dest
=${2:-$1}
74 [[ -f $initdir$dest ]] && return 0
75 if [[ -L $src ]]; then
76 reallib
="$(readlink -f "$src")"
78 inst_simple
"$reallib" "$reallib"
79 mkdir
-p ${initdir}${dest%/*}
80 (cd "${initdir}${dest%/*}" && ln -s "$reallib" "$lib")
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.
89 local binpath
="/bin /sbin /usr/bin /usr/sbin" p
90 [[ -x $1 ]] && { echo $1; return 0; }
92 [[ -x $p/$1 ]] && { echo "$p/$1"; return 0; }
97 # Same as above, but specialized to install binary executables.
98 # Install binary executable, and all shared library dependencies, if any.
101 bin
=$
(find_binary
"$1") ||
return 1
103 local LDSO NAME IO FILE ADDR I1 n f TLIBDIR
104 [[ -f $initdir$target ]] && return 0
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."
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]}
121 # prefer nosegneg libs, then unoptimized ones.
122 for f
in "$TLIBDIR/i686/nosegneg" "$TLIBDIR"; do
123 [[ -f $f/$BASE ]] ||
continue
127 inst_library
"$FILE" "$TLIBDIR/$BASE"
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.
139 [[ -f $1 ]] ||
return 1
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
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.
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; }
167 # udev rules always get installed in the same place, so
168 # create a function to install them to make life simpler.
170 local target
="/etc/udev/rules.d"
171 mkdir
-p "$initdir/lib/udev/rules.d" "$initdir$target"
173 rule
=$
(find_rule
$rule) && \
174 inst_simple
"$rule" "$target/${rule##*/}"
178 # general purpose installation function
179 # Same args as above.
181 if (($# != 1 && $# != 2 )); then
182 derror
"inst only takes 1 or 2 arguments"
185 for x
in inst_symlink inst_script inst_binary inst_simple
; do
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.
195 if ! [[ -f $3 ]]; then
196 derror
"Cannot install a hook ($3) that does not exist."
197 derror
"Aborting initrd creation."
199 elif ! strstr
"$hookdirs" "$1"; then
200 derror
"No such hook type $1. Aborting initrd creation."
203 inst_simple
"$3" "/${1}/${2}${3##*/}"
212 derror
"Failed to install $1"; exit 1
216 check_module_deps
() {
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
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."
238 should_source_module
() {
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."
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" && \
259 strstr
"$omit_dracutmodules" "$mod" && continue
260 if ! strstr
"$add_dracutmodules" "$mod"; then
261 should_source_module
"$moddir" ||
continue
263 mods_to_load
+=" $mod "
267 # install kernel modules, and handle installing all their dependencies as well.
269 local mod mpargs modpath modname cmd
270 local srcmods
="/lib/modules/$kernel/"
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#=} )
281 instmods
$mpargs $
(find "$srcmods" -path "*/${mod#=}/*")
284 --*) mpargs
+=" $mod";;
286 # Must never run this diagnostic-only module
290 # if we are already installed, skip this module and go on
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
&& {
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"
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"
314 dwarning
"Possible missing firmware /lib/firmware/${fw} for module ${mod}.ko"
323 # vim:ts=8:sw=4:sts=4:et