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