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