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