Skip to content

Commit 50f459b

Browse files
committed
kpatch-build: building OOT livepatch modules for kernels built from source
Ability to build out-of-tree modules for non-host kernel is currently missing. This commit introduces that ability for any kernel built from source, including cross-compiled ones. The workaround checks for existence of /lib/modules/$ARCHVERSION/build, and if nonexistent, assumes the kernel source directory to be a build directory.
1 parent 61fc3f1 commit 50f459b

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

kpatch-build/kpatch-build

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -540,13 +540,14 @@ usage() {
540540
echo " (can be specified multiple times)" >&2
541541
echo " -e, --oot-module Enable patching out-of-tree module," >&2
542542
echo " specify current version of module" >&2
543+
echo " -p, --oot-module-src Set OOT module source directory" >&2
543544
echo " -R, --non-replace Disable replace patch (replace is on by default)" >&2
544545
echo " --skip-cleanup Skip post-build cleanup" >&2
545546
echo " --skip-compiler-check Skip compiler version matching check" >&2
546547
echo " (not recommended)" >&2
547548
}
548549

549-
options="$(getopt -o ha:r:s:c:v:j:t:n:o:de:R -l "help,archversion:,sourcerpm:,sourcedir:,config:,vmlinux:,jobs:,target:,name:,output:,oot-module:,debug,skip-gcc-check,skip-compiler-check,skip-cleanup,non-replace" -- "$@")" || die "getopt failed"
550+
options="$(getopt -o ha:p:r:s:c:v:j:t:n:o:de:R -l "help,archversion:,oot-module-src:,sourcerpm:,sourcedir:,config:,vmlinux:,jobs:,target:,name:,output:,oot-module:,debug,skip-gcc-check,skip-compiler-check,skip-cleanup,non-replace" -- "$@")" || die "getopt failed"
550551

551552
eval set -- "$options"
552553

@@ -609,6 +610,11 @@ while [[ $# -gt 0 ]]; do
609610
OOT_MODULE="$(readlink -f "$2")"
610611
shift
611612
;;
613+
-p|--oot-module-src)
614+
[[ ! -d "$2" ]] && die "OOT module source dir '$2' not found"
615+
OOT_SOURCE="$(readlink -f "$2")"
616+
shift
617+
;;
612618
-R|--non-replace)
613619
KLP_REPLACE=0
614620
;;
@@ -662,6 +668,16 @@ if [[ -n "$OOT_MODULE" ]] && [[ -z "$USERSRCDIR" ]]; then
662668
exit 1
663669
fi
664670

671+
672+
if [[ -n "$OOT_MODULE" ]] && [[ -z "$OOT_SOURCE" ]]; then
673+
if [[ -e "$(dirname "$OOT_MODULE")/Makefile" ]]; then
674+
OOT_SOURCE="$(dirname "$OOT_MODULE")"
675+
else
676+
warn "--oot-module requires --oot-module-src"
677+
exit 1
678+
fi
679+
fi
680+
665681
# ensure cachedir and tempdir are setup properly and cleaned
666682
mkdir -p "$TEMPDIR" || die "Couldn't create $TEMPDIR"
667683
rm -rf "${TEMPDIR:?}"/*
@@ -672,7 +688,9 @@ if [[ -n "$USERSRCDIR" ]]; then
672688
warn "--archversion is incompatible with --sourcedir"
673689
exit 1
674690
fi
675-
SRCDIR="$USERSRCDIR"
691+
692+
[[ -z "$OOT_MODULE" ]] && SRCDIR="$USERSRCDIR" || SRCDIR="$OOT_SOURCE"
693+
676694

677695
if [[ -z "$OOT_MODULE" ]]; then
678696
[[ -z "$VMLINUX" ]] && VMLINUX="$SRCDIR"/vmlinux
@@ -914,6 +932,7 @@ if [[ "$SKIPCOMPILERCHECK" -eq 0 ]]; then
914932
fi
915933

916934
echo "Testing patch file(s)"
935+
917936
cd "$SRCDIR" || die
918937
verify_patch_files
919938
apply_patches
@@ -1050,9 +1069,12 @@ ERROR=0
10501069

10511070
# Prepare OOT module symvers file
10521071
if [[ -n "$OOT_MODULE" ]]; then
1053-
BUILDDIR="/lib/modules/$ARCHVERSION/build/"
1054-
cp -f "$SRCDIR/Module.symvers" "$TEMPDIR/Module.symvers" || die
1055-
awk '{ print $1 "\t" $2 "\t" $3 "\t" $4}' "${BUILDDIR}/Module.symvers" >> "$TEMPDIR/Module.symvers"
1072+
cp -f "$SRCDIR/Module.symvers" "$TEMPDIR/Module.symvers" || die
1073+
# merge host symbols only if we do compile for host kernel
1074+
if [[ -e "/lib/modules/$ARCHVERSION/build/" ]]; then
1075+
BUILDDIR="/lib/modules/$ARCHVERSION/build/"
1076+
awk '{ print $1 "\t" $2 "\t" $3 "\t" $4}' "${BUILDDIR}/Module.symvers" >> "$TEMPDIR/Module.symvers"
1077+
fi
10561078
fi
10571079

10581080
for i in $FILES; do
@@ -1161,8 +1183,11 @@ else
11611183
fi
11621184

11631185
cd "$TEMPDIR/patch" || die
1186+
11641187
if [[ -z "$OOT_MODULE" ]]; then
11651188
KPATCH_BUILD="$SRCDIR"
1189+
elif [[ ! -e "/lib/modules/$ARCHVERSION/build" ]]; then
1190+
KPATCH_BUILD="$USERSRCDIR"
11661191
else
11671192
KPATCH_BUILD="/lib/modules/$ARCHVERSION/build"
11681193
fi

0 commit comments

Comments
 (0)