Skip to content

Commit 5e4cc8f

Browse files
committed
sys-kernel/coreos-modules: Install external mod build files with script
The kernel now includes a script for installing the files needed to build out-of-tree modules, rendering our existing code obsolete. The layout is different, but we were following Ubuntu's non-standard layout when there was no need to. Ubuntu's approach is seemingly designed to save space by symlinking common files across different platforms, but Flatcar doesn't need to do this. More importantly, our previous approach relied on a kernel patch we have carried for years that no longer applies from v6.13. The patch cannot simply be reworked as the underlying mechanism has changed. This clears the last major blocker for the arm64 SDK as the previous approach also relied on implicit execution by QEMU. There has been concern that this may break compatibility with some modules, but I have not seen any issues in practise. I have symlinked `source` to `build` even though we don't install the full kernel sources because this is what Fedora does, and it makes the layout resemble Ubuntu a little more. Should any issues arise, I will gladly work with upstreams to resolve them or otherwise make adjustments. Signed-off-by: James Le Cuirot <jlecuirot@microsoft.com>
1 parent f2cd2b3 commit 5e4cc8f

File tree

3 files changed

+18
-108
lines changed

3 files changed

+18
-108
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- The way that files for building custom kernel modules are installed has changed from a Ubuntu-inspired method to the standard upstream kernel method. In the unlikely event that this breaks your module builds, please let the Flatcar team know immediately.

