-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathquiz-lib
More file actions
301 lines (274 loc) · 8.15 KB
/
quiz-lib
File metadata and controls
301 lines (274 loc) · 8.15 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env bash
# SPDX-License-Identifier: MPL-2.0
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2023-2025, Rob Norris <robn@despairlabs.com>
#
# Linux source version numbers (the tarball version) don't always match their
# installed version number (module dir version, uname -a output), eg:
#
# source installed
# 6.9-rc1 6.9.0-rc1
# 6.9 6.9.0
# 6.9.1 6.9.1
#
# we allow any form. internally, we'll set up variables for the "version"
# (installed) and "source version" (source), and also vars for all the pieces
#
# separate function for the pre-cooked parse; used by quiz-kernel
# to request "latest in series"
function _quiz_kernel_vars () {
local kver=$1
_quiz_kver=""
_quiz_ksrcver=""
_quiz_kmajor=""
_quiz_kminor=""
_quiz_kpatch=""
_quiz_ktype=""
_quiz_ktype_patch=""
if [[ ${kver%%-*} = "next" ]] ; then
_quiz_kmajor=0
_quiz_kminor=0
_quiz_kpatch=0
_quiz_ktype=next
_quiz_ktype_patch=${kver##next-}
elif [[ $kver = "master" ]] ; then
_quiz_kmajor=0
_quiz_kminor=0
_quiz_kpatch=0
_quiz_ktype=master
_quiz_ktype_patch=
else
shopt -s lastpipe
eval $(echo $kver | perl -nE '
($maj, $min, $pat, $ty, $typat) = m/
^
([1-9][0-9]*) # major
\. # dot sep
(0|[1-9][0-9]*) # minor
(?:\.(0|[1-9][0-9]*))? # patch 1+, optional
(?:
-(rc|next|master) # type
(?:-?(
(?:[0-9a-f]+\+) # typpatch: git ref
|
(?:[1-9][0-9]*) # typpatch: number
)?)
)?
$
/gx;
print <<~EOF;
_quiz_kmajor=$maj
_quiz_kminor=$min
_quiz_kpatch=$pat
_quiz_ktype=$ty
_quiz_ktype_patch=$typat
EOF
')
fi
if [[ -n "${QUIZ_DEBUG_VARS_PARSE:-}" ]] ; then
cat 2>&1 <<EOF
DEBUG: exploded kernel version (parse): $kver
_quiz_kmajor: $_quiz_kmajor
_quiz_kminor: $_quiz_kminor
_quiz_kpatch: $_quiz_kpatch
_quiz_ktype: $_quiz_ktype
_quiz_ktype_patch: $_quiz_ktype_patch
EOF
fi
return 0
}
function quiz_kernel_vars () {
local kver="${1:-}"
if [[ -z $kver ]] ; then
fail "no kernel version set. use -k or set QUIZ_KERNEL_VERSION"
fi
_quiz_kernel_vars $kver
if [[ -z $_quiz_kmajor || -z $_quiz_kminor ]] ; then
fail "'$kver' is not a valid kernel version"
fi
if [[ -z $_quiz_kpatch ]] ; then
_quiz_kpatch="0"
fi
if [[ -z $_quiz_ktype ]] ; then
_quiz_ktype='release'
_quiz_ksrcver=$_quiz_kmajor.$_quiz_kminor
_quiz_kver=$_quiz_ksrcver
if [[ "$_quiz_kpatch" = "0" ]] ; then
_quiz_kver="$_quiz_kver.0"
else
_quiz_kver="$_quiz_kver.$_quiz_kpatch"
_quiz_ksrcver="$_quiz_ksrcver.$_quiz_kpatch"
fi
elif [[ $_quiz_ktype == "rc" ]] ; then
_quiz_kver=$_quiz_kmajor.$_quiz_kminor.0-$_quiz_ktype$_quiz_ktype_patch
_quiz_ksrcver=$_quiz_kmajor.$_quiz_kminor-$_quiz_ktype$_quiz_ktype_patch
elif [[ $_quiz_ktype == "next" ]] ; then
_quiz_kver=$_quiz_kmajor.$_quiz_kminor.0-$_quiz_ktype-$_quiz_ktype_patch
_quiz_ksrcver=$_quiz_kmajor.$_quiz_kminor-$_quiz_ktype-$_quiz_ktype_patch
elif [[ $_quiz_ktype == "master" ]] ; then
if [[ -n $_quiz_ktype_patch ]] ; then
_quiz_kver=$_quiz_kmajor.$_quiz_kminor.0-$_quiz_ktype-$_quiz_ktype_patch
_quiz_ksrcver=$_quiz_kmajor.$_quiz_kminor-$_quiz_ktype-$_quiz_ktype_patch
else
_quiz_kver=$_quiz_kmajor.$_quiz_kminor.0-$_quiz_ktype
_quiz_ksrcver=$_quiz_kmajor.$_quiz_kminor-$_quiz_ktype
fi
fi
if [[ -n "${QUIZ_DEBUG_VARS:-}" ]] ; then
cat 2>&1 <<EOF
DEBUG: exploded kernel version: $kver
_quiz_kmajor: $_quiz_kmajor
_quiz_kminor: $_quiz_kminor
_quiz_kpatch: $_quiz_kpatch
_quiz_ktype: $_quiz_ktype
_quiz_ktype_patch: $_quiz_ktype_patch
_quiz_kver: $_quiz_kver
_quiz_ksrcver: $_quiz_ksrcver
EOF
fi
}
function quiz_kernel_best_available () {
local kver="${1:-}"
if [[ -z $kver ]] ; then
fail "no kernel version set. use -k or set QUIZ_KERNEL_VERSION"
fi
local kernel_dir=$RUNDIR/system/kernel
local kernel_slug=quiz-kernel-$_quiz_arch_kernel
_quiz_kernel_dir=""
_quiz_kernel_image=""
quiz_kernel_vars $kver
if [[ $kver = $_quiz_kver ]] ; then
# canonical kernel version specified, must use only that version
local dir=$kernel_dir/$kernel_slug-$_quiz_kver
local image=$dir/kernel-$_quiz_kver
if [[ ! -e $image ]] ; then
fail "kernel image not found: $_quiz_kver"
fi
_quiz_kernel_dir=$dir
_quiz_kernel_image=$image
else
# partial version specified, choose from best available
local matchtype=$_quiz_ktype
local searchpat
if [[ $_quiz_ktype = "next" ]] ; then
searchpat="$kernel_slug-*-$_quiz_ktype-$_quiz_ktype_patch"
else
searchpat="$kernel_slug-$_quiz_ksrcver.*"
fi
local candidates="$(find $kernel_dir -maxdepth 1 -name $searchpat | cut -f4- -d- | sort -V)"
local best=""
for candidate in $candidates ; do
quiz_kernel_vars $candidate
if [[ $_quiz_ktype != $matchtype ]] ; then
trace "skipped non-release kernel image: $_quiz_kver"
else
best="$_quiz_kver"
trace "matched kernel image: $_quiz_kver"
fi
done
if [[ -z $best ]] ; then
fail "matching kernel image not found: $kver"
fi
quiz_kernel_vars $best
_quiz_kernel_dir=$kernel_dir/$kernel_slug-$_quiz_kver
_quiz_kernel_image=$_quiz_kernel_dir/kernel-$_quiz_kver
fi
trace "using kernel: $_quiz_kver"
}
function quiz_arch_vars () {
local arch="${1:-}"
if [[ -z $arch ]] ; then
fail "no architecture set. use -a or set QUIZ_ARCH"
fi
# defaults
_quiz_arch=$arch
_quiz_arch_kernel=""
_quiz_arch_qemu=$arch
_quiz_arch_prefix=$arch-linux-gnu
_quiz_arch_native=""
case "$arch" in
x86_64)
_quiz_arch_kernel=x86
;;
aarch64)
_quiz_arch_kernel=arm64
;;
ppc64)
_quiz_arch_kernel=powerpc
;;
riscv64)
_quiz_arch_kernel=riscv
;;
loongarch64)
_quiz_arch_kernel=loongarch
;;
*)
fail "no support for architecture: $arch"
;;
esac
if [[ $(uname -m) == $_quiz_arch ]] ; then
_quiz_arch_native=1
fi
if [[ -n "${QUIZ_DEBUG_VARS:-}" ]] ; then
cat 2>&1 <<EOF
DEBUG: exploded arch vars: $arch
_quiz_arch: $_quiz_arch
_quiz_arch_kernel: $_quiz_arch_kernel
_quiz_arch_qemu: $_quiz_arch_qemu
_quiz_arch_prefix: $_quiz_arch_prefix
_quiz_arch_native: $_quiz_arch_native
EOF
fi
trace "using arch: $_quiz_arch"
}
#
# XXX this are actually more like arch vars, but I'm still not sure where
# debian sits in the whole structure, so for now I'm just keeping it
# contained -- robn, 2025-01-10
#
function quiz_debian_vars () {
if [[ -z "${_quiz_arch:-}" ]] ; then
fail "internal error: call quiz_arch_vars() first"
fi
# defaults for supported architectures
_quiz_debian_arch=""
_quiz_debian_release=trixie
_quiz_debian_mirror="https://deb.debian.org/debian"
case "$_quiz_arch" in
x86_64)
_quiz_debian_arch=amd64
;;
aarch64)
_quiz_debian_arch=arm64
;;
riscv64)
_quiz_debian_arch=riscv64
;;
ppc64)
_quiz_debian_arch=ppc64
_quiz_debian_release=unstable
_quiz_debian_mirror="[trusted=yes] https://deb.debian.org/debian-ports"
;;
loongarch64)
_quiz_debian_arch=loong64
_quiz_debian_release=unstable
_quiz_debian_mirror="[trusted=yes] https://deb.debian.org/debian-ports"
;;
*)
fail "internal error: debian vars for arch $_quiz_arch not defined"
;;
esac
if [[ -n "${QUIZ_DEBUG_VARS:-}" ]] ; then
cat 2>&1 <<EOF
DEBUG: exploded debian vars for arch: $_quiz_arch
_quiz_debian_arch: $_quiz_debian_arch
_quiz_debian_release: $_quiz_debian_release
_quiz_debian_mirror: $_quiz_debian_mirror
EOF
fi
}
# vim: ft=bash