Skip to content

Commit 649b814

Browse files
authored
Merge pull request #258 from sky1122/makefile_hygiene
Makefile: fix targets `merge-kernel-configs` and `full-config`
2 parents bafbcdd + 4c55d63 commit 649b814

File tree

3 files changed

+147
-134
lines changed

3 files changed

+147
-134
lines changed

Makefile

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -22,66 +22,8 @@ endif
2222

2323
all: build
2424

25-
merge-kernel-configs:
26-
@echo "Validating parameters for merge-kernel-configs..."
27-
@usage_msg="Usage: make merge-kernel-configs RPM_FILE=/path/to/kernel-source.rpm SDK_IMAGE=image:tag KVER=x.y"; \
28-
validate_param() { \
29-
param_name="$${1}"; param_value="$${2}"; error_msg="$${3}"; \
30-
[ -n "$${param_value}" ] || { echo "Error: $${error_msg}"; echo "$${usage_msg}"; exit 1; }; \
31-
}; \
32-
validate_param "SDK_IMAGE" "$(SDK_IMAGE)" "SDK_IMAGE parameter is required"; \
33-
validate_param "KVER" "$(KVER)" "KVER parameter is required"; \
34-
echo "All parameter checks passed. Merging kernel configs for version $(KVER)..."
35-
36-
@tmpdir=$$(mktemp -d); \
37-
if [ $$? -ne 0 ]; then \
38-
echo "Error: Failed to create temporary directory"; \
39-
exit 1; \
40-
fi; \
41-
cp "$(RPM_FILE)" "$${tmpdir}/kernel-source.rpm"; \
42-
if [ $$? -ne 0 ]; then \
43-
echo "Error: Failed to copy RPM file to temporary directory"; \
44-
rm -rf "$${tmpdir}"; \
45-
exit 1; \
46-
fi; \
47-
cd packages/kernel-$(KVER) && \
48-
docker run --rm \
49-
-v "$(PWD)/packages/kernel-$(KVER)/":/kernel-package \
50-
-v "$${tmpdir}":/work \
51-
-v "$(PWD)/packages/microcode/":/microcode \
52-
-v "$(PWD)/tools/latest-kernel-full-config.sh":/latest-kernel-full-config.sh \
53-
--user "$$(id -u):$$(id -g)" \
54-
--name "kernel-$(KVER)-inner-full" \
55-
"$(SDK_IMAGE)" \
56-
bash -c "source /latest-kernel-full-config.sh && inner_full_config"; \
57-
docker_exit_code=$$?; \
58-
rm -rf "$${tmpdir}"; \
59-
if [ $$docker_exit_code -ne 0 ]; then \
60-
echo "Error: Docker container execution failed with exit code $$docker_exit_code"; \
61-
exit $$docker_exit_code; \
62-
fi; \
63-
echo "Successfully merged kernel configs for version $(KVER)"
64-
6525
full-config:
66-
@if [ -z "$(RPM_FILE)" ]; then \
67-
echo "Error: RPM_FILE parameter is required"; \
68-
echo "Usage: make full-config RPM_FILE=/path/to/kernel-source.rpm"; \
69-
exit 1; \
70-
fi; \
71-
echo "Checking kernel configuration dependencies..."; \
72-
source $(KERNEL_CONFIG_SCRIPT) && check_dependencies; \
73-
echo "All dependencies are available."; \
74-
rpm_file=$$(realpath "$(RPM_FILE)"); \
75-
if [ ! -f "$${rpm_file}" ]; then \
76-
echo "Error: RPM file not found: $${rpm_file}"; \
77-
exit 1; \
78-
fi; \
79-
sdk_image=$$(source $(KERNEL_CONFIG_SCRIPT) && resolve_bottlerocket_sdk); \
80-
kver=$$(rpm --query --nosignature --queryformat '%{VERSION}' "$${rpm_file}" | sed 's/\.[^.]*$$//'); \
81-
echo "Processing kernel version: $${kver}"; \
82-
echo "Using SDK image: $${sdk_image}"; \
83-
echo "Using RPM file: $${rpm_file}"; \
84-
$(MAKE) merge-kernel-configs RPM_FILE="$${rpm_file}" SDK_IMAGE="$${sdk_image}" KVER="$${kver}"
26+
./tools/docker-run.sh "/bottlerocket-kernel-kit/tools/latest-kernel-full-config.sh"
8527

8628
prep:
8729
@mkdir -p $(TWOLITER_DIR)
@@ -119,4 +61,4 @@ endif
11961
twoliter: prep
12062
@$(TWOLITER_MAKE) $(TWOLITER_MAKE_ARGS)
12163

