Skip to content

Commit 82344ac

Browse files
author
Rafal Stefanowski
committed
pckgen: Improve RPM building
- fix building on SUSE in mock environment - always allow specifying a partial kernel version and install the newest one matching - improve kernel version check - add option to list available kernels in mock environment Signed-off-by: Rafal Stefanowski <rafal.stefanowski@huawei.com>
1 parent 3150c3f commit 82344ac

File tree

2 files changed

+65
-36
lines changed

2 files changed

+65
-36
lines changed

tools/pckgen.d/rpm/CAS_NAME.spec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ BuildRequires: gcc
3737
BuildRequires: make
3838
BuildRequires: procps
3939
BuildRequires: python3
40-
BuildRequires: kernel = %{kver_pkg}
41-
BuildRequires: kernel-devel = %{kver_pkg}
4240
# Allow using different version of kernel-headers package (some distros requires it).
4341
BuildRequires: kernel-headers
42+
BuildRequires: <KERNEL_PKG> = %{kver_pkg}
43+
BuildRequires: <KERNEL_DEVEL_PKG> = %{kver_pkg}
4444
BuildRequires: <LIBELF_PKG>
4545
BuildRequires: <UTIL_PKG>
4646
Requires: <CAS_NAME>-modules-%{version}

tools/pckgen.sh

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ DEB_CONTROL_FILES_DIR="$SCRIPT_BASE_DIR/${THIS%.*}.d/deb/debian"
2828
PACKAGE_MAINTAINER="Rafal Stefanowski <rafal.stefanowski@open-cas.com>"
2929
PACKAGE_DATE="$(date -R)"
3030
TEMP_TEMPLATE="opencas-${THIS}"
31+
RHEL_KERNEL_PKG_NAME="kernel"
32+
RHEL_KERNEL_DEVEL_PKG_NAME="kernel-devel"
3133
RHEL_LIBELF_PKG_NAME="elfutils-libelf-devel"
3234
RHEL_UTIL_PKG_NAME="util-linux"
35+
SUSE_KERNEL_PKG_NAME="kernel-default"
36+
SUSE_KERNEL_DEVEL_PKG_NAME="kernel-default-devel"
3337
SUSE_LIBELF_PKG_NAME="libelf-devel"
3438
SUSE_UTIL_PKG_NAME="util-linux-systemd"
3539
DEPENDENCIES=(git mktemp rsync sed)
@@ -46,8 +50,8 @@ SUBMODULES=(
4650
)
4751

4852
# Unset all variables that may be checked for existence:
49-
unset ${!GENERATE_*} ARCHIVE_PREPARED DEBUG FAILED_DEPS KVER KVER_FULL MOCK_CFG OUTPUT_DIR\
50-
RPM_BUILT SOURCES_DIR SUBMODULES_MISSING TAR_CREATED
53+
unset ${!GENERATE_*} ARCHIVE_PREPARED DEBUG FAILED_DEPS KVER KVER_FULL MOCK_CFG MOCK_LIST_KERNELS\
54+
OUTPUT_DIR RPM_BUILT SOURCES_DIR SUBMODULES_MISSING TAR_CREATED
5155

5256

