-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquiz-lib
229 lines (203 loc) · 6.25 KB
/
quiz-lib
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
#!/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 <[email protected]>
#
# 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=""
shopt -s lastpipe
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)-?([1-9][0-9]*))? # type & typepatch
$
/gx;
print qq{$maj $min $pat $ty $typat}
' | read _quiz_kmajor _quiz_kminor _quiz_kpatch _quiz_ktype _quiz_ktype_patch
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
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/$_quiz_arch_kernel
_quiz_kernel_image=""
quiz_kernel_vars $kver
if [[ $kver = $_quiz_kver ]] ; then
# canonical kernel version specified, must use only that version
local image=$kernel_dir/kernel-$_quiz_kver
if [[ ! -e $image ]] ; then
fail "kernel image not found: $_quiz_kver"
fi
_quiz_kernel_image=$image
else
# partial version specified, choose from best available
local candidates="$(find $kernel_dir -maxdepth 1 -name "kernel-$_quiz_ksrcver.*" | cut -f2- -d- | sort -V)"
local best=""
for candidate in $candidates ; do
quiz_kernel_vars $candidate
if [[ $_quiz_ktype != "release" ]] ; then
trace "skipped non-release kernel image: $_quiz_kver"
else
best="$_quiz_kver"
trace "matched release kernel image: $_quiz_kver"
fi
done
if [[ -z $best ]] ; then
fail "matching kernel image not found: $kver"
fi
quiz_kernel_vars $best
_quiz_kernel_image=$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
_quiz_arch=""
_quiz_arch_kernel=""
_quiz_arch_qemu=""
_quiz_arch_prefix=""
_quiz_arch_native=""
case "$arch" in
x86_64)
_quiz_arch=x86_64
_quiz_arch_kernel=x86
_quiz_arch_qemu=x86_64
_quiz_arch_prefix=x86_64-linux-gnu
;;
ppc64)
_quiz_arch=ppc64
_quiz_arch_kernel=powerpc
_quiz_arch_qemu=ppc64
_quiz_arch_prefix=powerpc64-linux-gnu
;;
*)
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
_quiz_debian_arch=""
_quiz_debian_release=""
_quiz_debian_mirror=""
case "$_quiz_arch" in
x86_64)
_quiz_debian_arch=amd64
_quiz_debian_release=bookworm
_quiz_debian_mirror="https://deb.debian.org/debian"
;;
ppc64)
_quiz_debian_arch=ppc64
_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