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