]> git.ipfire.org Git - thirdparty/dracut.git/blame - dracut-functions
dracut.spec: clean up the requirements
[thirdparty/dracut.git] / dracut-functions
CommitLineData
04b56f3a
JK
1#!/bin/bash
2#
33ee031c 3# functions used by dracut and other tools.
04b56f3a 4#
33ee031c 5# Copyright 2005-2009 Red Hat, Inc. All rights reserved.
04b56f3a
JK
6#
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.
11#
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.
16#
17# You should have received a copy of the GNU General Public License
0f9c78c1 18# along with this program. If not, see <http://www.gnu.org/licenses/>.
04b56f3a 19#
04b56f3a
JK
20
21IF_RTLD=""
22IF_dynamic=""
36b24d7c 23
3198f171 24# Generic substring function. If $2 is in $1, return 0.
f76ef3aa 25strstr() { [[ $1 =~ $2 ]]; }
f04dc5f3 26
4cf26268 27# Log initrd creation.
1a918b40 28if ! [[ $dracutlogfile ]]; then
5d791c0e 29 [[ $dracutbasedir = /usr/share/dracut ]] && \
e0dca0e4
VL
30 dracutlogfile=/var/log/dracut.log || \
31 dracutlogfile=/tmp/dracut.log
59580f28
HH
32# [[ -w $dracutlogfile ]] || dracutlogfile=/tmp/dracut.log
33 if [[ -w $dracutlogfile ]]; then
34 >"$dracutlogfile"
35 fi
e0dca0e4 36fi
1a918b40 37
6fac4691
HH
38dwarning() {
39 echo "W: $@" >&2
161efa1f 40 [[ -w $dracutlogfile ]] && echo "W: $@" >>"$dracutlogfile"
6fac4691
HH
41}
42
43dinfo() {
84ffb877 44 [[ $beverbose ]] && echo "I: $@" >&2
161efa1f 45 [[ -w $dracutlogfile ]] && echo "I: $@" >>"$dracutlogfile"
6fac4691
HH
46}
47
48derror() {
49 echo "E: $@" >&2
161efa1f 50 [[ -w $dracutlogfile ]] && echo "E: $@" >>"$dracutlogfile"
6fac4691
HH
51}
52
9ede1929 53get_fs_env() {
d8b9844c
HH
54 eval $(udevadm info --query=env --name=$1|egrep 'ID_FS_(TYPE|UUID)=')
55 [[ $ID_FS_TYPE ]] && return
56
59c88f49 57 if [[ -x /lib/udev/vol_id ]]; then
9ede1929
VL
58 eval $(/lib/udev/vol_id --export $1)
59 elif find_binary blkid >/dev/null; then
60 eval $(blkid -o udev $1)
59c88f49
VL
61 else
62 return 1
63 fi
9ede1929
VL
64}
65
66get_fs_type() (
67 get_fs_env $1 || return
68 echo $ID_FS_TYPE
69)
70
71get_fs_uuid() (
72 get_fs_env $1 || return
73 echo $ID_FS_UUID
59c88f49
VL
74)
75
17829e94 76# finds the major:minor of the block device backing the root filesystem.
f76ef3aa 77find_block_device() {
17829e94
VL
78 local rootdev blkdev fs type opts misc
79 while read blkdev fs type opts misc; do
80 [[ $blkdev = rootfs ]] && continue # skip rootfs entry
f76ef3aa 81 [[ $fs = $1 ]] && { rootdev=$blkdev; break; } # we have a winner!
17829e94
VL
82 done < /proc/mounts
83 [[ -b $rootdev ]] || return 1 # oops, not a block device.
84 # get major/minor for the device
85 ls -nLl "$rootdev" | \
86 (read x x x x maj min x; maj=${maj//,/}; echo $maj:$min)
87}
88
f76ef3aa
VL
89find_root_block_device() { find_block_device /; }
90
17829e94
VL
91# Walk all the slave relationships for a given block device.
92# Stop when our helper function returns success
93# $1 = function to call on every found block device
94# $2 = block device in major:minor format
95check_block_and_slaves() {
96 local x
97 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
98 "$1" $2 && return
533d7dc4 99 check_vol_slaves "$@" && return 0
17829e94 100 [[ -d /sys/dev/block/$2/slaves ]] || return 1
17829e94 101 for x in /sys/dev/block/$2/slaves/*/dev; do
9defc609 102 [[ -f $x ]] || continue
17829e94
VL
103 check_block_and_slaves $1 $(cat "$x") && return 0
104 done
105 return 1
106}
107
533d7dc4
HH
108get_numeric_dev() {
109 ls -lH "$1" | { read a b c d maj min rest; printf "%d:%d" ${maj%%,} $min;}
110}
111
112# ugly workaround for the lvm design
113# There is no volume group device,
114# so, there are no slave devices for volume groups.
115# Logical volumes only have the slave devices they really live on,
116# but you cannot create the logical volume without the volume group.
95bde758 117# And the volume group might be bigger than the devices the LV needs.
533d7dc4
HH
118check_vol_slaves() {
119 for i in /dev/mapper/*; do
120 lv=$(get_numeric_dev $i)
121 if [[ $lv = $2 ]]; then
59580f28 122 vg=$(lvm lvs --noheadings -o vg_name $i 2>/dev/null)
533d7dc4
HH
123 # strip space
124 vg=$(echo $vg)
125 if [[ $vg ]]; then
59580f28 126 for pv in $(lvm vgs --noheadings -o pv_name "$vg" 2>/dev/null); \
533d7dc4
HH
127 do
128 check_block_and_slaves $1 $(get_numeric_dev $pv) \
129 && return 0
130 done
131 fi
132 fi
133 done
134 return 1
135}
136
19612483
LA
137# Install a directory, keeping symlinks as on the original system.
138# Example: if /lib64 points to /lib on the host, "inst_dir /lib/file"
139# will create ${initdir}/lib64, ${initdir}/lib64/file,
140# and a symlink ${initdir}/lib -> lib64.
141inst_dir() {
142 local dir="$1"
143 [[ -e "${initdir}$dir" ]] && return 0
144
145 # iterate over parent directories
146 local file=""
147 local IFS="/"
148 for part in $dir; do
149 [ -z "$part" ] && continue
150 file="$file/$part"
151 [[ -e "${initdir}$file" ]] && continue
152
153 if [ -L "$file" ]; then
154 # create link as the original
155 local target=$(readlink "$file")
156 ln -sfn "$target" "${initdir}$file" || return 1
157 # resolve relative path and recursively install destionation
158 [[ "$target" = "${target##*/}" ]] && target="${file%/*}/$target"
159 inst_dir "$target"
160 else
161 # create directory
162 mkdir -p "${initdir}$file" || return 1
163 fi
164 done
165}
166
36b24d7c
VL
167# $1 = file to copy to ramdisk
168# $2 (optional) Name for the file on the ramdisk
169# Location of the image dir is assumed to be $initdir
3198f171 170# We never overwrite the target if it exists.
36b24d7c 171inst_simple() {
54b44196
VL
172 local src target
173 [[ -f $1 ]] || return 1
19612483 174 src=$1 target="${2:-$1}"
3c56f372
HH
175 if ! [[ -d ${initdir}$target ]]; then
176 [[ -e ${initdir}$target ]] && return 0
177 inst_dir "${target%/*}"
178 fi
6fac4691 179 dinfo "Installing $src"
f6d1cb89 180 cp -pfL "$src" "${initdir}$target"
36b24d7c
VL
181}
182
95bde758 183# Same as above, but specialized to handle dynamic libraries.
3198f171
VL
184# It handles making symlinks according to how the original library
185# is referenced.
36b24d7c
VL
186inst_library() {
187 local src=$1 dest=${2:-$1}
d46c2e8b 188 [[ -e $initdir$dest ]] && return 0
36b24d7c 189 if [[ -L $src ]]; then
161efa1f 190 reallib=$(readlink -f "$src")
36b24d7c 191 lib=${src##*/}
9f88fcd9 192 inst_simple "$reallib" "$reallib"
19612483 193 inst_dir "${dest%/*}"
36b24d7c
VL
194 (cd "${initdir}${dest%/*}" && ln -s "$reallib" "$lib")
195 else
196 inst_simple "$src" "$dest"
197 fi
198}
3198f171
VL
199
200# find a binary. If we were not passed the full path directly,
201# search in the usual places to find the binary.
6b25d71a
VL
202find_binary() {
203 local binpath="/bin /sbin /usr/bin /usr/sbin" p
0c296f15 204 [[ -z ${1##/*} && -x $1 ]] && { echo $1; return 0; }
3359c8da 205 for p in $binpath; do
6b25d71a 206 [[ -x $p/$1 ]] && { echo "$p/$1"; return 0; }
3359c8da
VL
207 done
208 return 1
209}
36b24d7c 210
3198f171
VL
211# Same as above, but specialized to install binary executables.
212# Install binary executable, and all shared library dependencies, if any.
36b24d7c 213inst_binary() {
54b44196 214 local bin target
6b25d71a 215 bin=$(find_binary "$1") || return 1
3198f171 216 target=${2:-$bin}
d46c2e8b 217 inst_symlink $bin $target && return 0
36b24d7c 218 local LDSO NAME IO FILE ADDR I1 n f TLIBDIR
d46c2e8b 219 [[ -e $initdir$target ]] && return 0
8667f2b7 220 # I love bash!
5c862533 221 ldd $bin 2>/dev/null | while read line; do
f04dc5f3 222 [[ $line = 'not a dynamic executable' ]] && return 1
cdad82fd 223 if [[ $line =~ not\ found ]]; then
6fac4691
HH
224 derror "Missing a shared library required by $bin."
225 derror "Run \"ldd $bin\" to find out what it is."
226 derror "dracut cannot create an initrd."
36b24d7c 227 exit 1
cdad82fd 228 fi
fdb3d52d
MP
229 so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
230 [[ $line =~ $so_regex ]] || continue
f04dc5f3 231 FILE=${BASH_REMATCH[1]}
d46c2e8b 232 [[ -e ${initdir}$FILE ]] && continue
8667f2b7 233 # see if we are loading an optimized version of a shared lib.
fdb3d52d
MP
234 lib_regex='^(/lib[^/]*).*'
235 if [[ $FILE =~ $lib_regex ]]; then
8667f2b7 236 TLIBDIR=${BASH_REMATCH[1]}
161efa1f 237 BASE=${FILE##*/}
8667f2b7
VL
238 # prefer nosegneg libs, then unoptimized ones.
239 for f in "$TLIBDIR/i686/nosegneg" "$TLIBDIR"; do
d46c2e8b 240 [[ -e $f/$BASE ]] || continue
161efa1f 241 FILE=$f/$BASE
8667f2b7
VL
242 break
243 done
36b24d7c 244 inst_library "$FILE" "$TLIBDIR/$BASE"
161efa1f 245 IF_dynamic=yes
36b24d7c 246 continue
cdad82fd 247 fi
36b24d7c 248 inst_library "$FILE"
5c862533 249 done
3198f171 250 inst_simple "$bin" "$target"
36b24d7c 251}
04b56f3a 252
36b24d7c
VL
253# same as above, except for shell scripts.
254# If your shell script does not start with shebang, it is not a shell script.
255inst_script() {
54b44196 256 [[ -f $1 ]] || return 1
c7b2624f
VL
257 local line
258 read -r -n 80 line <"$1"
00531568
AT
259 # If debug is set, clean unprintable chars to prevent messing up the term
260 [[ $debug ]] && line=$(echo -n "$line" | tr -c -d '[:print:][:space:]')
fdb3d52d
MP
261 shebang_regex='(#! *)(/[^ ]+).*'
262 [[ $line =~ $shebang_regex ]] || return 1
c7b2624f 263 inst "${BASH_REMATCH[2]}" && inst_simple "$@"
04b56f3a
JK
264}
265
36b24d7c
VL
266# same as above, but specialized for symlinks
267inst_symlink() {
268 local src=$1 target=$initdir${2:-$1} realsrc
269 [[ -L $1 ]] || return 1
270 [[ -L $target ]] && return 0
9f88fcd9 271 realsrc=$(readlink -f "$src")
161efa1f 272 [[ $realsrc = ${realsrc##*/} ]] && realsrc=${src%/*}/$realsrc
36b24d7c
VL
273 inst "$realsrc" && ln -s "$realsrc" "$target"
274}
275
95bde758 276# find a udev rule in the usual places.
09805e02 277find_rule() {
b649f1c2 278 [[ -f $1 ]] && { echo "$1"; return 0; }
5d791c0e 279 for r in . /lib/udev/rules.d /etc/udev/rules.d $dracutbasedir/rules.d; do
09805e02
VL
280 [[ -f $r/$1 ]] && { echo "$r/$1"; return 0; }
281 done
282 return 1
283}
284
f04dc5f3
VL
285# udev rules always get installed in the same place, so
286# create a function to install them to make life simpler.
287inst_rules() {
161efa1f 288 local target=/etc/udev/rules.d
19612483
LA
289 inst_dir "/lib/udev/rules.d"
290 inst_dir "$target"
f04dc5f3 291 for rule in "$@"; do
161efa1f 292 rule=$(find_rule "$rule") && \
496d08bb 293 inst_simple "$rule" "$target/${rule##*/}"
f04dc5f3
VL
294 done
295}
296
36b24d7c
VL
297# general purpose installation function
298# Same args as above.
6b24de99 299inst() {
94dcc5b8
HH
300 case $# in
301 1) ;;
302 2)
303 [[ -z $initdir ]] && [[ -d $2 ]] && export initdir=$2
304 [[ $initdir = $2 ]] && set $1
305 ;;
306 3)
307 [[ -z $initdir ]] && export initdir=$2
308 set $1 $3
309 ;;
310 *)
311 derror "inst only takes 1 or 2 or 3 arguments"
312 exit 1
313 ;;
314 esac
8c1faa35 315 for x in inst_symlink inst_script inst_binary inst_simple; do
34fffd88 316 $x "$@" && return 0
36b24d7c
VL
317 done
318 return 1
04b56f3a
JK
319}
320
53f95456
VL
321# install function specialized for hooks
322# $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
323# All hooks should be POSIX/SuS compliant, they will be sourced by init.
324inst_hook() {
cdad82fd 325 if ! [[ -f $3 ]]; then
6fac4691
HH
326 derror "Cannot install a hook ($3) that does not exist."
327 derror "Aborting initrd creation."
53f95456 328 exit 1
cdad82fd 329 elif ! strstr "$hookdirs" "$1"; then
6fac4691 330 derror "No such hook type $1. Aborting initrd creation."
53f95456 331 exit 1
cdad82fd 332 fi
53f95456
VL
333 inst_simple "$3" "/${1}/${2}${3##*/}"
334}
335
f4fff04e 336dracut_install() {
bd4c4bcb
VL
337 if [[ $1 = '-o' ]]; then
338 local optional=yes
339 shift
340 fi
f4fff04e 341 while (($# > 0)); do
bd4c4bcb
VL
342 if ! inst "$1" ; then
343 if [[ $optional = yes ]]; then
344 dwarning "Skipping program $1 as it cannot be found and is flagged to be optional"
345 else
346 derror "Failed to install $1"
347 exit 1
348 fi
349 fi
350 shift
f4fff04e
VL
351 done
352}
353
0c2e3d12 354check_module_deps() {
4cf26268
VL
355 local moddir dep ret
356 # if we are already set to be loaded, we do not have to be checked again.
bd4c4bcb 357 strstr "$mods_to_load" " $1 " && return
0c2e3d12 358 # turn a module name into a directory, if we can.
5d791c0e 359 moddir=$(echo ${dracutbasedir}/modules.d/??${1})
0c2e3d12
VL
360 [[ -d $moddir && -x $moddir/install ]] || return 1
361 # if we do not have a check script, we are unconditionally included
362 if [[ -x $moddir/check ]]; then
4cf26268
VL
363 "$moddir/check"
364 ret=$?
365 # a return value of 255 = load module only as a dependency.
366 ((ret==0||ret==255)) || return 1
0c2e3d12 367 for dep in $("$moddir/check" -d); do
4cf26268 368 check_module_deps "$dep" && continue
0c2e3d12
VL
369 dwarning "Dependency $mod failed."
370 return 1
371 done
372 fi
373 mods_to_load+=" $1 "
374}
375
376should_source_module() {
377 local dep
0c1a8ebc 378 if [[ $kernel_only = yes ]]; then
33ee031c
HH
379 [[ -x $1/installkernel ]] && return 0
380 return 1
381 fi
973c23b0 382 [[ -x $1/install ]] || [[ -x $1/installkernel ]] || return 1
0c2e3d12
VL
383 [[ -x $1/check ]] || return 0
384 "$1/check" $hostonly || return 1
385 for dep in $("$1/check" -d); do
4cf26268 386 check_module_deps "$dep" && continue
0c2e3d12
VL
387 dwarning "Cannot load $mod, dependencies failed."
388 return 1
389 done
390}
391
392check_modules() {
5d791c0e 393 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
0c2e3d12 394 local mod=${moddir##*/}; mod=${mod#[0-9][0-9]}
4cf26268
VL
395 # If we are already scheduled to be loaded, no need to check again.
396 strstr "$mods_to_load" " $mod " && continue
397 # This should never happen, but...
0c2e3d12
VL
398 [[ -d $moddir ]] || continue
399 [[ $dracutmodules != all ]] && ! strstr "$dracutmodules" "$mod" && \
400 continue
401 strstr "$omit_dracutmodules" "$mod" && continue
624b52c4 402 if ! strstr "$add_dracutmodules" "$mod"; then
970e646b
HH
403 should_source_module "$moddir" || continue
404 fi
0c2e3d12
VL
405 mods_to_load+=" $mod "
406 done
407}
408
ddfd1d10
VL
409# Install a single kernel module along with any firmware it may require.
410# $1 = full path to kernel module to install
411install_kmod_with_fw() {
412 local modname=${1##*/} fwdir found
9b37edbf 413 modname=${modname%.ko*}
fb8923f6
VL
414 inst_simple "$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" || \
415 return 0 # no need to go further if the module is already installed
ddfd1d10
VL
416 for fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do
417 found=''
418 for fwdir in $fw_dir; do
419 if [[ -d $fwdir && -f $fwdir/$fw ]]; then
420 inst_simple "$fwdir/$fw" "/lib/firmware/$fw"
421 found=yes
422 fi
423 done
424 if [[ $found != yes ]]; then
116cb137 425 dinfo "Possible missing firmware ${fw} for module ${mod}.ko"
ddfd1d10
VL
426 fi
427 done
428}
429
430# Do something with all the dependencies of a kernel module.
431# Note that kernel modules depend on themselves using the technique we use
432# $1 = function to call for each dependency we find
433# It will be passed the full path to the found kernel module
434# $2 = module to get dependencies for
435# rest of args = arguments to modprobe
436for_each_kmod_dep() {
437 local func=$1 kmod=$2 cmd modpapth options
438 shift 2
439 modprobe "$@" --ignore-install --show-depends $kmod 2>/dev/null | \
440 while read cmd modpath options; do
441 [[ $cmd = insmod ]] || continue
442 $func $modpath
443 done
444}
445
95bde758 446# filter kernel modules to install certain modules that meet specific
240cc7c4
VL
447# requirements.
448# $1 = function to call with module name to filter.
449# This function will be passed the full path to the module to test.
450# The behaviour of this function can vary depending on whether $hostonly is set.
451# If it is, we will only look at modules that are already in memory.
452# If it is not, we will look at all kernel modules
fb8923f6 453# This function returns the full filenames of modules that match $1
240cc7c4 454filter_kernel_modules () (
ba67b923 455 if ! [[ $hostonly ]]; then
9b37edbf 456 filtercmd='find "$srcmods/kernel/drivers" -name "*.ko" -o -name "*.ko.gz"'
240cc7c4 457 else
fb8923f6 458 filtercmd='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename -k $kernel'
240cc7c4 459 fi
fb8923f6 460 for modname in $(eval $filtercmd); do
9b37edbf
LB
461 case $modname in
462 *.ko)
463 "$1" "$modname" && echo "$modname"
464 ;;
465 *.ko.gz)
466 gzip -dc "$modname" > $initdir/$$.ko
467 $1 $initdir/$$.ko && echo "$modname"
468 rm -f $initdir/$$.ko
469 ;;
470 esac
fb8923f6 471 done
240cc7c4
VL
472)
473
ddfd1d10 474# install kernel modules along with all their dependencies.
6fa0c3d6 475instmods() {
0c1a8ebc 476 [[ $no_kernel = yes ]] && return
6fa0c3d6
VL
477 local mod mpargs modpath modname cmd
478 while (($# > 0)); do
9b37edbf 479 mod=${1%.ko*}
6fa0c3d6 480 case $mod in
992acaa9 481 =*) # This introduces 2 incompatible meanings for =* arguments
74534e19 482 # to instmods. We need to decide which one to keep.
0c1a8ebc 483 if [[ $mod = =ata && -f $srcmods/modules.block ]] ; then
c93d9f62 484 instmods $mpargs $(egrep 'ata|ahci' "${srcmods}/modules.block")
86c9fa48 485 elif [ -f $srcmods/modules.${mod#=} ]; then
8d385248 486 instmods $mpargs $(cat ${srcmods}/modules.${mod#=} )
20122a83 487 else
8d385248 488 instmods $mpargs $(find "$srcmods" -path "*/${mod#=}/*")
20122a83
HH
489 fi
490 ;;
6568d86a
HH
491 --*)
492 mod=${mod##*/}
493 mpargs+=" $mod";;
71afe645
WT
494 i2o_scsi)
495 # Must never run this diagnostic-only module
496 shift; continue;
497 ;;
6568d86a
HH
498 *)
499 mod=${mod##*/}
500 # if we are already installed, skip this module and go on
4cf26268 501 # to the next one.
3198f171 502 [[ -f $initdir/$1 ]] && { shift; continue; }
4cf26268
VL
503 # If we are building a host-specific initramfs and this
504 # module is not already loaded, move on to the next one.
74c5dd21
LB
505 [[ $hostonly ]] && ! grep -q "${mod//-/_}" /proc/modules && \
506 ! echo $add_drivers | grep -qe "\<${mod}\>" && {
4cf26268
VL
507 shift; continue;
508 }
0c1a8ebc
VL
509 # ok, load the module, all its dependencies, and any firmware
510 # it may require
ddfd1d10
VL
511 for_each_kmod_dep install_kmod_with_fw $mod \
512 --set-version $kernel -d ${srcmods%%/lib/modules/*}/
6fa0c3d6 513 ;;
c93d9f62 514 esac
6fa0c3d6 515 shift
04b56f3a 516 done
6fa0c3d6 517}
04b56f3a 518
04b56f3a 519# vim:ts=8:sw=4:sts=4:et