Debian Codegraph @

hook-functions

raw
1 # -*- shell-script -*-
2 # shellcheck shell=sh
3
4 catenate_cpiogz() {
5 # Sanity check
6 if [ ! -e "${1}" ]; then
7 echo "W: catenate_cpiogz: arg1='${1}' does not exist." >&2
8 return
9 fi
10
11 cat "${1}" >>"${__TMPCPIOGZ}"
12 }
13
14 prepend_earlyinitramfs() {
15 # Sanity check
16 if [ ! -e "${1}" ]; then
17 echo "W: prepend_earlyinitramfs: arg1='${1}' does not exist." >&2
18 return
19 fi
20
21 cat "${1}" >>"${__TMPEARLYCPIO}"
22 }
23
24 # force_load module [args...]
25 force_load()
26 {
27 manual_add_modules "$1"
28 echo "${@}" >>"${DESTDIR}/conf/modules"
29 }
30
31 # Takes a file containing a list of modules to be added as an
32 # argument, figures out dependencies, and adds them.
33 #
34 # Input file syntax:
35 #
36 # # comment
37 # modprobe_module_name [args ...]
38 # [...]
39 #
40 add_modules_from_file()
41 {
42 # Sanity check
43 if [ ! -e "${1}" ]; then
44 echo "W: add_modules_from_file: arg1='${1}' does not exist." >&2
45 return
46 fi
47
48 grep '^[^#]' "${1}" | while read -r module args; do
49 [ -n "$module" ] || continue
50 force_load "${module}" "${args}"
51 done
52 }
53
54 # Locate a firmware file with the given name and copy it into DESTDIR, unless
55 # DESTDIR already contains such a file.
56 # Returns an error if no such firmware file can be located and DESTDIR doesn't
57 # already contain any matching file. (It is up to the caller to figure out
58 # whether a warning should be printed in that case.)
59 add_firmware()
60 {
61 local firmware found_fwloc fwloc fw_path_para path suffix
62
63 firmware="${1}"
64
65 if [ -e /sys/module/firmware_class/parameters/path ]; then
66 read -r fw_path_para < /sys/module/firmware_class/parameters/path
67 fi
68
69 for suffix in "" ".zst" ".xz"; do
70 for path in ${fw_path_para:+"$fw_path_para"} \
71 "/lib/firmware/updates/${version?}" \
72 "/lib/firmware/updates" \
73 "/lib/firmware/${version}" \
74 "/lib/firmware"; do
75 fwloc="${path}/${firmware}${suffix}"
76 if [ -e "${DESTDIR}${fwloc}" ]; then
77 # DESTDIR already contains a matching firmware file.
78 return 0
79 fi
80 if [ -z "${found_fwloc}" ] && [ -e "${fwloc}" ]; then
81 found_fwloc="$fwloc"
82 fi
83 done
84 done
85
86 if [ -z "${found_fwloc}" ]; then
87 # We can't figure out where to get that firmware from.
88 return 1
89 fi
90
91 copy_file firmware "${found_fwloc}"
92 }
93
94 # Add dependent modules + eventual firmware
95 # This function was changed to only collect the wanted kernel modules.
96 # Call apply_add_modules to copy the kernel modules.
97 manual_add_modules()
98 {
99 for module in "$@"; do
100 echo "$module" >> "$__MODULES_TO_ADD"
101 done
102 }
103
104 # Copy the kernel modules that were marked in manual_add_modules
105 apply_add_modules()
106 {
107 # shellcheck disable=SC2046
108 _call_dracut_install $(sort -u "$__MODULES_TO_ADD") || return $?
109 true > "$__MODULES_TO_ADD"
110 }
111
112 _call_dracut_install()
113 {
114 local dracut_verbose fw_path_para
115
116 if [ $# -eq 0 ]; then
117 return
118 fi
119
120 if [ "${verbose?}" = "y" ]; then
121 dracut_verbose=-v
122 fi
123
124 if [ -e /sys/module/firmware_class/parameters/path ]; then
125 read -r fw_path_para < /sys/module/firmware_class/parameters/path
126 fi
127 /usr/lib/dracut/dracut-install -D "$DESTDIR" --kerneldir "/lib/modules/${version?}" \
128 --firmwaredirs "${fw_path_para:+${fw_path_para}:}/lib/firmware/updates/${version?}:/lib/firmware/updates:/lib/firmware/${version?}:/lib/firmware" \
129 ${dracut_verbose-} -o -m "$@"
130 }
131
132 # manual_add_modules() takes care of adding firmware for things that were built
133 # as modules; but drivers can also be built into the kernel itself. To cover
134 # that case, we have to look at modules.builtin.modinfo instead.
135 # This file is generated by the kernel's build process since commit 898490c010b5
136 # ("moduleparam: Save information about built-in modules in separate file"),
137 # which was added in Linux 5.2.
138 add_builtin_firmware()
139 {
140 local builtin_modinfo_path builtin_modname firmware
141
142 builtin_modinfo_path="/lib/modules/${version?}/modules.builtin.modinfo"
143 if [ ! -e "$builtin_modinfo_path" ]; then
144 if linux-version compare "${version}" ge 5.2; then
145 echo "W: Can't find modules.builtin.modinfo (for locating built-in drivers' firmware, supported in Linux >=5.2)" >&2
146 fi
147 return
148 fi
149
150 tr '\0' '\n' < "$builtin_modinfo_path" | grep -E '^[^=]*\.firmware=' | sed -n 's/\.firmware=/\t/p' | while read -r builtin_modname firmware; do
151 if ! add_firmware "$firmware"; then
152 echo "W: Possible missing firmware /lib/firmware/${firmware} for built-in driver ${builtin_modname}" >&2
153 fi
154 done
155 }
156
157 # Copy a file to the initramfs:
158 # $1 = file type, for debug logging
159 # $2 = source file name
160 # $3 (optional) = target file or directory name in the initramfs
161 #
162 # * If the target is not specified, it defaults to the source file
163 # name.
164 # * If the target is specified and exists as a directory under
165 # $DESTDIR or ends in a slash, the basename of the source is
166 # appended to it.
167 #
168 # The target file's containing directories are created if necessary.
169 #
170 # If the source file name includes a symlink, other than usr-merge
171 # symlinks, and the canonical name of the source is not the same as
172 # the target, the source file is copied to its canonical name in the
173 # initramfs and the target is created as a symlink.
174 #
175 # Returns:
176 # * If the file was copied successfully, 0
177 # * If the target file already existed, 1
178 # * On error, >1
179 copy_file() {
180 local type src target link_target
181
182 type="${1}"
183 src="${2}"
184 target="${3:-$2}"
185
186 if [ ! -f "${src}" ]; then
187 echo "mkinitramfs: copy_file: $type '$src' not found" >&2
188 return 2
189 fi
190
191 if [ -d "${DESTDIR}/${target}" ] || [ "${target%/}" != "$target" ]; then
192 target="${target}/${src##*/}"
193 fi
194
195 # Canonicalise target to be absolute, so the comparisons below
196 # will work
197 target="$(realpath -ms "/${target}")" || return 2
198
199 # Canonicalise usr-merged directories
200 case "${src}" in
201 /bin/* | /lib* | /sbin/*)
202 [ "$(readlink -f /bin)" = /usr/bin ] && src="/usr${src}"
203 ;;
204 esac
205 case "${target}" in
206 /bin/* | /lib* | /sbin/*) target="/usr${target}" ;;
207 esac
208
209 # check if already copied
210 [ -e "${DESTDIR}/${target}" ] && return 1
211
212 mkdir -p "${DESTDIR}/${target%/*}"
213
214 # Check whether source or one of its ancestors is a symlink.
215 # If so, copy the symlink target and make the target a symlink
216 # too. We don't need to replicate a chain of links completely;
217 # just link directly to the ultimate target.
218 link_target="$(readlink -f "${src}")" || return $(($? + 1))
219 if [ "${link_target}" != "$(realpath -s "$src")" ]; then
220 # Update source for the copy
221 src="${link_target}"
222
223 # Canonicalise usr-merged target directories
224 case "${link_target}" in
225 /bin/* | /lib* | /sbin/*) link_target="/usr${link_target}" ;;
226 esac
227
228 if [ "${link_target}" != "${target}" ]; then
229 [ "${verbose?}" = "y" ] && echo "Adding ${type}-link ${target}"
230
231 # Create a relative link so it always points
232 # to the right place
233 ln -rs "${DESTDIR}/${link_target}" "${DESTDIR}/${target}"
234 fi
235
236 # Copy the link target if it doesn't already exist
237 target="${link_target}"
238 [ -e "${DESTDIR}/${target}" ] && return 0
239 mkdir -p "${DESTDIR}/${target%/*}"
240 fi
241
242 [ "${verbose}" = "y" ] && echo "Adding ${type} ${src}"
243
244 cp -pP "${src}" "${DESTDIR}/${target}" || return $(($? + 1))
245 }
246
247 # Copy an executable or shared library to the initramfs:
248 # $1 = source file name
249 # $2 (optional) = target file or directory name in the initramfs
250 #
251 # The source and all its shared library dependencies are copied
252 # using copy_file.
253 #
254 # Returns:
255 # * If the files were copied successfully or already exited, 0
256 # * On error, >0
257 copy_exec() {
258 local src target x nonoptlib ret
259
260 src="${1}"
261 target="${2:-$1}"
262
263 copy_file binary "${src}" "${target}" || return $(($? - 1))
264
265 # Copy the dependant libraries
266 for x in $(env --unset=LD_PRELOAD ldd "${src}" 2>/dev/null | sed -e '
267 /^[^\t]/d;
268 /\//!d;
269 /linux-gate/d;
270 /=>/ {s/.*=>[[:blank:]]*\([^[:blank:]]*\).*/\1/};
271 s/[[:blank:]]*\([^[:blank:]]*\) (.*)/\1/' 2>/dev/null); do
272
273 # Try to use non-optimised libraries where possible.
274 # We assume that all HWCAP libraries will be in tls,
275 # sse2, vfp or neon.
276 nonoptlib=$(echo "${x}" | sed -e 's#/lib/\([^/]*/\)\?\(tls\|i686\|sse2\|neon\|vfp\).*/\(lib.*\)#/lib/\1\3#')
277 nonoptlib=$(echo "${nonoptlib}" | sed -e 's#-linux-gnu/\(tls\|i686\|sse2\|neon\|vfp\).*/\(lib.*\)#-linux-gnu/\2#')
278
279 if [ -e "${nonoptlib}" ]; then
280 x="${nonoptlib}"
281 fi
282
283 # Handle common dlopen() dependency (Debian bug #950254)
284 case "${x}" in
285 */libpthread.so.*)
286 copy_libgcc || return
287 ;;
288 esac
289
290 copy_file binary "${x}" || {
291 ret=$?
292 [ ${ret} = 1 ] || return $((ret - 1))
293 }
294 done
295 }
296
297 # Copy libgcc_s.so for the running architecture to the initramfs.
298 # This function takes no parameter.
299 #
300 # Returns:
301 # * If the library was copied successfully or already existed, 0
302 # * On error, >0
303 copy_libgcc() {
304 local libdir library
305
306 for libdir in $(ld.so --list-diagnostics | sed -n 's/^path.system_dirs.*="\(.*\)"$/\1/p'); do
307 for library in "${libdir}"/libgcc_s.so.[1-9]; do
308 [ -e "$library" ] || continue
309 copy_exec "${library}" || return
310 done
311 done
312 }
313
314 # Copy entire subtrees to the initramfs
315 copy_modules_dir()
316 {
317 local kmod first exclude
318 local modules=
319 local dir="$1"
320 shift
321
322 if ! [ -d "${MODULESDIR}/${dir}" ]; then
323 return;
324 fi
325 if [ "${verbose}" = "y" ]; then
326 echo "Copying module directory ${dir}"
327 if [ $# -ge 1 ]; then
328 echo "(excluding $*)"
329 fi
330 fi
331
332 # Build up an expression for find
333 first=true
334 for exclude in "$@"; do
335 # Change .ko suffix in exclusion to .ko*
336 if [ "${exclude%.ko}" != "${exclude}" ]; then
337 exclude="${exclude}*"
338 fi
339 $first && set --
340 set -- "$@" -name "${exclude}" -prune -o
341 first=false
342 done
343
344 # shellcheck disable=SC2044
345 for kmod in $(find "${MODULESDIR}/${dir}" "$@" -name '*.ko*' -printf '%f\n'); do
346 modules="$modules ${kmod%%.*}"
347 done
348 # shellcheck disable=SC2086
349 manual_add_modules $modules
350 }
351
352 # walk /sys for relevant modules
353 sys_walk_mod_add()
354 {
355 local driver_path module device_path modalias devlink
356
357 # This is a crude way of storing two stacks in "$@" by using
358 # "--" as a separator between them, to implement iterative
359 # graph traversal over parent/supplier dependencies. The first
360 # part is device paths to traverse, the other is visited ones.
361 set -- "$1" "--"
362 while [ "$1" != "--" ]; do
363 device_path="$(readlink -f "$1")"
364 shift
365
366 # Don't walk into same path twice, avoid infinite recursion
367 case "$*" in
368 "${device_path}") continue ;;
369 "${device_path} "*) continue ;;
370 *" ${device_path} "*) continue ;;
371 *" ${device_path}") continue ;;
372 esac
373
374 if [ "$device_path" = "/sys" ]; then
375 continue
376 fi
377
378 # Keep current path to add module/modalias for
379 set -- "$@" "$device_path"
380
381 # Walk into suppliers in next iteration
382 for devlink in "${device_path}/supplier:"*; do
383 if [ -d "$devlink" ]; then
384 set -- "${devlink}/supplier/" "$@"
385 fi
386 done
387
388 # Walk into parent in next iteration
389 set -- "$(dirname "$device_path")" "$@"
390 done
391 shift
392
393 for device_path in "$@"; do
394 # device modalias
395 if [ -e "${device_path}/modalias" ]; then
396 modalias=$(cat "${device_path}/modalias")
397 if [ -n "${modalias}" ]; then
398 manual_add_modules "${modalias}"
399 fi
400 fi
401
402 # current driver module
403 driver_path="$(readlink -f "${device_path}/driver/module")"
404 if [ -e "$driver_path" ]; then
405 module="$(basename "$(readlink -f "$driver_path")")"
406 if [ -n "${module}" ]; then
407 manual_add_modules "${module}"
408 fi
409 fi
410 done
411 }
412
413 block_dev_sys_walk_mod_add()
414 {
415 local dev_sys_path disk_sys_path component
416
417 # Resolve symlink so sys_walk_mod_add can walk up the hierarchy
418 dev_sys_path="$(readlink -f "$1")"
419
420 # Find whole disk from partition
421 if grep -q "^DEVTYPE=partition$" "$dev_sys_path/uevent"; then
422 disk_sys_path="$dev_sys_path/.."
423 else
424 disk_sys_path="$dev_sys_path"
425 fi
426
427 # Iterate over component of a layered device
428 find "$disk_sys_path/slaves" -mindepth 1 -maxdepth 1 | while read -r component; do
429 block_dev_sys_walk_mod_add "$component"
430 done
431
432 sys_walk_mod_add "${dev_sys_path}"
433 }
434
435 block_dev_mod_add()
436 {
437 local dev_node dev_num dev_sys_path
438 dev_node="$1"
439
440 # Look up device number and convert to decimal as it appears in sysfs
441 dev_num="$(stat -L -c %t:%T "$dev_node")"
442 dev_num="$((0x${dev_num%:*})):$((0x${dev_num#*:}))"
443
444 # Look up device in sysfs
445 dev_sys_path="/sys/dev/block/$dev_num"
446 if [ ! -d "$dev_sys_path" ]; then
447 echo "mkinitramfs: for device ${dev_node} missing ${dev_sys_path}" >&2
448 echo "mkinitramfs: workaround is MODULES=most" >&2
449 echo "mkinitramfs: Error please report the bug" >&2
450 exit 1
451 fi
452
453 block_dev_sys_walk_mod_add "$dev_sys_path"
454 }
455
456 # Copy all loaded or built-in modules matching the given pattern.
457 # Pattern mustn't include directory or '.ko' suffix but should use
458 # '[-_]' to allow for differences in naming between /sys/module and
459 # modules.builtin.
460 add_loaded_modules()
461 {
462 local pattern="$1"
463 local module device builtin
464 builtin="/lib/modules/$(uname -r)/modules.builtin"
465
466 for module in /sys/module/$pattern; do
467 if [ -d "$module" ]; then
468 manual_add_modules "$(basename "$module")"
469 fi
470 done
471 # shellcheck disable=SC2231
472 for device in /sys/module/$pattern/drivers/*/*; do
473 if [ -d "$device" ] && [ "$(basename "$device")" != "module" ]; then
474 sys_walk_mod_add "$device"
475 fi
476 done
477 if [ -f "$builtin" ]; then
478 while read -r module; do
479 case "$module" in
480 */$pattern.ko)
481 manual_add_modules "$(basename "$module" .ko)"
482 ;;
483 esac
484 done < "$builtin"
485 fi
486 }
487
488 # find and only copy modules relevant to a mountpoint
489 dep_add_modules_mount()
490 {
491 local dir dev_node FSTYPE
492
493 dir="$1"
494
495 # require mounted sysfs
496 if [ ! -d /sys/devices/ ]; then
497 echo "mkinitramfs: MODULES dep requires mounted sysfs on /sys" >&2
498 exit 1
499 fi
500
501 # find out block device + fstype
502 # shellcheck disable=SC2034
503 eval "$(while read -r dev mp fs opts rest ; do \
504 [ "$mp" = "$dir" ] && [ "$fs" != "rootfs" ] \
505 && printf "dev_node=%s\\nFSTYPE=%s" "$dev" "$fs"\
506 && break; done < /proc/mounts)"
507
508 # Only the root mountpoint has to exist; do nothing if any other
509 # directory is not a mountpoint.
510 if [ "$dir" != / ] && [ -z "$dev_node" ]; then
511 return
512 fi
513
514 # handle ubifs and return since ubifs is mounted on char devices
515 # but most of the commands below only work with block devices.
516 if [ "${FSTYPE}" = "ubifs" ]; then
517 manual_add_modules "${FSTYPE}"
518 return
519 fi
520
521 # Return upon finding a zfs because when used as rootfs it is
522 # not presented as a typical mount point like other fs, so that
523 # can not be handled with commands below. Since zfs has its own
524 # addons in the zfs-initramfs package, do nothing here.
525 if [ "${FSTYPE}" = "zfs" ]; then
526 return
527 fi
528
529 if [ "$dir" = / ] && [ "${dev_node}" = "/dev/root" ] ; then
530 if [ -b "${dev_node}" ]; then
531 # Match it to the canonical device name by UUID
532 dev_node="/dev/disk/by-uuid/"$(blkid -o value -s UUID "${dev_node}") 2>/dev/null
533 else
534 # Does not exist in our namespace, so look at the
535 # kernel command line
536 dev_node=
537 # shellcheck disable=SC2013
538 for arg in $(cat /proc/cmdline); do
539 case "$arg" in
540 root=*)
541 dev_node="${arg#root=}"
542 if [ "${dev_node#/dev/}" = "$dev_node" ]; then
543 dev_node="/dev/$dev_node"
544 fi
545 ;;
546 --)
547 break
548 ;;
549 *)
550 ;;
551 esac
552 done
553 fi
554 fi
555
556 # recheck device
557 if [ -z "$dev_node" ] || ! dev_node="$(readlink -f "${dev_node}")" \
558 || ! [ -b "$dev_node" ]; then
559 echo "mkinitramfs: failed to determine device for $dir" >&2
560 echo "mkinitramfs: workaround is MODULES=most, check:" >&2
561 echo "grep -r MODULES ${CONFDIR}" >&2
562 echo "" >&2
563 echo "Error please report bug on initramfs-tools" >&2
564 echo "Include the output of 'mount' and 'cat /proc/mounts'" >&2
565 exit 1
566 fi
567
568 # do not trust mount, check superblock
569 eval "$(/usr/lib/klibc/bin/fstype "${dev_node}")"
570
571 # check that fstype fs recognition
572 if [ "${FSTYPE}" = "unknown" ]; then
573 FSTYPE=$(blkid -o value -s TYPE "${dev_node}")
574 if [ -z "${FSTYPE}" ]; then
575 echo "mkinitramfs: unknown fstype on device ${dev_node}" >&2
576 echo "mkinitramfs: workaround is MODULES=most" >&2
577 echo "Error please report bug on initramfs-tools" >&2
578 exit 1
579 fi
580 fi
581
582 # Add filesystem
583 manual_add_modules "${FSTYPE}"
584
585 block_dev_mod_add "$dev_node"
586 }
587
588 class_add_modules()
589 {
590 local device
591
592 for device in "/sys/class/$1"/*; do
593 device="$(readlink -f "$device")" \
594 && sys_walk_mod_add "$device"
595 done
596 }
597
598 dep_add_modules()
599 {
600 local device dev_node
601 local modules=
602
603 dep_add_modules_mount /
604 dep_add_modules_mount /usr
605
606 if [ -n "${RESUME}" ]; then
607 dev_node="$(resolve_device "${RESUME}")"
608 if [ -n "${dev_node}" ]; then
609 block_dev_mod_add "${dev_node}"
610 fi
611 fi
612
613 # sys walk some important device classes
614 for class in extcon gpio phy pwm regulator rtc; do
615 class_add_modules "$class"
616 done
617
618 # clk, USB-PHY, pinctrl and reset devices are outside the device
619 # model (!) so match loaded modules by name
620 add_loaded_modules 'clk[-_]*'
621 add_loaded_modules 'phy[-_]*'
622 add_loaded_modules 'pinctrl[-_]*'
623 add_loaded_modules 'reset[-_]*'
624
625 # Sys walk keyboards. We identify keyboards as input devices
626 # that can generate at least key events 1-31; udev has the
627 # same heuristic. Note that the format of the bitmap
628 # properties depends on the word size of the process reading
629 # the uevent file!
630 for device in /sys/class/input/input*; do
631 if grep -qs "^KEY=.*fffffff[ef]$" "${device}/uevent"; then
632 sys_walk_mod_add "$(readlink -f "$device")"
633 fi
634 done
635
636 # May be necessary to use firmware framebuffers, if built as modules
637 modules="$modules simpledrm simplefb"
638
639 # Sys walk graphics for machines that don't have a generic framebuffer
640 # device and wouldn't have a working video console otherwise.
641 walk_graphics=yes
642 for device in /sys/bus/platform/drivers/efi-framebuffer/* \
643 /sys/bus/platform/drivers/platform-framebuffer/* \
644 /sys/bus/platform/drivers/simple-framebuffer/* \
645 /sys/bus/platform/drivers/vesa-framebuffer/*; do
646 if [ -d "$device" ] && [ "$(basename "$device")" != "module" ]; then
647 walk_graphics=no
648 break
649 fi
650 done
651
652 for device in /sys/bus/coreboot/drivers/framebuffer/*; do
653 if [ -d "$device" ] && [ "$(basename "$device")" != "module" ]; then
654 modules="$modules framebuffer_coreboot"
655 walk_graphics=no
656 fi
657 done
658
659 # It's possible that a generic framebuffer device works, but is taken
660 # over by a more capable driver and no longer available in /sys. We
661 # have no reliable consistent way to detect that, so apply heuristics.
662 case "$(uname -m)" in
663 i386|i686|x86_64) walk_graphics=no ;;
664 esac
665
666 if [ -d "/sys/firmware/efi/efivars" ]; then
667 walk_graphics=no
668 fi
669
670 for node in /sys/firmware/devicetree/base/chosen/framebuffer@*; do
671 if [ -d "$node" ] && grep -qz '^simple-framebuffer$' "$node/compatible"; then
672 if [ ! -f "$node/status" ] || grep -qz '^\(okay\|ok\)$' "$node/status"; then
673 walk_graphics=no
674 fi
675 fi
676 done
677
678 if [ "$walk_graphics" = "yes" ]; then
679 class_add_modules backlight
680 class_add_modules graphics
681 fi
682
683 # catch old-style IDE
684 if [ -e /sys/bus/ide/devices/ ]; then
685 modules="$modules ide-gd_mod ide-cd"
686 fi
687
688 if [ -e /sys/bus/scsi/devices/ ]; then
689 modules="$modules sd_mod"
690 fi
691
692 if [ -e /sys/bus/mmc/devices/ ]; then
693 modules="$modules mmc_block"
694 fi
695
696 if [ -e /sys/bus/virtio ] ; then
697 modules="$modules virtio_pci virtio_mmio"
698 fi
699
700 if [ -e /sys/bus/ps3_system_bus/ ]; then
701 modules="$modules ps3disk ps3rom ps3-gelic ps3_sys_manager"
702 fi
703
704 if [ -e /sys/bus/vio/ ]; then
705 modules="$modules sunvnet sunvdc"
706 fi
707
708 for device in /sys/bus/*/drivers/*panel*/* /sys/module/*panel*/drivers/*/*; do
709 if [ -d "$device" ] && [ "$(basename "$device")" != "module" ]; then
710 sys_walk_mod_add "$device"
711 fi
712 done
713
714 # shellcheck disable=SC2086
715 manual_add_modules $modules
716 }
717
718 # The modules "most" classes added per default to the initramfs
719 auto_add_modules()
720 {
721 local arg exclude exclude_dir
722 local block_modules=
723 local modules=
724
725 if [ "$#" -eq 0 ] ; then
726 set -- base net ide scsi block ata dasd firewire mmc usb_storage fb
727 fi
728
729 local blockfuncs="ahci_platform_get_resources|ata_scsi_ioctl|scsi_add_host|blk_cleanup_queue"
730 blockfuncs="${blockfuncs}|register_mtd_blktrans|scsi_esp_register|register_virtio_device"
731 blockfuncs="${blockfuncs}|usb_stor_disconnect|mmc_add_host|sdhci_add_host|scsi_add_host_with_dma"
732 blockfuncs="${blockfuncs}|blk_mq_alloc_disk|blk_mq_alloc_request|blk_mq_destroy_queue|blk_cleanup_disk"
733 blockfuncs="${blockfuncs}|dw_mc_probe|dw_mci_pltfm_register|nvme_init_ctrl"
734
735 for arg in "$@" ; do
736 case "$arg" in
737 base)
738 modules="$modules btrfs ext2 ext3 ext4 f2fs"
739 modules="$modules isofs jfs reiserfs squashfs udf xfs"
740 modules="$modules nfs nfsv2 nfsv3 nfsv4"
741 modules="$modules af_packet atkbd i8042 psmouse"
742 modules="$modules virtio_pci virtio_mmio"
743
744 # Include most USB host and dual-role drivers
745 copy_modules_dir kernel/drivers/usb/host \
746 hwa-hc.ko sl811_cs.ko sl811-hcd.ko \
747 u132-hcd.ko whci-hcd.ko
748 modules="$modules =drivers/usb/c67x00"
749 modules="$modules =drivers/usb/cdns3"
750 modules="$modules =drivers/usb/chipidea"
751 modules="$modules =drivers/usb/dwc2"
752 modules="$modules =drivers/usb/dwc3"
753 modules="$modules =drivers/usb/isp1760"
754 modules="$modules =drivers/usb/mtu3"
755 modules="$modules =drivers/usb/musb"
756 modules="$modules =drivers/usb/renesas_usbhs"
757 modules="$modules =drivers/usb/typec"
758 # Add onboard_usb_hub so it can be probed before
759 # mounts, otherwise it might reset power to a
760 # USB disk already mounted as root
761 modules="$modules onboard_usb_hub onboard_usb_dev"
762
763 # needed for USB on some Qualcomm devices
764 modules="$modules =net/qrtr"
765
766 # Include all keyboard drivers and all HID drivers
767 # unless we're sure they don't support keyboards.
768 # hid-*ff covers various game controllers with
769 # force feedback.
770 modules="$modules =drivers/input/keyboard"
771 copy_modules_dir kernel/drivers/hid \
772 'hid-*ff.ko' hid-a4tech.ko hid-cypress.ko \
773 hid-dr.ko hid-elecom.ko hid-gyration.ko \
774 hid-icade.ko hid-kensington.ko hid-kye.ko \
775 hid-lcpower.ko hid-magicmouse.ko hid-ntrig.ko \
776 hid-petalynx.ko hid-picolcd.ko hid-pl.ko \
777 hid-ps3remote.ko hid-quanta.ko \
778 'hid-roccat-ko*.ko' hid-roccat-pyra.ko \
779 hid-saitek.ko hid-sensor-hub.ko hid-sony.ko \
780 hid-speedlink.ko hid-tivo.ko hid-twinhan.ko \
781 hid-uclogic.ko hid-wacom.ko hid-waltop.ko \
782 hid-wiimote.ko hid-zydacron.ko
783 modules="$modules =drivers/input/serio"
784 modules="$modules =drivers/tty/serial"
785
786 # Any of these might be needed by other drivers
787 modules="$modules =drivers/bus"
788 modules="$modules =drivers/clk"
789 modules="$modules =drivers/devfreq"
790 modules="$modules =drivers/dma"
791 modules="$modules =drivers/extcon"
792 modules="$modules =drivers/gpio"
793 modules="$modules =drivers/hwspinlock"
794 modules="$modules =drivers/interconnect"
795 modules="$modules =drivers/i2c/busses"
796 modules="$modules =drivers/i2c/muxes"
797 modules="$modules =drivers/mailbox"
798 modules="$modules =drivers/memory"
799 modules="$modules =drivers/mfd"
800 modules="$modules =drivers/nvmem"
801 modules="$modules =drivers/pci/controller"
802 modules="$modules =drivers/phy"
803 modules="$modules =drivers/platform/chrome"
804 modules="$modules =drivers/power"
805 modules="$modules =drivers/pinctrl"
806 modules="$modules =drivers/regulator"
807 modules="$modules =drivers/reset"
808 modules="$modules =drivers/rpmsg"
809 modules="$modules =drivers/soc"
810 modules="$modules =drivers/spi"
811 modules="$modules =drivers/spmi"
812 modules="$modules =drivers/usb/phy"
813 modules="$modules =drivers/watchdog"
814
815 # Needed for periodic fsck
816 modules="$modules =drivers/rtc"
817 ;;
818 net)
819 exclude="cdc_mbim|ipheth|qmi_wwan|sierra_net|tun|veth|xen-netback"
820 exclude_dir="isdn|net/(802|ethernet|llc|phy|team)|uwb|wan|wireless"
821 _call_dracut_install -P "/((${exclude})\.ko|(${exclude_dir})/)" \
822 -s "eth_type_trans|register_virtio_device|usbnet_open" \
823 "=drivers/net"
824 modules="$modules =drivers/net/ethernet =drivers/net/mdio"
825 modules="$modules =drivers/net/phy cdc_eem ch9200 ipvlan"
826 modules="$modules netconsole r8153_ecm"
827 ;;
828 ide)
829 modules="$modules =drivers/ide"
830 ;;
831 mmc)
832 modules="$modules =drivers/mmc"
833 ;;
834 scsi)
835 _call_dracut_install -s "${blockfuncs}|iscsi_register_transport" \
836 "=drivers/message/fusion" "=drivers/s390/scsi" "=drivers/scsi"
837 # The following modules are not covered by the symbol filter
838 # in the previous call but should be included anyway.
839 modules="$modules cxgb3i cxgb4i scsi_dh_alua scsi_dh_emc"
840 modules="$modules scsi_dh_rdac scsi_transport_srp"
841 ;;
842 ata)
843 block_modules="$block_modules =drivers/ata"
844 ;;
845 block)
846 block_modules="$block_modules =drivers/block =drivers/nvme"
847 modules="$modules =drivers/ufs"
848 modules="$modules vmd"
849 ;;
850 ubi)
851 modules="$modules deflate zlib lzo ubi ubifs"
852 ;;
853 firewire)
854 modules="$modules firewire-ohci firewire-sbp2"
855 ;;
856 dasd)
857 modules="$modules dasd_diag_mod dasd_eckd_mod dasd_fba_mod"
858 ;;
859 usb_storage)
860 modules="$modules =drivers/usb/storage"
861 ;;
862 fb)
863 modules="$modules simpledrm simplefb framebuffer_coreboot"
864 # For machines that don't have a generic framebuffer device.
865 modules="$modules rockchipdrm pwm-cros-ec pwm_bl pwm-rockchip"
866 modules="$modules analogix-anx6345 pwm-sun4i sun4i-drm sun8i-mixer"
867 modules="$modules mediatek-drm pwm-mtk-disp anx7625 parade_ps8640 msm"
868 # For panel/backlight on MNT Reform 2
869 modules="$modules pwm_imx27 nwl-dsi ti-sn65dsi86 imx-dcss"
870 modules="$modules mux-mmio mxsfb"
871 modules="$modules =drivers/gpu/drm/bridge"
872 modules="$modules =drivers/gpu/drm/panel"
873 modules="$modules =drivers/video/backlight"
874 ;;
875 esac
876 done
877
878 # shellcheck disable=SC2086
879 manual_add_modules $modules
880 if [ -n "$block_modules" ]; then
881 # shellcheck disable=SC2086
882 _call_dracut_install -s "${blockfuncs}" $block_modules
883 fi
884 }
885
886 # 'depmod' only looks at symbol dependencies and the 'softdep' module
887 # information field; there is no way for modules to declare weaker
888 # dependencies (modules that *might* be needed at run-time) through
889 # module information, Until this is fixed, we need to handle those
890 # hidden dependencies.
891 hidden_dep_add_modules()
892 {
893 # shellcheck disable=SC2046
894 _call_dracut_install $(
895 {
896 cat "${DESTDIR}/lib/modules/${version}/modules.builtin"
897 if [ -d "${DESTDIR}/lib/modules/${version}/kernel" ]; then
898 find "${DESTDIR}/lib/modules/${version}/kernel" -name '*.ko*'
899 fi
900 } |
901 while read -r module; do
902 module="${module##*/}"
903 module="${module%%.*}"
904 case "$module" in
905 libcrc32c)
906 echo crc32c
907 ;;
908 ubifs)
909 echo deflate zlib lzo
910 ;;
911 btrfs)
912 echo crc32c xxhash64 sha256 blake2b-256
913 ;;
914 f2fs)
915 echo crc32
916 ;;
917 mlx4_core)
918 echo mlx4_ib
919 ;;
920 mlx5_core)
921 echo mlx5_ib
922 ;;
923 i8042)
924 echo psmouse
925 ;;
926 nvme)
927 echo vmd
928 ;;
929 spi-rockchip)
930 echo pl330
931 ;;
932 dw_mmc-rockchip)
933 echo rockchip-io-domain io-domain
934 ;;
935 mediatek-drm)
936 echo mediatek-drm-hdmi mtk_iommu mtk-smi
937 ;;
938 mtk_iommu)
939 echo mtk-smi
940 ;;
941 phy-mtk-mipi-dsi-drv)
942 echo nvmem_mtk-efuse
943 ;;
944 mt6397)
945 echo mtk-pmic-wrap
946 ;;
947 qmi_helpers)
948 echo qrtr
949 ;;
950 panel-edp)
951 # panels don't have devlink to backlight
952 echo pwm_bl
953 ;;
954 msm)
955 # DP devices which don't have devlinks
956 echo pmic_glink_altmode gpio_sbu_mux qrtr
957 ;;
958 framebuffer_coreboot)
959 # Either one, we don't know which is a module
960 echo simpledrm simplefb
961 ;;
962 esac
963 done
964 )
965 }
966
967 # Find the source for a script file. This is needed to work around
968 # temporary directories mounted with the noexec option. The source
969 # will be on / or /usr which must be executable.
970 get_source()
971 {
972 if [ -z "$scriptdir" ]; then
973 echo "${initdir}/$1"
974 elif [ -f "${CONFDIR}${scriptdir}/$1" ]; then
975 echo "${CONFDIR}${scriptdir}/$1"
976 else
977 echo "/usr/share/initramfs-tools${scriptdir}/$1"
978 fi
979 }
980
981 set_initlist()
982 {
983 unset initlist
984 for si_x in "${initdir}"/*; do
985 # skip empty dirs without warning
986 [ "${si_x}" = "${initdir}/*" ] && return
987
988 # only allow variable name chars
989 case "${si_x#"${initdir}"/}" in
990 *[![:alnum:]\._-]*)
991 [ "${verbose}" = "y" ] \
992 && echo "$si_x ignored: not alphanumeric or '_' file" >&2
993 continue
994 ;;
995 esac
996
997 # skip directories
998 if [ -d "${si_x}" ]; then
999 [ "${verbose}" = "y" ] \
1000 && echo "$si_x ignored: a directory" >&2
1001 continue
1002 fi
1003
1004 si_x="$(get_source "${si_x#"${initdir}"/}")"
1005
1006 # skip non executable scripts
1007 if [ ! -x "${si_x}" ]; then
1008 [ "${verbose}" = "y" ] \
1009 && echo "$si_x ignored: not executable" >&2
1010 continue
1011 fi
1012
1013 # skip bad syntax
1014 if ! sh -n "${si_x}" ; then
1015 [ "${verbose}" = "y" ] \
1016 && echo "$si_x ignored: bad syntax" >&2
1017 continue
1018 fi
1019
1020 initlist="${initlist:-} ${si_x##*/}"
1021 done
1022 }
1023
1024 get_prereq_pairs()
1025 {
1026 set_initlist
1027 for gp_x in ${initlist:-}; do
1028 echo "${gp_x} ${gp_x}"
1029 gp_src="$(get_source "$gp_x")"
1030 prereqs=$("${gp_src}" prereqs)
1031 for prereq in ${prereqs}; do
1032 echo "${prereq} ${gp_x}"
1033 done
1034 done
1035 }
1036
1037 # cache boot scripts order
1038 cache_run_scripts()
1039 {
1040 DESTDIR=${1}
1041 scriptdir=${2}
1042 initdir=${DESTDIR}${scriptdir}
1043 [ ! -d "${initdir}" ] && return
1044
1045 true > "${initdir}/ORDER"
1046 runlist=$(get_prereq_pairs | tsort)
1047 for crs_x in ${runlist}; do
1048 [ -f "${initdir}/${crs_x}" ] || continue
1049 echo "${scriptdir}/${crs_x} \"\$@\"" >> "${initdir}/ORDER"
1050 echo "[ -e /conf/param.conf ] && . /conf/param.conf" >> "${initdir}/ORDER"
1051 done
1052 }
1053
1054 call_scripts()
1055 {
1056 set -e
1057 for cs_x in ${runlist}; do
1058 [ -f "${initdir}/${cs_x}" ] || continue
1059 # mkinitramfs verbose output
1060 if [ "${verbose}" = "y" ]; then
1061 echo "Calling hook ${cs_x}"
1062 fi
1063 "${initdir}/${cs_x}" && ec=$? || ec=$?
1064 # allow hooks to abort build:
1065 if [ "$ec" -ne 0 ]; then
1066 echo "E: ${initdir}/${cs_x} failed with return $ec." >&2
1067 # only errexit on mkinitramfs
1068 [ -n "${version}" ] && exit "$ec"
1069 fi
1070 # allow boot scripts to modify exported boot parameters
1071 if [ -e /conf/param.conf ]; then
1072 . /conf/param.conf
1073 fi
1074 done
1075 set +e
1076 }
1077
1078 run_scripts()
1079 {
1080 scriptdir=${2:-}
1081 initdir=${1}
1082 [ ! -d "${initdir}" ] && return
1083
1084 runlist=$(get_prereq_pairs | tsort)
1085 call_scripts "$scriptdir"
1086 }