]> git.ipfire.org Git - thirdparty/dracut.git/blame - dracut-functions.sh
Version 021
[thirdparty/dracut.git] / dracut-functions.sh
CommitLineData
04b56f3a 1#!/bin/bash
cc02093d
HH
2# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3# ex: ts=8 sw=4 sts=4 et filetype=sh
04b56f3a 4#
33ee031c 5# functions used by dracut and other tools.
04b56f3a 6#
33ee031c 7# Copyright 2005-2009 Red Hat, Inc. All rights reserved.
04b56f3a
JK
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
0f9c78c1 20# along with this program. If not, see <http://www.gnu.org/licenses/>.
04b56f3a 21#
258de828 22export LC_MESSAGES=C
f4031e8a
HH
23
24if [[ $DRACUT_KERNEL_LAZY ]] && ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
25 if ! [[ -d "$initdir/.kernelmodseen" ]]; then
26 mkdir -p "$initdir/.kernelmodseen"
27 fi
28 DRACUT_KERNEL_LAZY_HASHDIR="$initdir/.kernelmodseen"
29fi
ec1aa3bc 30
eaa924b6
HH
31if [[ $initdir ]] && ! [[ -d $initdir ]]; then
32 mkdir -p "$initdir"
33fi
34
8d95b8b3 35# Generic substring function. If $2 is in $1, return 0.
520047c3 36strstr() { [[ $1 = *$2* ]]; }
8d95b8b3 37
f4031e8a
HH
38# find a binary. If we were not passed the full path directly,
39# search in the usual places to find the binary.
40find_binary() {
41 if [[ -z ${1##/*} ]]; then
42 if [[ -x $1 ]] || { strstr "$1" ".so" && ldd $1 &>/dev/null; }; then
43 echo $1
44 return 0
45 fi
46 fi
47
48 type -P $1
49}
50
0874654c
HH
51if ! [[ $dracutbasedir ]]; then
52 dracutbasedir=${BASH_SOURCE[0]%/*}
53 [[ $dracutbasedir = "dracut-functions" ]] && dracutbasedir="."
54 [[ $dracutbasedir ]] || dracutbasedir="."
55 dracutbasedir="$(readlink -f $dracutbasedir)"
56fi
57
f4031e8a
HH
58if ! [[ $DRACUT_INSTALL ]]; then
59 DRACUT_INSTALL=$(find_binary dracut-install)
60fi
61
62if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install ]]; then
63 DRACUT_INSTALL=$dracutbasedir/dracut-install
64fi
65
8d95b8b3
HH
66# Detect lib paths
67if ! [[ $libdirs ]] ; then
68 if strstr "$(ldd /bin/sh)" "/lib64/" &>/dev/null \
69 && [[ -d /lib64 ]]; then
70 libdirs+=" /lib64"
71 [[ -d /usr/lib64 ]] && libdirs+=" /usr/lib64"
72 else
73 libdirs+=" /lib"
74 [[ -d /usr/lib ]] && libdirs+=" /usr/lib"
75 fi
76 export libdirs
77fi
78
79if ! [[ $kernel ]]; then
80 kernel=$(uname -r)
81 export kernel
82fi
83
84srcmods="/lib/modules/$kernel/"
85[[ $drivers_dir ]] && {
86 if vercmp $(modprobe --version | cut -d' ' -f3) lt 3.7; then
87 dfatal 'To use --kmoddir option module-init-tools >= 3.7 is required.'
88 exit 1
89 fi
90 srcmods="$drivers_dir"
91}
92export srcmods
93
d6d53f60 94if ! type dinfo >/dev/null 2>&1; then
552ecca6 95 . "$dracutbasedir/dracut-logger.sh"
e103615b
96 dlog_init
97fi
98
593b315c
HH
99if ! [[ $initdir ]]; then
100 dfatal "initdir not set"
101 exit 1
102fi
103
7e2bca48
HH
104# export standard hookdirs
105[[ $hookdirs ]] || {
4fed3ddf 106 hookdirs="cmdline pre-udev pre-trigger netroot "
6e3cc00f 107 hookdirs+="initqueue initqueue/settled initqueue/online initqueue/finished initqueue/timeout "
eef7649e
HH
108 hookdirs+="pre-mount pre-pivot cleanup mount "
109 hookdirs+="emergency shutdown-emergency shutdown "
7e2bca48
HH
110 export hookdirs
111}
112
87122afc
113# Create all subdirectories for given path without creating the last element.
114# $1 = path
f8be7528 115mksubdirs() { [[ -e ${1%/*} ]] || mkdir -m 0755 -p ${1%/*}; }
87122afc 116
22ecea45
117# Version comparision function. Assumes Linux style version scheme.
118# $1 = version a
119# $2 = comparision op (gt, ge, eq, le, lt, ne)
120# $3 = version b
ecf42850 121vercmp() {
29b10e65 122 local _n1=(${1//./ }) _op=$2 _n2=(${3//./ }) _i _res
ecf42850 123
29b10e65 124 for ((_i=0; ; _i++))
22ecea45 125 do
29b10e65
HH
126 if [[ ! ${_n1[_i]}${_n2[_i]} ]]; then _res=0
127 elif ((${_n1[_i]:-0} > ${_n2[_i]:-0})); then _res=1
128 elif ((${_n1[_i]:-0} < ${_n2[_i]:-0})); then _res=2
22ecea45
129 else continue
130 fi
131 break
ecf42850 132 done
ecf42850 133
29b10e65
HH
134 case $_op in
135 gt) ((_res == 1));;
136 ge) ((_res != 2));;
137 eq) ((_res == 0));;
138 le) ((_res != 1));;
139 lt) ((_res == 2));;
140 ne) ((_res != 0));;
22ecea45 141 esac
ecf42850
142}
143
7e2bca48
HH
144# is_func <command>
145# Check whether $1 is a function.
95d2dabc 146is_func() {
0b706743 147 [[ $(type -t $1) = "function" ]]
95d2dabc
HH
148}
149
87122afc
150# Function prints global variables in format name=value line by line.
151# $@ = list of global variables' name
152print_vars() {
29b10e65 153 local _var _value
87122afc 154
29b10e65 155 for _var in $@
87122afc 156 do
29b10e65
HH
157 _value=$(eval echo \$$_var)
158 [[ ${_value} ]] && echo "${_var}=\"${_value}\""
87122afc
159 done
160}
161
7e2bca48
HH
162# normalize_path <path>
163# Prints the normalized path, where it removes any duplicated
164# and trailing slashes.
165# Example:
166# $ normalize_path ///test/test//
167# /test/test
626d9eba 168normalize_path() {
c44e3cb4
MS
169 shopt -q -s extglob
170 set -- "${1//+(\/)//}"
171 shopt -q -u extglob
172 echo "${1%/}"
626d9eba 173}
d4bb4316 174
7e2bca48
HH
175# convert_abs_rel <from> <to>
176# Prints the relative path, when creating a symlink to <to> from <from>.
177# Example:
178# $ convert_abs_rel /usr/bin/test /bin/test-2
179# ../../bin/test-2
180# $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
d4bb4316 181convert_abs_rel() {
6d2a7942 182 local __current __absolute __abssize __cursize __newpath
c44e3cb4 183 local -i __i __level
d4bb4316 184
c44e3cb4
MS
185 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
186
c1609dd4
MS
187 # corner case #1 - self looping link
188 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
189
190 # corner case #2 - own dir link
191 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
192
6d2a7942
HH
193 IFS="/" __current=($1)
194 IFS="/" __absolute=($2)
d4bb4316
HH
195
196 __abssize=${#__absolute[@]}
197 __cursize=${#__current[@]}
198
199 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
200 do
201 (( __level++ ))
202 if (( __level > __abssize || __level > __cursize ))
203 then
204 break
205 fi
206 done
207
208 for ((__i = __level; __i < __cursize-1; __i++))
209 do
210 if ((__i > __level))
211 then
212 __newpath=$__newpath"/"
213 fi
214 __newpath=$__newpath".."
215 done
216
217 for ((__i = __level; __i < __abssize; __i++))
218 do
219 if [[ -n $__newpath ]]
220 then
221 __newpath=$__newpath"/"
222 fi
223 __newpath=$__newpath${__absolute[__i]}
224 done
225
226 echo "$__newpath"
227}
228
7224913d 229if strstr "$(ln --help)" "--relative"; then
a20d24de
HH
230 ln_r() {
231 ln -sfnr "${initdir}/$1" "${initdir}/$2"
232 }
233else
234 ln_r() {
235 local _source=$1
236 local _dest=$2
237 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
238 ln -sfn $(convert_abs_rel "${_dest}" "${_source}") "${initdir}/${_dest}"
239 }
240fi
241
7e2bca48
HH
242# get_fs_env <device>
243# Get and set the ID_FS_TYPE and ID_FS_UUID variable from udev for a device.
244# Example:
245# $ get_fs_env /dev/sda2; echo $ID_FS_TYPE; echo $ID_FS_UUID
246# ext4
247# 551a39aa-4ae9-4e70-a262-ef665cadb574
9ede1929 248get_fs_env() {
0e979d48
HH
249 local evalstr
250 local found
251
7c179686 252 [[ $1 ]] || return
29b10e65
HH
253 unset ID_FS_TYPE
254 unset ID_FS_UUID
0e979d48
HH
255 if evalstr=$(udevadm info --query=env --name=$1 \
256 | { while read line; do
257 strstr "$line" "DEVPATH" && found=1;
258 strstr "$line" "ID_FS_TYPE=" && { echo $line; exit 0;}
259 done; [[ $found ]] && exit 0; exit 1; }) ; then
260 eval $evalstr
261 [[ $ID_FS_TYPE ]] && return 0
262 return 1
263 fi
d8b9844c 264
0e979d48 265 # Fallback, if we don't have udev information
a5cde2dd 266 if find_binary blkid >/dev/null; then
66b750a0 267 eval $(blkid -o udev $1 \
a5cde2dd
HH
268 | while read line; do
269 strstr "$line" "ID_FS_TYPE=" && echo $line;
270 done)
271 [[ $ID_FS_TYPE ]] && return 0
7c179686 272 fi
a5cde2dd
HH
273 return 1
274}
9ede1929 275
7e2bca48
HH
276# get_maj_min <device>
277# Prints the major and minor of a device node.
278# Example:
279# $ get_maj_min /dev/sda2
280# 8:2
480d772f
HH
281get_maj_min() {
282 local _dev
74132a10 283 _dev=$(stat -L -c '$((0x%t)):$((0x%T))' "$1" 2>/dev/null)
480d772f
HH
284 _dev=$(eval "echo $_dev")
285 echo $_dev
286}
287
7e2bca48
HH
288# find_block_device <mountpoint>
289# Prints the major and minor number of the block device
290# for a given mountpoint.
291# Unless $use_fstab is set to "yes" the functions
292# uses /proc/self/mountinfo as the primary source of the
293# information and only falls back to /etc/fstab, if the mountpoint
294# is not found there.
295# Example:
296# $ find_block_device /usr
297# 8:4
f76ef3aa 298find_block_device() {
29b10e65 299 local _x _mpt _majmin _dev _fs _maj _min
0b706743 300 if [[ $use_fstab != yes ]]; then
29b10e65 301 while read _x _x _majmin _x _mpt _x _x _fs _dev _x; do
7ae5d9d1 302 [[ $_mpt = $1 ]] || continue
29b10e65
HH
303 [[ $_fs = nfs ]] && { echo $_dev; return 0;}
304 [[ $_fs = nfs3 ]] && { echo $_dev; return 0;}
305 [[ $_fs = nfs4 ]] && { echo $_dev; return 0;}
306 [[ $_fs = btrfs ]] && {
480d772f
HH
307 get_maj_min $_dev
308 return 0;
51c977d1 309 }
7ae5d9d1 310 if [[ ${_majmin#0:} = $_majmin ]]; then
29b10e65 311 echo $_majmin
cc02093d
HH
312 return 0 # we have a winner!
313 fi
0b706743 314 done < /proc/self/mountinfo
7c179686 315 fi
0b706743 316 # fall back to /etc/fstab
29b10e65 317 while read _dev _mpt _fs _x; do
85f8bb16
HH
318 [ "${_dev%%#*}" != "$_dev" ] && continue
319
29b10e65
HH
320 if [[ $_mpt = $1 ]]; then
321 [[ $_fs = nfs ]] && { echo $_dev; return 0;}
322 [[ $_fs = nfs3 ]] && { echo $_dev; return 0;}
323 [[ $_fs = nfs4 ]] && { echo $_dev; return 0;}
324 [[ $_dev != ${_dev#UUID=} ]] && _dev=/dev/disk/by-uuid/${_dev#UUID=}
325 [[ $_dev != ${_dev#LABEL=} ]] && _dev=/dev/disk/by-label/${_dev#LABEL=}
326 [[ -b $_dev ]] || return 1 # oops, not a block device.
c4e48eae 327 get_maj_min "$_dev" && return 0
cc02093d 328 fi
7c179686 329 done < /etc/fstab
0b706743
330
331 return 1
17829e94
VL
332}
333
7e2bca48
HH
334# find_dev_fstype <device>
335# Echo the filesystem type for a given device.
336# /proc/self/mountinfo is taken as the primary source of information
337# and /etc/fstab is used as a fallback.
338# No newline is appended!
339# Example:
340# $ find_dev_fstype /dev/sda2;echo
341# ext4
7ae5d9d1
HH
342find_dev_fstype() {
343 local _x _mpt _majmin _dev _fs _maj _min
344 while read _x _x _majmin _x _mpt _x _x _fs _dev _x; do
345 [[ $_dev = $1 ]] || continue
346 echo -n $_fs;
347 return 0;
348 done < /proc/self/mountinfo
349
350 # fall back to /etc/fstab
351 while read _dev _mpt _fs _x; do
352 [[ $_dev = $1 ]] || continue
353 echo -n $_fs;
354 return 0;
355 done < /etc/fstab
356
357 return 1
358}
359
360# finds the major:minor of the block device backing the root filesystem.
f76ef3aa
VL
361find_root_block_device() { find_block_device /; }
362
7e2bca48
HH
363# for_each_host_dev_fs <func>
364# Execute "<func> <dev> <filesystem>" for every "<dev>|<fs>" pair found
365# in ${host_fs_types[@]}
480d772f
HH
366for_each_host_dev_fs()
367{
368 local _func="$1"
d0096de7
AW
369 local _dev
370 local _fs
2efa546f 371 local _ret=1
480d772f
HH
372 for f in ${host_fs_types[@]}; do
373 OLDIFS="$IFS"
374 IFS="|"
375 set -- $f
376 IFS="$OLDIFS"
d0096de7
AW
377 _dev="$1"
378 [[ -b "$_dev" ]] || continue
379 _fs="$2"
2efa546f 380 $_func $_dev $_fs && _ret=0
480d772f 381 done
2efa546f 382 return $_ret
480d772f
HH
383}
384
17829e94
VL
385# Walk all the slave relationships for a given block device.
386# Stop when our helper function returns success
387# $1 = function to call on every found block device
388# $2 = block device in major:minor format
0b706743 389check_block_and_slaves() {
29b10e65 390 local _x
17829e94
VL
391 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
392 "$1" $2 && return
533d7dc4 393 check_vol_slaves "$@" && return 0
3478b314 394 if [[ -f /sys/dev/block/$2/../dev ]]; then
0b706743 395 check_block_and_slaves $1 $(cat "/sys/dev/block/$2/../dev") && return 0
a3afcf2a 396 fi
17829e94 397 [[ -d /sys/dev/block/$2/slaves ]] || return 1
29b10e65
HH
398 for _x in /sys/dev/block/$2/slaves/*/dev; do
399 [[ -f $_x ]] || continue
400 check_block_and_slaves $1 $(cat "$_x") && return 0
17829e94
VL
401 done
402 return 1
403}
404
533d7dc4
HH
405# ugly workaround for the lvm design
406# There is no volume group device,
407# so, there are no slave devices for volume groups.
408# Logical volumes only have the slave devices they really live on,
409# but you cannot create the logical volume without the volume group.
95bde758 410# And the volume group might be bigger than the devices the LV needs.
533d7dc4 411check_vol_slaves() {
29b10e65 412 local _lv _vg _pv
0b706743 413 for i in /dev/mapper/*; do
c4e48eae 414 _lv=$(get_maj_min $i)
29b10e65
HH
415 if [[ $_lv = $2 ]]; then
416 _vg=$(lvm lvs --noheadings -o vg_name $i 2>/dev/null)
cc02093d 417 # strip space
29b10e65
HH
418 _vg=$(echo $_vg)
419 if [[ $_vg ]]; then
420 for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2>/dev/null)
0b706743 421 do
c4e48eae 422 check_block_and_slaves $1 $(get_maj_min $_pv) && return 0
cc02093d
HH
423 done
424 fi
425 fi
533d7dc4
HH
426 done
427 return 1
428}
429
f4031e8a 430if [[ $DRACUT_INSTALL ]]; then
89d44e72
HH
431 [[ $DRACUT_RESOLVE_LAZY ]] || export DRACUT_RESOLVE_DEPS=1
432 inst_dir() {
433 [[ -e ${initdir}/"$1" ]] && return 0 # already there
f4031e8a
HH
434 $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@"
435 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@" || :
89d44e72 436 }
19612483 437
89d44e72
HH
438 inst() {
439 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
f4031e8a
HH
440 #dinfo "$DRACUT_INSTALL -l $@"
441 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
442 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
89d44e72
HH
443 }
444
445 inst_simple() {
446 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
447 [[ -e $1 ]] || return 1 # no source
f4031e8a
HH
448 $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@"
449 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@" || :
89d44e72
HH
450 }
451
452 inst_symlink() {
453 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
454 [[ -L $1 ]] || return 1
f4031e8a
HH
455 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
456 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
89d44e72
HH
457 }
458
459 dracut_install() {
f4031e8a
HH
460 #dinfo "initdir=$initdir $DRACUT_INSTALL -l $@"
461 $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
462 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
89d44e72
HH
463 }
464
465 inst_library() {
466 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
467 [[ -e $1 ]] || return 1 # no source
f4031e8a
HH
468 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
469 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
89d44e72
HH
470 }
471
472 inst_binary() {
f4031e8a
HH
473 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
474 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
89d44e72
HH
475 }
476
477 inst_script() {
f4031e8a
HH
478 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
479 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
89d44e72
HH
480 }
481
482else
483
484 # Install a directory, keeping symlinks as on the original system.
485 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
486 # will create ${initdir}/lib64, ${initdir}/lib64/file,
487 # and a symlink ${initdir}/lib -> lib64.
488 inst_dir() {
489 [[ -e ${initdir}/"$1" ]] && return 0 # already there
490
491 local _dir="$1" _part="${1%/*}" _file
492 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
493 _dir="$_part $_dir"
494 _part=${_part%/*}
495 done
496
497 # iterate over parent directories
498 for _file in $_dir; do
499 [[ -e "${initdir}/$_file" ]] && continue
500 if [[ -L $_file ]]; then
501 inst_symlink "$_file"
502 else
19612483 503 # create directory
89d44e72
HH
504 mkdir -m 0755 -p "${initdir}/$_file" || return 1
505 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
506 chmod u+w "${initdir}/$_file"
507 fi
508 done
509 }
510
511 # $1 = file to copy to ramdisk
512 # $2 (optional) Name for the file on the ramdisk
513 # Location of the image dir is assumed to be $initdir
514 # We never overwrite the target if it exists.
515 inst_simple() {
516 [[ -f "$1" ]] || return 1
517 strstr "$1" "/" || return 1
518 local _src=$1 _target="${2:-$1}"
519
520 [[ -L $_src ]] && { inst_symlink $_src $_target; return $?; }
521
522 if ! [[ -d ${initdir}/$_target ]]; then
523 [[ -e ${initdir}/$_target ]] && return 0
524 [[ -L ${initdir}/$_target ]] && return 0
525 [[ -d "${initdir}/${_target%/*}" ]] || inst_dir "${_target%/*}"
19612483 526 fi
89d44e72
HH
527 if [[ $DRACUT_FIPS_MODE ]]; then
528 # install checksum files also
529 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
530 inst "${_src%/*}/.${_src##*/}.hmac" "${_target%/*}/.${_target##*/}.hmac"
531 fi
532 fi
533 ddebug "Installing $_src"
534 cp --reflink=auto --sparse=auto -pfL "$_src" "${initdir}/$_target"
535 }
19612483 536
89d44e72
HH
537 # same as above, but specialized for symlinks
538 inst_symlink() {
539 local _src=$1 _target=${2:-$1} _realsrc
540 strstr "$1" "/" || return 1
541 [[ -L $1 ]] || return 1
542 [[ -L $initdir/$_target ]] && return 0
543 _realsrc=$(readlink -f "$_src")
544 if ! [[ -e $initdir/$_realsrc ]]; then
545 if [[ -d $_realsrc ]]; then
546 inst_dir "$_realsrc"
547 else
548 inst "$_realsrc"
549 fi
550 fi
551 [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}"
3f590c78 552
89d44e72
HH
553 ln_r "${_realsrc}" "${_target}"
554 }
22048b44 555
89d44e72
HH
556 # Same as above, but specialized to handle dynamic libraries.
557 # It handles making symlinks according to how the original library
558 # is referenced.
559 inst_library() {
560 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
561 strstr "$1" "/" || return 1
562 [[ -e $initdir/$_dest ]] && return 0
563 if [[ -L $_src ]]; then
564 if [[ $DRACUT_FIPS_MODE ]]; then
565 # install checksum files also
566 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
567 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
568 fi
569 fi
570 _reallib=$(readlink -f "$_src")
571 inst_simple "$_reallib" "$_reallib"
572 inst_dir "${_dest%/*}"
573 ln_r "${_reallib}" "${_dest}"
574 else
575 inst_simple "$_src" "$_dest"
576 fi
22048b44 577
89d44e72
HH
578 # Create additional symlinks. See rev_symlinks description.
579 for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do
580 [[ ! -e $initdir/$_symlink ]] && {
581 ddebug "Creating extra symlink: $_symlink"
582 inst_symlink $_symlink
583 }
584 done
585 }
586
587 # Same as above, but specialized to install binary executables.
588 # Install binary executable, and all shared library dependencies, if any.
589 inst_binary() {
590 local _bin _target
591 _bin=$(find_binary "$1") || return 1
592 _target=${2:-$_bin}
593 [[ -e $initdir/$_target ]] && return 0
594 local _file _line
595 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
596 # I love bash!
597 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
598 [[ $_line = 'not a dynamic executable' ]] && break
599
600 if [[ $_line =~ $_so_regex ]]; then
601 _file=${BASH_REMATCH[1]}
602 [[ -e ${initdir}/$_file ]] && continue
603 inst_library "$_file"
604 continue
605 fi
606
607 if [[ $_line =~ not\ found ]]; then
608 dfatal "Missing a shared library required by $_bin."
609 dfatal "Run \"ldd $_bin\" to find out what it is."
610 dfatal "$_line"
611 dfatal "dracut cannot create an initrd."
612 exit 1
613 fi
614 done
615 inst_simple "$_bin" "$_target"
616 }
617
618 # same as above, except for shell scripts.
619 # If your shell script does not start with shebang, it is not a shell script.
620 inst_script() {
621 local _bin
622 _bin=$(find_binary "$1") || return 1
623 shift
624 local _line _shebang_regex
625 read -r -n 80 _line <"$_bin"
626 # If debug is set, clean unprintable chars to prevent messing up the term
627 [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]')
628 _shebang_regex='(#! *)(/[^ ]+).*'
629 [[ $_line =~ $_shebang_regex ]] || return 1
630 inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@"
631 }
632
633 # general purpose installation function
634 # Same args as above.
635 inst() {
636 local _x
637
638 case $# in
639 1) ;;
640 2) [[ ! $initdir && -d $2 ]] && export initdir=$2
641 [[ $initdir = $2 ]] && set $1;;
642 3) [[ -z $initdir ]] && export initdir=$2
643 set $1 $3;;
644 *) dfatal "inst only takes 1 or 2 or 3 arguments"
645 exit 1;;
646 esac
647 for _x in inst_symlink inst_script inst_binary inst_simple; do
648 $_x "$@" && return 0
649 done
650 return 1
651 }
652
653 # dracut_install [-o ] <file> [<file> ... ]
654 # Install <file> to the initramfs image
655 # -o optionally install the <file> and don't fail, if it is not there
656 dracut_install() {
657 local _optional=no
658 if [[ $1 = '-o' ]]; then
659 _optional=yes
660 shift
661 fi
662 while (($# > 0)); do
663 if ! inst "$1" ; then
664 if [[ $_optional = yes ]]; then
665 dinfo "Skipping program $1 as it cannot be found and is" \
666 "flagged to be optional"
667 else
668 dfatal "Failed to install $1"
669 exit 1
670 fi
671 fi
672 shift
673 done
674 }
675
676fi
36b24d7c 677
a10a1416
678# find symlinks linked to given library file
679# $1 = library file
680# Function searches for symlinks by stripping version numbers appended to
681# library filename, checks if it points to the same target and finally
682# prints the list of symlinks to stdout.
683#
684# Example:
685# rev_lib_symlinks libfoo.so.8.1
686# output: libfoo.so.8 libfoo.so
687# (Only if libfoo.so.8 and libfoo.so exists on host system.)
688rev_lib_symlinks() {
689 [[ ! $1 ]] && return 0
690
691 local fn="$1" orig="$(readlink -f "$1")" links=''
692
693 [[ ${fn} =~ .*\.so\..* ]] || return 1
694
695 until [[ ${fn##*.} == so ]]; do
696 fn="${fn%.*}"
697 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
698 done
699
0b706743 700 echo "${links}"
a10a1416
701}
702
62073c30
CG
703# attempt to install any programs specified in a udev rule
704inst_rule_programs() {
705 local _prog _bin
706
707 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
708 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
604d1eb2
HH
709 if [ -x ${udevdir}/$_prog ]; then
710 _bin=${udevdir}/$_prog
711 else
712 _bin=$(find_binary "$_prog") || {
713 dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
714 continue;
715 }
716 fi
717
718 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
719 dracut_install "$_bin"
720 done
721 fi
722 if grep -qE 'RUN==?"[^ "]+' "$1"; then
723 for _prog in $(grep -E 'RUN==?"[^ "]+' "$1" | sed -r 's/.*RUN==?"([^ "]+).*/\1/'); do
724 if [ -x ${udevdir}/$_prog ]; then
725 _bin=${udevdir}/$_prog
726 else
727 _bin=$(find_binary "$_prog") || {
728 dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
729 continue;
730 }
731 fi
732
733 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
734 dracut_install "$_bin"
735 done
736 fi
737 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
738 for _prog in $(grep -E 'IMPORT==?"[^ "]+' "$1" | sed -r 's/.*IMPORT==?"([^ "]+).*/\1/'); do
739 if [ -x ${udevdir}/$_prog ]; then
740 _bin=${udevdir}/$_prog
62073c30
CG
741 else
742 _bin=$(find_binary "$_prog") || {
743 dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
744 continue;
745 }
746 fi
747
748 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
749 dracut_install "$_bin"
750 done
751 fi
752}
753
f04dc5f3
VL
754# udev rules always get installed in the same place, so
755# create a function to install them to make life simpler.
0b706743 756inst_rules() {
29b10e65 757 local _target=/etc/udev/rules.d _rule _found
fd2312e0 758
604d1eb2 759 inst_dir "${udevdir}/rules.d"
29b10e65
HH
760 inst_dir "$_target"
761 for _rule in "$@"; do
08769b7f 762 if [ "${rule#/}" = "$rule" ]; then
604d1eb2 763 for r in ${udevdir}/rules.d /etc/udev/rules.d; do
76f5fa54
HH
764 if [[ -f $r/$_rule ]]; then
765 _found="$r/$_rule"
62073c30 766 inst_rule_programs "$_found"
604d1eb2 767 inst_simple "$_found"
76f5fa54
HH
768 fi
769 done
770 fi
c97e1a76 771 for r in '' ./ $dracutbasedir/rules.d/; do
29b10e65
HH
772 if [[ -f ${r}$_rule ]]; then
773 _found="${r}$_rule"
62073c30 774 inst_rule_programs "$_found"
604d1eb2 775 inst_simple "$_found" "$_target/${_found##*/}"
c97e1a76
HH
776 fi
777 done
29b10e65 778 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
f04dc5f3
VL
779 done
780}
781
53f95456
VL
782# install function specialized for hooks
783# $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
784# All hooks should be POSIX/SuS compliant, they will be sourced by init.
785inst_hook() {
cdad82fd 786 if ! [[ -f $3 ]]; then
e27770e1
787 dfatal "Cannot install a hook ($3) that does not exist."
788 dfatal "Aborting initrd creation."
cc02093d 789 exit 1
cdad82fd 790 elif ! strstr "$hookdirs" "$1"; then
3b403b32 791 dfatal "No such hook type $1. Aborting initrd creation."
cc02093d 792 exit 1
cdad82fd 793 fi
0b53ca70 794 inst_simple "$3" "/lib/dracut/hooks/${1}/${2}${3##*/}"
53f95456
VL
795}
796
3378a54f
797# install any of listed files
798#
799# If first argument is '-d' and second some destination path, first accessible
800# source is installed into this path, otherwise it will installed in the same
801# path as source. If none of listed files was installed, function return 1.
802# On first successful installation it returns with 0 status.
803#
804# Example:
805#
806# inst_any -d /bin/foo /bin/bar /bin/baz
807#
808# Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
809# initramfs.
810inst_any() {
811 local to f
812
813 [[ $1 = '-d' ]] && to="$2" && shift 2
814
815 for f in "$@"; do
816 if [[ -e $f ]]; then
817 [[ $to ]] && inst "$f" "$to" && return 0
818 inst "$f" && return 0
819 fi
820 done
821
822 return 1
823}
824
c9143a63
JAH
825
826# inst_libdir_file [-n <pattern>] <file> [<file>...]
827# Install a <file> located on a lib directory to the initramfs image
cacaa90c 828# -n <pattern> install matching files
c9143a63 829inst_libdir_file() {
0c6565c8 830 local _files
c9143a63 831 if [[ "$1" == "-n" ]]; then
cacaa90c 832 local _pattern=$2
c9143a63
JAH
833 shift 2
834 for _dir in $libdirs; do
7828692f 835 for _i in "$@"; do
c9143a63 836 for _f in "$_dir"/$_i; do
7828692f 837 [[ "$_f" =~ $_pattern ]] || continue
0c6565c8 838 [[ -e "$_f" ]] && _files+="$_f "
c9143a63
JAH
839 done
840 done
841 done
842 else
843 for _dir in $libdirs; do
7828692f 844 for _i in "$@"; do
c9143a63 845 for _f in "$_dir"/$_i; do
0c6565c8 846 [[ -e "$_f" ]] && _files+="$_f "
c9143a63
JAH
847 done
848 done
849 done
850 fi
0c6565c8 851 [[ $_files ]] && dracut_install $_files
c9143a63
JAH
852}
853
854
87122afc
855# install function decompressing the target and handling symlinks
856# $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
857#
858# Function install targets in the same paths inside overlay but decompressed
859# and without extensions (.gz, .bz2).
860inst_decompress() {
b19c5d51 861 local _src _cmd
87122afc 862
29b10e65 863 for _src in $@
87122afc 864 do
29b10e65
HH
865 case ${_src} in
866 *.gz) _cmd='gzip -d' ;;
867 *.bz2) _cmd='bzip2 -d' ;;
87122afc
868 *) return 1 ;;
869 esac
b19c5d51 870 inst_simple ${_src}
87122afc
871 # Decompress with chosen tool. We assume that tool changes name e.g.
872 # from 'name.gz' to 'name'.
29b10e65 873 ${_cmd} "${initdir}${_src}"
87122afc
874 done
875}
876
877# It's similar to above, but if file is not compressed, performs standard
878# install.
879# $@ = list of files
880inst_opt_decompress() {
29b10e65 881 local _src
87122afc 882
29b10e65 883 for _src in $@
87122afc 884 do
29b10e65 885 inst_decompress "${_src}" || inst "${_src}"
87122afc
886 done
887}
888
7e2bca48
HH
889# module_check <dracut module>
890# execute the check() function of module-setup.sh of <dracut module>
891# or the "check" script, if module-setup.sh is not found
892# "check $hostonly" is called
95d2dabc 893module_check() {
29b10e65
HH
894 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
895 local _ret
31f1c02d
AW
896 local _forced=0
897 local _hostonly=$hostonly
898 [ $# -eq 2 ] && _forced=$2
29b10e65
HH
899 [[ -d $_moddir ]] || return 1
900 if [[ ! -f $_moddir/module-setup.sh ]]; then
95d2dabc 901 # if we do not have a check script, we are unconditionally included
29b10e65 902 [[ -x $_moddir/check ]] || return 0
31f1c02d 903 [ $_forced -ne 0 ] && unset hostonly
29b10e65 904 $_moddir/check $hostonly
31f1c02d 905 _ret=$?
95d2dabc
HH
906 else
907 unset check depends install installkernel
29b10e65 908 . $_moddir/module-setup.sh
95d2dabc 909 is_func check || return 0
31f1c02d
AW
910 [ $_forced -ne 0 ] && unset hostonly
911 check $hostonly
29b10e65 912 _ret=$?
95d2dabc 913 unset check depends install installkernel
0c2e3d12 914 fi
31f1c02d
AW
915 hostonly=$_hostonly
916 return $_ret
0c2e3d12
VL
917}
918
7e2bca48
HH
919# module_check_mount <dracut module>
920# execute the check() function of module-setup.sh of <dracut module>
921# or the "check" script, if module-setup.sh is not found
922# "mount_needs=1 check 0" is called
923module_check_mount() {
924 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
925 local _ret
926 mount_needs=1
927 [[ -d $_moddir ]] || return 1
928 if [[ ! -f $_moddir/module-setup.sh ]]; then
929 # if we do not have a check script, we are unconditionally included
930 [[ -x $_moddir/check ]] || return 0
931 mount_needs=1 $_moddir/check 0
932 _ret=$?
933 else
934 unset check depends install installkernel
935 . $_moddir/module-setup.sh
936 is_func check || return 1
937 check 0
938 _ret=$?
939 unset check depends install installkernel
940 fi
941 unset mount_needs
942 return $_ret
943}
944
945# module_depends <dracut module>
946# execute the depends() function of module-setup.sh of <dracut module>
947# or the "depends" script, if module-setup.sh is not found
95d2dabc 948module_depends() {
29b10e65
HH
949 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
950 local _ret
951 [[ -d $_moddir ]] || return 1
952 if [[ ! -f $_moddir/module-setup.sh ]]; then
95d2dabc 953 # if we do not have a check script, we have no deps
29b10e65
HH
954 [[ -x $_moddir/check ]] || return 0
955 $_moddir/check -d
95d2dabc
HH
956 return $?
957 else
958 unset check depends install installkernel
29b10e65 959 . $_moddir/module-setup.sh
95d2dabc
HH
960 is_func depends || return 0
961 depends
29b10e65 962 _ret=$?
95d2dabc 963 unset check depends install installkernel
29b10e65 964 return $_ret
33ee031c 965 fi
0c2e3d12
VL
966}
967
7e2bca48
HH
968# module_install <dracut module>
969# execute the install() function of module-setup.sh of <dracut module>
970# or the "install" script, if module-setup.sh is not found
95d2dabc 971module_install() {
29b10e65
HH
972 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
973 local _ret
974 [[ -d $_moddir ]] || return 1
975 if [[ ! -f $_moddir/module-setup.sh ]]; then
976 [[ -x $_moddir/install ]] && . "$_moddir/install"
95d2dabc
HH
977 return $?
978 else
979 unset check depends install installkernel
29b10e65 980 . $_moddir/module-setup.sh
95d2dabc
HH
981 is_func install || return 0
982 install
29b10e65 983 _ret=$?
95d2dabc 984 unset check depends install installkernel
29b10e65 985 return $_ret
95d2dabc
HH
986 fi
987}
988
7e2bca48
HH
989# module_installkernel <dracut module>
990# execute the installkernel() function of module-setup.sh of <dracut module>
991# or the "installkernel" script, if module-setup.sh is not found
95d2dabc 992module_installkernel() {
29b10e65
HH
993 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
994 local _ret
995 [[ -d $_moddir ]] || return 1
996 if [[ ! -f $_moddir/module-setup.sh ]]; then
997 [[ -x $_moddir/installkernel ]] && . "$_moddir/installkernel"
95d2dabc
HH
998 return $?
999 else
1000 unset check depends install installkernel
29b10e65 1001 . $_moddir/module-setup.sh
95d2dabc
HH
1002 is_func installkernel || return 0
1003 installkernel
29b10e65 1004 _ret=$?
95d2dabc 1005 unset check depends install installkernel
29b10e65 1006 return $_ret
95d2dabc
HH
1007 fi
1008}
1009
7e2bca48
HH
1010# check_mount <dracut module>
1011# check_mount checks, if a dracut module is needed for the given
1012# device and filesystem types in "${host_fs_types[@]}"
1b7fd0fa
AW
1013check_mount() {
1014 local _mod=$1
1015 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1016 local _ret
1017 local _moddep
1018 # If we are already scheduled to be loaded, no need to check again.
1019 strstr " $mods_to_load " " $_mod " && return 0
1020 strstr " $mods_checked_as_dep " " $_mod " && return 1
1021
1022 # This should never happen, but...
1023 [[ -d $_moddir ]] || return 1
1024
1025 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
1026
0f283709
HH
1027 if strstr " $omit_dracutmodules " " $_mod "; then
1028 dinfo "Dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
1029 return 1
1030 fi
1b7fd0fa
AW
1031
1032 if [ "${#host_fs_types[*]}" -gt 0 ]; then
1033 module_check_mount $_mod || return 1
1034 else
1035 # skip this module
1036 return 1
1037 fi
1038
1039 for _moddep in $(module_depends $_mod); do
1040 # handle deps as if they were manually added
1041 strstr " $add_dracutmodules " " $_moddep " || \
1042 add_dracutmodules+=" $_moddep "
1043 strstr " $force_add_dracutmodules " " $_moddep " || \
1044 force_add_dracutmodules+=" $_moddep "
1045 # if a module we depend on fail, fail also
0f283709
HH
1046 if ! check_module $_moddep; then
1047 derror "Dracut module '$_mod' depends on '$_moddep', which can't be installed"
1048 return 1
1049 fi
1b7fd0fa
AW
1050 done
1051
1052 strstr " $mods_to_load " " $_mod " || \
1053 mods_to_load+=" $_mod "
1054
1055 return 0
1056}
1057
7e2bca48
HH
1058# check_module <dracut module> [<use_as_dep>]
1059# check if a dracut module is to be used in the initramfs process
1060# if <use_as_dep> is set, then the process also keeps track
1061# that the modules were checked for the dependency tracking process
95d2dabc 1062check_module() {
29b10e65
HH
1063 local _mod=$1
1064 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1065 local _ret
1066 local _moddep
95d2dabc 1067 # If we are already scheduled to be loaded, no need to check again.
29b10e65
HH
1068 strstr " $mods_to_load " " $_mod " && return 0
1069 strstr " $mods_checked_as_dep " " $_mod " && return 1
95d2dabc
HH
1070
1071 # This should never happen, but...
29b10e65 1072 [[ -d $_moddir ]] || return 1
95d2dabc 1073
29b10e65 1074 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
95d2dabc 1075
0f283709
HH
1076 if strstr " $omit_dracutmodules " " $_mod "; then
1077 dinfo "Dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
1078 return 1
1079 fi
95d2dabc 1080
31f1c02d
AW
1081 if strstr " $dracutmodules $add_dracutmodules $force_add_dracutmodules" " $_mod "; then
1082 if strstr " $force_add_dracutmodules" " $_mod"; then
1083 module_check $_mod 1; ret=$?
1084 else
1085 module_check $_mod 0; ret=$?
1086 fi
95d2dabc
HH
1087 # explicit module, so also accept ret=255
1088 [[ $ret = 0 || $ret = 255 ]] || return 1
1089 else
1090 # module not in our list
1091 if [[ $dracutmodules = all ]]; then
1092 # check, if we can and should install this module
29b10e65 1093 module_check $_mod || return 1
cc02093d 1094 else
95d2dabc
HH
1095 # skip this module
1096 return 1
970e646b 1097 fi
95d2dabc 1098 fi
bb764545 1099
29b10e65 1100 for _moddep in $(module_depends $_mod); do
95d2dabc 1101 # handle deps as if they were manually added
29b10e65
HH
1102 strstr " $add_dracutmodules " " $_moddep " || \
1103 add_dracutmodules+=" $_moddep "
31f1c02d
AW
1104 strstr " $force_add_dracutmodules " " $_moddep " || \
1105 force_add_dracutmodules+=" $_moddep "
95d2dabc 1106 # if a module we depend on fail, fail also
0f283709
HH
1107 if ! check_module $_moddep; then
1108 derror "Dracut module '$_mod' depends on '$_moddep', which can't be installed"
1109 return 1
1110 fi
0c2e3d12 1111 done
70503db4 1112
29b10e65
HH
1113 strstr " $mods_to_load " " $_mod " || \
1114 mods_to_load+=" $_mod "
95d2dabc
HH
1115
1116 return 0
1117}
1118
7e2bca48
HH
1119# for_each_module_dir <func>
1120# execute "<func> <dracut module> 1"
1b7fd0fa 1121for_each_module_dir() {
29b10e65
HH
1122 local _modcheck
1123 local _mod
1124 local _moddir
1b7fd0fa
AW
1125 local _func
1126 _func=$1
29b10e65
HH
1127 for _moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1128 _mod=${_moddir##*/}; _mod=${_mod#[0-9][0-9]}
1b7fd0fa 1129 $_func $_mod 1
70503db4 1130 done
724b87a6
HH
1131
1132 # Report any missing dracut modules, the user has specified
31f1c02d 1133 _modcheck="$add_dracutmodules $force_add_dracutmodules"
29b10e65
HH
1134 [[ $dracutmodules != all ]] && _modcheck="$m $dracutmodules"
1135 for _mod in $_modcheck; do
1136 strstr "$mods_to_load" "$_mod" && continue
1137 strstr "$omit_dracutmodules" "$_mod" && continue
89d44e72 1138 derror "Dracut module \"$_mod\" cannot be found or installed."
724b87a6 1139 done
0c2e3d12
VL
1140}
1141
ddfd1d10
VL
1142# Install a single kernel module along with any firmware it may require.
1143# $1 = full path to kernel module to install
1144install_kmod_with_fw() {
0b440844 1145 # no need to go further if the module is already installed
379c34d2 1146
0b706743
1147 [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
1148 && return 0
fcbcb252 1149
f4031e8a
HH
1150 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -e "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}" ]]; then
1151 read ret < "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
d8aeb3a7
HH
1152 return $ret
1153 fi
379c34d2 1154
fcbcb252
HH
1155 if [[ $omit_drivers ]]; then
1156 local _kmod=${1##*/}
1157 _kmod=${_kmod%.ko}
1158 _kmod=${_kmod/-/_}
34248c92
HH
1159 if [[ "$_kmod" =~ $omit_drivers ]]; then
1160 dinfo "Omitting driver $_kmod"
7abd4264 1161 return 0
34248c92
HH
1162 fi
1163 if [[ "${1##*/lib/modules/$kernel/}" =~ $omit_drivers ]]; then
fcbcb252 1164 dinfo "Omitting driver $_kmod"
7abd4264 1165 return 0
fcbcb252
HH
1166 fi
1167 fi
1168
d8aeb3a7
HH
1169 inst_simple "$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
1170 ret=$?
f4031e8a
HH
1171 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1172 [[ -d "$DRACUT_KERNEL_LAZY_HASHDIR" ]] && \
1173 echo $ret > "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
d8aeb3a7 1174 (($ret != 0)) && return $ret
e6024e00
JR
1175
1176 local _modname=${1##*/} _fwdir _found _fw
1177 _modname=${_modname%.ko*}
29b10e65
HH
1178 for _fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do
1179 _found=''
1180 for _fwdir in $fw_dir; do
1181 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1182 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1183 _found=yes
cc02093d
HH
1184 fi
1185 done
29b10e65 1186 if [[ $_found != yes ]]; then
fe1484f3 1187 if ! [[ -d $(echo /sys/module/${_modname//-/_}|{ read a b; echo $a; }) ]]; then
29b10e65
HH
1188 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
1189 "\"${_modname}.ko\""
e2cdb570 1190 else
29b10e65
HH
1191 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
1192 "\"${_modname}.ko\""
e2cdb570 1193 fi
cc02093d 1194 fi
ddfd1d10 1195 done
0b440844 1196 return 0
ddfd1d10
VL
1197}
1198
1199# Do something with all the dependencies of a kernel module.
1200# Note that kernel modules depend on themselves using the technique we use
0b706743
1201# $1 = function to call for each dependency we find
1202# It will be passed the full path to the found kernel module
ddfd1d10
VL
1203# $2 = module to get dependencies for
1204# rest of args = arguments to modprobe
c32bda6b 1205# _fderr specifies FD passed from surrounding scope
ddfd1d10 1206for_each_kmod_dep() {
7abd4264 1207 local _func=$1 _kmod=$2 _cmd _modpath _options
ddfd1d10 1208 shift 2
c32bda6b 1209 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
29b10e65
HH
1210 while read _cmd _modpath _options; do
1211 [[ $_cmd = insmod ]] || continue
1212 $_func ${_modpath} || exit $?
0b706743 1213 done
0b706743 1214 )
ddfd1d10
VL
1215}
1216
70c6b773 1217dracut_kernel_post() {
d8aeb3a7
HH
1218 local _moddirname=${srcmods%%/lib/modules/*}
1219
f4031e8a 1220 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" ]]; then
ec61f0a3
HH
1221 xargs -r modprobe -a ${_moddirname+-d ${_moddirname}/} \
1222 --ignore-install --show-depends --set-version $kernel \
f4031e8a 1223 < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" 2>/dev/null \
89d44e72
HH
1224 | sort -u \
1225 | while read _cmd _modpath _options; do
1226 [[ $_cmd = insmod ]] || continue
1227 echo "$_modpath"
f4031e8a 1228 done > "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
d8aeb3a7 1229
89d44e72 1230 (
73575f11
HH
1231 if [[ $DRACUT_INSTALL ]] && [[ -z $_moddirname ]]; then
1232 xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
89d44e72
HH
1233 else
1234 while read _modpath; do
1235 local _destpath=$_modpath
1236 [[ $_moddirname ]] && _destpath=${_destpath##$_moddirname/}
1237 _destpath=${_destpath##*/lib/modules/$kernel/}
1238 inst_simple "$_modpath" "/lib/modules/$kernel/${_destpath}" || exit $?
f4031e8a 1239 done < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
89d44e72
HH
1240 fi
1241 ) &
d8aeb3a7 1242
73575f11
HH
1243 if [[ $DRACUT_INSTALL ]]; then
1244 xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" \
89d44e72
HH
1245 | while read line; do
1246 for _fwdir in $fw_dir; do
1247 echo $_fwdir/$line;
1248 done;
73575f11 1249 done | xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a -o
89d44e72 1250 else
73575f11 1251 for _fw in $(xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"); do
89d44e72
HH
1252 for _fwdir in $fw_dir; do
1253 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1254 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1255 break
1256 fi
1257 done
1258 done
1259 fi
d8aeb3a7 1260
89d44e72
HH
1261 wait
1262 fi
70c6b773
HH
1263
1264 for _f in modules.builtin.bin modules.builtin; do
1265 [[ $srcmods/$_f ]] && break
1266 done || {
1267 dfatal "No modules.builtin.bin and modules.builtin found!"
1268 return 1
1269 }
1270
1271 for _f in modules.builtin.bin modules.builtin modules.order; do
1272 [[ $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f"
1273 done
1274
1275 # generate module dependencies for the initrd
1276 if [[ -d $initdir/lib/modules/$kernel ]] && \
1277 ! depmod -a -b "$initdir" $kernel; then
1278 dfatal "\"depmod -a $kernel\" failed."
1279 exit 1
1280 fi
1281
f4031e8a 1282 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && rm -fr "$DRACUT_KERNEL_LAZY_HASHDIR"
d8aeb3a7 1283}
fe1484f3 1284
881eda69 1285find_kernel_modules_by_path () (
74405477 1286 local _OLDIFS
881eda69 1287 if ! [[ $hostonly ]]; then
74405477
HH
1288 _OLDIFS=$IFS
1289 IFS=:
fe1484f3 1290 while read a rest; do
d8aeb3a7 1291 [[ $a = kernel*/$1/* ]] || continue
74405477 1292 echo $srcmods/$a
fe1484f3 1293 done < $srcmods/modules.dep
74405477 1294 IFS=$_OLDIFS
881eda69 1295 else
fe1484f3 1296 ( cd /sys/module; echo *; ) \
194b80f9
HH
1297 | xargs -r modinfo -F filename -k $kernel 2>/dev/null \
1298 | while read a; do
1299 [[ $a = kernel*/$1/* ]] || continue
1300 echo $srcmods/$a
1301 done
881eda69 1302 fi
74405477 1303 return 0
881eda69 1304)
4073c815 1305
881eda69
JR
1306find_kernel_modules () {
1307 find_kernel_modules_by_path drivers
1308}
1309
338b43cd
HH
1310# instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1311# instmods [-c [-s]] <kernel subsystem>
ddfd1d10 1312# install kernel modules along with all their dependencies.
7e2bca48 1313# <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
6fa0c3d6 1314instmods() {
0c1a8ebc 1315 [[ $no_kernel = yes ]] && return
c32bda6b
MS
1316 # called [sub]functions inherit _fderr
1317 local _fderr=9
a6d3be9d 1318 local _check=no
338b43cd 1319 local _silent=no
a6d3be9d
AW
1320 if [[ $1 = '-c' ]]; then
1321 _check=yes
1322 shift
1323 fi
881eda69 1324
338b43cd
HH
1325 if [[ $1 = '-s' ]]; then
1326 _silent=yes
1327 shift
1328 fi
1329
881eda69 1330 function inst1mod() {
a6d3be9d 1331 local _ret=0 _mod="$1"
29b10e65 1332 case $_mod in
0b706743 1333 =*)
d8aeb3a7
HH
1334 ( [[ "$_mpargs" ]] && echo $_mpargs
1335 find_kernel_modules_by_path "${_mod#=}" ) \
1336 | instmods
1337 ((_ret+=$?))
cc02093d 1338 ;;
0024702f 1339 --*) _mpargs+=" $_mod" ;;
7abd4264 1340 i2o_scsi) return 0;; # Do not load this diagnostic-only module
34248c92 1341 *)
379c34d2 1342 _mod=${_mod##*/}
6568d86a 1343 # if we are already installed, skip this module and go on
cc02093d 1344 # to the next one.
f4031e8a
HH
1345 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1346 [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko" ]]; then
1347 read _ret <"$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko"
d8aeb3a7
HH
1348 return $_ret
1349 fi
881eda69 1350
34248c92
HH
1351 if [[ $omit_drivers ]] && [[ "$1" =~ $omit_drivers ]]; then
1352 dinfo "Omitting driver ${_mod##$srcmods}"
7abd4264 1353 return 0
34248c92 1354 fi
cc02093d
HH
1355 # If we are building a host-specific initramfs and this
1356 # module is not already loaded, move on to the next one.
fe1484f3
HH
1357 [[ $hostonly ]] \
1358 && ! [[ -d $(echo /sys/module/${_mod//-/_}|{ read a b; echo $a; }) ]] \
1359 && ! [[ "$add_drivers" =~ " ${_mod} " ]] \
7abd4264 1360 && return 0
04b56f3a 1361
f4031e8a 1362 if [[ "$_check" = "yes" ]] || ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
d8aeb3a7
HH
1363 # We use '-d' option in modprobe only if modules prefix path
1364 # differs from default '/'. This allows us to use Dracut with
1365 # old version of modprobe which doesn't have '-d' option.
1366 local _moddirname=${srcmods%%/lib/modules/*}
1367 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1368
1369 # ok, load the module, all its dependencies, and any firmware
1370 # it may require
1371 for_each_kmod_dep install_kmod_with_fw $_mod \
1372 --set-version $kernel ${_moddirname} $_mpargs
1373 ((_ret+=$?))
1374 else
f4031e8a
HH
1375 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1376 echo $_mod >> "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist"
d8aeb3a7 1377 fi
cc02093d 1378 ;;
0b706743 1379 esac
a6d3be9d 1380 return $_ret
881eda69
JR
1381 }
1382
f9708da2 1383 function instmods_1() {
a6d3be9d 1384 local _mod _mpargs
f9708da2
JR
1385 if (($# == 0)); then # filenames from stdin
1386 while read _mod; do
a6d3be9d 1387 inst1mod "${_mod%.ko*}" || {
338b43cd
HH
1388 if [[ "$_check" == "yes" ]]; then
1389 [[ "$_silent" == "no" ]] && dfatal "Failed to install $_mod"
a6d3be9d
AW
1390 return 1
1391 fi
1392 }
f9708da2
JR
1393 done
1394 fi
1395 while (($# > 0)); do # filenames as arguments
a6d3be9d 1396 inst1mod ${1%.ko*} || {
338b43cd
HH
1397 if [[ "$_check" == "yes" ]]; then
1398 [[ "$_silent" == "no" ]] && dfatal "Failed to install $1"
a6d3be9d
AW
1399 return 1
1400 fi
1401 }
f9708da2 1402 shift
881eda69 1403 done
a6d3be9d 1404 return 0
f9708da2
JR
1405 }
1406
a6d3be9d 1407 local _ret _filter_not_found='FATAL: Module .* not found.'
c32bda6b
MS
1408 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1409 # redirections, but that would make dracut require bash4 at least.
1410 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
86191581 1411 | while read line; do [[ "$line" =~ $_filter_not_found ]] || echo $line;done | derror
a6d3be9d 1412 _ret=$?
a6d3be9d 1413 return $_ret
0b706743 1414}