build_library: Fix depmod issues with sysext kmods

OS-dependent sysexts that ship kernel modules, usually also ship the
files in /usr/lib/modules/*-flatcar/modules.XXX When multiple such
sysexts get activated, depmod files from just one sysext win and other
kernel modules cannot be loaded using modprobe. We get around this by
removing the depmod files from every sysext with kernel modules.
Instead, we set up modprobe hook, which dynamically runs depmod in a
temporary directory on every sysext kernel module activation.

Signed-off-by: Daniel Zatovic <daniel.zatovic@gmail.com>
This commit is contained in:
Daniel Zatovic 2025-05-29 15:57:12 +02:00
parent 92c9dc568d
commit 18d5de0d0c
8 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,14 @@
#!/bin/bash
set -euo pipefail
SCRIPT_NAME=$(basename "$(realpath "${BASH_SOURCE[0]}")")
SYSEXT_NAME=${SCRIPT_NAME#sysext_mangle_}
SYSEXT_NAME=${SYSEXT_NAME%.sh}
DIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")")
. "$DIR/sysext_mangle_kmod"
rootfs="${1}"
cd "${rootfs}"
configure_modprobe "$SYSEXT_NAME"

View File

@ -0,0 +1 @@
sysext_mangle_flatcar-nvidia-drivers-535

View File

@ -0,0 +1 @@
sysext_mangle_flatcar-nvidia-drivers-535

View File

@ -0,0 +1 @@
sysext_mangle_flatcar-nvidia-drivers-535

View File

@ -0,0 +1 @@
sysext_mangle_flatcar-nvidia-drivers-535

View File

@ -0,0 +1 @@
sysext_mangle_flatcar-nvidia-drivers-535

View File

@ -3,6 +3,9 @@
set -euo pipefail
rootfs="${1}"
DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
. "$DIR/sysext_mangle_kmod"
pushd "${rootfs}"
rm -rf ./usr/{lib/debug/,lib64/cmake/,include/}
@ -40,4 +43,5 @@ cat <<EOF >./usr/lib/systemd/system/systemd-udevd.service.d/10-zfs.conf
[Unit]
After=systemd-sysext.service
EOF
configure_modprobe flatcar-zfs
popd

View File

@ -0,0 +1,48 @@
#!/bin/bash
configure_modprobe() {
local sysext_name="${1}"
shift
local module_directories=(./usr/lib/modules/*-flatcar/)
mkdir -p ./usr/lib/modprobe.d/
for module_name in $(find "${module_directories[@]}" -type f \( -name "*.ko" -o -name "*.ko.*" \) -printf "%f\n" | sed -E 's/\.ko(\.\w+)?$//'); do
cat <<EOF >> "./usr/lib/modprobe.d/10-${sysext_name}-kmod-sysext.conf"
install $module_name /usr/local/bin/_${sysext_name}_modprobe_helper $module_name
remove $module_name /usr/local/bin/_${sysext_name}_modprobe_helper -r $module_name
EOF
done
mkdir -p ./usr/local/bin/
install -m0755 -D /dev/stdin "./usr/local/bin/_${sysext_name}_modprobe_helper" <<'EOF'
#!/bin/bash
set -euo pipefail
action="Loading"
for arg in "$@"; do
if [[ $arg == "-r" ]]; then
action="Unloading"
fi
done
echo "$action kernel module from a sysext..."
KMOD_PATH=/usr/lib/modules/$(uname -r)
TMP_DIR=$(mktemp -d)
trap "rm -rf -- '${TMP_DIR}'" EXIT
mkdir "${TMP_DIR}"/{upper,work}
unshare -m bash -s -- "${@}" <<FOE
set -euo pipefail
if ! mountpoint -q "${KMOD_PATH}"; then
mount -t overlay overlay -o lowerdir="${KMOD_PATH}",upperdir="${TMP_DIR}"/upper,workdir="${TMP_DIR}"/work "${KMOD_PATH}"
depmod
fi
modprobe --ignore-install "\${@}"
FOE
EOF
# prevent the sysext from masking /usr/lib/modules/*-flatcar/modules.XXX
find "${module_directories[@]}" -maxdepth 1 -mindepth 1 -type f -delete
}