do not use grep to test if sysroot is mounted
[dracut.git] / dracut-functions
blobf5f24d8917109906f01dcad704b380dce528e3d7
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 # $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.
35 inst_simple() {
36 local src target
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
47 # is referenced.
48 inst_library() {
49 local src=$1 dest=${2:-$1}
50 [[ -f $initdir$dest ]] && return 0
51 if [[ -L $src ]]; then
52 reallib="$(readlink -f "$src")"
53 lib=${src##*/}
54 inst_simple "$reallib" "$reallib"
55 mkdir -p ${initdir}${dest%/*}
56 (cd "${initdir}${dest%/*}" && ln -s "$reallib" "$lib")
57 else
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.
64 find_binary() {
65 local binpath="/bin /sbin /usr/bin /usr/sbin" p
66 [[ -x $1 ]] && { echo $1; return 0; }
67 for p in $binpath; do
68 [[ -x $p/$1 ]] && { echo "$p/$1"; return 0; }
69 done
70 return 1
73 # Same as above, but specialized to install binary executables.
74 # Install binary executable, and all shared library dependencies, if any.
75 inst_binary() {
76 local bin target
77 bin=$(find_binary "$1") || return 1
78 target=${2:-$bin}
79 local LDSO NAME IO FILE ADDR I1 n f TLIBDIR
80 [[ -f $initdir$target ]] && return 0
81 # I love bash!
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
88 exit 1
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]}
96 BASE="${FILE##*/}"
97 # prefer nosegneg libs, then unoptimized ones.
98 for f in "$TLIBDIR/i686/nosegneg" "$TLIBDIR"; do
99 [[ -f $f/$BASE ]] || continue
100 FILE="$f/$BASE"
101 break
102 done
103 inst_library "$FILE" "$TLIBDIR/$BASE"
104 IF_dynamic="yes"
105 continue
107 inst_library "$FILE"
108 done
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.
114 inst_script() {
115 [[ -f $1 ]] || return 1
116 local line
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
123 inst_symlink() {
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.
133 find_rule() {
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; }
136 done
137 return 1
140 # udev rules always get installed in the same place, so
141 # create a function to install them to make life simpler.
142 inst_rules() {
143 local target="/lib/udev/rules.d"
144 [[ -d $target ]] || target="/etc/udev/rules.d"
145 for rule in "$@"; do
146 rule=$(find_rule $rule) && \
147 inst_simple "$rule" "$target/${rule##*/}"
148 done
151 # general purpose installation function
152 # Same args as above.
153 inst() {
154 if (($# != 1 && $# != 2 )); then
155 echo "dracut error: inst only takes 1 or 2 arguments"
156 exit 1
158 for x in inst_symlink inst_script inst_binary inst_simple; do
159 $x "$@" && return 0
160 done
161 return 1
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.
167 inst_hook() {
168 [[ -f $3 ]] || {
169 echo "Cannot install a hook ($3) that does not exist." >&2
170 echo "Aborting initrd creation." >&2
171 exit 1
173 strstr "$hookdirs" "$1" || {
174 echo "No such hook type $1. Aborting initrd creation." >&2
175 exit 1
177 inst_simple "$3" "/${1}/${2}${3##*/}"
180 dracut_install() {
181 while (($# > 0)); do
182 if inst "$1" ; then
183 shift
184 continue
186 echo "Failed to install $1" >&2 ; exit 1
187 done
190 # install modules, and handle installing all their dependencies as well.
191 srcmods="/lib/modules/$kernel/"
192 instmods() {
193 local mod mpargs modpath modname cmd
194 while (($# > 0)); do
195 mod=${1%.ko}
196 case $mod in
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#=} )
203 else
204 instmods $mpargs $(find "$srcmods" -path "*/${mod#=}/*")
207 --*) mpargs+=" $mod";;
208 *) mod=${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
218 continue
220 inst_simple "$modpath"
221 done
223 esac
224 shift
225 done
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"
230 done
233 # vim:ts=8:sw=4:sts=4:et