Skip to content

extension: add ksrc packaging #8211

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
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
60 changes: 60 additions & 0 deletions extensions/linux-source-package.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
function extension_prepare_config__linux_source_package_extension() {
display_alert "Packaging kernel source enabled. This will enforce ARTIFACT_IGNORE_CACHE=yes in order to prepare the source code." "${EXTENSION}" "info"
declare -g ARTIFACT_IGNORE_CACHE=yes # enforce building from scratch
declare -g KERNEL_GIT=shallow # download necessary branch only
}

function add_host_dependencies__add_fakeroot() {
display_alert "Adding packages to host dependencies" "${EXTENSION}" "info"
EXTRA_BUILD_DEPS="${EXTRA_BUILD_DEPS} fakeroot"
}

function armbian_kernel_config__create_ksrc_package() {
if [[ -f .config ]]; then

#( set -o posix ; set )
echo ${kernel_version_family}

display_alert "Packaging kernel source..." "${EXTENSION}" "info"
declare kernel_work_dir="${SRC}/cache/sources/${LINUXSOURCEDIR}"
declare CHOSEN_KSRC=linux-source-${BRANCH}-${LINUXFAMILY}

ts=$(date +%s)
local sources_pkg_dir tmp_src_dir
tmp_src_dir=$(mktemp -d)
trap "ret=\$?; rm -rf \"${tmp_src_dir}\" ; exit \$ret" 0 1 2 3 15
sources_pkg_dir=${tmp_src_dir}/${CHOSEN_KSRC}_${REVISION}_all
mkdir -p "${sources_pkg_dir}"/usr/src/ \
Comment on lines +20 to +24
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve trap quoting and exit handling
The trap uses double quotes causing premature expansion and references ret before assignment (SC2064, SC2154). A more robust pattern is:

-trap "ret=\$?; rm -rf \"${tmp_src_dir}\" ; exit \$ret" 0 1 2 3 15
+trap 'rc=$?; rm -rf "'"${tmp_src_dir}"'"; exit $rc' 0 1 2 3 15

Alternatively, simplify to:

trap 'rm -rf "${tmp_src_dir}"' EXIT INT TERM
🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 25-25: ret is referenced but not assigned.

(SC2154)


[warning] 25-25: Use single quotes, otherwise this expands now rather than when signalled.

(SC2064)

🤖 Prompt for AI Agents
In extensions/linux-source-package.sh around lines 23 to 27, the trap command
uses double quotes causing premature variable expansion and references the
variable ret before it is assigned. To fix this, replace the double quotes with
single quotes in the trap command to delay expansion, or simplify the trap to
just remove the temporary directory on EXIT, INT, and TERM signals without
capturing the exit code. For example, use trap 'rm -rf "${tmp_src_dir}"' EXIT
INT TERM to ensure proper cleanup without premature expansion or undefined
variable usage.

"${sources_pkg_dir}"/usr/share/doc/linux-source-${version}-${LINUXFAMILY} \
"${sources_pkg_dir}"/DEBIAN

cp "${SRC}/config/kernel/${LINUXCONFIG}.config" "default_${LINUXCONFIG}.config"
xz < ${kernel_work_dir}/.config > "${sources_pkg_dir}/usr/src/${LINUXCONFIG}_${version}_${REVISION}_config.xz"

display_alert "Compressing sources for the linux-source package" "${EXTENSION}" "info"
tar cp --directory="$kernel_work_dir" --exclude='.git' --owner=root . |
pv -p -b -r -s "$(du -sb "$kernel_work_dir" --exclude=='.git' | cut -f1)" |
xz -T0 -1 > "${sources_pkg_dir}/usr/src/linux-source-${version}-${LINUXFAMILY}.tar.xz"
cp ${kernel_work_dir}/COPYING "${sources_pkg_dir}/usr/share/doc/linux-source-${version}-${LINUXFAMILY}/LICENSE"

cat <<- EOF > "${sources_pkg_dir}"/DEBIAN/control
Package: linux-source-${version}-${BRANCH}-${LINUXFAMILY}
Version: ${version}-${BRANCH}-${LINUXFAMILY}+${REVISION}
Architecture: all
Maintainer: $MAINTAINER <$MAINTAINERMAIL>
Section: kernel
Priority: optional
Depends: binutils, coreutils, linux-base
Provides: linux-source, linux-source-${REVISION}-${LINUXFAMILY}
Recommends: gcc, make
Description: This package provides the source code for the Linux kernel $REVISION
EOF

fakeroot dpkg-deb -b -z0 "${sources_pkg_dir}" "${sources_pkg_dir}.deb"
rsync --remove-source-files -rq "${sources_pkg_dir}.deb" "${DEB_STORAGE}/"

te=$(date +%s)
display_alert "Make the linux-source package" "$(($te - $ts)) sec." "info"
rm -rf "${tmp_src_dir}"
fi
}