1source $stdenv/setup
2
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.
6if ! 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
14 fi
15fi
16
17version=$(cd $kernel/lib/modules && ls -d *)
18
19echo "kernel version is $version"
20
21# Determine the dependencies of each root module.
22closure=
23for module in $rootModules; do
24 echo "root module: $module"
25 deps=$(modprobe --config no-config -d $kernel --set-version "$version" --show-depends "$module" \
26 | sed 's/^insmod //') \
27 || if test -z "$allowMissing"; then exit 1; fi
28 if [[ "$deps" != builtin* ]]; then
29 closure="$closure $deps"
30 fi
31done
32
33echo "closure:"
34mkdir -p $out/lib/modules/"$version"
35for module in $closure; do
36 target=$(echo $module | sed "s^$NIX_STORE.*/lib/modules/^$out/lib/modules/^")
37 if test -e "$target"; then continue; fi
38 if test \! -e "$module"; then continue; fi # XXX: to avoid error with "cp builtin builtin"
39 mkdir -p $(dirname $target)
40 echo $module
41 cp $module $target
42 # If the kernel is compiled with coverage instrumentation, it
43 # contains the paths of the *.gcda coverage data output files
44 # (which it doesn't actually use...). Get rid of them to prevent
45 # the whole kernel from being included in the initrd.
46 nuke-refs $target
47 echo $target >> $out/insmod-list
48done
49
50mkdir -p $out/lib/firmware
51for module in $closure; do
52 for i in $(modinfo -F firmware $module); do
53 mkdir -p "$out/lib/firmware/$(dirname "$i")"
54 echo "firmware for $module: $i"
55 cp "$firmware/lib/firmware/$i" "$out/lib/firmware/$i" 2>/dev/null || if test -z "$allowMissing"; then exit 1; fi
56 done
57done
58
59depmod -b $out -a $version