forked from openSUSE/suse-module-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver-check.sh
365 lines (328 loc) · 7.98 KB
/
driver-check.sh
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
#!/bin/bash
VERSION="0.6"
MAINTAINER="Martin Wilck <[email protected]>"
USAGE="Usage: ${0##*/} [-o|--out output-file]"
errors=0
warnings=0
trap 'rm -rf "$tmp"' EXIT
tmp=$(mktemp -d)
find_usrmerge_boot() {
local filename=$1
local kver=$2
local ext=${3:+."$3"}
local f
for f in "/usr/lib/modules/$kver/$filename$ext" "/boot/$filename-$kver$ext"
do
if [ -e "$f" ]; then
echo "$f"
return
fi
done
echo "WARNING: find_usrmerge_boot: $filename$ext not found for kernel $kver" >&2
}
find_depmod() {
local _d
[[ -x "$DEPMOD" ]] && return
DEPMOD=
for _d in /usr/sbin /sbin; do
if [[ -x ${_d}/depmod ]]; then
DEPMOD=${_d}/depmod
break;
fi
done
if [[ ! "$DEPMOD" ]]; then
echo "ERROR: depmod is not installed - aborting" >&2
exit 1
fi
}
rpm()
{
# rpm tends to send localized error messages to stdout :-(
LC_ALL=C command rpm "$@"
}
file_owner()
{
local f=$1
if (cd "$tmp/rpms"; grep -lFx "$f" *); then
return
fi
rpm -qf "$f"
}
_explain_called=()
explain()
{
local caller=${BASH_LINENO[0]}
if test -n "${_explain_called[$caller]}"; then
return
fi
_explain_called[$caller]=1
echo "$*"
}
error()
{
echo "ERROR: $*"
let errors++
}
warning()
{
echo "warning: $*" >&2
let warnings++
}
check_system()
{
if test ! -x /usr/lib/module-init-tools/weak-modules2; then
echo "This tool only works on SLE11 and later systems" >&2
exit 1
fi
if ! zypper search >/dev/null; then
echo "Cannot run zypper, please correct the above problem" >&2
exit 1
fi
}
check_rpm_V()
{
local attrs flags path
# kernel packages contain the initrd with permissions 0644,
# but dracut creates initrd with 0600. That's not an error.
while read attrs flags path; do
case $attrs in
.M.......)
if [[ "${path#/boot/initrd}" != "$path" && \
-f "$path" && \
$(stat -c %a "$path") = 600 ]]; then
continue
fi
;;
esac
echo "$attrs $flags $path"
error "$rpm was not installed correctly (see above)"
done
}
check_rpm()
{
local rpm=$1 name=${1%-*-*} out
# ignore changes to %config and %doc files and ignore changed mtimes
check_rpm_V < <(rpm -V "$rpm" | grep -Ev '^[^ ]{8,} [cd] |^\.{7}T\.* ')
}
check_kernel_package()
{
local kernel=$1
if ! rpm -q --qf '%{description}\n' "$kernel" | grep -q '^GIT '; then
error "$kernel does not look like a SUSE kernel package (no commit id)"
fi
if ! rpm -q --qf '%{postin}\n' "$kernel" | grep -Eq 'weak-modules2|kernel-scriptlets/rpm-post'; then
error "$kernel does not look like a SUSE kernel package (wrong %post script)"
fi
}
check_krel()
{
local krel=$1 system_map module_symvers msg res args bad=false
local mit_version
system_map=$(find_usrmerge_boot System.map "$krel")
module_symvers=$(find_usrmerge_boot symvers "$krel" gz)
if ! test -e "$system_map"; then
error "$system_map not found"
bad=true
fi
if ! test -e "$module_symvers"; then
error "$module_symvers not found"
bad=true
fi
if $bad; then
explain "Each kernel must install System.map and symvers.gz to be able to check module dependencies."
return
fi
set -- $("$DEPMOD" --version | sed -rn 's/.* ([0-9]+)(\.([0-9]+)(\..*)?)?/\1 \3/p')
if test -n "$1" -a -n "$2"; then
let "mit_version = $1 * 100 + $2"
elif test -n "$1" -a \! -n "$2" -a "$1" -gt 3; then
let "mit_version = $1 * 100"
else
warning "Cannot determine module-init-tools version, this is a bug in the script"
mit_version=0
fi
# depmod -E was introduced in 3.10
if test "$mit_version" -ge 310; then
gzip -cd <"$module_symvers" >"$tmp/symvers"
args=(-E "$tmp/symvers")
else
args=(-F "$system_map")
fi
msg=$("$DEPMOD" -n -e "${args[@]}" "$krel" 2>&1 >/dev/null)
res=$?
if test -n "$msg" -o "$res" -ne 0; then
echo "$msg"
error "depmod $krel returned errors (exit code $res)"
explain "depmod must pass without errors otherwise KMP scripts will break"
fi
}
req_re='^(kernel\([^:]*:kernel[[:alnum:]_]*\)|ksym\([^:]*:(struct_module|module_layout)\)) = [0-9a-f]+'
check_kmp()
{
local kmp=$1 prefix prev_krel krel path found_module=false
if ! rpm -q --qf '%{postin}\n' "$kmp" | grep -Eq 'weak-modules2|kernel-scriptlets/kmp-post'; then
error "$kmp does not look like a SUSE kernel module package (wrong %post)"
fi
if ! rpm -q -R "$kmp" | grep -Eq "$req_re"; then
error "$kmp does not have proper dependencies"
fi
exec 3< <(sed -rn 's:^(/lib/modules)?/([^/]*)/(.*\.ko(\.[gx]z|\.zst)?)$:\1 \2 \3:p' \
"$tmp/rpms/$kmp")
while read prefix krel path <&3; do
found_module=true
if test "$prefix" != "/lib/modules"; then
error "$kmp installs modules outside of /lib/modules"
continue
fi
if test -z "$prev_krel"; then
prev_krel=$krel
elif test "$prev_krel" != "$krel"; then
error "$kmp installs modules for multiple kernel versions"
fi
case "$path" in
updates/* | extra/*)
;;
weak-updates/*)
error "$kmp installs modules in weak-updates/ instead of updates/ or extra/"
explain "The weak-updates directory is reserved for automatically generated symlinks"
;;
*)
error "$kmp installs modules in an invalid directory"
explain \
"KMPs must install modules in the updates/ or extra/ subdirectories for the
weak-modules2 script to work"
;;
esac
done
if ! $found_module; then
error "$kmp does not contain any modules"
explain \
"A KMP must contain it's modules in the rpm filelist, otherwise weak-modules2
will not work"
fi
}
check_ko()
{
local ko=$1 kmp bad=false
case "$ko" in
*/weak-updates/*)
if test -L "$ko"; then
return
fi
esac
kmp=$(file_owner "$ko")
case "$kmp" in
kernel-* | *-kmp-*) ;;
*not\ owned\ by\ any\ package)
error "$ko is not owned by any package"
bad=true
;;
*)
error "$ko is not packaged as a KMP"
bad=true
;;
esac
if $bad; then
explain \
"External kernel modules must be packaged as KMPs, see
http://developer.novell.com/wiki/index.php/Kernel_Module_Packages_Manuals"
fi
}
options=$(getopt -n "${0##*/}" -o o:h --long out:,help -- "$@")
if test "$?" -ne 0; then
echo "$USAGE" >&2
exit 1
fi
eval set -- "$options"
logfile="driver-check-report.txt"
while :; do
case "$1" in
-o | --out)
logfile="$2"
shift 2
;;
-h | --help)
echo "${0##*/} $VERSION"
echo "$USAGE"
echo
echo "Please report bugs and enhancement requests to $MAINTAINER"
exit 0
;;
--)
shift
break
;;
esac
done
if test $# -gt 0; then
echo "Unrecognized arguments: $*" >&2
echo "$USAGE" >&2
exit 1
fi
find_depmod
check_system
# set up redirection
if test $logfile != "-"; then
if test -e "$logfile"; then
mv -f "$logfile" "$logfile~"
fi
if test -e /proc/self; then
exec 99> >(cat >"$logfile")
exec 1>&99
exec 2> >(tee -a /proc/self/fd/99 >&2)
else
exec 1>"$logfile"
exec 2>"$logfile"
warning "/proc not mounted"
fi
fi
echo "${0##*/} $VERSION started at $(date -R)" >&2
smt=$(rpm -q --qf '%{n}-%{v}-%{r}\n' module-init-tools) || \
smt=$(rpm -q --qf '%{n}-%{v}-%{r}\n' suse-module-tools)
check_rpm "$smt"
mkdir -p "$tmp/rpms"
found_kernel=false
for rpm in $(rpm -qa --qf '%{n}-%{v}-%{r}\n' 'kernel-*' '*-kmp-*' | \
/usr/lib/rpm/rpmsort); do
case "$rpm" in
kernel-source-* | kernel-syms-* | kernel-*-debug* | kernel-*-man-* | \
kernel-*-devel-* | kernel-firmware-* | kernel-coverage-* | \
kernel-docs-* | kernel-devel-* | kernel-macros-* | kernel-install-tools-*)
continue
esac
# store the filelist to speed up file_owner()
rpm -ql "$rpm" >"$tmp/rpms/$rpm"
check_rpm "$rpm"
case "$rpm" in
kernel-*)
check_kernel_package "$rpm"
found_kernel=true
;;
*-kmp-*)
check_kmp "$rpm"
;;
esac
done
if ! $found_kernel; then
warning "no kernel package found"
fi
for krel in /lib/modules/*/kernel; do
krel=${krel%/kernel}
krel=${krel##*/}
check_krel "$krel"
done
modules=($(find /lib/modules/ -name '*.ko' -o -name '*.ko.[gx]z' -o -name '*.ko.zst'))
for module in "${modules[@]}"; do
check_ko "$module"
done
echo "Found $errors error(s) and $warnings warning(s)" >&2
if test "$logfile" != -; then
echo "Report written to $logfile at $(date -R)" >&2
else
echo "Report finished at $(date -R)" >&2
fi
if test $errors -eq 0; then
exit 0
else
exit 1
fi