Skip to content

build_library: Fix depmod issues with sysext kmods #2976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions build_library/sysext_mangle_flatcar-nvidia-drivers-535
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions build_library/sysext_mangle_flatcar-nvidia-drivers-550
1 change: 1 addition & 0 deletions build_library/sysext_mangle_flatcar-nvidia-drivers-570
4 changes: 4 additions & 0 deletions build_library/sysext_mangle_flatcar-zfs
Original file line number Diff line number Diff line change
Expand Up @@ -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/}
Expand Down Expand Up @@ -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
48 changes: 48 additions & 0 deletions build_library/sysext_mangle_kmod
Original file line number Diff line number Diff line change
@@ -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/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized /use/libexec would be a better location for these internal helpers.

There are scenarios where /ust/local needs to be writeable and that is accomplished by using a sysext that redirects /usr/local to a different path using a symlink. Using the same path would conflict with those scenarios.

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
}