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