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