eclass: exclude unnecessary architectures from DTC in Kernel

DTC (Device Tree Compiler) source tree in Flatcar Kernel modules
unnecessarily takes too much space, especially the `include-prefixes`
directory.

```
$ sudo du -a /usr/lib64/modules/$(uname -r)/source/ | sort -n -r | head -n5
130100  /usr/lib64/modules/5.8.11-flatcar/source/
69180   /usr/lib64/modules/5.8.11-flatcar/source/include
56324   /usr/lib64/modules/5.8.11-flatcar/source/scripts
51384   /usr/lib64/modules/5.8.11-flatcar/source/scripts/dtc
50728   /usr/lib64/modules/5.8.11-flatcar/source/scripts/dtc/include-prefixes
$ sudo ls /usr/lib64/modules/$(uname -r)/source/scripts/dtc/include-prefixes/
arc  arm  arm64  c6x  dt-bindings  h8300  microblaze  mips  nios2 openrisc  powerpc  sh  xtensa
```

Most of them are for architectures that are not supported by Flatcar, so
we can remove them from the production image.

OTOH, as `dt-bindings` looks more like an architecture-independent one,
for now we keep it.

Before:

```
$ du -s /usr/lib64/modules/$(uname -r)/source/scripts/dtc/
51384   /usr/lib64/modules/5.8.11-flatcar/source/scripts/dtc/
$ du -s /usr/lib64/modules/
250308  /usr/lib64/modules/
$ df /usr
Filesystem      1K-blocks   Used Available Use% Mounted on
/dev/mapper/usr   1007760 934152     21592  98% /usr
```

After:

```
$ du -s /usr/lib64/modules/$(uname -r)/source/scripts/dtc/
6632    /usr/lib64/modules/5.8.11-flatcar/source/scripts/dtc/
$ du -s /usr/lib64/modules/
205144  /usr/lib64/modules/
$ df /usr
Filesystem      1K-blocks   Used Available Use% Mounted on
/dev/mapper/usr   1007760 907628     48116  95% /usr
```
This commit is contained in:
Dongsu Park 2020-09-30 07:39:09 +02:00
parent b5559c2485
commit c335e197bd

View File

@ -187,16 +187,55 @@ shred_keys() {
install_build_source() {
local kernel_arch=$(tc-arch-kernel)
# NOTE: We have to get ${archabspaths} before removing symlinks under
# /usr/lib/modules. However, do not exclude "dt-bindings" for now,
# as it looks architecture-independent.
local archabspaths=($(ls -1d ${D}/usr/lib/modules/${KV_FULL}/source/scripts/dtc/include-prefixes/* \
| grep -v dt-bindings ))
# remove the broken symlinks referencing $ROOT
rm "${D}/usr/lib/modules/${KV_FULL}"/{build,source} || die
# Compose list of architectures to be excluded from the kernel modules
# tree in the final image. It is an array to be used as a pattern for
# grep command below at the end of "find source/scripts" command for
# fetching kernel modules list, e.g.:
# find source/scripts -follow -print \
# | grep -E -v -w "include-prefixes/arc|include-prefixes/xtensa"
declare -a excarchlist
local excarchstr
for apath in "${archabspaths[@]}"; do
local arch
arch=$(basename "${apath}")
if [[ "${arch}" != "${kernel_arch}" ]]; then
excarchlist+=("include-prefixes/${arch}")
# Do not append delimiter '|' in case of the last element.
if [[ "${apath}" != "${archabspaths[-1]}" ]]; then
excarchlist+=("|")
fi
fi
done
# Remove every whitespace from the grep pattern string, to make pattern
# matching work well.
excarchstr=$(echo "${excarchlist[@]}" | sed -e 's/[[:space:]]*//g')
# Install a stripped source for out-of-tree module builds (Debian-derived)
#
# NOTE: we need to exclude unsupported architectures from source/scripts,
# to prevent the final image from having unnecessary directories under
# /usr/lib/modules/${KV_FULL}/source/scripts/dtc/include-prefixes.
# The grep must run with "-w" to exclude exact patterns like either arm
# or arm64.
{
echo source/Makefile
find source/arch/${kernel_arch} -follow -maxdepth 1 -name 'Makefile*' -print
find source/arch/${kernel_arch} -follow \( -name 'module.lds' -o -name 'Kbuild.platforms' -o -name 'Platform' \) -print
find $(find source/arch/${kernel_arch} -follow \( -name include -o -name scripts \) -follow -type d -print) -print
find source/include source/scripts -follow -print
find source/include -follow -print
find source/scripts -follow -print | grep -E -v -w "${excarchstr}"
find build/ -print
} | cpio -pd \
--preserve-modification-time \