biome: 1.9.2 -> 1.9.3
[NixPkgs.git] / pkgs / build-support / kernel / modules-closure.sh
blobb581b6e7d403f5c1015bb21f3f268aceb60e4e06
1 source $stdenv/setup
3 # When no modules are built, the $out/lib/modules directory will not
4 # exist. Because the rest of the script assumes it does exist, we
5 # handle this special case first.
6 if ! test -d "$kernel/lib/modules"; then
7 if test -z "$rootModules" || test -n "$allowMissing"; then
8 mkdir -p "$out"
9 exit 0
10 else
11 echo "Required modules: $rootModules"
12 echo "Can not derive a closure of kernel modules because no modules were provided."
13 exit 1
17 version=$(cd $kernel/lib/modules && ls -d *)
19 echo "kernel version is $version"
21 # Determine the dependencies of each root module.
22 mkdir -p $out/lib/modules/"$version"
23 touch closure
24 for module in $rootModules; do
25 echo "root module: $module"
26 modprobe --config no-config -d $kernel --set-version "$version" --show-depends "$module" \
27 | while read cmd module args; do
28 case "$cmd" in
29 builtin)
30 touch found
31 echo "$module" >>closure
32 echo " builtin dependency: $module";;
33 insmod)
34 touch found
35 if ! test -e "$module"; then
36 echo " dependency not found: $module"
37 exit 1
39 target=$(echo "$module" | sed "s^$NIX_STORE.*/lib/modules/^$out/lib/modules/^")
40 if test -e "$target"; then
41 echo " dependency already copied: $module"
42 continue
44 echo "$module" >>closure
45 echo " copying dependency: $module"
46 mkdir -p $(dirname $target)
47 cp "$module" "$target"
48 # If the kernel is compiled with coverage instrumentation, it
49 # contains the paths of the *.gcda coverage data output files
50 # (which it doesn't actually use...). Get rid of them to prevent
51 # the whole kernel from being included in the initrd.
52 nuke-refs "$target"
53 echo "$target" >> $out/insmod-list;;
55 echo " unexpected modprobe output: $cmd $module"
56 exit 1;;
57 esac
58 done || test -n "$allowMissing"
59 if ! test -e found; then
60 echo " not found"
61 if test -z "$allowMissing"; then
62 exit 1
64 else
65 rm found
67 done
69 cd "$firmware"
70 for module in $(< ~-/closure); do
71 # for builtin modules, modinfo will reply with a wrong output looking like:
72 # $ modinfo -F firmware unix
73 # name: unix
75 # There is a pending attempt to fix this:
76 # https://github.com/NixOS/nixpkgs/pull/96153
77 # https://lore.kernel.org/linux-modules/20200823215433.j5gc5rnsmahpf43v@blumerang/T/#u
79 # For now, the workaround is just to filter out the extraneous lines out
80 # of its output.
81 modinfo -b $kernel --set-version "$version" -F firmware $module | grep -v '^name:' | while read -r i; do
82 echo "firmware for $module: $i"
83 for name in "$i" "$i.xz" "$i.zst" ""; do
84 [ -z "$name" ] && echo "WARNING: missing firmware $i for module $module"
85 if cp -v --parents --no-preserve=mode lib/firmware/$name "$out" 2>/dev/null; then
86 break
88 done
89 done || :
90 done
92 if test -e lib/firmware/edid ; then
93 echo "lib/firmware/edid found, copying."
94 mkdir -p "$out/lib/firmware"
95 cp -v --no-preserve=mode --recursive --dereference --no-target-directory lib/firmware/edid "$out/lib/firmware/edid"
96 else
97 echo "lib/firmware/edid not found, skipping."
100 # copy module ordering hints for depmod
101 cp $kernel/lib/modules/"$version"/modules.order $out/lib/modules/"$version"/.
102 cp $kernel/lib/modules/"$version"/modules.builtin $out/lib/modules/"$version"/.
104 depmod -b $out -a $version
106 # remove original hints from final derivation
107 rm $out/lib/modules/"$version"/modules.order
108 rm $out/lib/modules/"$version"/modules.builtin