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 # $1 = file to copy to ramdisk
32 # $2 (optional) Name for the file on the ramdisk
33 # Location of the image dir is assumed to be $initdir
34 # We never overwrite the target if it exists.
37 [[ -f $1 ]] ||
return 1
38 src
=$1 target
="${initdir}${2:-$1}"
39 [[ -f $target ]] && return 0
40 mkdir
-p "${target%/*}"
41 echo "Installing $src" >&2
42 cp -fL "$src" "$target"
45 # Same as above, but specialzed to handle dynamic libraries.
46 # It handles making symlinks according to how the original library
49 local src
=$1 dest
=${2:-$1}
50 [[ -f $initdir$dest ]] && return 0
51 if [[ -L $src ]]; then
52 reallib
="$(readlink -f "$src")"
54 inst_simple
"$reallib" "$reallib"
55 mkdir
-p ${initdir}${dest%/*}
56 (cd "${initdir}${dest%/*}" && ln -s "$reallib" "$lib")
58 inst_simple
"$src" "$dest"
62 # find a binary. If we were not passed the full path directly,
63 # search in the usual places to find the binary.
65 local binpath
="/bin /sbin /usr/bin /usr/sbin" p
66 [[ -x $1 ]] && { echo $1; return 0; }
68 [[ -x $p/$1 ]] && { echo "$p/$1"; return 0; }
73 # Same as above, but specialized to install binary executables.
74 # Install binary executable, and all shared library dependencies, if any.
77 bin
=$
(find_binary
"$1") ||
return 1
79 local LDSO NAME IO FILE ADDR I1 n f TLIBDIR
80 [[ -f $initdir$target ]] && return 0
82 ldd
$bin 2>/dev
/null |
while read line
; do
83 [[ $line = 'not a dynamic executable' ]] && return 1
84 [[ $line =~ not\ found
]] &&{
85 echo "Missing a shared library required by $bin." >&2
86 echo "Run \"ldd $bin\" to find out what it is." >&2
87 echo "dracut cannot create an initrd." >&2
90 [[ $line =~
([^
]*/lib
[^
/]*/[^
]*\.so
[^
]*) ]] ||
continue
91 FILE
=${BASH_REMATCH[1]}
92 [[ -f ${initdir}$FILE ]] && continue
93 # see if we are loading an optimized version of a shared lib.
94 [[ $FILE =~ ^
(/lib
[^
/]*).
* ]] && {
95 TLIBDIR
=${BASH_REMATCH[1]}
97 # prefer nosegneg libs, then unoptimized ones.
98 for f
in "$TLIBDIR/i686/nosegneg" "$TLIBDIR"; do
99 [[ -f $f/$BASE ]] ||
continue
103 inst_library
"$FILE" "$TLIBDIR/$BASE"
109 inst_simple
"$bin" "$target"
112 # same as above, except for shell scripts.
113 # If your shell script does not start with shebang, it is not a shell script.
115 [[ -f $1 ]] ||
return 1
117 read -r -n 80 line
<"$1"
118 [[ $line =~
(#! *)(/[^ ]+).* ]] || return 1
119 inst
"${BASH_REMATCH[2]}" && inst_simple
"$@"
122 # same as above, but specialized for symlinks
124 local src
=$1 target
=$initdir${2:-$1} realsrc
125 [[ -L $1 ]] ||
return 1
126 [[ -L $target ]] && return 0
127 realsrc
=$
(readlink
-f "$src")
128 [[ $realsrc = ${realsrc##*/} ]] && realsrc
="${src%/*}/$realsrc"
129 inst
"$realsrc" && ln -s "$realsrc" "$target"
132 # find a rule in the usual places.
134 for r
in .
/lib
/udev
/rules.d
/etc
/udev
/rules.d
$dsrc/rules.d
; do
135 [[ -f $r/$1 ]] && { echo "$r/$1"; return 0; }
140 # udev rules always get installed in the same place, so
141 # create a function to install them to make life simpler.
143 local target
="/lib/udev/rules.d"
144 [[ -d $target ]] || target
="/etc/udev/rules.d"
146 rule
=$
(find_rule
$rule) && \
147 inst_simple
"$rule" "$target/${rule##*/}"
151 # general purpose installation function
152 # Same args as above.
154 if (($# != 1 && $# != 2 )); then
155 echo "dracut error: inst only takes 1 or 2 arguments"
158 for x
in inst_symlink inst_script inst_binary inst_simple
; do
164 # install function specialized for hooks
165 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
166 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
169 echo "Cannot install a hook ($3) that does not exist." >&2
170 echo "Aborting initrd creation." >&2
173 strstr
"$hookdirs" "$1" ||
{
174 echo "No such hook type $1. Aborting initrd creation." >&2
177 inst_simple
"$3" "/${1}/${2}${3##*/}"
186 echo "Failed to install $1" >&2 ; exit 1
190 # install modules, and handle installing all their dependencies as well.
191 srcmods
="/lib/modules/$kernel/"
193 local mod mpargs modpath modname cmd
197 =*) # This introduces 2 incompatible meanings for =* arguments
198 # to instmods. We need to decide which one to keep.
199 if [ "$mod" = "=ata" -a -f $srcmods/modules.block
] ; then
200 instmods
$mpargs $
(egrep 'ata|ahci' "${srcmods}/modules.block")
201 elif [ -f $srcmods/modules.
${mod#=} ]; then
202 instmods
$mpargs $
(cat ${srcmods}/modules.
${mod#=} )
204 instmods
$mpargs $
(find "$srcmods" -path "*/${mod#=}/*")
207 --*) mpargs
+=" $mod";;
209 [[ -f $initdir/$1 ]] && { shift; continue; }
210 modprobe
$mpargs --ignore-install --set-version $kernel \
211 --show-depends $mod 2>/dev
/null | \
212 while read cmd modpath options
; do
213 [[ $cmd = insmod
]] ||
continue
214 modname
=${modpath##*/}
215 modname
=${modname%.ko}
216 [[ ${mod/-/_} != ${modname/-/_} ]] && {
217 instmods
$mpargs $modname
220 inst_simple
"$modpath"
226 for fw
in $
(/sbin
/modinfo
-F firmware
$mod 2>/dev
/null
); do
227 if [ -f /lib
/firmware
/$fw ]; then
228 inst_simple
"/lib/firmware/$fw"
233 # vim:ts=8:sw=4:sts=4:et