test/MULTINIC: kill server after passing all tests
[dracut.git] / dracut-functions
blob16b7d3de93bb44d56fa64efbe385ad57a4905e56
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 ]]; }
27 # Log initrd creation.
28 if ! [[ $dracutlogfile ]]; then
29 [[ $dracutbasedir = /usr/share/dracut ]] && \
30 dracutlogfile=/var/log/dracut.log || \
31 dracutlogfile=/tmp/dracut.log
32 # [[ -w $dracutlogfile ]] || dracutlogfile=/tmp/dracut.log
33 if [[ -w $dracutlogfile ]]; then
34 >"$dracutlogfile"
38 dwarning() {
39 echo "W: $@" >&2
40 [[ -w $dracutlogfile ]] && echo "W: $@" >>"$dracutlogfile"
43 dinfo() {
44 [[ $beverbose ]] && echo "I: $@" >&2
45 [[ -w $dracutlogfile ]] && echo "I: $@" >>"$dracutlogfile"
48 derror() {
49 echo "E: $@" >&2
50 [[ -w $dracutlogfile ]] && echo "E: $@" >>"$dracutlogfile"
53 get_fs_env() {
54 if [[ -x /lib/udev/vol_id ]]; then
55 eval $(/lib/udev/vol_id --export $1)
56 elif find_binary blkid >/dev/null; then
57 eval $(blkid -o udev $1)
58 else
59 return 1
63 get_fs_type() (
64 get_fs_env $1 || return
65 echo $ID_FS_TYPE
68 get_fs_uuid() (
69 get_fs_env $1 || return
70 echo $ID_FS_UUID
73 # finds the major:minor of the block device backing the root filesystem.
74 find_block_device() {
75 local rootdev blkdev fs type opts misc
76 while read blkdev fs type opts misc; do
77 [[ $blkdev = rootfs ]] && continue # skip rootfs entry
78 [[ $fs = $1 ]] && { rootdev=$blkdev; break; } # we have a winner!
79 done < /proc/mounts
80 [[ -b $rootdev ]] || return 1 # oops, not a block device.
81 # get major/minor for the device
82 ls -nLl "$rootdev" | \
83 (read x x x x maj min x; maj=${maj//,/}; echo $maj:$min)
86 find_root_block_device() { find_block_device /; }
88 # Walk all the slave relationships for a given block device.
89 # Stop when our helper function returns success
90 # $1 = function to call on every found block device
91 # $2 = block device in major:minor format
92 check_block_and_slaves() {
93 local x
94 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
95 "$1" $2 && return
96 check_vol_slaves "$@" && return 0
97 [[ -d /sys/dev/block/$2/slaves ]] || return 1
98 for x in /sys/dev/block/$2/slaves/*/dev; do
99 [[ -f $x ]] || continue
100 check_block_and_slaves $1 $(cat "$x") && return 0
101 done
102 return 1
105 get_numeric_dev() {
106 ls -lH "$1" | { read a b c d maj min rest; printf "%d:%d" ${maj%%,} $min;}
109 # ugly workaround for the lvm design
110 # There is no volume group device,
111 # so, there are no slave devices for volume groups.
112 # Logical volumes only have the slave devices they really live on,
113 # but you cannot create the logical volume without the volume group.
114 # And the volume group might be bigger than the devices the LV needs.
115 check_vol_slaves() {
116 for i in /dev/mapper/*; do
117 lv=$(get_numeric_dev $i)
118 if [[ $lv = $2 ]]; then
119 vg=$(lvm lvs --noheadings -o vg_name $i 2>/dev/null)
120 # strip space
121 vg=$(echo $vg)
122 if [[ $vg ]]; then
123 for pv in $(lvm vgs --noheadings -o pv_name "$vg" 2>/dev/null); \
125 check_block_and_slaves $1 $(get_numeric_dev $pv) \
126 && return 0
127 done
130 done
131 return 1
134 # Install a directory, keeping symlinks as on the original system.
135 # Example: if /lib64 points to /lib on the host, "inst_dir /lib/file"
136 # will create ${initdir}/lib64, ${initdir}/lib64/file,
137 # and a symlink ${initdir}/lib -> lib64.
138 inst_dir() {
139 local dir="$1"
140 [[ -e "${initdir}$dir" ]] && return 0
142 # iterate over parent directories
143 local file=""
144 local IFS="/"
145 for part in $dir; do
146 [ -z "$part" ] && continue
147 file="$file/$part"
148 [[ -e "${initdir}$file" ]] && continue
150 if [ -L "$file" ]; then
151 # create link as the original
152 local target=$(readlink "$file")
153 ln -sfn "$target" "${initdir}$file" || return 1
154 # resolve relative path and recursively install destionation
155 [[ "$target" = "${target##*/}" ]] && target="${file%/*}/$target"
156 inst_dir "$target"
157 else
158 # create directory
159 mkdir -p "${initdir}$file" || return 1
161 done
164 # $1 = file to copy to ramdisk
165 # $2 (optional) Name for the file on the ramdisk
166 # Location of the image dir is assumed to be $initdir
167 # We never overwrite the target if it exists.
168 inst_simple() {
169 local src target
170 [[ -f $1 ]] || return 1
171 src=$1 target="${2:-$1}"
172 if ! [[ -d ${initdir}$target ]]; then
173 [[ -e ${initdir}$target ]] && return 0
174 inst_dir "${target%/*}"
176 dinfo "Installing $src"
177 cp -pfL "$src" "${initdir}$target"
180 # Same as above, but specialized to handle dynamic libraries.
181 # It handles making symlinks according to how the original library
182 # is referenced.
183 inst_library() {
184 local src=$1 dest=${2:-$1}
185 [[ -e $initdir$dest ]] && return 0
186 if [[ -L $src ]]; then
187 reallib=$(readlink -f "$src")
188 lib=${src##*/}
189 inst_simple "$reallib" "$reallib"
190 inst_dir "${dest%/*}"
191 (cd "${initdir}${dest%/*}" && ln -s "$reallib" "$lib")
192 else
193 inst_simple "$src" "$dest"
197 # find a binary. If we were not passed the full path directly,
198 # search in the usual places to find the binary.
199 find_binary() {
200 local binpath="/bin /sbin /usr/bin /usr/sbin" p
201 [[ -z ${1##/*} && -x $1 ]] && { echo $1; return 0; }
202 for p in $binpath; do
203 [[ -x $p/$1 ]] && { echo "$p/$1"; return 0; }
204 done
205 return 1
208 # Same as above, but specialized to install binary executables.
209 # Install binary executable, and all shared library dependencies, if any.
210 inst_binary() {
211 local bin target
212 bin=$(find_binary "$1") || return 1
213 target=${2:-$bin}
214 inst_symlink $bin $target && return 0
215 local LDSO NAME IO FILE ADDR I1 n f TLIBDIR
216 [[ -e $initdir$target ]] && return 0
217 # I love bash!
218 ldd $bin 2>/dev/null | while read line; do
219 [[ $line = 'not a dynamic executable' ]] && return 1
220 if [[ $line =~ not\ found ]]; then
221 derror "Missing a shared library required by $bin."
222 derror "Run \"ldd $bin\" to find out what it is."
223 derror "dracut cannot create an initrd."
224 exit 1
226 so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
227 [[ $line =~ $so_regex ]] || continue
228 FILE=${BASH_REMATCH[1]}
229 [[ -e ${initdir}$FILE ]] && continue
230 # see if we are loading an optimized version of a shared lib.
231 lib_regex='^(/lib[^/]*).*'
232 if [[ $FILE =~ $lib_regex ]]; then
233 TLIBDIR=${BASH_REMATCH[1]}
234 BASE=${FILE##*/}
235 # prefer nosegneg libs, then unoptimized ones.
236 for f in "$TLIBDIR/i686/nosegneg" "$TLIBDIR"; do
237 [[ -e $f/$BASE ]] || continue
238 FILE=$f/$BASE
239 break
240 done
241 inst_library "$FILE" "$TLIBDIR/$BASE"
242 IF_dynamic=yes
243 continue
245 inst_library "$FILE"
246 done
247 inst_simple "$bin" "$target"
250 # same as above, except for shell scripts.
251 # If your shell script does not start with shebang, it is not a shell script.
252 inst_script() {
253 [[ -f $1 ]] || return 1
254 local line
255 read -r -n 80 line <"$1"
256 # If debug is set, clean unprintable chars to prevent messing up the term
257 [[ $debug ]] && line=$(echo -n "$line" | tr -c -d '[:print:][:space:]')
258 shebang_regex='(#! *)(/[^ ]+).*'
259 [[ $line =~ $shebang_regex ]] || return 1
260 inst "${BASH_REMATCH[2]}" && inst_simple "$@"
263 # same as above, but specialized for symlinks
264 inst_symlink() {
265 local src=$1 target=$initdir${2:-$1} realsrc
266 [[ -L $1 ]] || return 1
267 [[ -L $target ]] && return 0
268 realsrc=$(readlink -f "$src")
269 [[ $realsrc = ${realsrc##*/} ]] && realsrc=${src%/*}/$realsrc
270 inst "$realsrc" && ln -s "$realsrc" "$target"
273 # find a udev rule in the usual places.
274 find_rule() {
275 [[ -f $1 ]] && { echo "$1"; return 0; }
276 for r in . /lib/udev/rules.d /etc/udev/rules.d $dracutbasedir/rules.d; do
277 [[ -f $r/$1 ]] && { echo "$r/$1"; return 0; }
278 done
279 return 1
282 # udev rules always get installed in the same place, so
283 # create a function to install them to make life simpler.
284 inst_rules() {
285 local target=/etc/udev/rules.d
286 inst_dir "/lib/udev/rules.d"
287 inst_dir "$target"
288 for rule in "$@"; do
289 rule=$(find_rule "$rule") && \
290 inst_simple "$rule" "$target/${rule##*/}"
291 done
294 # general purpose installation function
295 # Same args as above.
296 inst() {
297 case $# in
298 1) ;;
300 [[ -z $initdir ]] && [[ -d $2 ]] && export initdir=$2
301 [[ $initdir = $2 ]] && set $1
304 [[ -z $initdir ]] && export initdir=$2
305 set $1 $3
308 derror "inst only takes 1 or 2 or 3 arguments"
309 exit 1
311 esac
312 for x in inst_symlink inst_script inst_binary inst_simple; do
313 $x "$@" && return 0
314 done
315 return 1
318 # install function specialized for hooks
319 # $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
320 # All hooks should be POSIX/SuS compliant, they will be sourced by init.
321 inst_hook() {
322 if ! [[ -f $3 ]]; then
323 derror "Cannot install a hook ($3) that does not exist."
324 derror "Aborting initrd creation."
325 exit 1
326 elif ! strstr "$hookdirs" "$1"; then
327 derror "No such hook type $1. Aborting initrd creation."
328 exit 1
330 inst_simple "$3" "/${1}/${2}${3##*/}"
333 dracut_install() {
334 if [[ $1 = '-o' ]]; then
335 local optional=yes
336 shift
338 while (($# > 0)); do
339 if ! inst "$1" ; then
340 if [[ $optional = yes ]]; then
341 dwarning "Skipping program $1 as it cannot be found and is flagged to be optional"
342 else
343 derror "Failed to install $1"
344 exit 1
347 shift
348 done
351 check_module_deps() {
352 local moddir dep ret
353 # if we are already set to be loaded, we do not have to be checked again.
354 strstr "$mods_to_load" " $1 " && return
355 # turn a module name into a directory, if we can.
356 moddir=$(echo ${dracutbasedir}/modules.d/??${1})
357 [[ -d $moddir && -x $moddir/install ]] || return 1
358 # if we do not have a check script, we are unconditionally included
359 if [[ -x $moddir/check ]]; then
360 "$moddir/check"
361 ret=$?
362 # a return value of 255 = load module only as a dependency.
363 ((ret==0||ret==255)) || return 1
364 for dep in $("$moddir/check" -d); do
365 check_module_deps "$dep" && continue
366 dwarning "Dependency $mod failed."
367 return 1
368 done
370 mods_to_load+=" $1 "
373 should_source_module() {
374 local dep
375 if [[ $kernel_only = yes ]]; then
376 [[ -x $1/installkernel ]] && return 0
377 return 1
379 [[ -x $1/install ]] || [[ -x $1/installkernel ]] || return 1
380 [[ -x $1/check ]] || return 0
381 "$1/check" $hostonly || return 1
382 for dep in $("$1/check" -d); do
383 check_module_deps "$dep" && continue
384 dwarning "Cannot load $mod, dependencies failed."
385 return 1
386 done
389 check_modules() {
390 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
391 local mod=${moddir##*/}; mod=${mod#[0-9][0-9]}
392 # If we are already scheduled to be loaded, no need to check again.
393 strstr "$mods_to_load" " $mod " && continue
394 # This should never happen, but...
395 [[ -d $moddir ]] || continue
396 [[ $dracutmodules != all ]] && ! strstr "$dracutmodules" "$mod" && \
397 continue
398 strstr "$omit_dracutmodules" "$mod" && continue
399 if ! strstr "$add_dracutmodules" "$mod"; then
400 should_source_module "$moddir" || continue
402 mods_to_load+=" $mod "
403 done
406 # Install a single kernel module along with any firmware it may require.
407 # $1 = full path to kernel module to install
408 install_kmod_with_fw() {
409 local modname=${1##*/} fwdir found
410 modname=${modname%.ko*}
411 inst_simple "$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" || \
412 return 0 # no need to go further if the module is already installed
413 for fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do
414 found=''
415 for fwdir in $fw_dir; do
416 if [[ -d $fwdir && -f $fwdir/$fw ]]; then
417 inst_simple "$fwdir/$fw" "/lib/firmware/$fw"
418 found=yes
420 done
421 if [[ $found != yes ]]; then
422 dinfo "Possible missing firmware ${fw} for module ${mod}.ko"
424 done
427 # Do something with all the dependencies of a kernel module.
428 # Note that kernel modules depend on themselves using the technique we use
429 # $1 = function to call for each dependency we find
430 # It will be passed the full path to the found kernel module
431 # $2 = module to get dependencies for
432 # rest of args = arguments to modprobe
433 for_each_kmod_dep() {
434 local func=$1 kmod=$2 cmd modpapth options
435 shift 2
436 modprobe "$@" --ignore-install --show-depends $kmod 2>/dev/null | \
437 while read cmd modpath options; do
438 [[ $cmd = insmod ]] || continue
439 $func $modpath
440 done
443 # filter kernel modules to install certain modules that meet specific
444 # requirements.
445 # $1 = function to call with module name to filter.
446 # This function will be passed the full path to the module to test.
447 # The behaviour of this function can vary depending on whether $hostonly is set.
448 # If it is, we will only look at modules that are already in memory.
449 # If it is not, we will look at all kernel modules
450 # This function returns the full filenames of modules that match $1
451 filter_kernel_modules () (
452 if ! [[ $hostonly ]]; then
453 filtercmd='find "$srcmods/kernel/drivers" -name "*.ko" -o -name "*.ko.gz"'
454 else
455 filtercmd='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename -k $kernel'
457 for modname in $(eval $filtercmd); do
458 case $modname in
459 *.ko)
460 "$1" "$modname" && echo "$modname"
462 *.ko.gz)
463 gzip -dc "$modname" > $initdir/$$.ko
464 $1 $initdir/$$.ko && echo "$modname"
465 rm -f $initdir/$$.ko
467 esac
468 done
471 # install kernel modules along with all their dependencies.
472 instmods() {
473 [[ $no_kernel = yes ]] && return
474 local mod mpargs modpath modname cmd
475 while (($# > 0)); do
476 mod=${1%.ko*}
477 case $mod in
478 =*) # This introduces 2 incompatible meanings for =* arguments
479 # to instmods. We need to decide which one to keep.
480 if [[ $mod = =ata && -f $srcmods/modules.block ]] ; then
481 instmods $mpargs $(egrep 'ata|ahci' "${srcmods}/modules.block")
482 elif [ -f $srcmods/modules.${mod#=} ]; then
483 instmods $mpargs $(cat ${srcmods}/modules.${mod#=} )
484 else
485 instmods $mpargs $(find "$srcmods" -path "*/${mod#=}/*")
488 --*)
489 mod=${mod##*/}
490 mpargs+=" $mod";;
491 i2o_scsi)
492 # Must never run this diagnostic-only module
493 shift; continue;
496 mod=${mod##*/}
497 # if we are already installed, skip this module and go on
498 # to the next one.
499 [[ -f $initdir/$1 ]] && { shift; continue; }
500 # If we are building a host-specific initramfs and this
501 # module is not already loaded, move on to the next one.
502 [[ $hostonly ]] && ! grep -q "${mod//-/_}" /proc/modules && \
503 ! echo $add_drivers | grep -qe "\<${mod}\>" && {
504 shift; continue;
506 # ok, load the module, all its dependencies, and any firmware
507 # it may require
508 for_each_kmod_dep install_kmod_with_fw $mod \
509 --set-version $kernel -d ${srcmods%%/lib/modules/*}/
511 esac
512 shift
513 done
516 # vim:ts=8:sw=4:sts=4:et