5357
usage() {
@@ -78,15 +82,18 @@ print_help() {
7882
echo " -a, --arch <ARCH> target platform architecture for packages"
7983
echo " -k, --kernel-version <KVER> build packages for specific kernel version;"
8084
echo " sources with headers for this kernel must be"
81-
echo " available in /lib/modules/ (version should be"
82-
echo " unambiguous enough to match only one installed kernel),"
83-
echo " or if used with --mock specific kernel version"
84-
echo " will be installed automatically if available in repo"
85-
echo " (usually the newest one matching given version)"
85+
echo " available in /lib/modules/, or if used with --mock"
86+
echo " specific kernel version will be installed automatically"
87+
echo " if available in repo"
88+
echo " (NOTE: if given version matches more than one available"
89+
echo " kernel, the newest one matching will be taken)"
8690
echo " -m, --mock <MOCK_CFG> use 'mock' to build RPMs in chrooted environment"
8791
echo " (RPM only; check 'mock' documentation for details);"
8892
echo " if --kernel-version is not provided, RPMs will be"
8993
echo " built with the default kernel for chosen config"
94+
echo " --mock-list-kernels instead of normal operation, just list available kernel"
95+
echo " versions within chosen mock environment and exit;"
96+
echo " if --kernel-version is provided, list only matching versions"
9097
echo " -o, --output-dir <DIR> put all created files in the given directory;"
9198
echo " default: 'SOURCES_PATH/packages/'"
9299
echo " -d, --debug include debug information and create debug packages"
@@ -165,6 +172,8 @@ check_options() {
165172
invalid_usage "nothing to do - no command provided"
166173
elif [ "$MOCK_CFG" ] && [ ! "$GENERATE_RPM" ]; then
167174
invalid_usage "mock is supported only with 'rpm' command"
175+
elif [ "$MOCK_LIST_KERNELS" ] && [ ! "$MOCK_CFG" ]; then
176+
invalid_usage "no mock config provided to check kernel versions in"
168177
fi
169178
}
170179

@@ -198,34 +207,22 @@ check_kernel_version() {
198207
if [ "$MOCK_CFG" ]; then
199208
echo "--- Checking kernel version in mock environment '$MOCK_CFG'"
200209

201-
# TODO: find a better way of obtaining kernel version in mock env
202-
# without installing anything and allowing partial version input
203-
204-
# 'kernel-devel' seems to be the only package available by that exact name on all
205-
# supported distros (including SUSE) that can be used to determine kernel version.
206-
if ! mock -r "$MOCK_CFG" -i kernel-devel${KVER:+-$KVER}; then
207-
error "couldn't install kernel-devel package in mock environment"
210+
if is_suse; then
211+
local KDEVEL_PKG="$SUSE_KERNEL_DEVEL_PKG_NAME"
212+
else
213+
local KDEVEL_PKG="$RHEL_KERNEL_DEVEL_PKG_NAME"
208214
fi
209-
210-
# Save kernel version to file and then copy it instead of using direct output,
211-
# as mock may sometimes print some warnings on stdout despite the --quiet flag.
212-
mock -r "$MOCK_CFG" --quiet --chroot "rpm -q kernel-devel --qf '%{VERSION}-%{RELEASE}.%{ARCH}\n' > /tmp/mock-kver.txt"
213-
mock -r "$MOCK_CFG" --quiet --copyout /tmp/mock-kver.txt "$TEMP_DIR"
214-
215-
KVER_FULL=$(cat "$TEMP_DIR/mock-kver.txt")
215+
KVER_FULL=$(mock -r "$MOCK_CFG" --dnf-cmd -- repoquery --quiet --available --qf '%{VERSION}-%{RELEASE}.%{ARCH}\n' "$KDEVEL_PKG" | grep "^${KVER:-[0-9]}" | sort -V | tail -n 1)
216216
if [ ! "$KVER_FULL" ]; then
217-
error "couldn't obtain version of the kernel package in mock environment"
217+
error "kernel version matching '${KVER:-any}' not found in '$MOCK_CFG'"
218218
fi
219219

220220
echo -e "--- Building in mock environment '$MOCK_CFG' for kernel version: \e[33m$KVER_FULL\e[0m"
221221
else
222222
if [ "$KVER" ]; then
223-
KVER_FULL=$(find /lib/modules/ -maxdepth 1 -type d -name "$KVER*" -printf "%f\n")
224-
local KVER_COUNT=$(echo "$KVER_FULL" | wc -w)
225-
if [ "$KVER_COUNT" -lt 1 ]; then
226-
error "kernel version '$KVER' not found in /lib/modules/ - try more general version, e.g. 6.16"
227-
elif [ "$KVER_COUNT" -gt 1 ]; then
228-
error "more than one kernel version '$KVER' found in /lib/modules/ - try to be more precise"
223+
KVER_FULL=$(find /lib/modules/ -maxdepth 1 -type d -name "$KVER*" -printf "%f\n" | sort -V | tail -n 1)
224+
if [ ! "$KVER_FULL" ]; then
225+
error "kernel version matching '$KVER' not found in /lib/modules/"
229226
fi
230227
fi
231228

@@ -289,6 +286,21 @@ mock_prepare() {
289286
fi
290287
}
291288

289+
mock_list_kernels() {
290+
echo "--- Listing available kernel versions in mock environment '$MOCK_CFG'"
291+
292+
if is_suse; then
293+
local KDEVEL_PKG="$SUSE_KERNEL_DEVEL_PKG_NAME"
294+
else
295+
local KDEVEL_PKG="$RHEL_KERNEL_DEVEL_PKG_NAME"
296+
fi
297+
local KVER_LIST=$(mock -r "$MOCK_CFG" --dnf-cmd -- repoquery --quiet --available --qf '%{VERSION}-%{RELEASE}.%{ARCH}\n' "$KDEVEL_PKG" | grep "^${KVER:-[0-9]}" | sort -V)
298+
299+
echo -e "\nAvailable kernel versions in '$MOCK_CFG'${KVER:+ matching '$KVER'}:\n${KVER_LIST[@]}"
300+
301+
exit 0
302+
}
303+
292304
archive_prepare() {
293305
if [ "$ARCHIVE_PREPARED" ]; then
294306
return 0
@@ -394,15 +406,28 @@ rpm_spec_prepare() {
394406
sed -i "s/<KVER>/%(uname -r)/g" "$RPM_SPECS_DIR/$CAS_NAME.spec"
395407
fi
396408

397-
if is_rhel; then
398-
sed -i "s/<LIBELF_PKG>/$RHEL_LIBELF_PKG_NAME/g" "$RPM_SPECS_DIR/$CAS_NAME.spec"
399-
sed -i "s/<UTIL_PKG>/$RHEL_UTIL_PKG_NAME/g" "$RPM_SPECS_DIR/$CAS_NAME.spec"
400-
elif is_suse; then
409+
if is_suse; then
410+
if [ "$MOCK_CFG" ]; then
411+
# On SUSE kernel packages versions not necessarily corelate with the actual
412+
# kernel version reported by uname (which is in fact the name of kernel
413+
# sources directory). Since uname command doesn't work in mock environment
414+
# (as it is just a chroot and it is reporting info from the host system),
415+
# this workaround sets kernel directory to whatever exists in /lib/modules/
416+
# counting on that there will be only one kernel sources installed.
417+
# Odds are much more in favor of this scenario, than of the rare case of
418+
# having a match between kernel package version and the actual kernel version.
419+
sed -i "/export KERNEL_DIR/s/%{kver}/*/g" "$RPM_SPECS_DIR/$CAS_NAME.spec"
420+
fi
421+
422+
sed -i "s/<KERNEL_PKG>/$SUSE_KERNEL_PKG_NAME/g" "$RPM_SPECS_DIR/$CAS_NAME.spec"
423+
sed -i "s/<KERNEL_DEVEL_PKG>/$SUSE_KERNEL_DEVEL_PKG_NAME/g" "$RPM_SPECS_DIR/$CAS_NAME.spec"
401424
sed -i "s/<LIBELF_PKG>/$SUSE_LIBELF_PKG_NAME/g" "$RPM_SPECS_DIR/$CAS_NAME.spec"
402425
sed -i "s/<UTIL_PKG>/$SUSE_UTIL_PKG_NAME/g" "$RPM_SPECS_DIR/$CAS_NAME.spec"
403426
else
404-
sed -i "/<LIBELF_PKG>/d" "$RPM_SPECS_DIR/$CAS_NAME.spec"
405-
sed -i "/<UTIL_PKG>/d" "$RPM_SPECS_DIR/$CAS_NAME.spec"
427+
sed -i "s/<KERNEL_PKG>/$RHEL_KERNEL_PKG_NAME/g" "$RPM_SPECS_DIR/$CAS_NAME.spec"
428+
sed -i "s/<KERNEL_DEVEL_PKG>/$RHEL_KERNEL_DEVEL_PKG_NAME/g" "$RPM_SPECS_DIR/$CAS_NAME.spec"
429+
sed -i "s/<LIBELF_PKG>/$RHEL_LIBELF_PKG_NAME/g" "$RPM_SPECS_DIR/$CAS_NAME.spec"
430+
sed -i "s/<UTIL_PKG>/$RHEL_UTIL_PKG_NAME/g" "$RPM_SPECS_DIR/$CAS_NAME.spec"
406431
fi
407432

408433
if [ "$DEBUG" ]; then
@@ -628,6 +653,9 @@ while (( $# )); do
628653
DEPENDENCIES_RPM+=(mock)
629654
shift
630655
;;
656+
--mock-list-kernels)
657+
MOCK_LIST_KERNELS="mock_list_kernels"
658+
;;
631659
--output-dir|-o)
632660
OUTPUT_DIR="$2"
633661
if ! dirname $OUTPUT_DIR &>/dev/null; then
@@ -712,6 +740,7 @@ echo -e "\n"
712740

713741
check_dependencies
714742
[ "$MOCK_CFG" ] && mock_prepare
743+
[ "$MOCK_LIST_KERNELS" ] && mock_list_kernels
715744
check_kernel_version
716745
create_dir "$OUTPUT_DIR"
717746
for package in ${!GENERATE_*}; do

0 commit comments

Comments
 (0)