122-
.PHONY: prep update fetch build publish twoliter merge-kernel-configs full-config
64+
.PHONY: prep update fetch build publish twoliter full-config

tools/docker-run.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
set -e -o pipefail
3+
4+
5+
bail() {
6+
if [[ $# -gt 0 ]]; then
7+
>&2 echo "Error: $*"
8+
fi
9+
exit 1
10+
}
11+
12+
SCRIPT_PATH="$1"
13+
14+
source="$(tq -r ".sdk.source" -f Twoliter.lock)"
15+
version="$(tq -r ".sdk.version" -f Twoliter.lock)"
16+
_name="$(tq -r ".sdk.name" -f Twoliter.lock)"
17+
_registry="${source%/*}"
18+
19+
registry="$(tq -r ".bottlerocket.bottlerocket-sdk.registry" -f Twoliter.override 2>/dev/null || echo "$_registry")"
20+
name="$(tq -r ".bottlerocket.bottlerocket-sdk.name" -f Twoliter.override 2>/dev/null || echo "$_name")"
21+
22+
SDK="${registry}/${name}:v${version}"
23+
24+
docker run --rm \
25+
-v "$(pwd):/bottlerocket-kernel-kit" \
26+
--user "$(id -u):$(id -g)" \
27+
"${SDK}" \
28+
bash "${SCRIPT_PATH}"
29+

tools/latest-kernel-full-config.sh

Lines changed: 116 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,90 +2,66 @@
22

33
set -e -o pipefail
44

5-
# Common error handling
6-
bail() {
7-
if [[ $# -gt 0 ]]; then
8-
>&2 echo "Error: $*"
9-
fi
10-
exit 1
11-
}
5+
KERNEL_KIT_DIR="/bottlerocket-kernel-kit"
6+
MICROCODE_DIR="${KERNEL_KIT_DIR}/packages/microcode"
127

13-
# Function to display usage information
8+
# Usage information
149
usage() {
1510
cat << EOF
16-
Usage: $(basename "$0") -r RPM_FILE
11+
Usage: $0
1712
18-
Extract kernel configurations from an RPM, merge with Bottlerocket's, and write out to a file, per architecture (x86_64 and aarch64).
13+
This script generates and validates full kernel configurations for all available
14+
kernel versions in the Bottlerocket kernel kit.
1915
20-
This script provides functions intended to be run inside the bottlerocket-sdk container.
21-
For typical usage, run 'make full-config RPM_FILE=/path/to/kernel-source.rpm' from the top-level bottlerocket-kernel-kit directory.
16+
IMPORTANT: This script is designed to run inside the Bottlerocket SDK container
17+
and should typically be invoked via the Makefile target:
2218
23-
Direct script usage (for development/debugging):
24-
-r RPM_FILE Path to RPM file
25-
-h Display this help message
19+
make full-config
2620
27-
Dependencies (when running directly):
28-
- docker
29-
- rpm2cpio
30-
- cpio
31-
- tar
32-
- tq
21+
Running this script directly outside the SDK container will fail because it
22+
requires the proper build environment and mounted paths.
3323
34-
Note: The inner_full_config() function is designed to run within the bottlerocket-sdk container environment
35-
and expects specific mount points and toolchain paths to be available.
36-
EOF
37-
}
24+
The script will:
25+
1. Discover all kernel-* packages with available RPMs
26+
2. Extract and patch kernel sources
27+
3. Merge Bottlerocket-specific configurations
28+
4. Generate full configuration files
29+
5. Validate that all required configs are present
3830
39-
usage_error() {
40-
>&2 usage
41-
bail "$1"
31+
EOF
4232
}
4333

44-
check_dependencies() {
45-
hash rpm2cpio || usage_error "DEPENDENCY ERROR: Please install rpm2cpio somewhere in your PATH"
46-
hash cpio || usage_error "DEPENDENCY ERROR: Please install cpio somewhere in your PATH"
47-
hash tar || usage_error "DEPENDENCY ERROR: Please install tar somewhere in your PATH"
48-
hash docker || usage_error "DEPENDENCY ERROR: Please install docker somewhere in your PATH"
49-
hash tq || usage_error "DEPENDENCY ERROR: Please cargo install tomlq somewhere in your PATH"
34+
# Common error handling
35+
bail() {
36+
if [[ $# -gt 0 ]]; then
37+
>&2 echo "Error: $*"
38+
fi
39+
exit 1
5040
}
5141

52-
# Get the SDK version from workspace Twoliter.toml and Twoliter.override, if provided.
53-
# Assumes running in the top level of the project and `tq` on $PATH.
54-
resolve_bottlerocket_sdk() {
55-
# Inspect Twoliter.lock file for [sdk] section source
56-
source="$(tq -r ".sdk.source" -f Twoliter.lock)"
57-
version="$(tq -r ".sdk.version" -f Twoliter.lock)"
58-
_name="$(tq -r ".sdk.name" -f Twoliter.lock)"
59-
# Trim from last slash, e.g. public.ecr.aws/bottlerocket/bottlerocket-sdk:v0.61.0 -> public.ecr.aws/bottlerocket
60-
_registry="${source%/*}"
61-
62-
# Check Twoliter.override to get the registry. For simplicity, assume overrides are only against named project named bottlerocket-sdk
63-
registry="$(tq -r ".bottlerocket.bottlerocket-sdk.registry" -f Twoliter.override 2>/dev/null || echo "$_registry")"
64-
name="$(tq -r ".bottlerocket.bottlerocket-sdk.name" -f Twoliter.override 2>/dev/null || echo "$_name")"
65-
66-
# Form the final SDK
67-
echo "${registry}/${name}:v${version}"
68-
}
42+
# Function to merge kernel configurations for a specific kernel version
43+
merge_kernel_configs() {
44+
local version="$1"
45+
local majorminor="$2"
46+
local tmpdir="$3"
6947

70-
# Function to perform the kernel configuration merging inside Docker container
71-
inner_full_config() {
72-
pushd /work || bail "Unable to enter /work (bind mount to tmp dir)"
48+
local kernel_package_dir="${KERNEL_KIT_DIR}/packages/kernel-${majorminor}"
7349

74-
version="$(rpm --query --nosignature --queryformat '%{VERSION}' kernel-source.rpm)"
75-
majorminor=${version%.*} # Trim after last '.', e.g. 6.1.132 -> 6.1
7650
if [ "${majorminor}" == "6.12" ]; then
7751
# kernel 6.12 has a patch file that is not applied to the kernel sources, so
7852
# only pick up the 1000-series kernel patches for our purposes here.
79-
readarray -t br_patches < <(find /kernel-package -maxdepth 1 -name "10*.patch")
53+
readarray -t br_patches < <(find "${kernel_package_dir}" -maxdepth 1 -name "10*.patch")
8054
spec_file="kernel6.12.spec"
8155
microcode_file="config-microcode-6-12"
8256
else
83-
readarray -t br_patches < <(find /kernel-package -maxdepth 1 -name "*.patch")
57+
readarray -t br_patches < <(find "${kernel_package_dir}" -maxdepth 1 -name "*.patch")
8458
spec_file="kernel.spec"
8559
microcode_file="config-microcode"
8660
fi
8761

88-
kernel_path=/kernel-package
62+
local kernel_path="${kernel_package_dir}"
63+
64+
pushd "${tmpdir}" || bail "Unable to enter temporary directory"
8965

9066
rpm2cpio kernel-source.rpm | cpio -iu {,./}linux-"${version}".tar{,.xz} {,./}config-x86_64 {,./}config-aarch64 {,./}"*.patch" {,./}"${spec_file}"
9167

@@ -116,13 +92,9 @@ inner_full_config() {
11692
popd || bail "Could not move around - 'popd' back to /work failed. Lets stop before we break anything further."
11793

11894
for arch in "x86_64" "aarch64"; do
119-
120-
export CROSS_COMPILE=/usr/bin/${arch}-bottlerocket-linux-gnu-
121-
export KCONFIG_CONFIG=bottlerocket_${arch}_defconfig
122-
12395
br_cfg="${kernel_path}/config-bottlerocket"
12496
br_cfg_arch="${kernel_path}/config-bottlerocket-${arch}"
125-
microcode_cfg="/microcode/${microcode_file}"
97+
microcode_cfg="${MICROCODE_DIR}/${microcode_file}"
12698

12799
pushd "linux-${version}" || bail "Could not move into linux-${version}"
128100

@@ -134,25 +106,24 @@ inner_full_config() {
134106
script_args=("../config-${arch}" "${microcode_cfg}" "${br_cfg}" "${br_cfg_arch}")
135107
fi
136108

137-
ARCH=${karch} ./scripts/kconfig/merge_config.sh "${script_args[@]}"
109+
ARCH=${karch} \
110+
CROSS_COMPILE=/usr/bin/${arch}-bottlerocket-linux-gnu- \
111+
KCONFIG_CONFIG=bottlerocket_${arch}_defconfig \
112+
./scripts/kconfig/merge_config.sh "${script_args[@]}"
113+
138114
mv -f "bottlerocket_${arch}_defconfig" "${kernel_path}/config-full-bottlerocket-${arch}" || bail "Failed to create config-full-bottlerocket-${arch}"
139115
popd || bail "Could not move around - 'popd' failed in merge_config loop. Lets stop before we break anything further."
140116
done
141117

142-
# Validate the generated configurations
143-
echo "Validating generated kernel configurations..."
144-
validate_kernel_configs
118+
popd || bail "Could not return from temporary directory"
145119
}
146120

147121
# Function to validate kernel configurations (similar to validate_config.sh)
148122
validate_kernel_configs() {
123+
local version="$1"
124+
local majorminor="$2"
149125
local errors=0
150-
local kernel_path=/kernel-package
151-
152-
# Extract kernel version for validation
153-
local version
154-
version="$(rpm --query --nosignature --queryformat '%{VERSION}' kernel-source.rpm)"
155-
local majorminor=${version%.*} # Trim after last '.', e.g. 6.1.132 -> 6.1
126+
local kernel_path="${KERNEL_KIT_DIR}/packages/kernel-${majorminor}"
156127

157128
for arch in x86_64 aarch64; do
158129
echo "=== Validating kernel-${majorminor} ${arch} ==="
@@ -213,3 +184,74 @@ validate_kernel_configs() {
213184
bail "Kernel configuration validation failed"
214185
fi
215186
}
187+
188+
# Parse command line arguments
189+
while [[ $# -gt 0 ]]; do
190+
case $1 in
191+
-h|--help)
192+
usage
193+
exit 0
194+
;;
195+
*)
196+
echo "Unknown option: $1"
197+
usage
198+
exit 1
199+
;;
200+
esac
201+
shift
202+
done
203+
204+
# Ensure this script is run within the Bottlerocket SDK container
205+
if [[ ! -d "${KERNEL_KIT_DIR}" ]]; then
206+
usage
207+
bail
208+
fi
209+
210+
# In kernel kit automation, CodePipeline updates each kernel separately.
211+
# This script processes only kernels that have available RPM files,
212+
# skipping any kernel packages without corresponding source RPMs.
213+
kernels_to_process=()
214+
for kernel_dir in "${KERNEL_KIT_DIR}/packages"/kernel-*; do
215+
if [[ -d "${kernel_dir}" ]]; then
216+
kernel_pkg=$(basename "${kernel_dir}")
217+
# In CodePipeline, only one RPM exists per kernel package, but during development
218+
# multiple RPMs can coexist. Use version sorting to select the latest RPM.
219+
# Sorting rules: https://www.gnu.org/software/coreutils/manual/html_node/sort-invocation.html
220+
rpm_file=$(find "${kernel_dir}" -name "kernel*.src.rpm" | sort -V | tail -1)
221+
if [[ -n "${rpm_file}" ]]; then
222+
kernels_to_process+=("${kernel_pkg}")
223+
echo "Found RPM for ${kernel_pkg}: $(basename "${rpm_file}")"
224+
else
225+
echo "No RPM found for ${kernel_pkg}, skipping"
226+
fi
227+
else
228+
bail "No Kernel directory found for ${kernel_dir}"
229+
fi
230+
done
231+
232+
if [[ ${#kernels_to_process[@]} -eq 0 ]]; then
233+
bail "No kernel RPMs found in any package directory"
234+
fi
235+
236+
# Process available kernels
237+
for kernel_pkg in "${kernels_to_process[@]}"; do
238+
echo "Processing ${kernel_pkg}..."
239+
240+
tmpdir=$(mktemp -d)
241+
242+
pushd "${tmpdir}" || bail "Unable to enter temporary directory"
243+
244+
rpm_file=$(find "${KERNEL_KIT_DIR}/packages/${kernel_pkg}" -name "kernel*.src.rpm" | head -1)
245+
246+
cp "${rpm_file}" kernel-source.rpm
247+
248+
version="$(rpm --query --nosignature --queryformat '%{VERSION}' kernel-source.rpm)"
249+
majorminor=${version%.*} # Trim after last '.', e.g. 6.1.132 -> 6.1
250+
251+
merge_kernel_configs "${version}" "${majorminor}" "${tmpdir}"
252+
253+
echo "Validating ${kernel_pkg} configurations..."
254+
validate_kernel_configs "${version}" "${majorminor}" || bail "Validation failed for ${kernel_pkg}"
255+
256+
popd || bail "Could not return from temporary directory"
257+
done

0 commit comments

Comments
 (0)