forked from bottlerocket-os/bottlerocket-core-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff-kernel-config
executable file
·267 lines (207 loc) · 8.63 KB
/
diff-kernel-config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env bash
#
# Common error handling
#
exit_trap_cmds=()
on_exit() {
exit_trap_cmds+=( "$1" )
}
run_exit_trap_cmds() {
for cmd in "${exit_trap_cmds[@]}"; do
eval "${cmd}"
done
}
trap run_exit_trap_cmds EXIT
warn() {
>&2 echo "Warning: $*"
}
bail() {
if [[ $# -gt 0 ]]; then
>&2 echo "Error: $*"
fi
exit 1
}
usage() {
cat <<EOF
Usage: $0 -b GITREV_BEFORE -a GITREV_AFTER -o OUTPUT_DIR [-v VARIANT] [-r] [-h]
Compare kernel configurations before and after a series of commits.
-a, --after new Git revision to compare from
-b, --before baseline Git revision to compare against
-k, --kernel the kernel versions
-o, --output-dir path to the output directory; must not exist yet
-r, --resume resume work on a previous invocation; check which parts
already exist in OUTPUT_DIR and skip builds accordingly
-h, --help show this help text
Example invocation:
This compares the config changes for kernels 5.10 (through metal-k8s-1.23)
and 5.15 (through metal-k8s-1.26) before and after the most recent commit
has been applied:
$0 -b HEAD^ -a HEAD -k 5.10 -k 5.15 -o configs
Notes:
This compares the config changes for all combinations of aarch64/x86_64,
and kernel versions. Since this involves numerous full kernel builds the
comparison will take some time. Consider the working tree this is invoked
on busy while the script is running.
EOF
}
usage_error() {
>&2 usage
bail "$1"
}
#
# Parse arguments
#
kernel_versions=()
while [[ $# -gt 0 ]]; do
case $1 in
-a|--after)
shift; gitrev_after_arg=$1 ;;
-b|--before)
shift; gitrev_before_arg=$1 ;;
-k|--kernel)
shift; kernel_versions+=( "$1" ) ;;
-o|--output-dir)
shift; output_dir=$1 ;;
-r|--resume)
shift; resume=1 ;;
-h|--help)
usage; exit 0 ;;
*)
usage_error "Invalid option '$1'" ;;
esac
shift
done
[[ -n ${output_dir} ]] || usage_error 'require -o|--output-dir'
[[ -e ${output_dir} && ! -v resume ]] && bail "Output directory '${output_dir}' exists already, not touching it"
readonly output_dir
# Validate and resolve the given before and after Git revisions. Resolving
# them now prevents relative references from moving around after the first
# checkout.
[[ -n ${gitrev_before_arg} ]] || usage_error 'require -b|--before'
[[ -n ${gitrev_after_arg} ]] || usage_error 'require -a|--after'
[[ -n ${kernel_versions[*]} ]] || usage_error 'require -k|--kernel'
gitrev_before=$(git rev-parse --verify --end-of-options "${gitrev_before_arg}^{commit}")
gitrev_after=$(git rev-parse --verify --end-of-options "${gitrev_after_arg}^{commit}")
[[ -n ${gitrev_before} ]] || bail "Invalid Git revision '${gitrev_before_arg}'"
[[ -n ${gitrev_after} ]] || bail "Invalid Git revision '${gitrev_after_arg}'"
readonly gitrev_before
readonly gitrev_after
#
# Prepare working tree
#
# We'll check out the before and after states to compare. For that the working
# tree and the index need to be clean.
if [[ -n $(git status --porcelain --untracked-files=no) ]]; then
bail 'The working tree or index of the repository are not clean. ' \
'Consider running "git stash" to temporarily stow away your changes.'
fi
# Restore current repository state whenever we exit (either a checked out
# branch or the current detached head state).
gitrev_original=$(git rev-parse --abbrev-ref HEAD)
if [[ -z ${gitrev_original} ]]; then
gitrev_original=$(git rev-parse HEAD) || bail 'Cannot determine current repository HEAD.'
fi
readonly gitrev_original
on_exit "git checkout --quiet '${gitrev_original}'"
#
# Iterate over all viable build configurations in before and after states
#
mkdir -p "${output_dir}" || bail "Failed to create output directory '${output_dir}'"
for state in after before; do
gitrev_var=gitrev_${state}
git checkout --quiet "${!gitrev_var}" || bail "Cannot check out '${!gitrev_var}'."
for kver in "${kernel_versions[@]}"; do
arches=( aarch64 x86_64 )
for arch in "${arches[@]}"; do
config_path=${output_dir}/config-${arch}-${kver}-${state}
if [[ -v resume && -e ${config_path} ]]; then
echo "${config_path} already exists, skipping"
continue
fi
debug_id="state=${state} arch=${arch} kernel=${kver}"
#
# Run build
#
BUILDSYS_ARCH="${arch}" PACKAGE="kernel-${kver/./_}" make twoliter build-package \
|| bail "Build failed for ${debug_id}"
#
# Find kernel RPM
#
shopt -s nullglob
kernel_rpms=(
./build/rpms/kernel-"${kver}"/bottlerocket-kernel-"${kver}"-"${kver}".*.*.*.br1."${arch}".rpm
)
shopt -u nullglob
case ${#kernel_rpms[@]} in
0) bail "No kernel RPM found for ${debug_id}" ;;
1) kernel_rpm=${kernel_rpms[0]} ;;
*)
# shellcheck disable=SC2012 # find(1) cannot sort by mtime
kernel_rpm=$(ls -1t "${kernel_rpms[@]}" | head -n 1)
warn "More than one kernel RPM found for ${debug_id}. Choosing '${kernel_rpm}' as the latest build."
;;
esac
kver_full=$(rpm --query --queryformat '%{VERSION}-%{RELEASE}' "${kernel_rpm}")
#
# Extract kernel config
#
rpm2cpio "${kernel_rpm}" \
| cpio --quiet --extract --to-stdout ./boot/config >"${config_path}"
[[ -s "${config_path}" ]] || bail "Failed to extract config for ${debug_id}"
echo "config-${arch}-${kver}-${state} -> ${kver_full}" >> "${output_dir}"/kver_mapping
done # arch
done # kernel
done # state
#
# Post-process the collected pairs of "before" and "after" configs (generate diffs, a report, a summary)
#
# Get the helpful diffconfig script from the kernel source tree. We package it
# in the kernel-archive RPM from where it can be extracted. Here we extract the
# latest version of the script, but any kernel version and arch will do.
latest_kver=$(printf '%s\n' "${kernel_versions[@]}" | sort -V | tail -n1)
latest_archive_rpms=( ./build/rpms/kernel-"${latest_kver}"/bottlerocket-kernel-"${latest_kver}"-archive-*.rpm )
diffconfig=$(mktemp --suffix -bottlerocket-diffconfig)
on_exit "rm '${diffconfig}'"
rpm2cpio "${latest_archive_rpms[0]}" \
| cpio --quiet --extract --to-stdout \
| tar --xz --extract --to-stdout kernel-devel/scripts/diffconfig >"${diffconfig}"
[[ -s ${diffconfig} ]] || bail "Failed to extract diffconfig tool from '${latest_archive_rpms[0]}'."
chmod +x "${diffconfig}"
# Diff the before and after states for each collected pair
for config_before in "${output_dir}"/config-*-before; do
config_after=${config_before/before/after}
config_diff=${config_before/before/diff}
"${diffconfig}" "${config_before}" "${config_after}" >"${config_diff}" \
|| bail "Failed to diff '${config_before}' and '${config_after}'"
done
# Generate diff summary
echo
for config_diff in "${output_dir}"/config-*-diff; do
config_base=${config_diff##*/}
awk "
/^-/ { removed += 1 }
/^+/ { added += 1 }
/ -> / { changed += 1 }
END { printf \"${config_base}:\t%3d removed, %3d added, %3d changed\n\", removed, added, changed }
" "${config_diff}"
done | sort -V | tee "${output_dir}"/diff-summary
echo
# Generate combined report of changes
head -v -n 999999 "${output_dir}"/*-diff >"${output_dir}"/diff-report
echo "A full report has been placed in '${output_dir}/diff-report'"
# Generate combined report in tabular form (csv)
echo "config change" > "${output_dir}"/diff-table
cat "${output_dir}"/*-diff | sort | uniq >> "${output_dir}"/diff-table
for config_diff in "${output_dir}"/config-*-diff; do
kernel_version=$(echo "${config_diff}" | sed -e "s%^${output_dir}/config-%%" -e "s%-diff$%%")
kver_before=$(grep "${kernel_version}-before" "${output_dir}/kver_mapping" | cut -d ' ' -f 3)
kver_after=$(grep "${kernel_version}-after" "${output_dir}/kver_mapping" | cut -d ' ' -f 3)
col_name="${kernel_version} (${kver_before} -> ${kver_after})"
sed -i "s/$/,/" "${output_dir}"/diff-table
sed -i "/^config change/ s/$/${col_name}/" "${output_dir}"/diff-table
mapfile -t diff_lines < "${config_diff}"
for line in "${diff_lines[@]}"; do
sed -i "/^${line}/ s/$/x/" "${output_dir}"/diff-table
done
done
echo "A tabular report in csv-format has been placed in '${output_dir}/diff-table'"