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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
#!/bin/sh
set -eu
: "${ROOT:=}"
: "${OS_RELEASE_PRIMARY:=@@OS_RELEASE_PRIMARY@@}"
: "${OS_RELEASE_FALLBACK:=@@OS_RELEASE_FALLBACK@@}"
: "${PROC_OSRELEASE:=@@PROC_OSRELEASE@@}"
progname=${0##*/}
error() {
printf '%s\n' "$progname: $*" >&2
exit 1
}
usage() {
printf '%s\n' "usage: $progname [-kru] [-j jail]" >&2
exit 1
}
root_path() {
case $1 in
/*)
if [ -n "$ROOT" ]; then
printf '%s%s\n' "$ROOT" "$1"
else
printf '%s\n' "$1"
fi
;;
*)
error "internal error: expected absolute path, got '$1'"
;;
esac
}
path_basename() {
path=$1
while [ "$path" != "/" ] && [ "${path%/}" != "$path" ]; do
path=${path%/}
done
printf '%s\n' "${path##*/}"
}
extract_release_from_name() {
case $1 in
vmlinuz-*)
printf '%s\n' "${1#vmlinuz-}"
return 0
;;
bzImage-*)
printf '%s\n' "${1#bzImage-}"
return 0
;;
kernel-*)
printf '%s\n' "${1#kernel-}"
return 0
;;
esac
return 1
}
append_unique_candidate() {
candidate=$1
[ -n "$candidate" ] || return 0
if [ -z "${kernel_candidates:-}" ]; then
kernel_candidates=$candidate
return 0
fi
if printf '%s\n' "$kernel_candidates" | grep -Fx "$candidate" >/dev/null 2>&1; then
return 0
fi
kernel_candidates=$kernel_candidates'
'$candidate
}
extract_release_from_link() {
link_path=$1
[ -L "$link_path" ] || return 1
target=$(readlink "$link_path" 2>/dev/null) || return 1
extract_release_from_name "$(path_basename "$target")"
}
collect_boot_kernel_candidates() {
for absolute in /vmlinuz /boot/vmlinuz; do
link_path=$(root_path "$absolute")
if release=$(extract_release_from_link "$link_path"); then
printf '%s\n' "$release"
return 0
fi
done
for absolute in /vmlinuz-* /boot/vmlinuz-* /boot/bzImage-* /boot/kernel-*; do
pattern=$(root_path "$absolute")
for entry in $pattern; do
[ -e "$entry" ] || continue
release=$(extract_release_from_name "$(path_basename "$entry")") || continue
append_unique_candidate "$release"
done
done
return 1
}
collect_module_tree_candidates() {
for absolute in /lib/modules /usr/lib/modules; do
modules_root=$(root_path "$absolute")
[ -d "$modules_root" ] || continue
for entry in "$modules_root"/*; do
[ -d "$entry" ] || continue
if [ ! -d "$entry/kernel" ] && [ ! -f "$entry/modules.dep" ]; then
continue
fi
append_unique_candidate "$(path_basename "$entry")"
done
done
}
format_candidate_list() {
printf '%s\n' "$kernel_candidates" | awk '
NF {
if (count > 0)
printf(", ");
printf("%s", $0);
count++;
}
END {
if (count > 0)
printf("\n");
}
'
}
installed_kernel_version() {
kernel_candidates=
if release=$(collect_boot_kernel_candidates); then
printf '%s\n' "$release"
return 0
fi
collect_module_tree_candidates
count=$(printf '%s\n' "${kernel_candidates:-}" | awk 'NF { count++ } END { print count + 0 }')
case $count in
0)
error "unable to determine installed kernel version under '${ROOT:-/}'"
;;
1)
printf '%s\n' "$kernel_candidates"
;;
*)
error "unable to determine installed kernel version under '${ROOT:-/}': multiple kernel releases found ($(format_candidate_list))"
;;
esac
}
running_kernel_version() {
if [ -r "$PROC_OSRELEASE" ]; then
release=
IFS= read -r release <"$PROC_OSRELEASE" || true
[ -n "$release" ] || error "unable to determine running kernel version from $PROC_OSRELEASE"
printf '%s\n' "$release"
return 0
fi
if command -v uname >/dev/null 2>&1; then
command uname -r
return 0
fi
error "unable to determine running kernel version: $PROC_OSRELEASE is unavailable"
}
extract_os_release_field() {
field=$1
file=$2
if output=$(awk -v want="$field" '
function ltrim(s) {
sub(/^[[:space:]]+/, "", s)
return s
}
function rtrim(s) {
sub(/[[:space:]]+$/, "", s)
return s
}
function decode_quoted(s, out, i, c, esc) {
out = ""
esc = 0
for (i = 2; i <= length(s); i++) {
c = substr(s, i, 1)
if (esc) {
out = out c
esc = 0
continue
}
if (c == "\\") {
esc = 1
continue
}
if (c == "\"") {
if (rtrim(substr(s, i + 1)) != "")
return "__TRAILING__"
return out
}
out = out c
}
return "__UNTERMINATED__"
}
BEGIN {
found = 0
parse_error = 0
}
/^[[:space:]]*#/ || /^[[:space:]]*$/ {
next
}
{
line = $0
sub(/^[[:space:]]+/, "", line)
if (line !~ /^[A-Z0-9_]+=.*$/)
next
key = line
sub(/=.*/, "", key)
if (key != want)
next
value = line
sub(/^[^=]*=/, "", value)
value = ltrim(value)
if (value ~ /^"/) {
value = decode_quoted(value)
if (value == "__TRAILING__" || value == "__UNTERMINATED__") {
parse_error = 1
exit
}
print value
found = 1
exit 0
}
if (value ~ /[[:space:]]/) {
parse_error = 1
exit
}
print rtrim(value)
found = 1
exit 0
}
END {
if (parse_error)
exit 3
if (!found)
exit 1
}
' "$file" 2>&1); then
status=0
else
status=$?
fi
case $status in
0)
[ -n "$output" ] || return 1
printf '%s\n' "$output"
return 0
;;
1)
return 1
;;
3)
return 2
;;
*)
return 3
;;
esac
}
userland_version() {
primary=$(root_path "$OS_RELEASE_PRIMARY")
fallback=$(root_path "$OS_RELEASE_FALLBACK")
for file in "$primary" "$fallback"; do
[ -r "$file" ] || continue
for field in VERSION_ID BUILD_ID VERSION PRETTY_NAME; do
if value=$(extract_os_release_field "$field" "$file"); then
printf '%s\n' "$value"
return 0
else
status=$?
case $status in
1)
;;
2)
error "malformed os-release entry '$field' in $file"
;;
*)
error "failed to parse $file"
;;
esac
fi
done
error "unable to determine userland version from $file"
done
error "unable to locate os-release under '${ROOT:-/}'"
}
main() {
opt_k=0
opt_r=0
opt_u=0
opt_j=0
OPTIND=1
while getopts "kruj:" option; do
case $option in
k)
opt_k=1
;;
r)
opt_r=1
;;
u)
opt_u=1
;;
j)
opt_j=1
;;
*)
usage
;;
esac
done
shift $((OPTIND - 1))
[ $# -eq 0 ] || usage
if [ $((opt_k + opt_r + opt_u + opt_j)) -eq 0 ]; then
opt_u=1
fi
if [ "$opt_j" -ne 0 ]; then
error "option -j is not supported on Linux"
fi
if [ "$opt_k" -ne 0 ]; then
installed_kernel_version
fi
if [ "$opt_r" -ne 0 ]; then
running_kernel_version
fi
if [ "$opt_u" -ne 0 ]; then
userland_version
fi
}
main "$@"
|