]> git.ipfire.org Git - thirdparty/dracut.git/blame - dracut-functions.sh
ro_mnt - option at build time to force ro mount of / and /usr
[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
ec5e5ae2
HH
360# find_mp_fstype <mountpoint>
361# Echo the filesystem type for a given mountpoint.
81672479
HH
362# /proc/self/mountinfo is taken as the primary source of information
363# and /etc/fstab is used as a fallback.
364# No newline is appended!
365# Example:
ec5e5ae2 366# $ find_mp_fstype /;echo
81672479
HH
367# ext4
368find_mp_fstype() {
369 local _x _mpt _majmin _dev _fs _maj _min
370 while read _x _x _majmin _x _mpt _x _x _fs _dev _x; do
371 [[ $_mpt = $1 ]] || continue
372 echo -n $_fs;
373 return 0;
374 done < /proc/self/mountinfo
375
376 # fall back to /etc/fstab
377 while read _dev _mpt _fs _x; do
378 [[ $_mpt = $1 ]] || continue
379 echo -n $_fs;
380 return 0;
381 done < /etc/fstab
382
383 return 1
384}
385
386
7ae5d9d1 387# finds the major:minor of the block device backing the root filesystem.
f76ef3aa
VL
388find_root_block_device() { find_block_device /; }
389
7e2bca48
HH
390# for_each_host_dev_fs <func>
391# Execute "<func> <dev> <filesystem>" for every "<dev>|<fs>" pair found
392# in ${host_fs_types[@]}
480d772f
HH
393for_each_host_dev_fs()
394{
395 local _func="$1"
d0096de7
AW
396 local _dev
397 local _fs
2efa546f 398 local _ret=1
480d772f
HH
399 for f in ${host_fs_types[@]}; do
400 OLDIFS="$IFS"
401 IFS="|"
402 set -- $f
403 IFS="$OLDIFS"
d0096de7
AW
404 _dev="$1"
405 [[ -b "$_dev" ]] || continue
406 _fs="$2"
2efa546f 407 $_func $_dev $_fs && _ret=0
480d772f 408 done
2efa546f 409 return $_ret
480d772f
HH
410}
411
17829e94
VL
412# Walk all the slave relationships for a given block device.
413# Stop when our helper function returns success
414# $1 = function to call on every found block device
415# $2 = block device in major:minor format
0b706743 416check_block_and_slaves() {
29b10e65 417 local _x
17829e94
VL
418 [[ -b /dev/block/$2 ]] || return 1 # Not a block device? So sorry.
419 "$1" $2 && return
533d7dc4 420 check_vol_slaves "$@" && return 0
3478b314 421 if [[ -f /sys/dev/block/$2/../dev ]]; then
0b706743 422 check_block_and_slaves $1 $(cat "/sys/dev/block/$2/../dev") && return 0
a3afcf2a 423 fi
17829e94 424 [[ -d /sys/dev/block/$2/slaves ]] || return 1
29b10e65
HH
425 for _x in /sys/dev/block/$2/slaves/*/dev; do
426 [[ -f $_x ]] || continue
427 check_block_and_slaves $1 $(cat "$_x") && return 0
17829e94
VL
428 done
429 return 1
430}
431
533d7dc4
HH
432# ugly workaround for the lvm design
433# There is no volume group device,
434# so, there are no slave devices for volume groups.
435# Logical volumes only have the slave devices they really live on,
436# but you cannot create the logical volume without the volume group.
95bde758 437# And the volume group might be bigger than the devices the LV needs.
533d7dc4 438check_vol_slaves() {
29b10e65 439 local _lv _vg _pv
0b706743 440 for i in /dev/mapper/*; do
c4e48eae 441 _lv=$(get_maj_min $i)
29b10e65
HH
442 if [[ $_lv = $2 ]]; then
443 _vg=$(lvm lvs --noheadings -o vg_name $i 2>/dev/null)
cc02093d 444 # strip space
29b10e65
HH
445 _vg=$(echo $_vg)
446 if [[ $_vg ]]; then
447 for _pv in $(lvm vgs --noheadings -o pv_name "$_vg" 2>/dev/null)
0b706743 448 do
c4e48eae 449 check_block_and_slaves $1 $(get_maj_min $_pv) && return 0
cc02093d
HH
450 done
451 fi
452 fi
533d7dc4
HH
453 done
454 return 1
455}
456
f4031e8a 457if [[ $DRACUT_INSTALL ]]; then
89d44e72
HH
458 [[ $DRACUT_RESOLVE_LAZY ]] || export DRACUT_RESOLVE_DEPS=1
459 inst_dir() {
460 [[ -e ${initdir}/"$1" ]] && return 0 # already there
f4031e8a
HH
461 $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@"
462 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} -d "$@" || :
89d44e72 463 }
19612483 464
89d44e72
HH
465 inst() {
466 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
f4031e8a
HH
467 #dinfo "$DRACUT_INSTALL -l $@"
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_simple() {
473 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
474 [[ -e $1 ]] || return 1 # no source
f4031e8a
HH
475 $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@"
476 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} "$@" || :
89d44e72
HH
477 }
478
479 inst_symlink() {
480 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
481 [[ -L $1 ]] || return 1
f4031e8a
HH
482 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
483 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
89d44e72
HH
484 }
485
486 dracut_install() {
f4031e8a
HH
487 #dinfo "initdir=$initdir $DRACUT_INSTALL -l $@"
488 $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
489 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} -a ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
89d44e72
HH
490 }
491
492 inst_library() {
493 [[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
494 [[ -e $1 ]] || return 1 # no source
f4031e8a
HH
495 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
496 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
89d44e72
HH
497 }
498
499 inst_binary() {
f4031e8a
HH
500 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
501 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
89d44e72
HH
502 }
503
504 inst_script() {
f4031e8a
HH
505 $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@"
506 (($? != 0)) && derror $DRACUT_INSTALL ${initdir+-D "$initdir"} ${DRACUT_RESOLVE_DEPS+-l} ${DRACUT_FIPS_MODE+-H} "$@" || :
89d44e72
HH
507 }
508
509else
510
511 # Install a directory, keeping symlinks as on the original system.
512 # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
513 # will create ${initdir}/lib64, ${initdir}/lib64/file,
514 # and a symlink ${initdir}/lib -> lib64.
515 inst_dir() {
516 [[ -e ${initdir}/"$1" ]] && return 0 # already there
517
518 local _dir="$1" _part="${1%/*}" _file
519 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
520 _dir="$_part $_dir"
521 _part=${_part%/*}
522 done
523
524 # iterate over parent directories
525 for _file in $_dir; do
526 [[ -e "${initdir}/$_file" ]] && continue
527 if [[ -L $_file ]]; then
528 inst_symlink "$_file"
529 else
19612483 530 # create directory
89d44e72
HH
531 mkdir -m 0755 -p "${initdir}/$_file" || return 1
532 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
533 chmod u+w "${initdir}/$_file"
534 fi
535 done
536 }
537
538 # $1 = file to copy to ramdisk
539 # $2 (optional) Name for the file on the ramdisk
540 # Location of the image dir is assumed to be $initdir
541 # We never overwrite the target if it exists.
542 inst_simple() {
543 [[ -f "$1" ]] || return 1
544 strstr "$1" "/" || return 1
545 local _src=$1 _target="${2:-$1}"
546
547 [[ -L $_src ]] && { inst_symlink $_src $_target; return $?; }
548
549 if ! [[ -d ${initdir}/$_target ]]; then
550 [[ -e ${initdir}/$_target ]] && return 0
551 [[ -L ${initdir}/$_target ]] && return 0
552 [[ -d "${initdir}/${_target%/*}" ]] || inst_dir "${_target%/*}"
19612483 553 fi
89d44e72
HH
554 if [[ $DRACUT_FIPS_MODE ]]; then
555 # install checksum files also
556 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
557 inst "${_src%/*}/.${_src##*/}.hmac" "${_target%/*}/.${_target##*/}.hmac"
558 fi
559 fi
560 ddebug "Installing $_src"
561 cp --reflink=auto --sparse=auto -pfL "$_src" "${initdir}/$_target"
562 }
19612483 563
89d44e72
HH
564 # same as above, but specialized for symlinks
565 inst_symlink() {
566 local _src=$1 _target=${2:-$1} _realsrc
567 strstr "$1" "/" || return 1
568 [[ -L $1 ]] || return 1
569 [[ -L $initdir/$_target ]] && return 0
570 _realsrc=$(readlink -f "$_src")
571 if ! [[ -e $initdir/$_realsrc ]]; then
572 if [[ -d $_realsrc ]]; then
573 inst_dir "$_realsrc"
574 else
575 inst "$_realsrc"
576 fi
577 fi
578 [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}"
3f590c78 579
89d44e72
HH
580 ln_r "${_realsrc}" "${_target}"
581 }
22048b44 582
89d44e72
HH
583 # Same as above, but specialized to handle dynamic libraries.
584 # It handles making symlinks according to how the original library
585 # is referenced.
586 inst_library() {
587 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
588 strstr "$1" "/" || return 1
589 [[ -e $initdir/$_dest ]] && return 0
590 if [[ -L $_src ]]; then
591 if [[ $DRACUT_FIPS_MODE ]]; then
592 # install checksum files also
593 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
594 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
595 fi
596 fi
597 _reallib=$(readlink -f "$_src")
598 inst_simple "$_reallib" "$_reallib"
599 inst_dir "${_dest%/*}"
600 ln_r "${_reallib}" "${_dest}"
601 else
602 inst_simple "$_src" "$_dest"
603 fi
22048b44 604
89d44e72
HH
605 # Create additional symlinks. See rev_symlinks description.
606 for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do
607 [[ ! -e $initdir/$_symlink ]] && {
608 ddebug "Creating extra symlink: $_symlink"
609 inst_symlink $_symlink
610 }
611 done
612 }
613
614 # Same as above, but specialized to install binary executables.
615 # Install binary executable, and all shared library dependencies, if any.
616 inst_binary() {
617 local _bin _target
618 _bin=$(find_binary "$1") || return 1
619 _target=${2:-$_bin}
620 [[ -e $initdir/$_target ]] && return 0
621 local _file _line
622 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
623 # I love bash!
624 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
625 [[ $_line = 'not a dynamic executable' ]] && break
626
627 if [[ $_line =~ $_so_regex ]]; then
628 _file=${BASH_REMATCH[1]}
629 [[ -e ${initdir}/$_file ]] && continue
630 inst_library "$_file"
631 continue
632 fi
633
634 if [[ $_line =~ not\ found ]]; then
635 dfatal "Missing a shared library required by $_bin."
636 dfatal "Run \"ldd $_bin\" to find out what it is."
637 dfatal "$_line"
638 dfatal "dracut cannot create an initrd."
639 exit 1
640 fi
641 done
642 inst_simple "$_bin" "$_target"
643 }
644
645 # same as above, except for shell scripts.
646 # If your shell script does not start with shebang, it is not a shell script.
647 inst_script() {
648 local _bin
649 _bin=$(find_binary "$1") || return 1
650 shift
651 local _line _shebang_regex
652 read -r -n 80 _line <"$_bin"
653 # If debug is set, clean unprintable chars to prevent messing up the term
654 [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]')
655 _shebang_regex='(#! *)(/[^ ]+).*'
656 [[ $_line =~ $_shebang_regex ]] || return 1
657 inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@"
658 }
659
660 # general purpose installation function
661 # Same args as above.
662 inst() {
663 local _x
664
665 case $# in
666 1) ;;
667 2) [[ ! $initdir && -d $2 ]] && export initdir=$2
668 [[ $initdir = $2 ]] && set $1;;
669 3) [[ -z $initdir ]] && export initdir=$2
670 set $1 $3;;
671 *) dfatal "inst only takes 1 or 2 or 3 arguments"
672 exit 1;;
673 esac
674 for _x in inst_symlink inst_script inst_binary inst_simple; do
675 $_x "$@" && return 0
676 done
677 return 1
678 }
679
680 # dracut_install [-o ] <file> [<file> ... ]
681 # Install <file> to the initramfs image
682 # -o optionally install the <file> and don't fail, if it is not there
683 dracut_install() {
684 local _optional=no
685 if [[ $1 = '-o' ]]; then
686 _optional=yes
687 shift
688 fi
689 while (($# > 0)); do
690 if ! inst "$1" ; then
691 if [[ $_optional = yes ]]; then
692 dinfo "Skipping program $1 as it cannot be found and is" \
693 "flagged to be optional"
694 else
695 dfatal "Failed to install $1"
696 exit 1
697 fi
698 fi
699 shift
700 done
701 }
702
703fi
36b24d7c 704
a10a1416
705# find symlinks linked to given library file
706# $1 = library file
707# Function searches for symlinks by stripping version numbers appended to
708# library filename, checks if it points to the same target and finally
709# prints the list of symlinks to stdout.
710#
711# Example:
712# rev_lib_symlinks libfoo.so.8.1
713# output: libfoo.so.8 libfoo.so
714# (Only if libfoo.so.8 and libfoo.so exists on host system.)
715rev_lib_symlinks() {
716 [[ ! $1 ]] && return 0
717
718 local fn="$1" orig="$(readlink -f "$1")" links=''
719
720 [[ ${fn} =~ .*\.so\..* ]] || return 1
721
722 until [[ ${fn##*.} == so ]]; do
723 fn="${fn%.*}"
724 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
725 done
726
0b706743 727 echo "${links}"
a10a1416
728}
729
62073c30
CG
730# attempt to install any programs specified in a udev rule
731inst_rule_programs() {
732 local _prog _bin
733
734 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
735 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
604d1eb2
HH
736 if [ -x ${udevdir}/$_prog ]; then
737 _bin=${udevdir}/$_prog
738 else
739 _bin=$(find_binary "$_prog") || {
c072e131 740 dinfo "Skipping program $_prog using in udev rule $(${1##*/}) as it cannot be found"
604d1eb2
HH
741 continue;
742 }
743 fi
744
c072e131 745 #dinfo "Installing $_bin due to it's use in the udev rule $(${1##*/})"
604d1eb2
HH
746 dracut_install "$_bin"
747 done
748 fi
749 if grep -qE 'RUN==?"[^ "]+' "$1"; then
750 for _prog in $(grep -E 'RUN==?"[^ "]+' "$1" | sed -r 's/.*RUN==?"([^ "]+).*/\1/'); do
751 if [ -x ${udevdir}/$_prog ]; then
752 _bin=${udevdir}/$_prog
753 else
754 _bin=$(find_binary "$_prog") || {
c072e131 755 dinfo "Skipping program $_prog using in udev rule $(${1##*/}) as it cannot be found"
604d1eb2
HH
756 continue;
757 }
758 fi
759
c072e131 760 #dinfo "Installing $_bin due to it's use in the udev rule $(${1##*/})"
604d1eb2
HH
761 dracut_install "$_bin"
762 done
763 fi
764 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
765 for _prog in $(grep -E 'IMPORT==?"[^ "]+' "$1" | sed -r 's/.*IMPORT==?"([^ "]+).*/\1/'); do
766 if [ -x ${udevdir}/$_prog ]; then
767 _bin=${udevdir}/$_prog
62073c30
CG
768 else
769 _bin=$(find_binary "$_prog") || {
c072e131 770 dinfo "Skipping program $_prog using in udev rule $(${1##*/}) as it cannot be found"
62073c30
CG
771 continue;
772 }
773 fi
774
c072e131 775 #dinfo "Installing $_bin due to it's use in the udev rule $(${1##*/})"
62073c30
CG
776 dracut_install "$_bin"
777 done
778 fi
779}
780
f04dc5f3
VL
781# udev rules always get installed in the same place, so
782# create a function to install them to make life simpler.
0b706743 783inst_rules() {
29b10e65 784 local _target=/etc/udev/rules.d _rule _found
fd2312e0 785
604d1eb2 786 inst_dir "${udevdir}/rules.d"
29b10e65
HH
787 inst_dir "$_target"
788 for _rule in "$@"; do
08769b7f 789 if [ "${rule#/}" = "$rule" ]; then
604d1eb2 790 for r in ${udevdir}/rules.d /etc/udev/rules.d; do
76f5fa54
HH
791 if [[ -f $r/$_rule ]]; then
792 _found="$r/$_rule"
62073c30 793 inst_rule_programs "$_found"
604d1eb2 794 inst_simple "$_found"
76f5fa54
HH
795 fi
796 done
797 fi
c97e1a76 798 for r in '' ./ $dracutbasedir/rules.d/; do
29b10e65
HH
799 if [[ -f ${r}$_rule ]]; then
800 _found="${r}$_rule"
62073c30 801 inst_rule_programs "$_found"
604d1eb2 802 inst_simple "$_found" "$_target/${_found##*/}"
c97e1a76
HH
803 fi
804 done
29b10e65 805 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
f04dc5f3
VL
806 done
807}
808
53f95456
VL
809# install function specialized for hooks
810# $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
811# All hooks should be POSIX/SuS compliant, they will be sourced by init.
812inst_hook() {
cdad82fd 813 if ! [[ -f $3 ]]; then
e27770e1
814 dfatal "Cannot install a hook ($3) that does not exist."
815 dfatal "Aborting initrd creation."
cc02093d 816 exit 1
cdad82fd 817 elif ! strstr "$hookdirs" "$1"; then
3b403b32 818 dfatal "No such hook type $1. Aborting initrd creation."
cc02093d 819 exit 1
cdad82fd 820 fi
0b53ca70 821 inst_simple "$3" "/lib/dracut/hooks/${1}/${2}${3##*/}"
53f95456
VL
822}
823
3378a54f
824# install any of listed files
825#
826# If first argument is '-d' and second some destination path, first accessible
827# source is installed into this path, otherwise it will installed in the same
828# path as source. If none of listed files was installed, function return 1.
829# On first successful installation it returns with 0 status.
830#
831# Example:
832#
833# inst_any -d /bin/foo /bin/bar /bin/baz
834#
835# Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
836# initramfs.
837inst_any() {
838 local to f
839
840 [[ $1 = '-d' ]] && to="$2" && shift 2
841
842 for f in "$@"; do
843 if [[ -e $f ]]; then
844 [[ $to ]] && inst "$f" "$to" && return 0
845 inst "$f" && return 0
846 fi
847 done
848
849 return 1
850}
851
c9143a63
JAH
852
853# inst_libdir_file [-n <pattern>] <file> [<file>...]
854# Install a <file> located on a lib directory to the initramfs image
cacaa90c 855# -n <pattern> install matching files
c9143a63 856inst_libdir_file() {
0c6565c8 857 local _files
c9143a63 858 if [[ "$1" == "-n" ]]; then
cacaa90c 859 local _pattern=$2
c9143a63
JAH
860 shift 2
861 for _dir in $libdirs; do
7828692f 862 for _i in "$@"; do
c9143a63 863 for _f in "$_dir"/$_i; do
7828692f 864 [[ "$_f" =~ $_pattern ]] || continue
0c6565c8 865 [[ -e "$_f" ]] && _files+="$_f "
c9143a63
JAH
866 done
867 done
868 done
869 else
870 for _dir in $libdirs; do
7828692f 871 for _i in "$@"; do
c9143a63 872 for _f in "$_dir"/$_i; do
0c6565c8 873 [[ -e "$_f" ]] && _files+="$_f "
c9143a63
JAH
874 done
875 done
876 done
877 fi
0c6565c8 878 [[ $_files ]] && dracut_install $_files
c9143a63
JAH
879}
880
881
87122afc
882# install function decompressing the target and handling symlinks
883# $@ = list of compressed (gz or bz2) files or symlinks pointing to such files
884#
885# Function install targets in the same paths inside overlay but decompressed
886# and without extensions (.gz, .bz2).
887inst_decompress() {
b19c5d51 888 local _src _cmd
87122afc 889
29b10e65 890 for _src in $@
87122afc 891 do
29b10e65
HH
892 case ${_src} in
893 *.gz) _cmd='gzip -d' ;;
894 *.bz2) _cmd='bzip2 -d' ;;
87122afc
895 *) return 1 ;;
896 esac
b19c5d51 897 inst_simple ${_src}
87122afc
898 # Decompress with chosen tool. We assume that tool changes name e.g.
899 # from 'name.gz' to 'name'.
29b10e65 900 ${_cmd} "${initdir}${_src}"
87122afc
901 done
902}
903
904# It's similar to above, but if file is not compressed, performs standard
905# install.
906# $@ = list of files
907inst_opt_decompress() {
29b10e65 908 local _src
87122afc 909
29b10e65 910 for _src in $@
87122afc 911 do
29b10e65 912 inst_decompress "${_src}" || inst "${_src}"
87122afc
913 done
914}
915
7e2bca48
HH
916# module_check <dracut module>
917# execute the check() function of module-setup.sh of <dracut module>
918# or the "check" script, if module-setup.sh is not found
919# "check $hostonly" is called
95d2dabc 920module_check() {
29b10e65
HH
921 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
922 local _ret
31f1c02d
AW
923 local _forced=0
924 local _hostonly=$hostonly
925 [ $# -eq 2 ] && _forced=$2
29b10e65
HH
926 [[ -d $_moddir ]] || return 1
927 if [[ ! -f $_moddir/module-setup.sh ]]; then
95d2dabc 928 # if we do not have a check script, we are unconditionally included
29b10e65 929 [[ -x $_moddir/check ]] || return 0
31f1c02d 930 [ $_forced -ne 0 ] && unset hostonly
29b10e65 931 $_moddir/check $hostonly
31f1c02d 932 _ret=$?
95d2dabc
HH
933 else
934 unset check depends install installkernel
29b10e65 935 . $_moddir/module-setup.sh
95d2dabc 936 is_func check || return 0
31f1c02d
AW
937 [ $_forced -ne 0 ] && unset hostonly
938 check $hostonly
29b10e65 939 _ret=$?
95d2dabc 940 unset check depends install installkernel
0c2e3d12 941 fi
31f1c02d
AW
942 hostonly=$_hostonly
943 return $_ret
0c2e3d12
VL
944}
945
7e2bca48
HH
946# module_check_mount <dracut module>
947# execute the check() function of module-setup.sh of <dracut module>
948# or the "check" script, if module-setup.sh is not found
949# "mount_needs=1 check 0" is called
950module_check_mount() {
951 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
952 local _ret
953 mount_needs=1
954 [[ -d $_moddir ]] || return 1
955 if [[ ! -f $_moddir/module-setup.sh ]]; then
956 # if we do not have a check script, we are unconditionally included
957 [[ -x $_moddir/check ]] || return 0
958 mount_needs=1 $_moddir/check 0
959 _ret=$?
960 else
961 unset check depends install installkernel
962 . $_moddir/module-setup.sh
963 is_func check || return 1
964 check 0
965 _ret=$?
966 unset check depends install installkernel
967 fi
968 unset mount_needs
969 return $_ret
970}
971
972# module_depends <dracut module>
973# execute the depends() function of module-setup.sh of <dracut module>
974# or the "depends" script, if module-setup.sh is not found
95d2dabc 975module_depends() {
29b10e65
HH
976 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
977 local _ret
978 [[ -d $_moddir ]] || return 1
979 if [[ ! -f $_moddir/module-setup.sh ]]; then
95d2dabc 980 # if we do not have a check script, we have no deps
29b10e65
HH
981 [[ -x $_moddir/check ]] || return 0
982 $_moddir/check -d
95d2dabc
HH
983 return $?
984 else
985 unset check depends install installkernel
29b10e65 986 . $_moddir/module-setup.sh
95d2dabc
HH
987 is_func depends || return 0
988 depends
29b10e65 989 _ret=$?
95d2dabc 990 unset check depends install installkernel
29b10e65 991 return $_ret
33ee031c 992 fi
0c2e3d12
VL
993}
994
7e2bca48
HH
995# module_install <dracut module>
996# execute the install() function of module-setup.sh of <dracut module>
997# or the "install" script, if module-setup.sh is not found
95d2dabc 998module_install() {
29b10e65
HH
999 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1000 local _ret
1001 [[ -d $_moddir ]] || return 1
1002 if [[ ! -f $_moddir/module-setup.sh ]]; then
1003 [[ -x $_moddir/install ]] && . "$_moddir/install"
95d2dabc
HH
1004 return $?
1005 else
1006 unset check depends install installkernel
29b10e65 1007 . $_moddir/module-setup.sh
95d2dabc
HH
1008 is_func install || return 0
1009 install
29b10e65 1010 _ret=$?
95d2dabc 1011 unset check depends install installkernel
29b10e65 1012 return $_ret
95d2dabc
HH
1013 fi
1014}
1015
7e2bca48
HH
1016# module_installkernel <dracut module>
1017# execute the installkernel() function of module-setup.sh of <dracut module>
1018# or the "installkernel" script, if module-setup.sh is not found
95d2dabc 1019module_installkernel() {
29b10e65
HH
1020 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1021 local _ret
1022 [[ -d $_moddir ]] || return 1
1023 if [[ ! -f $_moddir/module-setup.sh ]]; then
1024 [[ -x $_moddir/installkernel ]] && . "$_moddir/installkernel"
95d2dabc
HH
1025 return $?
1026 else
1027 unset check depends install installkernel
29b10e65 1028 . $_moddir/module-setup.sh
95d2dabc
HH
1029 is_func installkernel || return 0
1030 installkernel
29b10e65 1031 _ret=$?
95d2dabc 1032 unset check depends install installkernel
29b10e65 1033 return $_ret
95d2dabc
HH
1034 fi
1035}
1036
7e2bca48
HH
1037# check_mount <dracut module>
1038# check_mount checks, if a dracut module is needed for the given
1039# device and filesystem types in "${host_fs_types[@]}"
1b7fd0fa
AW
1040check_mount() {
1041 local _mod=$1
1042 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1043 local _ret
1044 local _moddep
1045 # If we are already scheduled to be loaded, no need to check again.
1046 strstr " $mods_to_load " " $_mod " && return 0
1047 strstr " $mods_checked_as_dep " " $_mod " && return 1
1048
1049 # This should never happen, but...
1050 [[ -d $_moddir ]] || return 1
1051
1052 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
1053
0f283709
HH
1054 if strstr " $omit_dracutmodules " " $_mod "; then
1055 dinfo "Dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
1056 return 1
1057 fi
1b7fd0fa
AW
1058
1059 if [ "${#host_fs_types[*]}" -gt 0 ]; then
1060 module_check_mount $_mod || return 1
1061 else
1062 # skip this module
1063 return 1
1064 fi
1065
1066 for _moddep in $(module_depends $_mod); do
1067 # handle deps as if they were manually added
1068 strstr " $add_dracutmodules " " $_moddep " || \
1069 add_dracutmodules+=" $_moddep "
1070 strstr " $force_add_dracutmodules " " $_moddep " || \
1071 force_add_dracutmodules+=" $_moddep "
1072 # if a module we depend on fail, fail also
0f283709
HH
1073 if ! check_module $_moddep; then
1074 derror "Dracut module '$_mod' depends on '$_moddep', which can't be installed"
1075 return 1
1076 fi
1b7fd0fa
AW
1077 done
1078
1079 strstr " $mods_to_load " " $_mod " || \
1080 mods_to_load+=" $_mod "
1081
1082 return 0
1083}
1084
7e2bca48
HH
1085# check_module <dracut module> [<use_as_dep>]
1086# check if a dracut module is to be used in the initramfs process
1087# if <use_as_dep> is set, then the process also keeps track
1088# that the modules were checked for the dependency tracking process
95d2dabc 1089check_module() {
29b10e65
HH
1090 local _mod=$1
1091 local _moddir=$(echo ${dracutbasedir}/modules.d/??${1})
1092 local _ret
1093 local _moddep
95d2dabc 1094 # If we are already scheduled to be loaded, no need to check again.
29b10e65
HH
1095 strstr " $mods_to_load " " $_mod " && return 0
1096 strstr " $mods_checked_as_dep " " $_mod " && return 1
95d2dabc
HH
1097
1098 # This should never happen, but...
29b10e65 1099 [[ -d $_moddir ]] || return 1
95d2dabc 1100
29b10e65 1101 [[ $2 ]] || mods_checked_as_dep+=" $_mod "
95d2dabc 1102
0f283709
HH
1103 if strstr " $omit_dracutmodules " " $_mod "; then
1104 dinfo "Dracut module '$_mod' will not be installed, because it's in the list to be omitted!"
1105 return 1
1106 fi
95d2dabc 1107
31f1c02d
AW
1108 if strstr " $dracutmodules $add_dracutmodules $force_add_dracutmodules" " $_mod "; then
1109 if strstr " $force_add_dracutmodules" " $_mod"; then
1110 module_check $_mod 1; ret=$?
1111 else
1112 module_check $_mod 0; ret=$?
1113 fi
95d2dabc
HH
1114 # explicit module, so also accept ret=255
1115 [[ $ret = 0 || $ret = 255 ]] || return 1
1116 else
1117 # module not in our list
1118 if [[ $dracutmodules = all ]]; then
1119 # check, if we can and should install this module
29b10e65 1120 module_check $_mod || return 1
cc02093d 1121 else
95d2dabc
HH
1122 # skip this module
1123 return 1
970e646b 1124 fi
95d2dabc 1125 fi
bb764545 1126
29b10e65 1127 for _moddep in $(module_depends $_mod); do
95d2dabc 1128 # handle deps as if they were manually added
29b10e65
HH
1129 strstr " $add_dracutmodules " " $_moddep " || \
1130 add_dracutmodules+=" $_moddep "
31f1c02d
AW
1131 strstr " $force_add_dracutmodules " " $_moddep " || \
1132 force_add_dracutmodules+=" $_moddep "
95d2dabc 1133 # if a module we depend on fail, fail also
0f283709
HH
1134 if ! check_module $_moddep; then
1135 derror "Dracut module '$_mod' depends on '$_moddep', which can't be installed"
1136 return 1
1137 fi
0c2e3d12 1138 done
70503db4 1139
29b10e65
HH
1140 strstr " $mods_to_load " " $_mod " || \
1141 mods_to_load+=" $_mod "
95d2dabc
HH
1142
1143 return 0
1144}
1145
7e2bca48
HH
1146# for_each_module_dir <func>
1147# execute "<func> <dracut module> 1"
1b7fd0fa 1148for_each_module_dir() {
29b10e65
HH
1149 local _modcheck
1150 local _mod
1151 local _moddir
1b7fd0fa
AW
1152 local _func
1153 _func=$1
29b10e65
HH
1154 for _moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1155 _mod=${_moddir##*/}; _mod=${_mod#[0-9][0-9]}
1b7fd0fa 1156 $_func $_mod 1
70503db4 1157 done
724b87a6
HH
1158
1159 # Report any missing dracut modules, the user has specified
31f1c02d 1160 _modcheck="$add_dracutmodules $force_add_dracutmodules"
29b10e65
HH
1161 [[ $dracutmodules != all ]] && _modcheck="$m $dracutmodules"
1162 for _mod in $_modcheck; do
1163 strstr "$mods_to_load" "$_mod" && continue
1164 strstr "$omit_dracutmodules" "$_mod" && continue
f2271428 1165 derror "Dracut module '$_mod' cannot be found or installed."
724b87a6 1166 done
0c2e3d12
VL
1167}
1168
ddfd1d10
VL
1169# Install a single kernel module along with any firmware it may require.
1170# $1 = full path to kernel module to install
1171install_kmod_with_fw() {
0b440844 1172 # no need to go further if the module is already installed
379c34d2 1173
0b706743
1174 [[ -e "${initdir}/lib/modules/$kernel/${1##*/lib/modules/$kernel/}" ]] \
1175 && return 0
fcbcb252 1176
f4031e8a
HH
1177 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -e "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}" ]]; then
1178 read ret < "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
d8aeb3a7
HH
1179 return $ret
1180 fi
379c34d2 1181
fcbcb252
HH
1182 if [[ $omit_drivers ]]; then
1183 local _kmod=${1##*/}
1184 _kmod=${_kmod%.ko}
1185 _kmod=${_kmod/-/_}
34248c92
HH
1186 if [[ "$_kmod" =~ $omit_drivers ]]; then
1187 dinfo "Omitting driver $_kmod"
7abd4264 1188 return 0
34248c92
HH
1189 fi
1190 if [[ "${1##*/lib/modules/$kernel/}" =~ $omit_drivers ]]; then
fcbcb252 1191 dinfo "Omitting driver $_kmod"
7abd4264 1192 return 0
fcbcb252
HH
1193 fi
1194 fi
1195
d8aeb3a7
HH
1196 inst_simple "$1" "/lib/modules/$kernel/${1##*/lib/modules/$kernel/}"
1197 ret=$?
f4031e8a
HH
1198 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1199 [[ -d "$DRACUT_KERNEL_LAZY_HASHDIR" ]] && \
1200 echo $ret > "$DRACUT_KERNEL_LAZY_HASHDIR/${1##*/}"
d8aeb3a7 1201 (($ret != 0)) && return $ret
e6024e00
JR
1202
1203 local _modname=${1##*/} _fwdir _found _fw
1204 _modname=${_modname%.ko*}
29b10e65
HH
1205 for _fw in $(modinfo -k $kernel -F firmware $1 2>/dev/null); do
1206 _found=''
1207 for _fwdir in $fw_dir; do
1208 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1209 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1210 _found=yes
cc02093d
HH
1211 fi
1212 done
29b10e65 1213 if [[ $_found != yes ]]; then
fe1484f3 1214 if ! [[ -d $(echo /sys/module/${_modname//-/_}|{ read a b; echo $a; }) ]]; then
29b10e65
HH
1215 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
1216 "\"${_modname}.ko\""
e2cdb570 1217 else
29b10e65
HH
1218 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
1219 "\"${_modname}.ko\""
e2cdb570 1220 fi
cc02093d 1221 fi
ddfd1d10 1222 done
0b440844 1223 return 0
ddfd1d10
VL
1224}
1225
1226# Do something with all the dependencies of a kernel module.
1227# Note that kernel modules depend on themselves using the technique we use
0b706743
1228# $1 = function to call for each dependency we find
1229# It will be passed the full path to the found kernel module
ddfd1d10
VL
1230# $2 = module to get dependencies for
1231# rest of args = arguments to modprobe
c32bda6b 1232# _fderr specifies FD passed from surrounding scope
ddfd1d10 1233for_each_kmod_dep() {
7abd4264 1234 local _func=$1 _kmod=$2 _cmd _modpath _options
ddfd1d10 1235 shift 2
c32bda6b 1236 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
29b10e65
HH
1237 while read _cmd _modpath _options; do
1238 [[ $_cmd = insmod ]] || continue
1239 $_func ${_modpath} || exit $?
0b706743 1240 done
0b706743 1241 )
ddfd1d10
VL
1242}
1243
70c6b773 1244dracut_kernel_post() {
d8aeb3a7
HH
1245 local _moddirname=${srcmods%%/lib/modules/*}
1246
f4031e8a 1247 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" ]]; then
ec61f0a3
HH
1248 xargs -r modprobe -a ${_moddirname+-d ${_moddirname}/} \
1249 --ignore-install --show-depends --set-version $kernel \
f4031e8a 1250 < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist" 2>/dev/null \
89d44e72
HH
1251 | sort -u \
1252 | while read _cmd _modpath _options; do
1253 [[ $_cmd = insmod ]] || continue
1254 echo "$_modpath"
f4031e8a 1255 done > "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
d8aeb3a7 1256
89d44e72 1257 (
73575f11
HH
1258 if [[ $DRACUT_INSTALL ]] && [[ -z $_moddirname ]]; then
1259 xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
89d44e72
HH
1260 else
1261 while read _modpath; do
1262 local _destpath=$_modpath
1263 [[ $_moddirname ]] && _destpath=${_destpath##$_moddirname/}
1264 _destpath=${_destpath##*/lib/modules/$kernel/}
1265 inst_simple "$_modpath" "/lib/modules/$kernel/${_destpath}" || exit $?
f4031e8a 1266 done < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"
89d44e72
HH
1267 fi
1268 ) &
d8aeb3a7 1269
73575f11
HH
1270 if [[ $DRACUT_INSTALL ]]; then
1271 xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep" \
89d44e72
HH
1272 | while read line; do
1273 for _fwdir in $fw_dir; do
1274 echo $_fwdir/$line;
1275 done;
73575f11 1276 done | xargs -r $DRACUT_INSTALL ${initdir+-D "$initdir"} -a -o
89d44e72 1277 else
73575f11 1278 for _fw in $(xargs -r modinfo -k $kernel -F firmware < "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist.dep"); do
89d44e72
HH
1279 for _fwdir in $fw_dir; do
1280 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1281 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1282 break
1283 fi
1284 done
1285 done
1286 fi
d8aeb3a7 1287
89d44e72
HH
1288 wait
1289 fi
70c6b773
HH
1290
1291 for _f in modules.builtin.bin modules.builtin; do
1292 [[ $srcmods/$_f ]] && break
1293 done || {
1294 dfatal "No modules.builtin.bin and modules.builtin found!"
1295 return 1
1296 }
1297
1298 for _f in modules.builtin.bin modules.builtin modules.order; do
1299 [[ $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f"
1300 done
1301
1302 # generate module dependencies for the initrd
1303 if [[ -d $initdir/lib/modules/$kernel ]] && \
1304 ! depmod -a -b "$initdir" $kernel; then
1305 dfatal "\"depmod -a $kernel\" failed."
1306 exit 1
1307 fi
1308
f4031e8a 1309 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && rm -fr "$DRACUT_KERNEL_LAZY_HASHDIR"
d8aeb3a7 1310}
fe1484f3 1311
881eda69 1312find_kernel_modules_by_path () (
74405477 1313 local _OLDIFS
881eda69 1314 if ! [[ $hostonly ]]; then
74405477
HH
1315 _OLDIFS=$IFS
1316 IFS=:
fe1484f3 1317 while read a rest; do
d8aeb3a7 1318 [[ $a = kernel*/$1/* ]] || continue
74405477 1319 echo $srcmods/$a
fe1484f3 1320 done < $srcmods/modules.dep
74405477 1321 IFS=$_OLDIFS
881eda69 1322 else
fe1484f3 1323 ( cd /sys/module; echo *; ) \
194b80f9
HH
1324 | xargs -r modinfo -F filename -k $kernel 2>/dev/null \
1325 | while read a; do
52f74c80
HH
1326 [[ $a = */kernel*/$1/* ]] || continue
1327 echo $a
194b80f9 1328 done
881eda69 1329 fi
74405477 1330 return 0
881eda69 1331)
4073c815 1332
881eda69
JR
1333find_kernel_modules () {
1334 find_kernel_modules_by_path drivers
1335}
1336
338b43cd
HH
1337# instmods [-c [-s]] <kernel module> [<kernel module> ... ]
1338# instmods [-c [-s]] <kernel subsystem>
ddfd1d10 1339# install kernel modules along with all their dependencies.
7e2bca48 1340# <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
6fa0c3d6 1341instmods() {
0c1a8ebc 1342 [[ $no_kernel = yes ]] && return
c32bda6b
MS
1343 # called [sub]functions inherit _fderr
1344 local _fderr=9
a6d3be9d 1345 local _check=no
338b43cd 1346 local _silent=no
a6d3be9d
AW
1347 if [[ $1 = '-c' ]]; then
1348 _check=yes
1349 shift
1350 fi
881eda69 1351
338b43cd
HH
1352 if [[ $1 = '-s' ]]; then
1353 _silent=yes
1354 shift
1355 fi
1356
881eda69 1357 function inst1mod() {
a6d3be9d 1358 local _ret=0 _mod="$1"
29b10e65 1359 case $_mod in
0b706743 1360 =*)
d8aeb3a7
HH
1361 ( [[ "$_mpargs" ]] && echo $_mpargs
1362 find_kernel_modules_by_path "${_mod#=}" ) \
1363 | instmods
1364 ((_ret+=$?))
cc02093d 1365 ;;
0024702f 1366 --*) _mpargs+=" $_mod" ;;
7abd4264 1367 i2o_scsi) return 0;; # Do not load this diagnostic-only module
34248c92 1368 *)
379c34d2 1369 _mod=${_mod##*/}
6568d86a 1370 # if we are already installed, skip this module and go on
cc02093d 1371 # to the next one.
f4031e8a
HH
1372 if [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1373 [[ -f "$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko" ]]; then
1374 read _ret <"$DRACUT_KERNEL_LAZY_HASHDIR/${_mod%.ko}.ko"
d8aeb3a7
HH
1375 return $_ret
1376 fi
881eda69 1377
34248c92
HH
1378 if [[ $omit_drivers ]] && [[ "$1" =~ $omit_drivers ]]; then
1379 dinfo "Omitting driver ${_mod##$srcmods}"
7abd4264 1380 return 0
34248c92 1381 fi
cc02093d
HH
1382 # If we are building a host-specific initramfs and this
1383 # module is not already loaded, move on to the next one.
fe1484f3
HH
1384 [[ $hostonly ]] \
1385 && ! [[ -d $(echo /sys/module/${_mod//-/_}|{ read a b; echo $a; }) ]] \
1386 && ! [[ "$add_drivers" =~ " ${_mod} " ]] \
7abd4264 1387 && return 0
04b56f3a 1388
f4031e8a 1389 if [[ "$_check" = "yes" ]] || ! [[ $DRACUT_KERNEL_LAZY_HASHDIR ]]; then
d8aeb3a7
HH
1390 # We use '-d' option in modprobe only if modules prefix path
1391 # differs from default '/'. This allows us to use Dracut with
1392 # old version of modprobe which doesn't have '-d' option.
1393 local _moddirname=${srcmods%%/lib/modules/*}
1394 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1395
1396 # ok, load the module, all its dependencies, and any firmware
1397 # it may require
1398 for_each_kmod_dep install_kmod_with_fw $_mod \
1399 --set-version $kernel ${_moddirname} $_mpargs
1400 ((_ret+=$?))
1401 else
f4031e8a
HH
1402 [[ $DRACUT_KERNEL_LAZY_HASHDIR ]] && \
1403 echo $_mod >> "$DRACUT_KERNEL_LAZY_HASHDIR/lazylist"
d8aeb3a7 1404 fi
cc02093d 1405 ;;
0b706743 1406 esac
a6d3be9d 1407 return $_ret
881eda69
JR
1408 }
1409
f9708da2 1410 function instmods_1() {
a6d3be9d 1411 local _mod _mpargs
f9708da2
JR
1412 if (($# == 0)); then # filenames from stdin
1413 while read _mod; do
a6d3be9d 1414 inst1mod "${_mod%.ko*}" || {
338b43cd
HH
1415 if [[ "$_check" == "yes" ]]; then
1416 [[ "$_silent" == "no" ]] && dfatal "Failed to install $_mod"
a6d3be9d
AW
1417 return 1
1418 fi
1419 }
f9708da2
JR
1420 done
1421 fi
1422 while (($# > 0)); do # filenames as arguments
a6d3be9d 1423 inst1mod ${1%.ko*} || {
338b43cd
HH
1424 if [[ "$_check" == "yes" ]]; then
1425 [[ "$_silent" == "no" ]] && dfatal "Failed to install $1"
a6d3be9d
AW
1426 return 1
1427 fi
1428 }
f9708da2 1429 shift
881eda69 1430 done
a6d3be9d 1431 return 0
f9708da2
JR
1432 }
1433
a6d3be9d 1434 local _ret _filter_not_found='FATAL: Module .* not found.'
c32bda6b
MS
1435 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1436 # redirections, but that would make dracut require bash4 at least.
1437 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
86191581 1438 | while read line; do [[ "$line" =~ $_filter_not_found ]] || echo $line;done | derror
a6d3be9d 1439 _ret=$?
a6d3be9d 1440 return $_ret
0b706743 1441}