sdk_container/src/third_party/coreos-overlay/eclass/coreos-kernel.eclass

Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ kmake() {
117117
if gcc-specs-pie; then
118118
kernel_cflags="-nopie -fstack-check=no ${kernel_cflags}"
119119
fi
120-
emake "--directory=${S}/source" \
120+
emake "--directory=${KERNEL_DIR}" \
121121
ARCH="${kernel_arch}" \
122122
CROSS_COMPILE="${CHOST}-" \
123-
KBUILD_OUTPUT="../build" \
123+
KBUILD_OUTPUT="${S}/build" \
124124
KCFLAGS="${kernel_cflags}" \
125125
LDFLAGS="" \
126126
"V=1" \
@@ -206,85 +206,6 @@ setup_keys() {
206206
popd
207207
}
208208

209-
# Populate /lib/modules/$(uname -r)/{build,source}
210-
install_build_source() {
211-
local kernel_arch=$(tc-arch-kernel)
212-
local host_kernel_arch=$(tc-ninja_magic_to_arch kern "${CBUILD}")
213-
214-
# NOTE: We have to get ${archabspaths} before removing symlinks under
215-
# /usr/lib/modules. However, do not exclude "dt-bindings" for now,
216-
# as it looks architecture-independent.
217-
local archabspaths=($(ls -1d ${D}/usr/lib/modules/${KV_FULL}/source/scripts/dtc/include-prefixes/* \
218-
| grep -v dt-bindings ))
219-
220-
# remove the broken symlinks referencing $ROOT
221-
rm "${D}/usr/lib/modules/${KV_FULL}/build" || die
222-
223-
# Compose list of architectures to be excluded from the kernel modules
224-
# tree in the final image. It is an array to be used as a pattern for
225-
# grep command below at the end of "find source/scripts" command for
226-
# fetching kernel modules list, e.g.:
227-
# find source/scripts -follow -print \
228-
# | grep -E -v -w "include-prefixes/arc|include-prefixes/xtensa"
229-
declare -a excarchlist
230-
local excarchstr
231-
232-
for apath in "${archabspaths[@]}"; do
233-
local arch
234-
arch=$(basename "${apath}")
235-
if [[ "${arch}" != "${kernel_arch}" ]]; then
236-
excarchlist+=("include-prefixes/${arch}")
237-
238-
# Do not append delimiter '|' in case of the last element.
239-
if [[ "${apath}" != "${archabspaths[-1]}" ]]; then
240-
excarchlist+=("|")
241-
fi
242-
fi
243-
done
244-
245-
# Remove every whitespace from the grep pattern string, to make pattern
246-
# matching work well.
247-
excarchstr=$(echo "${excarchlist[@]}" | sed -e 's/[[:space:]]*//g')
248-
249-
# Install a stripped source for out-of-tree module builds (Debian-derived)
250-
#
251-
# NOTE: we need to exclude unsupported architectures from source/scripts,
252-
# to prevent the final image from having unnecessary directories under
253-
# /usr/lib/modules/${KV_FULL}/source/scripts/dtc/include-prefixes.
254-
# The grep must run with "-w" to exclude exact patterns like either arm
255-
# or arm64.
256-
{
257-
echo source/Makefile
258-
find source/arch/${host_kernel_arch} -follow -maxdepth 1 -name 'Makefile*' -print
259-
find source/arch/${kernel_arch} -follow -maxdepth 1 -name 'Makefile*' -print
260-
find source/arch/${kernel_arch} -follow \( -name 'module.lds' -o -name 'Kbuild.platforms' -o -name 'Platform' \) -print
261-
find $(find source/arch/${kernel_arch} -follow \( -name include -o -name scripts \) -follow -type d -print) -print
262-
find source/include -follow -print
263-
find source/scripts -follow -print | grep -E -v -w "${excarchstr}"
264-
find build/ -print
265-
} | cpio -pd \
266-
--preserve-modification-time \
267-
--owner=root:root \
268-
--dereference \
269-
"${D}/usr/lib/modules/${KV_FULL}" || die
270-
# ./build/source is a symbolic link so cpio ends up creating an empty dir.
271-
# Restore the symlink.
272-
pushd "${D}/usr/lib/modules/${KV_FULL}"
273-
rmdir build/source || die
274-
ln -sr source build || die
275-
# Symlink includes into the build directory to resemble Ubuntu's /lib/modules
276-
# layout. This lets the Nvidia driver build when passing SYSSRC=/lib/modules/../build
277-
# instead of requiring SYSOUT/SYSSRC.
278-
{
279-
find source/include -mindepth 1 -maxdepth 1 -type d
280-
find source/arch/${kernel_arch}/include -mindepth 1 -maxdepth 1 -type d
281-
} | while read src; do
282-
dst="${src/source/build}"
283-
ln -sr "${src}" "${dst}" || die
284-
done || die
285-
popd
286-
}
287-
288209
coreos-kernel_pkg_pretend() {
289210
[[ "${MERGE_TYPE}" == binary ]] && return
290211

@@ -302,10 +223,7 @@ coreos-kernel_pkg_setup() {
302223
}
303224

304225
coreos-kernel_src_unpack() {
305-
# we more or less reproduce the layout in /lib/modules/$(uname -r)/
306226
mkdir -p "${S}/build" || die
307-
mkdir -p "${S}/source" || die
308-
ln -s "${KERNEL_DIR}"/* "${S}/source/" || die
309227
}
310228

311229
coreos-kernel_src_configure() {

sdk_container/src/third_party/coreos-overlay/sys-kernel/coreos-modules/coreos-modules-6.12.54.ebuild

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,41 +35,32 @@ src_compile() {
3535
}
3636

3737
src_install() {
38+
local build="lib/modules/${KV_FULL}/build"
39+
3840
# Install modules to /usr.
39-
# Install firmware to a temporary (bogus) location.
40-
# The linux-firmware package will be used instead.
4141
# Stripping must be done here, not portage, to preserve sigs.
42-
kmake INSTALL_MOD_PATH="${D}/usr" \
42+
kmake INSTALL_MOD_PATH="${ED}/usr" \
4343
INSTALL_MOD_STRIP="--strip-debug" \
44-
INSTALL_FW_PATH="${T}/fw" \
4544
modules_install
4645

4746
# Install to /usr/lib/debug with debug symbols intact
48-
kmake INSTALL_MOD_PATH="${D}/usr/lib/debug/usr" \
49-
INSTALL_FW_PATH="${T}/fw" \
47+
kmake INSTALL_MOD_PATH="${ED}/usr/lib/debug/usr" \
5048
modules_install
51-
rm "${D}/usr/lib/debug/usr/lib/modules/${KV_FULL}/"modules.* || die
52-
rm "${D}/usr/lib/debug/usr/lib/modules/${KV_FULL}/build" || die
53-
54-
# Clean up the build tree
55-
kmake clean
49+
rm "${ED}/usr/lib/debug/usr/lib/modules/${KV_FULL}"/{build,modules.*} || die
5650

57-
# TODO: ensure that fixdep and kbuild tools shipped inside the image
58-
# are native (we previously shipped amd64 binaries on arm64).
59-
# Upstream has a new script from v6.12 that we might be able to use:
60-
# scripts/package/install-extmod-build
61-
kmake HOSTLD=$(tc-getLD) HOSTCC=$(tc-getCC) cmd_and_fixdep='$(cmd)' modules_prepare
62-
kmake clean
51+
# Replace the broken /lib/modules/${KV_FULL}/build symlink with a copy of
52+
# the files needed to build out-of-tree modules.
53+
rm "${ED}/usr/${build}" || die
54+
kmake run-command KBUILD_RUN_COMMAND="${KERNEL_DIR}/scripts/package/install-extmod-build ${ED}/usr/${build}"
6355

64-
find "build/" -type d -empty -delete || die
65-
rm "build/.config.old" || die
66-
67-
# Install /lib/modules/${KV_FULL}/{build,source}
68-
install_build_source
56+
# Install the original config because the above doesn't.
57+
insinto "/usr/${build}"
58+
doins build/.config
6959

7060
# Not strictly required but this is where we used to install the config.
71-
dodir "/usr/boot"
72-
local build="lib/modules/${KV_FULL}/build"
7361
dosym "../${build}/.config" "/usr/boot/config-${KV_FULL}"
7462
dosym "../${build}/.config" "/usr/boot/config"
63+
64+
# Symlink "source" to "build" for compatibility. Fedora does this.
65+
dosym build "/usr/${build}/../source"
7566
}

0 commit comments

Comments
 (0)